PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Ldap/Collection/DefaultIterator.php

https://github.com/MontmereLimited/zf2
PHP | 310 lines | 156 code | 22 blank | 132 comment | 26 complexity | 7fc8e495cd0dee3f60cea77265d39cb3 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. */
  20. /**
  21. * @namespace
  22. */
  23. namespace Zend\Ldap\Collection;
  24. use Zend\Ldap;
  25. /**
  26. * Zend_Ldap_Collection_Iterator_Default is the default collection iterator implementation
  27. * using ext/ldap
  28. *
  29. * @uses Countable
  30. * @uses Iterator
  31. * @uses \Zend\Ldap\Exception
  32. * @category Zend
  33. * @package Zend_Ldap
  34. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. */
  37. class DefaultIterator implements \Iterator, \Countable
  38. {
  39. const ATTRIBUTE_TO_LOWER = 1;
  40. const ATTRIBUTE_TO_UPPER = 2;
  41. const ATTRIBUTE_NATIVE = 3;
  42. /**
  43. * LDAP Connection
  44. *
  45. * @var \Zend\Ldap\Ldap
  46. */
  47. protected $_ldap = null;
  48. /**
  49. * Result identifier resource
  50. *
  51. * @var resource
  52. */
  53. protected $_resultId = null;
  54. /**
  55. * Current result entry identifier
  56. *
  57. * @var resource
  58. */
  59. protected $_current = null;
  60. /**
  61. * Number of items in query result
  62. *
  63. * @var integer
  64. */
  65. protected $_itemCount = -1;
  66. /**
  67. * The method that will be applied to the attribute's names.
  68. *
  69. * @var integer|callback
  70. */
  71. protected $_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  72. /**
  73. * Constructor.
  74. *
  75. * @param \Zend\Ldap\Ldap $ldap
  76. * @param resource $resultId
  77. * @return void
  78. */
  79. public function __construct(Ldap\Ldap $ldap, $resultId)
  80. {
  81. $this->_ldap = $ldap;
  82. $this->_resultId = $resultId;
  83. $this->_itemCount = @ldap_count_entries($ldap->getResource(), $resultId);
  84. if ($this->_itemCount === false) {
  85. throw new Ldap\Exception($this->_ldap, 'counting entries');
  86. }
  87. }
  88. public function __destruct()
  89. {
  90. $this->close();
  91. }
  92. /**
  93. * Closes the current result set
  94. *
  95. * @return bool
  96. */
  97. public function close()
  98. {
  99. $isClosed = false;
  100. if (is_resource($this->_resultId)) {
  101. $isClosed = @ldap_free_result($this->_resultId);
  102. $this->_resultId = null;
  103. $this->_current = null;
  104. }
  105. return $isClosed;
  106. }
  107. /**
  108. * Gets the current LDAP connection.
  109. *
  110. * @return \Zend\Ldap\Ldap
  111. */
  112. public function getLDAP()
  113. {
  114. return $this->_ldap;
  115. }
  116. /**
  117. * Sets the attribute name treatment.
  118. *
  119. * Can either be one of the following constants
  120. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_LOWER
  121. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_UPPER
  122. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_NATIVE
  123. * or a valid callback accepting the attribute's name as it's only
  124. * argument and returning the new attribute's name.
  125. *
  126. * @param integer|callback $attributeNameTreatment
  127. * @return \Zend\Ldap\Collection\DefaultIterator Provides a fluent interface
  128. */
  129. public function setAttributeNameTreatment($attributeNameTreatment)
  130. {
  131. if (is_callable($attributeNameTreatment)) {
  132. if (is_string($attributeNameTreatment) && !function_exists($attributeNameTreatment)) {
  133. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  134. } else if (is_array($attributeNameTreatment) &&
  135. !method_exists($attributeNameTreatment[0], $attributeNameTreatment[1])) {
  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. throw new Ldap\Exception($this->_ldap, 'getting dn');
  232. }
  233. return $currentDn;
  234. } else {
  235. return null;
  236. }
  237. }
  238. /**
  239. * Move forward to next result item
  240. * Implements Iterator
  241. *
  242. * @throws \Zend\Ldap\Exception
  243. */
  244. public function next()
  245. {
  246. if (is_resource($this->_current) && $this->_itemCount > 0) {
  247. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  248. if ($this->_current === false) {
  249. $msg = $this->_ldap->getLastError($code);
  250. if ($code === Ldap\Exception::LDAP_SIZELIMIT_EXCEEDED) {
  251. // we have reached the size limit enforced by the server
  252. return;
  253. } else if ($code > Ldap\Exception::LDAP_SUCCESS) {
  254. throw new Ldap\Exception($this->_ldap, 'getting next entry (' . $msg . ')');
  255. }
  256. }
  257. } else {
  258. $this->_current = false;
  259. }
  260. }
  261. /**
  262. * Rewind the Iterator to the first result item
  263. * Implements Iterator
  264. *
  265. * @throws \Zend\Ldap\Exception
  266. */
  267. public function rewind()
  268. {
  269. if (is_resource($this->_resultId)) {
  270. $this->_current = @ldap_first_entry($this->_ldap->getResource(), $this->_resultId);
  271. if ($this->_current === false &&
  272. $this->_ldap->getLastErrorCode() > Ldap\Exception::LDAP_SUCCESS) {
  273. throw new Ldap\Exception($this->_ldap, 'getting first entry');
  274. }
  275. }
  276. }
  277. /**
  278. * Check if there is a current result item
  279. * after calls to rewind() or next()
  280. * Implements Iterator
  281. *
  282. * @return boolean
  283. */
  284. public function valid()
  285. {
  286. return (is_resource($this->_current));
  287. }
  288. }