You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Result.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /*
  3. +-----------------------------------------------------------------------+
  4. | Net/LDAP3/Result.php |
  5. | |
  6. | Based on code created by the Roundcube Webmail team. |
  7. | |
  8. | Copyright (C) 2006-2014, The Roundcube Dev Team |
  9. | Copyright (C) 2012-2014, Kolab Systems AG |
  10. | |
  11. | This program is free software: you can redistribute it and/or modify |
  12. | it under the terms of the GNU General Public License as published by |
  13. | the Free Software Foundation, either version 3 of the License, or |
  14. | (at your option) any later version. |
  15. | |
  16. | This program is distributed in the hope that it will be useful, |
  17. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  18. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  19. | GNU General Public License for more details. |
  20. | |
  21. | You should have received a copy of the GNU General Public License |
  22. | along with this program. If not, see <http://www.gnu.org/licenses/>. |
  23. | |
  24. | PURPOSE: |
  25. | Provide advanced functionality for accessing LDAP directories |
  26. | |
  27. +-----------------------------------------------------------------------+
  28. | Authors: Thomas Bruederli <roundcube@gmail.com> |
  29. | Jeroen van Meeuwen <vanmeeuwen@kolabsys.com> |
  30. +-----------------------------------------------------------------------+
  31. */
  32. /**
  33. * Model class representing an LDAP search result
  34. *
  35. * @package LDAP
  36. */
  37. class Net_LDAP3_Result implements Iterator
  38. {
  39. protected $conn;
  40. protected $base_dn;
  41. protected $filter;
  42. protected $scope;
  43. private $count;
  44. private $current;
  45. private $iteratorkey = 0;
  46. /**
  47. * Default constructor
  48. *
  49. * @param resource $conn LDAP link identifier
  50. * @param string $base_dn Base DN used to get this result
  51. * @param string $filter Filter query used to get this result
  52. * @param string $scope Scope of the result
  53. * @param resource $result LDAP result entry identifier
  54. */
  55. function __construct($conn, $base_dn, $filter, $scope, $result)
  56. {
  57. $this->conn = $conn;
  58. $this->base_dn = $base_dn;
  59. $this->filter = $filter;
  60. $this->scope = $scope;
  61. $this->result = $result;
  62. }
  63. /**
  64. * Property value getter
  65. *
  66. * @param string $property Property name
  67. * @param mixed $default Return value if proprty is not set
  68. *
  69. * @return mixed Property value
  70. */
  71. public function get($property, $default = null)
  72. {
  73. return isset($this->$property) ? $this->$property : $default;
  74. }
  75. /**
  76. * Property value setter
  77. *
  78. * @param string $property Property name
  79. * @param mixed $value Property value
  80. */
  81. public function set($property, $value)
  82. {
  83. $this->$property = $value;
  84. }
  85. /**
  86. * Wrapper for ldap_sort()
  87. *
  88. * @param string $attr The attribute to use as a key in the sort
  89. *
  90. * @return bool True on success, False on failure
  91. */
  92. public function sort($attr)
  93. {
  94. // @TODO: Don't use ldap_sort() it's deprecated since PHP7
  95. // and will be removed in future
  96. return @ldap_sort($this->conn, $this->result, $attr);
  97. }
  98. /**
  99. * Get entries count
  100. *
  101. * @return int Number of entries in the result
  102. */
  103. public function count()
  104. {
  105. if (!isset($this->count)) {
  106. $this->count = ldap_count_entries($this->conn, $this->result);
  107. }
  108. return $this->count;
  109. }
  110. /**
  111. * Wrapper for ldap_get_entries()
  112. *
  113. * @param bool $normalize Optionally normalize the entries to a list of hash arrays
  114. *
  115. * @return array List of LDAP entries
  116. */
  117. public function entries($normalize = false)
  118. {
  119. $entries = ldap_get_entries($this->conn, $this->result);
  120. if ($normalize) {
  121. return Net_LDAP3::normalize_result($entries);
  122. }
  123. return $entries;
  124. }
  125. /**
  126. * Wrapper for ldap_get_dn() using the current entry pointer
  127. */
  128. public function get_dn()
  129. {
  130. return $this->current ? ldap_get_dn($this->conn, $this->current) : null;
  131. }
  132. /*** Implement PHP 5 Iterator interface to make foreach work ***/
  133. function current()
  134. {
  135. $attrib = ldap_get_attributes($this->conn, $this->current);
  136. $attrib['dn'] = ldap_get_dn($this->conn, $this->current);
  137. return $attrib;
  138. }
  139. function key()
  140. {
  141. return $this->iteratorkey;
  142. }
  143. function rewind()
  144. {
  145. $this->iteratorkey = 0;
  146. $this->current = ldap_first_entry($this->conn, $this->result);
  147. }
  148. function next()
  149. {
  150. $this->iteratorkey++;
  151. $this->current = ldap_next_entry($this->conn, $this->current);
  152. }
  153. function valid()
  154. {
  155. return (bool)$this->current;
  156. }
  157. }