ldap_contacts.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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_contacts.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. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  32. $langs->load("admin");
  33. $langs->load("errors");
  34. if (!$user->admin)
  35. accessforbidden();
  36. $action = GETPOST('action','aZ09');
  37. /*
  38. * Actions
  39. */
  40. if ($action == 'setvalue' && $user->admin)
  41. {
  42. $error=0;
  43. $db->begin();
  44. if (! dolibarr_set_const($db, 'LDAP_CONTACT_DN',GETPOST("contactdn"),'chaine',0,'',$conf->entity)) $error++;
  45. if (! dolibarr_set_const($db, 'LDAP_CONTACT_OBJECT_CLASS',GETPOST("objectclass"),'chaine',0,'',$conf->entity)) $error++;
  46. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FULLNAME',GETPOST("fieldfullname"),'chaine',0,'',$conf->entity)) $error++;
  47. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_NAME',GETPOST("fieldname"),'chaine',0,'',$conf->entity)) $error++;
  48. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FIRSTNAME',GETPOST("fieldfirstname"),'chaine',0,'',$conf->entity)) $error++;
  49. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COMPANY',GETPOST("fieldcompany"),'chaine',0,'',$conf->entity)) $error++;
  50. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MAIL',GETPOST("fieldmail"),'chaine',0,'',$conf->entity)) $error++;
  51. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_PHONE',GETPOST("fieldphone"),'chaine',0,'',$conf->entity)) $error++;
  52. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_HOMEPHONE',GETPOST("fieldhomephone"),'chaine',0,'',$conf->entity)) $error++;
  53. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_MOBILE',GETPOST("fieldmobile"),'chaine',0,'',$conf->entity)) $error++;
  54. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_SKYPE',GETPOST("fieldskype"),'chaine',0,'',$conf->entity)) $error++;
  55. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_FAX',GETPOST("fieldfax"),'chaine',0,'',$conf->entity)) $error++;
  56. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ADDRESS',GETPOST("fieldaddress"),'chaine',0,'',$conf->entity)) $error++;
  57. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_ZIP',GETPOST("fieldzip"),'chaine',0,'',$conf->entity)) $error++;
  58. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_TOWN',GETPOST("fieldtown"),'chaine',0,'',$conf->entity)) $error++;
  59. if (! dolibarr_set_const($db, 'LDAP_CONTACT_FIELD_COUNTRY',GETPOST("fieldcountry"),'chaine',0,'',$conf->entity)) $error++;
  60. // This one must be after the others
  61. $valkey='';
  62. $key=GETPOST("key");
  63. if ($key) $valkey=$conf->global->$key;
  64. if (! dolibarr_set_const($db, 'LDAP_KEY_CONTACTS',$valkey,'chaine',0,'',$conf->entity)) $error++;
  65. if (! $error)
  66. {
  67. $db->commit();
  68. setEventMessages($langs->trans("SetupSaved"), null, 'mesgs');
  69. }
  70. else
  71. {
  72. $db->rollback();
  73. dol_print_error($db);
  74. }
  75. }
  76. /*
  77. * View
  78. */
  79. $form=new Form($db);
  80. llxHeader('',$langs->trans("LDAPSetup"),'EN:Module_LDAP_En|FR:Module_LDAP|ES:M&oacute;dulo_LDAP');
  81. $linkback='<a href="'.DOL_URL_ROOT.'/admin/modules.php">'.$langs->trans("BackToModuleList").'</a>';
  82. print load_fiche_titre($langs->trans("LDAPSetup"),$linkback,'title_setup');
  83. $head = ldap_prepare_head();
  84. // Test si fonction LDAP actives
  85. if (! function_exists("ldap_connect"))
  86. {
  87. setEventMessages($langs->trans("LDAPFunctionsNotAvailableOnPHP"), null, 'errors');
  88. }
  89. dol_fiche_head($head, 'contacts', $langs->trans("LDAPSetup"));
  90. print $langs->trans("LDAPDescContact").'<br>';
  91. print '<br>';
  92. print '<form method="post" action="'.$_SERVER["PHP_SELF"].'?action=setvalue">';
  93. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  94. print '<table class="noborder" width="100%">';
  95. $var=true;
  96. print '<tr class="liste_titre">';
  97. print '<td colspan="4">'.$langs->trans("LDAPSynchronizeContacts").'</td>';
  98. print "</tr>\n";
  99. // DN Pour les contacts
  100. print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPContactDn").'</span></td><td>';
  101. print '<input size="48" type="text" name="contactdn" value="'.$conf->global->LDAP_CONTACT_DN.'">';
  102. print '</td><td>'.$langs->trans("LDAPContactDnExample").'</td>';
  103. print '<td>&nbsp;</td>';
  104. print '</tr>';
  105. // List of object class used to define attributes in structure
  106. print '<tr class="oddeven"><td width="25%"><span class="fieldrequired">'.$langs->trans("LDAPContactObjectClassList").'</span></td><td>';
  107. print '<input size="48" type="text" name="objectclass" value="'.$conf->global->LDAP_CONTACT_OBJECT_CLASS.'">';
  108. print '</td><td>'.$langs->trans("LDAPContactObjectClassListExample").'</td>';
  109. print '<td>&nbsp;</td>';
  110. print '</tr>';
  111. print '</table>';
  112. print '<br>';
  113. print '<table class="noborder" width="100%">';
  114. $var=true;
  115. print '<tr class="liste_titre">';
  116. print '<td width="25%">'.$langs->trans("LDAPDolibarrMapping").'</td>';
  117. print '<td colspan="2">'.$langs->trans("LDAPLdapMapping").'</td>';
  118. print '<td align="right">'.$langs->trans("LDAPNamingAttribute").'</td>';
  119. print "</tr>\n";
  120. // Common name
  121. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFullname").'</td><td>';
  122. print '<input size="25" type="text" name="fieldfullname" value="'.$conf->global->LDAP_CONTACT_FIELD_FULLNAME.'">';
  123. print '</td><td>'.$langs->trans("LDAPFieldFullnameExample").'</td>';
  124. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FULLNAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_FULLNAME?' checked':'')."></td>";
  125. print '</tr>';
  126. // Name
  127. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldName").'</td><td>';
  128. print '<input size="25" type="text" name="fieldname" value="'.$conf->global->LDAP_CONTACT_FIELD_NAME.'">';
  129. print '</td><td>'.$langs->trans("LDAPFieldNameExample").'</td>';
  130. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_NAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_NAME?' checked':'')."></td>";
  131. print '</tr>';
  132. // Firstname
  133. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFirstName").'</td><td>';
  134. print '<input size="25" type="text" name="fieldfirstname" value="'.$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME.'">';
  135. print '</td><td>'.$langs->trans("LDAPFieldFirstNameExample").'</td>';
  136. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FIRSTNAME"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_FIRSTNAME?' checked':'')."></td>";
  137. print '</tr>';
  138. // Company
  139. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCompany").'</td><td>';
  140. print '<input size="25" type="text" name="fieldcompany" value="'.$conf->global->LDAP_CONTACT_FIELD_COMPANY.'">';
  141. print '</td><td>'.$langs->trans("LDAPFieldCompanyExample").'</td>';
  142. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COMPANY"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_COMPANY?' checked':'')."></td>";
  143. print '</tr>';
  144. // Mail
  145. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMail").'</td><td>';
  146. print '<input size="25" type="text" name="fieldmail" value="'.$conf->global->LDAP_CONTACT_FIELD_MAIL.'">';
  147. print '</td><td>'.$langs->trans("LDAPFieldMailExample").'</td>';
  148. print '<td align="right"><input type="radio" name="key" value=">LDAP_CONTACT_FIELD_MAIL"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_MAIL?' checked':'')."></td>";
  149. print '</tr>';
  150. // Phone pro
  151. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldPhone").'</td><td>';
  152. print '<input size="25" type="text" name="fieldphone" value="'.$conf->global->LDAP_CONTACT_FIELD_PHONE.'">';
  153. print '</td><td>'.$langs->trans("LDAPFieldPhoneExample").'</td>';
  154. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_PHONE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_PHONE?' checked':'')."></td>";
  155. print '</tr>';
  156. // Phone home
  157. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldHomePhone").'</td><td>';
  158. print '<input size="25" type="text" name="fieldhomephone" value="'.$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE.'">';
  159. print '</td><td>'.$langs->trans("LDAPFieldHomePhoneExample").'</td>';
  160. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_HOMEPHONE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_HOMEPHONE?' checked':'')."></td>";
  161. print '</tr>';
  162. // Mobile
  163. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldMobile").'</td><td>';
  164. print '<input size="25" type="text" name="fieldmobile" value="'.$conf->global->LDAP_CONTACT_FIELD_MOBILE.'">';
  165. print '</td><td>'.$langs->trans("LDAPFieldMobileExample").'</td>';
  166. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_MOBILE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_MOBILE?' checked':'')."></td>";
  167. print '</tr>';
  168. // Skype
  169. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldSkype").'</td><td>';
  170. print '<input size="25" type="text" name="fieldskype" value="'.$conf->global->LDAP_CONTACT_FIELD_SKYPE.'">';
  171. print '</td><td>'.$langs->trans("LDAPFieldSkypeExample").'</td>';
  172. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_SKYPE"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_SKYPE?' checked':'')."></td>";
  173. print '</tr>';
  174. // Fax
  175. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldFax").'</td><td>';
  176. print '<input size="25" type="text" name="fieldfax" value="'.$conf->global->LDAP_CONTACT_FIELD_FAX.'">';
  177. print '</td><td>'.$langs->trans("LDAPFieldFaxExample").'</td>';
  178. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_FAX"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_FAX?' checked':'')."></td>";
  179. print '</tr>';
  180. // Address
  181. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldAddress").'</td><td>';
  182. print '<input size="25" type="text" name="fieldaddress" value="'.$conf->global->LDAP_CONTACT_FIELD_ADDRESS.'">';
  183. print '</td><td>'.$langs->trans("LDAPFieldAddressExample").'</td>';
  184. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ADDRESS"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_ADDRESS?' checked':'')."></td>";
  185. print '</tr>';
  186. // ZIP
  187. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldZip").'</td><td>';
  188. print '<input size="25" type="text" name="fieldzip" value="'.$conf->global->LDAP_CONTACT_FIELD_ZIP.'">';
  189. print '</td><td>'.$langs->trans("LDAPFieldZipExample").'</td>';
  190. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_ZIP"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_ZIP?' checked':'')."></td>";
  191. print '</tr>';
  192. // TOWN
  193. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldTown").'</td><td>';
  194. print '<input size="25" type="text" name="fieldtown" value="'.$conf->global->LDAP_CONTACT_FIELD_TOWN.'">';
  195. print '</td><td>'.$langs->trans("LDAPFieldTownExample").'</td>';
  196. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_TOWN"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_TOWN?' checked':'')."></td>";
  197. print '</tr>';
  198. // COUNTRY
  199. print '<tr class="oddeven"><td>'.$langs->trans("LDAPFieldCountry").'</td><td>';
  200. print '<input size="25" type="text" name="fieldcountry" value="'.$conf->global->LDAP_CONTACT_FIELD_COUNTRY.'">';
  201. print '</td><td>&nbsp;</td>';
  202. print '<td align="right"><input type="radio" name="key" value="LDAP_CONTACT_FIELD_COUNTRY"'.($conf->global->LDAP_KEY_CONTACTS && $conf->global->LDAP_KEY_CONTACTS==$conf->global->LDAP_CONTACT_FIELD_COUNTRY?' checked':'')."></td>";
  203. print '</tr>';
  204. print '</table>';
  205. print info_admin($langs->trans("LDAPDescValues"));
  206. dol_fiche_end();
  207. print '<div class="center"><input type="submit" class="button" value="'.$langs->trans("Modify").'"></div>';
  208. print '</form>';
  209. /*
  210. * Test de la connexion
  211. */
  212. $butlabel=$langs->trans("LDAPTestSynchroContact");
  213. $testlabel='test';
  214. $key=$conf->global->LDAP_KEY_CONTACTS;
  215. $dn=$conf->global->LDAP_CONTACT_DN;
  216. $objectclass=$conf->global->LDAP_CONTACT_OBJECT_CLASS;
  217. show_ldap_test_button($butlabel,$testlabel,$key,$dn,$objectclass);
  218. if (function_exists("ldap_connect"))
  219. {
  220. if ($_GET["action"] == 'test')
  221. {
  222. // Creation objet
  223. $object=new Contact($db);
  224. $object->initAsSpecimen();
  225. // Test synchro
  226. $ldap=new Ldap();
  227. $result=$ldap->connect_bind();
  228. if ($result > 0)
  229. {
  230. $info=$object->_load_ldap_info();
  231. $dn=$object->_load_ldap_dn($info);
  232. $result1=$ldap->delete($dn); // To be sure to delete existing records
  233. $result2=$ldap->add($dn,$info,$user); // Now the test
  234. $result3=$ldap->delete($dn); // Clean what we did
  235. if ($result2 > 0)
  236. {
  237. print img_picto('','info').' ';
  238. print '<font class="ok">'.$langs->trans("LDAPSynchroOK").'</font><br>';
  239. }
  240. else
  241. {
  242. print img_picto('','error').' ';
  243. print '<font class="error">'.$langs->trans("LDAPSynchroKOMayBePermissions");
  244. print ': '.$ldap->error;
  245. print '</font><br>';
  246. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  247. }
  248. print "<br>\n";
  249. print "LDAP input file used for test:<br><br>\n";
  250. print nl2br($ldap->dump_content($dn,$info));
  251. print "\n<br>";
  252. }
  253. else
  254. {
  255. print img_picto('','error').' ';
  256. print '<font class="error">'.$langs->trans("LDAPSynchroKO");
  257. print ': '.$ldap->error;
  258. print '</font><br>';
  259. print $langs->trans("ErrorLDAPMakeManualTest",$conf->ldap->dir_temp).'<br>';
  260. }
  261. }
  262. }
  263. llxFooter();
  264. $db->close();