Horde_Ldap_Util::explodeDN PHP Method

explodeDN() public static method

{@link http://www.ietf.org/rfc/rfc2253.txt RFC 2253} says, a Distinguished Name is a sequence of Relative Distinguished Names (RDNs), which themselves are sets of Attributes. For each RDN a array is constructed where the RDN part is stored. For example, the DN 'OU=Sales+CN=J. Smith,DC=example,DC=net' is exploded to: array(array('OU=Sales', 'CN=J. Smith'), 'DC=example', 'DC=net') [NOT IMPLEMENTED] DNs might also contain values, which are the bytes of the BER encoding of the X.500 AttributeValue rather than some LDAP string syntax. These values are hex-encoded and prefixed with a #. To distinguish such BER values, explodeDN uses references to the actual values, e.g. '1.3.6.1.4.1.1466.0=#04024869,DC=example,DC=com' is exploded to: array(array('1.3.6.1.4.1.1466.0' => "\004\002Hi"), array('DC' => 'example', array('DC' => 'com')) See {@link http://www.vijaymukhi.com/vmis/berldap.htm} for more information on BER. It also performs the following operations on the given DN: - Unescape "\" followed by ",", "+", """, "\", "<", ">", ";", "#", "=", " ", or a hexpair and strings beginning with "#". - Removes the leading 'OID.' characters if the type is an OID instead of a name. - If an RDN contains multiple parts, the parts are re-ordered so that the attribute type names are in alphabetical order. $options is a list of name/value pairs, valid options are: - casefold: Controls case folding of attribute types names. Attribute values are not affected by this option. The default is to uppercase. Valid values are: - lower: Lowercase attribute types names. - upper: Uppercase attribute type names. This is the default. - none: Do not change attribute type names. - reverse: If true, the RDN sequence is reversed. - onlyvalues: If true, then only attributes values are returned ('foo' instead of 'cn=foo')
public static explodeDN ( string $dn, array $options = [] ) : array
$dn string The DN that should be exploded.
$options array Options to use.
return array Parts of the exploded DN.
    public static function explodeDN($dn, array $options = array())
    {
        $options = array_merge(array('casefold' => 'upper', 'onlyvalues' => false, 'reverse' => false), $options);
        // Escaping of DN and stripping of "OID.".
        $dn = self::canonicalDN($dn, array('casefold' => $options['casefold']));
        // Splitting the DN.
        $dn_array = preg_split('/(?<!\\\\),/', $dn);
        // Clear wrong splitting (possibly we have split too much).
        // Not clear, if this is neccessary here:
        //$dn_array = self::_correctDNSplitting($dn_array, ',');
        $callback_upper = function ($value) {
            return Horde_String::upper($value[1]);
        };
        $callback_lower = function ($value) {
            return Horde_String::lower($value[1]);
        };
        // Construct subarrays for multivalued RDNs and unescape DN value, also
        // convert to output format and apply casefolding.
        foreach ($dn_array as $key => $value) {
            $value_u = self::unescapeDNValue($value);
            $rdns = self::splitRDNMultivalue($value_u[0]);
            // TODO: nuke code duplication
            if (count($rdns) > 1) {
                // Multivalued RDN!
                foreach ($rdns as $subrdn_k => $subrdn_v) {
                    // Casefolding.
                    if ($options['casefold'] == 'upper') {
                        $subrdn_v = preg_replace_callback('/^(\\w+=)/', $callback_upper, $subrdn_v);
                    }
                    if ($options['casefold'] == 'lower') {
                        $subrdn_v = preg_replace_callback('/^(\\w+=)/', $callback_lower, $subrdn_v);
                    }
                    if ($options['onlyvalues']) {
                        preg_match('/(.+?)(?<!\\\\)=(.+)/', $subrdn_v, $matches);
                        $rdn_val = $matches[2];
                        $unescaped = self::unescapeDNValue($rdn_val);
                        $rdns[$subrdn_k] = $unescaped[0];
                    } else {
                        $unescaped = self::unescapeDNValue($subrdn_v);
                        $rdns[$subrdn_k] = $unescaped[0];
                    }
                }
                $dn_array[$key] = $rdns;
            } else {
                // Singlevalued RDN.
                // Casefolding.
                if ($options['casefold'] == 'upper') {
                    $value = preg_replace_callback('/^(\\w+=)/', $callback_upper, $value);
                }
                if ($options['casefold'] == 'lower') {
                    $value = preg_replace_callback('/^(\\w+=)/', $callback_lower, $value);
                }
                if ($options['onlyvalues']) {
                    preg_match('/(.+?)(?<!\\\\)=(.+)/', $value, $matches);
                    $dn_val = $matches[2];
                    $unescaped = self::unescapeDNValue($dn_val);
                    $dn_array[$key] = $unescaped[0];
                } else {
                    $unescaped = self::unescapeDNValue($value);
                    $dn_array[$key] = $unescaped[0];
                }
            }
        }
        if ($options['reverse']) {
            return array_reverse($dn_array);
        }
        return $dn_array;
    }

Usage Example

コード例 #1
0
ファイル: Ldap.php プロジェクト: DSNS-LAB/Dmail
 /**
  * Returns the win32 AD epoch number of days the password may be unchanged.
  *
  * @return integer|boolean  Number of days or false if no limit.
  */
 protected function _getMaxPasswd()
 {
     $dn = Horde_Ldap_Util::explodeDN($this->_params['basedn']);
     $domaindn = array();
     foreach ($dn as $rdn) {
         $attribute = Horde_Ldap_Util::splitAttributeString($rdn);
         if ($attribute[0] == 'DC') {
             $domaindn[] = $rdn;
         }
     }
     $dn = Horde_Ldap_Util::canonicalDN($domaindn);
     $search = $this->_ldap->search($domaindn, 'objectClass=*');
     $entry = $search->shiftEntry();
     try {
         return $entry->getValue('maxPwdAge', 'single');
     } catch (Horde_Ldap_Exception $e) {
         return false;
     }
 }
All Usage Examples Of Horde_Ldap_Util::explodeDN