PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/simukti/zf1
PHP | 312 lines | 158 code | 21 blank | 133 comment | 26 complexity | d4f942dae6e4d56c8f8c0512e8dac35b 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. * @return void
  71. */
  72. public function __construct(Zend_Ldap $ldap, $resultId)
  73. {
  74. $this->_ldap = $ldap;
  75. $this->_resultId = $resultId;
  76. $this->_itemCount = @ldap_count_entries($ldap->getResource(), $resultId);
  77. if ($this->_itemCount === false) {
  78. /**
  79. * @see Zend_Ldap_Exception
  80. */
  81. require_once 'Zend/Ldap/Exception.php';
  82. throw new Zend_Ldap_Exception($this->_ldap, 'counting entries');
  83. }
  84. }
  85. public function __destruct()
  86. {
  87. $this->close();
  88. }
  89. /**
  90. * Closes the current result set
  91. *
  92. * @return bool
  93. */
  94. public function close()
  95. {
  96. $isClosed = false;
  97. if (is_resource($this->_resultId)) {
  98. $isClosed = @ldap_free_result($this->_resultId);
  99. $this->_resultId = null;
  100. $this->_current = null;
  101. }
  102. return $isClosed;
  103. }
  104. /**
  105. * Gets the current LDAP connection.
  106. *
  107. * @return Zend_Ldap
  108. */
  109. public function getLdap()
  110. {
  111. return $this->_ldap;
  112. }
  113. /**
  114. * Sets the attribute name treatment.
  115. *
  116. * Can either be one of the following constants
  117. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_LOWER
  118. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_TO_UPPER
  119. * - Zend_Ldap_Collection_Iterator_Default::ATTRIBUTE_NATIVE
  120. * or a valid callback accepting the attribute's name as it's only
  121. * argument and returning the new attribute's name.
  122. *
  123. * @param integer|callback $attributeNameTreatment
  124. * @return Zend_Ldap_Collection_Iterator_Default Provides a fluent interface
  125. */
  126. public function setAttributeNameTreatment($attributeNameTreatment)
  127. {
  128. if (is_callable($attributeNameTreatment)) {
  129. if (is_string($attributeNameTreatment) && !function_exists($attributeNameTreatment)) {
  130. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  131. } else if (is_array($attributeNameTreatment) &&
  132. !method_exists($attributeNameTreatment[0], $attributeNameTreatment[1])) {
  133. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  134. } else {
  135. $this->_attributeNameTreatment = $attributeNameTreatment;
  136. }
  137. } else {
  138. $attributeNameTreatment = (int)$attributeNameTreatment;
  139. switch ($attributeNameTreatment) {
  140. case self::ATTRIBUTE_TO_LOWER:
  141. case self::ATTRIBUTE_TO_UPPER:
  142. case self::ATTRIBUTE_NATIVE:
  143. $this->_attributeNameTreatment = $attributeNameTreatment;
  144. break;
  145. default:
  146. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  147. break;
  148. }
  149. }
  150. return $this;
  151. }
  152. /**
  153. * Returns the currently set attribute name treatment
  154. *
  155. * @return integer|callback
  156. */
  157. public function getAttributeNameTreatment()
  158. {
  159. return $this->_attributeNameTreatment;
  160. }
  161. /**
  162. * Returns the number of items in current result
  163. * Implements Countable
  164. *
  165. * @return int
  166. */
  167. public function count()
  168. {
  169. return $this->_itemCount;
  170. }
  171. /**
  172. * Return the current result item
  173. * Implements Iterator
  174. *
  175. * @return array|null
  176. * @throws Zend_Ldap_Exception
  177. */
  178. public function current()
  179. {
  180. if (!is_resource($this->_current)) {
  181. $this->rewind();
  182. }
  183. if (!is_resource($this->_current)) {
  184. return null;
  185. }
  186. $entry = array('dn' => $this->key());
  187. $ber_identifier = null;
  188. $name = @ldap_first_attribute($this->_ldap->getResource(), $this->_current,
  189. $ber_identifier);
  190. while ($name) {
  191. $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
  192. unset($data['count']);
  193. switch($this->_attributeNameTreatment) {
  194. case self::ATTRIBUTE_TO_LOWER:
  195. $attrName = strtolower($name);
  196. break;
  197. case self::ATTRIBUTE_TO_UPPER:
  198. $attrName = strtoupper($name);
  199. break;
  200. case self::ATTRIBUTE_NATIVE:
  201. $attrName = $name;
  202. break;
  203. default:
  204. $attrName = call_user_func($this->_attributeNameTreatment, $name);
  205. break;
  206. }
  207. $entry[$attrName] = $data;
  208. $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
  209. $ber_identifier);
  210. }
  211. ksort($entry, SORT_LOCALE_STRING);
  212. return $entry;
  213. }
  214. /**
  215. * Return the result item key
  216. * Implements Iterator
  217. *
  218. * @return string|null
  219. */
  220. public function key()
  221. {
  222. if (!is_resource($this->_current)) {
  223. $this->rewind();
  224. }
  225. if (is_resource($this->_current)) {
  226. $currentDn = @ldap_get_dn($this->_ldap->getResource(), $this->_current);
  227. if ($currentDn === false) {
  228. /** @see Zend_Ldap_Exception */
  229. require_once 'Zend/Ldap/Exception.php';
  230. throw new Zend_Ldap_Exception($this->_ldap, 'getting dn');
  231. }
  232. return $currentDn;
  233. } else {
  234. return null;
  235. }
  236. }
  237. /**
  238. * Move forward to next result item
  239. * Implements Iterator
  240. *
  241. * @throws Zend_Ldap_Exception
  242. */
  243. public function next()
  244. {
  245. if (is_resource($this->_current) && $this->_itemCount > 0) {
  246. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  247. /** @see Zend_Ldap_Exception */
  248. require_once 'Zend/Ldap/Exception.php';
  249. if ($this->_current === false) {
  250. $msg = $this->_ldap->getLastError($code);
  251. if ($code === Zend_Ldap_Exception::LDAP_SIZELIMIT_EXCEEDED) {
  252. // we have reached the size limit enforced by the server
  253. return;
  254. } else if ($code > Zend_Ldap_Exception::LDAP_SUCCESS) {
  255. throw new Zend_Ldap_Exception($this->_ldap, 'getting next entry (' . $msg . ')');
  256. }
  257. }
  258. } else {
  259. $this->_current = false;
  260. }
  261. }
  262. /**
  263. * Rewind the Iterator to the first result item
  264. * Implements Iterator
  265. *
  266. * @throws Zend_Ldap_Exception
  267. */
  268. public function rewind()
  269. {
  270. if (is_resource($this->_resultId)) {
  271. $this->_current = @ldap_first_entry($this->_ldap->getResource(), $this->_resultId);
  272. /** @see Zend_Ldap_Exception */
  273. require_once 'Zend/Ldap/Exception.php';
  274. if ($this->_current === false &&
  275. $this->_ldap->getLastErrorCode() > Zend_Ldap_Exception::LDAP_SUCCESS) {
  276. throw new Zend_Ldap_Exception($this->_ldap, 'getting first entry');
  277. }
  278. }
  279. }
  280. /**
  281. * Check if there is a current result item
  282. * after calls to rewind() or next()
  283. * Implements Iterator
  284. *
  285. * @return boolean
  286. */
  287. public function valid()
  288. {
  289. return (is_resource($this->_current));
  290. }
  291. }