dolibarr  16.0.5
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  *
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 
29 require '../main.inc.php';
30 require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
31 require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
32 require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
33 require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("admin", "errors"));
37 
38 if (!$user->admin) {
40 }
41 
42 $action = GETPOST('action', 'aZ09');
43 
44 /*
45  * Actions
46  */
47 
48 if ($action == 'setvalue' && $user->admin) {
49  $error = 0;
50 
51  $db->begin();
52 
53  if (!dolibarr_set_const($db, 'LDAP_CONTACT_DN', GETPOST("contactdn"), 'chaine', 0, '', $conf->entity)) {
54  $error++;
55  }
56  if (!dolibarr_set_const($db, 'LDAP_CONTACT_OBJECT_CLASS', GETPOST("objectclass"), 'chaine', 0, '', $conf->entity)) {
57  $error++;
58  }
59 
60  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FULLNAME', GETPOST("fieldfullname"), 'chaine', 0, '', $conf->entity)) {
61  $error++;
62  }
63  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_NAME', GETPOST("fieldname"), 'chaine', 0, '', $conf->entity)) {
64  $error++;
65  }
66  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FIRSTNAME', GETPOST("fieldfirstname"), 'chaine', 0, '', $conf->entity)) {
67  $error++;
68  }
69  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COMPANY', GETPOST("fieldcompany"), 'chaine', 0, '', $conf->entity)) {
70  $error++;
71  }
72  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MAIL', GETPOST("fieldmail"), 'chaine', 0, '', $conf->entity)) {
73  $error++;
74  }
75  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_PHONE', GETPOST("fieldphone"), 'chaine', 0, '', $conf->entity)) {
76  $error++;
77  }
78  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_HOMEPHONE', GETPOST("fieldhomephone"), 'chaine', 0, '', $conf->entity)) {
79  $error++;
80  }
81  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MOBILE', GETPOST("fieldmobile"), 'chaine', 0, '', $conf->entity)) {
82  $error++;
83  }
84  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_SKYPE', GETPOST("fieldskype"), 'chaine', 0, '', $conf->entity)) {
85  $error++;
86  }
87  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FAX', GETPOST("fieldfax"), 'chaine', 0, '', $conf->entity)) {
88  $error++;
89  }
90  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ADDRESS', GETPOST("fieldaddress"), 'chaine', 0, '', $conf->entity)) {
91  $error++;
92  }
93  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ZIP', GETPOST("fieldzip"), 'chaine', 0, '', $conf->entity)) {
94  $error++;
95  }
96  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_TOWN', GETPOST("fieldtown"), 'chaine', 0, '', $conf->entity)) {
97  $error++;
98  }
99  if (!dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COUNTRY', GETPOST("fieldcountry"), 'chaine', 0, '', $conf->entity)) {
100  $error++;
101  }
102 
103  // This one must be after the others
104  $valkey = '';
105  $key = GETPOST("key");
106  if ($key) {
107  $valkey = $conf->global->$key;
108  }
109  if (!dolibarr_set_const($db, 'LDAP_KEY_CONTACTS', $valkey, 'chaine', 0, '', $conf->entity)) {
110  $error++;
111  }
112 
113  if (!$error) {
114  $db->commit();
115  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
116  } else {
117  $db->rollback();
118  dol_print_error($db);
119  }
120 }
121 
122 
123 
124 /*
125  * View
126  */
127 
128 $form = new Form($db);
129 
130 llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
131 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
132 
133 print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
134 
135 $head = ldap_prepare_head();
136 
137 // Test si fonction LDAP actives
138 if (!function_exists("ldap_connect")) {
139  setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
140 }
141 
142 print dol_get_fiche_head($head, 'contacts', $langs->trans("LDAPSetup"), -1);
143 
144 
145 print '<span class="opacitymedium">'.$langs->trans("LDAPDescContact").'</span><br>';
146 print '<br>';
147 
148 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
149 print '<input type="hidden" name="token" value="'.newToken().'">';
150 
151 print '<table class="noborder centpercent">';
152 
153 print '<tr class="liste_titre">';
154 print '<td colspan="4">'.$langs->trans("LDAPSynchronizeContacts").'</td>';
155 print "</tr>\n";
156 
157 // DN Pour les contacts
158 print '<!-- LDAP_CONTACT_DN -->';
159 print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactDn").'</span></td><td>';
160 print '<input size="48" type="text" name="contactdn" value="'.$conf->global->LDAP_CONTACT_DN.'">';
161 print '</td><td>'.$langs->trans("LDAPContactDnExample").'</td>';
162 print '<td>&nbsp;</td>';
163 print '</tr>';
164 
165 // List of object class used to define attributes in structure
166 print '<!-- LDAP_CONTACT_OBJECT_CLASS -->';
167 print '<tr class="oddeven"><td><span class="fieldrequired">'.$langs->trans("LDAPContactObjectClassList").'</span></td><td>';
168 print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_CONTACT_OBJECT_CLASS.'">';
169 print '</td><td>'.$langs->trans("LDAPContactObjectClassListExample").'</td>';
170 print '<td>&nbsp;</td>';
171 print '</tr>';
172 
173 print '</table>';
174 print '<br>';
175 print '<table class="noborder centpercent">';
176 
177 print '<tr class="liste_titre">';
178 print '<td>'.$langs->trans("LDAPDolibarrMapping").'</td>';
179 print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
180 print '<td class="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
181 print "</tr>\n";
182 
183 // Common name
184 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFullname").'</td><td>';
185 print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_CONTACT_FIELD_FULLNAME.'">';
186 print '</td><td>'.$langs->trans("LDAPFieldFullnameExample").'</td>';
187 print '<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>";
188 print '</tr>';
189 
190 // Name
191 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
192 print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_CONTACT_FIELD_NAME.'">';
193 print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
194 print '<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>";
195 print '</tr>';
196 
197 // Firstname
198 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFirstName").'</td><td>';
199 print '<input size="25" type="text" name="fieldfirstname" value="'.$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME.'">';
200 print '</td><td>'.$langs->trans("LDAPFieldFirstNameExample").'</td>';
201 print '<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>";
202 print '</tr>';
203 
204 // Company
205 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCompany").'</td><td>';
206 print '<input size="25" type="text" name="fieldcompany" value="'.$conf->global->LDAP_CONTACT_FIELD_COMPANY.'">';
207 print '</td><td>'.$langs->trans("LDAPFieldCompanyExample").'</td>';
208 print '<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>";
209 print '</tr>';
210 
211 // Mail
212 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMail").'</td><td>';
213 print '<input size="25" type="text" name="fieldmail" value="'.$conf->global->LDAP_CONTACT_FIELD_MAIL.'">';
214 print '</td><td>'.$langs->trans("LDAPFieldMailExample").'</td>';
215 print '<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>";
216 print '</tr>';
217 
218 // Phone pro
219 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldPhone").'</td><td>';
220 print '<input size="25" type="text" name="fieldphone" value="'.$conf->global->LDAP_CONTACT_FIELD_PHONE.'">';
221 print '</td><td>'.$langs->trans("LDAPFieldPhoneExample").'</td>';
222 print '<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>";
223 print '</tr>';
224 
225 // Phone home
226 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldHomePhone").'</td><td>';
227 print '<input size="25" type="text" name="fieldhomephone" value="'.$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE.'">';
228 print '</td><td>'.$langs->trans("LDAPFieldHomePhoneExample").'</td>';
229 print '<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>";
230 print '</tr>';
231 
232 // Mobile
233 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMobile").'</td><td>';
234 print '<input size="25" type="text" name="fieldmobile" value="'.$conf->global->LDAP_CONTACT_FIELD_MOBILE.'">';
235 print '</td><td>'.$langs->trans("LDAPFieldMobileExample").'</td>';
236 print '<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>";
237 print '</tr>';
238 
239 // Skype
240 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldSkype").'</td><td>';
241 print '<input size="25" type="text" name="fieldskype" value="'.$conf->global->LDAP_CONTACT_FIELD_SKYPE.'">';
242 print '</td><td>'.$langs->trans("LDAPFieldSkypeExample").'</td>';
243 print '<td class="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_SKYPE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS == $conf->global->LDAP_CONTACT_FIELD_SKYPE ? ' checked' : '')."></td>";
244 print '</tr>';
245 
246 // Fax
247 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFax").'</td><td>';
248 print '<input size="25" type="text" name="fieldfax" value="'.$conf->global->LDAP_CONTACT_FIELD_FAX.'">';
249 print '</td><td>'.$langs->trans("LDAPFieldFaxExample").'</td>';
250 print '<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>";
251 print '</tr>';
252 
253 // Address
254 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldAddress").'</td><td>';
255 print '<input size="25" type="text" name="fieldaddress" value="'.$conf->global->LDAP_CONTACT_FIELD_ADDRESS.'">';
256 print '</td><td>'.$langs->trans("LDAPFieldAddressExample").'</td>';
257 print '<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>";
258 print '</tr>';
259 
260 // ZIP
261 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldZip").'</td><td>';
262 print '<input size="25" type="text" name="fieldzip" value="'.$conf->global->LDAP_CONTACT_FIELD_ZIP.'">';
263 print '</td><td>'.$langs->trans("LDAPFieldZipExample").'</td>';
264 print '<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>";
265 print '</tr>';
266 
267 // TOWN
268 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldTown").'</td><td>';
269 print '<input size="25" type="text" name="fieldtown" value="'.$conf->global->LDAP_CONTACT_FIELD_TOWN.'">';
270 print '</td><td>'.$langs->trans("LDAPFieldTownExample").'</td>';
271 print '<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>";
272 print '</tr>';
273 
274 // COUNTRY
275 print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCountry").'</td><td>';
276 print '<input size="25" type="text" name="fieldcountry" value="'.$conf->global->LDAP_CONTACT_FIELD_COUNTRY.'">';
277 print '</td><td>&nbsp;</td>';
278 print '<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>";
279 print '</tr>';
280 
281 print '</table>';
282 
283 print info_admin($langs->trans("LDAPDescValues"));
284 
285 print dol_get_fiche_end();
286 
287 print $form->buttonsSaveCancel("Modify", '');
288 
289 print '</form>';
290 
291 
292 /*
293  * Test de la connexion
294  */
295 $butlabel = $langs->trans("LDAPTestSynchroContact");
296 $testlabel = 'test';
297 $key = $conf->global->LDAP_KEY_CONTACTS;
298 $dn = $conf->global->LDAP_CONTACT_DN;
299 $objectclass = $conf->global->LDAP_CONTACT_OBJECT_CLASS;
300 
301 show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass);
302 
303 
304 if (function_exists("ldap_connect")) {
305  if ($_GET["action"] == 'test') {
306  // Creation objet
307  $object = new Contact($db);
308  $object->initAsSpecimen();
309 
310  // Test synchro
311  $ldap = new Ldap();
312  $result = $ldap->connect_bind();
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->dump_content($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
348 llxFooter();
349 $db->close();
Ldap
Class to manage LDAP features.
Definition: ldap.class.php:34
llxFooter
llxFooter()
Empty footer.
Definition: wrapper.php:73
load_fiche_titre
load_fiche_titre($titre, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
Definition: functions.lib.php:5204
GETPOST
GETPOST($paramname, $check='alphanohtml', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
Definition: functions.lib.php:484
dol_print_error
dol_print_error($db='', $error='', $errors=null)
Displays error message system with all the information to facilitate the diagnosis and the escalation...
Definition: functions.lib.php:4844
$form
if($cancel &&! $id) if($action=='add' &&! $cancel) if($action=='delete') if($id) $form
Actions.
Definition: card.php:142
img_picto
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
Definition: functions.lib.php:3880
Contact
Class to manage contact/addresses.
Definition: contact.class.php:40
dol_get_fiche_head
dol_get_fiche_head($links=array(), $active='', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='', $limittoshow=0, $moretabssuffix='')
Show tabs of a record.
Definition: functions.lib.php:1822
info_admin
info_admin($text, $infoonimgalt=0, $nodiv=0, $admin='1', $morecss='hideonsmartphone', $textfordropdown='')
Show information for admin users or standard users.
Definition: functions.lib.php:4800
newToken
newToken()
Return the value of token currently saved into session with name 'newtoken'.
Definition: functions.lib.php:10878
dol_get_fiche_end
dol_get_fiche_end($notab=0)
Return tab footer of a card.
Definition: functions.lib.php:2018
dolibarr_set_const
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).
Definition: admin.lib.php:627
Form
Class to manage generation of HTML components Only common components must be here.
Definition: html.form.class.php:52
ldap_prepare_head
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition: ldap.lib.php:31
setEventMessages
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='')
Set event messages in dol_events session object.
Definition: functions.lib.php:8137
accessforbidden
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Definition: security.lib.php:933
llxHeader
if(!defined('NOREQUIRESOC')) if(!defined('NOREQUIRETRAN')) if(!defined('NOCSRFCHECK')) if(!defined('NOTOKENRENEWAL')) if(!defined('NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined('NOREQUIREAJAX')) llxHeader()
Empty header.
Definition: wrapper.php:59
show_ldap_test_button
show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass)
Show button test LDAP synchro.
Definition: ldap.lib.php:102