sync_users_ldap2dolibarr.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file scripts/user/sync_users_ldap2dolibarr.php
  22. * \ingroup ldap member
  23. * \brief Script to update users into Dolibarr from LDAP
  24. */
  25. $sapi_type = php_sapi_name();
  26. $script_file = basename(__FILE__);
  27. $path=dirname(__FILE__).'/';
  28. // Test if batch mode
  29. if (substr($sapi_type, 0, 3) == 'cgi') {
  30. echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit(-1);
  32. }
  33. require_once($path."../../htdocs/master.inc.php");
  34. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  35. require_once(DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  36. require_once(DOL_DOCUMENT_ROOT."/user/class/user.class.php");
  37. $langs->load("main");
  38. $langs->load("errors");
  39. // Global variables
  40. $version=DOL_VERSION;
  41. $error=0;
  42. $forcecommit=0;
  43. /*
  44. * Main
  45. */
  46. @set_time_limit(0);
  47. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  48. // List of fields to get from LDAP
  49. $required_fields = array(
  50. $conf->global->LDAP_KEY_USERS,
  51. $conf->global->LDAP_FIELD_FULLNAME,
  52. $conf->global->LDAP_FIELD_NAME,
  53. $conf->global->LDAP_FIELD_FIRSTNAME,
  54. $conf->global->LDAP_FIELD_LOGIN,
  55. $conf->global->LDAP_FIELD_LOGIN_SAMBA,
  56. $conf->global->LDAP_FIELD_PASSWORD,
  57. $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
  58. $conf->global->LDAP_FIELD_PHONE,
  59. $conf->global->LDAP_FIELD_FAX,
  60. $conf->global->LDAP_FIELD_MOBILE,
  61. //$conf->global->LDAP_FIELD_ADDRESS,
  62. //$conf->global->LDAP_FIELD_ZIP,
  63. //$conf->global->LDAP_FIELD_TOWN,
  64. //$conf->global->LDAP_FIELD_COUNTRY,
  65. $conf->global->LDAP_FIELD_MAIL,
  66. $conf->global->LDAP_FIELD_TITLE,
  67. $conf->global->LDAP_FIELD_DESCRIPTION,
  68. $conf->global->LDAP_FIELD_SID
  69. );
  70. // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
  71. $required_fields=array_unique(array_values(array_filter($required_fields, "dolValidElement")));
  72. if ($argv[2]) $conf->global->LDAP_SERVER_HOST=$argv[2];
  73. print "***** $script_file ($version) *****\n";
  74. if (! isset($argv[1])) {
  75. //print "Usage: $script_file (nocommitiferror|commitiferror) [id_group]\n";
  76. print "Usage: $script_file (nocommitiferror|commitiferror) [ldapserverhost]\n";
  77. exit(-1);
  78. }
  79. $groupid=$argv[3];
  80. if ($argv[1] == 'commitiferror') $forcecommit=1;
  81. print "Mails sending disabled (useless in batch mode)\n";
  82. $conf->global->MAIN_DISABLE_ALL_MAILS=1; // On bloque les mails
  83. print "\n";
  84. print "----- Synchronize all records from LDAP database:\n";
  85. print "host=".$conf->global->LDAP_SERVER_HOST."\n";
  86. print "port=".$conf->global->LDAP_SERVER_PORT."\n";
  87. print "login=".$conf->global->LDAP_ADMIN_DN."\n";
  88. print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
  89. print "DN to extract=".$conf->global->LDAP_USER_DN."\n";
  90. print 'Filter=('.$conf->global->LDAP_KEY_USERS.'=*)'."\n";
  91. print "----- To Dolibarr database:\n";
  92. print "type=".$conf->db->type."\n";
  93. print "host=".$conf->db->host."\n";
  94. print "port=".$conf->db->port."\n";
  95. print "login=".$conf->db->user."\n";
  96. print "database=".$conf->db->name."\n";
  97. print "----- Options:\n";
  98. print "commitiferror=".$forcecommit."\n";
  99. print "Mapped LDAP fields=".join(',',$required_fields)."\n";
  100. print "\n";
  101. print "Press a key to confirm...";
  102. $input = trim(fgets(STDIN));
  103. print "Hit Enter to continue or CTRL+C to stop...\n";
  104. $input = trim(fgets(STDIN));
  105. if (empty($conf->global->LDAP_USER_DN))
  106. {
  107. print $langs->trans("Error").': '.$langs->trans("LDAP setup for users not defined inside Dolibarr");
  108. exit(-1);
  109. }
  110. // Charge tableau de correspondance des pays
  111. $hashlib2rowid=array();
  112. $countries=array();
  113. $sql = "SELECT rowid, code, libelle, active";
  114. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  115. $sql.= " WHERE active = 1";
  116. $sql.= " ORDER BY code ASC";
  117. $resql=$db->query($sql);
  118. if ($resql)
  119. {
  120. $num = $db->num_rows($resql);
  121. $i = 0;
  122. if ($num)
  123. {
  124. while ($i < $num)
  125. {
  126. $obj = $db->fetch_object($resql);
  127. if ($obj)
  128. {
  129. //print 'Load cache for country '.strtolower($obj->libelle).' rowid='.$obj->rowid."\n";
  130. $hashlib2rowid[strtolower($obj->libelle)]=$obj->rowid;
  131. $countries[$obj->rowid]=array('rowid' => $obj->rowid, 'label' => $obj->libelle, 'code' => $obj->code);
  132. }
  133. $i++;
  134. }
  135. }
  136. }
  137. else
  138. {
  139. dol_print_error($db);
  140. exit(-1);
  141. }
  142. $ldap = new Ldap();
  143. $result = $ldap->connect_bind();
  144. if ($result >= 0)
  145. {
  146. $justthese=array();
  147. // We disable synchro Dolibarr-LDAP
  148. $conf->global->LDAP_SYNCHRO_ACTIVE=0;
  149. $ldaprecords = $ldap->getRecords('*',$conf->global->LDAP_USER_DN, $conf->global->LDAP_KEY_USERS, $required_fields, 0);
  150. if (is_array($ldaprecords))
  151. {
  152. $db->begin();
  153. // Warning $ldapuser has a key in lowercase
  154. foreach ($ldaprecords as $key => $ldapuser)
  155. {
  156. $fuser = new User($db);
  157. if($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_SID) {
  158. $fuser->fetch('','',$ldapuser[$conf->global->LDAP_KEY_USERS]); // Chargement du user concerné par le SID
  159. } else if($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_LOGIN) {
  160. $fuser->fetch('',$ldapuser[$conf->global->LDAP_KEY_USERS]); // Chargement du user concerné par le login
  161. }
  162. // Propriete membre
  163. $fuser->firstname=$ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
  164. $fuser->lastname=$ldapuser[$conf->global->LDAP_FIELD_NAME];
  165. $fuser->login=$ldapuser[$conf->global->LDAP_FIELD_LOGIN];
  166. $fuser->pass=$ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
  167. $fuser->pass_indatabase_crypted=$ldapuser[$conf->global->LDAP_FIELD_PASSWORD_CRYPTED];
  168. //$user->societe;
  169. /*
  170. $fuser->address=$ldapuser[$conf->global->LDAP_FIELD_ADDRESS];
  171. $fuser->zip=$ldapuser[$conf->global->LDAP_FIELD_ZIP];
  172. $fuser->town=$ldapuser[$conf->global->LDAP_FIELD_TOWN];
  173. $fuser->country=$ldapuser[$conf->global->LDAP_FIELD_COUNTRY];
  174. $fuser->country_id=$countries[$hashlib2rowid[strtolower($fuser->country)]]['rowid'];
  175. $fuser->country_code=$countries[$hashlib2rowid[strtolower($fuser->country)]]['code'];
  176. */
  177. $fuser->office_phone=$ldapuser[$conf->global->LDAP_FIELD_PHONE];
  178. $fuser->user_mobile=$ldapuser[$conf->global->LDAP_FIELD_MOBILE];
  179. $fuser->office_fax=$ldapuser[$conf->global->LDAP_FIELD_FAX];
  180. $fuser->email=$ldapuser[$conf->global->LDAP_FIELD_MAIL];
  181. $fuser->ldap_sid=$ldapuser[$conf->global->LDAP_FIELD_SID];
  182. $fuser->job=$ldapuser[$conf->global->LDAP_FIELD_TITLE];
  183. $fuser->note=$ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
  184. $fuser->admin=0;
  185. $fuser->societe_id=0;
  186. $fuser->contact_id=0;
  187. $fuser->fk_member=0;
  188. $fuser->statut=1;
  189. // TODO : revoir la gestion du status
  190. /*if (isset($ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS]))
  191. {
  192. $fuser->datec=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  193. $fuser->datevalid=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  194. $fuser->statut=$ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS];
  195. }*/
  196. //if ($fuser->statut > 1) $fuser->statut=1;
  197. //print_r($ldapuser);
  198. if($fuser->id > 0) { // User update
  199. print $langs->transnoentities("UserUpdate").' # '.$key.': login='.$fuser->login.', fullname='.$fuser->getFullName($langs);
  200. $res=$fuser->update($user);
  201. if ($res < 0)
  202. {
  203. $error++;
  204. print ' --> '.$res.' '.$fuser->error;
  205. }
  206. else
  207. {
  208. print ' --> Updated user id='.$fuser->id.' login='.$fuser->login;
  209. }
  210. } else { // User creation
  211. print $langs->transnoentities("UserCreate").' # '.$key.': login='.$fuser->login.', fullname='.$fuser->getFullName($langs);
  212. $res=$fuser->create($user);
  213. if ($res > 0)
  214. {
  215. print ' --> Created user id='.$fuser->id.' login='.$fuser->login;
  216. }
  217. else
  218. {
  219. $error++;
  220. print ' --> '.$res.' '.$fuser->error;
  221. }
  222. }
  223. print "\n";
  224. //print_r($fuser);
  225. // Gestion des groupes
  226. // TODO : revoir la gestion des groupes (ou script de sync groupes)
  227. /*if(!$error) {
  228. foreach ($ldapuser[$conf->global->LDAP_FIELD_USERGROUPS] as $groupdn) {
  229. $groupdn;
  230. }
  231. }*/
  232. }
  233. if (! $error || $forcecommit)
  234. {
  235. if (! $error) print $langs->transnoentities("NoErrorCommitIsDone")."\n";
  236. else print $langs->transnoentities("ErrorButCommitIsDone")."\n";
  237. $db->commit();
  238. }
  239. else
  240. {
  241. print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone",$error)."\n";
  242. $db->rollback();
  243. }
  244. print "\n";
  245. }
  246. else
  247. {
  248. dol_print_error('',$ldap->error);
  249. $error++;
  250. }
  251. }
  252. else
  253. {
  254. dol_print_error('',$ldap->error);
  255. $error++;
  256. }
  257. exit($error);
  258. /**
  259. * Function to say if a value is empty or not
  260. *
  261. * @param string $element Value to test
  262. * @return boolean True of false
  263. */
  264. function dolValidElement($element)
  265. {
  266. return (trim($element) != '');
  267. }
  268. ?>