Net_LDAP
[ class tree: Net_LDAP ] [ index: Net_LDAP ] [ all elements ]

Source for file Schema.php

Documentation is available at Schema.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +--------------------------------------------------------------------------+
  4. // | Net_LDAP                                                                 |
  5. // +--------------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                    |
  7. // +--------------------------------------------------------------------------+
  8. // | This library is free software; you can redistribute it and/or            |
  9. // | modify it under the terms of the GNU Lesser General Public               |
  10. // | License as published by the Free Software Foundation; either             |
  11. // | version 2.1 of the License, or (at your option) any later version.       |
  12. // |                                                                          |
  13. // | This library is distributed in the hope that it will be useful,          |
  14. // | but WITHOUT ANY WARRANTY; without even the implied warranty of           |
  15. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU        |
  16. // | Lesser General Public License for more details.                          |
  17. // |                                                                          |
  18. // | You should have received a copy of the GNU Lesser General Public         |
  19. // | License along with this library; if not, write to the Free Software      |
  20. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA |
  21. // +--------------------------------------------------------------------------+
  22. // | Authors: Jan Wagner                                                      |
  23. // +--------------------------------------------------------------------------+
  24. //
  25. // $Id: Schema.php,v 1.7.2.2 2005/03/01 12:27:43 jw Exp $
  26.  
  27. /**
  28.  * Load an LDAP Schema and provide information
  29.  *
  30.  * This class takes a Subschema entry, parses this information
  31.  * and makes it available in an array. Most of the code has been
  32.  * inspired by perl-ldap( http://perl-ldap.sourceforge.net).
  33.  * You will find portions of their implementation in here.
  34.  *
  35.  * @package Net_LDAP
  36.  * @author Jan Wagner <wagner@netsols.de>
  37.  * @version $Revision: 1.7.2.2 $
  38.  */
  39.  class Net_LDAP_Schema extends PEAR
  40.  {    
  41.     /**
  42.      * Map of entry types to ldap attributes of subschema entry
  43.      *
  44.      * @access public
  45.      * @var array 
  46.      */
  47.     var $types = array('attribute'        => 'attributeTypes',
  48.                        'ditcontentrule'   => 'dITContentRules',
  49.                        'ditstructurerule' => 'dITStructureRules',
  50.                        'matchingrule'     => 'matchingRules',
  51.                        'matchingruleuse'  => 'matchingRuleUse',
  52.                        'nameform'         => 'nameForms',
  53.                        'objectclass'      => 'objectClasses',
  54.                        'syntax'           => 'ldapSyntaxes');
  55.  
  56.     /**#@+
  57.      * Array of entries belonging to this type
  58.      *
  59.      * @access private
  60.      * @var array
  61.      */
  62.     var $_attributeTypes    = array();
  63.     var $_matchingRules     = array();
  64.     var $_matchingRuleUse   = array();
  65.     var $_ldapSyntaxes      = array();
  66.     var $_objectClasses     = array();
  67.     var $_dITContentRules   = array();
  68.     var $_dITStructureRules = array();
  69.     var $_nameForms         = array();
  70.     /**#@-*/
  71.  
  72.     /**
  73.      * hash of all fetched oids
  74.      *
  75.      * @access private
  76.      * @var array 
  77.      */
  78.     var $_oids = array();
  79.         
  80.     /**
  81.      * constructor of the class
  82.      *
  83.      * @access protected
  84.      */       
  85.     function Net_LDAP_Schema()
  86.     {
  87.         $this->PEAR('Net_LDAP_Error')// default error class
  88.     }
  89.  
  90.     /**
  91.      * Return a hash of entries for the given type
  92.      *
  93.      * Returns a hash of entry for th givene type. Types may be:
  94.      * objectclasses, attributes, ditcontentrules, ditstructurerules, matchingrules,
  95.      * matchingruleuses, nameforms, syntaxes
  96.      *
  97.      * @access public
  98.      * @param string Type to fetch
  99.      * @return mixed Array or Net_LDAP_Error
  100.      */
  101.     function &getAll($type)
  102.     {
  103.         $map = array('objectclasses'     => &$this->_objectClasses,
  104.                      'attributes'        => &$this->_attributeTypes,
  105.                      'ditcontentrules'   => &$this->_dITContentRules,
  106.                      'ditstructurerules' => &$this->_dITStructureRules,
  107.                      'matchingrules'     => &$this->_matchingRules,
  108.                      'matchingruleuses'  => &$this->_matchingRuleUse,
  109.                      'nameforms'         => &$this->_nameForms,
  110.                      'syntaxes'          => &$this->_ldapSyntaxes );
  111.  
  112.         $key strtolower($type);
  113.         return ((key_exists($key$map)) $map[$key$this->raiseError("Unknown type $type"));
  114.     }
  115.     
  116.     /**
  117.      * Return a specific entry
  118.      *
  119.      * @access public
  120.      * @param string Type of name
  121.      * @param string Name or OID to fetch
  122.      * @return mixed Entry or Net_LDAP_Error
  123.      */
  124.      function &get($type$name)
  125.      {
  126.         $type strtolower($type);
  127.         if (false == key_exists($type$this->types)) {
  128.             return $this->raiseError("No such type $type");
  129.         }
  130.  
  131.         $name strtolower($name);
  132.         $type_var &$this->{'_' $this->types[$type]};
  133.         
  134.         ifkey_exists($name$type_var)) {
  135.             return $type_var[$name];
  136.         elseif(key_exists($name$this->_oids&& $this->_oids[$name]['type'== $type{
  137.             return $this->_oids[$name];
  138.         else {
  139.             return $this->raiseError("Could not find $type $name");
  140.         }
  141.      }
  142.  
  143.      
  144.     /**
  145.      * Fetches attributes that MAY be present in the given objectclass
  146.      *
  147.      * @access public
  148.      * @param string Name or OID of objectclass
  149.      * @return mixed Array with attributes or Net_LDAP_Error
  150.      */
  151.     function may($oc)
  152.     {
  153.         return $this->_getAttr($oc'may');
  154.     }
  155.     
  156.     /**
  157.      * Fetches attributes that MUST be present in the given objectclass
  158.      *
  159.      * @access public
  160.      * @param string Name or OID of objectclass
  161.      * @return mixed Array with attributes or Net_LDAP_Error
  162.      */
  163.     function must($oc)
  164.     {
  165.         return $this->_getAttr($oc'must');
  166.     }
  167.      
  168.     /**
  169.      * Fetches the given attribute from the given objectclass
  170.      *
  171.      * @access private
  172.      * @param string Name or OID of objectclass
  173.      * @param string Name of attribute to fetch
  174.      * @return mixed The attribute or Net_LDAP_Error
  175.      */
  176.     function _getAttr($oc$attr)
  177.     {
  178.         $oc strtolower($oc);
  179.         if (key_exists($oc$this->_objectClasses&& key_exists($attr$this->_objectClasses[$oc])) {
  180.             return $this->_objectClasses[$oc][$attr];
  181.         }
  182.         elseif (key_exists($oc$this->_oids&&
  183.                 $this->_oids[$oc]['type'== 'objectclass' &&
  184.                 key_exists($attr$this->_oids[$oc])) {
  185.             return $this->_oids[$oc][$attr];
  186.         else {
  187.             return $this->raiseError("Could not find $attr attributes for $oc ");
  188.         }
  189.     }
  190.     
  191.     /**
  192.      * Returns the name(s) of the immediate superclass(es)
  193.      *
  194.      * @param string Name or OID of objectclass
  195.      * @return mixed Array of names or Net_LDAP_Error
  196.      */
  197.     function superclass($oc)
  198.     {        
  199.         $o $this->get('objectclass'$oc);
  200.         if (Net_LDAP::isError($o)) {
  201.             return $o;
  202.         }
  203.         return (key_exists('sup'$o$o['sup': array());
  204.     }
  205.  
  206.     /**
  207.      * Parses the schema of the given Subschema entry
  208.      *
  209.      * @access public
  210.      * @param object Net_LDAP_Entry Subschema entry
  211.      */
  212.     function parse(&$entry)
  213.     {
  214.         foreach ($this->types as $type => $attr)
  215.         {
  216.             // initialize map type to entry
  217.             $type_var '_' $attr;
  218.             $this->{$type_var= array();
  219.             
  220.             // get values for this type
  221.             $values $entry->get_value($attr);
  222.                         
  223.             if (is_array($values))
  224.             {
  225.                 foreach ($values as $value{
  226.                     
  227.                     unset($schema_entry)// this was a real mess without it
  228.                                         
  229.                     // get the schema entry
  230.                     $schema_entry $this->_parse_entry($value);
  231.  
  232.                     // set the type
  233.                     $schema_entry['type'$type;
  234.                     
  235.                     // save a ref in $_oids
  236.                     $this->_oids[$schema_entry['oid']] =$schema_entry;                    
  237.                     
  238.                     // save refs for all names in type map
  239.                     $names $schema_entry['aliases'];
  240.                     array_push($names$schema_entry['name']);
  241.                     foreach ($names as $name{
  242.                         $this->{$type_var}[strtolower($name)=$schema_entry;
  243.                     }
  244.                 }
  245.             }
  246.         }
  247.     }
  248.     
  249.     /**
  250.      * parses an attribute value into a schema entry
  251.      *
  252.      * @access private
  253.      * @param string Attribute value
  254.      * @return mixed Schema entry array or false
  255.      */
  256.     function &_parse_entry($value)
  257.     {
  258.         // tokens that have no value associated
  259.         $noValue = array('single-value',
  260.                          'obsolete',
  261.                          'collective',
  262.                          'no-user-modification',
  263.                          'abstract',
  264.                          'structural',
  265.                          'auxiliary');
  266.         
  267.         // tokens that can have multiple values
  268.         $multiValue = array('must''may''sup');
  269.         
  270.         $schema_entry = array('aliases' => array())// initilization
  271.         
  272.         $tokens $this->_tokenize($value)// get an array of tokens
  273.        
  274.         // remove surrounding brackets
  275.         if ($tokens[0== '('array_shift($tokens);
  276.         if ($tokens[count($tokens- 1== ')'array_pop($tokens)// -1 doesnt work on arrays :-(
  277.  
  278.         $schema_entry['oid'array_shift($tokens)// first token is the oid
  279.         
  280.         // cycle over the tokens until none are left
  281.         while (count($tokens> 0{
  282.             $token strtolower(array_shift($tokens));
  283.             if (in_array($token$noValue)) {
  284.                 $schema_entry[$token= 1; // single value token
  285.             else {
  286.                 // this one follows a string or a list if it is multivalued
  287.                 if (($schema_entry[$tokenarray_shift($tokens)) == '('{
  288.                     // this creates the list of values and cycles through the tokens
  289.                     // until the end of the list is reached ')'
  290.                     $schema_entry[$token= array();
  291.                     while ($tmp array_shift($tokens)) {
  292.                         if ($tmp == ')'break;
  293.                         if ($tmp != '$'array_push($schema_entry[$token]$tmp);
  294.                     }
  295.                 }
  296.                 // create a array if the value should be multivalued but was not
  297.                 if (in_array($token$multiValue && !is_array($schema_entry[$token])) {
  298.                     $schema_entry[$token= array($schema_entry[$token]);
  299.                 }
  300.             }
  301.         }        
  302.         // get max length from syntax        
  303.         if (key_exists('syntax'$schema_entry)) {
  304.             if (preg_match('/{(\d+)}/'$schema_entry['syntax']$matches)) {
  305.                 $schema_entry['max_length'$matches[1];
  306.             }
  307.         }
  308.         // force a name
  309.         if (empty($schema_entry['name'])) {
  310.             $schema_entry['name'$schema_entry['oid'];
  311.         }        
  312.         // make one name the default and put the other ones into aliases
  313.         if (is_array($schema_entry['name'])) {
  314.             $aliases $schema_entry['name'];
  315.             $schema_entry['name'array_shift($aliases);
  316.             $schema_entry['aliases'$aliases;
  317.         }        
  318.         return $schema_entry;
  319.     }
  320.     
  321.     /**
  322.      * tokenizes the given value into an array of tokens
  323.      *
  324.      * @access private
  325.      * @param string String to parse
  326.      * @return array Array of tokens
  327.      */
  328.     function _tokenize($value)
  329.     {
  330.         $tokens = array();        // array of tokens
  331.         $matches = array();       // matches[0] full pattern match, [1,2,3] subpatterns
  332.         
  333.         // this one is taken from perl-ldap, modified for php
  334.         $pattern "/\s* (?:([()]) | ([^'\s()]+) | '((?:[^']+|'[^\s)])*)') \s*/x";
  335.         
  336.         /**
  337.          * This one matches one big pattern wherin only one of the three subpatterns matched
  338.          * We are interested in the subpatterns that matched. If it matched its value will be
  339.          * non-empty and so it is a token. Tokens may be round brackets, a string, or a string
  340.          * enclosed by '
  341.          */
  342.         preg_match_all($pattern$value$matches);
  343.  
  344.         for ($i = 0; $i count($matches[0])$i++{     // number of tokens (full pattern match)
  345.             for ($j = 1; $j < 4; $j++{                  // each subpattern
  346.                 if (null != trim($matches[$j][$i])) {     // pattern match in this subpattern
  347.                     $tokens[$itrim($matches[$j][$i])// this is the token
  348.                 }
  349.             }
  350.         }
  351.         return $tokens;
  352.     }
  353.  }
  354.  
  355. ?>

Documentation generated on Mon, 11 Mar 2019 14:26:18 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.