27if (!defined(
'NOSESSION')) {
28 define(
'NOSESSION',
'1');
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
36if (substr($sapi_type, 0, 3) ==
'cgi') {
37 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
41require_once $path.
"../../htdocs/master.inc.php";
42require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
43require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
44require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
46$langs->loadLangs(array(
"main",
"errors"));
49$version = DOL_VERSION;
52$excludeuser = array();
60print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
61dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
64$required_fields = array(
65 $conf->global->LDAP_KEY_USERS,
66 $conf->global->LDAP_FIELD_FULLNAME,
67 $conf->global->LDAP_FIELD_NAME,
68 $conf->global->LDAP_FIELD_FIRSTNAME,
69 $conf->global->LDAP_FIELD_LOGIN,
70 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
71 $conf->global->LDAP_FIELD_PASSWORD,
72 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
73 $conf->global->LDAP_FIELD_PHONE,
74 $conf->global->LDAP_FIELD_FAX,
75 $conf->global->LDAP_FIELD_MOBILE,
80 $conf->global->LDAP_FIELD_MAIL,
81 $conf->global->LDAP_FIELD_TITLE,
82 $conf->global->LDAP_FIELD_DESCRIPTION,
83 $conf->global->LDAP_FIELD_SID
87$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
89if (!isset($argv[1])) {
90 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
94foreach ($argv as $key => $val) {
95 if ($val ==
'commitiferror') {
98 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
99 $conf->global->LDAP_SERVER_HOST = $reg[1];
101 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
102 $excludeuser = explode(
',', $reg[1]);
104 if (preg_match(
'/-y$/', $val, $reg)) {
109print
"Mails sending disabled (useless in batch mode)\n";
110$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
112print
"----- Synchronize all records from LDAP database:\n";
123print
"----- To Dolibarr database:\n";
124print
"type=".$conf->db->type.
"\n";
125print
"host=".$conf->db->host.
"\n";
126print
"port=".$conf->db->port.
"\n";
127print
"login=".$conf->db->user.
"\n";
128print
"database=".$conf->db->name.
"\n";
129print
"----- Options:\n";
130print
"commitiferror=".$forcecommit.
"\n";
131print
"excludeuser=".join(
',', $excludeuser).
"\n";
132print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
136 print
"Hit Enter to continue or CTRL+C to stop...\n";
137 $input = trim(fgets(STDIN));
141 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
146$hashlib2rowid = array();
148$sql =
"SELECT rowid, code, label, active";
149$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
150$sql .=
" WHERE active = 1";
151$sql .=
" ORDER BY code ASC";
152$resql = $db->query($sql);
154 $num = $db->num_rows($resql);
158 $obj = $db->fetch_object($resql);
161 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
162 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
173$result = $ldap->connect_bind();
175 $justthese = array();
178 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
181 if (is_array($ldaprecords)) {
185 foreach ($ldaprecords as $key => $ldapuser) {
188 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[
getDolGlobalString(
'LDAP_FIELD_LOGIN')].
' --> Discarded'.
"\n";
192 $fuser =
new User($db);
205 $fuser->pass_indatabase_crypted = $ldapuser[
getDolGlobalString(
'LDAP_FIELD_PASSWORD_CRYPTED')];
227 $fuser->contact_id = 0;
228 $fuser->fk_member = 0;
243 if ($fuser->id > 0) {
244 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
245 $res = $fuser->update($user);
249 print
' --> '.$res.
' '.$fuser->error;
251 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
254 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
255 $res = $fuser->create($user);
258 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
261 print
' --> '.$res.
' '.$fuser->error;
278 if (!$error || $forcecommit) {
280 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
282 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
286 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
310 return (trim($element) !=
'');
Class to manage LDAP features.
Class to manage Dolibarr users.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolValidElement($element)
Function to say if a value is empty or not.