27 if (!defined(
'NOSESSION')) define(
'NOSESSION',
'1');
29 $sapi_type = php_sapi_name();
30 $script_file = basename(__FILE__);
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";
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.
"/user/class/user.class.php";
44 $langs->loadLangs(array(
"main",
"errors"));
47 $version = DOL_VERSION;
50 $excludeuser = array();
59 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
62 $required_fields = array($conf->global->LDAP_KEY_USERS, $conf->global->LDAP_FIELD_FULLNAME, $conf->global->LDAP_FIELD_NAME, $conf->global->LDAP_FIELD_FIRSTNAME, $conf->global->LDAP_FIELD_LOGIN, $conf->global->LDAP_FIELD_LOGIN_SAMBA, $conf->global->LDAP_FIELD_PASSWORD, $conf->global->LDAP_FIELD_PASSWORD_CRYPTED, $conf->global->LDAP_FIELD_PHONE, $conf->global->LDAP_FIELD_FAX, $conf->global->LDAP_FIELD_MOBILE,
67 $conf->global->LDAP_FIELD_MAIL, $conf->global->LDAP_FIELD_TITLE, $conf->global->LDAP_FIELD_DESCRIPTION, $conf->global->LDAP_FIELD_SID);
70 $required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
72 if (!isset($argv[1])) {
73 print "Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
77 foreach ($argv as $key => $val) {
78 if ($val ==
'commitiferror')
80 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg))
81 $conf->global->LDAP_SERVER_HOST = $reg[1];
82 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg))
83 $excludeuser = explode(
',', $reg[1]);
84 if (preg_match(
'/-y$/', $val, $reg))
88 print "Mails sending disabled (useless in batch mode)\n";
89 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
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_USER_DN.
"\n";
97 if (!empty($conf->global->LDAP_FILTER_CONNECTION))
98 print 'Filter=('.$conf->global->LDAP_FILTER_CONNECTION.
')'.
"\n";
99 else print 'Filter=('.$conf->global->LDAP_KEY_USERS.
'=*)'.
"\n";
100 print "----- To Dolibarr database:\n";
101 print "type=".$conf->db->type.
"\n";
102 print "host=".$conf->db->host.
"\n";
103 print "port=".$conf->db->port.
"\n";
104 print "login=".$conf->db->user.
"\n";
105 print "database=".$conf->db->name.
"\n";
106 print "----- Options:\n";
107 print "commitiferror=".$forcecommit.
"\n";
108 print "excludeuser=".join(
',', $excludeuser).
"\n";
109 print "Mapped LDAP fields=".join(
',', $required_fields).
"\n";
113 print "Hit Enter to continue or CTRL+C to stop...\n";
114 $input = trim(fgets(STDIN));
117 if (empty($conf->global->LDAP_USER_DN)) {
118 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
123 $hashlib2rowid = array();
124 $countries = array();
125 $sql =
"SELECT rowid, code, label, active";
126 $sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
127 $sql .=
" WHERE active = 1";
128 $sql .=
" ORDER BY code ASC";
129 $resql = $db->query($sql);
131 $num = $db->num_rows(
$resql);
135 $obj = $db->fetch_object(
$resql);
138 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
139 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
150 $result = $ldap->connect_bind();
152 $justthese = array();
155 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
157 $ldaprecords = $ldap->getRecords(
'*', $conf->global->LDAP_USER_DN, $conf->global->LDAP_KEY_USERS, $required_fields,
'user');
158 if (is_array($ldaprecords)) {
162 foreach ($ldaprecords as $key => $ldapuser) {
164 if (in_array($ldapuser[$conf->global->LDAP_FIELD_LOGIN], $excludeuser)) {
165 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[$conf->global->LDAP_FIELD_LOGIN].
' --> Discarded'.
"\n";
169 $fuser =
new User($db);
171 if ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_SID) {
172 $fuser->fetch(
'',
'', $ldapuser[$conf->global->LDAP_KEY_USERS]);
173 } elseif ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_LOGIN) {
174 $fuser->fetch(
'', $ldapuser[$conf->global->LDAP_KEY_USERS]);
178 $fuser->firstname = $ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
179 $fuser->lastname = $ldapuser[$conf->global->LDAP_FIELD_NAME];
180 $fuser->login = $ldapuser[$conf->global->LDAP_FIELD_LOGIN];
181 $fuser->pass = $ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
182 $fuser->pass_indatabase_crypted = $ldapuser[$conf->global->LDAP_FIELD_PASSWORD_CRYPTED];
194 $fuser->office_phone = $ldapuser[$conf->global->LDAP_FIELD_PHONE];
195 $fuser->user_mobile = $ldapuser[$conf->global->LDAP_FIELD_MOBILE];
196 $fuser->office_fax = $ldapuser[$conf->global->LDAP_FIELD_FAX];
197 $fuser->email = $ldapuser[$conf->global->LDAP_FIELD_MAIL];
198 $fuser->ldap_sid = $ldapuser[$conf->global->LDAP_FIELD_SID];
200 $fuser->job = $ldapuser[$conf->global->LDAP_FIELD_TITLE];
201 $fuser->note = $ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
203 $fuser->societe_id = 0;
204 $fuser->contact_id = 0;
205 $fuser->fk_member = 0;
221 if ($fuser->id > 0) {
222 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
223 $res = $fuser->update($user);
227 print ' --> '.$res.
' '.$fuser->error;
229 print ' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
232 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
233 $res = $fuser->create($user);
236 print ' --> Created user id='.$fuser->id.
' login='.$fuser->login;
239 print ' --> '.$res.
' '.$fuser->error;
256 if (!$error || $forcecommit) {
258 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
259 else print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
262 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
286 return (trim($element) !=
'');
Class to manage Dolibarr users.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dolValidElement($element)
Function to say if a value is empty or not.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage LDAP features.
print
Draft customers invoices.
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.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...