ldap.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /* Copyright (C) 2006-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006-2021 Regis Houssin <regis.houssin@inodbox.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/contact/ldap.php
  20. * \ingroup ldap
  21. * \brief Page fiche LDAP contact
  22. */
  23. // Load Dolibarr environment
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/contact.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/class/ldap.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/ldap.lib.php';
  29. // Load translation files required by the page
  30. $langs->loadLangs(array('companies', 'ldap'));
  31. $langs->load("admin");
  32. $action = GETPOST('action', 'aZ09');
  33. // Security check
  34. $id = GETPOST('id', 'int');
  35. if ($user->socid) {
  36. $socid = $user->socid;
  37. }
  38. $result = restrictedArea($user, 'contact', $id, 'socpeople&societe');
  39. $object = new Contact($db);
  40. if ($id > 0) {
  41. $object->fetch($id, $user);
  42. }
  43. /*
  44. * Actions
  45. */
  46. if ($action == 'dolibarr2ldap') {
  47. $db->begin();
  48. $ldap = new Ldap();
  49. $result = $ldap->connect_bind();
  50. $info = $object->_load_ldap_info();
  51. $dn = $object->_load_ldap_dn($info);
  52. $olddn = $dn; // We can say that old dn = dn as we force synchro
  53. $result = $ldap->update($dn, $info, $user, $olddn);
  54. if ($result >= 0) {
  55. setEventMessages($langs->trans("ContactSynchronized"), null, 'mesgs');
  56. $db->commit();
  57. } else {
  58. setEventMessages($ldap->error, $ldap->errors, 'errors');
  59. $db->rollback();
  60. }
  61. }
  62. /*
  63. * View
  64. */
  65. $form = new Form($db);
  66. $title = (!empty($conf->global->SOCIETE_ADDRESSES_MANAGEMENT) ? $langs->trans("Contacts") : $langs->trans("ContactsAddresses"));
  67. llxHeader('', $title, 'EN:Module_Third_Parties|FR:Module_Tiers|ES:M&oacute;dulo_Empresas');
  68. $head = contact_prepare_head($object);
  69. print dol_get_fiche_head($head, 'ldap', $title, -1, 'contact');
  70. $linkback = '<a href="'.DOL_URL_ROOT.'/contact/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  71. dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', '');
  72. print '<div class="fichecenter">';
  73. print '<div class="underbanner clearboth"></div>';
  74. print '<table class="border centpercent">';
  75. // Company
  76. if ($object->socid > 0) {
  77. $thirdparty = new Societe($db);
  78. $thirdparty->fetch($object->socid);
  79. print '<tr><td class="titlefield">'.$langs->trans("ThirdParty").'</td><td colspan="3">'.$thirdparty->getNomUrl(1).'</td></tr>';
  80. } else {
  81. print '<tr><td class="titlefield">'.$langs->trans("ThirdParty").'</td><td colspan="3">';
  82. print $langs->trans("ContactNotLinkedToCompany");
  83. print '</td></tr>';
  84. }
  85. // Civility
  86. print '<tr><td class="titlefield">'.$langs->trans("UserTitle").'</td><td colspan="3">';
  87. print $object->getCivilityLabel();
  88. print '</td></tr>';
  89. // LDAP DN
  90. print '<tr><td>LDAP '.$langs->trans("LDAPContactDn").'</td><td class="valeur" colspan="3">'.getDolGlobalString('LDAP_CONTACT_DN')."</td></tr>\n";
  91. // LDAP Cle
  92. print '<tr><td>LDAP '.$langs->trans("LDAPNamingAttribute").'</td><td class="valeur" colspan="3">'.getDolGlobalString('LDAP_KEY_CONTACTS')."</td></tr>\n";
  93. // LDAP Server
  94. print '<tr><td>LDAP '.$langs->trans("LDAPPrimaryServer").'</td><td class="valeur" colspan="3">'.getDolGlobalString('LDAP_SERVER_HOST')."</td></tr>\n";
  95. print '<tr><td>LDAP '.$langs->trans("LDAPSecondaryServer").'</td><td class="valeur" colspan="3">'.getDolGlobalString('LDAP_SERVER_HOST_SLAVE')."</td></tr>\n";
  96. print '<tr><td>LDAP '.$langs->trans("LDAPServerPort").'</td><td class="valeur" colspan="3">'.getDolGlobalString('LDAP_SERVER_PORT')."</td></tr>\n";
  97. print '</table>';
  98. print '</div>';
  99. print dol_get_fiche_end();
  100. /*
  101. * Action bar
  102. */
  103. print '<div class="tabsAction">';
  104. if (!empty($conf->global->LDAP_CONTACT_ACTIVE) && getDolGlobalInt('LDAP_CONTACT_ACTIVE') != Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
  105. print '<a class="butAction" href="'.$_SERVER["PHP_SELF"].'?id='.$object->id.'&amp;action=dolibarr2ldap">'.$langs->trans("ForceSynchronize").'</a>';
  106. }
  107. print "</div>\n";
  108. if (!empty($conf->global->LDAP_CONTACT_ACTIVE) && getDolGlobalInt('LDAP_CONTACT_ACTIVE') != Ldap::SYNCHRO_LDAP_TO_DOLIBARR) {
  109. print "<br>\n";
  110. }
  111. // Affichage attributs LDAP
  112. print load_fiche_titre($langs->trans("LDAPInformationsForThisContact"));
  113. print '<table width="100%" class="noborder">';
  114. print '<tr class="liste_titre">';
  115. print '<td>'.$langs->trans("LDAPAttributes").'</td>';
  116. print '<td>'.$langs->trans("Value").'</td>';
  117. print '</tr>';
  118. // Lecture LDAP
  119. $ldap = new Ldap();
  120. $result = $ldap->connect_bind();
  121. if ($result > 0) {
  122. $info = $object->_load_ldap_info();
  123. $dn = $object->_load_ldap_dn($info, 1);
  124. $search = "(".$object->_load_ldap_dn($info, 2).")";
  125. $records = $ldap->getAttribute($dn, $search);
  126. //var_dump($records);
  127. // Show tree
  128. if (((!is_numeric($records)) || $records != 0) && (!isset($records['count']) || $records['count'] > 0)) {
  129. if (!is_array($records)) {
  130. print '<tr class="oddeven"><td colspan="2"><span class="error">'.$langs->trans("ErrorFailedToReadLDAP").'</span></td></tr>';
  131. } else {
  132. $result = show_ldap_content($records, 0, $records['count'], true);
  133. }
  134. } else {
  135. print '<tr class="oddeven"><td colspan="2">'.$langs->trans("LDAPRecordNotFound").' (dn='.dol_escape_htmltag($dn).' - search='.dol_escape_htmltag($search).')</td></tr>';
  136. }
  137. $ldap->unbind();
  138. } else {
  139. setEventMessages($ldap->error, $ldap->errors, 'errors');
  140. }
  141. print '</table>';
  142. llxFooter();
  143. $db->close();