28 if (!defined(
'NOSESSION')) define(
'NOSESSION',
'1');
30 $sapi_type = php_sapi_name();
31 $script_file = basename(__FILE__);
35 if (substr($sapi_type, 0, 3) ==
'cgi') {
36 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
40 require_once $path.
"../../htdocs/master.inc.php";
41 require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
42 require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
43 require_once DOL_DOCUMENT_ROOT.
"/user/class/user.class.php";
44 require_once DOL_DOCUMENT_ROOT.
"/user/class/usergroup.class.php";
46 $langs->loadLangs(array(
"main",
"errors"));
49 $version = DOL_VERSION;
60 dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
63 $required_fields = array($conf->global->LDAP_KEY_GROUPS, $conf->global->LDAP_GROUP_FIELD_FULLNAME, $conf->global->LDAP_GROUP_FIELD_DESCRIPTION, $conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS);
66 $required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
68 if (!isset($argv[1])) {
70 print "Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
74 foreach ($argv as $key => $val) {
75 if ($val ==
'commitiferror')
77 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg))
78 $conf->global->LDAP_SERVER_HOST = $reg[1];
79 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg))
80 $excludeuser = explode(
',', $reg[1]);
81 if (preg_match(
'/-y$/', $val, $reg))
85 print "Mails sending disabled (useless in batch mode)\n";
86 $conf->global->MAIN_DISABLE_ALL_MAILS = 1;
88 print "----- Synchronize all records from LDAP database:\n";
89 print "host=".$conf->global->LDAP_SERVER_HOST.
"\n";
90 print "port=".$conf->global->LDAP_SERVER_PORT.
"\n";
91 print "login=".$conf->global->LDAP_ADMIN_DN.
"\n";
92 print "pass=".preg_replace(
'/./i',
'*', $conf->global->LDAP_ADMIN_PASS).
"\n";
93 print "DN to extract=".$conf->global->LDAP_GROUP_DN.
"\n";
94 print 'Filter=('.$conf->global->LDAP_KEY_GROUPS.
'=*)'.
"\n";
95 print "----- To Dolibarr database:\n";
96 print "type=".$conf->db->type.
"\n";
97 print "host=".$conf->db->host.
"\n";
98 print "port=".$conf->db->port.
"\n";
99 print "login=".$conf->db->user.
"\n";
100 print "database=".$conf->db->name.
"\n";
101 print "----- Options:\n";
102 print "commitiferror=".$forcecommit.
"\n";
103 print "Mapped LDAP fields=".join(
',', $required_fields).
"\n";
107 print "Hit Enter to continue or CTRL+C to stop...\n";
108 $input = trim(fgets(STDIN));
111 if (empty($conf->global->LDAP_GROUP_DN)) {
112 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for groups not defined inside Dolibarr");
117 $result = $ldap->connect_bind();
119 $justthese = array();
122 $conf->global->LDAP_SYNCHRO_ACTIVE = 0;
124 $ldaprecords = $ldap->getRecords(
'*', $conf->global->LDAP_GROUP_DN, $conf->global->LDAP_KEY_GROUPS, $required_fields, 0, array($conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS));
125 if (is_array($ldaprecords)) {
129 foreach ($ldaprecords as $key => $ldapgroup) {
131 $group->fetch(
'', $ldapgroup[$conf->global->LDAP_KEY_GROUPS]);
132 $group->name = $ldapgroup[$conf->global->LDAP_GROUP_FIELD_FULLNAME];
133 $group->nom = $group->name;
134 $group->note = $ldapgroup[$conf->global->LDAP_GROUP_FIELD_DESCRIPTION];
135 $group->entity = $conf->entity;
139 if ($group->id > 0) {
140 print $langs->transnoentities(
"GroupUpdate").
' # '.$key.
': name='.$group->name;
141 $res = $group->update();
144 print ' --> Updated group id='.$group->id.
' name='.$group->name;
147 print ' --> '.$res.
' '.$group->error;
151 print $langs->transnoentities(
"GroupCreate").
' # '.$key.
': name='.$group->name;
152 $res = $group->create();
155 print ' --> Created group id='.$group->id.
' name='.$group->name;
158 print ' --> '.$res.
' '.$group->error;
168 $userIdList = array();
169 foreach ($ldapgroup[$conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS] as $key => $userdn) {
170 if ($key ===
'count')
172 if (empty($userList[$userdn])) {
174 if ($conf->global->LDAP_GROUP_FIELD_GROUPMEMBERS ===
'memberUid') {
175 $userKey = array($userdn);
177 $userFilter = explode(
',', $userdn);
178 $userKey = $ldap->getAttributeValues(
'('.$userFilter[0].
')', $conf->global->LDAP_KEY_USERS);
180 if (!is_array($userKey))
183 $fuser =
new User($db);
185 if ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_SID) {
186 $fuser->fetch(
'',
'', $userKey[0]);
187 } elseif ($conf->global->LDAP_KEY_USERS == $conf->global->LDAP_FIELD_LOGIN) {
188 $fuser->fetch(
'', $userKey[0]);
191 $userList[$userdn] = $fuser;
193 $fuser = &$userList[$userdn];
196 $userIdList[$userdn] = $fuser->id;
199 if (!in_array($fuser->id, array_keys($group->members))) {
200 $fuser->SetInGroup($group->id, $group->entity);
201 echo $fuser->login.
' added'.
"\n";
206 foreach ($group->members as $guser) {
207 if (!in_array($guser->id, $userIdList)) {
208 $guser->RemoveFromGroup($group->id, $group->entity);
209 echo $guser->login.
' removed'.
"\n";
214 if (!$error || $forcecommit) {
216 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
217 else print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
220 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
244 return (trim($element) !=
'');
Class to manage Dolibarr users.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
Class to manage user groups.
dolValidElement($element)
Function to say if a value is empty or not.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
Class to manage LDAP features.
print
Draft customers invoices.
dol_print_error($db= '', $error= '', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...