public function testAsc2hex32() { $expected = '\\00\\01\\02\\03\\04\\05\\06\\07\\08\\09\\0a\\0b\\0c\\0d\\0e\\0f\\10\\11\\12\\13\\14\\15\\16\\17\\18\\19' . '\\1a\\1b\\1c\\1d\\1e\\1f !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`' . 'abcdefghijklmnopqrstuvwxyz{|}~'; $str = ''; for ($i = 0; $i < 127; $i++) { $str .= chr($i); } $this->assertEquals($expected, Ldap\Converter::ascToHex32($str)); }
/** * Escapes the given VALUES according to RFC 2254 so that they can be safely used in LDAP filters. * * Any control characters with an ACII code < 32 as well as the characters with special meaning in * LDAP filters "*", "(", ")", and "\" (the backslash) are converted into the representation of a * backslash followed by two hex digits representing the hexadecimal value of the character. * @see Net_LDAP2_Util::escape_filter_value() from Benedikt Hallinger <*****@*****.**> * @link http://pear.php.net/package/Net_LDAP2 * @author Benedikt Hallinger <*****@*****.**> * * @param string|array $values Array of values to escape * @return array Array $values, but escaped */ public static function escapeValue($values = array()) { if (!is_array($values)) { $values = array($values); } foreach ($values as $key => $val) { // Escaping of filter meta characters $val = str_replace(array('\\', '*', '(', ')'), array('\\5c', '\\2a', '\\28', '\\29'), $val); // ASCII < 32 escaping $val = Ldap\Converter::ascToHex32($val); if (null === $val) { $val = '\\0'; // apply escaped "null" if string is empty } $values[$key] = $val; } return count($values) == 1 ? $values[0] : $values; }