Default.php 6.5 KB

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