/library/Zend/Ldap/Collection/Iterator/Default.php

https://bitbucket.org/Ebozavrik/test-application · PHP · 320 lines · 160 code · 25 blank · 135 comment · 27 complexity · fbf5ee0f0edf67527e11eab043b50803 MD5 · raw file

  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Default.php 24612 2012-01-21 14:42:30Z sgehrig $
  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-2012 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. const ATTRIBUTE_TO_LOWER = 1;
  33. const ATTRIBUTE_TO_UPPER = 2;
  34. const ATTRIBUTE_NATIVE = 3;
  35. /**
  36. * LDAP Connection
  37. *
  38. * @var Zend_Ldap
  39. */
  40. protected $_ldap = null;
  41. /**
  42. * Result identifier resource
  43. *
  44. * @var resource
  45. */
  46. protected $_resultId = null;
  47. /**
  48. * Current result entry identifier
  49. *
  50. * @var resource
  51. */
  52. protected $_current = null;
  53. /**
  54. * Number of items in query result
  55. *
  56. * @var integer
  57. */
  58. protected $_itemCount = -1;
  59. /**
  60. * The method that will be applied to the attribute's names.
  61. *
  62. * @var integer|callback
  63. */
  64. protected $_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  65. /**
  66. * Constructor.
  67. *
  68. * @param Zend_Ldap $ldap
  69. * @param resource $resultId
  70. *
  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->_current = null;
  102. }
  103. return $isClosed;
  104. }
  105. /**
  106. * Gets the current LDAP connection.
  107. *
  108. * @return Zend_Ldap
  109. */
  110. public function getLdap ()
  111. {
  112. return $this->_ldap;
  113. }
  114. /**
  115. * Sets the attribute name treatment.
  116. *
  117. * Can either be one of the following constants
  118. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_LOWER
  119. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_UPPER
  120. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_NATIVE
  121. * or a valid callback accepting the attribute's name as it's only
  122. * argument and returning the new attribute's name.
  123. *
  124. * @param integer|callback $attributeNameTreatment
  125. *
  126. * @return Zend_Ldap_Collection_Iterator_Default Provides a fluent interface
  127. */
  128. public function setAttributeNameTreatment ($attributeNameTreatment)
  129. {
  130. if (is_callable($attributeNameTreatment)) {
  131. if (is_string($attributeNameTreatment) && !function_exists($attributeNameTreatment)) {
  132. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  133. } else if (is_array($attributeNameTreatment) &&
  134. !method_exists($attributeNameTreatment[0], $attributeNameTreatment[1])
  135. ) {
  136. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  137. } else {
  138. $this->_attributeNameTreatment = $attributeNameTreatment;
  139. }
  140. } else {
  141. $attributeNameTreatment = (int)$attributeNameTreatment;
  142. switch ($attributeNameTreatment) {
  143. case self::ATTRIBUTE_TO_LOWER:
  144. case self::ATTRIBUTE_TO_UPPER:
  145. case self::ATTRIBUTE_NATIVE:
  146. $this->_attributeNameTreatment = $attributeNameTreatment;
  147. break;
  148. default:
  149. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  150. break;
  151. }
  152. }
  153. return $this;
  154. }
  155. /**
  156. * Returns the currently set attribute name treatment
  157. *
  158. * @return integer|callback
  159. */
  160. public function getAttributeNameTreatment ()
  161. {
  162. return $this->_attributeNameTreatment;
  163. }
  164. /**
  165. * Returns the number of items in current result
  166. * Implements Countable
  167. *
  168. * @return int
  169. */
  170. public function count ()
  171. {
  172. return $this->_itemCount;
  173. }
  174. /**
  175. * Return the current result item
  176. * Implements Iterator
  177. *
  178. * @return array|null
  179. * @throws Zend_Ldap_Exception
  180. */
  181. public function current ()
  182. {
  183. if (!is_resource($this->_current)) {
  184. $this->rewind();
  185. }
  186. if (!is_resource($this->_current)) {
  187. return null;
  188. }
  189. $entry = array( 'dn' => $this->key() );
  190. $ber_identifier = null;
  191. $name = @ldap_first_attribute($this->_ldap->getResource(), $this->_current,
  192. $ber_identifier);
  193. while ($name) {
  194. $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
  195. unset( $data['count'] );
  196. switch ($this->_attributeNameTreatment) {
  197. case self::ATTRIBUTE_TO_LOWER:
  198. $attrName = strtolower($name);
  199. break;
  200. case self::ATTRIBUTE_TO_UPPER:
  201. $attrName = strtoupper($name);
  202. break;
  203. case self::ATTRIBUTE_NATIVE:
  204. $attrName = $name;
  205. break;
  206. default:
  207. $attrName = call_user_func($this->_attributeNameTreatment, $name);
  208. break;
  209. }
  210. $entry[$attrName] = $data;
  211. $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
  212. $ber_identifier);
  213. }
  214. ksort($entry, SORT_LOCALE_STRING);
  215. return $entry;
  216. }
  217. /**
  218. * Return the result item key
  219. * Implements Iterator
  220. *
  221. * @return string|null
  222. */
  223. public function key ()
  224. {
  225. if (!is_resource($this->_current)) {
  226. $this->rewind();
  227. }
  228. if (is_resource($this->_current)) {
  229. $currentDn = @ldap_get_dn($this->_ldap->getResource(), $this->_current);
  230. if ($currentDn === false) {
  231. /** @see Zend_Ldap_Exception */
  232. require_once 'Zend/Ldap/Exception.php';
  233. throw new Zend_Ldap_Exception( $this->_ldap, 'getting dn' );
  234. }
  235. return $currentDn;
  236. } else {
  237. return null;
  238. }
  239. }
  240. /**
  241. * Move forward to next result item
  242. * Implements Iterator
  243. *
  244. * @throws Zend_Ldap_Exception
  245. */
  246. public function next ()
  247. {
  248. if (is_resource($this->_current) && $this->_itemCount > 0) {
  249. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  250. /** @see Zend_Ldap_Exception */
  251. require_once 'Zend/Ldap/Exception.php';
  252. if ($this->_current === false) {
  253. $msg = $this->_ldap->getLastError($code);
  254. if ($code === Zend_Ldap_Exception::LDAP_SIZELIMIT_EXCEEDED) {
  255. // we have reached the size limit enforced by the server
  256. return;
  257. } else if ($code > Zend_Ldap_Exception::LDAP_SUCCESS) {
  258. throw new Zend_Ldap_Exception( $this->_ldap, 'getting next entry (' . $msg . ')' );
  259. }
  260. }
  261. } else {
  262. $this->_current = false;
  263. }
  264. }
  265. /**
  266. * Rewind the Iterator to the first result item
  267. * Implements Iterator
  268. *
  269. * @throws Zend_Ldap_Exception
  270. */
  271. public function rewind ()
  272. {
  273. if (is_resource($this->_resultId)) {
  274. $this->_current = @ldap_first_entry($this->_ldap->getResource(), $this->_resultId);
  275. /** @see Zend_Ldap_Exception */
  276. require_once 'Zend/Ldap/Exception.php';
  277. if ($this->_current === false &&
  278. $this->_ldap->getLastErrorCode() > Zend_Ldap_Exception::LDAP_SUCCESS
  279. ) {
  280. throw new Zend_Ldap_Exception( $this->_ldap, 'getting first entry' );
  281. }
  282. }
  283. }
  284. /**
  285. * Check if there is a current result item
  286. * after calls to rewind() or next()
  287. * Implements Iterator
  288. *
  289. * @return boolean
  290. */
  291. public function valid ()
  292. {
  293. return ( is_resource($this->_current) );
  294. }
  295. }