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