/** * 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. * * @param array $values Array of values to escape * * @static * @return array Array $values, but escaped */ function escape_ldap_filter_value($values = array()) { // Parameter validation $unwrap = !is_array($values); if ($unwrap) { $values = array($values); } foreach ($values as $key => $val) { // Escaping of filter meta characters $val = str_replace('\\', '\\5c', $val); $val = str_replace('*', '\\2a', $val); $val = str_replace('(', '\\28', $val); $val = str_replace(')', '\\29', $val); // ASCII < 32 escaping $val = asc2hex32($val); if (null === $val) { $val = '\\0'; // apply escaped "null" if string is empty } $values[$key] = $val; } if ($unwrap) { return $values[0]; } else { return $values; } }
/** * 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. * * @param array $values Array of values to escape * * @return array Array $values, but escaped */ function ldap_escape_filter_value($values = array()) { // Parameter validation if (!is_array($values)) { $values = array($values); } foreach ($values as $key => $val) { // Escaping of filter meta characters $val = str_replace('\\', '\\5c', $val); $val = str_replace('\\5c,', '\\2c', $val); $val = str_replace('*', '\\2a', $val); $val = str_replace('(', '\\28', $val); $val = str_replace(')', '\\29', $val); // ASCII < 32 escaping $val = asc2hex32($val); if (NULL === $val) { $val = '\\0'; } // apply escaped "null" if string is empty $values[$key] = $val; } return $values; }