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