]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/LdapCommon/extlib/Net/LDAP2/Schema.php
Updated LDAP2 extlib to latest version.
[quix0rs-gnu-social.git] / plugins / LdapCommon / extlib / Net / LDAP2 / Schema.php
index b590eabc51c2de70432f3e1713ecfe6af19279bd..7eb15662eb442a710588056b4e6f349426b9cfb0 100644 (file)
@@ -11,7 +11,7 @@
 * @author    Benedikt Hallinger <beni@php.net>
 * @copyright 2009 Jan Wagner, Benedikt Hallinger
 * @license   http://www.gnu.org/licenses/lgpl-3.0.txt LGPLv3
-* @version   SVN: $Id: Schema.php 286718 2009-08-03 07:30:49Z beni $
+* @version   SVN: $Id: Schema.php 296515 2010-03-22 14:46:41Z beni $
 * @link      http://pear.php.net/package/Net_LDAP2/
 * @todo see the comment at the end of the file
 */
@@ -168,12 +168,16 @@ class Net_LDAP2_Schema extends PEAR
                                 array('attributes' => array_values($schema_o->types),
                                         'scope' => 'base'));
         if (Net_LDAP2::isError($result)) {
-            return $result;
+            return PEAR::raiseError('Could not fetch Subschema entry: '.$result->getMessage());
         }
 
         $entry = $result->shiftEntry();
         if (!$entry instanceof Net_LDAP2_Entry) {
-            return PEAR::raiseError('Could not fetch Subschema entry');
+            if ($entry instanceof Net_LDAP2_Error) {
+                return PEAR::raiseError('Could not fetch Subschema entry: '.$entry->getMessage());
+            } else {
+                return PEAR::raiseError('Could not fetch Subschema entry (search returned '.$result->count().' entries. Check parameter \'basedn\')');
+            }
         }
 
         $schema_o->parse($entry);
@@ -183,7 +187,7 @@ class Net_LDAP2_Schema extends PEAR
     /**
     * Return a hash of entries for the given type
     *
-    * Returns a hash of entry for th givene type. Types may be:
+    * Returns a hash of entry for the givene type. Types may be:
     * objectclasses, attributes, ditcontentrules, ditstructurerules, matchingrules,
     * matchingruleuses, nameforms, syntaxes
     *
@@ -508,9 +512,111 @@ class Net_LDAP2_Schema extends PEAR
         return $return;
     }
 
-    // [TODO] add method that allows us to see to which objectclasses a certain attribute belongs to
-    // it should return the result structured, e.g. sorted in "may" and "must". Optionally it should
-    // be able to return it just "flat", e.g. array_merge()d.
-    // We could use get_all() to achieve this easily, i think
+    /**
+    * See if an schema element exists
+    *
+    * @param string $type Type of name, see get()
+    * @param string $name Name or OID
+    *
+    * @return boolean
+    */
+    public function exists($type, $name)
+    {
+        $entry = $this->get($type, $name);
+        if ($entry instanceof Net_LDAP2_ERROR) {
+                return false;
+        } else {
+            return true;
+        }
+    }
+
+    /**
+    * See if an attribute is defined in the schema
+    *
+    * @param string $attribute Name or OID of the attribute
+    * @return boolean
+    */
+    public function attributeExists($attribute)
+    {
+        return $this->exists('attribute', $attribute);
+    }
+
+    /**
+    * See if an objectClass is defined in the schema
+    *
+    * @param string $ocl Name or OID of the objectClass
+    * @return boolean
+    */
+    public function objectClassExists($ocl)
+    {
+        return $this->exists('objectclass', $ocl);
+    }
+
+
+    /**
+    * See to which ObjectClasses an attribute is assigned
+    *
+    * The objectclasses are sorted into the keys 'may' and 'must'.
+    *
+    * @param string $attribute Name or OID of the attribute
+    *
+    * @return array|Net_LDAP2_Error Associative array with OCL names or Error
+    */
+    public function getAssignedOCLs($attribute)
+    {
+        $may  = array();
+        $must = array();
+
+        // Test if the attribute type is defined in the schema,
+        // if so, retrieve real name for lookups
+        $attr_entry = $this->get('attribute', $attribute);
+        if ($attr_entry instanceof Net_LDAP2_ERROR) {
+            return PEAR::raiseError("Attribute $attribute not defined in schema: ".$attr_entry->getMessage());
+        } else {
+            $attribute = $attr_entry['name'];
+        }
+
+
+        // We need to get all defined OCLs for this.
+        $ocls = $this->getAll('objectclasses');
+        foreach ($ocls as $ocl => $ocl_data) {
+            // Fetch the may and must attrs and see if our searched attr is contained.
+            // If so, record it in the corresponding array.
+            $ocl_may_attrs  = $this->may($ocl);
+            $ocl_must_attrs = $this->must($ocl);
+            if (is_array($ocl_may_attrs) && in_array($attribute, $ocl_may_attrs)) {
+                array_push($may, $ocl_data['name']);
+            }
+            if (is_array($ocl_must_attrs) && in_array($attribute, $ocl_must_attrs)) {
+                array_push($must, $ocl_data['name']);
+            }
+        }
+
+        return array('may' => $may, 'must' => $must);
+    }
+
+    /**
+    * See if an attribute is available in a set of objectClasses
+    *
+    * @param string $attribute Attribute name or OID
+    * @param array $ocls       Names of OCLs to check for
+    *
+    * @return boolean TRUE, if the attribute is defined for at least one of the OCLs
+    */
+    public function checkAttribute($attribute, $ocls)
+    {
+        foreach ($ocls as $ocl) {
+            $ocl_entry = $this->get('objectclass', $ocl);
+            $ocl_may_attrs  = $this->may($ocl);
+            $ocl_must_attrs = $this->must($ocl);
+            if (is_array($ocl_may_attrs) && in_array($attribute, $ocl_may_attrs)) {
+                return true;
+            }
+            if (is_array($ocl_must_attrs) && in_array($attribute, $ocl_must_attrs)) {
+                return true;
+            }
+        }
+        return false; // no ocl for the ocls found.
+    }
 }
-?>
+?>
\ No newline at end of file