Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improves handling of letter case in word censor #8297

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion Sources/Lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -635,7 +635,35 @@ public static function censorText(string &$text, bool $force = false): string
}

// Censoring isn't so very complicated :P.
$text = preg_replace($censor_vulgar, $censor_proper, $text);
foreach ($censor_vulgar as $i => $pattern) {
$text = preg_replace_callback(
$pattern,
function ($matches) use ($censor_proper, $i) {
// Special case to return the original word unchanged.
if ($censor_proper[$i] === '$0') {
return $matches[0];
}

// If the replacement contains any letters, try to match case.
if (preg_match('/\p{L}/u', $censor_proper[$i])) {
// If original was all uppercase, return uppercase.
if (Utils::strtoupper($matches[0]) === $matches[0]) {
return Utils::strtoupper($censor_proper[$i]);
}

// If original started with a capital letter, return with a capital letter.
$first_vulgar_char = Utils::entitySubstr($matches[0], 0, 1);

if (Utils::ucfirst($first_vulgar_char) === $first_vulgar_char) {
return Utils::ucfirst($censor_proper[$i]);
}
}

return $censor_proper[$i];
},
$text,
);
}

return $text;
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/Unicode/SpoofDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ public static function enhanceWordCensor(string $text): void
$vulgar = explode("\n", Config::$modSettings['censor_vulgar']);
$proper = explode("\n", Config::$modSettings['censor_proper']);

if (!empty(Config::$modSettings['censorIgnoreCase'])) {
$text = Utils::convertCase($text, 'fold');

foreach ($vulgar as $i => $v) {
$vulgar[$i] = Utils::convertCase($v, 'fold');
}
}

$text_chars = preg_split('/(.)/su', $text, 0, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);

foreach ($text_chars as $text_char) {
Expand Down
Loading