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