Node.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  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-2008 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 (!is_null($ldap)) $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 (is_null($this->_ldap)) {
  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 (!is_null($this->_ldap));
  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
  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. $entry = new self($dn, $data, true, $ldap);
  264. return $entry;
  265. }
  266. /**
  267. * Factory method to create a detached Zend_Ldap_Node from array data.
  268. *
  269. * @param array $data
  270. * @param boolean $fromDataSource
  271. * @return Zend_Ldap_Node
  272. * @throws Zend_Ldap_Exception
  273. */
  274. public static function fromArray(array $data, $fromDataSource = false)
  275. {
  276. if (!array_key_exists('dn', $data)) {
  277. /**
  278. * @see Zend_Ldap_Exception
  279. */
  280. require_once 'Zend/Ldap/Exception.php';
  281. throw new Zend_Ldap_Exception(null, '\'dn\' key is missing in array.');
  282. }
  283. if (is_string($data['dn']) || is_array($data['dn'])) {
  284. $dn = Zend_Ldap_Dn::factory($data['dn']);
  285. } else if ($data['dn'] instanceof Zend_Ldap_Dn) {
  286. $dn = clone $data['dn'];
  287. } else {
  288. /**
  289. * @see Zend_Ldap_Exception
  290. */
  291. require_once 'Zend/Ldap/Exception.php';
  292. throw new Zend_Ldap_Exception(null, '\'dn\' key is of a wrong data type.');
  293. }
  294. $fromDataSource = ($fromDataSource === true) ? true : false;
  295. $new = new self($dn, $data, $fromDataSource, null);
  296. $new->_ensureRdnAttributeValues();
  297. return $new;
  298. }
  299. /**
  300. * Ensures that teh RDN attributes are correctly set.
  301. *
  302. * @return void
  303. */
  304. protected function _ensureRdnAttributeValues()
  305. {
  306. foreach ($this->getRdnArray() as $key => $value) {
  307. Zend_Ldap_Attribute::setAttribute($this->_currentData, $key, $value, false);
  308. }
  309. }
  310. /**
  311. * Marks this node as new.
  312. *
  313. * Node will be added (instead of updated) on calling update() if $new is true.
  314. *
  315. * @param boolean $new
  316. */
  317. protected function _markAsNew($new)
  318. {
  319. $this->_new = ($new === false) ? false : true;
  320. }
  321. /**
  322. * Tells if the node is consiedered as new (not present on the server)
  323. *
  324. * Please note, that this doesn't tell you if the node is present on the server.
  325. * Use {@link exits()} to see if a node is already there.
  326. *
  327. * @return boolean
  328. */
  329. public function isNew()
  330. {
  331. return $this->_new;
  332. }
  333. /**
  334. * Marks this node as to be deleted.
  335. *
  336. * Node will be deleted on calling update() if $delete is true.
  337. *
  338. * @param boolean $delete
  339. */
  340. protected function _markAsToBeDeleted($delete)
  341. {
  342. $this->_delete = ($delete === true) ? true : false;
  343. }
  344. /**
  345. * Is this node going to be deleted once update() is called?
  346. *
  347. * @return boolean
  348. */
  349. public function willBeDeleted()
  350. {
  351. return $this->_delete;
  352. }
  353. /**
  354. * Marks this node as to be deleted
  355. *
  356. * Node will be deleted on calling update() if $delete is true.
  357. *
  358. * @return Zend_Ldap_Node Provides a fluid interface
  359. */
  360. public function delete()
  361. {
  362. $this->_markAsToBeDeleted(true);
  363. return $this;
  364. }
  365. /**
  366. * Is this node going to be moved once update() is called?
  367. *
  368. * @return boolean
  369. */
  370. public function willBeMoved()
  371. {
  372. if ($this->isNew() || $this->willBeDeleted()) {
  373. return false;
  374. } else if ($this->_newDn !== null) {
  375. return ($this->_dn != $this->_newDn);
  376. } else {
  377. return false;
  378. }
  379. }
  380. /**
  381. * Sends all pending changes to the LDAP server
  382. *
  383. * @param Zend_Ldap $ldap
  384. * @return Zend_Ldap_Node Provides a fluid interface
  385. * @throws Zend_Ldap_Exception
  386. */
  387. public function update(Zend_Ldap $ldap = null)
  388. {
  389. if ($ldap !== null) {
  390. $this->attachLdap($ldap);
  391. }
  392. $ldap = $this->getLdap();
  393. if (!($ldap instanceof Zend_Ldap)) {
  394. /**
  395. * @see Zend_Ldap_Exception
  396. */
  397. require_once 'Zend/Ldap/Exception.php';
  398. throw new Zend_Ldap_Exception(null, 'No LDAP connection available');
  399. }
  400. if ($this->willBeDeleted()) {
  401. if ($ldap->exists($this->_dn)) {
  402. $ldap->delete($this->_dn);
  403. }
  404. return $this;
  405. }
  406. if ($this->isNew()) {
  407. $data = $this->getData();
  408. $ldap->add($this->_getDn(), $data);
  409. $this->_loadData($data, true);
  410. return $this;
  411. }
  412. $changedData = $this->getChangedData();
  413. if ($this->willBeMoved()) {
  414. $recursive = $this->hasChildren();
  415. $ldap->rename($this->_dn, $this->_newDn, $recursive, false);
  416. foreach ($this->_newDn->getRdn() as $key => $value) {
  417. if (array_key_exists($key, $changedData)) {
  418. unset($changedData[$key]);
  419. }
  420. }
  421. $this->_dn = $this->_newDn;
  422. $this->_newDn = null;
  423. }
  424. if (count($changedData) > 0) {
  425. $ldap->update($this->_getDn(), $changedData);
  426. }
  427. $this->_originalData = $this->_currentData;
  428. return $this;
  429. }
  430. /**
  431. * Gets the DN of the current node as a Zend_Ldap_Dn.
  432. *
  433. * This is an offline method.
  434. *
  435. * @return Zend_Ldap_Dn
  436. */
  437. protected function _getDn()
  438. {
  439. return ($this->_newDn === null) ? parent::_getDn() : $this->_newDn;
  440. }
  441. /**
  442. * Gets the current DN of the current node as a Zend_Ldap_Dn.
  443. * The method returns a clone of the node's DN to prohibit modification.
  444. *
  445. * This is an offline method.
  446. *
  447. * @return Zend_Ldap_Dn
  448. */
  449. public function getCurrentDn()
  450. {
  451. $dn = clone parent::_getDn();
  452. return $dn;
  453. }
  454. /**
  455. * Sets the new DN for this node
  456. *
  457. * This is an offline method.
  458. *
  459. * @param Zend_Ldap_Dn|string|array $newDn
  460. * @throws Zend_Ldap_Exception
  461. * @return Zend_Ldap_Node Provides a fluid interface
  462. */
  463. public function setDn($newDn)
  464. {
  465. if ($newDn instanceof Zend_Ldap_Dn) {
  466. $this->_newDn = clone $newDn;
  467. } else {
  468. $this->_newDn = Zend_Ldap_Dn::factory($newDn);
  469. }
  470. $this->_ensureRdnAttributeValues();
  471. return $this;
  472. }
  473. /**
  474. * {@see setDn()}
  475. *
  476. * This is an offline method.
  477. *
  478. * @param Zend_Ldap_Dn|string|array $newDn
  479. * @throws Zend_Ldap_Exception
  480. * @return Zend_Ldap_Node Provides a fluid interface
  481. */
  482. public function move($newDn)
  483. {
  484. return $this->setDn($newDn);
  485. }
  486. /**
  487. * {@see setDn()}
  488. *
  489. * This is an offline method.
  490. *
  491. * @param Zend_Ldap_Dn|string|array $newDn
  492. * @throws Zend_Ldap_Exception
  493. * @return Zend_Ldap_Node Provides a fluid interface
  494. */
  495. public function rename($newDn)
  496. {
  497. return $this->setDn($newDn);
  498. }
  499. /**
  500. * Sets the objectClass.
  501. *
  502. * This is an offline method.
  503. *
  504. * @param array|string $value
  505. * @return Zend_Ldap_Node Provides a fluid interface
  506. * @throws Zend_Ldap_Exception
  507. */
  508. public function setObjectClass($value)
  509. {
  510. $this->setAttribute('objectClass', $value);
  511. return $this;
  512. }
  513. /**
  514. * Appends to the objectClass.
  515. *
  516. * This is an offline method.
  517. *
  518. * @param array|string $value
  519. * @return Zend_Ldap_Node Provides a fluid interface
  520. * @throws Zend_Ldap_Exception
  521. */
  522. public function appendObjectClass($value)
  523. {
  524. $this->appendToAttribute('objectClass', $value);
  525. return $this;
  526. }
  527. /**
  528. * Returns a LDIF representation of the current node
  529. *
  530. * @param array $options Additional options used during encoding
  531. * @return string
  532. */
  533. public function toLdif(array $options = array())
  534. {
  535. $attributes = array_merge(array('dn' => $this->getDnString()), $this->getData(false));
  536. /**
  537. * Zend_Ldap_Ldif_Encoder
  538. */
  539. require_once 'Zend/Ldap/Ldif/Encoder.php';
  540. return Zend_Ldap_Ldif_Encoder::encode($attributes, $options);
  541. }
  542. /**
  543. * Gets changed node data.
  544. *
  545. * The array contains all changed attributes.
  546. * This format can be used in {@link Zend_Ldap::add()} and {@link Zend_Ldap::update()}.
  547. *
  548. * This is an offline method.
  549. *
  550. * @return array
  551. */
  552. public function getChangedData()
  553. {
  554. $changed = array();
  555. foreach ($this->_currentData as $key => $value) {
  556. if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
  557. $changed[$key] = $value;
  558. } else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
  559. $changed[$key] = $value;
  560. }
  561. }
  562. return $changed;
  563. }
  564. /**
  565. * Returns all changes made.
  566. *
  567. * This is an offline method.
  568. *
  569. * @return array
  570. */
  571. public function getChanges()
  572. {
  573. $changes = array(
  574. 'add' => array(),
  575. 'delete' => array(),
  576. 'replace' => array());
  577. foreach ($this->_currentData as $key => $value) {
  578. if (!array_key_exists($key, $this->_originalData) && !empty($value)) {
  579. $changes['add'][$key] = $value;
  580. } else if (count($this->_originalData[$key]) === 0 && !empty($value)) {
  581. $changes['add'][$key] = $value;
  582. } else if ($this->_originalData[$key] !== $this->_currentData[$key]) {
  583. if (empty($value)) {
  584. $changes['delete'][$key] = $value;
  585. } else {
  586. $changes['replace'][$key] = $value;
  587. }
  588. }
  589. }
  590. return $changes;
  591. }
  592. /**
  593. * Sets a LDAP attribute.
  594. *
  595. * This is an offline method.
  596. *
  597. * @param string $name
  598. * @param mixed $value
  599. * @return Zend_Ldap_Node Provides a fluid interface
  600. * @throws Zend_Ldap_Exception
  601. */
  602. public function setAttribute($name, $value)
  603. {
  604. $this->_setAttribute($name, $value, false);
  605. return $this;
  606. }
  607. /**
  608. * Appends to a LDAP attribute.
  609. *
  610. * This is an offline method.
  611. *
  612. * @param string $name
  613. * @param mixed $value
  614. * @return Zend_Ldap_Node Provides a fluid interface
  615. * @throws Zend_Ldap_Exception
  616. */
  617. public function appendToAttribute($name, $value)
  618. {
  619. $this->_setAttribute($name, $value, true);
  620. return $this;
  621. }
  622. /**
  623. * Checks if the attribute can be set and sets it accordingly.
  624. *
  625. * @param string $name
  626. * @param mixed $value
  627. * @param boolean $append
  628. * @throws Zend_Ldap_Exception
  629. */
  630. protected function _setAttribute($name, $value, $append)
  631. {
  632. $this->_assertChangeableAttribute($name);
  633. Zend_Ldap_Attribute::setAttribute($this->_currentData, $name, $value, $append);
  634. }
  635. /**
  636. * Sets a LDAP date/time attribute.
  637. *
  638. * This is an offline method.
  639. *
  640. * @param string $name
  641. * @param integer|array $value
  642. * @param boolean $utc
  643. * @return Zend_Ldap_Node Provides a fluid interface
  644. * @throws Zend_Ldap_Exception
  645. */
  646. public function setDateTimeAttribute($name, $value, $utc = false)
  647. {
  648. $this->_setDateTimeAttribute($name, $value, $utc, false);
  649. return $this;
  650. }
  651. /**
  652. * Appends to a LDAP date/time attribute.
  653. *
  654. * This is an offline method.
  655. *
  656. * @param string $name
  657. * @param integer|array $value
  658. * @param boolean $utc
  659. * @return Zend_Ldap_Node Provides a fluid interface
  660. * @throws Zend_Ldap_Exception
  661. */
  662. public function appendToDateTimeAttribute($name, $value, $utc = false)
  663. {
  664. $this->_setDateTimeAttribute($name, $value, $utc, true);
  665. return $this;
  666. }
  667. /**
  668. * Checks if the attribute can be set and sets it accordingly.
  669. *
  670. * @param string $name
  671. * @param integer|array $value
  672. * @param boolean $utc
  673. * @param boolean $append
  674. * @throws Zend_Ldap_Exception
  675. */
  676. protected function _setDateTimeAttribute($name, $value, $utc, $append)
  677. {
  678. $this->_assertChangeableAttribute($name);
  679. Zend_Ldap_Attribute::setDateTimeAttribute($this->_currentData, $name, $value, $utc, $append);
  680. }
  681. /**
  682. * Sets a LDAP password.
  683. *
  684. * @param string $password
  685. * @param string $hashType
  686. * @param string $attribName
  687. * @return Zend_Ldap_Node Provides a fluid interface
  688. * @throws Zend_Ldap_Exception
  689. */
  690. public function setPasswordAttribute($password, $hashType = Zend_Ldap_Attribute::PASSWORD_HASH_MD5,
  691. $attribName = 'userPassword')
  692. {
  693. $this->_assertChangeableAttribute($attribName);
  694. Zend_Ldap_Attribute::setPassword($this->_currentData, $password, $hashType, $attribName);
  695. return $this;
  696. }
  697. /**
  698. * Deletes a LDAP attribute.
  699. *
  700. * This method deletes the attribute.
  701. *
  702. * This is an offline method.
  703. *
  704. * @param string $name
  705. * @return Zend_Ldap_Node Provides a fluid interface
  706. * @throws Zend_Ldap_Exception
  707. */
  708. public function deleteAttribute($name)
  709. {
  710. if ($this->existsAttribute($name, true)) {
  711. $this->_setAttribute($name, null, false);
  712. }
  713. return $this;
  714. }
  715. /**
  716. * Removes duplicate values from a LDAP attribute
  717. *
  718. * @param string $attribName
  719. * @return void
  720. */
  721. public function removeDuplicatesFromAttribute($attribName)
  722. {
  723. Zend_Ldap_Attribute::removeDuplicatesFromAttribute($this->_currentData, $attribName);
  724. }
  725. /**
  726. * Remove given values from a LDAP attribute
  727. *
  728. * @param string $attribName
  729. * @param mixed|array $value
  730. * @return void
  731. */
  732. public function removeFromAttribute($attribName, $value)
  733. {
  734. Zend_Ldap_Attribute::removeFromAttribute($this->_currentData, $attribName, $value);
  735. }
  736. /**
  737. * @param string $name
  738. * @return boolean
  739. * @throws Zend_Ldap_Exception
  740. */
  741. protected function _assertChangeableAttribute($name)
  742. {
  743. $name = strtolower($name);
  744. $rdn = $this->getRdnArray(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  745. if ($name == 'dn') {
  746. /**
  747. * @see Zend_Ldap_Exception
  748. */
  749. require_once 'Zend/Ldap/Exception.php';
  750. throw new Zend_Ldap_Exception(null, 'DN cannot be changed.');
  751. }
  752. else if (array_key_exists($name, $rdn)) {
  753. /**
  754. * @see Zend_Ldap_Exception
  755. */
  756. require_once 'Zend/Ldap/Exception.php';
  757. throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s part of the RDN');
  758. } else if (in_array($name, self::$_systemAttributes)) {
  759. /**
  760. * @see Zend_Ldap_Exception
  761. */
  762. require_once 'Zend/Ldap/Exception.php';
  763. throw new Zend_Ldap_Exception(null, 'Cannot change attribute because it\'s read-only');
  764. }
  765. else return true;
  766. }
  767. /**
  768. * Sets a LDAP attribute.
  769. *
  770. * This is an offline method.
  771. *
  772. * @param string $name
  773. * @param mixed $value
  774. * @return null
  775. * @throws Zend_Ldap_Exception
  776. */
  777. public function __set($name, $value)
  778. {
  779. $this->setAttribute($name, $value);
  780. }
  781. /**
  782. * Deletes a LDAP attribute.
  783. *
  784. * This method deletes the attribute.
  785. *
  786. * This is an offline method.
  787. *
  788. * @param string $name
  789. * @return null
  790. * @throws Zend_Ldap_Exception
  791. */
  792. public function __unset($name)
  793. {
  794. $this->deleteAttribute($name);
  795. }
  796. /**
  797. * Sets a LDAP attribute.
  798. * Implements ArrayAccess.
  799. *
  800. * This is an offline method.
  801. *
  802. * @param string $name
  803. * @param mixed $value
  804. * @return null
  805. * @throws Zend_Ldap_Exception
  806. */
  807. public function offsetSet($name, $value)
  808. {
  809. $this->setAttribute($name, $value);
  810. }
  811. /**
  812. * Deletes a LDAP attribute.
  813. * Implements ArrayAccess.
  814. *
  815. * This method deletes the attribute.
  816. *
  817. * This is an offline method.
  818. *
  819. * @param string $name
  820. * @return null
  821. * @throws Zend_Ldap_Exception
  822. */
  823. public function offsetUnset($name)
  824. {
  825. $this->deleteAttribute($name);
  826. }
  827. /**
  828. * Check if node exists on LDAP.
  829. *
  830. * This is an online method.
  831. *
  832. * @param Zend_Ldap $ldap
  833. * @return boolean
  834. * @throws Zend_Ldap_Exception
  835. */
  836. public function exists(Zend_Ldap $ldap = null)
  837. {
  838. if ($ldap !== null) {
  839. $this->attachLdap($ldap);
  840. }
  841. $ldap = $this->getLdap();
  842. return $ldap->exists($this->_getDn());
  843. }
  844. /**
  845. * Reload node attributes from LDAP.
  846. *
  847. * This is an online method.
  848. *
  849. * @param Zend_Ldap $ldap
  850. * @return Zend_Ldap_Node Provides a fluid interface
  851. * @throws Zend_Ldap_Exception
  852. */
  853. public function reload(Zend_Ldap $ldap = null)
  854. {
  855. if ($ldap !== null) {
  856. $this->attachLdap($ldap);
  857. }
  858. $ldap = $this->getLdap();
  859. parent::reload($ldap);
  860. return $this;
  861. }
  862. /**
  863. * Search current subtree with given options.
  864. *
  865. * This is an online method.
  866. *
  867. * @param string|Zend_Ldap_Filter_Abstract $filter
  868. * @param integer $scope
  869. * @param string $sort
  870. * @return Zend_Ldap_Node_Collection
  871. * @throws Zend_Ldap_Exception
  872. */
  873. public function searchSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB, $sort = null)
  874. {
  875. /**
  876. * @see Zend_Ldap_Node_Collection
  877. */
  878. require_once 'Zend/Ldap/Node/Collection.php';
  879. return $this->getLdap()->search($filter, $this->_getDn(), $scope, array('*', '+'), $sort,
  880. 'Zend_Ldap_Node_Collection');
  881. }
  882. /**
  883. * Count items in current subtree found by given filter.
  884. *
  885. * This is an online method.
  886. *
  887. * @param string|Zend_Ldap_Filter_Abstract $filter
  888. * @param integer $scope
  889. * @return integer
  890. * @throws Zend_Ldap_Exception
  891. */
  892. public function countSubtree($filter, $scope = Zend_Ldap::SEARCH_SCOPE_SUB)
  893. {
  894. return $this->getLdap()->count($filter, $this->_getDn(), $scope);
  895. }
  896. /**
  897. * Count children of current node.
  898. *
  899. * This is an online method.
  900. *
  901. * @return integer
  902. * @throws Zend_Ldap_Exception
  903. */
  904. public function countChildren()
  905. {
  906. return $this->countSubtree('(objectClass=*)', Zend_Ldap::SEARCH_SCOPE_ONE);
  907. }
  908. /**
  909. * Gets children of current node.
  910. *
  911. * This is an online method.
  912. *
  913. * @param string|Zend_Ldap_Filter_Abstract $filter
  914. * @param string $sort
  915. * @return Zend_Ldap_Node_Collection
  916. * @throws Zend_Ldap_Exception
  917. */
  918. public function searchChildren($filter, $sort = null)
  919. {
  920. return $this->searchSubtree($filter, Zend_Ldap::SEARCH_SCOPE_ONE, $sort);
  921. }
  922. /**
  923. * Checks if current node has children.
  924. * Returns whether the current element has children.
  925. *
  926. * Can be used offline but returns false if children have not been retrieved yet.
  927. *
  928. * @return boolean
  929. * @throws Zend_Ldap_Exception
  930. */
  931. public function hasChildren()
  932. {
  933. if (!is_array($this->_children)) {
  934. if ($this->isAttached()) {
  935. return ($this->countChildren() > 0);
  936. } else {
  937. return false;
  938. }
  939. } else {
  940. return (count($this->_children) > 0);
  941. }
  942. }
  943. /**
  944. * Returns the children for the current node.
  945. *
  946. * Can be used offline but returns an empty array if children have not been retrieved yet.
  947. *
  948. * @return Zend_Ldap_Node_ChildrenIterator
  949. * @throws Zend_Ldap_Exception
  950. */
  951. public function getChildren()
  952. {
  953. if (!is_array($this->_children)) {
  954. $this->_children = array();
  955. if ($this->isAttached()) {
  956. $children = $this->searchChildren('(objectClass=*)', null);
  957. foreach ($children as $child) {
  958. $this->_children[$child->getRdnString(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER)] = $child;
  959. }
  960. }
  961. }
  962. /**
  963. * @see Zend_Ldap_Node_ChildrenIterator
  964. */
  965. require_once 'Zend/Ldap/Node/ChildrenIterator.php';
  966. return new Zend_Ldap_Node_ChildrenIterator($this->_children);
  967. }
  968. /**
  969. * Returns the parent of the current node.
  970. *
  971. * @param Zend_Ldap $ldap
  972. * @return Zend_Ldap_Node
  973. * @throws Zend_Ldap_Exception
  974. */
  975. public function getParent(Zend_Ldap $ldap = null)
  976. {
  977. if ($ldap !== null) {
  978. $this->attachLdap($ldap);
  979. }
  980. $ldap = $this->getLdap();
  981. $parentDn = $this->_getDn()->getParentDn(1);
  982. return self::fromLdap($parentDn, $ldap);
  983. }
  984. /**
  985. * Return the current attribute.
  986. * Implements Iterator
  987. *
  988. * @return array
  989. */
  990. public function current()
  991. {
  992. return $this;
  993. }
  994. /**
  995. * Return the attribute name.
  996. * Implements Iterator
  997. *
  998. * @return string
  999. */
  1000. public function key()
  1001. {
  1002. return $this->getRdnString();
  1003. }
  1004. /**
  1005. * Move forward to next attribute.
  1006. * Implements Iterator
  1007. */
  1008. public function next()
  1009. {
  1010. $this->_iteratorRewind = false;
  1011. }
  1012. /**
  1013. * Rewind the Iterator to the first attribute.
  1014. * Implements Iterator
  1015. */
  1016. public function rewind()
  1017. {
  1018. $this->_iteratorRewind = true;
  1019. }
  1020. /**
  1021. * Check if there is a current attribute
  1022. * after calls to rewind() or next().
  1023. * Implements Iterator
  1024. *
  1025. * @return boolean
  1026. */
  1027. public function valid()
  1028. {
  1029. return $this->_iteratorRewind;
  1030. }
  1031. }