diff --git a/app/Helpers/LanguageCensor.php b/app/Helpers/LanguageCensor.php new file mode 100644 index 0000000000..f545583b5a --- /dev/null +++ b/app/Helpers/LanguageCensor.php @@ -0,0 +1,86 @@ + 0); + $indexes = self::matchWordIndexes($source, $word); + $ignore = 0; + for ($i = 0; $i < $length; ++$i) { + if (count($indexes) > 0 && $indexes[0] == $i) { + $match = substr($source, $indexes[0], $word_length); + $result .= "{$match}"; + $ignore = $word_length - 1; + } elseif ($ignore > 0) { + --$ignore; + } else { + $result .= $source[$i]; + } + } + $source = $result; + } + + $replaceDict = Config::get('censor.replace', []); + foreach ($replaceDict as $word => $replacement) { + $source = str_ireplace($word, $replacement, $source); + } + return $source; + } +} diff --git a/app/Http/Controllers/ShoutboxController.php b/app/Http/Controllers/ShoutboxController.php index 88d29974c3..12346c3988 100755 --- a/app/Http/Controllers/ShoutboxController.php +++ b/app/Http/Controllers/ShoutboxController.php @@ -14,6 +14,7 @@ use App\Shoutbox; use App\User; +use App\Helpers\LanguageCensor; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Request; @@ -153,7 +154,7 @@ public function fetch($after = null) ' . ($message->created_at->diffForHumans()) . '
'; } diff --git a/app/Http/Middleware/LanguageCensor.php b/app/Http/Middleware/LanguageCensor.php deleted file mode 100644 index 110c939d45..0000000000 --- a/app/Http/Middleware/LanguageCensor.php +++ /dev/null @@ -1,89 +0,0 @@ -getContent(); - $content = $this->censorResponse($content); - - $response->setContent($content); - - return $response; - } - - protected function matchWords($string, $word) - { - $result = []; - $length = strlen($word); - $pos = stripos($string, $word, 0); - while ($pos !== false) { - $match = substr($string, $pos, $length); - array_push($result, $match); - $pos = stripos($string, $word, $pos + $length); - } - - return $result; - } - - /** - * Censor the request response. - * - * @param $source - * - * @return mixed - */ - protected function censorResponse($source) - { - $redactArray = Config::get('censor.redact', []); - foreach ($redactArray as $word) { - foreach (self::matchWords($source, $word) as $match) { - $replacement = "{$match}"; - $source = str_replace($match, $replacement, $source); - } - } - - $replaceDict = Config::get('censor.replace', []); - foreach ($replaceDict as $word => $replacement) { - $source = str_ireplace($word, $replacement, $source); - } - return $source; - } -} diff --git a/resources/views/blocks/chat.blade.php b/resources/views/blocks/chat.blade.php index fd6e52999d..a39a954d93 100644 --- a/resources/views/blocks/chat.blade.php +++ b/resources/views/blocks/chat.blade.php @@ -16,6 +16,11 @@ if (in_array(\Auth::user()->id, explode(',', $message->mentions))) { $class = 'mentioned'; } + + $messageHtml = App\Shoutbox::getMessageHtml($message->message); + $messageHtml = \LaravelEmojiOne::toImage($messageHtml); + $messageHtml = App\Helpers\LanguageCensor::censor($messageHtml); + @endphp