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