dolibarr  9.0.0
ldap.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-2017 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 <http://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 
34 // Load translation files required by the page
35 $langs->load("admin");
36 
37 if (!$user->admin)
39 
40 $action = GETPOST('action','aZ09');
41 
42 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
43 $hookmanager->initHooks(array('adminldap','globaladmin'));
44 
45 /*
46  * Actions
47  */
48 
49 $parameters=array();
50 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
51 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
52 
53 if (empty($reshook))
54 {
55  if ($action == 'setvalue' && $user->admin)
56  {
57  $error=0;
58 
59  $db->begin();
60  if (! dolibarr_set_const($db, 'LDAP_SERVER_TYPE',GETPOST("type"),'chaine',0,'',$conf->entity)) $error++;
61  if (! dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION',GETPOST("LDAP_SERVER_PROTOCOLVERSION"),'chaine',0,'',$conf->entity)) $error++;
62  if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST',GETPOST("host"),'chaine',0,'',$conf->entity)) $error++;
63  if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE',GETPOST("slave"),'chaine',0,'',$conf->entity)) $error++;
64  if (! dolibarr_set_const($db, 'LDAP_SERVER_PORT',GETPOST("port"),'chaine',0,'',$conf->entity)) $error++;
65  if (! dolibarr_set_const($db, 'LDAP_SERVER_DN',GETPOST("dn"),'chaine',0,'',$conf->entity)) $error++;
66  if (! dolibarr_set_const($db, 'LDAP_ADMIN_DN',GETPOST("admin"),'chaine',0,'',$conf->entity)) $error++;
67  if (! dolibarr_set_const($db, 'LDAP_ADMIN_PASS',GETPOST("pass"),'chaine',0,'',$conf->entity)) $error++;
68  if (! dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS',GETPOST("usetls"),'chaine',0,'',$conf->entity)) $error++;
69  if (! dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE',GETPOST("activesynchro"),'chaine',0,'',$conf->entity)) $error++;
70  if (! dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE',GETPOST("activecontact"),'chaine',0,'',$conf->entity)) $error++;
71  if (! dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE',GETPOST("activemembers"),'chaine',0,'',$conf->entity)) $error++;
72  if (! dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_ACTIVE',GETPOST("activememberstypes"),'chaine',0,'',$conf->entity)) $error++;
73 
74  if (! $error)
75  {
76  $db->commit();
77  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
78  }
79  else
80  {
81  $db->rollback();
82  dol_print_error($db);
83  }
84  }
85 }
86 
87 /*
88  * View
89  */
90 
91 llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
92 
93 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
94 
95 print load_fiche_titre($langs->trans("LDAPSetup"),$linkback,'title_setup');
96 
97 $head = ldap_prepare_head();
98 
99 // Test si fonction LDAP actives
100 if (! function_exists("ldap_connect"))
101 {
102  setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
103 }
104 
105 
106 $form=new Form($db);
107 
108 
109 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
110 print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
111 
112 dol_fiche_head($head, 'ldap', $langs->trans("LDAPSetup"), -1);
113 
114 print '<table class="noborder" width="100%">';
115 
116 // Liste de synchro actives
117 print '<tr class="liste_titre">';
118 print '<td colspan="3">'.$langs->trans("LDAPSynchronization").'</td>';
119 print "</tr>\n";
120 
121 // Synchro utilisateurs/groupes active
122 
123 print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
124 $arraylist=array();
125 $arraylist['0']=$langs->trans("No");
126 $arraylist['ldap2dolibarr']=$langs->trans("LDAPToDolibarr");
127 $arraylist['dolibarr2ldap']=$langs->trans("DolibarrToLDAP");
128 print $form->selectarray('activesynchro',$arraylist,$conf->global->LDAP_SYNCHRO_ACTIVE);
129 print '</td><td>'.$langs->trans("LDAPDnSynchroActiveExample");
130 if ($conf->global->LDAP_SYNCHRO_ACTIVE && ! $conf->global->LDAP_USER_DN)
131 {
132  print '<br><font class="error">'.$langs->trans("LDAPSetupNotComplete").'</font>';
133 }
134 print '</td></tr>';
135 
136 // Synchro contact active
137 if (! empty($conf->societe->enabled))
138 {
139  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
140  $arraylist=array();
141  $arraylist['0']=$langs->trans("No");
142  $arraylist['1']=$langs->trans("DolibarrToLDAP");
143  print $form->selectarray('activecontact',$arraylist,$conf->global->LDAP_CONTACT_ACTIVE);
144  print '</td><td>'.$langs->trans("LDAPDnContactActiveExample").'</td></tr>';
145 }
146 
147 // Synchro member active
148 if (! empty($conf->adherent->enabled))
149 {
150  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberActive").'</td><td>';
151  $arraylist=array();
152  $arraylist['0']=$langs->trans("No");
153  $arraylist['1']=$langs->trans("DolibarrToLDAP");
154  $arraylist['ldap2dolibarr']=$langs->trans("LDAPToDolibarr").' ('.$langs->trans("SupportedForLDAPImportScriptOnly").')';
155  print $form->selectarray('activemembers',$arraylist,$conf->global->LDAP_MEMBER_ACTIVE);
156  print '</td><td>'.$langs->trans("LDAPDnMemberActiveExample").'</td></tr>';
157 }
158 
159 // Synchro member type active
160 if (! empty($conf->adherent->enabled))
161 {
162  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberTypeActive").'</td><td>';
163  $arraylist=array();
164  $arraylist['0']=$langs->trans("No");
165  $arraylist['1']=$langs->trans("DolibarrToLDAP");
166  $arraylist['ldap2dolibarr']=$langs->trans("LDAPToDolibarr").' ('.$langs->trans("SupportedForLDAPImportScriptOnly").')';
167  print $form->selectarray('activememberstypes',$arraylist,$conf->global->LDAP_MEMBER_TYPE_ACTIVE);
168  print '</td><td>'.$langs->trans("LDAPDnMemberTypeActiveExample").'</td></tr>';
169 }
170 
171 // Fields from hook
172 $parameters=array();
173 $reshook=$hookmanager->executeHooks('addAdminLdapOptions',$parameters); // Note that $action and $object may have been modified by hook
174 print $hookmanager->resPrint;
175 
176 print '<tr class="liste_titre">';
177 print '<td>'.$langs->trans("Parameter").'</td>';
178 print '<td>'.$langs->trans("Value").'</td>';
179 print '<td>'.$langs->trans("Example").'</td>';
180 print "</tr>\n";
181 
182 // Type
183 print '<tr class="oddeven"><td>'.$langs->trans("Type").'</td><td>';
184 $arraylist=array();
185 $arraylist['activedirectory']='Active Directory';
186 $arraylist['openldap']='OpenLdap';
187 $arraylist['egroupware']='Egroupware';
188 print $form->selectarray('type',$arraylist,$conf->global->LDAP_SERVER_TYPE);
189 print '</td><td>&nbsp;</td></tr>';
190 
191 // Version
192 print '<tr class="oddeven"><td>'.$langs->trans("Version").'</td><td>';
193 $arraylist=array();
194 $arraylist['3']='Version 3';
195 $arraylist['2']='Version 2';
196 print $form->selectarray('LDAP_SERVER_PROTOCOLVERSION',$arraylist,$conf->global->LDAP_SERVER_PROTOCOLVERSION);
197 print '</td><td>'.$langs->trans("LDAPServerProtocolVersion").'</td></tr>';
198 
199 // Serveur primaire
200 print '<tr class="oddeven"><td>';
201 print $langs->trans("LDAPPrimaryServer").'</td><td>';
202 print '<input size="25" type="text" name="host" value="'.$conf->global->LDAP_SERVER_HOST.'">';
203 print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
204 
205 // Serveur secondaire
206 print '<tr class="oddeven"><td>';
207 print $langs->trans("LDAPSecondaryServer").'</td><td>';
208 print '<input size="25" type="text" name="slave" value="'.$conf->global->LDAP_SERVER_HOST_SLAVE.'">';
209 print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
210 
211 // Port
212 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerPort").'</td><td>';
213 if (! empty($conf->global->LDAP_SERVER_PORT))
214 {
215  print '<input size="25" type="text" name="port" value="'.$conf->global->LDAP_SERVER_PORT.'">';
216 }
217 else
218 {
219  print '<input size="25" type="text" name="port" value="389">';
220 }
221 print '</td><td>'.$langs->trans("LDAPServerPortExample").'</td></tr>';
222 
223 // DNserver
224 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerDn").'</td><td>';
225 print '<input size="25" type="text" name="dn" value="'.$conf->global->LDAP_SERVER_DN.'">';
226 print '</td><td>'.$langs->trans("LDAPServerDnExample").'</td></tr>';
227 
228 // Utiliser TLS
229 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
230 $arraylist=array();
231 $arraylist['0']=$langs->trans("No");
232 $arraylist['1']=$langs->trans("Yes");
233 print $form->selectarray('usetls',$arraylist,$conf->global->LDAP_SERVER_USE_TLS);
234 print '</td><td>'.$langs->trans("LDAPServerUseTLSExample").'</td></tr>';
235 
236 print '<tr class="liste_titre">';
237 print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
238 print "</tr>\n";
239 
240 // DNAdmin
241 print '<tr class="oddeven"><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
242 print '<input size="25" type="text" name="admin" value="'.$conf->global->LDAP_ADMIN_DN.'">';
243 print '</td><td>'.$langs->trans("LDAPAdminDnExample").'</td></tr>';
244 
245 // Pass
246 print '<tr class="oddeven"><td>'.$langs->trans("LDAPPassword").'</td><td>';
247 if (! empty($conf->global->LDAP_ADMIN_PASS))
248 {
249  print '<input size="25" type="password" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';// je le met en visible pour test
250 }
251 else
252 {
253  print '<input size="25" type="text" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';
254 }
255 print '</td><td>'.$langs->trans('Password').' (ex: secret)</td></tr>';
256 
257 print '</table>';
258 
259 dol_fiche_end();
260 
261 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
262 
263 print '</form>';
264 
265 print '<br>';
266 
267 
268 /*
269  * Test de la connexion
270  */
271 if (function_exists("ldap_connect"))
272 {
273  if (! empty($conf->global->LDAP_SERVER_HOST))
274  {
275  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
276  }
277 
278  if ($_GET["action"] == 'test')
279  {
280  $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
281 
282  $result = $ldap->connect_bind();
283  if ($result > 0)
284  {
285  // Test ldap connect and bind
286  print img_picto('','info').' ';
287  print '<font class="ok">'.$langs->trans("LDAPTCPConnectOK",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
288  print '<br>';
289 
290  if ($conf->global->LDAP_ADMIN_DN && ! empty($conf->global->LDAP_ADMIN_PASS))
291  {
292  if ($result == 2)
293  {
294  print img_picto('','info').' ';
295  print '<font class="ok">'.$langs->trans("LDAPBindOK",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT,$conf->global->LDAP_ADMIN_DN,preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)).'</font>';
296  print '<br>';
297  }
298  else
299  {
300  print img_picto('','error').' ';
301  print '<font class="error">'.$langs->trans("LDAPBindKO",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT,$conf->global->LDAP_ADMIN_DN,preg_replace('/./i','*',$conf->global->LDAP_ADMIN_PASS)).'</font>';
302  print '<br>';
303  print $langs->trans("Error").' '.$ldap->error;
304  print '<br>';
305  }
306  }
307  else
308  {
309  print img_picto('','warning').' ';
310  print '<font class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</font>';
311  print '<br>';
312  }
313 
314 
315  // Test ldap_getversion
316  if (($ldap->getVersion() == 3))
317  {
318  print img_picto('','info').' ';
319  print '<font class="ok">'.$langs->trans("LDAPSetupForVersion3").'</font>';
320  print '<br>';
321  }
322  else
323  {
324  print img_picto('','info').' ';
325  print '<font class="ok">'.$langs->trans("LDAPSetupForVersion2").'</font>';
326  print '<br>';
327  }
328 
329  $unbind = $ldap->unbind();
330  }
331  else
332  {
333  print img_picto('','error').' ';
334  print '<font class="error">'.$langs->trans("LDAPTCPConnectKO",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
335  print '<br>';
336  print $langs->trans("Error").' '.$ldap->error;
337  print '<br>';
338  }
339  }
340 }
341 
342 // End of page
343 llxFooter();
344 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:56
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
GETPOST($paramname, $check='none', $method=0, $filter=null, $options=null, $noreplace=0)
Return value of a param into GET or POST supervariable.
print
Draft customers invoices.
Definition: index.php:91
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
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:510
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0)
Show a message to say access is forbidden and stop program Calling this function terminate execution ...
Class to manage generation of HTML components Only common components must be here.
dol_fiche_end($notab=0)
Show tab footer of a card.
llxHeader()
Empty header.
Definition: wrapper.php:44
Class to manage LDAP features.
Definition: ldap.class.php:30
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition: ldap.lib.php:31
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it&#39;s its name (generic function)
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='', $morecss='')
Show tab header of a card.