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

/src/application/libraries/Zend/Ldap/Node/Schema/OpenLdap.php

https://bitbucket.org/masnug/grc276-blog-laravel
PHP | 502 lines | 278 code | 39 blank | 185 comment | 27 complexity | 710ea811da2c6ef958ae5fb34a988f3b 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 Schema
  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: OpenLdap.php 23775 2011-03-01 17:25:24Z ralph $
  21. */
  22. /**
  23. * @see Zend_Ldap_Node_Schema
  24. */
  25. require_once 'Zend/Ldap/Node/Schema.php';
  26. /**
  27. * @see Zend_Ldap_Node_Schema_AttributeType_OpenLdap
  28. */
  29. require_once 'Zend/Ldap/Node/Schema/AttributeType/OpenLdap.php';
  30. /**
  31. * @see Zend_Ldap_Node_Schema_ObjectClass_OpenLdap
  32. */
  33. require_once 'Zend/Ldap/Node/Schema/ObjectClass/OpenLdap.php';
  34. /**
  35. * Zend_Ldap_Node_Schema_OpenLdap provides a simple data-container for the Schema node of
  36. * an OpenLDAP server.
  37. *
  38. * @category Zend
  39. * @package Zend_Ldap
  40. * @subpackage Schema
  41. * @copyright Copyright (c) 2005-2011 Zend Technologies USA Inc. (http://www.zend.com)
  42. * @license http://framework.zend.com/license/new-bsd New BSD License
  43. */
  44. class Zend_Ldap_Node_Schema_OpenLdap extends Zend_Ldap_Node_Schema
  45. {
  46. /**
  47. * The attribute Types
  48. *
  49. * @var array
  50. */
  51. protected $_attributeTypes = null;
  52. /**
  53. * The object classes
  54. *
  55. * @var array
  56. */
  57. protected $_objectClasses = null;
  58. /**
  59. * The LDAP syntaxes
  60. *
  61. * @var array
  62. */
  63. protected $_ldapSyntaxes = null;
  64. /**
  65. * The matching rules
  66. *
  67. * @var array
  68. */
  69. protected $_matchingRules = null;
  70. /**
  71. * The matching rule use
  72. *
  73. * @var array
  74. */
  75. protected $_matchingRuleUse = null;
  76. /**
  77. * Parses the schema
  78. *
  79. * @param Zend_Ldap_Dn $dn
  80. * @param Zend_Ldap $ldap
  81. * @return Zend_Ldap_Node_Schema Provides a fluid interface
  82. */
  83. protected function _parseSchema(Zend_Ldap_Dn $dn, Zend_Ldap $ldap)
  84. {
  85. parent::_parseSchema($dn, $ldap);
  86. $this->_loadAttributeTypes();
  87. $this->_loadLdapSyntaxes();
  88. $this->_loadMatchingRules();
  89. $this->_loadMatchingRuleUse();
  90. $this->_loadObjectClasses();
  91. return $this;
  92. }
  93. /**
  94. * Gets the attribute Types
  95. *
  96. * @return array
  97. */
  98. public function getAttributeTypes()
  99. {
  100. return $this->_attributeTypes;
  101. }
  102. /**
  103. * Gets the object classes
  104. *
  105. * @return array
  106. */
  107. public function getObjectClasses()
  108. {
  109. return $this->_objectClasses;
  110. }
  111. /**
  112. * Gets the LDAP syntaxes
  113. *
  114. * @return array
  115. */
  116. public function getLdapSyntaxes()
  117. {
  118. return $this->_ldapSyntaxes;
  119. }
  120. /**
  121. * Gets the matching rules
  122. *
  123. * @return array
  124. */
  125. public function getMatchingRules()
  126. {
  127. return $this->_matchingRules;
  128. }
  129. /**
  130. * Gets the matching rule use
  131. *
  132. * @return array
  133. */
  134. public function getMatchingRuleUse()
  135. {
  136. return $this->_matchingRuleUse;
  137. }
  138. /**
  139. * Loads the attribute Types
  140. *
  141. * @return void
  142. */
  143. protected function _loadAttributeTypes()
  144. {
  145. $this->_attributeTypes = array();
  146. foreach ($this->getAttribute('attributeTypes') as $value) {
  147. $val = $this->_parseAttributeType($value);
  148. $val = new Zend_Ldap_Node_Schema_AttributeType_OpenLdap($val);
  149. $this->_attributeTypes[$val->getName()] = $val;
  150. }
  151. foreach ($this->_attributeTypes as $val) {
  152. if (count($val->sup) > 0) {
  153. $this->_resolveInheritance($val, $this->_attributeTypes);
  154. }
  155. foreach ($val->aliases as $alias) {
  156. $this->_attributeTypes[$alias] = $val;
  157. }
  158. }
  159. ksort($this->_attributeTypes, SORT_STRING);
  160. }
  161. /**
  162. * Parses an attributeType value
  163. *
  164. * @param string $value
  165. * @return array
  166. */
  167. protected function _parseAttributeType($value)
  168. {
  169. $attributeType = array(
  170. 'oid' => null,
  171. 'name' => null,
  172. 'desc' => null,
  173. 'obsolete' => false,
  174. 'sup' => null,
  175. 'equality' => null,
  176. 'ordering' => null,
  177. 'substr' => null,
  178. 'syntax' => null,
  179. 'max-length' => null,
  180. 'single-value' => false,
  181. 'collective' => false,
  182. 'no-user-modification' => false,
  183. 'usage' => 'userApplications',
  184. '_string' => $value,
  185. '_parents' => array());
  186. $tokens = $this->_tokenizeString($value);
  187. $attributeType['oid'] = array_shift($tokens); // first token is the oid
  188. $this->_parseLdapSchemaSyntax($attributeType, $tokens);
  189. if (array_key_exists('syntax', $attributeType)) {
  190. // get max length from syntax
  191. if (preg_match('/^(.+){(\d+)}$/', $attributeType['syntax'], $matches)) {
  192. $attributeType['syntax'] = $matches[1];
  193. $attributeType['max-length'] = $matches[2];
  194. }
  195. }
  196. $this->_ensureNameAttribute($attributeType);
  197. return $attributeType;
  198. }
  199. /**
  200. * Loads the object classes
  201. *
  202. * @return void
  203. */
  204. protected function _loadObjectClasses()
  205. {
  206. $this->_objectClasses = array();
  207. foreach ($this->getAttribute('objectClasses') as $value) {
  208. $val = $this->_parseObjectClass($value);
  209. $val = new Zend_Ldap_Node_Schema_ObjectClass_OpenLdap($val);
  210. $this->_objectClasses[$val->getName()] = $val;
  211. }
  212. foreach ($this->_objectClasses as $val) {
  213. if (count($val->sup) > 0) {
  214. $this->_resolveInheritance($val, $this->_objectClasses);
  215. }
  216. foreach ($val->aliases as $alias) {
  217. $this->_objectClasses[$alias] = $val;
  218. }
  219. }
  220. ksort($this->_objectClasses, SORT_STRING);
  221. }
  222. /**
  223. * Parses an objectClasses value
  224. *
  225. * @param string $value
  226. * @return array
  227. */
  228. protected function _parseObjectClass($value)
  229. {
  230. $objectClass = array(
  231. 'oid' => null,
  232. 'name' => null,
  233. 'desc' => null,
  234. 'obsolete' => false,
  235. 'sup' => array(),
  236. 'abstract' => false,
  237. 'structural' => false,
  238. 'auxiliary' => false,
  239. 'must' => array(),
  240. 'may' => array(),
  241. '_string' => $value,
  242. '_parents' => array());
  243. $tokens = $this->_tokenizeString($value);
  244. $objectClass['oid'] = array_shift($tokens); // first token is the oid
  245. $this->_parseLdapSchemaSyntax($objectClass, $tokens);
  246. $this->_ensureNameAttribute($objectClass);
  247. return $objectClass;
  248. }
  249. /**
  250. * Resolves inheritance in objectClasses and attributes
  251. *
  252. * @param Zend_Ldap_Node_Schema_Item $node
  253. * @param array $repository
  254. */
  255. protected function _resolveInheritance(Zend_Ldap_Node_Schema_Item $node, array $repository)
  256. {
  257. $data = $node->getData();
  258. $parents = $data['sup'];
  259. if ($parents === null || !is_array($parents) || count($parents) < 1) return;
  260. foreach ($parents as $parent) {
  261. if (!array_key_exists($parent, $repository)) continue;
  262. if (!array_key_exists('_parents', $data) || !is_array($data['_parents'])) {
  263. $data['_parents'] = array();
  264. }
  265. $data['_parents'][] = $repository[$parent];
  266. }
  267. $node->setData($data);
  268. }
  269. /**
  270. * Loads the LDAP syntaxes
  271. *
  272. * @return void
  273. */
  274. protected function _loadLdapSyntaxes()
  275. {
  276. $this->_ldapSyntaxes = array();
  277. foreach ($this->getAttribute('ldapSyntaxes') as $value) {
  278. $val = $this->_parseLdapSyntax($value);
  279. $this->_ldapSyntaxes[$val['oid']] = $val;
  280. }
  281. ksort($this->_ldapSyntaxes, SORT_STRING);
  282. }
  283. /**
  284. * Parses an ldapSyntaxes value
  285. *
  286. * @param string $value
  287. * @return array
  288. */
  289. protected function _parseLdapSyntax($value)
  290. {
  291. $ldapSyntax = array(
  292. 'oid' => null,
  293. 'desc' => null,
  294. '_string' => $value);
  295. $tokens = $this->_tokenizeString($value);
  296. $ldapSyntax['oid'] = array_shift($tokens); // first token is the oid
  297. $this->_parseLdapSchemaSyntax($ldapSyntax, $tokens);
  298. return $ldapSyntax;
  299. }
  300. /**
  301. * Loads the matching rules
  302. *
  303. * @return void
  304. */
  305. protected function _loadMatchingRules()
  306. {
  307. $this->_matchingRules = array();
  308. foreach ($this->getAttribute('matchingRules') as $value) {
  309. $val = $this->_parseMatchingRule($value);
  310. $this->_matchingRules[$val['name']] = $val;
  311. }
  312. ksort($this->_matchingRules, SORT_STRING);
  313. }
  314. /**
  315. * Parses an matchingRules value
  316. *
  317. * @param string $value
  318. * @return array
  319. */
  320. protected function _parseMatchingRule($value)
  321. {
  322. $matchingRule = array(
  323. 'oid' => null,
  324. 'name' => null,
  325. 'desc' => null,
  326. 'obsolete' => false,
  327. 'syntax' => null,
  328. '_string' => $value);
  329. $tokens = $this->_tokenizeString($value);
  330. $matchingRule['oid'] = array_shift($tokens); // first token is the oid
  331. $this->_parseLdapSchemaSyntax($matchingRule, $tokens);
  332. $this->_ensureNameAttribute($matchingRule);
  333. return $matchingRule;
  334. }
  335. /**
  336. * Loads the matching rule use
  337. *
  338. * @return void
  339. */
  340. protected function _loadMatchingRuleUse()
  341. {
  342. $this->_matchingRuleUse = array();
  343. foreach ($this->getAttribute('matchingRuleUse') as $value) {
  344. $val = $this->_parseMatchingRuleUse($value);
  345. $this->_matchingRuleUse[$val['name']] = $val;
  346. }
  347. ksort($this->_matchingRuleUse, SORT_STRING);
  348. }
  349. /**
  350. * Parses an matchingRuleUse value
  351. *
  352. * @param string $value
  353. * @return array
  354. */
  355. protected function _parseMatchingRuleUse($value)
  356. {
  357. $matchingRuleUse = array(
  358. 'oid' => null,
  359. 'name' => null,
  360. 'desc' => null,
  361. 'obsolete' => false,
  362. 'applies' => array(),
  363. '_string' => $value);
  364. $tokens = $this->_tokenizeString($value);
  365. $matchingRuleUse['oid'] = array_shift($tokens); // first token is the oid
  366. $this->_parseLdapSchemaSyntax($matchingRuleUse, $tokens);
  367. $this->_ensureNameAttribute($matchingRuleUse);
  368. return $matchingRuleUse;
  369. }
  370. /**
  371. * Ensures that a name element is present and that it is single-values.
  372. *
  373. * @param array $data
  374. */
  375. protected function _ensureNameAttribute(array &$data)
  376. {
  377. if (!array_key_exists('name', $data) || empty($data['name'])) {
  378. // force a name
  379. $data['name'] = $data['oid'];
  380. }
  381. if (is_array($data['name'])) {
  382. // make one name the default and put the other ones into aliases
  383. $aliases = $data['name'];
  384. $data['name'] = array_shift($aliases);
  385. $data['aliases'] = $aliases;
  386. } else {
  387. $data['aliases'] = array();
  388. }
  389. }
  390. /**
  391. * Parse the given tokens into a data structure
  392. *
  393. * @param array $data
  394. * @param array $tokens
  395. * @return void
  396. */
  397. protected function _parseLdapSchemaSyntax(array &$data, array $tokens)
  398. {
  399. // tokens that have no value associated
  400. $noValue = array('single-value',
  401. 'obsolete',
  402. 'collective',
  403. 'no-user-modification',
  404. 'abstract',
  405. 'structural',
  406. 'auxiliary');
  407. // tokens that can have multiple values
  408. $multiValue = array('must', 'may', 'sup');
  409. while (count($tokens) > 0) {
  410. $token = strtolower(array_shift($tokens));
  411. if (in_array($token, $noValue)) {
  412. $data[$token] = true; // single value token
  413. } else {
  414. $data[$token] = array_shift($tokens);
  415. // this one follows a string or a list if it is multivalued
  416. if ($data[$token] == '(') {
  417. // this creates the list of values and cycles through the tokens
  418. // until the end of the list is reached ')'
  419. $data[$token] = array();
  420. while ($tmp = array_shift($tokens)) {
  421. if ($tmp == ')') break;
  422. if ($tmp != '$') {
  423. $data[$token][] = Zend_Ldap_Attribute::convertFromLdapValue($tmp);
  424. }
  425. }
  426. } else {
  427. $data[$token] = Zend_Ldap_Attribute::convertFromLdapValue($data[$token]);
  428. }
  429. // create a array if the value should be multivalued but was not
  430. if (in_array($token, $multiValue) && !is_array($data[$token])) {
  431. $data[$token] = array($data[$token]);
  432. }
  433. }
  434. }
  435. }
  436. /**
  437. * Tokenizes the given value into an array
  438. *
  439. * @param string $value
  440. * @return array tokens
  441. */
  442. protected function _tokenizeString($value)
  443. {
  444. $tokens = array();
  445. $matches = array();
  446. // this one is taken from PEAR::Net_LDAP2
  447. $pattern = "/\s* (?:([()]) | ([^'\s()]+) | '((?:[^']+|'[^\s)])*)') \s*/x";
  448. preg_match_all($pattern, $value, $matches);
  449. $cMatches = count($matches[0]);
  450. $cPattern = count($matches);
  451. for ($i = 0; $i < $cMatches; $i++) { // number of tokens (full pattern match)
  452. for ($j = 1; $j < $cPattern; $j++) { // each subpattern
  453. $tok = trim($matches[$j][$i]);
  454. if (!empty($tok)) { // pattern match in this subpattern
  455. $tokens[$i] = $tok; // this is the token
  456. }
  457. }
  458. }
  459. if ($tokens[0] == '(') array_shift($tokens);
  460. if ($tokens[count($tokens) - 1] == ')') array_pop($tokens);
  461. return $tokens;
  462. }
  463. }