27if (!defined(
'NOSESSION')) {
28 define(
'NOSESSION',
'1');
31$sapi_type = php_sapi_name();
32$script_file = basename(__FILE__);
36if (substr($sapi_type, 0, 3) ==
'cgi') {
37 echo
"Error: You are using PHP for CGI. To execute ".$script_file.
" from command line, you must use PHP for CLI mode.\n";
41require_once $path.
"../../htdocs/master.inc.php";
42require_once DOL_DOCUMENT_ROOT.
"/core/lib/date.lib.php";
43require_once DOL_DOCUMENT_ROOT.
"/core/class/ldap.class.php";
44require_once DOL_DOCUMENT_ROOT.
"/adherents/class/adherent.class.php";
45require_once DOL_DOCUMENT_ROOT.
"/adherents/class/subscription.class.php";
47$langs->loadLangs(array(
"main",
"errors"));
50$version = constant(
'DOL_VERSION');
55$hookmanager->initHooks(array(
'cli'));
63print
"***** ".$script_file.
" (".$version.
") pid=".
dol_getmypid().
" *****\n";
64dol_syslog($script_file.
" launched with arg ".join(
',', $argv));
67$required_fields = array(
68 $conf->global->LDAP_KEY_MEMBERS,
69 $conf->global->LDAP_FIELD_FULLNAME,
70 $conf->global->LDAP_FIELD_LOGIN,
71 $conf->global->LDAP_FIELD_LOGIN_SAMBA,
72 $conf->global->LDAP_FIELD_PASSWORD,
73 $conf->global->LDAP_FIELD_PASSWORD_CRYPTED,
74 $conf->global->LDAP_FIELD_NAME,
75 $conf->global->LDAP_FIELD_FIRSTNAME,
76 $conf->global->LDAP_FIELD_MAIL,
77 $conf->global->LDAP_FIELD_PHONE,
78 $conf->global->LDAP_FIELD_PHONE_PERSO,
79 $conf->global->LDAP_FIELD_MOBILE,
80 $conf->global->LDAP_FIELD_FAX,
81 $conf->global->LDAP_FIELD_ADDRESS,
82 $conf->global->LDAP_FIELD_ZIP,
83 $conf->global->LDAP_FIELD_TOWN,
84 $conf->global->LDAP_FIELD_COUNTRY,
85 $conf->global->LDAP_FIELD_DESCRIPTION,
86 $conf->global->LDAP_FIELD_BIRTHDATE,
87 $conf->global->LDAP_FIELD_MEMBER_STATUS,
88 $conf->global->LDAP_FIELD_MEMBER_END_LASTSUBSCRIPTION,
90 $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_DATE,
91 $conf->global->LDAP_FIELD_MEMBER_FIRSTSUBSCRIPTION_AMOUNT,
92 $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_DATE,
93 $conf->global->LDAP_FIELD_MEMBER_LASTSUBSCRIPTION_AMOUNT
97$required_fields = array_unique(array_values(array_filter($required_fields,
"dolValidElement")));
99if (!isset($argv[2]) || !is_numeric($argv[2])) {
100 print
"Usage: $script_file (nocommitiferror|commitiferror) id_member_type [--server=ldapserverhost] [-y]\n";
104$typeid = (int) $argv[2];
105foreach ($argv as $key => $val) {
106 if ($val ==
'commitiferror') {
109 if (preg_match(
'/--server=([^\s]+)$/', $val, $reg)) {
110 $conf->global->LDAP_SERVER_HOST = $reg[1];
112 if (preg_match(
'/-y$/', $val, $reg)) {
117print
"Mails sending disabled (useless in batch mode)\n";
118$conf->global->MAIN_DISABLE_ALL_MAILS = 1;
120print
"----- Synchronize all records from LDAP database:\n";
131print
"----- To Dolibarr database:\n";
132print
"type=".$conf->db->type.
"\n";
133print
"host=".$conf->db->host.
"\n";
134print
"port=".$conf->db->port.
"\n";
135print
"login=".$conf->db->user.
"\n";
136print
"database=".$conf->db->name.
"\n";
137print
"----- Options:\n";
138print
"commitiferror=".$forcecommit.
"\n";
139print
"Mapped LDAP fields=".join(
',', $required_fields).
"\n";
144 print $langs->trans(
"Error").
': '.$langs->trans(
"LDAP setup for members not defined inside Dolibarr").
"\n";
148 print $langs->trans(
"Error").
': Parameter id_member_type is not a valid ref of an existing member type'.
"\n";
152if (!empty($dolibarr_main_db_readonly)) {
153 print
"Error: instance in read-onyl mode\n";
158 print
"Hit Enter to continue or CTRL+C to stop...\n";
159 $input = trim(fgets(STDIN));
163$hashlib2rowid = array();
165$sql =
"SELECT rowid, code, label, active";
166$sql .=
" FROM ".MAIN_DB_PREFIX.
"c_country";
167$sql .=
" WHERE active = 1";
168$sql .=
" ORDER BY code ASC";
169$resql = $db->query($sql);
171 $num = $db->num_rows($resql);
175 $obj = $db->fetch_object($resql);
178 $hashlib2rowid[strtolower($obj->label)] = $obj->rowid;
179 $countries[$obj->rowid] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'code' => $obj->code);
190$result = $ldap->connectBind();
192 $justthese = array();
196 $conf->global->LDAP_MEMBER_ACTIVE = 0;
199 if (is_array($ldaprecords)) {
203 foreach ($ldaprecords as $key => $ldapuser) {
217 $member->country_id = $countries[$hashlib2rowid[strtolower($member->country)]][
'rowid'];
218 $member->country_code = $countries[$hashlib2rowid[strtolower($member->country)]][
'code'];
226 $member->morphy =
'phy';
231 $member->statut = -1;
242 $member->typeid = $typeid;
245 print $langs->transnoentities(
"MemberCreate").
' # '.$key.
': login='.$member->login.
', fullname='.$member->getFullName($langs);
246 print
', datec='.$member->datec;
247 $member_id = $member->create($user);
248 if ($member_id > 0) {
249 print
' --> Created member id='.$member_id.
' login='.$member->login;
252 print
' --> '.$member->error;
273 if ($datefirst && $datelast && $datelast <= $datefirst) {
276 if (!$pricefirst && $pricelast) {
277 $pricefirst = $pricelast;
286 $crowid = $member->subscription($datefirst, $pricefirst, 0);
293 $crowid = $member->subscription($datelast, $pricelast, 0);
297 if (!$error || $forcecommit) {
299 print $langs->transnoentities(
"NoErrorCommitIsDone").
"\n";
301 print $langs->transnoentities(
"ErrorButCommitIsDone").
"\n";
305 print $langs->transnoentities(
"ErrorSomeErrorWereFoundRollbackIsDone", $error).
"\n";
329 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 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.