[ticket/13614] Removed code that relies on phpbb_pcre_utf8_support()

Removed all calls to phpbb_pcre_utf8_support() as well as unreachable code.

PHPBB3-13614
This commit is contained in:
JoshyPHP 2015-05-07 16:01:42 +02:00
parent 98db63e8cc
commit 8b7a42f338
4 changed files with 20 additions and 150 deletions

View file

@ -416,8 +416,6 @@ class acp_bbcodes
// Allow unicode characters for URL|LOCAL_URL|RELATIVE_URL|INTTEXT tokens // Allow unicode characters for URL|LOCAL_URL|RELATIVE_URL|INTTEXT tokens
$utf8 = preg_match('/(URL|LOCAL_URL|RELATIVE_URL|INTTEXT)/', $bbcode_match); $utf8 = preg_match('/(URL|LOCAL_URL|RELATIVE_URL|INTTEXT)/', $bbcode_match);
$utf8_pcre_properties = phpbb_pcre_utf8_support();
$fp_match = preg_quote($bbcode_match, '!'); $fp_match = preg_quote($bbcode_match, '!');
$fp_replace = preg_replace('#^\[(.*?)\]#', '[$1:$uid]', $bbcode_match); $fp_replace = preg_replace('#^\[(.*?)\]#', '[$1:$uid]', $bbcode_match);
$fp_replace = preg_replace('#\[/(.*?)\]$#', '[/$1:$uid]', $fp_replace); $fp_replace = preg_replace('#\[/(.*?)\]$#', '[/$1:$uid]', $fp_replace);
@ -448,7 +446,7 @@ class acp_bbcodes
'!([a-zA-Z0-9-+.,_ ]+)!' => "$1" '!([a-zA-Z0-9-+.,_ ]+)!' => "$1"
), ),
'INTTEXT' => array( 'INTTEXT' => array(
($utf8_pcre_properties) ? '!([\p{L}\p{N}\-+,_. ]+)!u' : '!([a-zA-Z0-9\-+,_. ]+)!u' => "$1" '!([\p{L}\p{N}\-+,_. ]+)!u' => "$1"
), ),
'IDENTIFIER' => array( 'IDENTIFIER' => array(
'!([a-zA-Z0-9-_]+)!' => "$1" '!([a-zA-Z0-9-_]+)!' => "$1"
@ -468,7 +466,7 @@ class acp_bbcodes
'EMAIL' => '(' . get_preg_expression('email') . ')', 'EMAIL' => '(' . get_preg_expression('email') . ')',
'TEXT' => '(.*?)', 'TEXT' => '(.*?)',
'SIMPLETEXT' => '([a-zA-Z0-9-+.,_ ]+)', 'SIMPLETEXT' => '([a-zA-Z0-9-+.,_ ]+)',
'INTTEXT' => ($utf8_pcre_properties) ? '([\p{L}\p{N}\-+,_. ]+)' : '([a-zA-Z0-9\-+,_. ]+)', 'INTTEXT' => '([\p{L}\p{N}\-+,_. ]+)',
'IDENTIFIER' => '([a-zA-Z0-9-_]+)', 'IDENTIFIER' => '([a-zA-Z0-9-_]+)',
'COLOR' => '([a-zA-Z]+|#[0-9abcdefABCDEF]+)', 'COLOR' => '([a-zA-Z]+|#[0-9abcdefABCDEF]+)',
'NUMBER' => '([0-9]+)', 'NUMBER' => '([0-9]+)',
@ -476,7 +474,7 @@ class acp_bbcodes
$pad = 0; $pad = 0;
$modifiers = 'i'; $modifiers = 'i';
$modifiers .= ($utf8 && $utf8_pcre_properties) ? 'u' : ''; $modifiers .= ($utf8) ? 'u' : '';
if (preg_match_all('/\{(' . implode('|', array_keys($tokens)) . ')[0-9]*\}/i', $bbcode_match, $m)) if (preg_match_all('/\{(' . implode('|', array_keys($tokens)) . ')[0-9]*\}/i', $bbcode_match, $m))
{ {

View file

@ -2811,31 +2811,19 @@ function get_preg_expression($mode)
* Depends on whether installed PHP version supports unicode properties * Depends on whether installed PHP version supports unicode properties
* *
* @param string $word word template to be replaced * @param string $word word template to be replaced
* @param bool $use_unicode whether or not to take advantage of PCRE supporting unicode
* *
* @return string $preg_expr regex to use with word censor * @return string $preg_expr regex to use with word censor
*/ */
function get_censor_preg_expression($word, $use_unicode = true) function get_censor_preg_expression($word)
{ {
// Unescape the asterisk to simplify further conversions // Unescape the asterisk to simplify further conversions
$word = str_replace('\*', '*', preg_quote($word, '#')); $word = str_replace('\*', '*', preg_quote($word, '#'));
if ($use_unicode && phpbb_pcre_utf8_support()) // Replace asterisk(s) inside the pattern, at the start and at the end of it with regexes
{ $word = preg_replace(array('#(?<=[\p{Nd}\p{L}_])\*+(?=[\p{Nd}\p{L}_])#iu', '#^\*+#', '#\*+$#'), array('([\x20]*?|[\p{Nd}\p{L}_-]*?)', '[\p{Nd}\p{L}_-]*?', '[\p{Nd}\p{L}_-]*?'), $word);
// Replace asterisk(s) inside the pattern, at the start and at the end of it with regexes
$word = preg_replace(array('#(?<=[\p{Nd}\p{L}_])\*+(?=[\p{Nd}\p{L}_])#iu', '#^\*+#', '#\*+$#'), array('([\x20]*?|[\p{Nd}\p{L}_-]*?)', '[\p{Nd}\p{L}_-]*?', '[\p{Nd}\p{L}_-]*?'), $word);
// Generate the final substitution // Generate the final substitution
$preg_expr = '#(?<![\p{Nd}\p{L}_-])(' . $word . ')(?![\p{Nd}\p{L}_-])#iu'; $preg_expr = '#(?<![\p{Nd}\p{L}_-])(' . $word . ')(?![\p{Nd}\p{L}_-])#iu';
}
else
{
// Replace the asterisk inside the pattern, at the start and at the end of it with regexes
$word = preg_replace(array('#(?<=\S)\*+(?=\S)#iu', '#^\*+#', '#\*+$#'), array('(\x20*?\S*?)', '\S*?', '\S*?'), $word);
// Generate the final substitution
$preg_expr = '#(?<!\S)(' . $word . ')(?!\S)#iu';
}
return $preg_expr; return $preg_expr;
} }
@ -4690,22 +4678,6 @@ function phpbb_user_session_handler()
return; return;
} }
/**
* Check if PCRE has UTF-8 support
* PHP may not be linked with the bundled PCRE lib and instead with an older version
*
* @return bool Returns true if PCRE (the regular expressions library) supports UTF-8 encoding
*/
function phpbb_pcre_utf8_support()
{
static $utf8_pcre_properties = null;
if (is_null($utf8_pcre_properties))
{
$utf8_pcre_properties = (@preg_match('/\p{L}/u', 'a') !== false);
}
return $utf8_pcre_properties;
}
/** /**
* Casts a numeric string $input to an appropriate numeric type (i.e. integer or float) * Casts a numeric string $input to an appropriate numeric type (i.e. integer or float)
* *

View file

@ -1647,89 +1647,37 @@ function validate_username($username, $allowed_username = false)
return 'INVALID_CHARS'; return 'INVALID_CHARS';
} }
$mbstring = $pcre = false;
// generic UTF-8 character types supported?
if (phpbb_pcre_utf8_support())
{
$pcre = true;
}
else if (function_exists('mb_ereg_match'))
{
mb_regex_encoding('UTF-8');
$mbstring = true;
}
switch ($config['allow_name_chars']) switch ($config['allow_name_chars'])
{ {
case 'USERNAME_CHARS_ANY': case 'USERNAME_CHARS_ANY':
$pcre = true;
$regex = '.+'; $regex = '.+';
break; break;
case 'USERNAME_ALPHA_ONLY': case 'USERNAME_ALPHA_ONLY':
$pcre = true;
$regex = '[A-Za-z0-9]+'; $regex = '[A-Za-z0-9]+';
break; break;
case 'USERNAME_ALPHA_SPACERS': case 'USERNAME_ALPHA_SPACERS':
$pcre = true;
$regex = '[A-Za-z0-9-[\]_+ ]+'; $regex = '[A-Za-z0-9-[\]_+ ]+';
break; break;
case 'USERNAME_LETTER_NUM': case 'USERNAME_LETTER_NUM':
if ($pcre) $regex = '[\p{Lu}\p{Ll}\p{N}]+';
{
$regex = '[\p{Lu}\p{Ll}\p{N}]+';
}
else if ($mbstring)
{
$regex = '[[:upper:][:lower:][:digit:]]+';
}
else
{
$pcre = true;
$regex = '[a-zA-Z0-9]+';
}
break; break;
case 'USERNAME_LETTER_NUM_SPACERS': case 'USERNAME_LETTER_NUM_SPACERS':
if ($pcre) $regex = '[-\]_+ [\p{Lu}\p{Ll}\p{N}]+';
{
$regex = '[-\]_+ [\p{Lu}\p{Ll}\p{N}]+';
}
else if ($mbstring)
{
$regex = '[-\]_+ \[[:upper:][:lower:][:digit:]]+';
}
else
{
$pcre = true;
$regex = '[-\]_+ [a-zA-Z0-9]+';
}
break; break;
case 'USERNAME_ASCII': case 'USERNAME_ASCII':
default: default:
$pcre = true;
$regex = '[\x01-\x7F]+'; $regex = '[\x01-\x7F]+';
break; break;
} }
if ($pcre) if (!preg_match('#^' . $regex . '$#u', $username))
{ {
if (!preg_match('#^' . $regex . '$#u', $username)) return 'INVALID_CHARS';
{
return 'INVALID_CHARS';
}
}
else if ($mbstring)
{
mb_ereg_search_init($username, '^' . $regex . '$');
if (!mb_ereg_search())
{
return 'INVALID_CHARS';
}
} }
$sql = 'SELECT username $sql = 'SELECT username
@ -1784,35 +1732,10 @@ function validate_password($password)
return false; return false;
} }
$pcre = $mbstring = false; $upp = '\p{Lu}';
$low = '\p{Ll}';
// generic UTF-8 character types supported? $num = '\p{N}';
if (phpbb_pcre_utf8_support()) $sym = '[^\p{Lu}\p{Ll}\p{N}]';
{
$upp = '\p{Lu}';
$low = '\p{Ll}';
$num = '\p{N}';
$sym = '[^\p{Lu}\p{Ll}\p{N}]';
$pcre = true;
}
else if (function_exists('mb_ereg_match'))
{
mb_regex_encoding('UTF-8');
$upp = '[[:upper:]]';
$low = '[[:lower:]]';
$num = '[[:digit:]]';
$sym = '[^[:upper:][:lower:][:digit:]]';
$mbstring = true;
}
else
{
$upp = '[A-Z]';
$low = '[a-z]';
$num = '[0-9]';
$sym = '[^A-Za-z0-9]';
$pcre = true;
}
$chars = array(); $chars = array();
switch ($config['pass_complex']) switch ($config['pass_complex'])
@ -1835,24 +1758,11 @@ function validate_password($password)
$chars[] = $upp; $chars[] = $upp;
} }
if ($pcre) foreach ($chars as $char)
{ {
foreach ($chars as $char) if (!preg_match('#' . $char . '#u', $password))
{ {
if (!preg_match('#' . $char . '#u', $password)) return 'INVALID_CHARS';
{
return 'INVALID_CHARS';
}
}
}
else if ($mbstring)
{
foreach ($chars as $char)
{
if (mb_ereg($char, $password) === false)
{
return 'INVALID_CHARS';
}
} }
} }

View file

@ -37,17 +37,7 @@ class phpbb_regex_censor_test extends phpbb_test_case
*/ */
public function test_censor_unicode($pattern, $subject) public function test_censor_unicode($pattern, $subject)
{ {
$regex = get_censor_preg_expression($pattern, true); $regex = get_censor_preg_expression($pattern);
$this->assertRegExp($regex, $subject);
}
/**
* @dataProvider censor_test_data
*/
public function test_censor_no_unicode($pattern, $subject)
{
$regex = get_censor_preg_expression($pattern, false);
$this->assertRegExp($regex, $subject); $this->assertRegExp($regex, $subject);
} }