dolibarr 21.0.0-beta
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 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", "errors"));
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$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
139
140print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
141
142$head = ldap_prepare_head();
143
144// Test si function LDAP actives
145if (!function_exists("ldap_connect")) {
146 setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
147}
148
149print dol_get_fiche_head($head, 'contacts', '', -1);
150
151
152print '<span class="opacitymedium">'.$langs->trans("LDAPDescContact").'</span><br>';
153print '<br>';
154
155print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
156print '<input type="hidden" name="token" value="'.newToken().'">';
157
158print '<table class="noborder centpercent">';
159
160print '<tr class="liste_titre">';
161print '<td colspan="4">'.$langs->trans("LDAPSynchronizeContacts").'</td>';
162print "</tr>\n";
163
164// DN Pour les contacts
165print '<!-- LDAP_CONTACT_DN -->';
166print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactDn").'</span></td><td>';
167print '<input size="48" type="text" name="contactdn" value="' . getDolGlobalString('LDAP_CONTACT_DN').'">';
168print '</td><td>'.$langs->trans("LDAPContactDnExample").'</td>';
169print '<td>&nbsp;</td>';
170print '</tr>';
171
172// List of object class used to define attributes in structure
173print '<!-- LDAP_CONTACT_OBJECT_CLASS -->';
174print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactObjectClassList").'</span></td><td>';
175print '<input size="48" type="text" name="objectclass" value="' . getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS').'">';
176print '</td><td>'.$langs->trans("LDAPContactObjectClassListExample").'</td>';
177print '<td>&nbsp;</td>';
178print '</tr>';
179
180print '</table>';
181print '<br>';
182print '<table class="noborder centpercent">';
183
184print '<tr class="liste_titre">';
185print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
186print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
187print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
188print "</tr>\n";
189
190// Common name
191print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFullname").'</td><td>';
192print '<input size="25" type="text" name="fieldfullname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FULLNAME').'">';
193print '</td><td>'.$langs->trans("LDAPFieldFullnameExample").'</td>';
194print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FULLNAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_FULLNAME ? ' checked' : '')."></td>";
195print '</tr>';
196
197// Name
198print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
199print '<input size="25" type="text" name="fieldname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_NAME').'">';
200print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
201print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_NAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_NAME ? ' checked' : '')."></td>";
202print '</tr>';
203
204// Firstname
205print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFirstName").'</td><td>';
206print '<input size="25" type="text" name="fieldfirstname" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FIRSTNAME').'">';
207print '</td><td>'.$langs->trans("LDAPFieldFirstNameExample").'</td>';
208print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FIRSTNAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_FIRSTNAME ? ' checked' : '')."></td>";
209print '</tr>';
210
211// Company
212print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCompany").'</td><td>';
213print '<input size="25" type="text" name="fieldcompany" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_COMPANY').'">';
214print '</td><td>'.$langs->trans("LDAPFieldCompanyExample").'</td>';
215print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COMPANY"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_COMPANY ? ' checked' : '')."></td>";
216print '</tr>';
217
218// Mail
219print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMail").'</td><td>';
220print '<input size="25" type="text" name="fieldmail" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_MAIL').'">';
221print '</td><td>'.$langs->trans("LDAPFieldMailExample").'</td>';
222print '<td class="right"><input type="radio" name="key" value=">LDAP_CONTACT_FIELD_MAIL"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_MAIL ? ' checked' : '')."></td>";
223print '</tr>';
224
225// Phone pro
226print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldPhone").'</td><td>';
227print '<input size="25" type="text" name="fieldphone" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_PHONE').'">';
228print '</td><td>'.$langs->trans("LDAPFieldPhoneExample").'</td>';
229print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_PHONE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_PHONE ? ' checked' : '')."></td>";
230print '</tr>';
231
232// Phone home
233print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldHomePhone").'</td><td>';
234print '<input size="25" type="text" name="fieldhomephone" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_HOMEPHONE').'">';
235print '</td><td>'.$langs->trans("LDAPFieldHomePhoneExample").'</td>';
236print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_HOMEPHONE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_HOMEPHONE ? ' checked' : '')."></td>";
237print '</tr>';
238
239// Mobile
240print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMobile").'</td><td>';
241print '<input size="25" type="text" name="fieldmobile" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_MOBILE').'">';
242print '</td><td>'.$langs->trans("LDAPFieldMobileExample").'</td>';
243print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_MOBILE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_MOBILE ? ' checked' : '')."></td>";
244print '</tr>';
245
246// Fax
247print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFax").'</td><td>';
248print '<input size="25" type="text" name="fieldfax" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_FAX').'">';
249print '</td><td>'.$langs->trans("LDAPFieldFaxExample").'</td>';
250print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FAX"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_FAX ? ' checked' : '')."></td>";
251print '</tr>';
252
253// Address
254print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldAddress").'</td><td>';
255print '<input size="25" type="text" name="fieldaddress" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_ADDRESS').'">';
256print '</td><td>'.$langs->trans("LDAPFieldAddressExample").'</td>';
257print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ADDRESS"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_ADDRESS ? ' checked' : '')."></td>";
258print '</tr>';
259
260// ZIP
261print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldZip").'</td><td>';
262print '<input size="25" type="text" name="fieldzip" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_ZIP').'">';
263print '</td><td>'.$langs->trans("LDAPFieldZipExample").'</td>';
264print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ZIP"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_ZIP ? ' checked' : '')."></td>";
265print '</tr>';
266
267// TOWN
268print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldTown").'</td><td>';
269print '<input size="25" type="text" name="fieldtown" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_TOWN').'">';
270print '</td><td>'.$langs->trans("LDAPFieldTownExample").'</td>';
271print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_TOWN"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_TOWN ? ' checked' : '')."></td>";
272print '</tr>';
273
274// COUNTRY
275print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCountry").'</td><td>';
276print '<input size="25" type="text" name="fieldcountry" value="' . getDolGlobalString('LDAP_CONTACT_FIELD_COUNTRY').'">';
277print '</td><td>&nbsp;</td>';
278print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COUNTRY"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_COUNTRY ? ' checked' : '')."></td>";
279print '</tr>';
280
281print '</table>';
282
283print info_admin($langs->trans("LDAPDescValues"));
284
285print dol_get_fiche_end();
286
287print $form->buttonsSaveCancel("Modify", '');
288
289print '</form>';
290
291
292/*
293 * Test the connection
294 */
295$butlabel = $langs->trans("LDAPTestSynchroContact");
296$testlabel = 'test';
297$key = getDolGlobalString('LDAP_KEY_CONTACTS');
298$dn = getDolGlobalString('LDAP_CONTACT_DN');
299$objectclass = getDolGlobalString('LDAP_CONTACT_OBJECT_CLASS');
300
301show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
302
303
304if (function_exists("ldap_connect")) {
305 if ($action == 'test') {
306 // Create object
307 $object = new Contact($db);
308 $object->initAsSpecimen();
309
310 // Test synchro
311 $ldap = new Ldap();
312 $result = $ldap->connectBind();
313
314 if ($result > 0) {
315 $info = $object->_load_ldap_info();
316 $dn = $object->_load_ldap_dn($info);
317
318 $result1 = $ldap->delete($dn); // To be sure to delete existing records
319 $result2 = $ldap->add($dn, $info, $user); // Now the test
320 $result3 = $ldap->delete($dn); // Clean what we did
321
322 if ($result2 > 0) {
323 print img_picto('', 'info').' ';
324 print '<span class="ok">'.$langs->trans("LDAPSynchroOK").'</span><br>';
325 } else {
326 print img_picto('', 'error').' ';
327 print '<span class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
328 print ': '.$ldap->error;
329 print '</span><br>';
330 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
331 }
332
333 print "<br>\n";
334 print "LDAP input file used for test:<br><br>\n";
335 print nl2br($ldap->dumpContent($dn, $info));
336 print "\n<br>";
337 } else {
338 print img_picto('', 'error').' ';
339 print '<span class="error">'.$langs->trans("LDAPSynchroKO");
340 print ': '.$ldap->error;
341 print '</span><br>';
342 print $langs->trans("ErrorLDAPMakeManualTest", $conf->ldap->dir_temp).'<br>';
343 }
344 }
345}
346
347// End of page
348llxFooter();
349$db->close();
if( $user->socid > 0) if(! $user->hasRight('accounting', 'chartofaccount')) $object
Definition card.php:66
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).
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:71
Class to manage contact/addresses.
Class to manage generation of HTML components Only common components must be here.
Class to manage LDAP features.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='', $dragdropfile=0)
Show tabs of a record.
dol_get_fiche_end($notab=0)
Return tab footer of a card.
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)
Return value of a param into GET or POST supervariable.
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.
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='', $picto='')
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
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.