@ -39,246 +39,6 @@ $this_section = SECTION_PLATFORM_ADMIN;
api_protect_admin_script();
/**
* Get the total number of users on the platform
* @see SortableTable#get_total_number_of_items()
*/
function get_ldap_users()
{
global $ldap_basedn, $ldap_host, $ldap_port, $ldap_rdn, $ldap_pass;
$keyword_firstname = trim(Database::escape_string($_GET['keyword_firstname']));
$keyword_lastname = trim(Database::escape_string($_GET['keyword_lastname']));
$keyword_username = trim(Database::escape_string($_GET['keyword_username']));
$keyword_type = Database::escape_string($_GET['keyword_type']);
$ldap_query=array();
if ($keyword_username != "") {
$ldap_query[]="(uid=".$keyword_username."*)";
} else if ($keyword_lastname!=""){
$ldap_query[]="(sn=".$keyword_lastname."*)";
if ($keyword_firstname!="") {
$ldap_query[]="(givenName=".$keyword_firstname."*)";
}
}
if ($keyword_type !="" & & $keyword_type !="all") {
$ldap_query[]="(eduPersonPrimaryAffiliation=".$keyword_type.")";
}
if (count($ldap_query)>1){
$str_query.="(& ";
foreach ($ldap_query as $query){
$str_query.=" $query";
}
$str_query.=" )";
} else {
$str_query=$ldap_query[0];
}
$ds = ldap_connect($ldap_host, $ldap_port);
ldap_set_version($ds);
if ($ds & & count($ldap_query)>0) {
$r = false;
$res = ldap_handle_bind($ds, $r);
//$sr = ldap_search($ds, "ou=test-ou,$ldap_basedn", $str_query);
$sr = ldap_search($ds, $ldap_basedn, $str_query);
//echo "Le nombre de resultats est : ".ldap_count_entries($ds,$sr)."< p > ";
$info = ldap_get_entries($ds, $sr);
return $info;
} else {
if (count($ldap_query)!=0)
Display :: display_error_message(get_lang('LDAPConnectionError'));
return array();
}
}
/**
* Get the total number of users on the platform
* @see SortableTable#get_total_number_of_items()
*/
function get_number_of_users()
{
$info = get_ldap_users();
if (count($info)>0)
return $info['count'];
else
return 0;
}
/**
* Get the users to display on the current page.
* @see SortableTable#get_table_data($from)
*/
function get_user_data($from, $number_of_items, $column, $direction)
{
$users = array();
if (isset($_GET['submit']))
{
$info = get_ldap_users();
if ($info['count']>0)
{
for ($key = 0; $key < $info["count"]; $key ++)
{
$user=array();
// Get uid from dn
//YW: this might be a variation between LDAP 2 and LDAP 3, but in LDAP 3, the uid is in
//the corresponding index of the array
//$dn_array=ldap_explode_dn($info[$key]["dn"],1);
//$user[] = $dn_array[0]; // uid is first key
//$user[] = $dn_array[0]; // uid is first key
$user[] = $info[$key]['uid'][0];
$user[] = $info[$key]['uid'][0];
$user[] = iconv('utf-8', api_get_setting('platform_charset'), $info[$key]['sn'][0]);
$user[] = iconv('utf-8', api_get_setting('platform_charset'), $info[$key]['givenname'][0]);
$user[] = $info[$key]['mail'][0];
$outab[] = $info[$key]['eduPersonPrimaryAffiliation'][0]; // Ici "student"
$users[] = $user;
}
}
else
{
Display :: display_error_message(get_lang('NoUser'));
}
}
return $users;
}
/**
* Build the modify-column of the table
* @param int $user_id The user id
* @param string $url_params
* @return string Some HTML-code with modify-buttons
*/
function modify_filter($user_id,$url_params, $row)
{
$url_params_id="id[]=".$row[0];
//$url_params_id="id=".$row[0];
$result .= '< a href = "ldap_users_list.php?action=add_user&user_id='.$user_id.'&id_session='.Security::remove_XSS($_GET['id_session']).'&'.$url_params_id.'&sec_token='.$_SESSION['sec_token'].'" onclick = "javascript:if(!confirm('." ' " . addslashes ( htmlentities ( get_lang ( " ConfirmYourChoice " ) ) ) . " ' " . ' ) ) return false ; " > < img src = "../img/add_user.gif" border = "0" style = "vertical-align: middle;" title = "'.get_lang('AddUsers').'" alt = "'.get_lang('AddUsers').'" / > < / a > ';
return $result;
}
function addLdapUser($login)
{
global $ldap_basedn, $ldap_host, $ldap_port, $ldap_rdn, $ldap_pass;
$ds = ldap_connect($ldap_host, $ldap_port);
ldap_set_version($ds);
if ($ds)
{
$str_query="(uid=".$login.")";
$r = false;
$res = ldap_handle_bind($ds, $r);
$sr = ldap_search($ds, $ldap_basedn, $str_query);
//echo "Le nombre de resultats est : ".ldap_count_entries($ds,$sr)."< p > ";
$info = ldap_get_entries($ds, $sr);
for ($key = 0; $key < $info['count']; $key ++)
{
$lastname = iconv('utf-8', api_get_setting('platform_charset'), $info[$key]['sn'][0]);
$firstname = iconv('utf-8', api_get_setting('platform_charset'), $info[$key]['givenname'][0]);
$email = $info[$key]['mail'][0];
// Get uid from dn
$dn_array=ldap_explode_dn($info[$key]['dn'],1);
$username = $dn_array[0]; // uid is first key
$outab[] = $info[$key]['edupersonprimaryaffiliation'][0]; // Ici "student"
//$val = ldap_get_values_len($ds, $entry, "userPassword");
//$val = ldap_get_values_len($ds, $info[$key], "userPassword");
//$password = $val[0];
// TODO the password, if encrypted at the source, will be encrypted twice, which makes it useless. Try to fix that.
$password = $info[$key]['userPassword'][0];
$structure=$info[$key]['edupersonprimaryorgunitdn'][0];
$array_structure=explode(",", $structure);
$array_val=explode("=", $array_structure[0]);
$etape=$array_val[1];
$array_val=explode("=", $array_structure[1]);
$annee=$array_val[1];
// Pour faciliter la gestion on ajoute le code "etape-annee"
$official_code=$etape."-".$annee;
$auth_source='ldap';
// Pas de date d'expiration d'etudiant (a recuperer par rapport au shadow expire LDAP)
$expiration_date='0000-00-00 00:00:00';
$active=1;
if(empty($status)){$status = 5;}
if(empty($phone)){$phone = '';}
if(empty($picture_uri)){$picture_uri = '';}
// Ajout de l'utilisateur
if (UserManager::is_username_available($username))
{
$user_id = UserManager::create_user($firstname,$lastname,$status,$email,$username,$password,$official_code,api_get_setting('platformLanguage'),$phone,$picture_uri,$auth_source,$expiration_date,$active);
}
else
{
$user = UserManager::get_user_info($username);
$user_id=$user['user_id'];
UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
}
}
}
else
{
Display :: display_error_message(get_lang('LDAPConnectionError'));
}
return $user_id;;
}
function addUserToSession($UserList, $id_session)
{
// Database Table Definitions
$tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
$tbl_session_rel_class = Database::get_main_table(TABLE_MAIN_SESSION_CLASS);
$tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
$tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
$tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
$tbl_user = Database::get_main_table(TABLE_MAIN_USER);
$tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
$tbl_class = Database::get_main_table(TABLE_MAIN_CLASS);
$tbl_class_user = Database::get_main_table(TABLE_MAIN_CLASS_USER);
// Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter a<EFBFBD> la session
$result=api_sql_query("SELECT course_code FROM $tbl_session_rel_course " .
"WHERE id_session='$id_session'",__FILE__,__LINE__);
$CourseList=array();
while($row=Database::fetch_array($result))
{
$CourseList[]=$row['course_code'];
}
foreach($CourseList as $enreg_course)
{
foreach($UserList as $enreg_user)
{
api_sql_query("INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')",__FILE__,__LINE__);
}
$sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_course_rel_user " .
"WHERE id_session='$id_session' AND course_code='$enreg_course'";
$rs = api_sql_query($sql, __FILE__, __LINE__);
list($nbr_users) = Database::fetch_array($rs);
api_sql_query("UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users " .
"WHERE id_session='$id_session' AND course_code='$enreg_course'",__FILE__,__LINE__);
}
foreach($UserList as $enreg_user)
{
api_sql_query("INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) " .
"VALUES('$id_session','$enreg_user')",__FILE__,__LINE__);
}
// On mets a jour le nombre d'utilisateurs dans la session
$sql = "SELECT COUNT(id_user) as nbUsers FROM $tbl_session_rel_user WHERE id_session='$id_session'";
$rs = api_sql_query($sql, __FILE__, __LINE__);
list($nbr_users) = Database::fetch_array($rs);
api_sql_query("UPDATE $tbl_session SET nbr_users=$nbr_users WHERE id='$id_session'",__FILE__,__LINE__);
}
/**
==============================================================================
INIT SECTION
@ -295,12 +55,14 @@ if ($_GET['action'] == "login_as" && isset ($login_as_user_id))
}
//if we already have a session id and a user...
/*
if (($_GET['action']=="add_user") & & ($_GET['id_session'] == strval(intval($_GET['id_session']))) & & $_GET['id_session']>0 ){
header('Location: ldap_import_students_to_session.php?id_session='.$_GET['id_session'].'&ldap_user='.$_GET['id']);
}
*/
$interbreadcrumb[] = array ("url" => 'index.php', "name" => get_lang('PlatformAdmin'));
$tool_name = get_lang('SearchAUser'). " - LDAP" ;
$tool_name = get_lang('SearchLD AP Users ');
//Display :: display_header($tool_name); //cannot display now as we need to redirect
//api_display_tool_title($tool_name);
@ -341,12 +103,12 @@ if (isset ($_GET['action']))
$UserList=array();
$userid_match_login = array();
foreach ($id as $user_id) {
$tmp = addLdapU ser($user_id);
$tmp = ldap_add_u ser($user_id);
$UserList[]= $tmp;
$userid_match_login[$tmp] = $user_id;
}
if (isset($_GET['id_session']) & & ($_GET['id_session'] == strval(intval($_GET['id_session']))) & & ($_GET['id_session']>0)) {
addUserToS ession($UserList, $_GET['id_session']);
ldap_add_user_to_s ession($UserList, $_GET['id_session']);
header('Location: resume_session.php?id_session='.$_GET['id_session']);
} else {
Display :: display_header($tool_name);
@ -416,7 +178,7 @@ if (isset ($_POST['action']))
{
if($user_id != $_user['user_id'])
{
$UserList[] = addLdapU ser($user_id);
$UserList[] = ldap_add_u ser($user_id);
}
}
if (isset($_GET['id_session']) & & (trim($_GET['id_session'])!=""))
@ -467,7 +229,7 @@ if (isset($_GET['id_session']))
// Create a sortable table with user-data
$parameters['sec_token'] = Security::get_token();
$table = new SortableTable('users', 'get_number_of_users', 'get_user_data',2);
$table = new SortableTable('users', 'ldap_ get_number_of_users', 'ldap_ get_user_data',2);
$table->set_additional_parameters($parameters);
$table->set_header(0, '', false);
$table->set_header(1, get_lang('LoginName'));