Minor - Minor applying format

skala
Julio Montoya 13 years ago
parent a0174da749
commit cc410bf7db
  1. 286
      main/auth/external_login/ldap.inc.php

@ -1,12 +1,13 @@
<?php // External login module : LDAP <?php
// External login module : LDAP
/** /**
* This files is included by newUser.ldap.php and login.ldap.php * This files is included by newUser.ldap.php and login.ldap.php
* It implements the functions nedded by both files * It implements the functions nedded by both files
**/ * */
//Includes the configuration file //Includes the configuration file
require_once dirname(__FILE__).'/../../inc/global.inc.php'; require_once dirname(__FILE__) . '/../../inc/global.inc.php';
require_once dirname(__FILE__).'/../../inc/conf/auth.conf.php'; require_once dirname(__FILE__) . '/../../inc/conf/auth.conf.php';
/** /**
* Returns a transcoded and trimmed string * Returns a transcoded and trimmed string
@ -14,15 +15,14 @@ require_once dirname(__FILE__).'/../../inc/conf/auth.conf.php';
* @param string * @param string
* @return string * @return string
* @author ndiechburg <noel@cblue.be> * @author ndiechburg <noel@cblue.be>
**/ * */
function extldap_purify_string($string) { function extldap_purify_string($string) {
global $extldap_config; global $extldap_config;
if(isset($extldap_config['encoding'])) { if (isset($extldap_config['encoding'])) {
return trim(api_to_system_encoding($string, $extldap_config['encoding'])); return trim(api_to_system_encoding($string, $extldap_config['encoding']));
} } else {
else { return trim($string);
return trim($string); }
}
} }
/** /**
@ -30,46 +30,45 @@ function extldap_purify_string($string) {
* *
* @return resource ldap link identifier or false * @return resource ldap link identifier or false
* @author ndiechburg <noel@cblue.be> * @author ndiechburg <noel@cblue.be>
**/ * */
function extldap_connect() function extldap_connect() {
{ global $extldap_config;
global $extldap_config;
if (!is_array($extldap_config['host']))
if (!is_array($extldap_config['host'])) $extldap_config['host'] = array($extldap_config['host']);
$extldap_config['host'] = array($extldap_config['host']);
foreach ($extldap_config['host'] as $host) {
foreach($extldap_config['host'] as $host) { //Trying to connect
//Trying to connect if (isset($extldap_config['port'])) {
if (isset($extldap_config['port'])) { $ds = ldap_connect($host, $extldap_config['port']);
$ds = ldap_connect($host,$extldap_config['port']); } else {
} else { $ds = ldap_connect($host);
$ds = ldap_connect($host); }
if (!$ds) {
$port = isset($extldap_config['port']) ? $ldap_config['port'] : 389;
error_log('EXTLDAP ERROR : cannot connect to ' . $extldap_config['host'] . ':' . $port);
} else
break;
} }
if (!$ds) { if (!$ds) {
$port = isset($extldap_config['port']) ? $ldap_config['port'] : 389; error_log('EXTLDAP ERROR : no valid server found');
error_log('EXTLDAP ERROR : cannot connect to '.$extldap_config['host'].':'. $port); return false;
} else }
break; //Setting protocol version
} if (isset($extldap_config['protocol_version'])) {
if (!$ds) { if (!ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $extldap_config['protocol_version'])) {
error_log('EXTLDAP ERROR : no valid server found'); ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 2);
return false; }
}
//Setting protocol version
if (isset($extldap_config['protocol_version'])) {
if ( ! ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, $extldap_config['protocol_version'])) {
ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 2);
} }
}
//Setting protocol version //Setting protocol version
if (isset($extldap_config['referrals'])) { if (isset($extldap_config['referrals'])) {
if ( ! ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals'])) { if (!ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals'])) {
ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals']); ldap_set_option($ds, LDAP_OPT_REFERRALS, $extldap_config['referrals']);
}
} }
}
return $ds;
return $ds;
} }
/** /**
@ -80,59 +79,57 @@ function extldap_connect()
* Modified by hubert.borderiou@grenet.fr * Modified by hubert.borderiou@grenet.fr
* Add possibility to get user info from LDAP without check password (if CAS auth and LDAP profil update) * Add possibility to get user info from LDAP without check password (if CAS auth and LDAP profil update)
* *
**/ * */
function extldap_authenticate($username, $password, $in_auth_with_no_password=false) function extldap_authenticate($username, $password, $in_auth_with_no_password = false) {
{ global $extldap_config;
global $extldap_config;
if (empty($username) or empty($password)){ if (empty($username) or empty($password)) {
return false; return false;
} }
$ds = extldap_connect(); $ds = extldap_connect();
if (!$ds) { if (!$ds) {
return false; return false;
} }
//Connection as admin to search dn of user //Connection as admin to search dn of user
$ldapbind = @ldap_bind($ds, $extldap_config['admin_dn'], $extldap_config['admin_password']); $ldapbind = @ldap_bind($ds, $extldap_config['admin_dn'], $extldap_config['admin_password']);
if ($ldapbind === false){ if ($ldapbind === false) {
error_log('EXTLDAP ERROR : cannot connect with admin login/password'); error_log('EXTLDAP ERROR : cannot connect with admin login/password');
return false; return false;
} }
$user_search = extldap_get_user_search_string($username); $user_search = extldap_get_user_search_string($username);
//Search distinguish name of user //Search distinguish name of user
$sr = ldap_search($ds, $extldap_config['base_dn'], $user_search); $sr = ldap_search($ds, $extldap_config['base_dn'], $user_search);
if ( !$sr ){ if (!$sr) {
error_log('EXTLDAP ERROR : ldap_search('.$ds.', '.$extldap_config['base_dn'].", $user_search) failed"); error_log('EXTLDAP ERROR : ldap_search(' . $ds . ', ' . $extldap_config['base_dn'] . ", $user_search) failed");
return false; return false;
} }
$entries_count = ldap_count_entries($ds,$sr); $entries_count = ldap_count_entries($ds, $sr);
if ($entries_count > 1) { if ($entries_count > 1) {
error_log('EXTLDAP ERROR : more than one entry for that user ( ldap_search(ds, '.$extldap_config['base_dn'].", $user_search) )"); error_log('EXTLDAP ERROR : more than one entry for that user ( ldap_search(ds, ' . $extldap_config['base_dn'] . ", $user_search) )");
return false; return false;
} }
if ($entries_count < 1) { if ($entries_count < 1) {
error_log('EXTLDAP ERROR : No entry for that user ( ldap_search(ds, '.$extldap_config['base_dn'].", $user_search) )"); error_log('EXTLDAP ERROR : No entry for that user ( ldap_search(ds, ' . $extldap_config['base_dn'] . ", $user_search) )");
return false; return false;
} }
$users = ldap_get_entries($ds,$sr); $users = ldap_get_entries($ds, $sr);
$user = $users[0]; $user = $users[0];
// If we just want to have user info from LDAP and not to check password // If we just want to have user info from LDAP and not to check password
if ($in_auth_with_no_password) { if ($in_auth_with_no_password) {
return $user; return $user;
} }
//now we try to autenthicate the user in the ldap //now we try to autenthicate the user in the ldap
$ubind = @ldap_bind($ds, $user['dn'], $password); $ubind = @ldap_bind($ds, $user['dn'], $password);
if($ubind !== false){ if ($ubind !== false) {
return $user; return $user;
} } else {
else { error_log('EXTLDAP : Wrong password for ' . $user['dn']);
error_log('EXTLDAP : Wrong password for '.$user['dn']); return false;
return false; }
}
} }
/** /**
@ -143,87 +140,84 @@ function extldap_authenticate($username, $password, $in_auth_with_no_password=fa
* @param array correspondance array (if not set use extldap_user_correspondance declared in auth.conf.php * @param array correspondance array (if not set use extldap_user_correspondance declared in auth.conf.php
* @return array userinfo array * @return array userinfo array
* @author ndiechburg <noel@cblue.be> * @author ndiechburg <noel@cblue.be>
**/ * */
function extldap_get_chamilo_user($ldap_user, $cor = null) function extldap_get_chamilo_user($ldap_user, $cor = null) {
{ global $extldap_user_correspondance;
global $extldap_user_correspondance; if (is_null($cor)) {
if ( is_null($cor) ) { $cor = $extldap_user_correspondance;
$cor = $extldap_user_correspondance;
}
$chamilo_user =array();
foreach ($cor as $chamilo_field => $ldap_field) {
if (is_array($ldap_field)){
$chamilo_user[$chamilo_field] = extldap_get_chamilo_user($ldap_user, $ldap_field);
continue;
} }
switch ($ldap_field) { $chamilo_user = array();
case 'func': foreach ($cor as $chamilo_field => $ldap_field) {
$func = "extldap_get_$chamilo_field"; if (is_array($ldap_field)) {
if (function_exists($func)) { $chamilo_user[$chamilo_field] = extldap_get_chamilo_user($ldap_user, $ldap_field);
$chamilo_user[$chamilo_field] = extldap_purify_string($func($ldap_user)); continue;
} else { }
error_log("EXTLDAP WARNING : You forgot to declare $func");
} switch ($ldap_field) {
break; case 'func':
default: $func = "extldap_get_$chamilo_field";
//if string begins with "!", then this is a constant if (function_exists($func)) {
if($ldap_field[0] === '!' ){ $chamilo_user[$chamilo_field] = extldap_purify_string($func($ldap_user));
$chamilo_user[$chamilo_field] = trim($ldap_field, "!\t\n\r\0"); } else {
break; error_log("EXTLDAP WARNING : You forgot to declare $func");
} }
if ( isset($ldap_user[$ldap_field][0]) ) { break;
$chamilo_user[$chamilo_field] = extldap_purify_string($ldap_user[$ldap_field][0]); default:
} else { //if string begins with "!", then this is a constant
error_log('EXTLDAP WARNING : '.$ldap_field. '[0] field is not set in ldap array'); if ($ldap_field[0] === '!') {
$chamilo_user[$chamilo_field] = trim($ldap_field, "!\t\n\r\0");
} break;
break; }
if (isset($ldap_user[$ldap_field][0])) {
$chamilo_user[$chamilo_field] = extldap_purify_string($ldap_user[$ldap_field][0]);
} else {
error_log('EXTLDAP WARNING : ' . $ldap_field . '[0] field is not set in ldap array');
}
break;
}
} }
} return $chamilo_user;
return $chamilo_user;
} }
/** /**
* Please declare here all the function you use in extldap_user_correspondance * Please declare here all the function you use in extldap_user_correspondance
* All these functions must have an $ldap_user parameter. This parameter is the * All these functions must have an $ldap_user parameter. This parameter is the
* array returned by the ldap for the user * array returned by the ldap for the user
**/ * */
/** /**
* example function for email * example function for email
**/ * */
/* /*
function extldap_get_email($ldap_user){ function extldap_get_email($ldap_user){
return $ldap_user['cn'].$ldap['sn'].'@gmail.com'; return $ldap_user['cn'].$ldap['sn'].'@gmail.com';
} }
*/ */
function extldap_get_status($ldap_user){ function extldap_get_status($ldap_user) {
return STUDENT; return STUDENT;
} }
function extldap_get_admin($ldap_user){
function extldap_get_admin($ldap_user) {
return false; return false;
} }
/** /**
* return the string used to search a user in ldap * return the string used to search a user in ldap
* *
* @param string username * @param string username
* @return string the serach string * @return string the serach string
* @author ndiechburg <noel@cblue.be> * @author ndiechburg <noel@cblue.be>
**/ * */
function extldap_get_user_search_string($username) { function extldap_get_user_search_string($username) {
global $extldap_config; global $extldap_config;
// init // init
$filter = '('.$extldap_config['user_search'].')'; $filter = '(' . $extldap_config['user_search'] . ')';
// replacing %username% by the actual username // replacing %username% by the actual username
$filter = str_replace('%username%',$username,$filter); $filter = str_replace('%username%', $username, $filter);
// append a global filter if needed // append a global filter if needed
if (isset($extldap_config['filter']) && $extldap_config['filter'] != "") if (isset($extldap_config['filter']) && $extldap_config['filter'] != "")
$filter = '(&'.$filter.'('.$extldap_config['filter'].'))'; $filter = '(&' . $filter . '(' . $extldap_config['filter'] . '))';
return $filter; return $filter;
} }
Loading…
Cancel
Save