|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Modules\Validator; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Arguments; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Contracts\ValidatorContract; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Cast; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Request; |
|
9
|
|
|
|
|
10
|
|
|
class ValidateForm |
|
11
|
|
|
{ |
|
12
|
|
|
protected Request $request; |
|
13
|
|
|
|
|
14
|
5 |
|
public function __construct() |
|
15
|
|
|
{ |
|
16
|
5 |
|
$this->request = new Request(); |
|
17
|
5 |
|
glsr()->sessionPluck('form_blacklisted'); |
|
18
|
5 |
|
glsr()->sessionPluck('form_errors'); |
|
19
|
5 |
|
glsr()->sessionPluck('form_invalid'); |
|
20
|
5 |
|
glsr()->sessionPluck('form_message'); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @compat |
|
25
|
|
|
* @todo remove in v8 |
|
26
|
|
|
*/ |
|
27
|
|
|
public function __get($property) |
|
28
|
|
|
{ |
|
29
|
|
|
$result = $this->result(); |
|
30
|
|
|
if ('message' === $property) { |
|
31
|
|
|
return $result->$property; |
|
32
|
|
|
} |
|
33
|
|
|
if ('errors' !== $property) { |
|
34
|
|
|
return; |
|
35
|
|
|
} |
|
36
|
|
|
if (!empty($result->errors)) { |
|
37
|
|
|
return $result->errors; |
|
38
|
|
|
} |
|
39
|
|
|
if (!$result->failed) { |
|
40
|
|
|
return false; // validation success |
|
41
|
|
|
} |
|
42
|
|
|
return []; // validation fail |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
5 |
|
public function result(): Arguments |
|
46
|
|
|
{ |
|
47
|
5 |
|
return glsr()->args([ |
|
48
|
5 |
|
'blacklisted' => glsr()->session()->cast('form_blacklisted', 'bool'), |
|
49
|
5 |
|
'errors' => glsr()->session()->array('form_errors'), |
|
50
|
5 |
|
'failed' => glsr()->session()->cast('form_invalid', 'bool'), |
|
51
|
5 |
|
'message' => glsr()->session()->cast('form_message', 'string'), |
|
52
|
5 |
|
]); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
5 |
|
public function isValid(): bool |
|
56
|
|
|
{ |
|
57
|
5 |
|
if (false === $this->result()->failed) { |
|
58
|
4 |
|
glsr()->sessionClear(); |
|
59
|
4 |
|
return true; |
|
60
|
|
|
} |
|
61
|
1 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return static |
|
66
|
|
|
*/ |
|
67
|
5 |
|
public function validate(Request $request, array $validatorClasses = []) |
|
68
|
|
|
{ |
|
69
|
5 |
|
$this->request = $request; |
|
70
|
5 |
|
$validators = $this->validators($validatorClasses); |
|
71
|
5 |
|
foreach ($validators as $validatorClass) { |
|
72
|
5 |
|
$validator = glsr($validatorClass, ['request' => $this->request])->validate(); |
|
73
|
5 |
|
$this->request = $validator->request(); |
|
74
|
|
|
} |
|
75
|
5 |
|
return $this; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
5 |
|
public function validators(array $validatorClasses = []): array |
|
79
|
|
|
{ |
|
80
|
5 |
|
$defaults = [ // order is intentional |
|
81
|
5 |
|
DefaultValidator::class, |
|
82
|
5 |
|
CustomValidator::class, |
|
83
|
5 |
|
SignatureValidator::class, |
|
84
|
5 |
|
PermissionValidator::class, |
|
85
|
5 |
|
DuplicateValidator::class, |
|
86
|
5 |
|
HoneypotValidator::class, |
|
87
|
5 |
|
ReviewLimitsValidator::class, |
|
88
|
5 |
|
BlacklistValidator::class, |
|
89
|
5 |
|
AkismetValidator::class, |
|
90
|
5 |
|
FriendlycaptchaValidator::class, |
|
91
|
5 |
|
HcaptchaValidator::class, |
|
92
|
5 |
|
ProcaptchaValidator::class, |
|
93
|
5 |
|
RecaptchaV2InvisibleValidator::class, |
|
94
|
5 |
|
RecaptchaV3Validator::class, |
|
95
|
5 |
|
TurnstileValidator::class, |
|
96
|
5 |
|
]; |
|
97
|
5 |
|
if (empty($validatorClasses)) { |
|
98
|
1 |
|
$validatorClasses = $defaults; |
|
99
|
|
|
} |
|
100
|
5 |
|
$validatorClasses = glsr()->filterArray('validators', $validatorClasses); |
|
101
|
5 |
|
$validators = []; |
|
102
|
5 |
|
foreach ($validatorClasses as $validatorClass) { |
|
103
|
|
|
try { |
|
104
|
5 |
|
$validator = new \ReflectionClass($validatorClass); |
|
105
|
|
|
} catch (\ReflectionException $e) { |
|
106
|
|
|
glsr_log()->error("Validator not found [$validatorClass]"); |
|
107
|
|
|
continue; |
|
108
|
|
|
} |
|
109
|
5 |
|
if (!$validator->implementsInterface(ValidatorContract::class)) { |
|
110
|
|
|
glsr_log()->error("Validator implementation invalid [$validatorClass]"); |
|
111
|
|
|
continue; |
|
112
|
|
|
} |
|
113
|
5 |
|
$validators[] = $validatorClass; |
|
114
|
|
|
} |
|
115
|
5 |
|
return $validators; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|