/** * Escapes a DN value according to RFC 2253 * * 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. * @see Net_LDAP2_Util::escape_dn_value() from Benedikt Hallinger <*****@*****.**> * @link http://pear.php.net/package/Net_LDAP2 * @author Benedikt Hallinger <*****@*****.**> * * @param string|array $values An array containing the DN values that should be escaped * @return array The 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('\\\\', '\\,', '\\+', '\\"', '\\<', '\\>', '\\;', '\\#', '\\='), $val); $val = Converter\Converter::ascToHex32($val); // Convert all leading and trailing spaces to sequences of \20. if (preg_match('/^(\\s*)(.+?)(\\s*)$/', $val, $matches)) { $val = $matches[2]; for ($i = 0; $i < strlen($matches[1]); $i++) { $val = '\\20' . $val; } for ($i = 0; $i < strlen($matches[3]); $i++) { $val = $val . '\\20'; } } if (null === $val) { $val = '\\0'; } // apply escaped "null" if string is empty $values[$key] = $val; } return count($values) == 1 ? $values[0] : $values; }