dolibarr  16.0.5
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-2021 Regis Houssin <regis.houssin@inodbox.com>
6  * Copyright (C) 2006-2020 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/class/html.formldap.class.php';
33 require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
34 
35 // Load translation files required by the page
36 $langs->loadLangs(array("admin", "ldap"));
37 
38 if (!$user->admin) {
40 }
41 
42 $action = GETPOST('action', 'aZ09');
43 
44 // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
45 $hookmanager->initHooks(array('adminldap', 'globaladmin'));
46 
47 
48 /*
49  * Actions
50  */
51 
52 $parameters = array();
53 $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
54 if ($reshook < 0) {
55  setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
56 }
57 
58 if (empty($reshook)) {
59  if ($action == 'setvalue' && $user->admin) {
60  $error = 0;
61 
62  $db->begin();
63 
64  if (!dolibarr_set_const($db, 'LDAP_SERVER_TYPE', GETPOST("type", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
65  $error++;
66  }
67  if (!dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION', GETPOST("LDAP_SERVER_PROTOCOLVERSION", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
68  $error++;
69  }
70  if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST', GETPOST("host", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
71  $error++;
72  }
73  if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE', GETPOST("slave", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
74  $error++;
75  }
76  if (!dolibarr_set_const($db, 'LDAP_SERVER_PORT', GETPOST("port", 'int'), 'chaine', 0, '', $conf->entity)) {
77  $error++;
78  }
79  if (!dolibarr_set_const($db, 'LDAP_SERVER_DN', GETPOST("dn", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
80  $error++;
81  }
82  if (!dolibarr_set_const($db, 'LDAP_ADMIN_DN', GETPOST("admin", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
83  $error++;
84  }
85  if (!dolibarr_set_const($db, 'LDAP_ADMIN_PASS', GETPOST("pass", 'none'), 'chaine', 0, '', $conf->entity)) {
86  $error++;
87  }
88  if (!dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS', GETPOST("usetls", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
89  $error++;
90  }
91  if (!dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE', GETPOST("activesynchro", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
92  $error++;
93  }
94  if (!dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE', GETPOST("activecontact", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
95  $error++;
96  }
97  if (!dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE', GETPOST("activemembers", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
98  $error++;
99  }
100  if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_ACTIVE', GETPOST("activememberstypes", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
101  $error++;
102  }
103  if (!dolibarr_set_const($db, 'LDAP_PASSWORD_HASH_TYPE', GETPOST("LDAP_PASSWORD_HASH_TYPE", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
104  $error++;
105  }
106 
107  if (!$error) {
108  $db->commit();
109  setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
110  } else {
111  $db->rollback();
112  dol_print_error($db);
113  }
114  }
115 }
116 
117 /*
118  * View
119  */
120 
121 llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
122 
123 $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
124 
125 print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
126 
127 $head = ldap_prepare_head();
128 
129 // Test si fonction LDAP actives
130 if (!function_exists("ldap_connect")) {
131  setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
132 }
133 
134 
135 $form = new Form($db);
136 $formldap = new FormLdap($db);
137 
138 print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
139 print '<input type="hidden" name="token" value="'.newToken().'">';
140 
141 print dol_get_fiche_head($head, 'ldap', $langs->trans("LDAPSetup"), -1);
142 
143 print '<table class="noborder centpercent">';
144 
145 // Liste de synchro actives
146 print '<tr class="liste_titre">';
147 print '<td colspan="3">'.$langs->trans("LDAPSynchronization").'</td>';
148 print "</tr>\n";
149 
150 // Synchro utilisateurs/groupes active
151 
152 print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
153 print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_SYNCHRO_ACTIVE'), 'activesynchro');
154 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPDnSynchroActiveExample").'</span>';
155 if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE) && empty($conf->global->LDAP_USER_DN)) {
156  print '<br><span class="error">'.$langs->trans("LDAPSetupNotComplete").'</span>';
157 }
158 print '</td></tr>';
159 
160 // Synchro contact active
161 if (!empty($conf->societe->enabled)) {
162  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
163  print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_CONTACT_ACTIVE'), 'activecontact', array(Ldap::SYNCHRO_LDAP_TO_DOLIBARR));
164  print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPDnContactActiveExample").'</span></td></tr>';
165 }
166 
167 // Synchro member active
168 if (!empty($conf->adherent->enabled)) {
169  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberActive").'</td><td>';
170  print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_MEMBER_ACTIVE'), 'activemembers', array(), 2);
171  print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPDnMemberActiveExample").'</span></td></tr>';
172 }
173 
174 // Synchro member type active
175 if (!empty($conf->adherent->enabled)) {
176  print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberTypeActive").'</td><td>';
177  print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE'), 'activememberstypes', array(), 2);
178  print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPDnMemberTypeActiveExample").'</span></td></tr>';
179 }
180 
181 // Fields from hook
182 $parameters = array();
183 $reshook = $hookmanager->executeHooks('addAdminLdapOptions', $parameters); // Note that $action and $object may have been modified by hook
184 print $hookmanager->resPrint;
185 
186 print '<tr class="liste_titre">';
187 print '<td>'.$langs->trans("Parameter").'</td>';
188 print '<td>'.$langs->trans("Value").'</td>';
189 print '<td>'.$langs->trans("Example").'</td>';
190 print "</tr>\n";
191 
192 // Type
193 print '<tr class="oddeven"><td>'.$langs->trans("Type").'</td><td>';
194 print $formldap->selectLdapServerType(getDolGlobalString('LDAP_SERVER_TYPE'), 'type');
195 print '</td><td>&nbsp;</td></tr>';
196 
197 // Version
198 print '<tr class="oddeven"><td>'.$langs->trans("Version").'</td><td>';
199 print $formldap->selectLdapServerProtocolVersion(getDolGlobalString('LDAP_SERVER_PROTOCOLVERSION'), 'LDAP_SERVER_PROTOCOLVERSION');
200 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerProtocolVersion").'</span></td></tr>';
201 
202 // Serveur primaire
203 print '<tr class="oddeven"><td>';
204 print $langs->trans("LDAPPrimaryServer").'</td><td>';
205 print '<input class="minwidth200" type="text" name="host" value="'.getDolGlobalString('LDAP_SERVER_HOST').'">';
206 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerExample").'</span></td></tr>';
207 
208 // Serveur secondaire
209 print '<tr class="oddeven"><td>';
210 print $langs->trans("LDAPSecondaryServer").'</td><td>';
211 print '<input class="minwidth200" type="text" name="slave" value="'.getDolGlobalString('LDAP_SERVER_HOST_SLAVE').'">';
212 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerExample").'</span></td></tr>';
213 
214 // Port
215 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerPort").'</td><td>';
216 print '<input class="width75" type="text" name="port" value="'.getDolGlobalString('LDAP_SERVER_PORT', '389').'">';
217 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerPortExample").'</span></td></tr>';
218 
219 // DNserver
220 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerDn").'</td><td>';
221 print '<input class="minwidth300" type="text" name="dn" value="'.getDolGlobalString('LDAP_SERVER_DN').'">';
222 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerDnExample").'</span></td></tr>';
223 
224 // Utiliser TLS
225 print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
226 print $form->selectyesno('usetls', getDolGlobalInt('LDAP_SERVER_USE_TLS'), 1);
227 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerUseTLSExample").'</span></td></tr>';
228 
229 // Password hash type
230 print '<tr class="oddeven"><td>'.$langs->trans("LDAPPasswordHashType").'</td><td>';
231 print $formldap->selectLdapPasswordHashType(getDolGlobalString('LDAP_PASSWORD_HASH_TYPE'), 'LDAP_PASSWORD_HASH_TYPE');
232 print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPPasswordHashTypeExample").'</span></td></tr>';
233 
234 print '<tr class="liste_titre">';
235 print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
236 print "</tr>\n";
237 
238 // DNAdmin
239 print '<!-- LDAP_ADMIN_DN -->';
240 print '<tr class="oddeven"><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
241 print '<input class="minwidth300" type="text" name="admin" value="'.getDolGlobalString('LDAP_ADMIN_DN').'">';
242 print '</td><td class="maxwidthhalf"><span class="opacitymedium">'.$langs->trans("LDAPAdminDnExample").'</span></td></tr>';
243 
244 // Pass
245 print '<!-- LDAP_ADMIN_PASS -->';
246 print '<tr class="oddeven"><td>'.$langs->trans("LDAPPassword").'</td><td>';
247 print '<input class="minwidth150" type="password" name="pass" value="'.dol_escape_htmltag($conf->global->LDAP_ADMIN_PASS).'">';
248 print '</td><td><span class="opacitymedium">'.$langs->trans('Password').' (ex: secret)</span></td></tr>';
249 
250 print '</table>';
251 
252 print dol_get_fiche_end();
253 
254 print $form->buttonsSaveCancel("Modify", '');
255 
256 print '</form>';
257 
258 print '<br>';
259 
260 
261 /*
262  * Test de la connexion
263  */
264 if (function_exists("ldap_connect")) {
265  if (!empty($conf->global->LDAP_SERVER_HOST)) {
266  print '<a class="butAction reposition" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
267  }
268 
269  if ($action == 'test') {
270  $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
271 
272  $result = $ldap->connect_bind();
273  if ($result > 0) {
274  // Test ldap connect and bind
275  print img_picto('', 'info').' ';
276  print '<span class="ok">'.$langs->trans("LDAPTCPConnectOK", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT')).'</span>';
277  print '<br>';
278 
279  if (!empty($conf->global->LDAP_ADMIN_DN) && !empty($conf->global->LDAP_ADMIN_PASS)) {
280  if ($result == 2) {
281  print img_picto('', 'info').' ';
282  print '<span class="ok">'.$langs->trans("LDAPBindOK", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT'), $conf->global->LDAP_ADMIN_DN, preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)).'</span>';
283  print '<br>';
284  } else {
285  print img_picto('', 'error').' ';
286  print '<span class="error">'.$langs->trans("LDAPBindKO", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT'), $conf->global->LDAP_ADMIN_DN, preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)).'</span>';
287  print '<br>';
288  print $langs->trans("Error").' '.$ldap->error;
289  print '<br>';
290  }
291  } else {
292  print img_picto('', 'warning').' ';
293  print '<span class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</span>';
294  print '<br>';
295  }
296 
297 
298  // Test ldap_getversion
299  if (($ldap->getVersion() == 3)) {
300  print img_picto('', 'info').' ';
301  print '<span class="ok">'.$langs->trans("LDAPSetupForVersion3").'</span>';
302  print '<br>';
303  } else {
304  print img_picto('', 'info').' ';
305  print '<span class="ok">'.$langs->trans("LDAPSetupForVersion2").'</span>';
306  print '<br>';
307  }
308 
309  $ldap->unbind();
310  } else {
311  print img_picto('', 'error').' ';
312  print '<span class="error">'.$langs->trans("LDAPTCPConnectKO", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT')).'</span>';
313  print '<br>';
314  print $langs->trans("Error").' '.$ldap->error;
315  print '<br>';
316  }
317  }
318 }
319 
320 // End of page
321 llxFooter();
322 $db->close();
FormLdap
Class to manage generation of HTML components for ldap module.
Definition: html.formldap.class.php:28
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
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
getDolGlobalString
if(!function_exists('utf8_encode')) if(!function_exists('utf8_decode')) getDolGlobalString($key, $default='')
Return dolibarr global constant string value.
Definition: functions.lib.php:80
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\SYNCHRO_LDAP_TO_DOLIBARR
const SYNCHRO_LDAP_TO_DOLIBARR
Ldap to Dolibarr synchronization.
Definition: ldap.class.php:138
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
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93
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