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