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/date.lib.php";
45require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
46require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent_type.class.php";
48$langs->loadLangs(array(
"main",
"errors"));
51$version = constant(
'DOL_VERSION');
61print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
62dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
65$required_fields = array($conf->global->LDAP_KEY_MEMBERS_TYPES, $conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME, $conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION, $conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS);
68$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElementType")));
70if (!isset($argv[1])) {
72 print
"Usage: $script_file (nocommitiferror|commitiferror) [--server=ldapserverhost] [--excludeuser=user1,user2...] [-y]\n";
76foreach ($argv as $key => $val) {
77 if ($val ==
'commitiferror') {
80 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
81 $conf->global->LDAP_SERVER_HOST = $reg[1];
83 if (preg_match(
'/--excludeuser=([^\s]+)$/', $val, $reg)) {
84 $excludeuser = explode(
',', $reg[1]);
86 if (preg_match(
'/-y$/', $val, $reg)) {
91if (!empty($dolibarr_main_db_readonly)) {
92 print
"Error: instance in read-onyl mode\n";
96print
"Mails sending disabled (useless in batch mode)\n";
97$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
99print
"----- Synchronize all records from LDAP database:\n";
100print
"host=".$conf->global->LDAP_SERVER_HOST.
"\n";
101print
"port=".$conf->global->LDAP_SERVER_PORT.
"\n";
102print
"login=".$conf->global->LDAP_ADMIN_DN.
"\n";
103print
"pass=".preg_replace(
'/./i',
'*', $conf->global->LDAP_ADMIN_PASS).
"\n";
104print
"DN to extract=".$conf->global->LDAP_MEMBER_TYPE_DN.
"\n";
105print
'Filter=('.$conf->global->LDAP_KEY_MEMBERS_TYPES.
'=*)'.
"\n";
106print
"----- To Dolibarr database:\n";
107print
"type=".$conf->db->type.
"\n";
108print
"host=".$conf->db->host.
"\n";
109print
"port=".$conf->db->port.
"\n";
110print
"login=".$conf->db->user.
"\n";
111print
"database=".$conf->db->name.
"\n";
112print
"----- Options:\n";
113print
"commitiferror=".$forcecommit.
"\n";
114print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
118 print
"Hit Enter to continue or CTRL+C to stop...\n";
119 $input = trim(fgets(STDIN));
122if (empty($conf->global->LDAP_MEMBER_TYPE_DN)) {
123 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for members types not defined inside Dolibarr");
128$result = $ldap->connect_bind();
130 $justthese = array();
133 $conf->global->LDAP_MEMBER_TYPE_ACTIVE = 0;
135 $ldaprecords = $ldap->getRecords(
'*', $conf->global->LDAP_MEMBER_TYPE_DN, $conf->global->LDAP_KEY_MEMBERS_TYPES, $required_fields, 0, array($conf->global->LDAP_MEMBER_TYPE_FIELD_GROUPMEMBERS));
136 if (is_array($ldaprecords)) {
140 foreach ($ldaprecords as $key => $ldapgroup) {
142 $membertype->fetch($ldapgroup[$conf->global->LDAP_KEY_MEMBERS_TYPES]);
143 $membertype->label = $ldapgroup[$conf->global->LDAP_MEMBER_TYPE_FIELD_FULLNAME];
144 $membertype->description = $ldapgroup[$conf->global->LDAP_MEMBER_TYPE_FIELD_DESCRIPTION];
145 $membertype->entity = $conf->entity;
149 if ($membertype->id > 0) {
150 print $langs->transnoentities(
"MemberTypeUpdate").
' # '.$key.
': name='.$membertype->label;
151 $res = $membertype->update($user);
154 print
' --> Updated member type id='.$membertype->id.
' name='.$membertype->label;
157 print
' --> '.$res.
' '.$membertype->error;
161 print $langs->transnoentities(
"MemberTypeCreate").
' # '.$key.
': name='.$membertype->label;
162 $res = $membertype->create($user);
165 print
' --> Created member type id='.$membertype->id.
' name='.$membertype->label;
168 print
' --> '.$res.
' '.$membertype->error;
176 if (!$error || $forcecommit) {
178 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
180 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
184 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
208 return (trim($element) !=
'');
Class to manage members type.
Class to manage LDAP features.
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
dolValidElementType($element)
Function to say if a value is empty or not.