dolibarr 21.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-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 * Copyright (C) 2024 Frédéric France <frederic.france@free.fr>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 */
23
30// Load Dolibarr environment
31require '../main.inc.php';
32require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
33require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
34require_once DOL_DOCUMENT_ROOT.'/core/class/html.formldap.class.php';
35require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
36
45// Load translation files required by the page
46$langs->loadLangs(array("admin", "ldap"));
47
48if (!$user->admin) {
50}
51
52$action = GETPOST('action', 'aZ09');
53
54// Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context
55$hookmanager->initHooks(array('adminldap', 'globaladmin'));
56
57
58/*
59 * Actions
60 */
61
62$parameters = array();
63$reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
64if ($reshook < 0) {
65 setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
66}
67
68if (empty($reshook)) {
69 if ($action == 'setvalue' && $user->admin) {
70 $error = 0;
71
72 $db->begin();
73
74 if (!dolibarr_set_const($db, 'LDAP_SERVER_TYPE', GETPOST("type", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
75 $error++;
76 }
77 if (!dolibarr_set_const($db, 'LDAP_USERACCOUNTCONTROL', GETPOSTINT("userAccountControl"), 'chaine', 0, '', $conf->entity)) {
78 $error++;
79 }
80 if (!dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION', GETPOST("LDAP_SERVER_PROTOCOLVERSION", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
81 $error++;
82 }
83 if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST', GETPOST("host", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
84 $error++;
85 }
86 if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE', GETPOST("slave", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
87 $error++;
88 }
89 if (!dolibarr_set_const($db, 'LDAP_SERVER_PORT', GETPOSTINT("port"), 'chaine', 0, '', $conf->entity)) {
90 $error++;
91 }
92 if (!dolibarr_set_const($db, 'LDAP_SERVER_DN', GETPOST("dn", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
93 $error++;
94 }
95 if (!dolibarr_set_const($db, 'LDAP_ADMIN_DN', GETPOST("admin", 'alphanohtml'), 'chaine', 0, '', $conf->entity)) {
96 $error++;
97 }
98 if (!dolibarr_set_const($db, 'LDAP_ADMIN_PASS', GETPOST("pass", 'none'), 'chaine', 0, '', $conf->entity)) {
99 $error++;
100 }
101 if (!dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS', GETPOST("usetls", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
102 $error++;
103 }
104 if (!dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE', GETPOST("activesynchro", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
105 $error++;
106 }
107 if (!dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE', GETPOST("activecontact", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
108 $error++;
109 }
110 if (!dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE', GETPOST("activemembers", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
111 $error++;
112 }
113 if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_ACTIVE', GETPOST("activememberstypes", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
114 $error++;
115 }
116 if (!dolibarr_set_const($db, 'LDAP_PASSWORD_HASH_TYPE', GETPOST("LDAP_PASSWORD_HASH_TYPE", 'aZ09'), 'chaine', 0, '', $conf->entity)) {
117 $error++;
118 }
119
120 if (!$error) {
121 $db->commit();
122 setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
123 } else {
124 $db->rollback();
125 dol_print_error($db);
126 }
127 }
128}
129
130/*
131 * View
132 */
133
134llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP', '', 0, 0, '', '', '', 'mod-admin page-ldap');
135
136$linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
137
138print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
139
140$head = ldap_prepare_head();
141
142// Test if the LDAP functionality is available
143if (!function_exists("ldap_connect")) {
144 setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
145}
146
147
148$form = new Form($db);
149$formldap = new FormLdap($db);
150
151print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue&token='.newToken().'">';
152print '<input type="hidden" name="token" value="'.newToken().'">';
153
154print dol_get_fiche_head($head, 'ldap', '', -1);
155
156print '<table class="noborder centpercent">';
157
158// List of active synchronisations
159print '<tr class="liste_titre">';
160print '<td colspan="3">'.$langs->trans("LDAPSynchronization").'</td>';
161print "</tr>\n";
162
163// Synchronise active users and groups
164
165print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
166print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_SYNCHRO_ACTIVE'), 'activesynchro');
167print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPDnSynchroActiveExample").'</span>';
168if (getDolGlobalString('LDAP_SYNCHRO_ACTIVE') && !getDolGlobalString('LDAP_USER_DN')) {
169 print '<br><span class="error">'.$langs->trans("LDAPSetupNotComplete").'</span>';
170}
171print '</td></tr>';
172
173// Synchro contact active
174if (isModEnabled('societe')) {
175 print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
176 print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_CONTACT_ACTIVE'), 'activecontact', array(Ldap::SYNCHRO_LDAP_TO_DOLIBARR));
177 print '</td><td><span class="opacitymedium">' . $langs->trans("LDAPDnContactActiveExample") . '</span></td></tr>';
178}
179
180// Synchro member active
181if (isModEnabled('member')) {
182 print '<tr class="oddeven"><td>' . $langs->trans("LDAPDnMemberActive") . '</td><td>';
183 print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_MEMBER_ACTIVE'), 'activemembers', array(), 2);
184 print '</td><td><span class="opacitymedium">' . $langs->trans("LDAPDnMemberActiveExample") . '</span></td></tr>';
185}
186
187// Synchro member type active
188if (isModEnabled('member')) {
189 print '<tr class="oddeven"><td>' . $langs->trans("LDAPDnMemberTypeActive") . '</td><td>';
190 print $formldap->selectLdapDnSynchroActive(getDolGlobalInt('LDAP_MEMBER_TYPE_ACTIVE'), 'activememberstypes', array(), 2);
191 print '</td><td><span class="opacitymedium">' . $langs->trans("LDAPDnMemberTypeActiveExample") . '</span></td></tr>';
192}
193
194// Fields from hook
195$parameters = array();
196$reshook = $hookmanager->executeHooks('addAdminLdapOptions', $parameters); // Note that $action and $object may have been modified by hook
197print $hookmanager->resPrint;
198
199print '<tr class="liste_titre">';
200print '<td>'.$langs->trans("Parameter").'</td>';
201print '<td>'.$langs->trans("Value").'</td>';
202print '<td>'.$langs->trans("Example").'</td>';
203print "</tr>\n";
204
205// Type
206print '<tr class="oddeven"><td>'.$langs->trans("Type").'</td><td>';
207print $formldap->selectLdapServerType(getDolGlobalString('LDAP_SERVER_TYPE'), 'type');
208print '</td><td>&nbsp;</td></tr>';
209
210// userAccountControl
211print '<tr class="oddeven"><td>'.$langs->trans("LDAPUserAccountControl").'</td><td>';
212print '<input class="width75" type="text" name="userAccountControl" value="'.getDolGlobalString('LDAP_USERACCOUNTCONTROL', '512').'">';
213print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPUserAccountControlExample").'</span></td></tr>';
214
215// Version
216print '<tr class="oddeven"><td>'.$langs->trans("Version").'</td><td>';
217print $formldap->selectLdapServerProtocolVersion(getDolGlobalString('LDAP_SERVER_PROTOCOLVERSION'), 'LDAP_SERVER_PROTOCOLVERSION');
218print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerProtocolVersion").'</span></td></tr>';
219
220// Serveur primaire
221print '<tr class="oddeven"><td>';
222print $langs->trans("LDAPPrimaryServer").'</td><td>';
223print '<input class="minwidth200" type="text" name="host" value="'.getDolGlobalString('LDAP_SERVER_HOST').'">';
224print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerExample").'</span></td></tr>';
225
226// Serveur secondaire
227print '<tr class="oddeven"><td>';
228print $langs->trans("LDAPSecondaryServer").'</td><td>';
229print '<input class="minwidth200" type="text" name="slave" value="'.getDolGlobalString('LDAP_SERVER_HOST_SLAVE').'">';
230print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerExample").'</span></td></tr>';
231
232// Port
233print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerPort").'</td><td>';
234print '<input class="width75" type="text" name="port" value="'.getDolGlobalString('LDAP_SERVER_PORT', '389').'">';
235print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerPortExample").'</span></td></tr>';
236
237// DNserver
238print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerDn").'</td><td>';
239print '<input class="minwidth300" type="text" name="dn" value="'.getDolGlobalString('LDAP_SERVER_DN').'">';
240print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerDnExample").'</span></td></tr>';
241
242// Utiliser TLS
243print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
244print $form->selectyesno('usetls', getDolGlobalInt('LDAP_SERVER_USE_TLS'), 1);
245print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPServerUseTLSExample").'</span></td></tr>';
246
247// Password hash type
248print '<tr class="oddeven"><td>'.$langs->trans("LDAPPasswordHashType").'</td><td>';
249print $formldap->selectLdapPasswordHashType(getDolGlobalString('LDAP_PASSWORD_HASH_TYPE'), 'LDAP_PASSWORD_HASH_TYPE');
250print '</td><td><span class="opacitymedium">'.$langs->trans("LDAPPasswordHashTypeExample").'</span></td></tr>';
251
252print '<tr class="liste_titre">';
253print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
254print "</tr>\n";
255
256// DNAdmin
257print '<!-- LDAP_ADMIN_DN -->';
258print '<tr class="oddeven"><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
259print '<input class="minwidth300" type="text" name="admin" value="'.getDolGlobalString('LDAP_ADMIN_DN').'">';
260print '</td><td class="maxwidthhalf"><span class="opacitymedium">'.$langs->trans("LDAPAdminDnExample").'</span></td></tr>';
261
262// Pass
263print '<!-- LDAP_ADMIN_PASS -->';
264print '<tr class="oddeven"><td>'.$langs->trans("LDAPPassword").'</td><td>';
265print '<input class="minwidth150" type="password" name="pass" value="'.dol_escape_htmltag(getDolGlobalString('LDAP_ADMIN_PASS')).'">';
266print showValueWithClipboardCPButton(getDolGlobalString('LDAP_ADMIN_PASS'), 0, '&nbsp;');
267print '</td><td><span class="opacitymedium">'.$langs->trans('Password').' (ex: secret)</span></td></tr>';
268
269print '</table>';
270
271print dol_get_fiche_end();
272
273print $form->buttonsSaveCancel("Modify", '');
274
275print '</form>';
276
277print '<br>';
278
279
280/*
281 * Test the connection
282 */
283if (function_exists("ldap_connect")) {
284 if (getDolGlobalString('LDAP_SERVER_HOST')) {
285 print '<a class="butAction reposition" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
286 }
287
288 if ($action == 'test') {
289 $ldap = new Ldap(); // The parameters are provided and recovered through $conf
290
291 $result = $ldap->connectBind();
292 if ($result > 0) {
293 // Test ldap connect and bind
294 print img_picto('', 'info').' ';
295 print '<span class="ok">'.$langs->trans("LDAPTCPConnectOK", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT')).'</span>';
296 print '<br>';
297
298 if (getDolGlobalString('LDAP_ADMIN_DN') && getDolGlobalString('LDAP_ADMIN_PASS')) {
299 if ($result == 2) {
300 print img_picto('', 'info').' ';
301 print '<span class="ok">'.$langs->trans("LDAPBindOK", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT'), getDolGlobalString('LDAP_ADMIN_DN'), preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)).'</span>';
302 print '<br>';
303 } else {
304 print img_picto('', 'error').' ';
305 print '<span class="error">'.$langs->trans("LDAPBindKO", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT'), getDolGlobalString('LDAP_ADMIN_DN'), preg_replace('/./i', '*', $conf->global->LDAP_ADMIN_PASS)).'</span>';
306 print '<br>';
307 print $langs->trans("Error").' '.$ldap->error;
308 print '<br>';
309 }
310 } else {
311 print img_picto('', 'warning').' ';
312 print '<span class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</span>';
313 print '<br>';
314 }
315
316
317 // Test ldap_getversion
318 if (($ldap->getVersion() == 3)) {
319 print img_picto('', 'info').' ';
320 print '<span class="ok">'.$langs->trans("LDAPSetupForVersion3").'</span>';
321 print '<br>';
322 } else {
323 print img_picto('', 'info').' ';
324 print '<span class="ok">'.$langs->trans("LDAPSetupForVersion2").'</span>';
325 print '<br>';
326 }
327
328 $ldap->unbind();
329 } else {
330 print img_picto('', 'error').' ';
331 print '<span class="error">'.$langs->trans("LDAPTCPConnectKO", $ldap->connectedServer, getDolGlobalString('LDAP_SERVER_PORT')).'</span>';
332 print '<br>';
333 print $langs->trans("Error").' '.$ldap->error;
334 print '<br>';
335 }
336 }
337}
338
339// End of page
340llxFooter();
341$db->close();
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).
if(!defined('NOREQUIRESOC')) if(!defined( 'NOREQUIRETRAN')) if(!defined('NOTOKENRENEWAL')) if(!defined( 'NOREQUIREMENU')) if(!defined('NOREQUIREHTML')) if(!defined( 'NOREQUIREAJAX')) llxHeader($head='', $title='', $help_url='', $target='', $disablejs=0, $disablehead=0, $arrayofjs='', $arrayofcss='', $morequerystring='', $morecssonbody='', $replacemainareaby='', $disablenofollow=0, $disablenoindex=0)
Empty header.
Definition wrapper.php:71
Class to manage generation of HTML components Only common components must be here.
Class to manage generation of HTML components for ldap module.
Class to manage LDAP features.
llxFooter()
Footer empty.
Definition document.php:107
load_fiche_titre($title, $morehtmlright='', $picto='generic', $pictoisfullpath=0, $id='', $morecssontable='', $morehtmlcenter='')
Load a title with picto.
setEventMessages($mesg, $mesgs, $style='mesgs', $messagekey='', $noduplicate=0, $attop=0)
Set event messages in dol_events session object.
showValueWithClipboardCPButton($valuetocopy, $showonlyonhover=1, $texttoshow='')
Create a button to copy $valuetocopy in the clipboard (for copy and paste feature).
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
GETPOSTINT($paramname, $method=0)
Return the value of a $_GET or $_POST supervariable, converted into integer.
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.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
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.
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 a Dolibarr global constant string value.
ldap_prepare_head()
Initialize the array of tabs for customer invoice.
Definition ldap.lib.php:33
global $conf
The following vars must be defined: $type2label $form $conf, $lang, The following vars may also be de...
Definition member.php:79
accessforbidden($message='', $printheader=1, $printfooter=1, $showonlymessage=0, $params=null)
Show a message to say access is forbidden and stop program.