[ticket/9933] Add $use_unicode parameter to get_censor_preg_expression().

Rename $unicode to $unicode_support, pass in $use_unicode defaulting to true.

In unit tests we can now pass in $use_unicode as false and also test the code
path that is taken when PCRE does not support unicode.

PHPBB3-9933
This commit is contained in:
Andreas Fischer 2011-01-16 20:03:08 +01:00
parent 8c1866bc0c
commit 97be6e7946
2 changed files with 19 additions and 8 deletions

View file

@ -3433,12 +3433,13 @@ 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) function get_censor_preg_expression($word, $use_unicode = true)
{ {
static $unicode = null; static $unicode_support = null;
if (empty($word)) if (empty($word))
{ {
@ -3446,15 +3447,15 @@ function get_censor_preg_expression($word)
} }
// Check whether PHP version supports unicode properties // Check whether PHP version supports unicode properties
if (is_null($unicode)) if (is_null($unicode_support))
{ {
$unicode = ((version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) && @preg_match('/\p{L}/u', 'a') !== false) ? true : false; $unicode_support = ((version_compare(PHP_VERSION, '5.1.0', '>=') || (version_compare(PHP_VERSION, '5.0.0-dev', '<=') && version_compare(PHP_VERSION, '4.4.0', '>='))) && @preg_match('/\p{L}/u', 'a') !== false) ? true : false;
} }
// 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 ($unicode) if ($use_unicode && $unicode_support)
{ {
// Replace asterisk(s) inside the pattern, at the start and at the end of it with regexes // 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); $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);

View file

@ -31,9 +31,19 @@ class phpbb_regex_censor_test extends phpbb_test_case
/** /**
* @dataProvider censor_test_data * @dataProvider censor_test_data
*/ */
public function test_censor($pattern, $subject) public function test_censor_unicode($pattern, $subject)
{ {
$regex = get_censor_preg_expression($pattern); $regex = get_censor_preg_expression($pattern, true);
$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);
} }