|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
6
|
|
|
|
|
7
|
|
|
class BlacklistValidator extends ValidatorAbstract |
|
8
|
|
|
{ |
|
9
|
1 |
|
public function isValid(): bool |
|
10
|
|
|
{ |
|
11
|
1 |
|
$target = implode("\n", array_filter([ |
|
12
|
1 |
|
$this->request->name, |
|
13
|
1 |
|
$this->request->content, |
|
14
|
1 |
|
$this->request->email, |
|
15
|
1 |
|
$this->request->ip_address, |
|
16
|
1 |
|
$this->request->title, |
|
17
|
1 |
|
])); |
|
18
|
1 |
|
$isValid = $this->validateBlacklist($target); |
|
19
|
1 |
|
return glsr()->filterBool('validate/blacklist', $isValid, $target, $this->request); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
1 |
|
public function performValidation(): void |
|
23
|
|
|
{ |
|
24
|
1 |
|
if (!$this->isValid()) { |
|
25
|
1 |
|
if ('reject' !== glsr_get_option('forms.blacklist.action')) { |
|
26
|
1 |
|
glsr()->sessionSet('form_blacklisted', true); |
|
27
|
1 |
|
return; |
|
28
|
|
|
} |
|
29
|
1 |
|
$this->fail( |
|
30
|
1 |
|
__('Your review cannot be submitted at this time.', 'site-reviews'), |
|
31
|
1 |
|
'Blacklisted submission detected.' |
|
32
|
1 |
|
); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
protected function blacklist(): string |
|
37
|
|
|
{ |
|
38
|
1 |
|
return 'comments' === glsr_get_option('forms.blacklist.integration') |
|
39
|
1 |
|
? trim(glsr(OptionManager::class)->wp('disallowed_keys')) |
|
40
|
1 |
|
: trim(glsr_get_option('forms.blacklist.entries')); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
1 |
|
protected function validateBlacklist(string $target): bool |
|
44
|
|
|
{ |
|
45
|
1 |
|
if (empty($blacklist = $this->blacklist())) { |
|
46
|
1 |
|
return true; |
|
47
|
|
|
} |
|
48
|
1 |
|
$lines = explode("\n", $blacklist); |
|
49
|
1 |
|
foreach ((array) $lines as $line) { |
|
50
|
1 |
|
$line = trim($line); |
|
51
|
1 |
|
if (empty($line) || 256 < strlen($line)) { |
|
52
|
1 |
|
continue; |
|
53
|
|
|
} |
|
54
|
1 |
|
$pattern = sprintf('#%s#iu', preg_quote($line, '#')); |
|
55
|
1 |
|
if (preg_match($pattern, $target)) { |
|
56
|
1 |
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
1 |
|
return true; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|