/** * Unquote control characters in distinguished names used in LDAP - See RFC 4514/2253 * * @param string The text quoted * @return string The text unquoted */ function ldap_stripslashes($text) { $special_dn_chars = ldap_get_dn_special_chars(); // First unquote the simply backslashed special characters. If we // do it the other way, we remove too many slashes. $text = str_replace($special_dn_chars[LDAP_DN_SPECIAL_CHARS_QUOTED_ALPHA], $special_dn_chars[LDAP_DN_SPECIAL_CHARS], $text); // Next unquote the 'numerically' quoted characters. We don't use // LDAP_DN_SPECIAL_CHARS_QUOTED_NUM because the standard allows us // to quote any character with this encoding, not just the special // ones. $text = preg_replace('/\\\\([0-9A-Fa-f]{2})/e', "chr(hexdec('\\1'))", $text); return $text; }
/** * Unquote control characters in AttributeValue parts of a RelativeDistinguishedName * used in LDAP distinguished names - See RFC 4514/2253 * * @param string the AttributeValue quoted * @return string the AttributeValue unquoted */ function ldap_stripslashes($text) { $specialchars = ldap_get_dn_special_chars(); // We can't unquote in two steps, as we end up unquoting too much in certain cases. So // we need to build a regexp containing both the 'numerically' and 'alphabetically' // quoted characters. We don't use LDAP_DN_SPECIAL_CHARS_QUOTED_NUM because the // standard allows us to quote any character with this encoding, not just the special // ones. // @TODO: This still misses some special (and rarely used) cases, but we need // a full state machine to handle them. $quoted = '/(\\\\[0-9A-Fa-f]{2}|' . $specialchars[LDAP_DN_SPECIAL_CHARS_QUOTED_ALPHA_REGEX] . ')/'; $text = preg_replace_callback($quoted, function ($match) use($specialchars) { if (ctype_xdigit(ltrim($match[1], '\\'))) { return chr(hexdec($match[1])); } else { return str_replace($specialchars[LDAP_DN_SPECIAL_CHARS_QUOTED_ALPHA], $specialchars[LDAP_DN_SPECIAL_CHARS], $match[1]); } }, $text); return $text; }