dolibarr 22.0.5
sync_contacts_dolibarr2ldap.php
Go to the documentation of this file.
1#!/usr/bin/env php
2<?php
3/*
4 * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
5 * Copyright (C) 2006-2009 Laurent Destailleur <eldy@users.sourceforge.net>
6 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
7 * Copyright (C) 2025 MDW <mdeweerd@users.noreply.github.com>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
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."/contact/class/contact.class.php";
46require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
47require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
48
57// Global variables
58$version = 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
84$now = $argv[1];
85
86if (!empty($dolibarr_main_db_readonly)) {
87 print "Error: instance in read-only mode\n";
88 exit(1);
89}
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_CONTACT_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_CONTACT_ACTIVE')) {
121 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
122 * exit(1);
123 * }
124 */
125
126$sql = "SELECT rowid";
127$sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
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 $contact = new Contact($db);
143 $contact->id = $obj->rowid;
144 $contact->fetch($contact->id);
145
146 print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
147
148 $oldobject = $contact;
149
150 $oldinfo = $oldobject->_load_ldap_info();
151 $olddn = $oldobject->_load_ldap_dn($oldinfo);
152
153 $info = $contact->_load_ldap_info();
154 $dn = $contact->_load_ldap_dn($info);
155
156 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
157 $result = $ldap->update($dn, $info, $user, $olddn);
158 if ($result > 0) {
159 print " - ".$langs->trans("OK");
160 } else {
161 $error++;
162 print " - ".$langs->trans("KO").' - '.$ldap->error;
163 }
164 print "\n";
165
166 $i++;
167 }
168
169 $ldap->unbind();
170} else {
171 dol_print_error($db);
172}
173
174exit($error);
Class to manage contact/addresses.
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