dolibarr 25.0.0-alpha
ldap_contacts.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
3 * Copyright (C) 2004 Sebastien Di Cintio <sdicintio@ressource-toi.org>
4 * Copyright (C) 2004 Benoit Mortier <benoit.mortier@opensides.be>
5 * Copyright (C) 2005 Regis Houssin <regis.houssin@inodbox.com>
6 * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
7 * Copyright (C) 2011-2013 Juanjo Menent <jmenent@2byte.es>
8 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Load Dolibarr environment
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
35require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
36
45// Load translation files required by the page
46$langs->loadLangs(array("admin"));
47
48if (!$user->admin) {
50}
51
52$action = GETPOST('action', 'aZ09');
53
54/*
55 * Actions
56 */
57
58if ($action == 'setvalue' /* && $user->admin */) {
59 $error = 0;
60
61 $db->begin();
62
63 if (!dolibarr_set_const($db, 'LDAP_CONTACT_DN', GETPOST("contactdn"), 'chaine', 0, '', $conf->entity)) {
64 $error++;
65 }
66 if (!dolibarr_set_const($db, 'LDAP_CONTACT_OBJECT_CLASS', GETPOST("objectclass"), 'chaine', 0, '', $conf->entity)) {
67 $error++;
68 }
69
70 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FULLNAME', GETPOST("fieldfullname"), 'chaine', 0, '', $conf->entity)) {
71 $error++;
72 }
73 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_NAME', GETPOST("fieldname"), 'chaine', 0, '', $conf->entity)) {
74 $error++;
75 }
76 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FIRSTNAME', GETPOST("fieldfirstname"), 'chaine', 0, '', $conf->entity)) {
77 $error++;
78 }
79 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COMPANY', GETPOST("fieldcompany"), 'chaine', 0, '', $conf->entity)) {
80 $error++;
81 }
82 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MAIL', GETPOST("fieldmail"), 'chaine', 0, '', $conf->entity)) {
83 $error++;
84 }
85 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_PHONE', GETPOST("fieldphone"), 'chaine', 0, '', $conf->entity)) {
86 $error++;
87 }
88 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_HOMEPHONE', GETPOST("fieldhomephone"), 'chaine', 0, '', $conf->entity)) {
89 $error++;
90 }
91 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MOBILE', GETPOST("fieldmobile"), 'chaine', 0, '', $conf->entity)) {
92 $error++;
93 }
94 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FAX', GETPOST("fieldfax"), 'chaine', 0, '', $conf->entity)) {
95 $error++;
96 }
97 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ADDRESS', GETPOST("fieldaddress"), 'chaine', 0, '', $conf->entity)) {
98 $error++;
99 }
100 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ZIP', GETPOST("fieldzip"), 'chaine', 0, '', $conf->entity)) {
101 $error++;
102 }
103 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_TOWN', GETPOST("fieldtown"), 'chaine', 0, '', $conf->entity)) {
104 $error++;
105 }
106 if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COUNTRY', GETPOST("fieldcountry"), 'chaine', 0, '', $conf->entity)) {
107 $error++;
108 }
109
110 // This one must be after the others
111 $valkey = '';
112 $key = GETPOST("key");
113 if ($key) {
114 $valkey = getDolGlobalString($key);
115 }
116 if (!dolibarr_set_const($db, 'LDAP_KEY_CONTACTS', $valkey, 'chaine', 0, '', $conf->entity)) {
117 $error++;
118 }
119
120 if (!$error) {
121 $db->commit();
122 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
123 } else {
124 $db->rollback();
125 dol_print_error($db);
126 }
127}
128
129
130
131/*
132 * View
133 */
134
135$form = new Form($db);
136
137llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP', '', 0, 0, '', '', '', 'mod-admin page-ldap_contacts');
138
139$linkback = '<a href="'.dolBuildUrl(DOL_URL_ROOT.'/admin/modules.php', ['restore_lastsearch_values' => 1]).'">'.img_picto($langs->trans("BackToModuleList"), 'back', 'class="pictofixedwidth"').'<span class="hideonsmartphone">'.$langs->trans("BackToModuleList").'</span></a>';
140
141print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
142
143$head = ldap_prepare_head();
144
145// Test si function LDAP actives
146if (!function_exists("ldap_connect")) {
147 setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
148}
149
150print dol_get_fiche_head($head, 'contacts', '', -1);
151
152
153print '<span class="opacitymedium">'.$langs->trans("LDAPDescContact").'</span><br>';
154print '<br>';
155
156print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'" spellcheck="false">';
157print '<input type="hidden" name="token" value="'.newToken().'">';
158
159print '<table class="noborder centpercent">';
160
161print '<tr class="liste_titre">';
162print '<td colspan="4">'.$langs->trans("LDAPSynchronizeContacts").'</td>';
163print "</tr>\n";
164
165// DN For the contacts
166print '<!-- LDAP_CONTACT_DN -->';
167print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactDn").'</span></td><td>';
168print '<input size="48" type="text" name="contactdn" value="' . getDolGlobalString('LDAP_CONTACT_DN').'">';
169print '</td><td>'.$langs->trans("LDAPContactDnExample").'</td>';
170print '<td>&nbsp;</td>';
171print '</tr>';
172
173// List of object class used to define attributes in structure
174print '<!-- LDAP_CONTACT_OBJECT_CLASS -->';
175print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactObjectClassList").'</span></td><td>';
176print '<input size="48" type="text" name="objectclass" value="' . getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS').'">';
177print '</td><td>'.$langs->trans("LDAPContactObjectClassListExample").'</td>';
178print '<td>&nbsp;</td>';
179print '</tr>';
180
181print '</table>';
182print '<br>';
183print '<table class="noborder centpercent">';
184
185print '<tr class="liste_titre">';
186print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
187print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
188print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
189print "</tr>\n";
190
191// Common name
192print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFullname").'</td><td>';
193print '<input size="25" type="text" name="fieldfullname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME').'">';
194print '</td><td>'.$langs->trans("LDAPFieldFullnameExample").'</td>';
195print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FULLNAME"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME') ? ' checked' : '')."></td>";
196print '</tr>';
197
198// Name
199print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
200print '<input size="25" type="text" name="fieldname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_NAME').'">';
201print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
202print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_NAME"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_NAME') ? ' checked' : '')."></td>";
203print '</tr>';
204
205// Firstname
206print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFirstName").'</td><td>';
207print '<input size="25" type="text" name="fieldfirstname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME').'">';
208print '</td><td>'.$langs->trans("LDAPFieldFirstNameExample").'</td>';
209print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FIRSTNAME"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME') ? ' checked' : '')."></td>";
210print '</tr>';
211
212// Company
213print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCompany").'</td><td>';
214print '<input size="25" type="text" name="fieldcompany" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY').'">';
215print '</td><td>'.$langs->trans("LDAPFieldCompanyExample").'</td>';
216print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COMPANY"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY') ? ' checked' : '')."></td>";
217print '</tr>';
218
219// Mail
220print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMail").'</td><td>';
221print '<input size="25" type="text" name="fieldmail" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_MAIL').'">';
222print '</td><td>'.$langs->trans("LDAPFieldMailExample").'</td>';
223print '<td class="right"><input type="radio" name="key" value=">LDAP_CONTACT_FIELD_MAIL"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_MAIL') ? ' checked' : '')."></td>";
224print '</tr>';
225
226// Phone pro
227print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldPhone").'</td><td>';
228print '<input size="25" type="text" name="fieldphone" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_PHONE').'">';
229print '</td><td>'.$langs->trans("LDAPFieldPhoneExample").'</td>';
230print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_PHONE"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_PHONE') ? ' checked' : '')."></td>";
231print '</tr>';
232
233// Phone home
234print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldHomePhone").'</td><td>';
235print '<input size="25" type="text" name="fieldhomephone" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE').'">';
236print '</td><td>'.$langs->trans("LDAPFieldHomePhoneExample").'</td>';
237print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_HOMEPHONE"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE') ? ' checked' : '')."></td>";
238print '</tr>';
239
240// Mobile
241print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMobile").'</td><td>';
242print '<input size="25" type="text" name="fieldmobile" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE').'">';
243print '</td><td>'.$langs->trans("LDAPFieldMobileExample").'</td>';
244print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_MOBILE"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE') ? ' checked' : '')."></td>";
245print '</tr>';
246
247// Fax
248print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFax").'</td><td>';
249print '<input size="25" type="text" name="fieldfax" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FAX').'">';
250print '</td><td>'.$langs->trans("LDAPFieldFaxExample").'</td>';
251print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FAX"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_FAX') ? ' checked' : '')."></td>";
252print '</tr>';
253
254// Address
255print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldAddress").'</td><td>';
256print '<input size="25" type="text" name="fieldaddress" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS').'">';
257print '</td><td>'.$langs->trans("LDAPFieldAddressExample").'</td>';
258print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ADDRESS"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS') ? ' checked' : '')."></td>";
259print '</tr>';
260
261// ZIP
262print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldZip").'</td><td>';
263print '<input size="25" type="text" name="fieldzip" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_ZIP').'">';
264print '</td><td>'.$langs->trans("LDAPFieldZipExample").'</td>';
265print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ZIP"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_ZIP') ? ' checked' : '')."></td>";
266print '</tr>';
267
268// TOWN
269print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldTown").'</td><td>';
270print '<input size="25" type="text" name="fieldtown" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_TOWN').'">';
271print '</td><td>'.$langs->trans("LDAPFieldTownExample").'</td>';
272print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_TOWN"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_TOWN') ? ' checked' : '')."></td>";
273print '</tr>';
274
275// COUNTRY
276print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCountry").'</td><td>';
277print '<input size="25" type="text" name="fieldcountry" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY').'">';
278print '</td><td>&nbsp;</td>';
279print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COUNTRY"'.(getDolGlobalString('LDAP_KEY_CONTACTS') && getDolGlobalString('LDAP_KEY_CONTACTS') == getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY') ? ' checked' : '')."></td>";
280print '</tr>';
281
282print '</table>';
283
284print info_admin($langs->trans("LDAPDescValues"));
285
286print dol_get_fiche_end();
287
288print $form->buttonsSaveCancel("Modify", '');
289
290print '</form>';
291
292
293/*
294 * Test the connection
295 */
296$butlabel = $langs->trans("LDAPTestSynchroContact");
297$testlabel = 'test';
298$key = getDolGlobalString('LDAP_KEY_CONTACTS');
299$dn = getDolGlobalString('LDAP_CONTACT_DN');
300$objectclass = getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS');
301
302show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
303
304
305if (function_exists("ldap_connect")) {
306 if ($action == 'test') {
307 // Create object
308 $object = new Contact($db);
309 $object->initAsSpecimen();
310
311 // Test synchro
312 $ldap = new Ldap();
313 $result = $ldap->connectBind();
314
315 if ($result > 0) {
316 $info = $object->_load_ldap_info();
317 $dn = $object->_load_ldap_dn($info);
318
319 $result1 = $ldap->delete($dn); // To be sure to delete existing records
320 $result2 = $ldap->add($dn, $info, $user); // Now the test
321 $result3 = $ldap->delete($dn); // Clean what we did
322
323 if ($result2 > 0) {
324 print img_picto('', 'info').' ';
325 print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
326 } else {
327 print img_picto('', 'error').' ';
328 print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
329 print ': '.$ldap->error;
330 print '</span><br>';
331 $langs->load('errors');
332 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
333 }
334
335 print "<br>\n";
336 print "LDAP input file used for test:<br><br>\n";
337 print nl2br($ldap->dumpContent($dn, $info));
338 print "\n<br>";
339 } else {
340 print img_picto('', 'error').' ';
341 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
342 print ': '.$ldap->error;
343 print '</span><br>';
344 $langs->load('errors');
345 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
346 }
347 }
348}
349
350// End of page
351llxFooter();
352$db->close();
if(! $sortfield) if(! $sortorder) $object
Definition account.php:100
dolibarr_set_const($db, $name, $value, $type='chaine', $visible=0, $note='', $entity=1)
Insert a parameter (key,value) into database (delete old key then insert it again).
llxFooter($comment='', $zone='private', $disabledoutputofmessages=0)
Empty footer.
Definition wrapper.php:91
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:73
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Class to manage contact/addresses.
Class to manage generation of HTML components Only common components must be here.
Class to manage LDAP features.
newToken()
Return the value of token currently saved into session with name 'newtoken'.
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0, $nodefault=0)
Return value of a param into GET or POST supervariable.
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0, $morecssdiv='')
Show tabs of a record.
Definition html.lib.php:542
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition html.lib.php:740
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='', $morecssonpicto='widthpictotitle')
Load a title with picto.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='', $textonpictotooltip='', $cssfordropdown='info_admin')
Show information in HTML for admin users or standard users.
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition ldap.lib.php:33
show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass)
Show button test LDAP synchro.
Definition ldap.lib.php:104
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.