28if (!defined(
'NOSESSION')) {
29 define(
'NOSESSION',
'1');
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
37if (substr($sapi_type, 0, 3) ==
'cgi') {
38 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
42require_once $path.
"../../htdocs/master.inc.php";
43require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
44require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
45require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
46require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
56$langs->loadLangs(array(
"main",
"errors"));
59$version = DOL_VERSION;
62$excludeuser = array();
65$hookmanager->initHooks(array(
'cli'));
73print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
74dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
77$required_fields = array(
78 $conf->global->LDAP_KEY_USERS,
79 $conf->global->LDAP_FIELD_FULLNAME,
80 $conf->global->LDAP_FIELD_NAME,
81 $conf->global->LDAP_FIELD_FIRSTNAME,
82 $conf->global->LDAP_FIELD_LOGIN,
83 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
84 $conf->global->LDAP_FIELD_PASSWORD,
85 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
86 $conf->global->LDAP_FIELD_PHONE,
87 $conf->global->LDAP_FIELD_FAX,
88 $conf->global->LDAP_FIELD_MOBILE,
93 $conf->global->LDAP_FIELD_MAIL,
94 $conf->global->LDAP_FIELD_TITLE,
95 $conf->global->LDAP_FIELD_DESCRIPTION,
96 $conf->global->LDAP_FIELD_SID
100$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
102if (!isset($argv[1])) {
103 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
107foreach ($argv as $key => $val) {
108 if ($val ==
'commitiferror') {
111 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
112 $conf->global->LDAP_SERVER_HOST = $reg[1];
114 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
115 $excludeuser = explode(
',', $reg[1]);
117 if (preg_match(
'/-y$/', $val, $reg)) {
122print
"Mails sending disabled (useless in batch mode)\n";
123$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
125print
"----- Synchronize all records from LDAP database:\n";
136print
"----- To Dolibarr database:\n";
137print
"type=".$conf->db->type.
"\n";
138print
"host=".$conf->db->host.
"\n";
139print
"port=".$conf->db->port.
"\n";
140print
"login=".$conf->db->user.
"\n";
141print
"database=".$conf->db->name.
"\n";
142print
"----- Options:\n";
143print
"commitiferror=".$forcecommit.
"\n";
144print
"excludeuser=".join(
',', $excludeuser).
"\n";
145print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
149 print
"Hit Enter to continue or CTRL+C to stop...\n";
150 $input = trim(fgets(STDIN));
154 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
159$hashlib2rowid = array();
161$sql =
"SELECT rowid, code, label, active";
162$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
163$sql .=
" WHERE active = 1";
164$sql .=
" ORDER BY code ASC";
165$resql = $db->query($sql);
167 $num = $db->num_rows($resql);
171 $obj = $db->fetch_object($resql);
174 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
175 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
186$result = $ldap->connectBind();
188 $justthese = array();
191 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
194 if (is_array($ldaprecords)) {
198 foreach ($ldaprecords as $key => $ldapuser) {
201 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[
getDolGlobalString(
'LDAP_FIELD_LOGIN')].
' --> Discarded'.
"\n";
205 $fuser =
new User($db);
218 $fuser->pass_indatabase_crypted = $ldapuser[
getDolGlobalString(
'LDAP_FIELD_PASSWORD_CRYPTED')] ??
null;
240 $fuser->contact_id = 0;
241 $fuser->fk_member = 0;
256 if ($fuser->id > 0) {
257 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
258 $res = $fuser->update($user);
262 print
' --> '.$res.
' '.$fuser->error;
264 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
267 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
268 $res = $fuser->create($user);
271 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
274 print
' --> '.$res.
' '.$fuser->error;
291 if (!$error || $forcecommit) {
293 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
295 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
299 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
323 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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
dolValidElement($element)
Function to say if a value is empty or not.