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";
52require_once DOL_DOCUMENT_ROOT.
'/core/lib/functionscli.lib.php';
53require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
54require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
55require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
56require_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 ".implode(
',', $argv));
81$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidLdapElement2")));
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=".(string)
$conf->db->user.
"\n";
123print
"database=".$conf->db->name.
"\n";
124print
"----- Options:\n";
125print
"commitiferror=".$forcecommit.
"\n";
126print
"Mapped LDAP fields=".implode(
',', $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;
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();
193 if (!is_array($groupMembers)) {
194 $groupMembers = array();
196 foreach ($groupMembers as $tmpkey => $userdn) {
197 if ($tmpkey ===
'count') {
200 if (empty($userList[$userdn])) {
203 $userKey = array($userdn);
205 $userFilter = explode(
',', $userdn);
206 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')',
getDolGlobalString(
'LDAP_KEY_USERS'));
208 if (!is_array($userKey)) {
215 $fuser->fetch(0,
'', $userKey[0]);
217 $fuser->fetch(0, $userKey[0]);
220 $userList[$userdn] = $fuser;
222 $fuser = &$userList[$userdn];
225 $userIdList[$userdn] = $fuser->id;
228 if (!in_array($fuser->id, array_keys($group->members))) {
229 $fuser->SetInGroup($group->id, $group->entity);
230 echo $fuser->login.
' added'.
"\n";
235 foreach ($group->members as $guser) {
236 if (!in_array($guser->id, $userIdList)) {
237 $guser->RemoveFromGroup($group->id, $group->entity);
238 echo $guser->login.
' removed'.
"\n";
243 if (!$error || $forcecommit) {
245 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
247 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
251 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", (
string) $error).
"\n";
275 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.