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