dolibarr 22.0.5
sync_members_dolibarr2ldap.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
29if (!defined('NOSESSION')) {
30 define('NOSESSION', '1');
31}
32
33$sapi_type = php_sapi_name();
34$script_file = basename(__FILE__);
35$path = __DIR__.'/';
36
37// Test if batch mode
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";
40 exit(1);
41}
42
43require_once $path."../../htdocs/master.inc.php";
44require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
45require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
46require_once DOL_DOCUMENT_ROOT."/adherents/class/adherent.class.php";
47
55$langs->load("main");
56
57// Global variables
58$version = constant('DOL_VERSION');
59$error = 0;
60$confirmed = 0;
61
62$hookmanager->initHooks(array('cli'));
63
64
65/*
66 * Main
67 */
68
69@set_time_limit(0);
70print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
71dol_syslog($script_file." launched with arg ".implode(',', $argv));
72
73if (!isset($argv[1]) || !$argv[1]) {
74 print "Usage: $script_file now [-y]\n";
75 exit(1);
76}
77
78foreach ($argv as $key => $val) {
79 if (preg_match('/-y$/', $val, $reg)) {
80 $confirmed = 1;
81 }
82}
83
84if (!empty($dolibarr_main_db_readonly)) {
85 print "Error: instance in read-onyl mode\n";
86 exit(1);
87}
88
89$now = $argv[1];
90
91print "Mails sending disabled (useless in batch mode)\n";
92$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
93print "\n";
94print "----- Synchronize all records from Dolibarr database:\n";
95print "type=".$conf->db->type."\n";
96print "host=".$conf->db->host."\n";
97print "port=".$conf->db->port."\n";
98print "login=".$conf->db->user."\n";
99// print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
100print "database=".$conf->db->name."\n";
101print "\n";
102print "----- To LDAP database:\n";
103print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
104print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
105print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
106print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
107print "DN target=" . getDolGlobalString('LDAP_MEMBER_DN')."\n";
108print "\n";
109
110if (!$confirmed) {
111 print "Press a key to confirm...\n";
112 $input = trim(fgets(STDIN));
113 print "Warning, this operation may result in data loss if it failed.\n";
114 print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
115 print "Hit Enter to continue or CTRL+C to stop...\n";
116 $input = trim(fgets(STDIN));
117}
118
119/*
120 * if (getDolGlobalString('LDAP_MEMBER_ACTIVE') {
121 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
122 * exit(1);
123 * }
124 */
125
126$sql = "SELECT rowid";
127$sql .= " FROM ".MAIN_DB_PREFIX."adherent";
128
129$resql = $db->query($sql);
130if ($resql) {
131 $num = $db->num_rows($resql);
132 $i = 0;
133
134 $ldap = new Ldap();
135 $ldap->connectBind();
136
137 while ($i < $num) {
138 $ldap->error = "";
139
140 $obj = $db->fetch_object($resql);
141
142 $member = new Adherent($db);
143 $result = $member->fetch($obj->rowid);
144 if ($result < 0) {
145 dol_print_error($db, $member->error);
146 exit(1);
147 }
148 $result = $member->fetch_subscriptions();
149 if ($result < 0) {
150 dol_print_error($db, $member->error);
151 exit(1);
152 }
153
154 print $langs->transnoentities("UpdateMember")." rowid=".$member->id." ".$member->getFullName($langs);
155
156 $oldobject = $member;
157
158 $oldinfo = $oldobject->_load_ldap_info();
159 $olddn = $oldobject->_load_ldap_dn($oldinfo);
160
161 $info = $member->_load_ldap_info();
162 $dn = $member->_load_ldap_dn($info);
163
164 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
165 $result = $ldap->update($dn, $info, $user, $olddn);
166 if ($result > 0) {
167 print " - ".$langs->transnoentities("OK");
168 } else {
169 $error++;
170 print " - ".$langs->transnoentities("KO").' - '.$ldap->error;
171 }
172 print "\n";
173
174 $i++;
175 }
176
177 $ldap->unbind();
178} else {
179 dol_print_error($db);
180}
181
182exit($error);
Class to manage members of a foundation.
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=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.
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79