sync_members_ldap2dolibarr.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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 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/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 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."/adherents/class/adherent.class.php");
  37. require_once(DOL_DOCUMENT_ROOT."/adherents/class/cotisation.class.php");
  38. $langs->load("main");
  39. $langs->load("errors");
  40. // Global variables
  41. $version=DOL_VERSION;
  42. $error=0;
  43. $forcecommit=0;
  44. /*
  45. * Main
  46. */
  47. @set_time_limit(0);
  48. print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
  49. // List of fields to get from LDAP
  50. $required_fields = array(
  51. $conf->global->LDAP_KEY_MEMBERS,
  52. $conf->global->LDAP_FIELD_FULLNAME,
  53. $conf->global->LDAP_FIELD_LOGIN,
  54. $conf->global->LDAP_FIELD_LOGIN_SAMBA,
  55. $conf->global->LDAP_FIELD_PASSWORD,
  56. $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
  57. $conf->global->LDAP_FIELD_NAME,
  58. $conf->global->LDAP_FIELD_FIRSTNAME,
  59. $conf->global->LDAP_FIELD_MAIL,
  60. $conf->global->LDAP_FIELD_PHONE,
  61. $conf->global->LDAP_FIELD_PHONE_PERSO,
  62. $conf->global->LDAP_FIELD_MOBILE,
  63. $conf->global->LDAP_FIELD_FAX,
  64. $conf->global->LDAP_FIELD_ADDRESS,
  65. $conf->global->LDAP_FIELD_ZIP,
  66. $conf->global->LDAP_FIELD_TOWN,
  67. $conf->global->LDAP_FIELD_COUNTRY,
  68. $conf->global->LDAP_FIELD_DESCRIPTION,
  69. $conf->global->LDAP_FIELD_BIRTHDATE,
  70. $conf->global->LDAP_FIELD_MEMBER_STATUS,
  71. $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION,
  72. // Subscriptions
  73. $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE,
  74. $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT,
  75. $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE,
  76. $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
  77. );
  78. // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
  79. $required_fields=array_unique(array_values(array_filter($required_fields, "dolValidElement")));
  80. if ($argv[3]) $conf->global->LDAP_SERVER_HOST=$argv[2];
  81. print "***** $script_file ($version) *****\n";
  82. if (! isset($argv[2]) || ! is_numeric($argv[2])) {
  83. print "Usage: $script_file (nocommitiferror|commitiferror) id_member_type [ldapserverhost]\n";
  84. exit(-1);
  85. }
  86. $typeid=$argv[2];
  87. if ($argv[1] == 'commitiferror') $forcecommit=1;
  88. print "Mails sending disabled (useless in batch mode)\n";
  89. $conf->global->MAIN_DISABLE_ALL_MAILS=1; // On bloque les mails
  90. print "\n";
  91. print "----- Synchronize all records from LDAP database:\n";
  92. print "host=".$conf->global->LDAP_SERVER_HOST."\n";
  93. print "port=".$conf->global->LDAP_SERVER_PORT."\n";
  94. print "login=".$conf->global->LDAP_ADMIN_DN."\n";
  95. print "pass=".preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)."\n";
  96. print "DN to extract=".$conf->global->LDAP_MEMBER_DN."\n";
  97. print 'Filter=('.$conf->global->LDAP_KEY_MEMBERS.'=*)'."\n";
  98. print "----- To Dolibarr database:\n";
  99. print "type=".$conf->db->type."\n";
  100. print "host=".$conf->db->host."\n";
  101. print "port=".$conf->db->port."\n";
  102. print "login=".$conf->db->user."\n";
  103. print "database=".$conf->db->name."\n";
  104. print "----- Options:\n";
  105. print "commitiferror=".$forcecommit."\n";
  106. print "Mapped LDAP fields=".join(',',$required_fields)."\n";
  107. print "\n";
  108. // Check parameters
  109. if (empty($conf->global->LDAP_MEMBER_DN))
  110. {
  111. print $langs->trans("Error").': '.$langs->trans("LDAP setup for members not defined inside Dolibarr")."\n";
  112. exit(-1);
  113. }
  114. if ($typeid <= 0)
  115. {
  116. print $langs->trans("Error").': Parameter id_member_type is not a valid ref of an existing member type'."\n";
  117. exit(-2);
  118. }
  119. print "Press a key to confirm...";
  120. $input = trim(fgets(STDIN));
  121. print "Hit Enter to continue or CTRL+C to stop...\n";
  122. $input = trim(fgets(STDIN));
  123. // Charge tableau de correspondance des pays
  124. $hashlib2rowid=array();
  125. $countries=array();
  126. $sql = "SELECT rowid, code, libelle, active";
  127. $sql.= " FROM ".MAIN_DB_PREFIX."c_pays";
  128. $sql.= " WHERE active = 1";
  129. $sql.= " ORDER BY code ASC";
  130. $resql=$db->query($sql);
  131. if ($resql)
  132. {
  133. $num = $db->num_rows($resql);
  134. $i = 0;
  135. if ($num)
  136. {
  137. while ($i < $num)
  138. {
  139. $obj = $db->fetch_object($resql);
  140. if ($obj)
  141. {
  142. //print 'Load cache for country '.strtolower($obj->libelle).' rowid='.$obj->rowid."\n";
  143. $hashlib2rowid[strtolower($obj->libelle)]=$obj->rowid;
  144. $countries[$obj->rowid]=array('rowid' => $obj->rowid, 'label' => $obj->libelle, 'code' => $obj->code);
  145. }
  146. $i++;
  147. }
  148. }
  149. }
  150. else
  151. {
  152. dol_print_error($db);
  153. exit(-1);
  154. }
  155. $ldap = new Ldap();
  156. $result = $ldap->connect_bind();
  157. if ($result >= 0)
  158. {
  159. $justthese=array();
  160. // We disable synchro Dolibarr-LDAP
  161. $conf->global->LDAP_MEMBER_ACTIVE=0;
  162. $ldaprecords = $ldap->getRecords('*',$conf->global->LDAP_MEMBER_DN, $conf->global->LDAP_KEY_MEMBERS, $required_fields, 0);
  163. if (is_array($ldaprecords))
  164. {
  165. $db->begin();
  166. // Warning $ldapuser has a key in lowercase
  167. foreach ($ldaprecords as $key => $ldapuser)
  168. {
  169. $member = new Adherent($db);
  170. // Propriete membre
  171. $member->firstname=$ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
  172. $member->lastname=$ldapuser[$conf->global->LDAP_FIELD_NAME];
  173. $member->login=$ldapuser[$conf->global->LDAP_FIELD_LOGIN];
  174. $member->pass=$ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
  175. //$member->societe;
  176. $member->address=$ldapuser[$conf->global->LDAP_FIELD_ADDRESS];
  177. $member->zip=$ldapuser[$conf->global->LDAP_FIELD_ZIP];
  178. $member->town=$ldapuser[$conf->global->LDAP_FIELD_TOWN];
  179. $member->country=$ldapuser[$conf->global->LDAP_FIELD_COUNTRY];
  180. $member->country_id=$countries[$hashlib2rowid[strtolower($member->country)]]['rowid'];
  181. $member->country_code=$countries[$hashlib2rowid[strtolower($member->country)]]['code'];
  182. $member->phone=$ldapuser[$conf->global->LDAP_FIELD_PHONE];
  183. $member->phone_perso=$ldapuser[$conf->global->LDAP_FIELD_PHONE_PERSO];
  184. $member->phone_mobile=$ldapuser[$conf->global->LDAP_FIELD_MOBILE];
  185. $member->email=$ldapuser[$conf->global->LDAP_FIELD_MAIL];
  186. $member->note=$ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
  187. $member->morphy='phy';
  188. $member->photo='';
  189. $member->public=1;
  190. $member->birth=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_BIRTHDATE]);
  191. $member->statut=-1;
  192. if (isset($ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS]))
  193. {
  194. $member->datec=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  195. $member->datevalid=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  196. $member->statut=$ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS];
  197. }
  198. //if ($member->statut > 1) $member->statut=1;
  199. //print_r($ldapuser);
  200. // Propriete type membre
  201. $member->typeid=$typeid;
  202. // Creation membre
  203. print $langs->transnoentities("MemberCreate").' # '.$key.': login='.$member->login.', fullname='.$member->getFullName($langs);
  204. print ', datec='.$member->datec;
  205. $member_id=$member->create($user);
  206. if ($member_id > 0)
  207. {
  208. print ' --> Created member id='.$member_id.' login='.$member->login;
  209. }
  210. else
  211. {
  212. $error++;
  213. print ' --> '.$member->error;
  214. }
  215. print "\n";
  216. //print_r($member);
  217. $datefirst='';
  218. if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE)
  219. {
  220. $datefirst=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
  221. $pricefirst=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT]);
  222. }
  223. $datelast='';
  224. if ($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE)
  225. {
  226. $datelast=dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE]);
  227. $pricelast=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
  228. }
  229. elseif ($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION)
  230. {
  231. $datelast=dol_time_plus_duree(dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION]),-1,'y')+60*60*24;
  232. $pricelast=price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
  233. // Cas special ou date derniere <= date premiere
  234. if ($datefirst && $datelast && $datelast <= $datefirst)
  235. {
  236. // On ne va inserer que la premiere
  237. $datelast=0;
  238. if (! $pricefirst && $pricelast) $pricefirst = $pricelast;
  239. }
  240. }
  241. // Insert first subscription
  242. if ($datefirst)
  243. {
  244. // Cree premiere cotisation et met a jour datefin dans adherent
  245. //print "xx".$datefirst."\n";
  246. $crowid=$member->cotisation($datefirst, $pricefirst, 0);
  247. }
  248. // Insert last subscription
  249. if ($datelast)
  250. {
  251. // Cree derniere cotisation et met a jour datefin dans adherent
  252. //print "yy".dol_print_date($datelast)."\n";
  253. $crowid=$member->cotisation($datelast, $pricelast, 0);
  254. }
  255. }
  256. if (! $error || $forcecommit)
  257. {
  258. if (! $error) print $langs->transnoentities("NoErrorCommitIsDone")."\n";
  259. else print $langs->transnoentities("ErrorButCommitIsDone")."\n";
  260. $db->commit();
  261. }
  262. else
  263. {
  264. print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone",$error)."\n";
  265. $db->rollback();
  266. }
  267. print "\n";
  268. }
  269. else
  270. {
  271. dol_print_error('',$ldap->error);
  272. $error++;
  273. }
  274. }
  275. else
  276. {
  277. dol_print_error('',$ldap->error);
  278. $error++;
  279. }
  280. exit($error);
  281. /**
  282. * Function to say if a value is empty or not
  283. *
  284. * @param string $element Value to test
  285. * @return boolean True of false
  286. */
  287. function dolValidElement($element)
  288. {
  289. return (trim($element) != '');
  290. }
  291. ?>