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";
113print
"host=".$conf->global->LDAP_SERVER_HOST.
"\n";
114print
"port=".$conf->global->LDAP_SERVER_PORT.
"\n";
115print
"login=".$conf->global->LDAP_ADMIN_DN.
"\n";
116print
"pass=".preg_replace(
'/./i',
'*', $conf->global->LDAP_ADMIN_PASS).
"\n";
117print
"DN to extract=".$conf->global->LDAP_USER_DN.
"\n";
118if (!empty($conf->global->LDAP_FILTER_CONNECTION)) {
119 print
'Filter=('.$conf->global->LDAP_FILTER_CONNECTION.
')'.
"\n";
121 print
'Filter=('.$conf->global->LDAP_KEY_USERS.
'=*)'.
"\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));
140if (empty($conf->global->LDAP_USER_DN)) {
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;
180 $ldaprecords = $ldap->getRecords(
'*', $conf->global->LDAP_USER_DN, $conf->global->LDAP_KEY_USERS, $required_fields,
'user');
181 if (is_array($ldaprecords)) {
185 foreach ($ldaprecords as $key => $ldapuser) {
187 if (in_array($ldapuser[$conf->global->LDAP_FIELD_LOGIN], $excludeuser)) {
188 print $langs->transnoentities(
"UserDiscarded").
' # '.$key.
': login='.$ldapuser[$conf->global->LDAP_FIELD_LOGIN].
' --> Discarded'.
"\n";
192 $fuser =
new User($db);
194 if ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_SID) {
195 $fuser->fetch(
'',
'', $ldapuser[$conf->global->LDAP_KEY_USERS]);
196 } elseif ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_LOGIN) {
197 $fuser->fetch(
'', $ldapuser[$conf->global->LDAP_KEY_USERS]);
201 $fuser->firstname = $ldapuser[$conf->global->LDAP_FIELD_FIRSTNAME];
202 $fuser->lastname = $ldapuser[$conf->global->LDAP_FIELD_NAME];
203 $fuser->login = $ldapuser[$conf->global->LDAP_FIELD_LOGIN];
204 $fuser->pass = $ldapuser[$conf->global->LDAP_FIELD_PASSWORD];
205 $fuser->pass_indatabase_crypted = $ldapuser[$conf->global->LDAP_FIELD_PASSWORD_CRYPTED];
217 $fuser->office_phone = $ldapuser[$conf->global->LDAP_FIELD_PHONE];
218 $fuser->user_mobile = $ldapuser[$conf->global->LDAP_FIELD_MOBILE];
219 $fuser->office_fax = $ldapuser[$conf->global->LDAP_FIELD_FAX];
220 $fuser->email = $ldapuser[$conf->global->LDAP_FIELD_MAIL];
221 $fuser->ldap_sid = $ldapuser[$conf->global->LDAP_FIELD_SID];
223 $fuser->job = $ldapuser[$conf->global->LDAP_FIELD_TITLE];
224 $fuser->note = $ldapuser[$conf->global->LDAP_FIELD_DESCRIPTION];
227 $fuser->contact_id = 0;
228 $fuser->fk_member = 0;
244 if ($fuser->id > 0) {
245 print $langs->transnoentities(
"UserUpdate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
246 $res = $fuser->update($user);
250 print
' --> '.$res.
' '.$fuser->error;
252 print
' --> Updated user id='.$fuser->id.
' login='.$fuser->login;
255 print $langs->transnoentities(
"UserCreate").
' # '.$key.
': login='.$fuser->login.
', fullname='.$fuser->getFullName($langs);
256 $res = $fuser->create($user);
259 print
' --> Created user id='.$fuser->id.
' login='.$fuser->login;
262 print
' --> '.$res.
' '.$fuser->error;
279 if (!$error || $forcecommit) {
281 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
283 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
287 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
311 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...
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.