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/functionscli.lib.php';
43require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
44require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
45require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
47$langs->loadLangs(array(
"main",
"errors"));
50$version = DOL_VERSION;
53$excludeuser = array();
56$hookmanager->initHooks(array(
'cli'));
64print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
65dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
68$required_fields = array(
69 $conf->global->LDAP_KEY_USERS,
70 $conf->global->LDAP_FIELD_FULLNAME,
71 $conf->global->LDAP_FIELD_NAME,
72 $conf->global->LDAP_FIELD_FIRSTNAME,
73 $conf->global->LDAP_FIELD_LOGIN,
74 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
75 $conf->global->LDAP_FIELD_PASSWORD,
76 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
77 $conf->global->LDAP_FIELD_PHONE,
78 $conf->global->LDAP_FIELD_FAX,
79 $conf->global->LDAP_FIELD_MOBILE,
84 $conf->global->LDAP_FIELD_MAIL,
85 $conf->global->LDAP_FIELD_TITLE,
86 $conf->global->LDAP_FIELD_DESCRIPTION,
87 $conf->global->LDAP_FIELD_SID
91$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
93if (!isset($argv[1])) {
94 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
98foreach ($argv as $key => $val) {
99 if ($val ==
'commitiferror') {
102 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
103 $conf->global->LDAP_SERVER_HOST = $reg[1];
105 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
106 $excludeuser = explode(
',', $reg[1]);
108 if (preg_match(
'/-y$/', $val, $reg)) {
113print
"Mails sending disabled (useless in batch mode)\n";
114$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
116print
"----- Synchronize all records from LDAP database:\n";
127print
"----- To Dolibarr database:\n";
128print
"type=".$conf->db->type.
"\n";
129print
"host=".$conf->db->host.
"\n";
130print
"port=".$conf->db->port.
"\n";
131print
"login=".$conf->db->user.
"\n";
132print
"database=".$conf->db->name.
"\n";
133print
"----- Options:\n";
134print
"commitiferror=".$forcecommit.
"\n";
135print
"excludeuser=".join(
',', $excludeuser).
"\n";
136print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
140 print
"Hit Enter to continue or CTRL+C to stop...\n";
141 $input = trim(fgets(STDIN));
145 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
150$hashlib2rowid = array();
152$sql =
"SELECT rowid, code, label, active";
153$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
154$sql .=
" WHERE active = 1";
155$sql .=
" ORDER BY code ASC";
156$resql = $db->query($sql);
158 $num = $db->num_rows($resql);
162 $obj = $db->fetch_object($resql);
165 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
166 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
177$result = $ldap->connectBind();
179 $justthese = array();
182 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
185 if (is_array($ldaprecords)) {
189 foreach ($ldaprecords as $key => $ldapuser) {
192 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[
getDolGlobalString(
'LDAP_FIELD_LOGIN')].
' --> Discarded'.
"\n";
196 $fuser =
new User($db);
209 $fuser->pass_indatabase_crypted = $ldapuser[
getDolGlobalString(
'LDAP_FIELD_PASSWORD_CRYPTED')] ??
null;
231 $fuser->contact_id = 0;
232 $fuser->fk_member = 0;
247 if ($fuser->id > 0) {
248 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
249 $res = $fuser->update($user);
253 print
' --> '.$res.
' '.$fuser->error;
255 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
258 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
259 $res = $fuser->create($user);
262 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
265 print
' --> '.$res.
' '.$fuser->error;
282 if (!$error || $forcecommit) {
284 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
286 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
290 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
314 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.