ldap.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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");
  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. setEventMessage($langs->trans("SetupSaved"));
  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_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. setEventMessage($langs->trans("LDAPFunctionsNotAvailableOnPHP"),'errors');
  76. }
  77. dol_fiche_head($head, 'ldap', $langs->trans("LDAPSetup"));
  78. $var=true;
  79. $form=new Form($db);
  80. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  81. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  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. $var=!$var;
  89. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
  90. $arraylist=array();
  91. $arraylist['0']=$langs->trans("No");
  92. $arraylist['ldap2dolibarr']=$langs->trans("LDAPToDolibarr");
  93. $arraylist['dolibarr2ldap']=$langs->trans("DolibarrToLDAP");
  94. print $form->selectarray('activesynchro',$arraylist,$conf->global->LDAP_SYNCHRO_ACTIVE);
  95. print '</td><td>'.$langs->trans("LDAPDnSynchroActiveExample");
  96. if ($conf->global->LDAP_SYNCHRO_ACTIVE && ! $conf->global->LDAP_USER_DN)
  97. {
  98. print '<br><font class="error">'.$langs->trans("LDAPSetupNotComplete").'</font>';
  99. }
  100. print '</td></tr>';
  101. // Synchro contact active
  102. if (! empty($conf->societe->enabled))
  103. {
  104. $var=!$var;
  105. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
  106. $arraylist=array();
  107. $arraylist['0']=$langs->trans("No");
  108. $arraylist['1']=$langs->trans("DolibarrToLDAP");
  109. print $form->selectarray('activecontact',$arraylist,$conf->global->LDAP_CONTACT_ACTIVE);
  110. print '</td><td>'.$langs->trans("LDAPDnContactActiveExample").'</td></tr>';
  111. }
  112. // Synchro adherentt active
  113. if (! empty($conf->adherent->enabled))
  114. {
  115. $var=!$var;
  116. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnMemberActive").'</td><td>';
  117. $arraylist=array();
  118. $arraylist['0']=$langs->trans("No");
  119. $arraylist['1']=$langs->trans("DolibarrToLDAP");
  120. print $form->selectarray('activemembers',$arraylist,$conf->global->LDAP_MEMBER_ACTIVE);
  121. print '</td><td>'.$langs->trans("LDAPDnMemberActiveExample").'</td></tr>';
  122. }
  123. print '<tr class="liste_titre">';
  124. print '<td>'.$langs->trans("Parameter").'</td>';
  125. print '<td>'.$langs->trans("Value").'</td>';
  126. print '<td>'.$langs->trans("Example").'</td>';
  127. print "</tr>\n";
  128. // Type
  129. $var=!$var;
  130. print '<tr '.$bc[$var].'><td>'.$langs->trans("Type").'</td><td>';
  131. $arraylist=array();
  132. $arraylist['activedirectory']='Active Directory';
  133. $arraylist['openldap']='OpenLdap';
  134. $arraylist['egroupware']='Egroupware';
  135. print $form->selectarray('type',$arraylist,$conf->global->LDAP_SERVER_TYPE);
  136. print '</td><td>&nbsp;</td></tr>';
  137. // Version
  138. $var=!$var;
  139. print '<tr '.$bc[$var].'><td>'.$langs->trans("Version").'</td><td>';
  140. $arraylist=array();
  141. $arraylist['3']='Version 3';
  142. $arraylist['2']='Version 2';
  143. print $form->selectarray('LDAP_SERVER_PROTOCOLVERSION',$arraylist,$conf->global->LDAP_SERVER_PROTOCOLVERSION);
  144. print '</td><td>'.$langs->trans("LDAPServerProtocolVersion").'</td></tr>';
  145. // Serveur primaire
  146. $var=!$var;
  147. print '<tr '.$bc[$var].'><td>';
  148. print $langs->trans("LDAPPrimaryServer").'</td><td>';
  149. print '<input size="25" type="text" name="host" value="'.$conf->global->LDAP_SERVER_HOST.'">';
  150. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  151. // Serveur secondaire
  152. $var=!$var;
  153. print '<tr '.$bc[$var].'><td>';
  154. print $langs->trans("LDAPSecondaryServer").'</td><td>';
  155. print '<input size="25" type="text" name="slave" value="'.$conf->global->LDAP_SERVER_HOST_SLAVE.'">';
  156. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  157. // Port
  158. $var=!$var;
  159. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerPort").'</td><td>';
  160. if (! empty($conf->global->LDAP_SERVER_PORT))
  161. {
  162. print '<input size="25" type="text" name="port" value="'.$conf->global->LDAP_SERVER_PORT.'">';
  163. }
  164. else
  165. {
  166. print '<input size="25" type="text" name="port" value="389">';
  167. }
  168. print '</td><td>'.$langs->trans("LDAPServerPortExample").'</td></tr>';
  169. // DNserver
  170. $var=!$var;
  171. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerDn").'</td><td>';
  172. print '<input size="25" type="text" name="dn" value="'.$conf->global->LDAP_SERVER_DN.'">';
  173. print '</td><td>'.$langs->trans("LDAPServerDnExample").'</td></tr>';
  174. // Utiliser TLS
  175. $var=!$var;
  176. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
  177. $arraylist=array();
  178. $arraylist['0']=$langs->trans("No");
  179. $arraylist['1']=$langs->trans("Yes");
  180. print $form->selectarray('usetls',$arraylist,$conf->global->LDAP_SERVER_USE_TLS);
  181. print '</td><td>'.$langs->trans("LDAPServerUseTLSExample").'</td></tr>';
  182. print '<tr class="liste_titre">';
  183. print '<td colspan="3">'.$langs->trans("ForANonAnonymousAccess").'</td>';
  184. print "</tr>\n";
  185. // DNAdmin
  186. $var=!$var;
  187. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
  188. print '<input size="25" type="text" name="admin" value="'.$conf->global->LDAP_ADMIN_DN.'">';
  189. print '</td><td>'.$langs->trans("LDAPAdminDnExample").'</td></tr>';
  190. // Pass
  191. $var=!$var;
  192. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPPassword").'</td><td>';
  193. if (! empty($conf->global->LDAP_ADMIN_PASS))
  194. {
  195. print '<input size="25" type="password" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';// je le met en visible pour test
  196. }
  197. else
  198. {
  199. print '<input size="25" type="text" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';
  200. }
  201. print '</td><td>secret</td></tr>';
  202. print '</table>';
  203. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
  204. print '</form>';
  205. print '</div>';
  206. print '<br>';
  207. /*
  208. * Test de la connexion
  209. */
  210. if (function_exists("ldap_connect"))
  211. {
  212. if (! empty($conf->global->LDAP_SERVER_HOST))
  213. {
  214. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
  215. }
  216. if ($_GET["action"] == 'test')
  217. {
  218. $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
  219. $result = $ldap->connect_bind();
  220. if ($result)
  221. {
  222. // Test ldap connect and bind
  223. print img_picto('','info').' ';
  224. print '<font class="ok">'.$langs->trans("LDAPTCPConnectOK",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
  225. print '<br>';
  226. if ($conf->global->LDAP_ADMIN_DN && ! empty($conf->global->LDAP_ADMIN_PASS))
  227. {
  228. if ($result == 2)
  229. {
  230. print img_picto('','info').' ';
  231. 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>';
  232. print '<br>';
  233. }
  234. else
  235. {
  236. print img_picto('','error').' ';
  237. 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>';
  238. print '<br>';
  239. print $langs->trans("Error").' '.$ldap->error;
  240. print '<br>';
  241. }
  242. }
  243. else
  244. {
  245. print img_picto('','warning').' ';
  246. print '<font class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</font>';
  247. print '<br>';
  248. }
  249. // Test ldap_getversion
  250. if (($ldap->getVersion() == 3))
  251. {
  252. print img_picto('','info').' ';
  253. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion3").'</font>';
  254. print '<br>';
  255. }
  256. else
  257. {
  258. print img_picto('','info').' ';
  259. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion2").'</font>';
  260. print '<br>';
  261. }
  262. $unbind = $ldap->unbind();
  263. }
  264. else
  265. {
  266. print img_picto('','error').' ';
  267. print '<font class="error">'.$langs->trans("LDAPTCPConnectKO",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
  268. print '<br>';
  269. print $langs->trans("Error").' '.$ldap->error;
  270. print '<br>';
  271. }
  272. }
  273. }
  274. $db->close();
  275. llxFooter();