dolibarr  7.0.0-beta
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@capnetworks.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 $langs->load("admin");
35 
36 if (!$user->admin)
38 
39 $action = GETPOST('action','aZ09');
40 
41 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
42 $hookmanager->initHooks(array('adminldap','globaladmin'));
43 
44 /*
45  * Actions
46  */
47 
48 $parameters=array();
49 $reshook=$hookmanager->executeHooks('doActions',$parameters,$object,$action); // Note that $action and $object may have been modified by some hooks
50 if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
51 
52 if (empty($reshook))
53 {
54  if ($action == 'setvalue' && $user->admin)
55  {
56  $error=0;
57 
58  $db->begin();
59  if (! dolibarr_set_const($db, 'LDAP_SERVER_TYPE',GETPOST("type"),'chaine',0,'',$conf->entity)) $error++;
60  if (! dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION',GETPOST("LDAP_SERVER_PROTOCOLVERSION"),'chaine',0,'',$conf->entity)) $error++;
61  if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST',GETPOST("host"),'chaine',0,'',$conf->entity)) $error++;
62  if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE',GETPOST("slave"),'chaine',0,'',$conf->entity)) $error++;
63  if (! dolibarr_set_const($db, 'LDAP_SERVER_PORT',GETPOST("port"),'chaine',0,'',$conf->entity)) $error++;
64  if (! dolibarr_set_const($db, 'LDAP_SERVER_DN',GETPOST("dn"),'chaine',0,'',$conf->entity)) $error++;
65  if (! dolibarr_set_const($db, 'LDAP_ADMIN_DN',GETPOST("admin"),'chaine',0,'',$conf->entity)) $error++;
66  if (! dolibarr_set_const($db, 'LDAP_ADMIN_PASS',GETPOST("pass"),'chaine',0,'',$conf->entity)) $error++;
67  if (! dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS',GETPOST("usetls"),'chaine',0,'',$conf->entity)) $error++;
68  if (! dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE',GETPOST("activesynchro"),'chaine',0,'',$conf->entity)) $error++;
69  if (! dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE',GETPOST("activecontact"),'chaine',0,'',$conf->entity)) $error++;
70  if (! dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE',GETPOST("activemembers"),'chaine',0,'',$conf->entity)) $error++;
71  if (! dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_ACTIVE',GETPOST("activememberstypes"),'chaine',0,'',$conf->entity)) $error++;
72 
73  if (! $error)
74  {
75  $db->commit();
76  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
77  }
78  else
79  {
80  $db->rollback();
81  dol_print_error($db);
82  }
83  }
84 }
85 
86 /*
87  * View
88  */
89 
90 llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
91 
92 $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
93 
94 print load_fiche_titre($langs->trans("LDAPSetup"),$linkback,'title_setup');
95 
96 $head = ldap_prepare_head();
97 
98 // Test si fonction LDAP actives
99 if (! function_exists("ldap_connect"))
100 {
101  setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
102 }
103 
104 
105 $var=true;
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 
184 print '<tr class="oddeven"><td>'.$langs->trans("Type").'</td><td>';
185 $arraylist=array();
186 $arraylist['activedirectory']='Active Directory';
187 $arraylist['openldap']='OpenLdap';
188 $arraylist['egroupware']='Egroupware';
189 print $form->selectarray('type',$arraylist,$conf->global->LDAP_SERVER_TYPE);
190 print '</td><td>&nbsp;</td></tr>';
191 
192 // Version
193 
194 print '<tr class="oddeven"><td>'.$langs->trans("Version").'</td><td>';
195 $arraylist=array();
196 $arraylist['3']='Version 3';
197 $arraylist['2']='Version 2';
198 print $form->selectarray('LDAP_SERVER_PROTOCOLVERSION',$arraylist,$conf->global->LDAP_SERVER_PROTOCOLVERSION);
199 print '</td><td>'.$langs->trans("LDAPServerProtocolVersion").'</td></tr>';
200 
201 // Serveur primaire
202 
203 print '<tr class="oddeven"><td>';
204 print $langs->trans("LDAPPrimaryServer").'</td><td>';
205 print '<input size="25" type="text" name="host" value="'.$conf->global->LDAP_SERVER_HOST.'">';
206 print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
207 
208 // Serveur secondaire
209 
210 print '<tr class="oddeven"><td>';
211 print $langs->trans("LDAPSecondaryServer").'</td><td>';
212 print '<input size="25" type="text" name="slave" value="'.$conf->global->LDAP_SERVER_HOST_SLAVE.'">';
213 print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
214 
215 // Port
216 
217 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerPort").'</td><td>';
218 if (! empty($conf->global->LDAP_SERVER_PORT))
219 {
220  print '<input size="25" type="text" name="port" value="'.$conf->global->LDAP_SERVER_PORT.'">';
221 }
222 else
223 {
224  print '<input size="25" type="text" name="port" value="389">';
225 }
226 print '</td><td>'.$langs->trans("LDAPServerPortExample").'</td></tr>';
227 
228 // DNserver
229 
230 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerDn").'</td><td>';
231 print '<input size="25" type="text" name="dn" value="'.$conf->global->LDAP_SERVER_DN.'">';
232 print '</td><td>'.$langs->trans("LDAPServerDnExample").'</td></tr>';
233 
234 // Utiliser TLS
235 
236 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
237 $arraylist=array();
238 $arraylist['0']=$langs->trans("No");
239 $arraylist['1']=$langs->trans("Yes");
240 print $form->selectarray('usetls',$arraylist,$conf->global->LDAP_SERVER_USE_TLS);
241 print '</td><td>'.$langs->trans("LDAPServerUseTLSExample").'</td></tr>';
242 
243 print '<tr class="liste_titre">';
244 print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
245 print "</tr>\n";
246 
247 // DNAdmin
248 
249 print '<tr class="oddeven"><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
250 print '<input size="25" type="text" name="admin" value="'.$conf->global->LDAP_ADMIN_DN.'">';
251 print '</td><td>'.$langs->trans("LDAPAdminDnExample").'</td></tr>';
252 
253 // Pass
254 
255 print '<tr class="oddeven"><td>'.$langs->trans("LDAPPassword").'</td><td>';
256 if (! empty($conf->global->LDAP_ADMIN_PASS))
257 {
258  print '<input size="25" type="password" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';// je le met en visible pour test
259 }
260 else
261 {
262  print '<input size="25" type="text" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';
263 }
264 print '</td><td>'.$langs->trans('Password').' (ex: secret)</td></tr>';
265 
266 print '</table>';
267 
268 dol_fiche_end();
269 
270 print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
271 
272 print '</form>';
273 
274 print '<br>';
275 
276 
277 /*
278  * Test de la connexion
279  */
280 if (function_exists("ldap_connect"))
281 {
282  if (! empty($conf->global->LDAP_SERVER_HOST))
283  {
284  print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
285  }
286 
287  if ($_GET["action"] == 'test')
288  {
289  $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
290 
291  $result = $ldap->connect_bind();
292  if ($result > 0)
293  {
294  // Test ldap connect and bind
295  print img_picto('','info').' ';
296  print '<font class="ok">'.$langs->trans("LDAPTCPConnectOK",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
297  print '<br>';
298 
299  if ($conf->global->LDAP_ADMIN_DN && ! empty($conf->global->LDAP_ADMIN_PASS))
300  {
301  if ($result == 2)
302  {
303  print img_picto('','info').' ';
304  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>';
305  print '<br>';
306  }
307  else
308  {
309  print img_picto('','error').' ';
310  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>';
311  print '<br>';
312  print $langs->trans("Error").' '.$ldap->error;
313  print '<br>';
314  }
315  }
316  else
317  {
318  print img_picto('','warning').' ';
319  print '<font class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</font>';
320  print '<br>';
321  }
322 
323 
324  // Test ldap_getversion
325  if (($ldap->getVersion() == 3))
326  {
327  print img_picto('','info').' ';
328  print '<font class="ok">'.$langs->trans("LDAPSetupForVersion3").'</font>';
329  print '<br>';
330  }
331  else
332  {
333  print img_picto('','info').' ';
334  print '<font class="ok">'.$langs->trans("LDAPSetupForVersion2").'</font>';
335  print '<br>';
336  }
337 
338  $unbind = $ldap->unbind();
339  }
340  else
341  {
342  print img_picto('','error').' ';
343  print '<font class="error">'.$langs->trans("LDAPTCPConnectKO",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
344  print '<br>';
345  print $langs->trans("Error").' '.$ldap->error;
346  print '<br>';
347  }
348 
349  }
350 }
351 
352 llxFooter();
353 $db->close();
llxFooter()
Empty footer.
Definition: wrapper.php:58
img_picto($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='')
Show picto whatever it's its name (generic function)
setEventMessages($mesg, $mesgs, $style='mesgs')
Set event messages in dol_events session object.
dol_fiche_head($links=array(), $active='0', $title='', $notab=0, $picto='', $pictoisfullpath=0, $morehtmlright='')
Show tab header of a card.
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:485
dol_print_error($db='', $error='', $errors=null)
Affiche message erreur system avec toutes les informations pour faciliter le diagnostic et la remonte...
if(empty($reshook)) $form
View.
Definition: perms.php:103
GETPOST($paramname, $check='none', $method=0, $filter=NULL, $options=NULL, $noreplace=0)
Return value of a param into GET or POST supervariable.
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.
load_fiche_titre($titre, $morehtmlright='', $picto='title_generic.png', $pictoisfullpath=0, $id=0, $morecssontable='', $morehtmlcenter='')
Load a title with picto.
llxHeader()
Empty header.
Definition: wrapper.php:46
Class to manage LDAP features.
Definition: ldap.class.php:30
print
Draft customers invoices.
Definition: index.php:91
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition: ldap.lib.php:31