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

/lib/Zend/Ldap/Node.php

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 1101 lines | 443 code | 67 blank | 591 comment | 73 complexity | 8af1870e9797334c3df6e598a22f6afa MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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. * @subpackage Node
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Node.php 22662 2010-07-24 17:37:36Z mabe $
  21. */
  22. /**
  23. * @see Zend_Ldap
  24. */
  25. #require_once 'Zend/Ldap.php';
  26. /**
  27. * @see Zend_Ldap_Node_Abstract
  28. */
  29. #require_once 'Zend/Ldap/Node/Abstract.php';
  30. /**
  31. * Zend_Ldap_Node provides an object oriented view into a LDAP node.
  32. *
  33. * @category Zend
  34. * @package Zend_Ldap
  35. * @subpackage Node
  36. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. class Zend_Ldap_Node extends Zend_Ldap_Node_Abstract implements Iterator, RecursiveIterator
  40. {
  41. /**
  42. * Holds the node's new DN if node is renamed.
  43. *
  44. * @var Zend_Ldap_Dn
  45. */
  46. protected $_newDn;
  47. /**
  48. * Holds the node's orginal attributes (as loaded).
  49. *
  50. * @var array
  51. */
  52. protected $_originalData;
  53. /**
  54. * This node will be added
  55. *
  56. * @var boolean
  57. */
  58. protected $_new;
  59. /**
  60. * This node will be deleted
  61. *
  62. * @var boolean
  63. */
  64. protected $_delete;
  65. /**
  66. * Holds the connection to the LDAP server if in connected mode.
  67. *
  68. * @var Zend_Ldap
  69. */
  70. protected $_ldap;
  71. /**
  72. * Holds an array of the current node's children.
  73. *
  74. * @var array
  75. */
  76. protected $_children;
  77. /**
  78. * Controls iteration status
  79. *
  80. * @var boolean
  81. */
  82. private $_iteratorRewind = false;
  83. /**
  84. * Constructor.
  85. *
  86. * Constructor is protected to enforce the use of factory methods.
  87. *
  88. * @param Zend_Ldap_Dn $dn
  89. * @param array $data
  90. * @param boolean $fromDataSource
  91. * @param Zend_Ldap $ldap
  92. * @throws Zend_Ldap_Exception
  93. */
  94. protected function __construct(Zend_Ldap_Dn $dn, array $data, $fromDataSource, Zend_Ldap $ldap = null)
  95. {
  96. parent::__construct($dn, $data, $fromDataSource);
  97. if ($ldap !== null) $this->attachLdap($ldap);
  98. else $this->detachLdap();
  99. }
  100. /**
  101. * Serialization callback
  102. *
  103. * Only DN and attributes will be serialized.
  104. *
  105. * @return array
  106. */
  107. public function __sleep()
  108. {
  109. return array('_dn', '_currentData', '_newDn', '_originalData',
  110. '_new', '_delete', '_children');
  111. }
  112. /**
  113. * Deserialization callback
  114. *
  115. * Enforces a detached node.
  116. *
  117. * @return null
  118. */
  119. public function __wakeup()
  120. {
  121. $this->detachLdap();
  122. }
  123. /**
  124. * Gets the current LDAP connection.
  125. *
  126. * @return Zend_Ldap
  127. * @throws Zend_Ldap_Exception
  128. */
  129. public function getLdap()
  130. {
  131. if ($this->_ldap === null) {
  132. /**
  133. * @see Zend_Ldap_Exception
  134. */
  135. #require_once 'Zend/Ldap/Exception.php';
  136. throw new Zend_Ldap_Exception(null, 'No LDAP connection specified.', Zend_Ldap_Exception::LDAP_OTHER);
  137. }
  138. else return $this->_ldap;
  139. }
  140. /**
  141. * Attach node to an LDAP connection
  142. *
  143. * This is an offline method.
  144. *
  145. * @uses Zend_Ldap_Dn::isChildOf()
  146. * @param Zend_Ldap $ldap
  147. * @return Zend_Ldap_Node Provides a fluid interface
  148. * @throws Zend_Ldap_Exception
  149. */
  150. public function attachLdap(Zend_Ldap $ldap)
  151. {
  152. if (!Zend_Ldap_Dn::isChildOf($this->_getDn(), $ldap->getBaseDn())) {
  153. /**
  154. * @see Zend_Ldap_Exception
  155. */
  156. #require_once 'Zend/Ldap/Exception.php';
  157. throw new Zend_Ldap_Exception(null, 'LDAP connection is not responsible for given node.',
  158. Zend_Ldap_Exception::LDAP_OTHER);
  159. }
  160. if ($ldap !== $this->_ldap) {
  161. $this->_ldap = $ldap;
  162. if (is_array($this->_children)) {
  163. foreach ($this->_children as $child) {
  164. $child->attachLdap($ldap);
  165. }
  166. }
  167. }
  168. return $this;
  169. }
  170. /**
  171. * Detach node from LDAP connection
  172. *
  173. * This is an offline method.
  174. *
  175. * @return Zend_Ldap_Node Provides a fluid interface
  176. */
  177. public function detachLdap()
  178. {
  179. $this->_ldap = null;
  180. if (is_array($this->_children)) {
  181. foreach ($this->_children as $child) {
  182. $child->detachLdap();
  183. }
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Checks if the current node is attached to a LDAP server.
  189. *
  190. * This is an offline method.
  191. *
  192. * @return boolean
  193. */
  194. public function isAttached()
  195. {
  196. return ($this->_ldap !== null);
  197. }
  198. /**
  199. * @param array $data
  200. * @param boolean $fromDataSource
  201. * @throws Zend_Ldap_Exception
  202. */
  203. protected function _loadData(array $data, $fromDataSource)
  204. {
  205. parent::_loadData($data, $fromDataSource);
  206. if ($fromDataSource === true) {
  207. $this->_originalData = $data;
  208. } else {
  209. $this->_originalData = array();
  210. }
  211. $this->_children = null;
  212. $this->_markAsNew(($fromDataSource === true) ? false : true);
  213. $this->_markAsToBeDeleted(false);
  214. }
  215. /**
  216. * Factory method to create a new detached Zend_Ldap_Node for a given DN.
  217. *
  218. * @param string|array|Zend_Ldap_Dn $dn
  219. * @param array $objectClass
  220. * @return Zend_Ldap_Node
  221. * @throws Zend_Ldap_Exception
  222. */
  223. public static function create($dn, array $objectClass = array())
  224. {
  225. if (is_string($dn) || is_array($dn)) {
  226. $dn = Zend_Ldap_Dn::factory($dn);
  227. } else if ($dn instanceof Zend_Ldap_Dn) {
  228. $dn = clone $dn;
  229. } else {
  230. /**
  231. * @see Zend_Ldap_Exception
  232. */
  233. #require_once 'Zend/Ldap/Exception.php';
  234. throw new Zend_Ldap_Exception(null, '$dn is of a wrong data type.');
  235. }
  236. $new = new self($dn, array(), false, null);
  237. $new->_ensureRdnAttributeValues();
  238. $new->setAttribute('objectClass', $objectClass);
  239. return $new;
  240. }
  241. /**
  242. * Factory method to create an attached Zend_Ldap_Node for a given DN.
  243. *
  244. * @param string|array|Zend_Ldap_Dn $dn
  245. * @param Zend_Ldap $ldap
  246. * @return Zend_Ldap_Node|null
  247. * @throws Zend_Ldap_Exception
  248. */
  249. public static function fromLdap($dn, Zend_Ldap $ldap)
  250. {
  251. if (is_string($dn) || is_array($dn)) {
  252. $dn = Zend_Ldap_Dn::factory($dn);
  253. } else if ($dn instanceof Zend_Ldap_Dn) {
  254. $dn = clone $dn;
  255. } else {
  256. /**
  257. * @see Zend_Ldap_Exception
  258. */
  259. #require_once 'Zend/Ldap/Exception.php';
  260. throw new Zend_Ldap_Exception(null, '$dn is of a wrong data type.');
  261. }
  262. $data = $ldap->getEntry($dn, array('*', '+'), true);
  263. if ($data === null) {
  264. return null;
  265. }
  266. $entry = new self($dn, $data, true, $ldap);
  267. return $entry;
  268. }
  269. /**
  270. * Factory method to create a detached Zend_Ldap_Node from array data.
  271. *
  272. * @param array $data
  273. * @param boolean $fromDataSource
  274. * @return Zend_Ldap_Node
  275. * @throws Zend_Ldap_Exception
  276. */
  277. public static function fromArray(array $data, $fromDataSource = false)
  278. {
  279. if (!array_key_exists('dn', $data)) {
  280. /**
  281. * @see Zend_Ldap_Exception
  282. */
  283. #require_once 'Zend/Ldap/Exception.php';
  284. throw new Zend_Ldap_Exception(null, '\'dn\' key is missing in array.');
  285. }
  286. if (is_string($data['dn']) || is_array($data['dn'])) {
  287. $dn = Zend_Ldap_Dn::factory($data['dn']);
  288. } else if ($data['dn'] instanceof Zend_Ldap_Dn) {
  289. $dn = clone $data['dn'];
  290. } else {
  291. /**
  292. * @see Zend_Ldap_Exception
  293. */
  294. #require_once 'Zend/Ldap/Exception.php';
  295. throw new Zend_Ldap_Exception(null, '\'dn\' key is of a wrong data type.');
  296. }
  297. $fromDataSource = ($fromDataSource === true) ? true : false;
  298. $new = new self($dn, $data, $fromDataSource, null);
  299. $new->_ensureRdnAttributeValues();
  300. return $new;
  301. }
  302. /**
  303. * Ensures that teh RDN attributes are correctly set.
  304. *
  305. * @return void
  306. */
  307. protected function _ensureRdnAttributeValues()
  308. {
  309. foreach ($this->getRdnArray() as $key => $value) {
  310. Zend_Ldap_Attribute::setAttribute($this->_currentData, $key, $value, false);
  311. }
  312. }
  313. /**
  314. * Marks this node as new.
  315. *
  316. * Node will be added (instead of updated) on calling update() if $new is true.
  317. *
  318. * @param boolean $new
  319. */
  320. protected function _markAsNew($new)
  321. {
  322. $this->_new = ($new === false) ? false : true;
  323. }
  324. /**
  325. * Tells if the node is consiedered as new (not present on the server)
  326. *
  327. * Please note, that this doesn't tell you if the node is present on the server.
  328. * Use {@link exits()} to see if a node is already there.
  329. *
  330. * @return boolean
  331. */
  332. public function isNew()
  333. {
  334. return $this->_new;
  335. }
  336. /**
  337. * Marks this node as to be deleted.
  338. *
  339. * Node will be deleted on calling update() if $delete is true.
  340. *
  341. * @param boolean $delete
  342. */
  343. protected function _markAsToBeDeleted($delete)
  344. {
  345. $this->_delete = ($delete === true) ? true : false;
  346. }
  347. /**
  348. * Is this node going to be deleted once update() is called?
  349. *
  350. * @return boolean
  351. */
  352. public function willBeDeleted()
  353. {
  354. return $this->_delete;
  355. }
  356. /**
  357. * Marks this node as to be deleted
  358. *
  359. * Node will be deleted on calling update() if $delete is true.
  360. *
  361. * @return Zend_Ldap_Node Provides a fluid interface
  362. */
  363. public function delete()
  364. {
  365. $this->_markAsToBeDeleted(true);
  366. return $this;
  367. }
  368. /**
  369. * Is this node going to be moved once update() is called?
  370. *
  371. * @return boolean
  372. */
  373. public function willBeMoved()
  374. {
  375. if ($this->isNew() || $this->willBeDeleted()) {
  376. return false;
  377. } else if ($this->_newDn !== null) {
  378. return ($this->_dn != $this->_newDn);
  379. } else {
  380. return false;
  381. }
  382. }
  383. /**
  384. * Sends all pending changes to the LDAP server
  385. *
  386. * @param Zend_Ldap $ldap
  387. * @return Zend_Ldap_Node Provides a fluid interface
  388. * @throws Zend_Ldap_Exception
  389. */
  390. public function update(Zend_Ldap $ldap = null)
  391. {
  392. if ($ldap !== null) {
  393. $this->attachLdap($ldap);
  394. }
  395. $ldap = $this->getLdap();
  396. if (!($ldap instanceof Zend_Ldap)) {
  397. /**
  398. * @see Zend_Ldap_Exception
  399. */
  400. #require_once 'Zend/Ldap/Exception.php';
  401. throw new Zend_Ldap_Exception(null, 'No LDAP connection available');
  402. }
  403. if ($this->willBeDeleted()) {
  404. if ($ldap->exists($this->_dn)) {
  405. $ldap->delete($this->_dn);
  406. }
  407. return $this;
  408. }
  409. if ($this->isNew()) {
  410. $data = $this->getData();
  411. $ldap->add($this->_getDn(), $data);
  412. $this->_loadData($data, true);
  413. return $this;
  414. }
  415. $changedData = $this->getChangedData();
  416. if ($this->willBeMoved()) {
  417. $recursive = $this->hasChildren();
  418. $ldap->rename($this->_dn, $this->_newDn, $recursive, false);
  419. foreach ($this->_newDn->getRdn() as $key => $value) {
  420. if (array_key_exists($key, $changedData)) {
  421. unset($changedData[$key]);
  422. }
  423. }
  424. $this->_dn = $this->_newDn;
  425. $this->_newDn = null;
  426. }
  427. if (count($changedData) > 0) {
  428. $ldap->update($this->_getDn(), $changedData);
  429. }
  430. $this->_originalData = $this->_currentData;
  431. return $this;
  432. }
  433. /**
  434. * Gets the DN of the current node as a Zend_Ldap_Dn.
  435. *
  436. * This is an offline method.
  437. *
  438. * @return Zend_Ldap_Dn
  439. */
  440. protected function _getDn()
  441. {
  442. return ($this->_newDn === null) ? parent::_getDn() : $this->_newDn;
  443. }
  444. /**
  445. * Gets the current DN of the current node as a Zend_Ldap_Dn.
  446. * The method returns a clone of the node's DN to prohibit modification.
  447. *
  448. * This is an offline method.
  449. *
  450. * @return Zend_Ldap_Dn
  451. */
  452. public function getCurrentDn()
  453. {
  454. $dn = clone parent::_getDn();
  455. return $dn;
  456. }
  457. /**
  458. * Sets the new DN for this node
  459. *
  460. * This is an offline method.
  461. *
  462. * @param Zend_Ldap_Dn|string|array $newDn
  463. * @throws Zend_Ldap_Exception
  464. * @return Zend_Ldap_Node Provides a fluid interface
  465. */
  466. public function setDn($newDn)
  467. {
  468. if ($newDn instanceof Zend_Ldap_Dn) {
  469. $this->_newDn = clone $newDn;
  470. } else {
  471. $this->_newDn = Zend_Ldap_Dn::factory($newDn);
  472. }
  473. $this->_ensureRdnAttributeValues();
  474. return $this;
  475. }
  476. /**
  477. * {@see setDn()}
  478. *
  479. * This is an offline method.
  480. *
  481. * @param Zend_Ldap_Dn|string|array $newDn
  482. * @throws Zend_Ldap_Exception
  483. * @return Zend_Ldap_Node Provides a fluid interface
  484. */
  485. public function move($newDn)
  486. {
  487. return $this->setDn($newDn);
  488. }
  489. /**
  490. * {@see setDn()}
  491. *
  492. * This is an offline method.
  493. *
  494. * @param Zend_Ldap_Dn|string|array $newDn
  495. * @throws Zend_Ldap_Exception
  496. * @return Zend_Ldap_Node Provides a fluid interface
  497. */
  498. public function rename($newDn)
  499. {
  500. return $this->setDn($newDn);
  501. }
  502. /**
  503. * Sets the objectClass.
  504. *
  505. * This is an offline method.
  506. *
  507. * @param array|string $value
  508. * @return Zend_Ldap_Node Provides a fluid interface
  509. * @throws Zend_Ldap_Exception
  510. */
  511. public function setObjectClass($value)
  512. {
  513. $this->setAttribute('objectClass', $value);
  514. return $this;
  515. }
  516. /**
  517. * Appends to the objectClass.
  518. *
  519. * This is an offline method.
  520. *
  521. * @param array|string $value
  522. * @return Zend_Ldap_Node Provides a fluid interface
  523. * @throws Zend_Ldap_Exception
  524. */
  525. public function appendObjectClass($value)
  526. {
  527. $this->appendToAttribute('objectClass', $value);
  528. return $this;
  529. }
  530. /**
  531. * Returns a LDIF representation of the current node
  532. *
  533. * @param array $options Additional options used during encoding
  534. * @return string
  535. */
  536. public function toLdif(array $options = array())
  537. {
  538. $attributes = array_merge(array('dn' => $this->getDnString()), $this->getData(false));
  539. /**
  540. * Zend_Ldap_Ldif_Encoder
  541. */
  542. #require_once 'Zend/Ldap/Ldif/Encoder.php';
  543. return Zend_Ldap_Ldif_Encoder::encode($attributes, $options);
  544. }
  545. /**
  546. * Gets changed node data.
  547. *
  548. * The array contains all changed attributes.
  549. * This format can be used in {@link Zend_Ldap::add()} and {@link Zend_Ldap::update()}.
  550. *
  551. * This is an offline method.
  552. *
  553. * @return array
  554. */
  555. public function getChangedData()
  556. {
  557. $changed = array();
  558. foreach ($this->_currentData as $key => $value) {
  559. if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
  560. $changed[$key] = $value;
  561. } else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
  562. $changed[$key] = $value;
  563. }
  564. }
  565. return $changed;
  566. }
  567. /**
  568. * Returns all changes made.
  569. *
  570. * This is an offline method.
  571. *
  572. * @return array
  573. */
  574. public function getChanges()
  575. {
  576. $changes = array(
  577. 'add' => array(),
  578. 'delete' => array(),
  579. 'replace' => array());
  580. foreach ($this->_currentData as $key => $value) {
  581. if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
  582. $changes['add'][$key] = $value;
  583. } else if (count($this->_originalData[$key]) === 0 && !empty($value)) {
  584. $changes['add'][$key] = $value;
  585. } else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
  586. if (empty($value)) {
  587. $changes['delete'][$key] = $value;
  588. } else {
  589. $changes['replace'][$key] = $value;
  590. }
  591. }
  592. }
  593. return $changes;
  594. }
  595. /**
  596. * Sets a LDAP attribute.
  597. *
  598. * This is an offline method.
  599. *
  600. * @param string $name
  601. * @param mixed $value
  602. * @return Zend_Ldap_Node Provides a fluid interface
  603. * @throws Zend_Ldap_Exception
  604. */
  605. public function setAttribute($name, $value)
  606. {
  607. $this->_setAttribute($name, $value, false);
  608. return $this;
  609. }
  610. /**
  611. * Appends to a LDAP attribute.
  612. *
  613. * This is an offline method.
  614. *
  615. * @param string $name
  616. * @param mixed $value
  617. * @return Zend_Ldap_Node Provides a fluid interface
  618. * @throws Zend_Ldap_Exception
  619. */
  620. public function appendToAttribute($name, $value)
  621. {
  622. $this->_setAttribute($name, $value, true);
  623. return $this;
  624. }
  625. /**
  626. * Checks if the attribute can be set and sets it accordingly.
  627. *
  628. * @param string $name
  629. * @param mixed $value
  630. * @param boolean $append
  631. * @throws Zend_Ldap_Exception
  632. */
  633. protected function _setAttribute($name, $value, $append)
  634. {
  635. $this->_assertChangeableAttribute($name);
  636. Zend_Ldap_Attribute::setAttribute($this->_currentData, $name, $value, $append);
  637. }
  638. /**
  639. * Sets a LDAP date/time attribute.
  640. *
  641. * This is an offline method.
  642. *
  643. * @param string $name
  644. * @param integer|array $value
  645. * @param boolean $utc
  646. * @return Zend_Ldap_Node Provides a fluid interface
  647. * @throws Zend_Ldap_Exception
  648. */
  649. public function setDateTimeAttribute($name, $value, $utc = false)
  650. {
  651. $this->_setDateTimeAttribute($name, $value, $utc, false);
  652. return $this;
  653. }
  654. /**
  655. * Appends to a LDAP date/time attribute.
  656. *
  657. * This is an offline method.
  658. *
  659. * @param string $name
  660. * @param integer|array $value
  661. * @param boolean $utc
  662. * @return Zend_Ldap_Node Provides a fluid interface
  663. * @throws Zend_Ldap_Exception
  664. */
  665. public function appendToDateTimeAttribute($name, $value, $utc = false)
  666. {
  667. $this->_setDateTimeAttribute($name, $value, $utc, true);
  668. return $this;
  669. }
  670. /**
  671. * Checks if the attribute can be set and sets it accordingly.
  672. *
  673. * @param string $name
  674. * @param integer|array $value
  675. * @param boolean $utc
  676. * @param boolean $append
  677. * @throws Zend_Ldap_Exception
  678. */
  679. protected function _setDateTimeAttribute($name, $value, $utc, $append)
  680. {
  681. $this->_assertChangeableAttribute($name);
  682. Zend_Ldap_Attribute::setDateTimeAttribute($this->_currentData, $name, $value, $utc, $append);
  683. }
  684. /**
  685. * Sets a LDAP password.
  686. *
  687. * @param string $password
  688. * @param string $hashType
  689. * @param string $attribName
  690. * @return Zend_Ldap_Node Provides a fluid interface
  691. * @throws Zend_Ldap_Exception
  692. */
  693. public function setPasswordAttribute($password, $hashType = Zend_Ldap_Attribute::PASSWORD_HASH_MD5,
  694. $attribName = 'userPassword')
  695. {
  696. $this->_assertChangeableAttribute($attribName);
  697. Zend_Ldap_Attribute::setPassword($this->_currentData, $password, $hashType, $attribName);
  698. return $this;
  699. }
  700. /**
  701. * Deletes a LDAP attribute.
  702. *
  703. * This method deletes the attribute.
  704. *
  705. * This is an offline method.
  706. *
  707. * @param string $name
  708. * @return Zend_Ldap_Node Provides a fluid interface
  709. * @throws Zend_Ldap_Exception
  710. */
  711. public function deleteAttribute($name)
  712. {
  713. if ($this->existsAttribute($name, true)) {
  714. $this->_setAttribute($name, null, false);
  715. }
  716. return $this;
  717. }
  718. /**
  719. * Removes duplicate values from a LDAP attribute
  720. *
  721. * @param string $attribName
  722. * @return void
  723. */
  724. public function removeDuplicatesFromAttribute($attribName)
  725. {
  726. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($this->_currentData, $attribName);
  727. }
  728. /**
  729. * Remove given values from a LDAP attribute
  730. *
  731. * @param string $attribName
  732. * @param mixed|array $value
  733. * @return void
  734. */
  735. public function removeFromAttribute($attribName, $value)
  736. {
  737. Zend_Ldap_Attribute::removeFromAttribute($this->_currentData, $attribName, $value);
  738. }
  739. /**
  740. * @param string $name
  741. * @return boolean
  742. * @throws Zend_Ldap_Exception
  743. */
  744. protected function _assertChangeableAttribute($name)
  745. {
  746. $name = strtolower($name);
  747. $rdn = $this->getRdnArray(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  748. if ($name == 'dn') {
  749. /**
  750. * @see Zend_Ldap_Exception
  751. */
  752. #require_once 'Zend/Ldap/Exception.php';
  753. throw new Zend_Ldap_Exception(null, 'DN cannot be changed.');
  754. }
  755. else if (array_key_exists($name, $rdn)) {
  756. /**
  757. * @see Zend_Ldap_Exception
  758. */
  759. #require_once 'Zend/Ldap/Exception.php';
  760. throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s part of the RDN');
  761. } else if (in_array($name, self::$_systemAttributes)) {
  762. /**
  763. * @see Zend_Ldap_Exception
  764. */
  765. #require_once 'Zend/Ldap/Exception.php';
  766. throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s read-only');
  767. }
  768. else return true;
  769. }
  770. /**
  771. * Sets a LDAP attribute.
  772. *
  773. * This is an offline method.
  774. *
  775. * @param string $name
  776. * @param mixed $value
  777. * @return null
  778. * @throws Zend_Ldap_Exception
  779. */
  780. public function __set($name, $value)
  781. {
  782. $this->setAttribute($name, $value);
  783. }
  784. /**
  785. * Deletes a LDAP attribute.
  786. *
  787. * This method deletes the attribute.
  788. *
  789. * This is an offline method.
  790. *
  791. * @param string $name
  792. * @return null
  793. * @throws Zend_Ldap_Exception
  794. */
  795. public function __unset($name)
  796. {
  797. $this->deleteAttribute($name);
  798. }
  799. /**
  800. * Sets a LDAP attribute.
  801. * Implements ArrayAccess.
  802. *
  803. * This is an offline method.
  804. *
  805. * @param string $name
  806. * @param mixed $value
  807. * @return null
  808. * @throws Zend_Ldap_Exception
  809. */
  810. public function offsetSet($name, $value)
  811. {
  812. $this->setAttribute($name, $value);
  813. }
  814. /**
  815. * Deletes a LDAP attribute.
  816. * Implements ArrayAccess.
  817. *
  818. * This method deletes the attribute.
  819. *
  820. * This is an offline method.
  821. *
  822. * @param string $name
  823. * @return null
  824. * @throws Zend_Ldap_Exception
  825. */
  826. public function offsetUnset($name)
  827. {
  828. $this->deleteAttribute($name);
  829. }
  830. /**
  831. * Check if node exists on LDAP.
  832. *
  833. * This is an online method.
  834. *
  835. * @param Zend_Ldap $ldap
  836. * @return boolean
  837. * @throws Zend_Ldap_Exception
  838. */
  839. public function exists(Zend_Ldap $ldap = null)
  840. {
  841. if ($ldap !== null) {
  842. $this->attachLdap($ldap);
  843. }
  844. $ldap = $this->getLdap();
  845. return $ldap->exists($this->_getDn());
  846. }
  847. /**
  848. * Reload node attributes from LDAP.
  849. *
  850. * This is an online method.
  851. *
  852. * @param Zend_Ldap $ldap
  853. * @return Zend_Ldap_Node Provides a fluid interface
  854. * @throws Zend_Ldap_Exception
  855. */
  856. public function reload(Zend_Ldap $ldap = null)
  857. {
  858. if ($ldap !== null) {
  859. $this->attachLdap($ldap);
  860. }
  861. $ldap = $this->getLdap();
  862. parent::reload($ldap);
  863. return $this;
  864. }
  865. /**
  866. * Search current subtree with given options.
  867. *
  868. * This is an online method.
  869. *
  870. * @param string|Zend_Ldap_Filter_Abstract $filter
  871. * @param integer $scope
  872. * @param string $sort
  873. * @return Zend_Ldap_Node_Collection
  874. * @throws Zend_Ldap_Exception
  875. */
  876. public function searchSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB, $sort = null)
  877. {
  878. /**
  879. * @see Zend_Ldap_Node_Collection
  880. */
  881. #require_once 'Zend/Ldap/Node/Collection.php';
  882. return $this->getLdap()->search($filter, $this->_getDn(), $scope, array('*', '+'), $sort,
  883. 'Zend_Ldap_Node_Collection');
  884. }
  885. /**
  886. * Count items in current subtree found by given filter.
  887. *
  888. * This is an online method.
  889. *
  890. * @param string|Zend_Ldap_Filter_Abstract $filter
  891. * @param integer $scope
  892. * @return integer
  893. * @throws Zend_Ldap_Exception
  894. */
  895. public function countSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB)
  896. {
  897. return $this->getLdap()->count($filter, $this->_getDn(), $scope);
  898. }
  899. /**
  900. * Count children of current node.
  901. *
  902. * This is an online method.
  903. *
  904. * @return integer
  905. * @throws Zend_Ldap_Exception
  906. */
  907. public function countChildren()
  908. {
  909. return $this->countSubtree('(objectClass=*)', Zend_Ldap::SEARCH_SCOPE_ONE);
  910. }
  911. /**
  912. * Gets children of current node.
  913. *
  914. * This is an online method.
  915. *
  916. * @param string|Zend_Ldap_Filter_Abstract $filter
  917. * @param string $sort
  918. * @return Zend_Ldap_Node_Collection
  919. * @throws Zend_Ldap_Exception
  920. */
  921. public function searchChildren($filter, $sort = null)
  922. {
  923. return $this->searchSubtree($filter, Zend_Ldap::SEARCH_SCOPE_ONE, $sort);
  924. }
  925. /**
  926. * Checks if current node has children.
  927. * Returns whether the current element has children.
  928. *
  929. * Can be used offline but returns false if children have not been retrieved yet.
  930. *
  931. * @return boolean
  932. * @throws Zend_Ldap_Exception
  933. */
  934. public function hasChildren()
  935. {
  936. if (!is_array($this->_children)) {
  937. if ($this->isAttached()) {
  938. return ($this->countChildren() > 0);
  939. } else {
  940. return false;
  941. }
  942. } else {
  943. return (count($this->_children) > 0);
  944. }
  945. }
  946. /**
  947. * Returns the children for the current node.
  948. *
  949. * Can be used offline but returns an empty array if children have not been retrieved yet.
  950. *
  951. * @return Zend_Ldap_Node_ChildrenIterator
  952. * @throws Zend_Ldap_Exception
  953. */
  954. public function getChildren()
  955. {
  956. if (!is_array($this->_children)) {
  957. $this->_children = array();
  958. if ($this->isAttached()) {
  959. $children = $this->searchChildren('(objectClass=*)', null);
  960. foreach ($children as $child) {
  961. $this->_children[$child->getRdnString(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER)] = $child;
  962. }
  963. }
  964. }
  965. /**
  966. * @see Zend_Ldap_Node_ChildrenIterator
  967. */
  968. #require_once 'Zend/Ldap/Node/ChildrenIterator.php';
  969. return new Zend_Ldap_Node_ChildrenIterator($this->_children);
  970. }
  971. /**
  972. * Returns the parent of the current node.
  973. *
  974. * @param Zend_Ldap $ldap
  975. * @return Zend_Ldap_Node
  976. * @throws Zend_Ldap_Exception
  977. */
  978. public function getParent(Zend_Ldap $ldap = null)
  979. {
  980. if ($ldap !== null) {
  981. $this->attachLdap($ldap);
  982. }
  983. $ldap = $this->getLdap();
  984. $parentDn = $this->_getDn()->getParentDn(1);
  985. return self::fromLdap($parentDn, $ldap);
  986. }
  987. /**
  988. * Return the current attribute.
  989. * Implements Iterator
  990. *
  991. * @return array
  992. */
  993. public function current()
  994. {
  995. return $this;
  996. }
  997. /**
  998. * Return the attribute name.
  999. * Implements Iterator
  1000. *
  1001. * @return string
  1002. */
  1003. public function key()
  1004. {
  1005. return $this->getRdnString();
  1006. }
  1007. /**
  1008. * Move forward to next attribute.
  1009. * Implements Iterator
  1010. */
  1011. public function next()
  1012. {
  1013. $this->_iteratorRewind = false;
  1014. }
  1015. /**
  1016. * Rewind the Iterator to the first attribute.
  1017. * Implements Iterator
  1018. */
  1019. public function rewind()
  1020. {
  1021. $this->_iteratorRewind = true;
  1022. }
  1023. /**
  1024. * Check if there is a current attribute
  1025. * after calls to rewind() or next().
  1026. * Implements Iterator
  1027. *
  1028. * @return boolean
  1029. */
  1030. public function valid()
  1031. {
  1032. return $this->_iteratorRewind;
  1033. }
  1034. }