ldap.php 11 KB

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