[Date Prev][Date Next] [Chronological] [Thread] [Top]

Re: getting at the schema for an object



Ben,

This code might be of interest to you. It's PHP, and I use it to fetch
required/optional attributes for objectClasses. The functions return a
hash of objectClass names as keys with their respective attributes as
values. The way I use it is to see which attributes have not been
defined for a given object. Sounds like you have similar needs. Hope it
helps:

To see what it returns, just print_r() the result of each function.

/*
 * Returns all attributes for each objectClass
 */
function get_schema_object_classes()
{
        $ds = ldap_connect( /* Your info here */ );
        $result = ldap_read($ds, 'cn=subschema', "(objectclass=*)",
array( "objectclasses" ), 0, 200, 0, LDAP_DEREF_ALWAYS );

        if( $result ) $results = ldap_get_entries($ds,$result );

        for( $att=0; $att < count( $results[0]["objectclasses"] );
$att++ )
        {
                $class = $results[0]["objectclasses"][$att];

                preg_match(
"/[\s]+NAME[\s'\(]+([a-zA-Z0-9\-_]+)[\s'\)]/" , $class, $name);
                $key = trim(strtolower( $name[1] )) ;
                if( $key ) $server_objectClasses["$key"] = trim(
$name[1] );
                unset($name);

                if( $key )
                {
                        preg_match(
"/MUST[\s\(]+([a-zA-Z0-9\s$]+)(MAY|\))/" , $class, $name);
                        $fieldnames = str_replace( ' ', '', strtolower(
$name[1] ) );
                        $object_classes["$key"] = array_filter( explode(
'$', $fieldnames ), "not_white" );
                        unset($name);

                        preg_match(
"/MAY[\s\(]+([a-zA-Z0-9\s$]+)(MUST|\))/" , $class, $name);
                        $fieldnames = str_replace( ' ', '', strtolower(
$name[1] ) );
                        $object_classes["$key"] = array_filter(
array_merge( $object_classes["$key"], explode( '$', $fieldnames ) ),
"not_white" );
                        unset($name);
                }
        }

        ksort( $object_classes );
        return $object_classes;

}

/*
 * Returns only required attributes for all objectClasses
 */
function get_schema_object_classes_must()
{

        $ds = ldap_connect( /* your info here */);
        $result = ldap_read($ds, 'cn=subschema', "(objectclass=*)",
array( "objectclasses" ), 0, 200, 0, LDAP_DEREF_ALWAYS );

        if( $result ) $results = ldap_get_entries($ds,$result );

        for( $att=0; $att < count( $results[0]["objectclasses"] );
$att++ )
        {
                $class = $results[0]["objectclasses"][$att];

                preg_match(
"/[\s]+NAME[\s'\(]+([a-zA-Z0-9\-_]+)[\s'\)]/" , $class, $name);
                $key = trim(strtolower( $name[1] )) ;
                if( $key ) $server_objectClasses["$key"] = trim(
$name[1] );
                unset($name);

                if( $key )
                {
                        preg_match(
"/MUST[\s\(]+([a-zA-Z0-9\s$]+)(MAY|\))/" , $class, $name);
                        $fieldnames = str_replace( ' ', '', strtolower(
$name[1] ) );
                        $object_classes["$key"] = array_filter( explode(
'$', $fieldnames ), "not_white" );
                        unset($name);
                }
        }

        ksort( $object_classes );
        return $object_classes;

}