ldap_synchro.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. /* For licensing terms, see /license.txt */
  3. /**
  4. * @package chamilo.admin
  5. */
  6. /**
  7. * Code
  8. */
  9. exit(); //not yet functional, needs to be revised
  10. // name of the language file that needs to be included
  11. $language_file='admin';
  12. $cidReset=true;
  13. require('../inc/global.inc.php');
  14. $libpath = api_get_path(LIBRARY_PATH);
  15. require_once($libpath.'formvalidator/FormValidator.class.php');
  16. require_once($libpath.'usermanager.lib.php');
  17. require_once('../auth/ldap/authldap.php');
  18. $annee_base=date('Y');
  19. // setting the section (for the tabs)
  20. $this_section = SECTION_PLATFORM_ADMIN;
  21. //api_protect_admin_script(); // on vire la secu... qui n'a pas lieu d'etre ici (script de synchro)
  22. // setting breadcrumbs
  23. $interbreadcrumb[]=array('url' => 'index.php',"name" => get_lang('PlatformAdmin'));
  24. $interbreadcrumb[]=array('url' => api_get_self(),"name" => "Liste des sessions");
  25. // Database Table Definitions
  26. $tbl_session = Database::get_main_table(TABLE_MAIN_SESSION);
  27. $tbl_session_rel_class = Database::get_main_table(TABLE_MAIN_SESSION_CLASS);
  28. $tbl_session_rel_course = Database::get_main_table(TABLE_MAIN_SESSION_COURSE);
  29. $tbl_session_rel_course_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_COURSE_USER);
  30. $tbl_course = Database::get_main_table(TABLE_MAIN_COURSE);
  31. $tbl_user = Database::get_main_table(TABLE_MAIN_USER);
  32. $tbl_session_rel_user = Database::get_main_table(TABLE_MAIN_SESSION_USER);
  33. $tbl_class = Database::get_main_table(TABLE_MAIN_CLASS);
  34. $tbl_class_user = Database::get_main_table(TABLE_MAIN_CLASS_USER);
  35. $tbl_session_rel_etape = "session_rel_etape";
  36. $message="";
  37. $result=Database::query("SELECT id, name FROM $tbl_session");
  38. $Sessions=Database::store_result($result);
  39. $result=Database::query($sql);
  40. $users=Database::store_result($result);
  41. foreach($Sessions as $session){
  42. $id_session = $session['id'];
  43. $name_session = $session['name'];
  44. $UserList=array();
  45. $UserUpdate=array();
  46. $UserAdd=array();
  47. // Parse des code etape de la session
  48. /*
  49. $sql = "SELECT id_session, code_etape, etape_description, code_ufr, annee
  50. FROM $tbl_session_rel_etape
  51. WHERE id_session='$id_session'
  52. ORDER BY code_ufr, code_etape";
  53. $result = Database::query($sql);
  54. */
  55. $ds = ldap_connect($ldap_host, $ldap_port) or die(get_lang('LDAPConnectionError'));
  56. ldap_set_version($ds);
  57. // Import des utilisateurs des etapes dans la session
  58. if ($ds)
  59. {
  60. $r = false;
  61. $res = ldap_handle_bind($ds, $r);
  62. $UserList=array();
  63. if($result !== false)
  64. {
  65. //while($row = Database::fetch_array($result))
  66. //{
  67. /*
  68. $annee = $row['annee'];
  69. $code_ufr = $row['code_ufr'];
  70. $etape = $row['code_etape'];
  71. */
  72. // LDAP Query
  73. // edupersonorgunitdn=ou=12CI1,ou=2006,ou=diploma,o=Paris1,dc=univ-paris1,dc=fr
  74. //etapescommented
  75. //$sr = @ ldap_search($ds, "ou=people,$LDAPbasedn", "edupersonorgunitdn=ou=$etape,ou=$annee,ou=diploma,$LDAPbasedn");
  76. $sr = @ ldap_search($ds, $ldap_basedn, '(uid=*)');
  77. $info = ldap_get_entries($ds, $sr);
  78. for ($key = 0; $key < $info["count"]; $key ++)
  79. {
  80. echo "<pre>";
  81. print_r($info[$key]);
  82. echo "</pre>";
  83. $lastname = api_utf8_decode($info[$key]["sn"][0], api_get_system_encoding());
  84. $firstname = api_utf8_decode($info[$key]["givenname"][0], api_get_system_encoding());
  85. $email = $info[$key]["mail"][0];
  86. // Get uid from dn
  87. $dn_array=ldap_explode_dn($info[$key]["dn"],1);
  88. $username = $dn_array[0]; // uid is first key
  89. $outab[] = $info[$key]["edupersonprimaryaffiliation"][0]; // Ici "student"
  90. $val = ldap_get_values_len($ds, $sr, "userPassword");
  91. $password = $val[0];
  92. // Pour faciliter la gestion on ajoute le code "etape-annee"
  93. $official_code=$etape."-".$annee;
  94. $auth_source="ldap";
  95. // Pas de date d'expiration d'etudiant (a recuperer par rapport au shadow expire LDAP)
  96. $expiration_date='0000-00-00 00:00:00';
  97. $active=1;
  98. // Ajout de l'utilisateur
  99. if (UserManager::is_username_available($username)) {
  100. $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);
  101. $UserAdd[]=$user_id;
  102. } else {
  103. $user = UserManager::get_user_info($username);
  104. $user_id=$user['user_id'];
  105. UserManager::update_user($user_id, $firstname, $lastname, $username, null, null, $email, $status, $official_code, $phone, $picture_uri, $expiration_date, $active);
  106. $UserUpdate[]=$user_id;
  107. }
  108. $UserList[]=$user_id;
  109. }
  110. //}
  111. }
  112. if (isset($included) && ($included))
  113. {
  114. $message .= "> $name_session: ".count($UserAdd)." ".get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified').'<br/>';
  115. }
  116. else
  117. {
  118. print "> $name_session: ".count($UserAdd).get_lang('Added').' '.get_lang('And').' '.count($UserUpdate).' '.get_lang('Modified')."\n";
  119. }
  120. // Une fois les utilisateurs importer dans la base des utilisateurs, on peux les affecter a� la session
  121. $result=Database::query("SELECT course_code FROM $tbl_session_rel_course " .
  122. "WHERE id_session='$id_session'");
  123. $CourseList=array();
  124. while($row=Database::fetch_array($result))
  125. {
  126. $CourseList[]=$row['course_code'];
  127. }
  128. foreach($CourseList as $enreg_course)
  129. {
  130. // On ajoute la relation entre l'utilisateur et le cours
  131. foreach($UserList as $enreg_user)
  132. {
  133. Database::query("INSERT IGNORE INTO $tbl_session_rel_course_rel_user(id_session,course_code,id_user) VALUES('$id_session','$enreg_course','$enreg_user')");
  134. }
  135. $sql = "SELECT COUNT(id_user) as nbUsers " .
  136. "FROM $tbl_session_rel_course_rel_user " .
  137. "WHERE id_session='$id_session' AND course_code='$enreg_course'";
  138. $rs = Database::query($sql);
  139. list($nbr_users) = Database::fetch_array($rs);
  140. $sql = "UPDATE $tbl_session_rel_course SET nbr_users=$nbr_users WHERE id_session='$id_session' AND course_code='$enreg_course'";
  141. Database::query($sql);
  142. }
  143. // On ajoute la relation entre l'utilisateur et la session
  144. foreach($UserList as $enreg_user){
  145. $sql = "INSERT IGNORE INTO $tbl_session_rel_user(id_session, id_user) " .
  146. "VALUES('$id_session','$enreg_user')";
  147. Database::query($sql);
  148. }
  149. $sql = "SELECT COUNT(id_user) as nbUsers " .
  150. "FROM $tbl_session_rel_user " .
  151. "WHERE id_session='$id_session' AND relation_type<>".SESSION_RELATION_TYPE_RRHH."";
  152. $rs = Database::query($sql);
  153. list($nbr_users) = Database::fetch_array($rs);
  154. $sql = "UPDATE $tbl_session SET nbr_users=$nbr_users WHERE id='$id_session'";
  155. Database::query($sql);
  156. }
  157. }
  158. ?>