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/functionscli.lib.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.class.php";
47require_once DOL_DOCUMENT_ROOT.
"/adherents/class/subscription.class.php";
49$langs->loadLangs(array(
"main",
"errors"));
52$version = constant(
'DOL_VERSION');
57$hookmanager->initHooks(array(
'cli'));
65print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
66dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
69$required_fields = array(
70 $conf->global->LDAP_KEY_MEMBERS,
71 $conf->global->LDAP_FIELD_FULLNAME,
72 $conf->global->LDAP_FIELD_LOGIN,
73 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
74 $conf->global->LDAP_FIELD_PASSWORD,
75 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
76 $conf->global->LDAP_FIELD_NAME,
77 $conf->global->LDAP_FIELD_FIRSTNAME,
78 $conf->global->LDAP_FIELD_MAIL,
79 $conf->global->LDAP_FIELD_PHONE,
80 $conf->global->LDAP_FIELD_PHONE_PERSO,
81 $conf->global->LDAP_FIELD_MOBILE,
82 $conf->global->LDAP_FIELD_FAX,
83 $conf->global->LDAP_FIELD_ADDRESS,
84 $conf->global->LDAP_FIELD_ZIP,
85 $conf->global->LDAP_FIELD_TOWN,
86 $conf->global->LDAP_FIELD_COUNTRY,
87 $conf->global->LDAP_FIELD_DESCRIPTION,
88 $conf->global->LDAP_FIELD_BIRTHDATE,
89 $conf->global->LDAP_FIELD_MEMBER_STATUS,
90 $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION,
92 $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE,
93 $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT,
94 $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE,
95 $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
99$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
101if (!isset($argv[2]) || !is_numeric($argv[2])) {
102 print
"Usage: $script_file (nocommitiferror|commitiferror) id_member_type [--server=ldapserverhost] [-y]\n";
106$typeid = (int) $argv[2];
107foreach ($argv as $key => $val) {
108 if ($val ==
'commitiferror') {
111 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
112 $conf->global->LDAP_SERVER_HOST = $reg[1];
114 if (preg_match(
'/-y$/', $val, $reg)) {
119print
"Mails sending disabled (useless in batch mode)\n";
120$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
122print
"----- Synchronize all records from LDAP database:\n";
133print
"----- To Dolibarr database:\n";
134print
"type=".$conf->db->type.
"\n";
135print
"host=".$conf->db->host.
"\n";
136print
"port=".$conf->db->port.
"\n";
137print
"login=".$conf->db->user.
"\n";
138print
"database=".$conf->db->name.
"\n";
139print
"----- Options:\n";
140print
"commitiferror=".$forcecommit.
"\n";
141print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
146 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for members not defined inside Dolibarr").
"\n";
150 print $langs->trans(
"Error").
': Parameter id_member_type is not a valid ref of an existing member type'.
"\n";
154if (!empty($dolibarr_main_db_readonly)) {
155 print
"Error: instance in read-onyl mode\n";
160 print
"Hit Enter to continue or CTRL+C to stop...\n";
161 $input = trim(fgets(STDIN));
165$hashlib2rowid = array();
167$sql =
"SELECT rowid, code, label, active";
168$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
169$sql .=
" WHERE active = 1";
170$sql .=
" ORDER BY code ASC";
171$resql = $db->query($sql);
173 $num = $db->num_rows($resql);
177 $obj = $db->fetch_object($resql);
180 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
181 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
192$result = $ldap->connectBind();
194 $justthese = array();
198 $conf->global->LDAP_MEMBER_ACTIVE = 0;
201 if (is_array($ldaprecords)) {
205 foreach ($ldaprecords as $key => $ldapuser) {
219 $member->country_id = $countries[$hashlib2rowid[strtolower($member->country)]][
'rowid'];
220 $member->country_code = $countries[$hashlib2rowid[strtolower($member->country)]][
'code'];
228 $member->morphy =
'phy';
233 $member->statut = -1;
244 $member->typeid = $typeid;
247 print $langs->transnoentities(
"MemberCreate").
' # '.$key.
': login='.$member->login.
', fullname='.$member->getFullName($langs);
248 print
', datec='.$member->datec;
249 $member_id = $member->create($user);
250 if ($member_id > 0) {
251 print
' --> Created member id='.$member_id.
' login='.$member->login;
254 print
' --> '.$member->error;
275 if ($datefirst && $datelast && $datelast <= $datefirst) {
278 if (!$pricefirst && $pricelast) {
279 $pricefirst = $pricelast;
288 $crowid = $member->subscription($datefirst, $pricefirst, 0);
295 $crowid = $member->subscription($datelast, $pricelast, 0);
299 if (!$error || $forcecommit) {
301 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
303 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
307 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
331 return (trim($element) !=
'');
Class to manage members of a foundation.
Class to manage LDAP features.
dol_time_plus_duree($time, $duration_value, $duration_unit, $ruleforendofmonth=0)
Add a delay to a date.
dol_stringtotime($string, $gm=1)
Convert a string date into a GM Timestamps date Warning: YYYY-MM-DDTHH:MM:SS+02:00 (RFC3339) is not s...
dol_getmypid()
Return getmypid() or random PID when function is disabled Some web hosts disable this php function fo...
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.
dolValidElement($element)
Function to say if a value is empty or not.