30if (!defined(
'NOSESSION')) {
31 define(
'NOSESSION',
'1');
34$sapi_type = php_sapi_name();
35$script_file = basename(__FILE__);
39if (substr($sapi_type, 0, 3) ==
'cgi') {
40 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
44require_once $path.
"../../htdocs/master.inc.php";
45require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
46require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
47require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
48require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
49require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
59$langs->loadLangs(array(
"main",
"errors"));
62$version = DOL_VERSION;
67$hookmanager->initHooks(array(
'cli'));
75print
"***** ".$script_file.
" (".$version.
") pid=".dol_getmypid().
" *****\n";
76dol_syslog($script_file.
" launched with arg ".implode(
',', $argv));
82$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidLdapElement2")));
84if (!isset($argv[1])) {
86 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
90foreach ($argv as $key => $val) {
91 if ($val ==
'commitiferror') {
94 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
95 $conf->global->LDAP_SERVER_HOST = $reg[1];
97 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
98 $excludeuser = explode(
',', $reg[1]);
100 if (preg_match(
'/-y$/', $val, $reg)) {
105print
"Mails sending disabled (useless in batch mode)\n";
106$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
108print
"----- Synchronize all records from LDAP database:\n";
119print
"----- To Dolibarr database:\n";
120print
"type=".$conf->db->type.
"\n";
121print
"host=".$conf->db->host.
"\n";
122print
"port=".$conf->db->port.
"\n";
123print
"login=".$conf->db->user.
"\n";
124print
"database=".$conf->db->name.
"\n";
125print
"----- Options:\n";
126print
"commitiferror=".$forcecommit.
"\n";
127print
"Mapped LDAP fields=".implode(
',', $required_fields).
"\n";
131 print
"Hit Enter to continue or CTRL+C to stop...\n";
132 $input = trim(fgets(STDIN));
136 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
141$result = $ldap->connectBind();
143 $justthese = array();
146 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
149 if (is_array($ldaprecords)) {
153 foreach ($ldaprecords as $key => $ldapgroup) {
157 $group->nom = $group->name;
159 $group->entity = $conf->entity;
163 if ($group->id > 0) {
164 print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
165 $res = $group->update();
168 print
' --> Updated group id='.$group->id.
' name='.$group->name;
171 print
' --> '.$res.
' '.$group->error;
175 print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
176 $res = $group->create();
179 print
' --> Created group id='.$group->id.
' name='.$group->name;
182 print
' --> '.$res.
' '.$group->error;
192 $userIdList = array();
194 if (!is_array($groupMembers)) {
195 $groupMembers = array();
197 foreach ($groupMembers as $tmpkey => $userdn) {
198 if ($tmpkey ===
'count') {
201 if (empty($userList[$userdn])) {
204 $userKey = array($userdn);
206 $userFilter = explode(
',', $userdn);
207 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')',
getDolGlobalString(
'LDAP_KEY_USERS'));
209 if (!is_array($userKey)) {
213 $fuser =
new User($db);
216 $fuser->fetch(0,
'', $userKey[0]);
218 $fuser->fetch(0, $userKey[0]);
221 $userList[$userdn] = $fuser;
223 $fuser = &$userList[$userdn];
226 $userIdList[$userdn] = $fuser->id;
229 if (!in_array($fuser->id, array_keys($group->members))) {
230 $fuser->SetInGroup($group->id, $group->entity);
231 echo $fuser->login.
' added'.
"\n";
236 foreach ($group->members as $guser) {
237 if (!in_array($guser->id, $userIdList)) {
238 $guser->RemoveFromGroup($group->id, $group->entity);
239 echo $guser->login.
' removed'.
"\n";
244 if (!$error || $forcecommit) {
246 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
248 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
252 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", (
string) $error).
"\n";
276 return (trim($element) !=
'');
Class to manage LDAP features.
Class to manage user groups.
Class to manage Dolibarr users.
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.
dolValidLdapElement2($element)
Function to say if a value is empty or not.