Merge branch 'develop-olympus' into develop

* develop-olympus:
  [ticket/9933] Remove empty word check.
  [ticket/9933] Add $use_unicode parameter to get_censor_preg_expression().
  [ticket/9933] Adjust word censor regex for non-unicode mode.

Conflicts:
	phpBB/includes/functions.php

Also remove static $unicode_support.
This commit is contained in:
Igor Wiedler 2011-01-16 20:28:06 +01:00
commit 656f18d3af
2 changed files with 23 additions and 21 deletions

View file

@ -3246,29 +3246,17 @@ 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;
if (empty($word))
{
return '';
}
// Check whether PHP version supports unicode properties
if (is_null($unicode))
{
$unicode = pcre_utf8_support();
}
if ($unicode)
{ {
// 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 && pcre_utf8_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);
@ -3277,7 +3265,11 @@ function get_censor_preg_expression($word)
} }
else else
{ {
$preg_expr = '#(?<!\S)(' . str_replace('\*', '\S*?', preg_quote($word, '#')) . ')(?!\S)#iu'; // 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;

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);
} }