ldap.lib.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /* Copyright (C) 2006 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. * or see https://www.gnu.org/
  18. */
  19. /**
  20. * \file htdocs/core/lib/ldap.lib.php
  21. * \brief Ensemble de fonctions de base pour le module LDAP
  22. * \ingroup ldap
  23. */
  24. /**
  25. * Initialize the array of tabs for customer invoice
  26. *
  27. * @return array Array of head tabs
  28. */
  29. function ldap_prepare_head()
  30. {
  31. global $langs, $conf, $user;
  32. $langs->load("ldap");
  33. // Onglets
  34. $head = array();
  35. $h = 0;
  36. $head[$h][0] = DOL_URL_ROOT."/admin/ldap.php";
  37. $head[$h][1] = $langs->trans("LDAPGlobalParameters");
  38. $head[$h][2] = 'ldap';
  39. $h++;
  40. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE)) {
  41. $head[$h][0] = DOL_URL_ROOT."/admin/ldap_users.php";
  42. $head[$h][1] = $langs->trans("LDAPUsersSynchro");
  43. $head[$h][2] = 'users';
  44. $h++;
  45. }
  46. if (!empty($conf->global->LDAP_SYNCHRO_ACTIVE)) {
  47. $head[$h][0] = DOL_URL_ROOT."/admin/ldap_groups.php";
  48. $head[$h][1] = $langs->trans("LDAPGroupsSynchro");
  49. $head[$h][2] = 'groups';
  50. $h++;
  51. }
  52. if (isModEnabled("societe") && !empty($conf->global->LDAP_CONTACT_ACTIVE)) {
  53. $head[$h][0] = DOL_URL_ROOT."/admin/ldap_contacts.php";
  54. $head[$h][1] = $langs->trans("LDAPContactsSynchro");
  55. $head[$h][2] = 'contacts';
  56. $h++;
  57. }
  58. if (isModEnabled('adherent') && !empty($conf->global->LDAP_MEMBER_ACTIVE)) {
  59. $head[$h][0] = DOL_URL_ROOT."/admin/ldap_members.php";
  60. $head[$h][1] = $langs->trans("LDAPMembersSynchro");
  61. $head[$h][2] = 'members';
  62. $h++;
  63. }
  64. if (isModEnabled('adherent') && !empty($conf->global->LDAP_MEMBER_TYPE_ACTIVE)) {
  65. $head[$h][0] = DOL_URL_ROOT."/admin/ldap_members_types.php";
  66. $head[$h][1] = $langs->trans("LDAPMembersTypesSynchro");
  67. $head[$h][2] = 'memberstypes';
  68. $h++;
  69. }
  70. // Show more tabs from modules
  71. // Entries must be declared in modules descriptor with line
  72. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  73. // $this->tabs = array('entity:-tabname); to remove a tab
  74. complete_head_from_modules($conf, $langs, null, $head, $h, 'ldap');
  75. complete_head_from_modules($conf, $langs, null, $head, $h, 'ldap', 'remove');
  76. return $head;
  77. }
  78. /**
  79. * Show button test LDAP synchro
  80. *
  81. * @param string $butlabel Label
  82. * @param string $testlabel Label
  83. * @param string $key Key
  84. * @param string $dn Dn
  85. * @param string $objectclass Class
  86. * @return void
  87. */
  88. function show_ldap_test_button($butlabel, $testlabel, $key, $dn, $objectclass)
  89. {
  90. global $langs, $conf, $user;
  91. //print 'key='.$key.' dn='.$dn.' objectclass='.$objectclass;
  92. print '<br>';
  93. if (!function_exists("ldap_connect")) {
  94. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans('LDAPFunctionsNotAvailableOnPHP').'">'.$butlabel.'</a>';
  95. } elseif (empty($conf->global->LDAP_SERVER_HOST)) {
  96. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans('LDAPSetupNotComplete').'">'.$butlabel.'</a>';
  97. } elseif (empty($key) || empty($dn) || empty($objectclass)) {
  98. $langs->load("errors");
  99. print '<a class="butActionRefused classfortooltip" href="#" title="'.$langs->trans('ErrorLDAPSetupNotComplete').'">'.$butlabel.'</a>';
  100. } else {
  101. print '<a class="butAction reposition" href="'.$_SERVER["PHP_SELF"].'?action='.$testlabel.'">'.$butlabel.'</a>';
  102. }
  103. print '<br><br>';
  104. }
  105. /**
  106. * Show a LDAP array into an HTML output array.
  107. *
  108. * @param array $result Array to show. This array is already encoded into charset_output
  109. * @param int $level Level
  110. * @param int $count Count
  111. * @param string $var Var
  112. * @param int $hide Hide
  113. * @param int $subcount Subcount
  114. * @return int
  115. */
  116. function show_ldap_content($result, $level, $count, $var, $hide = 0, $subcount = 0)
  117. {
  118. global $bc, $conf;
  119. $count--;
  120. if ($count == 0) {
  121. return -1; // To stop loop
  122. }
  123. if (!is_array($result)) {
  124. return -1;
  125. }
  126. foreach ($result as $key => $val) {
  127. if ("$key" == "objectclass") {
  128. continue;
  129. }
  130. if ("$key" == "count") {
  131. continue;
  132. }
  133. if ("$key" == "dn") {
  134. continue;
  135. }
  136. if (!is_array($val) && "$val" == "objectclass") {
  137. continue;
  138. }
  139. $lastkey[$level] = $key;
  140. if (is_array($val)) {
  141. $hide = 0;
  142. if (!is_numeric($key)) {
  143. print '<tr class="oddeven">';
  144. print '<td>';
  145. print $key;
  146. print '</td><td>';
  147. if (strtolower($key) == 'userpassword') {
  148. $hide = 1;
  149. }
  150. }
  151. show_ldap_content($val, $level + 1, $count, $var, $hide, $val["count"]);
  152. } elseif ($subcount) {
  153. $subcount--;
  154. $newstring = dol_htmlentitiesbr($val);
  155. if ($hide) {
  156. print preg_replace('/./i', '*', $newstring);
  157. } else {
  158. print $newstring;
  159. }
  160. print '<br>';
  161. }
  162. if (!is_array($val) && "$val" != $lastkey[$level] && !$subcount) {
  163. print '</td></tr>';
  164. }
  165. }
  166. return 1;
  167. }