PageRenderTime 33ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/sas05/zf-1.11.x-doctrine-2.x
PHP | 312 lines | 158 code | 21 blank | 133 comment | 25 complexity | b0e6e8e2f2b3a93a39281834b74b4fc6 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-2011 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 23775 2011-03-01 17:25:24Z ralph $
  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-2011 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. ) {
  134. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  135. } else {
  136. $this->_attributeNameTreatment = $attributeNameTreatment;
  137. }
  138. } else {
  139. $attributeNameTreatment = (int)$attributeNameTreatment;
  140. switch ($attributeNameTreatment) {
  141. case self::ATTRIBUTE_TO_LOWER:
  142. case self::ATTRIBUTE_TO_UPPER:
  143. case self::ATTRIBUTE_NATIVE:
  144. $this->_attributeNameTreatment = $attributeNameTreatment;
  145. break;
  146. default:
  147. $this->_attributeNameTreatment = self::ATTRIBUTE_TO_LOWER;
  148. break;
  149. }
  150. }
  151. return $this;
  152. }
  153. /**
  154. * Returns the currently set attribute name treatment
  155. *
  156. * @return integer|callback
  157. */
  158. public function getAttributeNameTreatment()
  159. {
  160. return $this->_attributeNameTreatment;
  161. }
  162. /**
  163. * Returns the number of items in current result
  164. * Implements Countable
  165. *
  166. * @return int
  167. */
  168. public function count()
  169. {
  170. return $this->_itemCount;
  171. }
  172. /**
  173. * Return the current result item
  174. * Implements Iterator
  175. *
  176. * @return array|null
  177. * @throws Zend_Ldap_Exception
  178. */
  179. public function current()
  180. {
  181. if (!is_resource($this->_current)) {
  182. $this->rewind();
  183. }
  184. if (!is_resource($this->_current)) {
  185. return null;
  186. }
  187. $entry = array('dn' => $this->key());
  188. $ber_identifier = null;
  189. $name = @ldap_first_attribute($this->_ldap->getResource(), $this->_current,
  190. $ber_identifier);
  191. while ($name) {
  192. $data = @ldap_get_values_len($this->_ldap->getResource(), $this->_current, $name);
  193. unset($data['count']);
  194. switch ($this->_attributeNameTreatment) {
  195. case self::ATTRIBUTE_TO_LOWER:
  196. $attrName = strtolower($name);
  197. break;
  198. case self::ATTRIBUTE_TO_UPPER:
  199. $attrName = strtoupper($name);
  200. break;
  201. case self::ATTRIBUTE_NATIVE:
  202. $attrName = $name;
  203. break;
  204. default:
  205. $attrName = call_user_func($this->_attributeNameTreatment, $name);
  206. break;
  207. }
  208. $entry[$attrName] = $data;
  209. $name = @ldap_next_attribute($this->_ldap->getResource(), $this->_current,
  210. $ber_identifier);
  211. }
  212. ksort($entry, SORT_LOCALE_STRING);
  213. return $entry;
  214. }
  215. /**
  216. * Return the result item key
  217. * Implements Iterator
  218. *
  219. * @return string|null
  220. */
  221. public function key()
  222. {
  223. if (!is_resource($this->_current)) {
  224. $this->rewind();
  225. }
  226. if (is_resource($this->_current)) {
  227. $currentDn = @ldap_get_dn($this->_ldap->getResource(), $this->_current);
  228. if ($currentDn === false) {
  229. /** @see Zend_Ldap_Exception */
  230. require_once 'Zend/Ldap/Exception.php';
  231. throw new Zend_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)) {
  247. $this->_current = @ldap_next_entry($this->_ldap->getResource(), $this->_current);
  248. /** @see Zend_Ldap_Exception */
  249. require_once 'Zend/Ldap/Exception.php';
  250. if ($this->_current === false) {
  251. $msg = $this->_ldap->getLastError($code);
  252. if ($code === Zend_Ldap_Exception::LDAP_SIZELIMIT_EXCEEDED) {
  253. // we have reached the size limit enforced by the server
  254. return;
  255. } else if ($code > Zend_Ldap_Exception::LDAP_SUCCESS) {
  256. throw new Zend_Ldap_Exception($this->_ldap, 'getting next entry (' . $msg . ')');
  257. }
  258. }
  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. /** @see Zend_Ldap_Exception */
  272. require_once 'Zend/Ldap/Exception.php';
  273. if ($this->_current === false &&
  274. $this->_ldap->getLastErrorCode() > Zend_Ldap_Exception::LDAP_SUCCESS
  275. ) {
  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. }