ldap.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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 <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/admin/ldap.php
  24. * \ingroup ldap
  25. * \brief Page d'administration/configuration du module Ldap
  26. */
  27. require '../main.inc.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
  31. // Load translation files required by the page
  32. $langs->load("admin");
  33. if (!$user->admin)
  34. accessforbidden();
  35. $action = GETPOST('action', 'aZ09');
  36. // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context
  37. $hookmanager->initHooks(array('adminldap', 'globaladmin'));
  38. /*
  39. * Actions
  40. */
  41. $parameters = array();
  42. $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  43. if ($reshook < 0) setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  44. if (empty($reshook))
  45. {
  46. if ($action == 'setvalue' && $user->admin)
  47. {
  48. $error = 0;
  49. $db->begin();
  50. if (!dolibarr_set_const($db, 'LDAP_SERVER_TYPE', GETPOST("type"), 'chaine', 0, '', $conf->entity)) $error++;
  51. if (!dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION', GETPOST("LDAP_SERVER_PROTOCOLVERSION"), 'chaine', 0, '', $conf->entity)) $error++;
  52. if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST', GETPOST("host"), 'chaine', 0, '', $conf->entity)) $error++;
  53. if (!dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE', GETPOST("slave"), 'chaine', 0, '', $conf->entity)) $error++;
  54. if (!dolibarr_set_const($db, 'LDAP_SERVER_PORT', GETPOST("port"), 'chaine', 0, '', $conf->entity)) $error++;
  55. if (!dolibarr_set_const($db, 'LDAP_SERVER_DN', GETPOST("dn"), 'chaine', 0, '', $conf->entity)) $error++;
  56. if (!dolibarr_set_const($db, 'LDAP_ADMIN_DN', GETPOST("admin"), 'chaine', 0, '', $conf->entity)) $error++;
  57. if (!dolibarr_set_const($db, 'LDAP_ADMIN_PASS', GETPOST("pass"), 'chaine', 0, '', $conf->entity)) $error++;
  58. if (!dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS', GETPOST("usetls"), 'chaine', 0, '', $conf->entity)) $error++;
  59. if (!dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE', GETPOST("activesynchro"), 'chaine', 0, '', $conf->entity)) $error++;
  60. if (!dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE', GETPOST("activecontact"), 'chaine', 0, '', $conf->entity)) $error++;
  61. if (!dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE', GETPOST("activemembers"), 'chaine', 0, '', $conf->entity)) $error++;
  62. if (!dolibarr_set_const($db, 'LDAP_MEMBER_TYPE_ACTIVE', GETPOST("activememberstypes"), 'chaine', 0, '', $conf->entity)) $error++;
  63. if (!$error)
  64. {
  65. $db->commit();
  66. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  67. }
  68. else
  69. {
  70. $db->rollback();
  71. dol_print_error($db);
  72. }
  73. }
  74. }
  75. /*
  76. * View
  77. */
  78. llxHeader('', $langs->trans("LDAPSetup"), 'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  79. $linkback = '<a href="'.DOL_URL_ROOT.'/admin/modules.php?restore_lastsearch_values=1">'.$langs->trans("BackToModuleList").'</a>';
  80. print load_fiche_titre($langs->trans("LDAPSetup"), $linkback, 'title_setup');
  81. $head = ldap_prepare_head();
  82. // Test si fonction LDAP actives
  83. if (!function_exists("ldap_connect"))
  84. {
  85. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  86. }
  87. $form = new Form($db);
  88. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  89. print '<input type="hidden" name="token" value="'.newToken().'">';
  90. dol_fiche_head($head, 'ldap', $langs->trans("LDAPSetup"), -1);
  91. print '<table class="noborder centpercent">';
  92. // Liste de synchro actives
  93. print '<tr class="liste_titre">';
  94. print '<td colspan="3">'.$langs->trans("LDAPSynchronization").'</td>';
  95. print "</tr>\n";
  96. // Synchro utilisateurs/groupes active
  97. print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
  98. $arraylist = array();
  99. $arraylist['0'] = $langs->trans("No");
  100. $arraylist['ldap2dolibarr'] = $langs->trans("LDAPToDolibarr");
  101. $arraylist['dolibarr2ldap'] = $langs->trans("DolibarrToLDAP");
  102. print $form->selectarray('activesynchro', $arraylist, $conf->global->LDAP_SYNCHRO_ACTIVE);
  103. print '</td><td>'.$langs->trans("LDAPDnSynchroActiveExample");
  104. if ($conf->global->LDAP_SYNCHRO_ACTIVE && !$conf->global->LDAP_USER_DN)
  105. {
  106. print '<br><font class="error">'.$langs->trans("LDAPSetupNotComplete").'</font>';
  107. }
  108. print '</td></tr>';
  109. // Synchro contact active
  110. if (!empty($conf->societe->enabled))
  111. {
  112. print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
  113. $arraylist = array();
  114. $arraylist['0'] = $langs->trans("No");
  115. $arraylist['1'] = $langs->trans("DolibarrToLDAP");
  116. print $form->selectarray('activecontact', $arraylist, $conf->global->LDAP_CONTACT_ACTIVE);
  117. print '</td><td>'.$langs->trans("LDAPDnContactActiveExample").'</td></tr>';
  118. }
  119. // Synchro member active
  120. if (!empty($conf->adherent->enabled))
  121. {
  122. print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberActive").'</td><td>';
  123. $arraylist = array();
  124. $arraylist['0'] = $langs->trans("No");
  125. $arraylist['1'] = $langs->trans("DolibarrToLDAP");
  126. $arraylist['ldap2dolibarr'] = $langs->trans("LDAPToDolibarr").' ('.$langs->trans("SupportedForLDAPImportScriptOnly").')';
  127. print $form->selectarray('activemembers', $arraylist, $conf->global->LDAP_MEMBER_ACTIVE);
  128. print '</td><td>'.$langs->trans("LDAPDnMemberActiveExample").'</td></tr>';
  129. }
  130. // Synchro member type active
  131. if (!empty($conf->adherent->enabled))
  132. {
  133. print '<tr class="oddeven"><td>'.$langs->trans("LDAPDnMemberTypeActive").'</td><td>';
  134. $arraylist = array();
  135. $arraylist['0'] = $langs->trans("No");
  136. $arraylist['1'] = $langs->trans("DolibarrToLDAP");
  137. $arraylist['ldap2dolibarr'] = $langs->trans("LDAPToDolibarr").' ('.$langs->trans("SupportedForLDAPImportScriptOnly").')';
  138. print $form->selectarray('activememberstypes', $arraylist, $conf->global->LDAP_MEMBER_TYPE_ACTIVE);
  139. print '</td><td>'.$langs->trans("LDAPDnMemberTypeActiveExample").'</td></tr>';
  140. }
  141. // Fields from hook
  142. $parameters = array();
  143. $reshook = $hookmanager->executeHooks('addAdminLdapOptions', $parameters); // Note that $action and $object may have been modified by hook
  144. print $hookmanager->resPrint;
  145. print '<tr class="liste_titre">';
  146. print '<td>'.$langs->trans("Parameter").'</td>';
  147. print '<td>'.$langs->trans("Value").'</td>';
  148. print '<td>'.$langs->trans("Example").'</td>';
  149. print "</tr>\n";
  150. // Type
  151. print '<tr class="oddeven"><td>'.$langs->trans("Type").'</td><td>';
  152. $arraylist = array();
  153. $arraylist['activedirectory'] = 'Active Directory';
  154. $arraylist['openldap'] = 'OpenLdap';
  155. $arraylist['egroupware'] = 'Egroupware';
  156. print $form->selectarray('type', $arraylist, $conf->global->LDAP_SERVER_TYPE);
  157. print '</td><td>&nbsp;</td></tr>';
  158. // Version
  159. print '<tr class="oddeven"><td>'.$langs->trans("Version").'</td><td>';
  160. $arraylist = array();
  161. $arraylist['3'] = 'Version 3';
  162. $arraylist['2'] = 'Version 2';
  163. print $form->selectarray('LDAP_SERVER_PROTOCOLVERSION', $arraylist, $conf->global->LDAP_SERVER_PROTOCOLVERSION);
  164. print '</td><td>'.$langs->trans("LDAPServerProtocolVersion").'</td></tr>';
  165. // Serveur primaire
  166. print '<tr class="oddeven"><td>';
  167. print $langs->trans("LDAPPrimaryServer").'</td><td>';
  168. print '<input size="25" type="text" name="host" value="'.$conf->global->LDAP_SERVER_HOST.'">';
  169. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  170. // Serveur secondaire
  171. print '<tr class="oddeven"><td>';
  172. print $langs->trans("LDAPSecondaryServer").'</td><td>';
  173. print '<input size="25" type="text" name="slave" value="'.$conf->global->LDAP_SERVER_HOST_SLAVE.'">';
  174. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  175. // Port
  176. print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerPort").'</td><td>';
  177. if (!empty($conf->global->LDAP_SERVER_PORT))
  178. {
  179. print '<input size="25" type="text" name="port" value="'.$conf->global->LDAP_SERVER_PORT.'">';
  180. }
  181. else
  182. {
  183. print '<input size="25" type="text" name="port" value="389">';
  184. }
  185. print '</td><td>'.$langs->trans("LDAPServerPortExample").'</td></tr>';
  186. // DNserver
  187. print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerDn").'</td><td>';
  188. print '<input size="25" type="text" name="dn" value="'.$conf->global->LDAP_SERVER_DN.'">';
  189. print '</td><td>'.$langs->trans("LDAPServerDnExample").'</td></tr>';
  190. // Utiliser TLS
  191. print '<tr class="oddeven"><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
  192. $arraylist = array();
  193. $arraylist['0'] = $langs->trans("No");
  194. $arraylist['1'] = $langs->trans("Yes");
  195. print $form->selectarray('usetls', $arraylist, $conf->global->LDAP_SERVER_USE_TLS);
  196. print '</td><td>'.$langs->trans("LDAPServerUseTLSExample").'</td></tr>';
  197. print '<tr class="liste_titre">';
  198. print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
  199. print "</tr>\n";
  200. // DNAdmin
  201. print '<tr class="oddeven"><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
  202. print '<input size="25" type="text" name="admin" value="'.$conf->global->LDAP_ADMIN_DN.'">';
  203. print '</td><td>'.$langs->trans("LDAPAdminDnExample").'</td></tr>';
  204. // Pass
  205. print '<tr class="oddeven"><td>'.$langs->trans("LDAPPassword").'</td><td>';
  206. if (!empty($conf->global->LDAP_ADMIN_PASS))
  207. {
  208. print '<input size="25" type="password" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">'; // je le met en visible pour test
  209. }
  210. else
  211. {
  212. print '<input size="25" type="text" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';
  213. }
  214. print '</td><td>'.$langs->trans('Password').' (ex: secret)</td></tr>';
  215. print '</table>';
  216. dol_fiche_end();
  217. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
  218. print '</form>';
  219. print '<br>';
  220. /*
  221. * Test de la connexion
  222. */
  223. if (function_exists("ldap_connect"))
  224. {
  225. if (!empty($conf->global->LDAP_SERVER_HOST))
  226. {
  227. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
  228. }
  229. if ($_GET["action"] == 'test')
  230. {
  231. $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
  232. $result = $ldap->connect_bind();
  233. if ($result > 0)
  234. {
  235. // Test ldap connect and bind
  236. print img_picto('', 'info').' ';
  237. print '<font class="ok">'.$langs->trans("LDAPTCPConnectOK", $conf->global->LDAP_SERVER_HOST, $conf->global->LDAP_SERVER_PORT).'</font>';
  238. print '<br>';
  239. if ($conf->global->LDAP_ADMIN_DN && !empty($conf->global->LDAP_ADMIN_PASS))
  240. {
  241. if ($result == 2)
  242. {
  243. print img_picto('', 'info').' ';
  244. 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>';
  245. print '<br>';
  246. }
  247. else
  248. {
  249. print img_picto('', 'error').' ';
  250. 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>';
  251. print '<br>';
  252. print $langs->trans("Error").' '.$ldap->error;
  253. print '<br>';
  254. }
  255. }
  256. else
  257. {
  258. print img_picto('', 'warning').' ';
  259. print '<font class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</font>';
  260. print '<br>';
  261. }
  262. // Test ldap_getversion
  263. if (($ldap->getVersion() == 3))
  264. {
  265. print img_picto('', 'info').' ';
  266. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion3").'</font>';
  267. print '<br>';
  268. }
  269. else
  270. {
  271. print img_picto('', 'info').' ';
  272. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion2").'</font>';
  273. print '<br>';
  274. }
  275. $unbind = $ldap->unbind();
  276. }
  277. else
  278. {
  279. print img_picto('', 'error').' ';
  280. print '<font class="error">'.$langs->trans("LDAPTCPConnectKO", $conf->global->LDAP_SERVER_HOST, $conf->global->LDAP_SERVER_PORT).'</font>';
  281. print '<br>';
  282. print $langs->trans("Error").' '.$ldap->error;
  283. print '<br>';
  284. }
  285. }
  286. }
  287. // End of page
  288. llxFooter();
  289. $db->close();