PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Ldap/Node/Abstract.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 485 lines | 164 code | 33 blank | 288 comment | 11 complexity | 06a306475d8850c5ccfe6a75869b30c2 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. * @subpackage Node
  18. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id: Abstract.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Ldap_Attribute
  24. */
  25. require_once 'Zend/Ldap/Attribute.php';
  26. /**
  27. * @see Zend_Ldap_Dn
  28. */
  29. require_once 'Zend/Ldap/Dn.php';
  30. /**
  31. * Zend_Ldap_Node_Abstract provides a bas eimplementation for LDAP nodes
  32. *
  33. * @category Zend
  34. * @package Zend_Ldap
  35. * @subpackage Node
  36. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  37. * @license http://framework.zend.com/license/new-bsd New BSD License
  38. */
  39. abstract class Zend_Ldap_Node_Abstract implements ArrayAccess, Countable
  40. {
  41. protected static $_systemAttributes=array('createtimestamp', 'creatorsname',
  42. 'entrycsn', 'entrydn', 'entryuuid', 'hassubordinates', 'modifiersname',
  43. 'modifytimestamp', 'structuralobjectclass', 'subschemasubentry',
  44. 'distinguishedname', 'instancetype', 'name', 'objectcategory', 'objectguid',
  45. 'usnchanged', 'usncreated', 'whenchanged', 'whencreated');
  46. /**
  47. * Holds the node's DN.
  48. *
  49. * @var Zend_Ldap_Dn
  50. */
  51. protected $_dn;
  52. /**
  53. * Holds the node's current data.
  54. *
  55. * @var array
  56. */
  57. protected $_currentData;
  58. /**
  59. * Constructor.
  60. *
  61. * Constructor is protected to enforce the use of factory methods.
  62. *
  63. * @param Zend_Ldap_Dn $dn
  64. * @param array $data
  65. * @param boolean $fromDataSource
  66. */
  67. protected function __construct(Zend_Ldap_Dn $dn, array $data, $fromDataSource)
  68. {
  69. $this->_dn = $dn;
  70. $this->_loadData($data, $fromDataSource);
  71. }
  72. /**
  73. * @param array $data
  74. * @param boolean $fromDataSource
  75. * @throws Zend_Ldap_Exception
  76. */
  77. protected function _loadData(array $data, $fromDataSource)
  78. {
  79. if (array_key_exists('dn', $data)) {
  80. unset($data['dn']);
  81. }
  82. ksort($data, SORT_STRING);
  83. $this->_currentData = $data;
  84. }
  85. /**
  86. * Reload node attributes from LDAP.
  87. *
  88. * This is an online method.
  89. *
  90. * @param Zend_Ldap $ldap
  91. * @return Zend_Ldap_Node_Abstract Provides a fluid interface
  92. * @throws Zend_Ldap_Exception
  93. */
  94. public function reload(Zend_Ldap $ldap = null)
  95. {
  96. if ($ldap !== null) {
  97. $data = $ldap->getEntry($this->_getDn(), array('*', '+'), true);
  98. $this->_loadData($data, true);
  99. }
  100. return $this;
  101. }
  102. /**
  103. * Gets the DN of the current node as a Zend_Ldap_Dn.
  104. *
  105. * This is an offline method.
  106. *
  107. * @return Zend_Ldap_Dn
  108. */
  109. protected function _getDn()
  110. {
  111. return $this->_dn;
  112. }
  113. /**
  114. * Gets the DN of the current node as a Zend_Ldap_Dn.
  115. * The method returns a clone of the node's DN to prohibit modification.
  116. *
  117. * This is an offline method.
  118. *
  119. * @return Zend_Ldap_Dn
  120. */
  121. public function getDn()
  122. {
  123. $dn = clone $this->_getDn();
  124. return $dn;
  125. }
  126. /**
  127. * Gets the DN of the current node as a string.
  128. *
  129. * This is an offline method.
  130. *
  131. * @param string $caseFold
  132. * @return string
  133. */
  134. public function getDnString($caseFold = null)
  135. {
  136. return $this->_getDn()->toString($caseFold);
  137. }
  138. /**
  139. * Gets the DN of the current node as an array.
  140. *
  141. * This is an offline method.
  142. *
  143. * @param string $caseFold
  144. * @return array
  145. */
  146. public function getDnArray($caseFold = null)
  147. {
  148. return $this->_getDn()->toArray($caseFold);
  149. }
  150. /**
  151. * Gets the RDN of the current node as a string.
  152. *
  153. * This is an offline method.
  154. *
  155. * @param string $caseFold
  156. * @return string
  157. */
  158. public function getRdnString($caseFold = null)
  159. {
  160. return $this->_getDn()->getRdnString($caseFold);
  161. }
  162. /**
  163. * Gets the RDN of the current node as an array.
  164. *
  165. * This is an offline method.
  166. *
  167. * @param string $caseFold
  168. * @return array
  169. */
  170. public function getRdnArray($caseFold = null)
  171. {
  172. return $this->_getDn()->getRdn($caseFold);
  173. }
  174. /**
  175. * Gets the objectClass of the node
  176. *
  177. * @return array
  178. */
  179. public function getObjectClass()
  180. {
  181. return $this->getAttribute('objectClass', null);
  182. }
  183. /**
  184. * Gets all attributes of node.
  185. *
  186. * The collection contains all attributes.
  187. *
  188. * This is an offline method.
  189. *
  190. * @param boolean $includeSystemAttributes
  191. * @return array
  192. */
  193. public function getAttributes($includeSystemAttributes = true)
  194. {
  195. $data = array();
  196. foreach ($this->getData($includeSystemAttributes) as $name => $value) {
  197. $data[$name] = $this->getAttribute($name, null);
  198. }
  199. return $data;
  200. }
  201. /**
  202. * Returns the DN of the current node. {@see getDnString()}
  203. *
  204. * @return string
  205. */
  206. public function toString()
  207. {
  208. return $this->getDnString();
  209. }
  210. /**
  211. * Cast to string representation {@see toString()}
  212. *
  213. * @return string
  214. */
  215. public function __toString()
  216. {
  217. return $this->toString();
  218. }
  219. /**
  220. * Returns an array representation of the current node
  221. *
  222. * @param boolean $includeSystemAttributes
  223. * @return array
  224. */
  225. public function toArray($includeSystemAttributes = true)
  226. {
  227. $attributes = $this->getAttributes($includeSystemAttributes);
  228. return array_merge(array('dn' => $this->getDnString()), $attributes);
  229. }
  230. /**
  231. * Returns a JSON representation of the current node
  232. *
  233. * @param boolean $includeSystemAttributes
  234. * @return string
  235. */
  236. public function toJson($includeSystemAttributes = true)
  237. {
  238. return json_encode($this->toArray($includeSystemAttributes));
  239. }
  240. /**
  241. * Gets node attributes.
  242. *
  243. * The array contains all attributes in its internal format (no conversion).
  244. *
  245. * This is an offline method.
  246. *
  247. * @param boolean $includeSystemAttributes
  248. * @return array
  249. */
  250. public function getData($includeSystemAttributes = true)
  251. {
  252. if ($includeSystemAttributes === false) {
  253. $data = array();
  254. foreach ($this->_currentData as $key => $value) {
  255. if (!in_array($key, self::$_systemAttributes)) {
  256. $data[$key] = $value;
  257. }
  258. }
  259. return $data;
  260. } else {
  261. return $this->_currentData;
  262. }
  263. }
  264. /**
  265. * Checks whether a given attribute exists.
  266. *
  267. * If $emptyExists is false empty attributes (containing only array()) are
  268. * treated as non-existent returning false.
  269. * If $emptyExists is true empty attributes are treated as existent returning
  270. * true. In this case method returns false only if the attribute name is
  271. * missing in the key-collection.
  272. *
  273. * @param string $name
  274. * @param boolean $emptyExists
  275. * @return boolean
  276. */
  277. public function existsAttribute($name, $emptyExists = false)
  278. {
  279. $name = strtolower($name);
  280. if (isset($this->_currentData[$name])) {
  281. if ($emptyExists) return true;
  282. return count($this->_currentData[$name])>0;
  283. }
  284. else return false;
  285. }
  286. /**
  287. * Checks if the given value(s) exist in the attribute
  288. *
  289. * @param string $attribName
  290. * @param mixed|array $value
  291. * @return boolean
  292. */
  293. public function attributeHasValue($attribName, $value)
  294. {
  295. return Zend_Ldap_Attribute::attributeHasValue($this->_currentData, $attribName, $value);
  296. }
  297. /**
  298. * Gets a LDAP attribute.
  299. *
  300. * This is an offline method.
  301. *
  302. * @param string $name
  303. * @param integer $index
  304. * @return mixed
  305. * @throws Zend_Ldap_Exception
  306. */
  307. public function getAttribute($name, $index = null)
  308. {
  309. if ($name == 'dn') {
  310. return $this->getDnString();
  311. }
  312. else {
  313. return Zend_Ldap_Attribute::getAttribute($this->_currentData, $name, $index);
  314. }
  315. }
  316. /**
  317. * Gets a LDAP date/time attribute.
  318. *
  319. * This is an offline method.
  320. *
  321. * @param string $name
  322. * @param integer $index
  323. * @return array|integer
  324. * @throws Zend_Ldap_Exception
  325. */
  326. public function getDateTimeAttribute($name, $index = null)
  327. {
  328. return Zend_Ldap_Attribute::getDateTimeAttribute($this->_currentData, $name, $index);
  329. }
  330. /**
  331. * Sets a LDAP attribute.
  332. *
  333. * This is an offline method.
  334. *
  335. * @param string $name
  336. * @param mixed $value
  337. * @return null
  338. * @throws BadMethodCallException
  339. */
  340. public function __set($name, $value)
  341. {
  342. throw new BadMethodCallException();
  343. }
  344. /**
  345. * Gets a LDAP attribute.
  346. *
  347. * This is an offline method.
  348. *
  349. * @param string $name
  350. * @return array
  351. * @throws Zend_Ldap_Exception
  352. */
  353. public function __get($name)
  354. {
  355. return $this->getAttribute($name, null);
  356. }
  357. /**
  358. * Deletes a LDAP attribute.
  359. *
  360. * This method deletes the attribute.
  361. *
  362. * This is an offline method.
  363. *
  364. * @param string $name
  365. * @return null
  366. * @throws BadMethodCallException
  367. */
  368. public function __unset($name)
  369. {
  370. throw new BadMethodCallException();
  371. }
  372. /**
  373. * Checks whether a given attribute exists.
  374. *
  375. * Empty attributes will be treated as non-existent.
  376. *
  377. * @param string $name
  378. * @return boolean
  379. */
  380. public function __isset($name)
  381. {
  382. return $this->existsAttribute($name, false);
  383. }
  384. /**
  385. * Sets a LDAP attribute.
  386. * Implements ArrayAccess.
  387. *
  388. * This is an offline method.
  389. *
  390. * @param string $name
  391. * @param mixed $value
  392. * @return null
  393. * @throws BadMethodCallException
  394. */
  395. public function offsetSet($name, $value)
  396. {
  397. throw new BadMethodCallException();
  398. }
  399. /**
  400. * Gets a LDAP attribute.
  401. * Implements ArrayAccess.
  402. *
  403. * This is an offline method.
  404. *
  405. * @param string $name
  406. * @return array
  407. * @throws Zend_Ldap_Exception
  408. */
  409. public function offsetGet($name)
  410. {
  411. return $this->getAttribute($name, null);
  412. }
  413. /**
  414. * Deletes a LDAP attribute.
  415. * Implements ArrayAccess.
  416. *
  417. * This method deletes the attribute.
  418. *
  419. * This is an offline method.
  420. *
  421. * @param string $name
  422. * @return null
  423. * @throws BadMethodCallException
  424. */
  425. public function offsetUnset($name)
  426. {
  427. throw new BadMethodCallException();
  428. }
  429. /**
  430. * Checks whether a given attribute exists.
  431. * Implements ArrayAccess.
  432. *
  433. * Empty attributes will be treated as non-existent.
  434. *
  435. * @param string $name
  436. * @return boolean
  437. */
  438. public function offsetExists($name)
  439. {
  440. return $this->existsAttribute($name, false);
  441. }
  442. /**
  443. * Returns the number of attributes in node.
  444. * Implements Countable
  445. *
  446. * @return int
  447. */
  448. public function count()
  449. {
  450. return count($this->_currentData);
  451. }
  452. }