-
Notifications
You must be signed in to change notification settings - Fork 144
/
Censorship.class.php
214 lines (188 loc) · 6.65 KB
/
Censorship.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
namespace wcf\system\message\censorship;
use wcf\system\SingletonFactory;
use wcf\util\ArrayUtil;
use wcf\util\StringUtil;
/**
* Finds censored words.
*
* @author Marcel Werk
* @copyright 2001-2019 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
*/
class Censorship extends SingletonFactory
{
/**
* censored words
* @var string[]
*/
protected $censoredWords = [];
/**
* word delimiters
* @var string
*/
protected $delimiters = '[\s\x21-\x2F\x3A-\x3F\x5B-\x60\x7B-\x7E]';
/**
* list of words
* @var string[]
*/
protected $words = [];
/**
* @inheritDoc
*/
protected function init()
{
// get words which should be censored
$censoredWords = ArrayUtil::trim(\explode(
"\n",
StringUtil::unifyNewlines(\mb_strtolower(CENSORED_WORDS))
));
// format censored words
foreach ($censoredWords as $censoredWord) {
$displayedCensoredWord = \str_replace(['~', '*'], '', $censoredWord);
// check if censored word contains at least one delimiter
if (\preg_match('!' . $this->delimiters . '+!', $displayedCensoredWord)) {
// remove delimiters
$censoredWord = \preg_replace('!' . $this->delimiters . '!', '', $censoredWord);
// enforce partial matching
$censoredWord = '~' . $censoredWord;
}
$this->censoredWords[$displayedCensoredWord] = $censoredWord;
}
}
/**
* Returns censored words from a text.
*
* @param string $text
* @return mixed $matches / false
*/
public function test($text)
{
if (empty($this->censoredWords)) {
return false;
}
// reset values
$matches = $this->words = [];
// string to lower case
$text = \mb_strtolower($text);
// ignore bbcode tags
$text = \preg_replace('~\[/?[a-z]+[^\]]*\]~i', '', $text);
// split the text in single words
$this->words = \preg_split("!" . $this->delimiters . "+!", $text, -1, \PREG_SPLIT_NO_EMPTY);
// check each word if it's censored.
for ($i = 0, $count = \count($this->words); $i < $count; $i++) {
$word = $this->words[$i];
foreach ($this->censoredWords as $displayedCensoredWord => $censoredWord) {
// check for direct matches ("badword" == "badword")
if ($censoredWord == $word) {
// store censored word
if (isset($matches[$word])) {
$matches[$word]++;
} else {
$matches[$word] = 1;
}
continue 2;
} // check for asterisk matches ("*badword*" == "FooBadwordBar")
elseif (\str_contains($censoredWord, '*')) {
$censoredWord = \str_replace('\*', '.*', \preg_quote($censoredWord, '!'));
if (\preg_match('!^' . $censoredWord . '$!', $word)) {
// store censored word
if (isset($matches[$word])) {
$matches[$word]++;
} else {
$matches[$word] = 1;
}
continue 2;
}
} // check for partial matches ("~badword~" == "bad-word")
elseif (\str_contains($censoredWord, '~')) {
$censoredWord = \str_replace('~', '', $censoredWord);
if (($position = \mb_strpos($censoredWord, $word)) !== false) {
if ($position > 0) {
// look behind
if (!$this->lookBehind($i - 1, \mb_substr($censoredWord, 0, $position))) {
continue;
}
}
if ($position + \mb_strlen($word) < \mb_strlen($censoredWord)) {
// look ahead
if (
$newIndex = $this->lookAhead(
$i + 1,
\mb_substr($censoredWord, $position + \mb_strlen($word))
)
) {
$i = $newIndex;
} else {
continue;
}
}
// store censored word
if (isset($matches[$displayedCensoredWord])) {
$matches[$displayedCensoredWord]++;
} else {
$matches[$displayedCensoredWord] = 1;
}
continue 2;
}
}
}
}
// at least one censored word was found
if (\count($matches) > 0) {
return $matches;
} // text is clean
else {
return false;
}
}
/**
* Looks behind in the word list.
*
* @param int $index
* @param string $search
* @return bool
*/
protected function lookBehind($index, $search)
{
if (isset($this->words[$index])) {
if (
\mb_strpos(
$this->words[$index],
$search
) === (\mb_strlen($this->words[$index]) - \mb_strlen($search))
) {
return true;
} elseif (
\mb_strpos(
$search,
$this->words[$index]
) === (\mb_strlen($search) - \mb_strlen($this->words[$index]))
) {
return $this->lookBehind(
$index - 1,
\mb_substr($search, 0, \mb_strlen($search) - \mb_strlen($this->words[$index]))
);
}
}
return false;
}
/**
* Looks ahead in the word list.
*
* @param int $index
* @param string $search
* @return mixed
*/
protected function lookAhead($index, $search)
{
if (isset($this->words[$index])) {
if (\str_starts_with($this->words[$index], $search)) {
return $index;
} elseif (\str_starts_with($search, $this->words[$index])) {
return $this->lookAhead($index + 1, \mb_substr($search, \mb_strlen($this->words[$index])));
}
}
return false;
}
}