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