PageRenderTime 130ms CodeModel.GetById 27ms RepoModel.GetById 6ms app.codeStats 0ms

/lib/Zend/Ldap.php

https://bitbucket.org/sunil_nextbits/magento2
PHP | 1594 lines | 948 code | 98 blank | 548 comment | 149 complexity | 8637cfbabb8a7c05583625aa7cc36f27 MD5 | raw file

Large files files are truncated, but you can click here to view the full 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-2010 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Ldap.php 22996 2010-09-22 17:01:46Z sgehrig $
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_Ldap
  24. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  25. * @license http://framework.zend.com/license/new-bsd New BSD License
  26. */
  27. class Zend_Ldap
  28. {
  29. const SEARCH_SCOPE_SUB = 1;
  30. const SEARCH_SCOPE_ONE = 2;
  31. const SEARCH_SCOPE_BASE = 3;
  32. const ACCTNAME_FORM_DN = 1;
  33. const ACCTNAME_FORM_USERNAME = 2;
  34. const ACCTNAME_FORM_BACKSLASH = 3;
  35. const ACCTNAME_FORM_PRINCIPAL = 4;
  36. /**
  37. * String used with ldap_connect for error handling purposes.
  38. *
  39. * @var string
  40. */
  41. private $_connectString;
  42. /**
  43. * The options used in connecting, binding, etc.
  44. *
  45. * @var array
  46. */
  47. protected $_options = null;
  48. /**
  49. * The raw LDAP extension resource.
  50. *
  51. * @var resource
  52. */
  53. protected $_resource = null;
  54. /**
  55. * FALSE if no user is bound to the LDAP resource
  56. * NULL if there has been an anonymous bind
  57. * username of the currently bound user
  58. *
  59. * @var boolean|null|string
  60. */
  61. protected $_boundUser = false;
  62. /**
  63. * Caches the RootDSE
  64. *
  65. * @var Zend_Ldap_Node
  66. */
  67. protected $_rootDse = null;
  68. /**
  69. * Caches the schema
  70. *
  71. * @var Zend_Ldap_Node
  72. */
  73. protected $_schema = null;
  74. /**
  75. * @deprecated will be removed, use {@see Zend_Ldap_Filter_Abstract::escapeValue()}
  76. * @param string $str The string to escape.
  77. * @return string The escaped string
  78. */
  79. public static function filterEscape($str)
  80. {
  81. /**
  82. * @see Zend_Ldap_Filter_Abstract
  83. */
  84. #require_once 'Zend/Ldap/Filter/Abstract.php';
  85. return Zend_Ldap_Filter_Abstract::escapeValue($str);
  86. }
  87. /**
  88. * @deprecated will be removed, use {@see Zend_Ldap_Dn::checkDn()}
  89. * @param string $dn The DN to parse
  90. * @param array $keys An optional array to receive DN keys (e.g. CN, OU, DC, ...)
  91. * @param array $vals An optional array to receive DN values
  92. * @return boolean True if the DN was successfully parsed or false if the string is
  93. * not a valid DN.
  94. */
  95. public static function explodeDn($dn, array &$keys = null, array &$vals = null)
  96. {
  97. /**
  98. * @see Zend_Ldap_Dn
  99. */
  100. #require_once 'Zend/Ldap/Dn.php';
  101. return Zend_Ldap_Dn::checkDn($dn, $keys, $vals);
  102. }
  103. /**
  104. * Constructor.
  105. *
  106. * @param array|Zend_Config $options Options used in connecting, binding, etc.
  107. * @return void
  108. * @throws Zend_Ldap_Exception if ext/ldap is not installed
  109. */
  110. public function __construct($options = array())
  111. {
  112. if (!extension_loaded('ldap')) {
  113. /**
  114. * @see Zend_Ldap_Exception
  115. */
  116. #require_once 'Zend/Ldap/Exception.php';
  117. throw new Zend_Ldap_Exception(null, 'LDAP extension not loaded',
  118. Zend_Ldap_Exception::LDAP_X_EXTENSION_NOT_LOADED);
  119. }
  120. $this->setOptions($options);
  121. }
  122. /**
  123. * Destructor.
  124. *
  125. * @return void
  126. */
  127. public function __destruct()
  128. {
  129. $this->disconnect();
  130. }
  131. /**
  132. * @return resource The raw LDAP extension resource.
  133. */
  134. public function getResource()
  135. {
  136. if (!is_resource($this->_resource) || $this->_boundUser === false) {
  137. $this->bind();
  138. }
  139. return $this->_resource;
  140. }
  141. /**
  142. * Return the LDAP error number of the last LDAP command
  143. *
  144. * @return int
  145. */
  146. public function getLastErrorCode()
  147. {
  148. $ret = @ldap_get_option($this->_resource, LDAP_OPT_ERROR_NUMBER, $err);
  149. if ($ret === true) {
  150. if ($err <= -1 && $err >= -17) {
  151. /**
  152. * @see Zend_Ldap_Exception
  153. */
  154. #require_once 'Zend/Ldap/Exception.php';
  155. /* For some reason draft-ietf-ldapext-ldap-c-api-xx.txt error
  156. * codes in OpenLDAP are negative values from -1 to -17.
  157. */
  158. $err = Zend_Ldap_Exception::LDAP_SERVER_DOWN + (-$err - 1);
  159. }
  160. return $err;
  161. }
  162. return 0;
  163. }
  164. /**
  165. * Return the LDAP error message of the last LDAP command
  166. *
  167. * @param int $errorCode
  168. * @param array $errorMessages
  169. * @return string
  170. */
  171. public function getLastError(&$errorCode = null, array &$errorMessages = null)
  172. {
  173. $errorCode = $this->getLastErrorCode();
  174. $errorMessages = array();
  175. /* The various error retrieval functions can return
  176. * different things so we just try to collect what we
  177. * can and eliminate dupes.
  178. */
  179. $estr1 = @ldap_error($this->_resource);
  180. if ($errorCode !== 0 && $estr1 === 'Success') {
  181. $estr1 = @ldap_err2str($errorCode);
  182. }
  183. if (!empty($estr1)) {
  184. $errorMessages[] = $estr1;
  185. }
  186. @ldap_get_option($this->_resource, LDAP_OPT_ERROR_STRING, $estr2);
  187. if (!empty($estr2) && !in_array($estr2, $errorMessages)) {
  188. $errorMessages[] = $estr2;
  189. }
  190. $message = '';
  191. if ($errorCode > 0) {
  192. $message = '0x' . dechex($errorCode) . ' ';
  193. } else {
  194. $message = '';
  195. }
  196. if (count($errorMessages) > 0) {
  197. $message .= '(' . implode('; ', $errorMessages) . ')';
  198. } else {
  199. $message .= '(no error message from LDAP)';
  200. }
  201. return $message;
  202. }
  203. /**
  204. * Get the currently bound user
  205. *
  206. * FALSE if no user is bound to the LDAP resource
  207. * NULL if there has been an anonymous bind
  208. * username of the currently bound user
  209. *
  210. * @return false|null|string
  211. */
  212. public function getBoundUser()
  213. {
  214. return $this->_boundUser;
  215. }
  216. /**
  217. * Sets the options used in connecting, binding, etc.
  218. *
  219. * Valid option keys:
  220. * host
  221. * port
  222. * useSsl
  223. * username
  224. * password
  225. * bindRequiresDn
  226. * baseDn
  227. * accountCanonicalForm
  228. * accountDomainName
  229. * accountDomainNameShort
  230. * accountFilterFormat
  231. * allowEmptyPassword
  232. * useStartTls
  233. * optRefferals
  234. * tryUsernameSplit
  235. *
  236. * @param array|Zend_Config $options Options used in connecting, binding, etc.
  237. * @return Zend_Ldap Provides a fluent interface
  238. * @throws Zend_Ldap_Exception
  239. */
  240. public function setOptions($options)
  241. {
  242. if ($options instanceof Zend_Config) {
  243. $options = $options->toArray();
  244. }
  245. $permittedOptions = array(
  246. 'host' => null,
  247. 'port' => 0,
  248. 'useSsl' => false,
  249. 'username' => null,
  250. 'password' => null,
  251. 'bindRequiresDn' => false,
  252. 'baseDn' => null,
  253. 'accountCanonicalForm' => null,
  254. 'accountDomainName' => null,
  255. 'accountDomainNameShort' => null,
  256. 'accountFilterFormat' => null,
  257. 'allowEmptyPassword' => false,
  258. 'useStartTls' => false,
  259. 'optReferrals' => false,
  260. 'tryUsernameSplit' => true,
  261. );
  262. foreach ($permittedOptions as $key => $val) {
  263. if (array_key_exists($key, $options)) {
  264. $val = $options[$key];
  265. unset($options[$key]);
  266. /* Enforce typing. This eliminates issues like Zend_Config_Ini
  267. * returning '1' as a string (ZF-3163).
  268. */
  269. switch ($key) {
  270. case 'port':
  271. case 'accountCanonicalForm':
  272. $permittedOptions[$key] = (int)$val;
  273. break;
  274. case 'useSsl':
  275. case 'bindRequiresDn':
  276. case 'allowEmptyPassword':
  277. case 'useStartTls':
  278. case 'optReferrals':
  279. case 'tryUsernameSplit':
  280. $permittedOptions[$key] = ($val === true ||
  281. $val === '1' || strcasecmp($val, 'true') == 0);
  282. break;
  283. default:
  284. $permittedOptions[$key] = trim($val);
  285. break;
  286. }
  287. }
  288. }
  289. if (count($options) > 0) {
  290. $key = key($options);
  291. /**
  292. * @see Zend_Ldap_Exception
  293. */
  294. #require_once 'Zend/Ldap/Exception.php';
  295. throw new Zend_Ldap_Exception(null, "Unknown Zend_Ldap option: $key");
  296. }
  297. $this->_options = $permittedOptions;
  298. return $this;
  299. }
  300. /**
  301. * @return array The current options.
  302. */
  303. public function getOptions()
  304. {
  305. return $this->_options;
  306. }
  307. /**
  308. * @return string The hostname of the LDAP server being used to authenticate accounts
  309. */
  310. protected function _getHost()
  311. {
  312. return $this->_options['host'];
  313. }
  314. /**
  315. * @return int The port of the LDAP server or 0 to indicate that no port value is set
  316. */
  317. protected function _getPort()
  318. {
  319. return $this->_options['port'];
  320. }
  321. /**
  322. * @return boolean The default SSL / TLS encrypted transport control
  323. */
  324. protected function _getUseSsl()
  325. {
  326. return $this->_options['useSsl'];
  327. }
  328. /**
  329. * @return string The default acctname for binding
  330. */
  331. protected function _getUsername()
  332. {
  333. return $this->_options['username'];
  334. }
  335. /**
  336. * @return string The default password for binding
  337. */
  338. protected function _getPassword()
  339. {
  340. return $this->_options['password'];
  341. }
  342. /**
  343. * @return boolean Bind requires DN
  344. */
  345. protected function _getBindRequiresDn()
  346. {
  347. return $this->_options['bindRequiresDn'];
  348. }
  349. /**
  350. * Gets the base DN under which objects of interest are located
  351. *
  352. * @return string
  353. */
  354. public function getBaseDn()
  355. {
  356. return $this->_options['baseDn'];
  357. }
  358. /**
  359. * @return integer Either ACCTNAME_FORM_BACKSLASH, ACCTNAME_FORM_PRINCIPAL or
  360. * ACCTNAME_FORM_USERNAME indicating the form usernames should be canonicalized to.
  361. */
  362. protected function _getAccountCanonicalForm()
  363. {
  364. /* Account names should always be qualified with a domain. In some scenarios
  365. * using non-qualified account names can lead to security vulnerabilities. If
  366. * no account canonical form is specified, we guess based in what domain
  367. * names have been supplied.
  368. */
  369. $accountCanonicalForm = $this->_options['accountCanonicalForm'];
  370. if (!$accountCanonicalForm) {
  371. $accountDomainName = $this->_getAccountDomainName();
  372. $accountDomainNameShort = $this->_getAccountDomainNameShort();
  373. if ($accountDomainNameShort) {
  374. $accountCanonicalForm = Zend_Ldap::ACCTNAME_FORM_BACKSLASH;
  375. } else if ($accountDomainName) {
  376. $accountCanonicalForm = Zend_Ldap::ACCTNAME_FORM_PRINCIPAL;
  377. } else {
  378. $accountCanonicalForm = Zend_Ldap::ACCTNAME_FORM_USERNAME;
  379. }
  380. }
  381. return $accountCanonicalForm;
  382. }
  383. /**
  384. * @return string The account domain name
  385. */
  386. protected function _getAccountDomainName()
  387. {
  388. return $this->_options['accountDomainName'];
  389. }
  390. /**
  391. * @return string The short account domain name
  392. */
  393. protected function _getAccountDomainNameShort()
  394. {
  395. return $this->_options['accountDomainNameShort'];
  396. }
  397. /**
  398. * @return string A format string for building an LDAP search filter to match
  399. * an account
  400. */
  401. protected function _getAccountFilterFormat()
  402. {
  403. return $this->_options['accountFilterFormat'];
  404. }
  405. /**
  406. * @return boolean Allow empty passwords
  407. */
  408. protected function _getAllowEmptyPassword()
  409. {
  410. return $this->_options['allowEmptyPassword'];
  411. }
  412. /**
  413. * @return boolean The default SSL / TLS encrypted transport control
  414. */
  415. protected function _getUseStartTls()
  416. {
  417. return $this->_options['useStartTls'];
  418. }
  419. /**
  420. * @return boolean Opt. Referrals
  421. */
  422. protected function _getOptReferrals()
  423. {
  424. return $this->_options['optReferrals'];
  425. }
  426. /**
  427. * @return boolean Try splitting the username into username and domain
  428. */
  429. protected function _getTryUsernameSplit()
  430. {
  431. return $this->_options['tryUsernameSplit'];
  432. }
  433. /**
  434. * @return string The LDAP search filter for matching directory accounts
  435. */
  436. protected function _getAccountFilter($acctname)
  437. {
  438. /**
  439. * @see Zend_Ldap_Filter_Abstract
  440. */
  441. #require_once 'Zend/Ldap/Filter/Abstract.php';
  442. $this->_splitName($acctname, $dname, $aname);
  443. $accountFilterFormat = $this->_getAccountFilterFormat();
  444. $aname = Zend_Ldap_Filter_Abstract::escapeValue($aname);
  445. if ($accountFilterFormat) {
  446. return sprintf($accountFilterFormat, $aname);
  447. }
  448. if (!$this->_getBindRequiresDn()) {
  449. // is there a better way to detect this?
  450. return sprintf("(&(objectClass=user)(sAMAccountName=%s))", $aname);
  451. }
  452. return sprintf("(&(objectClass=posixAccount)(uid=%s))", $aname);
  453. }
  454. /**
  455. * @param string $name The name to split
  456. * @param string $dname The resulting domain name (this is an out parameter)
  457. * @param string $aname The resulting account name (this is an out parameter)
  458. * @return void
  459. */
  460. protected function _splitName($name, &$dname, &$aname)
  461. {
  462. $dname = null;
  463. $aname = $name;
  464. if (!$this->_getTryUsernameSplit()) {
  465. return;
  466. }
  467. $pos = strpos($name, '@');
  468. if ($pos) {
  469. $dname = substr($name, $pos + 1);
  470. $aname = substr($name, 0, $pos);
  471. } else {
  472. $pos = strpos($name, '\\');
  473. if ($pos) {
  474. $dname = substr($name, 0, $pos);
  475. $aname = substr($name, $pos + 1);
  476. }
  477. }
  478. }
  479. /**
  480. * @param string $acctname The name of the account
  481. * @return string The DN of the specified account
  482. * @throws Zend_Ldap_Exception
  483. */
  484. protected function _getAccountDn($acctname)
  485. {
  486. /**
  487. * @see Zend_Ldap_Dn
  488. */
  489. #require_once 'Zend/Ldap/Dn.php';
  490. if (Zend_Ldap_Dn::checkDn($acctname)) return $acctname;
  491. $acctname = $this->getCanonicalAccountName($acctname, Zend_Ldap::ACCTNAME_FORM_USERNAME);
  492. $acct = $this->_getAccount($acctname, array('dn'));
  493. return $acct['dn'];
  494. }
  495. /**
  496. * @param string $dname The domain name to check
  497. * @return boolean
  498. */
  499. protected function _isPossibleAuthority($dname)
  500. {
  501. if ($dname === null) {
  502. return true;
  503. }
  504. $accountDomainName = $this->_getAccountDomainName();
  505. $accountDomainNameShort = $this->_getAccountDomainNameShort();
  506. if ($accountDomainName === null && $accountDomainNameShort === null) {
  507. return true;
  508. }
  509. if (strcasecmp($dname, $accountDomainName) == 0) {
  510. return true;
  511. }
  512. if (strcasecmp($dname, $accountDomainNameShort) == 0) {
  513. return true;
  514. }
  515. return false;
  516. }
  517. /**
  518. * @param string $acctname The name to canonicalize
  519. * @param int $type The desired form of canonicalization
  520. * @return string The canonicalized name in the desired form
  521. * @throws Zend_Ldap_Exception
  522. */
  523. public function getCanonicalAccountName($acctname, $form = 0)
  524. {
  525. $this->_splitName($acctname, $dname, $uname);
  526. if (!$this->_isPossibleAuthority($dname)) {
  527. /**
  528. * @see Zend_Ldap_Exception
  529. */
  530. #require_once 'Zend/Ldap/Exception.php';
  531. throw new Zend_Ldap_Exception(null,
  532. "Binding domain is not an authority for user: $acctname",
  533. Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH);
  534. }
  535. if (!$uname) {
  536. /**
  537. * @see Zend_Ldap_Exception
  538. */
  539. #require_once 'Zend/Ldap/Exception.php';
  540. throw new Zend_Ldap_Exception(null, "Invalid account name syntax: $acctname");
  541. }
  542. if (function_exists('mb_strtolower')) {
  543. $uname = mb_strtolower($uname, 'UTF-8');
  544. } else {
  545. $uname = strtolower($uname);
  546. }
  547. if ($form === 0) {
  548. $form = $this->_getAccountCanonicalForm();
  549. }
  550. switch ($form) {
  551. case Zend_Ldap::ACCTNAME_FORM_DN:
  552. return $this->_getAccountDn($acctname);
  553. case Zend_Ldap::ACCTNAME_FORM_USERNAME:
  554. return $uname;
  555. case Zend_Ldap::ACCTNAME_FORM_BACKSLASH:
  556. $accountDomainNameShort = $this->_getAccountDomainNameShort();
  557. if (!$accountDomainNameShort) {
  558. /**
  559. * @see Zend_Ldap_Exception
  560. */
  561. #require_once 'Zend/Ldap/Exception.php';
  562. throw new Zend_Ldap_Exception(null, 'Option required: accountDomainNameShort');
  563. }
  564. return "$accountDomainNameShort\\$uname";
  565. case Zend_Ldap::ACCTNAME_FORM_PRINCIPAL:
  566. $accountDomainName = $this->_getAccountDomainName();
  567. if (!$accountDomainName) {
  568. /**
  569. * @see Zend_Ldap_Exception
  570. */
  571. #require_once 'Zend/Ldap/Exception.php';
  572. throw new Zend_Ldap_Exception(null, 'Option required: accountDomainName');
  573. }
  574. return "$uname@$accountDomainName";
  575. default:
  576. /**
  577. * @see Zend_Ldap_Exception
  578. */
  579. #require_once 'Zend/Ldap/Exception.php';
  580. throw new Zend_Ldap_Exception(null, "Unknown canonical name form: $form");
  581. }
  582. }
  583. /**
  584. * @param array $attrs An array of names of desired attributes
  585. * @return array An array of the attributes representing the account
  586. * @throws Zend_Ldap_Exception
  587. */
  588. protected function _getAccount($acctname, array $attrs = null)
  589. {
  590. $baseDn = $this->getBaseDn();
  591. if (!$baseDn) {
  592. /**
  593. * @see Zend_Ldap_Exception
  594. */
  595. #require_once 'Zend/Ldap/Exception.php';
  596. throw new Zend_Ldap_Exception(null, 'Base DN not set');
  597. }
  598. $accountFilter = $this->_getAccountFilter($acctname);
  599. if (!$accountFilter) {
  600. /**
  601. * @see Zend_Ldap_Exception
  602. */
  603. #require_once 'Zend/Ldap/Exception.php';
  604. throw new Zend_Ldap_Exception(null, 'Invalid account filter');
  605. }
  606. if (!is_resource($this->getResource())) {
  607. $this->bind();
  608. }
  609. $accounts = $this->search($accountFilter, $baseDn, self::SEARCH_SCOPE_SUB, $attrs);
  610. $count = $accounts->count();
  611. if ($count === 1) {
  612. $acct = $accounts->getFirst();
  613. $accounts->close();
  614. return $acct;
  615. } else if ($count === 0) {
  616. /**
  617. * @see Zend_Ldap_Exception
  618. */
  619. #require_once 'Zend/Ldap/Exception.php';
  620. $code = Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT;
  621. $str = "No object found for: $accountFilter";
  622. } else {
  623. /**
  624. * @see Zend_Ldap_Exception
  625. */
  626. #require_once 'Zend/Ldap/Exception.php';
  627. $code = Zend_Ldap_Exception::LDAP_OPERATIONS_ERROR;
  628. $str = "Unexpected result count ($count) for: $accountFilter";
  629. }
  630. $accounts->close();
  631. /**
  632. * @see Zend_Ldap_Exception
  633. */
  634. #require_once 'Zend/Ldap/Exception.php';
  635. throw new Zend_Ldap_Exception($this, $str, $code);
  636. }
  637. /**
  638. * @return Zend_Ldap Provides a fluent interface
  639. */
  640. public function disconnect()
  641. {
  642. if (is_resource($this->_resource)) {
  643. @ldap_unbind($this->_resource);
  644. }
  645. $this->_resource = null;
  646. $this->_boundUser = false;
  647. return $this;
  648. }
  649. /**
  650. * To connect using SSL it seems the client tries to verify the server
  651. * certificate by default. One way to disable this behavior is to set
  652. * 'TLS_REQCERT never' in OpenLDAP's ldap.conf and restarting Apache. Or,
  653. * if you really care about the server's cert you can put a cert on the
  654. * web server.
  655. *
  656. * @param string $host The hostname of the LDAP server to connect to
  657. * @param int $port The port number of the LDAP server to connect to
  658. * @param boolean $useSsl Use SSL
  659. * @param boolean $useStartTls Use STARTTLS
  660. * @return Zend_Ldap Provides a fluent interface
  661. * @throws Zend_Ldap_Exception
  662. */
  663. public function connect($host = null, $port = null, $useSsl = null, $useStartTls = null)
  664. {
  665. if ($host === null) {
  666. $host = $this->_getHost();
  667. }
  668. if ($port === null) {
  669. $port = $this->_getPort();
  670. } else {
  671. $port = (int)$port;
  672. }
  673. if ($useSsl === null) {
  674. $useSsl = $this->_getUseSsl();
  675. } else {
  676. $useSsl = (bool)$useSsl;
  677. }
  678. if ($useStartTls === null) {
  679. $useStartTls = $this->_getUseStartTls();
  680. } else {
  681. $useStartTls = (bool)$useStartTls;
  682. }
  683. if (!$host) {
  684. /**
  685. * @see Zend_Ldap_Exception
  686. */
  687. #require_once 'Zend/Ldap/Exception.php';
  688. throw new Zend_Ldap_Exception(null, 'A host parameter is required');
  689. }
  690. $useUri = false;
  691. /* Because ldap_connect doesn't really try to connect, any connect error
  692. * will actually occur during the ldap_bind call. Therefore, we save the
  693. * connect string here for reporting it in error handling in bind().
  694. */
  695. $hosts = array();
  696. if (preg_match_all('~ldap(?:i|s)?://~', $host, $hosts, PREG_SET_ORDER) > 0) {
  697. $this->_connectString = $host;
  698. $useUri = true;
  699. $useSsl = false;
  700. } else {
  701. if ($useSsl) {
  702. $this->_connectString = 'ldaps://' . $host;
  703. $useUri = true;
  704. } else {
  705. $this->_connectString = 'ldap://' . $host;
  706. }
  707. if ($port) {
  708. $this->_connectString .= ':' . $port;
  709. }
  710. }
  711. $this->disconnect();
  712. /* Only OpenLDAP 2.2 + supports URLs so if SSL is not requested, just
  713. * use the old form.
  714. */
  715. $resource = ($useUri) ? @ldap_connect($this->_connectString) : @ldap_connect($host, $port);
  716. if (is_resource($resource) === true) {
  717. $this->_resource = $resource;
  718. $this->_boundUser = false;
  719. $optReferrals = ($this->_getOptReferrals()) ? 1 : 0;
  720. if (@ldap_set_option($resource, LDAP_OPT_PROTOCOL_VERSION, 3) &&
  721. @ldap_set_option($resource, LDAP_OPT_REFERRALS, $optReferrals)) {
  722. if ($useSsl || !$useStartTls || @ldap_start_tls($resource)) {
  723. return $this;
  724. }
  725. }
  726. /**
  727. * @see Zend_Ldap_Exception
  728. */
  729. #require_once 'Zend/Ldap/Exception.php';
  730. $zle = new Zend_Ldap_Exception($this, "$host:$port");
  731. $this->disconnect();
  732. throw $zle;
  733. }
  734. /**
  735. * @see Zend_Ldap_Exception
  736. */
  737. #require_once 'Zend/Ldap/Exception.php';
  738. throw new Zend_Ldap_Exception(null, "Failed to connect to LDAP server: $host:$port");
  739. }
  740. /**
  741. * @param string $username The username for authenticating the bind
  742. * @param string $password The password for authenticating the bind
  743. * @return Zend_Ldap Provides a fluent interface
  744. * @throws Zend_Ldap_Exception
  745. */
  746. public function bind($username = null, $password = null)
  747. {
  748. $moreCreds = true;
  749. if ($username === null) {
  750. $username = $this->_getUsername();
  751. $password = $this->_getPassword();
  752. $moreCreds = false;
  753. }
  754. if (empty($username)) {
  755. /* Perform anonymous bind
  756. */
  757. $username = null;
  758. $password = null;
  759. } else {
  760. /* Check to make sure the username is in DN form.
  761. */
  762. /**
  763. * @see Zend_Ldap_Dn
  764. */
  765. #require_once 'Zend/Ldap/Dn.php';
  766. if (!Zend_Ldap_Dn::checkDn($username)) {
  767. if ($this->_getBindRequiresDn()) {
  768. /* moreCreds stops an infinite loop if _getUsername does not
  769. * return a DN and the bind requires it
  770. */
  771. if ($moreCreds) {
  772. try {
  773. $username = $this->_getAccountDn($username);
  774. } catch (Zend_Ldap_Exception $zle) {
  775. switch ($zle->getCode()) {
  776. case Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT:
  777. case Zend_Ldap_Exception::LDAP_X_DOMAIN_MISMATCH:
  778. case Zend_Ldap_Exception::LDAP_X_EXTENSION_NOT_LOADED:
  779. throw $zle;
  780. }
  781. throw new Zend_Ldap_Exception(null,
  782. 'Failed to retrieve DN for account: ' . $username .
  783. ' [' . $zle->getMessage() . ']',
  784. Zend_Ldap_Exception::LDAP_OPERATIONS_ERROR);
  785. }
  786. } else {
  787. /**
  788. * @see Zend_Ldap_Exception
  789. */
  790. #require_once 'Zend/Ldap/Exception.php';
  791. throw new Zend_Ldap_Exception(null, 'Binding requires username in DN form');
  792. }
  793. } else {
  794. $username = $this->getCanonicalAccountName($username,
  795. $this->_getAccountCanonicalForm());
  796. }
  797. }
  798. }
  799. if (!is_resource($this->_resource)) {
  800. $this->connect();
  801. }
  802. if ($username !== null && $password === '' && $this->_getAllowEmptyPassword() !== true) {
  803. /**
  804. * @see Zend_Ldap_Exception
  805. */
  806. #require_once 'Zend/Ldap/Exception.php';
  807. $zle = new Zend_Ldap_Exception(null,
  808. 'Empty password not allowed - see allowEmptyPassword option.');
  809. } else {
  810. if (@ldap_bind($this->_resource, $username, $password)) {
  811. $this->_boundUser = $username;
  812. return $this;
  813. }
  814. $message = ($username === null) ? $this->_connectString : $username;
  815. /**
  816. * @see Zend_Ldap_Exception
  817. */
  818. #require_once 'Zend/Ldap/Exception.php';
  819. switch ($this->getLastErrorCode()) {
  820. case Zend_Ldap_Exception::LDAP_SERVER_DOWN:
  821. /* If the error is related to establishing a connection rather than binding,
  822. * the connect string is more informative than the username.
  823. */
  824. $message = $this->_connectString;
  825. }
  826. $zle = new Zend_Ldap_Exception($this, $message);
  827. }
  828. $this->disconnect();
  829. throw $zle;
  830. }
  831. /**
  832. * A global LDAP search routine for finding information.
  833. *
  834. * Options can be either passed as single parameters according to the
  835. * method signature or as an array with one or more of the following keys
  836. * - filter
  837. * - baseDn
  838. * - scope
  839. * - attributes
  840. * - sort
  841. * - collectionClass
  842. * - sizelimit
  843. * - timelimit
  844. *
  845. * @param string|Zend_Ldap_Filter_Abstract|array $filter
  846. * @param string|Zend_Ldap_Dn|null $basedn
  847. * @param integer $scope
  848. * @param array $attributes
  849. * @param string|null $sort
  850. * @param string|null $collectionClass
  851. * @param integer $sizelimit
  852. * @param integer $timelimit
  853. * @return Zend_Ldap_Collection
  854. * @throws Zend_Ldap_Exception
  855. */
  856. public function search($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB, array $attributes = array(),
  857. $sort = null, $collectionClass = null, $sizelimit = 0, $timelimit = 0)
  858. {
  859. if (is_array($filter)) {
  860. $options = array_change_key_case($filter, CASE_LOWER);
  861. foreach ($options as $key => $value) {
  862. switch ($key) {
  863. case 'filter':
  864. case 'basedn':
  865. case 'scope':
  866. case 'sort':
  867. $$key = $value;
  868. break;
  869. case 'attributes':
  870. if (is_array($value)) {
  871. $attributes = $value;
  872. }
  873. break;
  874. case 'collectionclass':
  875. $collectionClass = $value;
  876. break;
  877. case 'sizelimit':
  878. case 'timelimit':
  879. $$key = (int)$value;
  880. }
  881. }
  882. }
  883. if ($basedn === null) {
  884. $basedn = $this->getBaseDn();
  885. }
  886. else if ($basedn instanceof Zend_Ldap_Dn) {
  887. $basedn = $basedn->toString();
  888. }
  889. if ($filter instanceof Zend_Ldap_Filter_Abstract) {
  890. $filter = $filter->toString();
  891. }
  892. switch ($scope) {
  893. case self::SEARCH_SCOPE_ONE:
  894. $search = @ldap_list($this->getResource(), $basedn, $filter, $attributes, 0, $sizelimit, $timelimit);
  895. break;
  896. case self::SEARCH_SCOPE_BASE:
  897. $search = @ldap_read($this->getResource(), $basedn, $filter, $attributes, 0, $sizelimit, $timelimit);
  898. break;
  899. case self::SEARCH_SCOPE_SUB:
  900. default:
  901. $search = @ldap_search($this->getResource(), $basedn, $filter, $attributes, 0, $sizelimit, $timelimit);
  902. break;
  903. }
  904. if($search === false) {
  905. /**
  906. * @see Zend_Ldap_Exception
  907. */
  908. #require_once 'Zend/Ldap/Exception.php';
  909. throw new Zend_Ldap_Exception($this, 'searching: ' . $filter);
  910. }
  911. if ($sort !== null && is_string($sort)) {
  912. $isSorted = @ldap_sort($this->getResource(), $search, $sort);
  913. if($isSorted === false) {
  914. /**
  915. * @see Zend_Ldap_Exception
  916. */
  917. #require_once 'Zend/Ldap/Exception.php';
  918. throw new Zend_Ldap_Exception($this, 'sorting: ' . $sort);
  919. }
  920. }
  921. /**
  922. * Zend_Ldap_Collection_Iterator_Default
  923. */
  924. #require_once 'Zend/Ldap/Collection/Iterator/Default.php';
  925. $iterator = new Zend_Ldap_Collection_Iterator_Default($this, $search);
  926. return $this->_createCollection($iterator, $collectionClass);
  927. }
  928. /**
  929. * Extension point for collection creation
  930. *
  931. * @param Zend_Ldap_Collection_Iterator_Default $iterator
  932. * @param string|null $collectionClass
  933. * @return Zend_Ldap_Collection
  934. * @throws Zend_Ldap_Exception
  935. */
  936. protected function _createCollection(Zend_Ldap_Collection_Iterator_Default $iterator, $collectionClass)
  937. {
  938. if ($collectionClass === null) {
  939. /**
  940. * Zend_Ldap_Collection
  941. */
  942. #require_once 'Zend/Ldap/Collection.php';
  943. return new Zend_Ldap_Collection($iterator);
  944. } else {
  945. $collectionClass = (string)$collectionClass;
  946. if (!class_exists($collectionClass)) {
  947. /**
  948. * @see Zend_Ldap_Exception
  949. */
  950. #require_once 'Zend/Ldap/Exception.php';
  951. throw new Zend_Ldap_Exception(null,
  952. "Class '$collectionClass' can not be found");
  953. }
  954. if (!is_subclass_of($collectionClass, 'Zend_Ldap_Collection')) {
  955. /**
  956. * @see Zend_Ldap_Exception
  957. */
  958. #require_once 'Zend/Ldap/Exception.php';
  959. throw new Zend_Ldap_Exception(null,
  960. "Class '$collectionClass' must subclass 'Zend_Ldap_Collection'");
  961. }
  962. return new $collectionClass($iterator);
  963. }
  964. }
  965. /**
  966. * Count items found by given filter.
  967. *
  968. * @param string|Zend_Ldap_Filter_Abstract $filter
  969. * @param string|Zend_Ldap_Dn|null $basedn
  970. * @param integer $scope
  971. * @return integer
  972. * @throws Zend_Ldap_Exception
  973. */
  974. public function count($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB)
  975. {
  976. try {
  977. $result = $this->search($filter, $basedn, $scope, array('dn'), null);
  978. } catch (Zend_Ldap_Exception $e) {
  979. if ($e->getCode() === Zend_Ldap_Exception::LDAP_NO_SUCH_OBJECT) return 0;
  980. else throw $e;
  981. }
  982. return $result->count();
  983. }
  984. /**
  985. * Count children for a given DN.
  986. *
  987. * @param string|Zend_Ldap_Dn $dn
  988. * @return integer
  989. * @throws Zend_Ldap_Exception
  990. */
  991. public function countChildren($dn)
  992. {
  993. return $this->count('(objectClass=*)', $dn, self::SEARCH_SCOPE_ONE);
  994. }
  995. /**
  996. * Check if a given DN exists.
  997. *
  998. * @param string|Zend_Ldap_Dn $dn
  999. * @return boolean
  1000. * @throws Zend_Ldap_Exception
  1001. */
  1002. public function exists($dn)
  1003. {
  1004. return ($this->count('(objectClass=*)', $dn, self::SEARCH_SCOPE_BASE) == 1);
  1005. }
  1006. /**
  1007. * Search LDAP registry for entries matching filter and optional attributes
  1008. *
  1009. * Options can be either passed as single parameters according to the
  1010. * method signature or as an array with one or more of the following keys
  1011. * - filter
  1012. * - baseDn
  1013. * - scope
  1014. * - attributes
  1015. * - sort
  1016. * - reverseSort
  1017. * - sizelimit
  1018. * - timelimit
  1019. *
  1020. * @param string|Zend_Ldap_Filter_Abstract|array $filter
  1021. * @param string|Zend_Ldap_Dn|null $basedn
  1022. * @param integer $scope
  1023. * @param array $attributes
  1024. * @param string|null $sort
  1025. * @param boolean $reverseSort
  1026. * @param integer $sizelimit
  1027. * @param integer $timelimit
  1028. * @return array
  1029. * @throws Zend_Ldap_Exception
  1030. */
  1031. public function searchEntries($filter, $basedn = null, $scope = self::SEARCH_SCOPE_SUB,
  1032. array $attributes = array(), $sort = null, $reverseSort = false, $sizelimit = 0, $timelimit = 0)
  1033. {
  1034. if (is_array($filter)) {
  1035. $filter = array_change_key_case($filter, CASE_LOWER);
  1036. if (isset($filter['collectionclass'])) {
  1037. unset($filter['collectionclass']);
  1038. }
  1039. if (isset($filter['reversesort'])) {
  1040. $reverseSort = $filter['reversesort'];
  1041. unset($filter['reversesort']);
  1042. }
  1043. }
  1044. $result = $this->search($filter, $basedn, $scope, $attributes, $sort, null, $sizelimit, $timelimit);
  1045. $items = $result->toArray();
  1046. if ((bool)$reverseSort === true) {
  1047. $items = array_reverse($items, false);
  1048. }
  1049. return $items;
  1050. }
  1051. /**
  1052. * Get LDAP entry by DN
  1053. *
  1054. * @param string|Zend_Ldap_Dn $dn
  1055. * @param array $attributes
  1056. * @param boolean $throwOnNotFound
  1057. * @return array
  1058. * @throws Zend_Ldap_Exception
  1059. */
  1060. public function getEntry($dn, array $attributes = array(), $throwOnNotFound = false)
  1061. {
  1062. try {
  1063. $result = $this->search("(objectClass=*)", $dn, self::SEARCH_SCOPE_BASE,
  1064. $attributes, null);
  1065. return $result->getFirst();
  1066. } catch (Zend_Ldap_Exception $e){
  1067. if ($throwOnNotFound !== false) throw $e;
  1068. }
  1069. return null;
  1070. }
  1071. /**
  1072. * Prepares an ldap data entry array for insert/update operation
  1073. *
  1074. * @param array $entry
  1075. * @return void
  1076. * @throws InvalidArgumentException
  1077. */
  1078. public static function prepareLdapEntryArray(array &$entry)
  1079. {
  1080. if (array_key_exists('dn', $entry)) unset($entry['dn']);
  1081. foreach ($entry as $key => $value) {
  1082. if (is_array($value)) {
  1083. foreach ($value as $i => $v) {
  1084. if ($v === null) unset($value[$i]);
  1085. else if (!is_scalar($v)) {
  1086. throw new InvalidArgumentException('Only scalar values allowed in LDAP data');
  1087. } else {
  1088. $v = (string)$v;
  1089. if (strlen($v) == 0) {
  1090. unset($value[$i]);
  1091. } else {
  1092. $value[$i] = $v;
  1093. }
  1094. }
  1095. }
  1096. $entry[$key] = array_values($value);
  1097. } else {
  1098. if ($value === null) $entry[$key] = array();
  1099. else if (!is_scalar($value)) {
  1100. throw new InvalidArgumentException('Only scalar values allowed in LDAP data');
  1101. } else {
  1102. $value = (string)$value;
  1103. if (strlen($value) == 0) {
  1104. $entry[$key] = array();
  1105. } else {
  1106. $entry[$key] = array($value);
  1107. }
  1108. }
  1109. }
  1110. }
  1111. $entry = array_change_key_case($entry, CASE_LOWER);
  1112. }
  1113. /**
  1114. * Add new information to the LDAP repository
  1115. *
  1116. * @param string|Zend_Ldap_Dn $dn
  1117. * @param array $entry
  1118. * @return Zend_Ldap Provides a fluid interface
  1119. * @throws Zend_Ldap_Exception
  1120. */
  1121. public function add($dn, array $entry)
  1122. {
  1123. if (!($dn instanceof Zend_Ldap_Dn)) {
  1124. $dn = Zend_Ldap_Dn::factory($dn, null);
  1125. }
  1126. self::prepareLdapEntryArray($entry);
  1127. foreach ($entry as $key => $value) {
  1128. if (is_array($value) && count($value) === 0) {
  1129. unset($entry[$key]);
  1130. }
  1131. }
  1132. $rdnParts = $dn->getRdn(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  1133. foreach ($rdnParts as $key => $value) {
  1134. $value = Zend_Ldap_Dn::unescapeValue($value);
  1135. if (!array_key_exists($key, $entry)) {
  1136. $entry[$key] = array($value);
  1137. } else if (!in_array($value, $entry[$key])) {
  1138. $entry[$key] = array_merge(array($value), $entry[$key]);
  1139. }
  1140. }
  1141. $adAttributes = array('distinguishedname', 'instancetype', 'name', 'objectcategory',
  1142. 'objectguid', 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  1143. foreach ($adAttributes as $attr) {
  1144. if (array_key_exists($attr, $entry)) {
  1145. unset($entry[$attr]);
  1146. }
  1147. }
  1148. $isAdded = @ldap_add($this->getResource(), $dn->toString(), $entry);
  1149. if($isAdded === false) {
  1150. /**
  1151. * @see Zend_Ldap_Exception
  1152. */
  1153. #require_once 'Zend/Ldap/Exception.php';
  1154. throw new Zend_Ldap_Exception($this, 'adding: ' . $dn->toString());
  1155. }
  1156. return $this;
  1157. }
  1158. /**
  1159. * Update LDAP registry
  1160. *
  1161. * @param string|Zend_Ldap_Dn $dn
  1162. * @param array $entry
  1163. * @return Zend_Ldap Provides a fluid interface
  1164. * @throws Zend_Ldap_Exception
  1165. */
  1166. public function update($dn, array $entry)
  1167. {
  1168. if (!($dn instanceof Zend_Ldap_Dn)) {
  1169. $dn = Zend_Ldap_Dn::factory($dn, null);
  1170. }
  1171. self::prepareLdapEntryArray($entry);
  1172. $rdnParts = $dn->getRdn(Zend_Ldap_Dn::ATTR_CASEFOLD_LOWER);
  1173. foreach ($rdnParts as $key => $value) {
  1174. $value = Zend_Ldap_Dn::unescapeValue($value);
  1175. if (array_key_exists($key, $entry) && !in_array($value, $entry[$key])) {
  1176. $entry[$key] = array_merge(array($value), $entry[$key]);
  1177. }
  1178. }
  1179. $adAttributes = array('distinguishedname', 'instancetype', 'name', 'objectcategory',
  1180. 'objectguid', 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  1181. foreach ($adAttributes as $attr) {
  1182. if (array_key_exists($attr, $entry)) {
  1183. unset($entry[$attr]);
  1184. }
  1185. }
  1186. if (count($entry) > 0) {
  1187. $isModified = @ldap_modify($this->getResource(), $dn->toString(), $entry);
  1188. if($isModified === false) {
  1189. /**
  1190. * @see Zend_Ldap_Exception
  1191. */
  1192. #require_once 'Zend/Ldap/Exception.php';
  1193. throw new Zend_Ldap_Exception($this, 'updating: ' . $dn->toString());
  1194. }
  1195. }
  1196. return $this;
  1197. }
  1198. /**
  1199. * Save entry to LDAP registry.
  1200. *
  1201. * Internally decides if entry will be updated to added by calling
  1202. * {@link exists()}.
  1203. *
  1204. * @param string|Zend_Ldap_Dn $dn
  1205. * @param array $entry
  1206. * @return Zend_Ldap Provides a fluid interface
  1207. * @throws Zend_Ldap_Exception
  1208. */
  1209. public function save($dn, array $entry)
  1210. {
  1211. if ($dn instanceof Zend_Ldap_Dn) {
  1212. $dn = $dn->toString();
  1213. }
  1214. if ($this->exists($dn)) $this->update($dn, $entry);
  1215. else $this->add($dn, $entry);
  1216. return $this;
  1217. }
  1218. /**
  1219. * Delete an LDAP entry
  1220. *
  1221. * @param string|Zend_Ldap_Dn $dn
  1222. * @param boolean $recursively
  1223. * @return Zend_Ldap Provides a fluid interface
  1224. * @throws Zend_Ldap_Exception
  1225. */
  1226. public function delete($dn, $recursively = false)
  1227. {
  1228. if ($dn instanceof Zend_Ldap_Dn) {
  1229. $dn = $dn->toString();
  1230. }
  1231. if ($recursively === true) {
  1232. if ($this->countChildren($dn)>0) {
  1233. $children = $this->_getChildrenDns($dn);
  1234. foreach ($children as $c) {
  1235. $this->delete($c, true);
  1236. }
  1237. }
  1238. }
  1239. $isDeleted = @ldap_delete($this->getResource(), $dn);
  1240. if($isDeleted === false) {
  1241. /**
  1242. * @see Zend_Ldap_Exception
  1243. */
  1244. #require_once 'Zend/Ldap/Exception.php';
  1245. throw new Zend_Ldap_Exception($this, 'deleting: ' . $dn);
  1246. }
  1247. return $this;
  1248. }
  1249. /**
  1250. * Retrieve the immediate children DNs of the given $parentDn
  1251. *
  1252. * This method is used in recursive methods like {@see delete()}
  1253. * or {@see copy()}
  1254. *
  1255. * @param string|Zend_Ldap_Dn $parentDn
  1256. * @return array of DNs
  1257. */
  1258. protected function _getChildrenDns($parentDn)
  1259. {
  1260. if ($parentDn instanceof Zend_Ldap_Dn) {
  1261. $parentDn = $parentDn->toString();
  1262. }
  1263. $children = array();
  1264. $search = @ldap_list($this->getResource(), $parentDn, '(objectClass=*)', array('dn'));
  1265. for ($entry = @ldap_first_entry($this->getResource(), $search);
  1266. $entry !== false;
  1267. $entry = @ldap_next_entry($this->getResource(), $entry)) {
  1268. $childDn = @ldap_get_dn($this->getResource(), $entry);
  1269. if ($childDn === false) {
  1270. /**
  1271. * @see Zend_Ldap_Exception
  1272. */
  1273. #require_once 'Zend/Ldap/Exception.php';
  1274. throw new Zend_Ldap_Exception($this, 'getting dn');
  1275. }
  1276. $children[] = $childDn;
  1277. }
  1278. @ldap_free_result($search);
  1279. return $children;
  1280. }
  1281. /**
  1282. * Moves a LDAP entry from one DN to another subtree.
  1283. *
  1284. * @param string|Zend_Ldap_Dn $from
  1285. * @param string|Zend_Ldap_Dn $to
  1286. * @param boolean $recursively
  1287. * @param boolean $alwaysEmulate
  1288. * @return Zend_Ldap Provides a fluid interface
  1289. * @throws Zend_Ldap_Exception
  1290. */
  1291. public function moveToSubtree($from, $to, $recursively = false, $alwaysEmulate = false)
  1292. {
  1293. if ($from instanceof Zend_Ldap_Dn) {
  1294. $orgDnParts = $from->toArray();
  1295. } else {
  1296. $orgDnParts = Zend_Ldap_Dn::explodeDn($from);
  1297. }
  1298. if ($to instanceof Zend_Ldap_Dn) {
  1299. $newParentDnParts = $to->toArray();
  1300. } else {
  1301. $newParentDnParts = Zend_Ldap_Dn::explodeDn($to);
  1302. }
  1303. $newDnParts = array_merge(array(array_shift($orgDnParts)), $newParentDnParts);
  1304. $newDn = Zend_Ldap_Dn::fromArray($newDnParts);
  1305. return $this->rename($from, $newDn, $recursively, $alwaysEmulate);
  1306. }
  1307. /**
  1308. * Moves a LDAP entry from one DN to another DN.
  1309. *
  1310. * This is an alias for {@link rename()}
  1311. *
  1312. * @param string|Zend_Ldap_Dn $from
  1313. * @param string|Zend_Ldap_Dn $to
  1314. * @param boolean $recursively
  1315. * @param boolean $alwaysEmulate
  1316. * @return Zend_Ldap Provides a fluid interface
  1317. * @throws Zend_Ldap_Exception
  1318. */
  1319. public function move($from, $to, $recursively = false, $alwaysEmulate = false)
  1320. {
  1321. return $this->rename($from, $to, $recursively, $alwaysEmulate);
  1322. }
  1323. /**
  1324. * Renames a LDAP entry from one DN to another DN.
  1325. *
  1326. * This method implicitely moves the entry to another location within the tree.
  1327. *
  1328. * @param string|Zend_Ldap_Dn $from
  1329. * @param string|Zend_Ldap_Dn $to
  1330. * @param boolean $recursively
  1331. * @param boolean $alwaysEmulate
  1332. * @return Zend_Ldap Provides a fluid interface
  1333. * @throws Zend_Ldap_Exception
  1334. */
  1335. public function rename($from, $to, $recursively = false, $alwaysEmulate = false)
  1336. {
  1337. $emulate = (bool)$alwaysEmulate;
  1338. if (!function_exists('ldap_rename')) $emulate = true;
  1339. else if ($recursively) $emulate = true;
  1340. if ($emulate === false) {
  1341. if ($from instanceof Zend_Ldap_Dn) {
  1342. $from = $from->toString();
  1343. }
  1344. if ($to instanceof Zend_Ldap_Dn) {
  1345. $newDnParts = $to->toArray();
  1346. } else {
  1347. $newDnParts = Zend_Ldap_Dn::explodeDn($to);
  1348. }
  1349. $newRdn = Zend_Ldap_Dn::implodeRdn(array_shift($newDnParts));
  1350. $newParent = Zend_Ldap_Dn::implodeDn($newDnParts);
  1351. $isOK = @ldap_rename($this->getResource(), $from, $newRdn, $newParent, true);
  1352. if($isOK === false) {
  1353. /**
  1354. * @see Zend_Ldap_Exception
  1355. */
  1356. #require_once 'Zend/Ldap/Exception.php';
  1357. throw new Zend_Ldap_Exception($this, 'renaming ' . $from . ' to ' . $to);
  1358. }
  1359. else if (!$this->exists($to)) $emulate = true;
  1360. }
  1361. if ($emulate) {
  1362. $this->copy($from, $to, $recursively);
  1363. $this->delete($from, $recursively);
  1364. }
  1365. return $this;
  1366. }
  1367. /**
  1368. * Copies a LDAP entry from one DN to another subtree.
  1369. *
  1370. * @param string|Zend_Ldap_Dn $from
  1371. * @param string|Zend_Ldap_Dn $to
  1372. * @param boolean $recursively
  1373. * @return Zend_Ldap Provides a fluid interface
  1374. * @throws Zend_Ldap_Exception
  1375. */
  1376. public function copyToSubtree($from, $to, $recursively = false)
  1377. {
  1378. if ($from instanceof Zend_Ldap_Dn) {
  1379. $orgDnParts = $from->toArray();
  1380. } else {
  1381. $orgDnParts = Zend_Ldap_Dn::explodeDn($from);
  1382. }
  1383. if ($to instanceof Zend_Ldap_Dn) {
  1384. $newParentDnParts = $to->toArray();
  1385. } else {
  1386. $newParentDnParts = Zend_Ldap_Dn::explodeDn($to);
  1387. }
  1388. $newDnParts = array_merge(array(array_shift($orgDnParts)), $newParentDnParts);
  1389. $newDn = Zend_Ldap_Dn::fromArray($newDnParts);
  1390. return $this->copy($from, $newDn, $recursively);
  1391. }
  1392. /**
  1393. * Copies a LDAP entry from one DN to another DN.
  1394. *
  1395. * @param string|Zend_Ldap_Dn $from
  1396. * @pa…

Large files files are truncated, but you can click here to view the full file