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();
55$hookmanager->initHooks(array(
'cli'));
63print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
64dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
67$required_fields = array(
68 $conf->global->LDAP_KEY_USERS,
69 $conf->global->LDAP_FIELD_FULLNAME,
70 $conf->global->LDAP_FIELD_NAME,
71 $conf->global->LDAP_FIELD_FIRSTNAME,
72 $conf->global->LDAP_FIELD_LOGIN,
73 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
74 $conf->global->LDAP_FIELD_PASSWORD,
75 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
76 $conf->global->LDAP_FIELD_PHONE,
77 $conf->global->LDAP_FIELD_FAX,
78 $conf->global->LDAP_FIELD_MOBILE,
83 $conf->global->LDAP_FIELD_MAIL,
84 $conf->global->LDAP_FIELD_TITLE,
85 $conf->global->LDAP_FIELD_DESCRIPTION,
86 $conf->global->LDAP_FIELD_SID
90$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
92if (!isset($argv[1])) {
93 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
97foreach ($argv as $key => $val) {
98 if ($val ==
'commitiferror') {
101 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
102 $conf->global->LDAP_SERVER_HOST = $reg[1];
104 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
105 $excludeuser = explode(
',', $reg[1]);
107 if (preg_match(
'/-y$/', $val, $reg)) {
112print
"Mails sending disabled (useless in batch mode)\n";
113$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
115print
"----- Synchronize all records from LDAP database:\n";
126print
"----- To Dolibarr database:\n";
127print
"type=".$conf->db->type.
"\n";
128print
"host=".$conf->db->host.
"\n";
129print
"port=".$conf->db->port.
"\n";
130print
"login=".$conf->db->user.
"\n";
131print
"database=".$conf->db->name.
"\n";
132print
"----- Options:\n";
133print
"commitiferror=".$forcecommit.
"\n";
134print
"excludeuser=".join(
',', $excludeuser).
"\n";
135print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
139 print
"Hit Enter to continue or CTRL+C to stop...\n";
140 $input = trim(fgets(STDIN));
144 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
149$hashlib2rowid = array();
151$sql =
"SELECT rowid, code, label, active";
152$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
153$sql .=
" WHERE active = 1";
154$sql .=
" ORDER BY code ASC";
155$resql = $db->query($sql);
157 $num = $db->num_rows($resql);
161 $obj = $db->fetch_object($resql);
164 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
165 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
176$result = $ldap->connectBind();
178 $justthese = array();
181 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
184 if (is_array($ldaprecords)) {
188 foreach ($ldaprecords as $key => $ldapuser) {
191 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[
getDolGlobalString(
'LDAP_FIELD_LOGIN')].
' --> Discarded'.
"\n";
195 $fuser =
new User($db);
208 $fuser->pass_indatabase_crypted = $ldapuser[
getDolGlobalString(
'LDAP_FIELD_PASSWORD_CRYPTED')];
230 $fuser->contact_id = 0;
231 $fuser->fk_member = 0;
246 if ($fuser->id > 0) {
247 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
248 $res = $fuser->update($user);
252 print
' --> '.$res.
' '.$fuser->error;
254 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
257 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
258 $res = $fuser->create($user);
261 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
264 print
' --> '.$res.
' '.$fuser->error;
281 if (!$error || $forcecommit) {
283 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
285 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
289 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
313 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=null, $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
getDolGlobalString($key, $default='')
Return a 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.