Skip to content
Snippets Groups Projects
Commit 2b32417c authored by Gábor Hojtsy's avatar Gábor Hojtsy
Browse files

#523058 by catch, sun, c960657, smk-ka, pwolanin, andypost: optimize calls to...

#523058 by catch, sun, c960657, smk-ka, pwolanin, andypost: optimize calls to check_plain() by trying to use new PHP 5 features and inlining UTF-8 checking
parent 85742e1c
No related branches found
No related tags found
No related merge requests found
......@@ -734,11 +734,35 @@ function referer_uri() {
/**
* Encode special characters in a plain-text string for display as HTML.
*
* Uses drupal_validate_utf8 to prevent cross site scripting attacks on
* Also validates strings as UTF-8 to prevent cross site scripting attacks on
* Internet Explorer 6.
*
* @param $text
* The text to be checked or processed.
* @return
* An HTML safe version of $text, or an empty string if $text is not
* valid UTF-8.
*
* @see drupal_validate_utf8().
*/
function check_plain($text) {
return drupal_validate_utf8($text) ? htmlspecialchars($text, ENT_QUOTES) : '';
static $php525;
if (!isset($php525)) {
$php525 = version_compare(PHP_VERSION, '5.2.5', '>=');
}
// We duplicate the preg_match() to validate strings as UTF-8 from
// drupal_validate_utf8() here. This avoids the overhead of an additional
// function call, since check_plain() may be called hundreds of times during
// a request. For PHP 5.2.5+, this check for valid UTF-8 should be handled
// internally by PHP in htmlspecialchars().
// @see http://www.php.net/releases/5_2_5.php
// @todo remove this when support for either IE6 or PHP < 5.2.5 is dropped.
if ($php525) {
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
return (preg_match('/^./us', $text) == 1) ? htmlspecialchars($text, ENT_QUOTES, 'UTF-8') : '';
}
/**
......@@ -774,6 +798,7 @@ function drupal_validate_utf8($text) {
if (strlen($text) == 0) {
return TRUE;
}
// For performance reasons this logic is duplicated in check_plain().
return (preg_match('/^./us', $text) == 1);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment