Default.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Ldap
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id$
  20. */
  21. /**
  22. * Zend_Ldap_Collection_Iterator_Default is the default collection iterator implementation
  23. * using ext/ldap
  24. *
  25. * @category Zend
  26. * @package Zend_Ldap
  27. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  28. * @license http://framework.zend.com/license/new-bsd New BSD License
  29. */
  30. class Zend_Ldap_Collection_Iterator_Default implements Iterator, Countable
  31. {
  32. /**
  33. * LDAP Connection
  34. *
  35. * @var Zend_Ldap
  36. */
  37. protected $_ldap = null;
  38. /**
  39. * Result identifier resource
  40. *
  41. * @var resource
  42. */
  43. protected $_resultId = null;
  44. /**
  45. * Current result entry identifier
  46. *
  47. * @var resource
  48. */
  49. protected $_current = null;
  50. /**
  51. * Number of items in query result
  52. *
  53. * @var integer
  54. */
  55. protected $_itemCount = -1;
  56. /**
  57. * Constructor.
  58. *
  59. * @param Zend_Ldap $ldap
  60. * @param resource $resultId
  61. * @return void
  62. */
  63. public function __construct(Zend_Ldap $ldap, $resultId)
  64. {
  65. $this->_ldap = $ldap;
  66. $this->_resultId = $resultId;
  67. $this->_itemCount = @ldap_count_entries($ldap->getResource(), $resultId);
  68. if ($this->_itemCount === false) {
  69. /**
  70. * @see Zend_Ldap_Exception
  71. */
  72. require_once 'Zend/Ldap/Exception.php';
  73. throw new Zend_Ldap_Exception($this->_ldap, 'counting entries');
  74. }
  75. }
  76. public function __destruct()
  77. {
  78. $this->close();
  79. }
  80. /**
  81. * Closes the current result set
  82. *
  83. * @return bool
  84. */
  85. public function close()
  86. {
  87. $isClosed = false;
  88. if (is_resource($this->_resultId)) {
  89. $isClosed = @ldap_free_result($this->_resultId);
  90. $this->_resultId = null;
  91. $this->_current = null;
  92. }
  93. return $isClosed;
  94. }
  95. /**
  96. * Gets the current LDAP connection.
  97. *
  98. * @return Zend_Ldap
  99. */
  100. public function getLdap()
  101. {
  102. return $this->_ldap;
  103. }
  104. /**
  105. * Returns the number of items in current result
  106. * Implements Countable
  107. *
  108. * @return int
  109. */
  110. public function count()
  111. {
  112. return $this->_itemCount;
  113. }
  114. /**
  115. * Return the current result item
  116. * Implements Iterator
  117. *
  118. * @return array|null
  119. * @throws Zend_Ldap_Exception
  120. */
  121. public function current()
  122. {
  123. if (!is_resource($this->_current)) {
  124. $this->rewind();
  125. }
  126. if (!is_resource($this->_current)) {
  127. return null;
  128. }
  129. $entry = array('dn' => $this->key());
  130. $ber_identifier = null;
  131. $name = @ldap_first_attribute($this->_ldap->getResource(), $this->_current,
  132. $ber_identifier);
  133. while ($name) {
  134. $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
  135. unset($data['count']);
  136. $entry[strtolower($name)] = $data;
  137. $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
  138. $ber_identifier);
  139. }
  140. ksort($entry, SORT_LOCALE_STRING);
  141. return $entry;
  142. }
  143. /**
  144. * Return the result item key
  145. * Implements Iterator
  146. *
  147. * @return string|null
  148. */
  149. public function key()
  150. {
  151. if (!is_resource($this->_current)) {
  152. $this->rewind();
  153. }
  154. if (is_resource($this->_current)) {
  155. $currentDn = @ldap_get_dn($this->_ldap->getResource(), $this->_current);
  156. if ($currentDn === false) {
  157. /** @see Zend_Ldap_Exception */
  158. require_once 'Zend/Ldap/Exception.php';
  159. throw new Zend_Ldap_Exception($this->_ldap, 'getting dn');
  160. }
  161. return $currentDn;
  162. } else {
  163. return null;
  164. }
  165. }
  166. /**
  167. * Move forward to next result item
  168. * Implements Iterator
  169. *
  170. * @throws Zend_Ldap_Exception
  171. */
  172. public function next()
  173. {
  174. if (is_resource($this->_current)) {
  175. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  176. /** @see Zend_Ldap_Exception */
  177. require_once 'Zend/Ldap/Exception.php';
  178. if ($this->_current === false) {
  179. $msg = $this->_ldap->getLastError($code);
  180. if ($code === Zend_Ldap_Exception::LDAP_SIZELIMIT_EXCEEDED) {
  181. // we have reached the size limit enforced by the server
  182. return;
  183. } else if ($code > Zend_Ldap_Exception::LDAP_SUCCESS) {
  184. throw new Zend_Ldap_Exception($this->_ldap, 'getting next entry (' . $msg . ')');
  185. }
  186. }
  187. }
  188. }
  189. /**
  190. * Rewind the Iterator to the first result item
  191. * Implements Iterator
  192. *
  193. * @throws Zend_Ldap_Exception
  194. */
  195. public function rewind()
  196. {
  197. if (is_resource($this->_resultId)) {
  198. $this->_current = @ldap_first_entry($this->_ldap->getResource(), $this->_resultId);
  199. /** @see Zend_Ldap_Exception */
  200. require_once 'Zend/Ldap/Exception.php';
  201. if ($this->_current === false &&
  202. $this->_ldap->getLastErrorCode() > Zend_Ldap_Exception::LDAP_SUCCESS) {
  203. throw new Zend_Ldap_Exception($this->_ldap, 'getting first entry');
  204. }
  205. }
  206. }
  207. /**
  208. * Check if there is a current result item
  209. * after calls to rewind() or next()
  210. * Implements Iterator
  211. *
  212. * @return boolean
  213. */
  214. public function valid()
  215. {
  216. return (is_resource($this->_current));
  217. }
  218. }