Horde_Ldap_Util::escapeDNValue PHP Method

escapeDNValue() public static method

Escapes the given VALUES according to RFC 2253 so that they can be safely used in LDAP DNs. The characters ",", "+", """, "\", "<", ">", ";", "#", "=" with a special meaning in RFC 2252 are preceeded by ba backslash. Control characters with an ASCII code < 32 are represented as \hexpair. Finally all leading and trailing spaces are converted to sequences of \20.
public static escapeDNValue ( string | array $values ) : array
$values string | array DN values that should be escaped.
return array The escaped values.
    public static function escapeDNValue($values)
    {
        // Parameter validation.
        if (!is_array($values)) {
            $values = array($values);
        }
        foreach ($values as $key => $val) {
            // Escaping of filter meta characters.
            $val = addcslashes($val, '\\,+"<>;#=');
            // ASCII < 32 escaping.
            $val = self::asc2hex32($val);
            // Convert all leading and trailing spaces to sequences of \20.
            if (preg_match('/^(\\s*)(.+?)(\\s*)$/', $val, $matches)) {
                $val = str_repeat('\\20', strlen($matches[1])) . $matches[2] . str_repeat('\\20', strlen($matches[3]));
            }
            if (null === $val) {
                // Apply escaped "null" if string is empty.
                $val = '\\0';
            }
            $values[$key] = $val;
        }
        return $values;
    }

Usage Example

Ejemplo n.º 1
0
 /**
  * Test escapeDNValue()
  */
 public function testEscapeDNValue()
 {
     $dnval = '  ' . chr(22) . ' t,e+s"t,\\v<a>l;u#e=!    ';
     $expected = '\\20\\20\\16 t\\,e\\+s\\"t\\,\\\\v\\<a\\>l\\;u\\#e\\=!\\20\\20\\20\\20';
     // String call.
     $this->assertEquals(array($expected), Horde_Ldap_Util::escapeDNValue($dnval));
     // Array call.
     $this->assertEquals(array($expected), Horde_Ldap_Util::escapeDNValue(array($dnval)));
     // Multiple arrays.
     $this->assertEquals(array($expected, $expected, $expected), Horde_Ldap_Util::escapeDNValue(array($dnval, $dnval, $dnval)));
 }