dolibarr  13.0.2
sync_members_ldap2dolibarr.php
Go to the documentation of this file.
1 #!/usr/bin/env php
2 <?php
27 if (!defined('NOSESSION')) define('NOSESSION', '1');
28 
29 $sapi_type = php_sapi_name();
30 $script_file = basename(__FILE__);
31 $path = __DIR__.'/';
32 
33 // Test if batch mode
34 if (substr($sapi_type, 0, 3) == 'cgi') {
35  echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
36  exit(-1);
37 }
38 
39 require_once $path."../../htdocs/master.inc.php";
40 require_once DOL_DOCUMENT_ROOT."/core/lib/date.lib.php";
41 require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
42 require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php";
43 require_once DOL_DOCUMENT_ROOT."/adherents/class/subscription.class.php";
44 
45 $langs->loadLangs(array("main", "errors"));
46 
47 // Global variables
48 $version = constant('DOL_VERSION');
49 $error = 0;
50 $forcecommit = 0;
51 $confirmed = 0;
52 
53 /*
54  * Main
55  */
56 
57 @set_time_limit(0);
58 print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
59 dol_syslog($script_file." launched with arg ".join(',', $argv));
60 
61 // List of fields to get from LDAP
62 $required_fields = array(
63  $conf->global->LDAP_KEY_MEMBERS,
64  $conf->global->LDAP_FIELD_FULLNAME,
65  $conf->global->LDAP_FIELD_LOGIN,
66  $conf->global->LDAP_FIELD_LOGIN_SAMBA,
67  $conf->global->LDAP_FIELD_PASSWORD,
68  $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
69  $conf->global->LDAP_FIELD_NAME,
70  $conf->global->LDAP_FIELD_FIRSTNAME,
71  $conf->global->LDAP_FIELD_MAIL,
72  $conf->global->LDAP_FIELD_PHONE,
73  $conf->global->LDAP_FIELD_PHONE_PERSO,
74  $conf->global->LDAP_FIELD_MOBILE,
75  $conf->global->LDAP_FIELD_FAX,
76  $conf->global->LDAP_FIELD_ADDRESS,
77  $conf->global->LDAP_FIELD_ZIP,
78  $conf->global->LDAP_FIELD_TOWN,
79  $conf->global->LDAP_FIELD_COUNTRY,
80  $conf->global->LDAP_FIELD_DESCRIPTION,
81  $conf->global->LDAP_FIELD_BIRTHDATE,
82  $conf->global->LDAP_FIELD_MEMBER_STATUS,
83  $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION,
84  // Subscriptions
85  $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE,
86  $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT,
87  $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE,
88  $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
89 );
90 
91 // Remove from required_fields all entries not configured in LDAP (empty) and duplicated
92 $required_fields = array_unique(array_values(array_filter($required_fields, "dolValidElement")));
93 
94 if (!isset($argv[2]) || !is_numeric($argv[2])) {
95  print "Usage: $script_file (nocommitiferror|commitiferror) id_member_type [--server=ldapserverhost] [-y]\n";
96  exit(-1);
97 }
98 
99 $typeid = $argv[2];
100 foreach ($argv as $key => $val) {
101  if ($val == 'commitiferror')
102  $forcecommit = 1;
103  if (preg_match('/--server=([^\s]+)$/', $val, $reg))
104  $conf->global->LDAP_SERVER_HOST = $reg[1];
105  if (preg_match('/-y$/', $val, $reg))
106  $confirmed = 1;
107 }
108 
109 print "Mails sending disabled (useless in batch mode)\n";
110 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
111 print "\n";
112 print "----- Synchronize all records from LDAP database:\n";
113 print "host=".$conf->global->LDAP_SERVER_HOST."\n";
114 print "port=".$conf->global->LDAP_SERVER_PORT."\n";
115 print "login=".$conf->global->LDAP_ADMIN_DN."\n";
116 print "pass=".preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)."\n";
117 print "DN to extract=".$conf->global->LDAP_MEMBER_DN."\n";
118 if (!empty($conf->global->LDAP_MEMBER_FILTER))
119  print 'Filter=('.$conf->global->LDAP_MEMBER_FILTER.')'."\n"; // Note: filter is defined into function getRecords
120 else print 'Filter=('.$conf->global->LDAP_KEY_MEMBERS.'=*)'."\n";
121 print "----- To Dolibarr database:\n";
122 print "type=".$conf->db->type."\n";
123 print "host=".$conf->db->host."\n";
124 print "port=".$conf->db->port."\n";
125 print "login=".$conf->db->user."\n";
126 print "database=".$conf->db->name."\n";
127 print "----- Options:\n";
128 print "commitiferror=".$forcecommit."\n";
129 print "Mapped LDAP fields=".join(',', $required_fields)."\n";
130 print "\n";
131 
132 // Check parameters
133 if (empty($conf->global->LDAP_MEMBER_DN)) {
134  print $langs->trans("Error").': '.$langs->trans("LDAP setup for members not defined inside Dolibarr")."\n";
135  exit(-1);
136 }
137 if ($typeid <= 0) {
138  print $langs->trans("Error").': Parameter id_member_type is not a valid ref of an existing member type'."\n";
139  exit(-2);
140 }
141 
142 if (!$confirmed) {
143  print "Hit Enter to continue or CTRL+C to stop...\n";
144  $input = trim(fgets(STDIN));
145 }
146 
147 // Load table of correspondence of countries
148 $hashlib2rowid = array();
149 $countries = array();
150 $sql = "SELECT rowid, code, label, active";
151 $sql .= " FROM ".MAIN_DB_PREFIX."c_country";
152 $sql .= " WHERE active = 1";
153 $sql .= " ORDER BY code ASC";
154 $resql = $db->query($sql);
155 if ($resql) {
156  $num = $db->num_rows($resql);
157  $i = 0;
158  if ($num) {
159  while ($i < $num) {
160  $obj = $db->fetch_object($resql);
161  if ($obj) {
162  // print 'Load cache for country '.strtolower($obj->label).' rowid='.$obj->rowid."\n";
163  $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
164  $countries[$obj->rowid] = array('rowid' => $obj->rowid, 'label' => $obj->label, 'code' => $obj->code);
165  }
166  $i++;
167  }
168  }
169 } else {
170  dol_print_error($db);
171  exit(-1);
172 }
173 
174 $ldap = new Ldap();
175 $result = $ldap->connect_bind();
176 if ($result >= 0) {
177  $justthese = array();
178 
179  // We disable synchro Dolibarr-LDAP
180  $conf->global->LDAP_MEMBER_ACTIVE = 0;
181 
182  $ldaprecords = $ldap->getRecords('*', $conf->global->LDAP_MEMBER_DN, $conf->global->LDAP_KEY_MEMBERS, $required_fields, 'member'); // Fiter on 'member' filter param
183  if (is_array($ldaprecords)) {
184  $db->begin();
185 
186  // Warning $ldapuser has a key in lowercase
187  foreach ($ldaprecords as $key => $ldapuser) {
188  $member = new Adherent($db);
189 
190  // Propriete membre
191  $member->firstname = $ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
192  $member->lastname = $ldapuser[$conf->global->LDAP_FIELD_NAME];
193  $member->login = $ldapuser[$conf->global->LDAP_FIELD_LOGIN];
194  $member->pass = $ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
195 
196  // $member->societe;
197  $member->address = $ldapuser[$conf->global->LDAP_FIELD_ADDRESS];
198  $member->zip = $ldapuser[$conf->global->LDAP_FIELD_ZIP];
199  $member->town = $ldapuser[$conf->global->LDAP_FIELD_TOWN];
200  $member->country = $ldapuser[$conf->global->LDAP_FIELD_COUNTRY];
201  $member->country_id = $countries[$hashlib2rowid[strtolower($member->country)]]['rowid'];
202  $member->country_code = $countries[$hashlib2rowid[strtolower($member->country)]]['code'];
203 
204  $member->phone = $ldapuser[$conf->global->LDAP_FIELD_PHONE];
205  $member->phone_perso = $ldapuser[$conf->global->LDAP_FIELD_PHONE_PERSO];
206  $member->phone_mobile = $ldapuser[$conf->global->LDAP_FIELD_MOBILE];
207  $member->email = $ldapuser[$conf->global->LDAP_FIELD_MAIL];
208 
209  $member->note = $ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
210  $member->morphy = 'phy';
211  $member->photo = '';
212  $member->public = 1;
213  $member->birth = dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_BIRTHDATE]);
214 
215  $member->statut = - 1;
216  if (isset($ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS])) {
217  $member->datec = dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
218  $member->datevalid = dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
219  $member->statut = $ldapuser[$conf->global->LDAP_FIELD_MEMBER_STATUS];
220  }
221  // if ($member->statut > 1) $member->statut=1;
222 
223  // print_r($ldapuser);
224 
225  // Propriete type membre
226  $member->typeid = $typeid;
227 
228  // Creation membre
229  print $langs->transnoentities("MemberCreate").' # '.$key.': login='.$member->login.', fullname='.$member->getFullName($langs);
230  print ', datec='.$member->datec;
231  $member_id = $member->create($user);
232  if ($member_id > 0) {
233  print ' --> Created member id='.$member_id.' login='.$member->login;
234  } else {
235  $error++;
236  print ' --> '.$member->error;
237  }
238  print "\n";
239 
240  // print_r($member);
241 
242  $datefirst = '';
243  if ($conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE) {
244  $datefirst = dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE]);
245  $pricefirst = price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT]);
246  }
247 
248  $datelast = '';
249  if ($conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE) {
250  $datelast = dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE]);
251  $pricelast = price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
252  } elseif ($conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION) {
253  $datelast = dol_time_plus_duree(dol_stringtotime($ldapuser[$conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION]), - 1, 'y') + 60 * 60 * 24;
254  $pricelast = price2num($ldapuser[$conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT]);
255 
256  // Cas special ou date derniere <= date premiere
257  if ($datefirst && $datelast && $datelast <= $datefirst) {
258  // On ne va inserer que la premiere
259  $datelast = 0;
260  if (!$pricefirst && $pricelast)
261  $pricefirst = $pricelast;
262  }
263  }
264 
265  // Insert first subscription
266  if ($datefirst) {
267  // Cree premiere cotisation et met a jour datefin dans adherent
268  // print "xx".$datefirst."\n";
269  $crowid = $member->subscription($datefirst, $pricefirst, 0);
270  }
271 
272  // Insert last subscription
273  if ($datelast) {
274  // Cree derniere cotisation et met a jour datefin dans adherent
275  // print "yy".dol_print_date($datelast)."\n";
276  $crowid = $member->subscription($datelast, $pricelast, 0);
277  }
278  }
279 
280  if (!$error || $forcecommit) {
281  if (!$error)
282  print $langs->transnoentities("NoErrorCommitIsDone")."\n";
283  else print $langs->transnoentities("ErrorButCommitIsDone")."\n";
284  $db->commit();
285  } else {
286  print $langs->transnoentities("ErrorSomeErrorWereFoundRollbackIsDone", $error)."\n";
287  $db->rollback();
288  }
289  print "\n";
290  } else {
291  dol_print_error('', $ldap->error);
292  $error++;
293  }
294 } else {
295  dol_print_error('', $ldap->error);
296  $error++;
297 }
298 
299 exit($error);
300 
301 
308 function dolValidElement($element)
309 {
310  return (trim($element) != '');
311 }
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
Definition: date.lib.php:319
dolValidElement($element)
Function to say if a value is empty or not.
price2num($amount, $rounding= '', $option=0)
Function that return a number with universal decimal format (decimal separator is &#39;...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage members of a foundation.
Class to manage LDAP features.
Definition: ldap.class.php:30
print
Draft customers invoices.
Definition: index.php:89
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_time_plus_duree($time, $duration_value, $duration_unit)
Add a delay to a date.
Definition: date.lib.php:114