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

Make LanguageCensor a helper #91

Merged
merged 4 commits into from Dec 30, 2017
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
86 changes: 86 additions & 0 deletions app/Helpers/LanguageCensor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* NOTICE OF LICENSE
*
* UNIT3D is open-sourced software licensed under the GNU General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D
* @license https://choosealicense.com/licenses/gpl-3.0/ GNU General Public License v3.0
* @author HDVinnie
*/

namespace App\Helpers;

use Config;

/**
* Class LanguageCensor
*
* A class that can redact/replace words.
*
*/
class LanguageCensor
{
static protected function isSpecial($c)
{
$specialChars = " [].;,";
return strpos($specialChars, $c) !== false;
}

static protected function matchWordIndexes($string, $word)
{
$result = [];
$length = strlen($word);
$string_length = strlen($string);
$pos = stripos($string, $word, 0);
while ($pos !== false) {
$prev = ($pos === 0) ? ' ' : $string[$pos - 1];
$last = ($pos + $length) < $string_length ? $string[$pos + $length] : ' ';
if (self::isSpecial($prev) && self::isSpecial($last)) {
array_push($result, $pos);
}
$pos = stripos($string, $word, $pos + $length);
}

return $result;
}

/**
* Censor a text.
*
* @param $source
*
* @return mixed
*/
static public function censor($source)
{
$redactArray = Config::get('censor.redact', []);
foreach ($redactArray as $word) {
$result = "";
$length = strlen($source);
$word_length = strlen($word);
assert($word_length > 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 .= "<span class='censor'>{$match}</span>";
$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;
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/ShoutboxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use App\Shoutbox;
use App\User;
use App\Helpers\LanguageCensor;

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Request;
Expand Down Expand Up @@ -153,7 +154,7 @@ public function fetch($after = null)
</span>&nbsp;<span class="text-muted"><small><em>' . ($message->created_at->diffForHumans()) . '</em></small></span>
</h4>
<p class="message-content">
' . \LaravelEmojiOne::toImage(Shoutbox::getMessageHtml($message->message)) . '
' . \LaravelEmojiOne::toImage(LanguageCensor::censor(Shoutbox::getMessageHtml($message->message))) . '
' . ($flag ? $delete : "") . '
</p></li>';
}
Expand Down
89 changes: 0 additions & 89 deletions app/Http/Middleware/LanguageCensor.php

This file was deleted.

7 changes: 6 additions & 1 deletion resources/views/blocks/chat.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
<li class="list-group-item {{ $class }}">
@if($message->poster->image != null)
Expand All @@ -36,7 +41,7 @@
<a title="Delete Shout" href="{{route('shout-delete',['id' => $message->id])}}"><i class="pull-right fa fa-lg fa-times"></i></a>
@endif

@emojione(App\Shoutbox::getMessageHtml($message->message))</p>
{!! $messageHtml !!}
</li>
@endforeach
</ul>
Expand Down
2 changes: 1 addition & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@
| ShoutBox Routes Group (when authorized)
|------------------------------------------
*/
Route::group(['prefix' => 'shoutbox', 'middleware' => ['auth','censor']], function () {
Route::group(['prefix' => 'shoutbox', 'middleware' => 'auth'], function () {
Route::get('/', 'HomeController@home')->name('shoutbox-home');
Route::get('/messages/{after?}', 'ShoutboxController@fetch')->name('shoutbox-fetch');
Route::post('/send', 'ShoutboxController@send')->name('shoutbox-send');
Expand Down