29if (!defined(
'NOSESSION')) {
30 define(
'NOSESSION',
'1');
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
38if (substr($sapi_type, 0, 3) ==
'cgi') {
39 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
43require_once $path.
"../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
45require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
46require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
47require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
57$langs->loadLangs(array(
"main",
"errors"));
60$version = DOL_VERSION;
63$excludeuser = array();
66$hookmanager->initHooks(array(
'cli'));
74print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
75dol_syslog($script_file.
" launched with arg ".implode(
',', $argv));
78$required_fields = array(
79 $conf->global->LDAP_KEY_USERS,
80 $conf->global->LDAP_FIELD_FULLNAME,
81 $conf->global->LDAP_FIELD_NAME,
82 $conf->global->LDAP_FIELD_FIRSTNAME,
83 $conf->global->LDAP_FIELD_LOGIN,
84 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
85 $conf->global->LDAP_FIELD_PASSWORD,
86 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
87 $conf->global->LDAP_FIELD_PHONE,
88 $conf->global->LDAP_FIELD_FAX,
89 $conf->global->LDAP_FIELD_MOBILE,
94 $conf->global->LDAP_FIELD_MAIL,
95 $conf->global->LDAP_FIELD_TITLE,
96 $conf->global->LDAP_FIELD_DESCRIPTION,
97 $conf->global->LDAP_FIELD_SID
101$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidLdapElement3")));
103if (!isset($argv[1])) {
104 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
108foreach ($argv as $key => $val) {
109 if ($val ==
'commitiferror') {
112 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
113 $conf->global->LDAP_SERVER_HOST = $reg[1];
115 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
116 $excludeuser = explode(
',', $reg[1]);
118 if (preg_match(
'/-y$/', $val, $reg)) {
123print
"Mails sending disabled (useless in batch mode)\n";
124$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
126print
"----- Synchronize all records from LDAP database:\n";
137print
"----- To Dolibarr database:\n";
138print
"type=".$conf->db->type.
"\n";
139print
"host=".$conf->db->host.
"\n";
140print
"port=".$conf->db->port.
"\n";
141print
"login=".$conf->db->user.
"\n";
142print
"database=".$conf->db->name.
"\n";
143print
"----- Options:\n";
144print
"commitiferror=".$forcecommit.
"\n";
145print
"excludeuser=".implode(
',', $excludeuser).
"\n";
146print
"Mapped LDAP fields=".implode(
',', $required_fields).
"\n";
150 print
"Hit Enter to continue or CTRL+C to stop...\n";
151 $input = trim(fgets(STDIN));
155 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for users not defined inside Dolibarr");
160$hashlib2rowid = array();
162$sql =
"SELECT rowid, code, label, active";
163$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
164$sql .=
" WHERE active = 1";
165$sql .=
" ORDER BY code ASC";
166$resql = $db->query($sql);
168 $num = $db->num_rows($resql);
172 $obj = $db->fetch_object($resql);
175 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
176 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
187$result = $ldap->connectBind();
189 $justthese = array();
192 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
195 if (is_array($ldaprecords)) {
199 foreach ($ldaprecords as $key => $ldapuser) {
202 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[
getDolGlobalString(
'LDAP_FIELD_LOGIN')].
' --> Discarded'.
"\n";
206 $fuser =
new User($db);
219 $fuser->pass_indatabase_crypted = $ldapuser[
getDolGlobalString(
'LDAP_FIELD_PASSWORD_CRYPTED')] ??
'';
241 $fuser->contact_id = 0;
242 $fuser->fk_member = 0;
257 if ($fuser->id > 0) {
258 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
259 $res = $fuser->update($user);
263 print
' --> '.$res.
' '.$fuser->error;
265 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
268 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
269 $res = $fuser->create($user);
272 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
275 print
' --> '.$res.
' '.$fuser->error;
292 if (!$error || $forcecommit) {
294 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
296 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
300 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", (
string) $error).
"\n";
324 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...
dolValidLdapElement3($element)
Function to say if a value is empty or not.