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";
47require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
49$langs->loadLangs(array(
"main",
"errors"));
52$version = DOL_VERSION;
57$hookmanager->initHooks(array(
'cli'));
65print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
66dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
72$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
74if (!isset($argv[1])) {
76 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
80foreach ($argv as $key => $val) {
81 if ($val ==
'commitiferror') {
84 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
85 $conf->global->LDAP_SERVER_HOST = $reg[1];
87 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
88 $excludeuser = explode(
',', $reg[1]);
90 if (preg_match(
'/-y$/', $val, $reg)) {
95print
"Mails sending disabled (useless in batch mode)\n";
96$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
98print
"----- Synchronize all records from LDAP database:\n";
109print
"----- To Dolibarr database:\n";
110print
"type=".$conf->db->type.
"\n";
111print
"host=".$conf->db->host.
"\n";
112print
"port=".$conf->db->port.
"\n";
113print
"login=".$conf->db->user.
"\n";
114print
"database=".$conf->db->name.
"\n";
115print
"----- Options:\n";
116print
"commitiferror=".$forcecommit.
"\n";
117print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
121 print
"Hit Enter to continue or CTRL+C to stop...\n";
122 $input = trim(fgets(STDIN));
126 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
131$result = $ldap->connectBind();
133 $justthese = array();
136 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
139 if (is_array($ldaprecords)) {
143 foreach ($ldaprecords as $key => $ldapgroup) {
147 $group->nom = $group->name;
148 $group->note = $ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_DESCRIPTION')] ??
null;
149 $group->entity = $conf->entity;
153 if ($group->id > 0) {
154 print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
155 $res = $group->update();
158 print
' --> Updated group id='.$group->id.
' name='.$group->name;
161 print
' --> '.$res.
' '.$group->error;
165 print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
166 $res = $group->create();
169 print
' --> Created group id='.$group->id.
' name='.$group->name;
172 print
' --> '.$res.
' '.$group->error;
182 $userIdList = array();
183 foreach ($ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPMEMBERS')] as $tmpkey => $userdn) {
184 if ($tmpkey ===
'count') {
187 if (empty($userList[$userdn])) {
190 $userKey = array($userdn);
192 $userFilter = explode(
',', $userdn);
193 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')',
getDolGlobalString(
'LDAP_KEY_USERS'));
195 if (!is_array($userKey)) {
199 $fuser =
new User($db);
202 $fuser->fetch(
'',
'', $userKey[0]);
204 $fuser->fetch(
'', $userKey[0]);
207 $userList[$userdn] = $fuser;
209 $fuser = &$userList[$userdn];
212 $userIdList[$userdn] = $fuser->id;
215 if (!in_array($fuser->id, array_keys($group->members))) {
216 $fuser->SetInGroup($group->id, $group->entity);
217 echo $fuser->login.
' added'.
"\n";
222 foreach ($group->members as $guser) {
223 if (!in_array($guser->id, $userIdList)) {
224 $guser->RemoveFromGroup($group->id, $group->entity);
225 echo $guser->login.
' removed'.
"\n";
230 if (!$error || $forcecommit) {
232 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
234 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
238 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
262 return (trim($element) !=
'');
Class to manage LDAP features.
Class to manage user groups.
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.