This commit is contained in:
Jun Pataleta 2024-08-06 15:52:59 +08:00
commit daed5e02a1
No known key found for this signature in database
GPG Key ID: F83510526D99E2C7
2 changed files with 26 additions and 7 deletions

View File

@ -39,12 +39,10 @@ class text_filter extends \core_filters\text_filter {
// Pattern to find a mailto link with the linked text.
$pattern = '|(<a\s+href\s*=\s*[\'"]?mailto:)' . $emailregex . '([\'"]?\s*>)' . '(.*)' . '(</a>)|iU';
$text = preg_replace_callback($pattern, 'filter_emailprotect_alter_mailto', $text);
$text = preg_replace_callback($pattern, [self::class, 'alter_mailto'], $text);
// Pattern to find any other email address in the text.
$pattern = '/(^|\s+|>)' . $emailregex . '($|\s+|\.\s+|\.$|<)/i';
$text = preg_replace_callback($pattern, 'filter_emailprotect_alter_email', $text);
$text = preg_replace_callback($pattern, [self::class, 'alter_email'], $text);
return $text;
@ -57,7 +55,7 @@ class text_filter extends \core_filters\text_filter {
* @return string
*/
private function alter_email($matches) {
return $matches[1].obfuscate_text($matches[2]).$matches[3];
return $matches[1] . obfuscate_text($matches[2]) . $matches[3];
}
/**

View File

@ -36,9 +36,18 @@ final class text_filter_test extends \advanced_testcase {
public function test_filter(
string $expression,
string $text,
bool $exactmatch,
): void {
$filter = new text_filter(\core\context\system::instance(), []);
$this->assertMatchesRegularExpression($expression, $text);
$result = $filter->filter($text);
$this->assertMatchesRegularExpression($expression, $result);
if ($exactmatch) {
$this->assertEquals($text, $result);
} else {
$this->assertNotEquals($text, $result);
}
}
/**
@ -50,11 +59,23 @@ final class text_filter_test extends \advanced_testcase {
$email = 'chaise@example.com';
return [
// No email address found.
['/Hello, world!/', 'Hello, world!'],
[
'/Hello, world!/',
'Hello, world!',
true,
],
// Email addresses present.
// Note: The obfuscation randomly choose which chars to obfuscate.
['/.*@.*/', $email],
["~<a href='mailto:.*@.*'>.*@.*</a>~", "<a href='mailto:$email'>$email</a>"],
[
'/.*(@|&#64;).*/',
$email,
false,
],
[
"~<a href=\".*:.*(@|&#64;).*\">.*(@|&#64;).*</a>~",
"<a href='mailto:$email'>$email</a>",
false,
],
];
}
}