dolibarr 21.0.0-beta
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 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
28if (!defined('NOSESSION')) {
29 define('NOSESSION', '1');
30}
31
32$sapi_type = php_sapi_name();
33$script_file = basename(__FILE__);
34$path = __DIR__.'/';
35
36// Test if batch mode
37if (substr($sapi_type, 0, 3) == 'cgi') {
38 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n";
39 exit(1);
40}
41
42require_once $path."../../htdocs/master.inc.php";
43require_once DOL_DOCUMENT_ROOT.'/core/lib/functionscli.lib.php';
44require_once DOL_DOCUMENT_ROOT."/contact/class/contact.class.php";
45require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
46require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
47
56// Global variables
57$version = DOL_VERSION;
58$error = 0;
59$confirmed = 0;
60
61$hookmanager->initHooks(array('cli'));
62
63
64/*
65 * Main
66 */
67
68@set_time_limit(0);
69print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
70dol_syslog($script_file." launched with arg ".join(',', $argv));
71
72if (!isset($argv[1]) || !$argv[1]) {
73 print "Usage: $script_file now [-y]\n";
74 exit(1);
75}
76
77foreach ($argv as $key => $val) {
78 if (preg_match('/-y$/', $val, $reg)) {
79 $confirmed = 1;
80 }
81}
82
83$now = $argv[1];
84
85if (!empty($dolibarr_main_db_readonly)) {
86 print "Error: instance in read-only mode\n";
87 exit(1);
88}
89
90print "Mails sending disabled (useless in batch mode)\n";
91$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
92print "\n";
93print "----- Synchronize all records from Dolibarr database:\n";
94print "type=".$conf->db->type."\n";
95print "host=".$conf->db->host."\n";
96print "port=".$conf->db->port."\n";
97print "login=".$conf->db->user."\n";
98// print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
99print "database=".$conf->db->name."\n";
100print "\n";
101print "----- To LDAP database:\n";
102print "host=" . getDolGlobalString('LDAP_SERVER_HOST')."\n";
103print "port=" . getDolGlobalString('LDAP_SERVER_PORT')."\n";
104print "login=" . getDolGlobalString('LDAP_ADMIN_DN')."\n";
105print "pass=".preg_replace('/./i', '*', getDolGlobalString('LDAP_ADMIN_PASS'))."\n";
106print "DN target=" . getDolGlobalString('LDAP_CONTACT_DN')."\n";
107print "\n";
108
109if (!$confirmed) {
110 print "Press a key to confirm...\n";
111 $input = trim(fgets(STDIN));
112 print "Warning, this operation may result in data loss if it failed.\n";
113 print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
114 print "Hit Enter to continue or CTRL+C to stop...\n";
115 $input = trim(fgets(STDIN));
116}
117
118/*
119 * if (!getDolGlobalString('LDAP_CONTACT_ACTIVE')) {
120 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
121 * exit(1);
122 * }
123 */
124
125$sql = "SELECT rowid";
126$sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
127
128$resql = $db->query($sql);
129if ($resql) {
130 $num = $db->num_rows($resql);
131 $i = 0;
132
133 $ldap = new Ldap();
134 $ldap->connectBind();
135
136 while ($i < $num) {
137 $ldap->error = "";
138
139 $obj = $db->fetch_object($resql);
140
141 $contact = new Contact($db);
142 $contact->id = $obj->rowid;
143 $contact->fetch($contact->id);
144
145 print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
146
147 $oldobject = $contact;
148
149 $oldinfo = $oldobject->_load_ldap_info();
150 $olddn = $oldobject->_load_ldap_dn($oldinfo);
151
152 $info = $contact->_load_ldap_info();
153 $dn = $contact->_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->trans("OK");
159 } else {
160 $error++;
161 print " - ".$langs->trans("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 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