ldap.php 11 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 Regis Houssin <regis@dolibarr.fr>
  6. * Copyright (C) 2006-2011 Laurent Destailleur <eldy@users.sourceforge.net>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. /**
  22. * \file htdocs/admin/ldap.php
  23. * \ingroup ldap
  24. * \brief Page d'administration/configuration du module Ldap
  25. */
  26. require("../main.inc.php");
  27. require_once(DOL_DOCUMENT_ROOT."/lib/admin.lib.php");
  28. require_once(DOL_DOCUMENT_ROOT."/lib/ldap.class.php");
  29. require_once(DOL_DOCUMENT_ROOT."/lib/ldap.lib.php");
  30. $langs->load("admin");
  31. if (!$user->admin)
  32. accessforbidden();
  33. /*
  34. * Actions
  35. */
  36. if ($_GET["action"] == 'setvalue' && $user->admin)
  37. {
  38. $error=0;
  39. if (! dolibarr_set_const($db, 'LDAP_SERVER_TYPE',$_POST["type"],'chaine',0,'',$conf->entity)) $error++;
  40. if (! dolibarr_set_const($db, 'LDAP_SERVER_PROTOCOLVERSION',$_POST["version"],'chaine',0,'',$conf->entity)) $error++;
  41. if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST',$_POST["host"],'chaine',0,'',$conf->entity)) $error++;
  42. if (! dolibarr_set_const($db, 'LDAP_SERVER_HOST_SLAVE',$_POST["slave"],'chaine',0,'',$conf->entity)) $error++;
  43. if (! dolibarr_set_const($db, 'LDAP_SERVER_PORT',$_POST["port"],'chaine',0,'',$conf->entity)) $error++;
  44. if (! dolibarr_set_const($db, 'LDAP_SERVER_DN',$_POST["dn"],'chaine',0,'',$conf->entity)) $error++;
  45. if (! dolibarr_set_const($db, 'LDAP_ADMIN_DN',$_POST["admin"],'chaine',0,'',$conf->entity)) $error++;
  46. if (! dolibarr_set_const($db, 'LDAP_ADMIN_PASS',$_POST["pass"],'chaine',0,'',$conf->entity)) $error++;
  47. if (! dolibarr_set_const($db, 'LDAP_SERVER_USE_TLS',$_POST["usetls"],'chaine',0,'',$conf->entity)) $error++;
  48. if (! dolibarr_set_const($db, 'LDAP_SYNCHRO_ACTIVE',$_POST["activesynchro"],'chaine',0,'',$conf->entity)) $error++;
  49. if (! dolibarr_set_const($db, 'LDAP_CONTACT_ACTIVE',$_POST["activecontact"],'chaine',0,'',$conf->entity)) $error++;
  50. if (! dolibarr_set_const($db, 'LDAP_MEMBER_ACTIVE',$_POST["activemembers"],'chaine',0,'',$conf->entity)) $error++;
  51. if ($error)
  52. {
  53. dol_print_error($db->error());
  54. }
  55. }
  56. /*
  57. * View
  58. */
  59. llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  60. print_fiche_titre($langs->trans("LDAPSetup"),'','setup');
  61. $head = ldap_prepare_head();
  62. // Test si fonction LDAP actives
  63. if (! function_exists("ldap_connect"))
  64. {
  65. $mesg=$langs->trans("LDAPFunctionsNotAvailableOnPHP");
  66. }
  67. if ($mesg) print '<div class="error">'.$mesg.'</div>';
  68. dol_fiche_head($head, 'ldap', $langs->trans("LDAPSetup"));
  69. $var=true;
  70. $html=new Form($db);
  71. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  72. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  73. print '<table class="noborder" width="100%">';
  74. // Liste de synchro actives
  75. print '<tr class="liste_titre">';
  76. print '<td colspan="3">'.$langs->trans("LDAPSynchronization").'</td>';
  77. print "</tr>\n";
  78. // Synchro utilisateurs/groupes active
  79. $var=!$var;
  80. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnSynchroActive").'</td><td>';
  81. $arraylist=array();
  82. $arraylist['0']=$langs->trans("No");
  83. $arraylist['ldap2dolibarr']=$langs->trans("LDAPToDolibarr");
  84. $arraylist['dolibarr2ldap']=$langs->trans("DolibarrToLDAP");
  85. print $html->selectarray('activesynchro',$arraylist,$conf->global->LDAP_SYNCHRO_ACTIVE);
  86. print '</td><td>'.$langs->trans("LDAPDnSynchroActiveExample");
  87. if ($conf->global->LDAP_SYNCHRO_ACTIVE && ! $conf->global->LDAP_USER_DN)
  88. {
  89. print '<br><font class="error">'.$langs->trans("LDAPSetupNotComplete").'</font>';
  90. }
  91. print '</td></tr>';
  92. // Synchro contact active
  93. if ($conf->societe->enabled)
  94. {
  95. $var=!$var;
  96. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnContactActive").'</td><td>';
  97. $arraylist=array();
  98. $arraylist['0']=$langs->trans("No");
  99. $arraylist['1']=$langs->trans("DolibarrToLDAP");
  100. print $html->selectarray('activecontact',$arraylist,$conf->global->LDAP_CONTACT_ACTIVE);
  101. print '</td><td>'.$langs->trans("LDAPDnContactActiveExample").'</td></tr>';
  102. }
  103. // Synchro adherentt active
  104. if ($conf->adherent->enabled)
  105. {
  106. $var=!$var;
  107. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPDnMemberActive").'</td><td>';
  108. $arraylist=array();
  109. $arraylist['0']=$langs->trans("No");
  110. $arraylist['1']=$langs->trans("DolibarrToLDAP");
  111. print $html->selectarray('activemembers',$arraylist,$conf->global->LDAP_MEMBER_ACTIVE);
  112. print '</td><td>'.$langs->trans("LDAPDnMemberActiveExample").'</td></tr>';
  113. }
  114. print '<tr class="liste_titre">';
  115. print '<td>'.$langs->trans("Parameter").'</td>';
  116. print '<td>'.$langs->trans("Value").'</td>';
  117. print '<td>'.$langs->trans("Example").'</td>';
  118. print "</tr>\n";
  119. // Type
  120. $var=!$var;
  121. print '<tr '.$bc[$var].'><td>'.$langs->trans("Type").'</td><td>';
  122. $arraylist=array();
  123. $arraylist['activedirectory']='Active Directory';
  124. $arraylist['openldap']='OpenLdap';
  125. $arraylist['egroupware']='Egroupware';
  126. print $html->selectarray('type',$arraylist,$conf->global->LDAP_SERVER_TYPE);
  127. print '</td><td>&nbsp;</td></tr>';
  128. // Version
  129. $var=!$var;
  130. print '<tr '.$bc[$var].'><td>'.$langs->trans("Version").'</td><td>';
  131. $arraylist=array();
  132. $arraylist['3']='Version 3';
  133. $arraylist['2']='Version 2';
  134. print $html->selectarray('version',$arraylist,$conf->global->LDAP_SERVER_PROTOCOLVERSION);
  135. print '</td><td>'.$langs->trans("LDAPServerProtocolVersion").'</td></tr>';
  136. // Serveur primaire
  137. $var=!$var;
  138. print '<tr '.$bc[$var].'><td>';
  139. print $langs->trans("LDAPPrimaryServer").'</td><td>';
  140. print '<input size="25" type="text" name="host" value="'.$conf->global->LDAP_SERVER_HOST.'">';
  141. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  142. // Serveur secondaire
  143. $var=!$var;
  144. print '<tr '.$bc[$var].'><td>';
  145. print $langs->trans("LDAPSecondaryServer").'</td><td>';
  146. print '<input size="25" type="text" name="slave" value="'.$conf->global->LDAP_SERVER_HOST_SLAVE.'">';
  147. print '</td><td>'.$langs->trans("LDAPServerExample").'</td></tr>';
  148. // Port
  149. $var=!$var;
  150. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerPort").'</td><td>';
  151. if ($conf->global->LDAP_SERVER_PORT)
  152. {
  153. print '<input size="25" type="text" name="port" value="'.$conf->global->LDAP_SERVER_PORT.'">';
  154. }
  155. else
  156. {
  157. print '<input size="25" type="text" name="port" value="389">';
  158. }
  159. print '</td><td>'.$langs->trans("LDAPServerPortExample").'</td></tr>';
  160. // DNserver
  161. $var=!$var;
  162. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerDn").'</td><td>';
  163. print '<input size="25" type="text" name="dn" value="'.$conf->global->LDAP_SERVER_DN.'">';
  164. print '</td><td>'.$langs->trans("LDAPServerDnExample").'</td></tr>';
  165. // Utiliser TLS
  166. $var=!$var;
  167. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPServerUseTLS").'</td><td>';
  168. $arraylist=array();
  169. $arraylist['0']=$langs->trans("No");
  170. $arraylist['1']=$langs->trans("Yes");
  171. print $html->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. $var=!$var;
  178. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPAdminDn").'</td><td>';
  179. print '<input size="25" type="text" name="admin" value="'.$conf->global->LDAP_ADMIN_DN.'">';
  180. print '</td><td>'.$langs->trans("LDAPAdminDnExample").'</td></tr>';
  181. // Pass
  182. $var=!$var;
  183. print '<tr '.$bc[$var].'><td>'.$langs->trans("LDAPPassword").'</td><td>';
  184. if ($conf->global->LDAP_ADMIN_PASS)
  185. {
  186. print '<input size="25" type="password" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';// je le met en visible pour test
  187. }
  188. else
  189. {
  190. print '<input size="25" type="text" name="pass" value="'.$conf->global->LDAP_ADMIN_PASS.'">';
  191. }
  192. print '</td><td>secret</td></tr>';
  193. print '</table>';
  194. print '<center><input type="submit" class="button" value="'.$langs->trans("Modify").'"></center>';
  195. print '</form>';
  196. print '</div>';
  197. /*
  198. * Test de la connexion
  199. */
  200. if (function_exists("ldap_connect"))
  201. {
  202. if ($conf->global->LDAP_SERVER_HOST)
  203. {
  204. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?action=test">'.$langs->trans("LDAPTestConnect").'</a><br><br>';
  205. }
  206. if ($_GET["action"] == 'test')
  207. {
  208. $ldap = new Ldap(); // Les parametres sont passes et recuperes via $conf
  209. $result = $ldap->connect_bind();
  210. if ($result)
  211. {
  212. // Test ldap connect and bind
  213. print img_picto('','info').' ';
  214. print '<font class="ok">'.$langs->trans("LDAPTCPConnectOK",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
  215. print '<br>';
  216. if ($conf->global->LDAP_ADMIN_DN && $conf->global->LDAP_ADMIN_PASS)
  217. {
  218. if ($result == 2)
  219. {
  220. print img_picto('','info').' ';
  221. 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>';
  222. print '<br>';
  223. }
  224. else
  225. {
  226. print img_picto('','error').' ';
  227. 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>';
  228. print '<br>';
  229. print $langs->trans("Error").' '.$ldap->error;
  230. print '<br>';
  231. }
  232. }
  233. else
  234. {
  235. print img_picto('','warning').' ';
  236. print '<font class="warning">'.$langs->trans("LDAPNoUserOrPasswordProvidedAccessIsReadOnly").'</font>';
  237. print '<br>';
  238. }
  239. // Test ldap_getversion
  240. if (($ldap->getVersion() == 3))
  241. {
  242. print img_picto('','info').' ';
  243. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion3").'</font>';
  244. print '<br>';
  245. }
  246. else
  247. {
  248. print img_picto('','info').' ';
  249. print '<font class="ok">'.$langs->trans("LDAPSetupForVersion2").'</font>';
  250. print '<br>';
  251. }
  252. $unbind = $ldap->unbind();
  253. }
  254. else
  255. {
  256. print img_picto('','error').' ';
  257. print '<font class="error">'.$langs->trans("LDAPTCPConnectKO",$conf->global->LDAP_SERVER_HOST,$conf->global->LDAP_SERVER_PORT).'</font>';
  258. print '<br>';
  259. print $langs->trans("Error").' '.$ldap->error;
  260. print '<br>';
  261. }
  262. }
  263. }
  264. $db->close();
  265. llxFooter();
  266. ?>