ldap.php 13 KB

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