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