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