ldap_contacts.php 15 KB

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