/scripts/members/sync_members_ldap2dolibarr.php

https://github.com/asterix14/dolibarr · PHP · 318 lines · 222 code · 49 blank · 47 comment · 32 complexity · 2d1b5174cc835c0d6dc9caa0db4b236d MD5 · raw file

  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  5. * Copyright (C) 2006-2010 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 2 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/members/sync_members_ldap2dolibarr.php
  22. * \ingroup ldap member
  23. * \brief Script de mise a jour des adherents dans Dolibarr depuis 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 ar usingr PH for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
  31. exit;
  32. }
  33. // Main
  34. $version='1.14';
  35. @set_time_limit(0);
  36. $error=0;
  37. $forcecommit=0;
  38. require_once($path."../../htdocs/master.inc.php");
  39. require_once(DOL_DOCUMENT_ROOT."/core/lib/date.lib.php");
  40. require_once(DOL_DOCUMENT_ROOT."/core/class/ldap.class.php");
  41. require_once(DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php");
  42. require_once(DOL_DOCUMENT_ROOT."/adherents/class/cotisation.class.php");
  43. $langs->load("main");
  44. if ($argv[2]) $conf->global->LDAP_SERVER_HOST=$argv[2];
  45. print "***** $script_file ($version) *****\n";
  46. if (! isset($argv[1]) || ! is_numeric($argv[1])) {
  47. print "Usage: $script_file id_member_type\n";
  48. exit;
  49. }
  50. $typeid=$argv[1];
  51. print "Mails sending disabled (useless in batch mode)\n";
  52. $conf->global->MAIN_DISABLE_ALL_MAILS=1; // On bloque les mails
  53. print "\n";
  54. print "----- Synchronize all records from LDAP database:\n";
  55. print "host=".$conf->global->LDAP_SERVER_HOST."\n";
  56. print "port=".$conf->global->LDAP_SERVER_PORT."\n";
  57. print "login=".$conf->global->LDAP_ADMIN_DN."\n";
  58. print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
  59. print "DN to extract=".$conf->global->LDAP_MEMBER_DN."\n";
  60. print 'Filter=('.$conf->global->LDAP_KEY_MEMBERS.'=*)'."\n";
  61. print "----- To Dolibarr database:\n";
  62. print "type=".$conf->db->type."\n";
  63. print "host=".$conf->db->host."\n";
  64. print "port=".$conf->db->port."\n";
  65. print "login=".$conf->db->user."\n";
  66. print "database=".$conf->db->name."\n";
  67. print "\n";
  68. print "Press a key to confirm...\n";
  69. $input = trim(fgets(STDIN));
  70. print "Warning, this operation may result in data loss if it failed.\n";
  71. print "Hit Enter to continue or CTRL+C to stop...\n";
  72. $input = trim(fgets(STDIN));
  73. if (! $conf->global->LDAP_MEMBER_DN)
  74. {
  75. print $langs->trans("Error").': '.$langs->trans("LDAP setup for members not defined inside Dolibarr");
  76. exit(1);
  77. }
  78. // Charge tableau de correspondance des pays
  79. $hashlib2rowid=array();
  80. $countries=array();
  81. $sql = "SELECT rowid, code, libelle, active";
  82. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  83. $sql.= " WHERE active = 1";
  84. $sql.= " ORDER BY code ASC";
  85. $resql=$db->query($sql);
  86. if ($resql)
  87. {
  88. $num = $db->num_rows($resql);
  89. $i = 0;
  90. if ($num)
  91. {
  92. while ($i < $num)
  93. {
  94. $obj = $db->fetch_object($resql);
  95. if ($obj)
  96. {
  97. //print 'Load cache for country '.strtolower($obj->libelle).' rowid='.$obj->rowid."\n";
  98. $hashlib2rowid[strtolower($obj->libelle)]=$obj->rowid;
  99. $countries[$obj->rowid]=array('rowid' => $obj->rowid, 'label' => $obj->libelle, 'code' => $obj->code);
  100. }
  101. $i++;
  102. }
  103. }
  104. }
  105. else
  106. {
  107. dol_print_error($db);
  108. exit;
  109. }
  110. $ldap = new Ldap();
  111. $result = $ldap->connect_bind();
  112. if ($result >= 0)
  113. {
  114. $justthese=array();
  115. // On d�sactive la synchro Dolibarr vers LDAP
  116. $conf->global->LDAP_MEMBER_ACTIVE=0;
  117. // Liste des champs a r�cup�rer de LDAP
  118. $required_fields = array(
  119. $conf->global->LDAP_FIELD_FULLNAME,
  120. $conf->global->LDAP_FIELD_LOGIN,
  121. $conf->global->LDAP_FIELD_LOGIN_SAMBA,
  122. $conf->global->LDAP_FIELD_PASSWORD,
  123. $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
  124. $conf->global->LDAP_FIELD_NAME,
  125. $conf->global->LDAP_FIELD_FIRSTNAME,
  126. $conf->global->LDAP_FIELD_MAIL,
  127. $conf->global->LDAP_FIELD_PHONE,
  128. $conf->global->LDAP_FIELD_PHONE_PERSO,
  129. $conf->global->LDAP_FIELD_MOBILE,
  130. $conf->global->LDAP_FIELD_FAX,
  131. $conf->global->LDAP_FIELD_ADDRESS,
  132. $conf->global->LDAP_FIELD_ZIP,
  133. $conf->global->LDAP_FIELD_TOWN,
  134. $conf->global->LDAP_FIELD_COUNTRY,
  135. $conf->global->LDAP_FIELD_DESCRIPTION,
  136. $conf->global->LDAP_FIELD_BIRTHDATE,
  137. $conf->global->LDAP_FIELD_MEMBER_STATUS,
  138. $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION,
  139. // Subscriptions
  140. $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE,
  141. $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT,
  142. $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE,
  143. $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
  144. );
  145. // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
  146. $required_fields=array_unique(array_values(array_filter($required_fields, "dolValidElement")));
  147. $ldaprecords = $ldap->getRecords('*',$conf->global->LDAP_MEMBER_DN, $conf->global->LDAP_KEY_MEMBERS, $required_fields, 0);
  148. if (is_array($ldaprecords))
  149. {
  150. $db->begin();
  151. // Warning $ldapuser has a key in lowercase
  152. foreach ($ldaprecords as $key => $ldapuser)
  153. {
  154. $member = new Adherent($db);
  155. // Propriete membre
  156. $member->prenom=$ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
  157. $member->nom=$ldapuser[$conf->global->LDAP_FIELD_NAME];
  158. $member->login=$ldapuser[$conf->global->LDAP_FIELD_LOGIN];
  159. $member->pass=$ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
  160. //$member->societe;
  161. $member->address=$ldapuser[$conf->global->LDAP_FIELD_ADDRESS];
  162. $member->adresse=$ldapuser[$conf->global->LDAP_FIELD_ADDRESS]; // TODO obsolete
  163. $member->cp=$ldapuser[$conf->global->LDAP_FIELD_ZIP];
  164. $member->ville=$ldapuser[$conf->global->LDAP_FIELD_TOWN];
  165. $member->pays=$ldapuser[$conf->global->LDAP_FIELD_COUNTRY]; // Pays en libelle
  166. $member->pays_id=$countries[$hashlib2rowid[strtolower($member->pays)]]['rowid'];
  167. $member->pays_code=$countries[$hashlib2rowid[strtolower($member->pays)]]['code'];
  168. $member->phone=$ldapuser[$conf->global->LDAP_FIELD_PHONE];
  169. $member->phone_perso=$ldapuser[$conf->global->LDAP_FIELD_PHONE_PERSO];
  170. $member->phone_mobile=$ldapuser[$conf->global->LDAP_FIELD_MOBILE];
  171. $member->email=$ldapuser[$conf->global->LDAP_FIELD_MAIL];
  172. $member->note=$ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
  173. $member->morphy='phy';
  174. $member->photo='';
  175. $member->public=1;
  176. $member->naiss=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_BIRTHDATE]);
  177. $member->statut=-1;
  178. if (isset($ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS]))
  179. {
  180. $member->datec=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  181. $member->datevalid=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  182. $member->statut=$ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS];
  183. }
  184. //if ($member->statut > 1) $member->statut=1;
  185. //print_r($ldapuser);
  186. // Propriete type membre
  187. $member->typeid=$typeid;
  188. // Creation membre
  189. print $langs->transnoentities("MemberCreate").' # '.$key.': login='.$member->login.', fullname='.$member->getFullName($langs);
  190. print ', datec='.$member->datec;
  191. $member_id=$member->create($user);
  192. if ($member_id > 0)
  193. {
  194. print ' --> Created member id='.$member_id.' login='.$member->login;
  195. }
  196. else
  197. {
  198. $error++;
  199. print ' --> '.$member->error;
  200. }
  201. print "\n";
  202. //print_r($member);
  203. $datefirst='';
  204. if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)
  205. {
  206. $datefirst=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  207. $pricefirst=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT]);
  208. }
  209. $datelast='';
  210. if ($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)
  211. {
  212. $datelast=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE]);
  213. $pricelast=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
  214. }
  215. elseif ($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)
  216. {
  217. $datelast=dol_time_plus_duree(dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION]),-1,'y')+60*60*24;
  218. $pricelast=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
  219. // Cas special ou date derniere <= date premiere
  220. if ($datefirst && $datelast && $datelast <= $datefirst)
  221. {
  222. // On ne va inserer que la premiere
  223. $datelast=0;
  224. if (! $pricefirst && $pricelast) $pricefirst = $pricelast;
  225. }
  226. }
  227. // Insert first subscription
  228. if ($datefirst)
  229. {
  230. // Cree premiere cotisation et met a jour datefin dans adherent
  231. //print "xx".$datefirst."\n";
  232. $crowid=$member->cotisation($datefirst, $pricefirst, 0);
  233. }
  234. // Insert last subscription
  235. if ($datelast)
  236. {
  237. // Cree derniere cotisation et met a jour datefin dans adherent
  238. //print "yy".dol_print_date($datelast)."\n";
  239. $crowid=$member->cotisation($datelast, $pricelast, 0);
  240. }
  241. }
  242. if (! $error || $forcecommit)
  243. {
  244. if (! $error) print $langs->transnoentities("NoErrorCommitIsDone")."\n";
  245. else print $langs->transnoentities("ErrorButCommitIsDone")."\n";
  246. $db->commit();
  247. }
  248. else
  249. {
  250. print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone",$error)."\n";
  251. $db->rollback();
  252. }
  253. }
  254. else
  255. {
  256. dol_print_error('',$ldap->error);
  257. $error++;
  258. }
  259. }
  260. else
  261. {
  262. dol_print_error('',$ldap->error);
  263. $error++;
  264. }
  265. return $error;
  266. function dolValidElement($element) {
  267. return (trim($element) != '');
  268. }
  269. ?>