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";
48require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
58$langs->loadLangs(array(
"main",
"errors"));
61$version = DOL_VERSION;
66$hookmanager->initHooks(array(
'cli'));
74print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
75dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
81$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
83if (!isset($argv[1])) {
85 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
89foreach ($argv as $key => $val) {
90 if ($val ==
'commitiferror') {
93 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
94 $conf->global->LDAP_SERVER_HOST = $reg[1];
96 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
97 $excludeuser = explode(
',', $reg[1]);
99 if (preg_match(
'/-y$/', $val, $reg)) {
104print
"Mails sending disabled (useless in batch mode)\n";
105$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
107print
"----- Synchronize all records from LDAP database:\n";
118print
"----- To Dolibarr database:\n";
119print
"type=".$conf->db->type.
"\n";
120print
"host=".$conf->db->host.
"\n";
121print
"port=".$conf->db->port.
"\n";
122print
"login=".$conf->db->user.
"\n";
123print
"database=".$conf->db->name.
"\n";
124print
"----- Options:\n";
125print
"commitiferror=".$forcecommit.
"\n";
126print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
130 print
"Hit Enter to continue or CTRL+C to stop...\n";
131 $input = trim(fgets(STDIN));
135 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
140$result = $ldap->connectBind();
142 $justthese = array();
145 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
148 if (is_array($ldaprecords)) {
152 foreach ($ldaprecords as $key => $ldapgroup) {
156 $group->nom = $group->name;
157 $group->note = $ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_DESCRIPTION')] ??
null;
158 $group->entity =
$conf->entity;
162 if ($group->id > 0) {
163 print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
164 $res = $group->update();
167 print
' --> Updated group id='.$group->id.
' name='.$group->name;
170 print
' --> '.$res.
' '.$group->error;
174 print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
175 $res = $group->create();
178 print
' --> Created group id='.$group->id.
' name='.$group->name;
181 print
' --> '.$res.
' '.$group->error;
191 $userIdList = array();
192 foreach ($ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPMEMBERS')] as $tmpkey => $userdn) {
193 if ($tmpkey ===
'count') {
196 if (empty($userList[$userdn])) {
199 $userKey = array($userdn);
201 $userFilter = explode(
',', $userdn);
202 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')',
getDolGlobalString(
'LDAP_KEY_USERS'));
204 if (!is_array($userKey)) {
208 $fuser =
new User($db);
211 $fuser->fetch(
'',
'', $userKey[0]);
213 $fuser->fetch(
'', $userKey[0]);
216 $userList[$userdn] = $fuser;
218 $fuser = &$userList[$userdn];
221 $userIdList[$userdn] = $fuser->id;
224 if (!in_array($fuser->id, array_keys($group->members))) {
225 $fuser->SetInGroup($group->id, $group->entity);
226 echo $fuser->login.
' added'.
"\n";
231 foreach ($group->members as $guser) {
232 if (!in_array($guser->id, $userIdList)) {
233 $guser->RemoveFromGroup($group->id, $group->entity);
234 echo $guser->login.
' removed'.
"\n";
239 if (!$error || $forcecommit) {
241 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
243 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
247 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
271 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.
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.