Default.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. * Current result entry DN
  52. *
  53. * @var string
  54. */
  55. protected $_currentDn = null;
  56. /**
  57. * Number of items in query result
  58. *
  59. * @var integer
  60. */
  61. protected $_itemCount = -1;
  62. /**
  63. * Constructor.
  64. *
  65. * @param Zend_Ldap $ldap
  66. * @param resource $resultId
  67. * @return void
  68. */
  69. public function __construct(Zend_Ldap $ldap, $resultId)
  70. {
  71. $this->_ldap = $ldap;
  72. $this->_resultId = $resultId;
  73. $this->_itemCount = @ldap_count_entries($ldap->getResource(), $resultId);
  74. if ($this->_itemCount === false) {
  75. /**
  76. * @see Zend_Ldap_Exception
  77. */
  78. require_once 'Zend/Ldap/Exception.php';
  79. throw new Zend_Ldap_Exception($this->_ldap, 'counting entries');
  80. }
  81. }
  82. public function __destruct()
  83. {
  84. $this->close();
  85. }
  86. /**
  87. * Closes the current result set
  88. *
  89. * @return bool
  90. */
  91. public function close()
  92. {
  93. $isClosed = false;
  94. if (is_resource($this->_resultId)) {
  95. $isClosed = @ldap_free_result($this->_resultId);
  96. $this->_resultId = null;
  97. $this->_currentDn = null;
  98. $this->_current = null;
  99. }
  100. return $isClosed;
  101. }
  102. /**
  103. * Gets the current LDAP connection.
  104. *
  105. * @return Zend_Ldap
  106. */
  107. public function getLdap()
  108. {
  109. return $this->_ldap;
  110. }
  111. /**
  112. * Returns the number of items in current result
  113. * Implements Countable
  114. *
  115. * @return int
  116. */
  117. public function count()
  118. {
  119. return $this->_itemCount;
  120. }
  121. /**
  122. * Return the current result item
  123. * Implements Iterator
  124. *
  125. * @return array
  126. * @throws Zend_Ldap_Exception
  127. */
  128. public function current()
  129. {
  130. if (!is_resource($this->_current) || !is_string($this->_currentDn)) return null;
  131. $entry = array('dn' => $this->_currentDn);
  132. $ber_identifier = null;
  133. $name = @ldap_first_attribute($this->_ldap->getResource(), $this->_current,
  134. $ber_identifier);
  135. while ($name)
  136. {
  137. $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
  138. unset($data['count']);
  139. $entry[strtolower($name)] = $data;
  140. $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
  141. $ber_identifier);
  142. }
  143. ksort($entry, SORT_LOCALE_STRING);
  144. return $entry;
  145. }
  146. /**
  147. * Return the result item key
  148. * Implements Iterator
  149. *
  150. * @return int
  151. */
  152. public function key()
  153. {
  154. return $this->_currentDn;
  155. }
  156. /**
  157. * Move forward to next result item
  158. * Implements Iterator
  159. *
  160. * @throws Zend_Ldap_Exception
  161. */
  162. public function next()
  163. {
  164. if (!is_resource($this->_current)) return;
  165. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  166. /**
  167. * @see Zend_Ldap_Exception
  168. */
  169. require_once 'Zend/Ldap/Exception.php';
  170. if ($this->_current === false) {
  171. $msg = $this->_ldap->getLastError($code);
  172. if ($code === Zend_Ldap_Exception::LDAP_SIZELIMIT_EXCEEDED) {
  173. // we have reached the size limit enforced by the server
  174. return;
  175. } else if ($code > Zend_Ldap_Exception::LDAP_SUCCESS) {
  176. throw new Zend_Ldap_Exception($this->_ldap, 'getting next entry (' . $msg . ')');
  177. }
  178. }
  179. $this->_storeCurrentDn();
  180. }
  181. /**
  182. * Rewind the Iterator to the first result item
  183. * Implements Iterator
  184. *
  185. * @throws Zend_Ldap_Exception
  186. */
  187. public function rewind()
  188. {
  189. if (!is_resource($this->_resultId)) return;
  190. $this->_current = @ldap_first_entry($this->_ldap->getResource(), $this->_resultId);
  191. /**
  192. * @see Zend_Ldap_Exception
  193. */
  194. require_once 'Zend/Ldap/Exception.php';
  195. if ($this->_current === false &&
  196. $this->_ldap->getLastErrorCode() > Zend_Ldap_Exception::LDAP_SUCCESS) {
  197. throw new Zend_Ldap_Exception($this->_ldap, 'getting first entry');
  198. }
  199. $this->_storeCurrentDn();
  200. }
  201. /**
  202. * Stores the current DN
  203. *
  204. * @return void
  205. * @throws Zend_Ldap_Exception
  206. */
  207. protected function _storeCurrentDn()
  208. {
  209. if (is_resource($this->_current)) {
  210. $this->_currentDn = @ldap_get_dn($this->_ldap->getResource(), $this->_current);
  211. if ($this->_currentDn === false) {
  212. throw new Zend_Ldap_Exception($this->_ldap, 'getting dn');
  213. }
  214. } else {
  215. $this->_currentDn = null;
  216. }
  217. }
  218. /**
  219. * Check if there is a current result item
  220. * after calls to rewind() or next()
  221. * Implements Iterator
  222. *
  223. * @return boolean
  224. */
  225. public function valid()
  226. {
  227. return (is_resource($this->_current) && is_string($this->_currentDn));
  228. }
  229. }