PageRenderTime 71ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/application/libraries/Zend/Ldap/Dn.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 794 lines | 460 code | 37 blank | 297 comment | 54 complexity | 15fb72413a778d9c62571ab482055886 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: Dn.php 23775 2011-03-01 17:25:24Z ralph $
  20. */
  21. /**
  22. * Zend_Ldap_Dn provides an API for DN manipulation
  23. *
  24. * @category Zend
  25. * @package Zend_Ldap
  26. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Ldap_Dn implements ArrayAccess
  30. {
  31. const ATTR_CASEFOLD_NONE = 'none';
  32. const ATTR_CASEFOLD_UPPER = 'upper';
  33. const ATTR_CASEFOLD_LOWER = 'lower';
  34. /**
  35. * The default case fold to use
  36. *
  37. * @var string
  38. */
  39. protected static $_defaultCaseFold = self::ATTR_CASEFOLD_NONE;
  40. /**
  41. * The case fold used for this instance
  42. *
  43. * @var string
  44. */
  45. protected $_caseFold;
  46. /**
  47. * The DN data
  48. *
  49. * @var array
  50. */
  51. protected $_dn;
  52. /**
  53. * Creates a DN from an array or a string
  54. *
  55. * @param string|array $dn
  56. * @param string|null $caseFold
  57. * @return Zend_Ldap_Dn
  58. * @throws Zend_Ldap_Exception
  59. */
  60. public static function factory($dn, $caseFold = null)
  61. {
  62. if (is_array($dn)) {
  63. return self::fromArray($dn, $caseFold);
  64. } else if (is_string($dn)) {
  65. return self::fromString($dn, $caseFold);
  66. } else {
  67. /**
  68. * Zend_Ldap_Exception
  69. */
  70. require_once 'Zend/Ldap/Exception.php';
  71. throw new Zend_Ldap_Exception(null, 'Invalid argument type for $dn');
  72. }
  73. }
  74. /**
  75. * Creates a DN from a string
  76. *
  77. * @param string $dn
  78. * @param string|null $caseFold
  79. * @return Zend_Ldap_Dn
  80. * @throws Zend_Ldap_Exception
  81. */
  82. public static function fromString($dn, $caseFold = null)
  83. {
  84. $dn = trim($dn);
  85. if (empty($dn)) {
  86. $dnArray = array();
  87. } else {
  88. $dnArray = self::explodeDn((string)$dn);
  89. }
  90. return new self($dnArray, $caseFold);
  91. }
  92. /**
  93. * Creates a DN from an array
  94. *
  95. * @param array $dn
  96. * @param string|null $caseFold
  97. * @return Zend_Ldap_Dn
  98. * @throws Zend_Ldap_Exception
  99. */
  100. public static function fromArray(array $dn, $caseFold = null)
  101. {
  102. return new self($dn, $caseFold);
  103. }
  104. /**
  105. * Constructor
  106. *
  107. * @param array $dn
  108. * @param string|null $caseFold
  109. */
  110. protected function __construct(array $dn, $caseFold)
  111. {
  112. $this->_dn = $dn;
  113. $this->setCaseFold($caseFold);
  114. }
  115. /**
  116. * Gets the RDN of the current DN
  117. *
  118. * @param string $caseFold
  119. * @return array
  120. * @throws Zend_Ldap_Exception if DN has no RDN (empty array)
  121. */
  122. public function getRdn($caseFold = null)
  123. {
  124. $caseFold = self::_sanitizeCaseFold($caseFold, $this->_caseFold);
  125. return self::_caseFoldRdn($this->get(0, 1, $caseFold), null);
  126. }
  127. /**
  128. * Gets the RDN of the current DN as a string
  129. *
  130. * @param string $caseFold
  131. * @return string
  132. * @throws Zend_Ldap_Exception if DN has no RDN (empty array)
  133. */
  134. public function getRdnString($caseFold = null)
  135. {
  136. $caseFold = self::_sanitizeCaseFold($caseFold, $this->_caseFold);
  137. return self::implodeRdn($this->getRdn(), $caseFold);
  138. }
  139. /**
  140. * Get the parent DN $levelUp levels up the tree
  141. *
  142. * @param int $levelUp
  143. * @return Zend_Ldap_Dn
  144. */
  145. public function getParentDn($levelUp = 1)
  146. {
  147. $levelUp = (int)$levelUp;
  148. if ($levelUp < 1 || $levelUp >= count($this->_dn)) {
  149. /**
  150. * Zend_Ldap_Exception
  151. */
  152. require_once 'Zend/Ldap/Exception.php';
  153. throw new Zend_Ldap_Exception(null, 'Cannot retrieve parent DN with given $levelUp');
  154. }
  155. $newDn = array_slice($this->_dn, $levelUp);
  156. return new self($newDn, $this->_caseFold);
  157. }
  158. /**
  159. * Get a DN part
  160. *
  161. * @param int $index
  162. * @param int $length
  163. * @param string $caseFold
  164. * @return array
  165. * @throws Zend_Ldap_Exception if index is illegal
  166. */
  167. public function get($index, $length = 1, $caseFold = null)
  168. {
  169. $caseFold = self::_sanitizeCaseFold($caseFold, $this->_caseFold);
  170. $this->_assertIndex($index);
  171. $length = (int)$length;
  172. if ($length <= 0) {
  173. $length = 1;
  174. }
  175. if ($length === 1) {
  176. return self::_caseFoldRdn($this->_dn[$index], $caseFold);
  177. }
  178. else {
  179. return self::_caseFoldDn(array_slice($this->_dn, $index, $length, false), $caseFold);
  180. }
  181. }
  182. /**
  183. * Set a DN part
  184. *
  185. * @param int $index
  186. * @param array $value
  187. * @return Zend_Ldap_Dn Provides a fluent interface
  188. * @throws Zend_Ldap_Exception if index is illegal
  189. */
  190. public function set($index, array $value)
  191. {
  192. $this->_assertIndex($index);
  193. self::_assertRdn($value);
  194. $this->_dn[$index] = $value;
  195. return $this;
  196. }
  197. /**
  198. * Remove a DN part
  199. *
  200. * @param int $index
  201. * @param int $length
  202. * @return Zend_Ldap_Dn Provides a fluent interface
  203. * @throws Zend_Ldap_Exception if index is illegal
  204. */
  205. public function remove($index, $length = 1)
  206. {
  207. $this->_assertIndex($index);
  208. $length = (int)$length;
  209. if ($length <= 0) {
  210. $length = 1;
  211. }
  212. array_splice($this->_dn, $index, $length, null);
  213. return $this;
  214. }
  215. /**
  216. * Append a DN part
  217. *
  218. * @param array $value
  219. * @return Zend_Ldap_Dn Provides a fluent interface
  220. */
  221. public function append(array $value)
  222. {
  223. self::_assertRdn($value);
  224. $this->_dn[] = $value;
  225. return $this;
  226. }
  227. /**
  228. * Prepend a DN part
  229. *
  230. * @param array $value
  231. * @return Zend_Ldap_Dn Provides a fluent interface
  232. */
  233. public function prepend(array $value)
  234. {
  235. self::_assertRdn($value);
  236. array_unshift($this->_dn, $value);
  237. return $this;
  238. }
  239. /**
  240. * Insert a DN part
  241. *
  242. * @param int $index
  243. * @param array $value
  244. * @return Zend_Ldap_Dn Provides a fluent interface
  245. * @throws Zend_Ldap_Exception if index is illegal
  246. */
  247. public function insert($index, array $value)
  248. {
  249. $this->_assertIndex($index);
  250. self::_assertRdn($value);
  251. $first = array_slice($this->_dn, 0, $index + 1);
  252. $second = array_slice($this->_dn, $index + 1);
  253. $this->_dn = array_merge($first, array($value), $second);
  254. return $this;
  255. }
  256. /**
  257. * Assert index is correct and usable
  258. *
  259. * @param mixed $index
  260. * @return boolean
  261. * @throws Zend_Ldap_Exception
  262. */
  263. protected function _assertIndex($index)
  264. {
  265. if (!is_int($index)) {
  266. /**
  267. * Zend_Ldap_Exception
  268. */
  269. require_once 'Zend/Ldap/Exception.php';
  270. throw new Zend_Ldap_Exception(null, 'Parameter $index must be an integer');
  271. }
  272. if ($index < 0 || $index >= count($this->_dn)) {
  273. /**
  274. * Zend_Ldap_Exception
  275. */
  276. require_once 'Zend/Ldap/Exception.php';
  277. throw new Zend_Ldap_Exception(null, 'Parameter $index out of bounds');
  278. }
  279. return true;
  280. }
  281. /**
  282. * Assert if value is in a correct RDN format
  283. *
  284. * @param array $value
  285. * @return boolean
  286. * @throws Zend_Ldap_Exception
  287. */
  288. protected static function _assertRdn(array $value)
  289. {
  290. if (count($value)<1) {
  291. /**
  292. * Zend_Ldap_Exception
  293. */
  294. require_once 'Zend/Ldap/Exception.php';
  295. throw new Zend_Ldap_Exception(null, 'RDN Array is malformed: it must have at least one item');
  296. }
  297. foreach (array_keys($value) as $key) {
  298. if (!is_string($key)) {
  299. /**
  300. * Zend_Ldap_Exception
  301. */
  302. require_once 'Zend/Ldap/Exception.php';
  303. throw new Zend_Ldap_Exception(null, 'RDN Array is malformed: it must use string keys');
  304. }
  305. }
  306. }
  307. /**
  308. * Sets the case fold
  309. *
  310. * @param string|null $caseFold
  311. */
  312. public function setCaseFold($caseFold)
  313. {
  314. $this->_caseFold = self::_sanitizeCaseFold($caseFold, self::$_defaultCaseFold);
  315. }
  316. /**
  317. * Return DN as a string
  318. *
  319. * @param string $caseFold
  320. * @return string
  321. * @throws Zend_Ldap_Exception
  322. */
  323. public function toString($caseFold = null)
  324. {
  325. $caseFold = self::_sanitizeCaseFold($caseFold, $this->_caseFold);
  326. return self::implodeDn($this->_dn, $caseFold);
  327. }
  328. /**
  329. * Return DN as an array
  330. *
  331. * @param string $caseFold
  332. * @return array
  333. */
  334. public function toArray($caseFold = null)
  335. {
  336. $caseFold = self::_sanitizeCaseFold($caseFold, $this->_caseFold);
  337. if ($caseFold === self::ATTR_CASEFOLD_NONE) {
  338. return $this->_dn;
  339. } else {
  340. return self::_caseFoldDn($this->_dn, $caseFold);
  341. }
  342. }
  343. /**
  344. * Do a case folding on a RDN
  345. *
  346. * @param array $part
  347. * @param string $caseFold
  348. * @return array
  349. */
  350. protected static function _caseFoldRdn(array $part, $caseFold)
  351. {
  352. switch ($caseFold) {
  353. case self::ATTR_CASEFOLD_UPPER:
  354. return array_change_key_case($part, CASE_UPPER);
  355. case self::ATTR_CASEFOLD_LOWER:
  356. return array_change_key_case($part, CASE_LOWER);
  357. case self::ATTR_CASEFOLD_NONE:
  358. default:
  359. return $part;
  360. }
  361. }
  362. /**
  363. * Do a case folding on a DN ort part of it
  364. *
  365. * @param array $dn
  366. * @param string $caseFold
  367. * @return array
  368. */
  369. protected static function _caseFoldDn(array $dn, $caseFold)
  370. {
  371. $return = array();
  372. foreach ($dn as $part) {
  373. $return[] = self::_caseFoldRdn($part, $caseFold);
  374. }
  375. return $return;
  376. }
  377. /**
  378. * Cast to string representation {@see toString()}
  379. *
  380. * @return string
  381. */
  382. public function __toString()
  383. {
  384. return $this->toString();
  385. }
  386. /**
  387. * Required by the ArrayAccess implementation
  388. *
  389. * @param int $offset
  390. * @return boolean
  391. */
  392. public function offsetExists($offset)
  393. {
  394. $offset = (int)$offset;
  395. if ($offset < 0 || $offset >= count($this->_dn)) {
  396. return false;
  397. } else {
  398. return true;
  399. }
  400. }
  401. /**
  402. * Proxy to {@see get()}
  403. * Required by the ArrayAccess implementation
  404. *
  405. * @param int $offset
  406. * @return array
  407. */
  408. public function offsetGet($offset)
  409. {
  410. return $this->get($offset, 1, null);
  411. }
  412. /**
  413. * Proxy to {@see set()}
  414. * Required by the ArrayAccess implementation
  415. *
  416. * @param int $offset
  417. * @param array $value
  418. */
  419. public function offsetSet($offset, $value)
  420. {
  421. $this->set($offset, $value);
  422. }
  423. /**
  424. * Proxy to {@see remove()}
  425. * Required by the ArrayAccess implementation
  426. *
  427. * @param int $offset
  428. */
  429. public function offsetUnset($offset)
  430. {
  431. $this->remove($offset, 1);
  432. }
  433. /**
  434. * Sets the default case fold
  435. *
  436. * @param string $caseFold
  437. */
  438. public static function setDefaultCaseFold($caseFold)
  439. {
  440. self::$_defaultCaseFold = self::_sanitizeCaseFold($caseFold, self::ATTR_CASEFOLD_NONE);
  441. }
  442. /**
  443. * Sanitizes the case fold
  444. *
  445. * @param string $caseFold
  446. * @return string
  447. */
  448. protected static function _sanitizeCaseFold($caseFold, $default)
  449. {
  450. switch ($caseFold) {
  451. case self::ATTR_CASEFOLD_NONE:
  452. case self::ATTR_CASEFOLD_UPPER:
  453. case self::ATTR_CASEFOLD_LOWER:
  454. return $caseFold;
  455. break;
  456. default:
  457. return $default;
  458. break;
  459. }
  460. }
  461. /**
  462. * Escapes a DN value according to RFC 2253
  463. *
  464. * Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs.
  465. * The characters ",", "+", """, "\", "<", ">", ";", "#", " = " with a special meaning in RFC 2252
  466. * are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair.
  467. * Finally all leading and trailing spaces are converted to sequences of \20.
  468. * @see Net_LDAP2_Util::escape_dn_value() from Benedikt Hallinger <beni@php.net>
  469. * @link http://pear.php.net/package/Net_LDAP2
  470. * @author Benedikt Hallinger <beni@php.net>
  471. *
  472. * @param string|array $values An array containing the DN values that should be escaped
  473. * @return array The array $values, but escaped
  474. */
  475. public static function escapeValue($values = array())
  476. {
  477. /**
  478. * @see Zend_Ldap_Converter
  479. */
  480. require_once 'Zend/Ldap/Converter.php';
  481. if (!is_array($values)) $values = array($values);
  482. foreach ($values as $key => $val) {
  483. // Escaping of filter meta characters
  484. $val = str_replace(array('\\', ',', '+', '"', '<', '>', ';', '#', '=', ),
  485. array('\\\\', '\,', '\+', '\"', '\<', '\>', '\;', '\#', '\='), $val);
  486. $val = Zend_Ldap_Converter::ascToHex32($val);
  487. // Convert all leading and trailing spaces to sequences of \20.
  488. if (preg_match('/^(\s*)(.+?)(\s*)$/', $val, $matches)) {
  489. $val = $matches[2];
  490. for ($i = 0; $i<strlen($matches[1]); $i++) {
  491. $val = '\20' . $val;
  492. }
  493. for ($i = 0; $i<strlen($matches[3]); $i++) {
  494. $val = $val . '\20';
  495. }
  496. }
  497. if (null === $val) $val = '\0'; // apply escaped "null" if string is empty
  498. $values[$key] = $val;
  499. }
  500. return (count($values) == 1) ? $values[0] : $values;
  501. }
  502. /**
  503. * Undoes the conversion done by {@link escapeValue()}.
  504. *
  505. * Any escape sequence starting with a baskslash - hexpair or special character -
  506. * will be transformed back to the corresponding character.
  507. * @see Net_LDAP2_Util::escape_dn_value() from Benedikt Hallinger <beni@php.net>
  508. * @link http://pear.php.net/package/Net_LDAP2
  509. * @author Benedikt Hallinger <beni@php.net>
  510. *
  511. * @param string|array $values Array of DN Values
  512. * @return array Same as $values, but unescaped
  513. */
  514. public static function unescapeValue($values = array())
  515. {
  516. /**
  517. * @see Zend_Ldap_Converter
  518. */
  519. require_once 'Zend/Ldap/Converter.php';
  520. if (!is_array($values)) $values = array($values);
  521. foreach ($values as $key => $val) {
  522. // strip slashes from special chars
  523. $val = str_replace(array('\\\\', '\,', '\+', '\"', '\<', '\>', '\;', '\#', '\='),
  524. array('\\', ',', '+', '"', '<', '>', ';', '#', '=', ), $val);
  525. $values[$key] = Zend_Ldap_Converter::hex32ToAsc($val);
  526. }
  527. return (count($values) == 1) ? $values[0] : $values;
  528. }
  529. /**
  530. * Creates an array containing all parts of the given DN.
  531. *
  532. * Array will be of type
  533. * array(
  534. * array("cn" => "name1", "uid" => "user"),
  535. * array("cn" => "name2"),
  536. * array("dc" => "example"),
  537. * array("dc" => "org")
  538. * )
  539. * for a DN of cn=name1+uid=user,cn=name2,dc=example,dc=org.
  540. *
  541. * @param string $dn
  542. * @param array $keys An optional array to receive DN keys (e.g. CN, OU, DC, ...)
  543. * @param array $vals An optional array to receive DN values
  544. * @param string $caseFold
  545. * @return array
  546. * @throws Zend_Ldap_Exception
  547. */
  548. public static function explodeDn($dn, array &$keys = null, array &$vals = null,
  549. $caseFold = self::ATTR_CASEFOLD_NONE)
  550. {
  551. $k = array();
  552. $v = array();
  553. if (!self::checkDn($dn, $k, $v, $caseFold)) {
  554. /**
  555. * Zend_Ldap_Exception
  556. */
  557. require_once 'Zend/Ldap/Exception.php';
  558. throw new Zend_Ldap_Exception(null, 'DN is malformed');
  559. }
  560. $ret = array();
  561. for ($i = 0; $i < count($k); $i++) {
  562. if (is_array($k[$i]) && is_array($v[$i]) && (count($k[$i]) === count($v[$i]))) {
  563. $multi = array();
  564. for ($j = 0; $j < count($k[$i]); $j++) {
  565. $key=$k[$i][$j];
  566. $val=$v[$i][$j];
  567. $multi[$key] = $val;
  568. }
  569. $ret[] = $multi;
  570. } else if (is_string($k[$i]) && is_string($v[$i])) {
  571. $ret[] = array($k[$i] => $v[$i]);
  572. }
  573. }
  574. if ($keys !== null) $keys = $k;
  575. if ($vals !== null) $vals = $v;
  576. return $ret;
  577. }
  578. /**
  579. * @param string $dn The DN to parse
  580. * @param array $keys An optional array to receive DN keys (e.g. CN, OU, DC, ...)
  581. * @param array $vals An optional array to receive DN values
  582. * @param string $caseFold
  583. * @return boolean True if the DN was successfully parsed or false if the string is not a valid DN.
  584. */
  585. public static function checkDn($dn, array &$keys = null, array &$vals = null,
  586. $caseFold = self::ATTR_CASEFOLD_NONE)
  587. {
  588. /* This is a classic state machine parser. Each iteration of the
  589. * loop processes one character. State 1 collects the key. When equals ( = )
  590. * is encountered the state changes to 2 where the value is collected
  591. * until a comma (,) or semicolon (;) is encountered after which we switch back
  592. * to state 1. If a backslash (\) is encountered, state 3 is used to collect the
  593. * following character without engaging the logic of other states.
  594. */
  595. $key = null;
  596. $value = null;
  597. $slen = strlen($dn);
  598. $state = 1;
  599. $ko = $vo = 0;
  600. $multi = false;
  601. $ka = array();
  602. $va = array();
  603. for ($di = 0; $di <= $slen; $di++) {
  604. $ch = ($di == $slen) ? 0 : $dn[$di];
  605. switch ($state) {
  606. case 1: // collect key
  607. if ($ch === '=') {
  608. $key = trim(substr($dn, $ko, $di - $ko));
  609. if ($caseFold == self::ATTR_CASEFOLD_LOWER) $key = strtolower($key);
  610. else if ($caseFold == self::ATTR_CASEFOLD_UPPER) $key = strtoupper($key);
  611. if (is_array($multi)) {
  612. $keyId = strtolower($key);
  613. if (in_array($keyId, $multi)) {
  614. return false;
  615. }
  616. $ka[count($ka)-1][] = $key;
  617. $multi[] = $keyId;
  618. } else {
  619. $ka[] = $key;
  620. }
  621. $state = 2;
  622. $vo = $di + 1;
  623. } else if ($ch === ',' || $ch === ';' || $ch === '+') {
  624. return false;
  625. }
  626. break;
  627. case 2: // collect value
  628. if ($ch === '\\') {
  629. $state = 3;
  630. } else if ($ch === ',' || $ch === ';' || $ch === 0 || $ch === '+') {
  631. $value = self::unescapeValue(trim(substr($dn, $vo, $di - $vo)));
  632. if (is_array($multi)) {
  633. $va[count($va)-1][] = $value;
  634. } else {
  635. $va[] = $value;
  636. }
  637. $state = 1;
  638. $ko = $di + 1;
  639. if ($ch === '+' && $multi === false) {
  640. $lastKey = array_pop($ka);
  641. $lastVal = array_pop($va);
  642. $ka[] = array($lastKey);
  643. $va[] = array($lastVal);
  644. $multi = array(strtolower($lastKey));
  645. } else if ($ch === ','|| $ch === ';' || $ch === 0) {
  646. $multi = false;
  647. }
  648. } else if ($ch === '=') {
  649. return false;
  650. }
  651. break;
  652. case 3: // escaped
  653. $state = 2;
  654. break;
  655. }
  656. }
  657. if ($keys !== null) {
  658. $keys = $ka;
  659. }
  660. if ($vals !== null) {
  661. $vals = $va;
  662. }
  663. return ($state === 1 && $ko > 0);
  664. }
  665. /**
  666. * Returns a DN part in the form $attribute = $value
  667. *
  668. * This method supports the creation of multi-valued RDNs
  669. * $part must contain an even number of elemets.
  670. *
  671. * @param array $attribute
  672. * @param string $caseFold
  673. * @return string
  674. * @throws Zend_Ldap_Exception
  675. */
  676. public static function implodeRdn(array $part, $caseFold = null)
  677. {
  678. self::_assertRdn($part);
  679. $part = self::_caseFoldRdn($part, $caseFold);
  680. $rdnParts = array();
  681. foreach ($part as $key => $value) {
  682. $value = self::escapeValue($value);
  683. $keyId = strtolower($key);
  684. $rdnParts[$keyId] = implode('=', array($key, $value));
  685. }
  686. ksort($rdnParts, SORT_STRING);
  687. return implode('+', $rdnParts);
  688. }
  689. /**
  690. * Implodes an array in the form delivered by {@link explodeDn()}
  691. * to a DN string.
  692. *
  693. * $dnArray must be of type
  694. * array(
  695. * array("cn" => "name1", "uid" => "user"),
  696. * array("cn" => "name2"),
  697. * array("dc" => "example"),
  698. * array("dc" => "org")
  699. * )
  700. *
  701. * @param array $dnArray
  702. * @param string $caseFold
  703. * @param string $separator
  704. * @return string
  705. * @throws Zend_Ldap_Exception
  706. */
  707. public static function implodeDn(array $dnArray, $caseFold = null, $separator = ',')
  708. {
  709. $parts = array();
  710. foreach ($dnArray as $p) {
  711. $parts[] = self::implodeRdn($p, $caseFold);
  712. }
  713. return implode($separator, $parts);
  714. }
  715. /**
  716. * Checks if given $childDn is beneath $parentDn subtree.
  717. *
  718. * @param string|Zend_Ldap_Dn $childDn
  719. * @param string|Zend_Ldap_Dn $parentDn
  720. * @return boolean
  721. */
  722. public static function isChildOf($childDn, $parentDn)
  723. {
  724. try {
  725. $keys = array();
  726. $vals = array();
  727. if ($childDn instanceof Zend_Ldap_Dn) {
  728. $cdn = $childDn->toArray(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  729. } else {
  730. $cdn = self::explodeDn($childDn, $keys, $vals, Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  731. }
  732. if ($parentDn instanceof Zend_Ldap_Dn) {
  733. $pdn = $parentDn->toArray(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  734. } else {
  735. $pdn = self::explodeDn($parentDn, $keys, $vals, Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  736. }
  737. }
  738. catch (Zend_Ldap_Exception $e) {
  739. return false;
  740. }
  741. $startIndex = count($cdn)-count($pdn);
  742. if ($startIndex<0) return false;
  743. for ($i = 0; $i<count($pdn); $i++) {
  744. if ($cdn[$i+$startIndex] != $pdn[$i]) return false;
  745. }
  746. return true;
  747. }
  748. }