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

Add forbidden word filter #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 20 additions & 4 deletions src/CensorWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class CensorWords
{
public $badwords;
public $forbiddenWords;

/*
* When the dictionary is loaded, a ton of regular expression strings are generated
Expand All @@ -29,8 +30,10 @@ public function __construct() {
* string
*/
public function setDictionary($dictionary) {

$this->badwords = $this->readBadWords($dictionary);

$words = $this->readBadWords($dictionary);
$this->badwords = $words['badwords'];
$this->forbiddenWords = $words['forbiddenWords'];
}

/**
Expand All @@ -43,7 +46,9 @@ public function setDictionary($dictionary) {
*/
public function addDictionary($dictionary) {

$this->badwords = array_merge($this->badwords, $this->readBadWords($dictionary));
$words = $this->readBadWords($dictionary);
$this->badwords = array_merge($this->badwords, $words['badwords']);
$this->forbiddenWords = array_merge($this->forbiddenWords, $words['forbiddenWords']);
}

/**
Expand All @@ -54,6 +59,7 @@ public function addDictionary($dictionary) {
*/
private function readBadWords($dictionary) {
$badwords = array();
$forbiddenWords = [];
$baseDictPath = __DIR__ . DIRECTORY_SEPARATOR .'dict/';

if (is_array($dictionary)) {
Expand All @@ -77,7 +83,12 @@ private function readBadWords($dictionary) {
}
}

return $badwords;
$wordList = [
'badwords' => $badwords,
'forbiddenWords' => $forbiddenWords
];

return $wordList;
}

/**
Expand Down Expand Up @@ -112,6 +123,7 @@ public function randCensor($chars, $len) {
private function generateCensorChecks($fullWords = false) {

$badwords = $this->badwords;
$forbiddenWords = $this->forbiddenWords;

// generate censor checks as soon as we load the dictionary
// utilize leet equivalents as well
Expand Down Expand Up @@ -147,6 +159,10 @@ private function generateCensorChecks($fullWords = false) {
for ($x=0; $x<count($badwords); $x++) {
$censorChecks[$x] = $fullWords ? '/\b'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'\b/i'
: '/'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'/i';
if(in_array($badwords[$x], $forbiddenWords, true)) {
$censorChecks[$x] = '/'.str_ireplace(array_keys($leet_replace),array_values($leet_replace), $badwords[$x]).'/i';
}

}

$this->censorChecks = $censorChecks;
Expand Down
6 changes: 5 additions & 1 deletion src/dict/en-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@
'vagina',
'vulva',
'wank'
);
);
array_push($forbiddenWords ,
'fuck',
'shit'
);