dolibarr 23.0.3
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
59// Global variables
60$version = 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
86$now = $argv[1];
87
88if (!empty($dolibarr_main_db_readonly)) {
89 print "Error: instance in read-only mode\n";
90 exit(1);
91}
92
93print "Mails sending disabled (useless in batch mode)\n";
94$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
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_CONTACT_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_CONTACT_ACTIVE')) {
123 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
124 * exit(1);
125 * }
126 */
127
128$sql = "SELECT rowid";
129$sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
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 $contact = new Contact($db);
145 $contact->id = $obj->rowid;
146 $contact->fetch($contact->id);
147
148 print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
149
150 $oldobject = $contact;
151
152 $oldinfo = $oldobject->_load_ldap_info();
153 $olddn = $oldobject->_load_ldap_dn($oldinfo);
154
155 $info = $contact->_load_ldap_info();
156 $dn = $contact->_load_ldap_dn($info);
157
158 $result = $ldap->add($dn, $info, $user); // Will fail if already exists
159 $result = $ldap->update($dn, $info, $user, $olddn);
160 if ($result > 0) {
161 print " - ".$langs->trans("OK");
162 } else {
163 $error++;
164 print " - ".$langs->trans("KO").' - '.$ldap->error;
165 }
166 print "\n";
167
168 $i++;
169 }
170
171 $ldap->unbind();
172} else {
173 dol_print_error($db);
174}
175
176exit($error);
Class to manage contact/addresses.
Class to manage LDAP features.
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.