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/date.lib.php";
44require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
45require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
46require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
48$langs->loadLangs(array(
"main",
"errors"));
51$version = DOL_VERSION;
56$hookmanager->initHooks(array(
'cli'));
64print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
65dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
71$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
73if (!isset($argv[1])) {
75 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
79foreach ($argv as $key => $val) {
80 if ($val ==
'commitiferror') {
83 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
84 $conf->global->LDAP_SERVER_HOST = $reg[1];
86 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
87 $excludeuser = explode(
',', $reg[1]);
89 if (preg_match(
'/-y$/', $val, $reg)) {
94print
"Mails sending disabled (useless in batch mode)\n";
95$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
97print
"----- Synchronize all records from LDAP database:\n";
108print
"----- To Dolibarr database:\n";
109print
"type=".$conf->db->type.
"\n";
110print
"host=".$conf->db->host.
"\n";
111print
"port=".$conf->db->port.
"\n";
112print
"login=".$conf->db->user.
"\n";
113print
"database=".$conf->db->name.
"\n";
114print
"----- Options:\n";
115print
"commitiferror=".$forcecommit.
"\n";
116print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
120 print
"Hit Enter to continue or CTRL+C to stop...\n";
121 $input = trim(fgets(STDIN));
125 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
130$result = $ldap->connectBind();
132 $justthese = array();
135 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
138 if (is_array($ldaprecords)) {
142 foreach ($ldaprecords as $key => $ldapgroup) {
146 $group->nom = $group->name;
148 $group->entity = $conf->entity;
152 if ($group->id > 0) {
153 print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
154 $res = $group->update();
157 print
' --> Updated group id='.$group->id.
' name='.$group->name;
160 print
' --> '.$res.
' '.$group->error;
164 print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
165 $res = $group->create();
168 print
' --> Created group id='.$group->id.
' name='.$group->name;
171 print
' --> '.$res.
' '.$group->error;
181 $userIdList = array();
182 foreach ($ldapgroup[
getDolGlobalString(
'LDAP_GROUP_FIELD_GROUPMEMBERS')] as $tmpkey => $userdn) {
183 if ($tmpkey ===
'count') {
186 if (empty($userList[$userdn])) {
189 $userKey = array($userdn);
191 $userFilter = explode(
',', $userdn);
192 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')',
getDolGlobalString(
'LDAP_KEY_USERS'));
194 if (!is_array($userKey)) {
198 $fuser =
new User($db);
201 $fuser->fetch(
'',
'', $userKey[0]);
203 $fuser->fetch(
'', $userKey[0]);
206 $userList[$userdn] = $fuser;
208 $fuser = &$userList[$userdn];
211 $userIdList[$userdn] = $fuser->id;
214 if (!in_array($fuser->id, array_keys($group->members))) {
215 $fuser->SetInGroup($group->id, $group->entity);
216 echo $fuser->login.
' added'.
"\n";
221 foreach ($group->members as $guser) {
222 if (!in_array($guser->id, $userIdList)) {
223 $guser->RemoveFromGroup($group->id, $group->entity);
224 echo $guser->login.
' removed'.
"\n";
229 if (!$error || $forcecommit) {
231 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
233 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
237 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
261 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 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.