dolibarr 18.0.6
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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
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."/contact/class/contact.class.php";
43require_once DOL_DOCUMENT_ROOT."/user/class/user.class.php";
44require_once DOL_DOCUMENT_ROOT."/core/class/ldap.class.php";
45
46// Global variables
47$version = DOL_VERSION;
48$error = 0;
49$confirmed = 0;
50
51/*
52 * Main
53 */
54
55@set_time_limit(0);
56print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n";
57dol_syslog($script_file." launched with arg ".join(',', $argv));
58
59if (!isset($argv[1]) || !$argv[1]) {
60 print "Usage: $script_file now [-y]\n";
61 exit(-1);
62}
63
64foreach ($argv as $key => $val) {
65 if (preg_match('/-y$/', $val, $reg)) {
66 $confirmed = 1;
67 }
68}
69
70$now = $argv[1];
71
72if (!empty($dolibarr_main_db_readonly)) {
73 print "Error: instance in read-onyl mode\n";
74 exit(-1);
75}
76
77print "Mails sending disabled (useless in batch mode)\n";
78$conf->global->MAIN_DISABLE_ALL_MAILS = 1; // On bloque les mails
79print "\n";
80print "----- Synchronize all records from Dolibarr database:\n";
81print "type=".$conf->db->type."\n";
82print "host=".$conf->db->host."\n";
83print "port=".$conf->db->port."\n";
84print "login=".$conf->db->user."\n";
85// print "pass=".preg_replace('/./i','*',$conf->db->password)."\n"; // Not defined for security reasons
86print "database=".$conf->db->name."\n";
87print "\n";
88print "----- To LDAP database:\n";
89print "host=".$conf->global->LDAP_SERVER_HOST."\n";
90print "port=".$conf->global->LDAP_SERVER_PORT."\n";
91print "login=".$conf->global->LDAP_ADMIN_DN."\n";
92print "pass=".preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)."\n";
93print "DN target=".$conf->global->LDAP_CONTACT_DN."\n";
94print "\n";
95
96if (!$confirmed) {
97 print "Press a key to confirm...\n";
98 $input = trim(fgets(STDIN));
99 print "Warning, this operation may result in data loss if it failed.\n";
100 print "Be sure to have a backup of your LDAP database (With OpenLDAP: slapcat > save.ldif).\n";
101 print "Hit Enter to continue or CTRL+C to stop...\n";
102 $input = trim(fgets(STDIN));
103}
104
105/*
106 * if (! $conf->global->LDAP_CONTACT_ACTIVE)
107 * {
108 * print $langs->trans("LDAPSynchronizationNotSetupInDolibarr");
109 * exit(-1);
110 * }
111 */
112
113$sql = "SELECT rowid";
114$sql .= " FROM ".MAIN_DB_PREFIX."socpeople";
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 $contact = new Contact($db);
130 $contact->id = $obj->rowid;
131 $contact->fetch($contact->id);
132
133 print $langs->trans("UpdateContact")." rowid=".$contact->id." ".$contact->getFullName($langs);
134
135 $oldobject = $contact;
136
137 $oldinfo = $oldobject->_load_ldap_info();
138 $olddn = $oldobject->_load_ldap_dn($oldinfo);
139
140 $info = $contact->_load_ldap_info();
141 $dn = $contact->_load_ldap_dn($info);
142
143 $result = $ldap->add($dn, $info, $user); // Wil fail if already exists
144 $result = $ldap->update($dn, $info, $user, $olddn);
145 if ($result > 0) {
146 print " - ".$langs->trans("OK");
147 } else {
148 $error++;
149 print " - ".$langs->trans("KO").' - '.$ldap->error;
150 }
151 print "\n";
152
153 $i++;
154 }
155
156 $ldap->unbind();
157} else {
158 dol_print_error($db);
159}
160
161exit($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='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.