1
|
|
|
<?php |
2
|
|
|
namespace DERHANSEN\SfEventMgt\Validation\Validator; |
3
|
|
|
|
4
|
|
|
/* |
5
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please read the |
8
|
|
|
* LICENSE.txt file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
12
|
|
|
use DERHANSEN\SfEventMgt\Service\SpamCheckService; |
13
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
14
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
15
|
|
|
use TYPO3\CMS\Extbase\Validation\Error; |
16
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator; |
17
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* RegistrationValidator |
21
|
|
|
* |
22
|
|
|
* @author Torben Hansen <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class RegistrationValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Configuration Manager |
28
|
|
|
* |
29
|
|
|
* @var \TYPO3\CMS\Extbase\Configuration\ConfigurationManager |
30
|
|
|
*/ |
31
|
|
|
protected $configurationManager; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Object Manager |
35
|
|
|
* |
36
|
|
|
* @var \TYPO3\CMS\Extbase\Object\ObjectManagerInterface |
37
|
|
|
*/ |
38
|
|
|
protected $objectManager; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* DI for $configurationManager |
42
|
|
|
* |
43
|
|
|
* @param \TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager |
44
|
|
|
*/ |
45
|
|
|
public function injectConfigurationManager( |
46
|
|
|
\TYPO3\CMS\Extbase\Configuration\ConfigurationManager $configurationManager |
47
|
|
|
) { |
48
|
|
|
$this->configurationManager = $configurationManager; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* DI for $objectManager |
53
|
|
|
* |
54
|
|
|
* @param \TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager |
55
|
18 |
|
*/ |
56
|
|
|
public function injectObjectManager(\TYPO3\CMS\Extbase\Object\ObjectManagerInterface $objectManager) |
57
|
18 |
|
{ |
58
|
18 |
|
$this->objectManager = $objectManager; |
59
|
18 |
|
} |
60
|
|
|
|
61
|
18 |
|
/** |
62
|
|
|
* Validates the given registration according to required fields set in plugin |
63
|
|
|
* settings. For boolean fields, the booleanValidator is used and it is assumed, |
64
|
18 |
|
* that boolean fields must have the value "TRUE" (for checkboxes) |
65
|
16 |
|
* |
66
|
18 |
|
* @param Registration $value Registration |
67
|
4 |
|
* |
68
|
|
|
* @return bool |
69
|
|
|
*/ |
70
|
14 |
|
protected function isValid($value) |
71
|
14 |
|
{ |
72
|
|
|
$settings = $this->configurationManager->getConfiguration( |
73
|
14 |
|
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, |
74
|
14 |
|
'SfEventMgt', |
75
|
12 |
|
'Pievent' |
76
|
|
|
); |
77
|
12 |
|
|
78
|
12 |
|
$spamSettings = $settings['registration']['spamCheck'] ?? []; |
79
|
4 |
|
if ((bool)$spamSettings['enabled'] && $this->isSpamCheckFailed($value, $spamSettings)) { |
80
|
4 |
|
$message = $this->translateErrorMessage('registration.spamCheckFailed', 'SfEventMgt'); |
81
|
4 |
|
$error = new Error($message, 1578855253965); |
82
|
4 |
|
$this->result->forProperty('spamCheck')->addError($error); |
83
|
4 |
|
|
84
|
12 |
|
return false; |
85
|
14 |
|
} |
86
|
|
|
|
87
|
14 |
|
// If no required fields are set, then the registration is valid |
88
|
|
|
if ($settings['registration']['requiredFields'] === '' || |
89
|
|
|
!isset($settings['registration']['requiredFields']) |
90
|
|
|
) { |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$requiredFields = array_map('trim', explode(',', $settings['registration']['requiredFields'])); |
95
|
|
|
$result = true; |
96
|
|
|
|
97
|
|
|
foreach ($requiredFields as $requiredField) { |
98
|
4 |
|
if ($value->_hasProperty($requiredField)) { |
99
|
|
|
$validator = $this->getValidator(gettype($value->_getProperty($requiredField)), $requiredField); |
100
|
|
|
/** @var \TYPO3\CMS\Extbase\Error\Result $validationResult */ |
101
|
4 |
|
$validationResult = $validator->validate($value->_getProperty($requiredField)); |
102
|
|
|
if ($validationResult->hasErrors()) { |
103
|
2 |
|
$result = false; |
104
|
2 |
|
foreach ($validationResult->getErrors() as $error) { |
105
|
2 |
|
$this->result->forProperty($requiredField)->addError($error); |
106
|
2 |
|
} |
107
|
2 |
|
} |
108
|
2 |
|
} |
109
|
2 |
|
} |
110
|
|
|
|
111
|
|
|
return $result; |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
/** |
115
|
|
|
* Processes the spam check and returns, if it failed or not |
116
|
2 |
|
* |
117
|
4 |
|
* @param Registration $registration |
118
|
|
|
* @param array $settings |
119
|
|
|
* @throws \DERHANSEN\SfEventMgt\SpamChecks\Exceptions\SpamCheckNotFoundException |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
protected function isSpamCheckFailed(Registration $registration, array $settings): bool |
123
|
|
|
{ |
124
|
|
|
$spamCheckService = new SpamCheckService( |
125
|
|
|
$registration, |
126
|
|
|
$settings, |
127
|
|
|
GeneralUtility::_GPmerged('tx_sfeventmgt_pievent') |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
return $spamCheckService->isSpamCheckFailed(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Returns a validator object depending on the given type of the property |
135
|
|
|
* |
136
|
|
|
* @param string $type Type |
137
|
|
|
* @param string $field The field |
138
|
|
|
* |
139
|
|
|
* @return \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator |
140
|
|
|
*/ |
141
|
|
|
protected function getValidator($type, $field) |
142
|
|
|
{ |
143
|
|
|
switch ($type) { |
144
|
|
|
case 'boolean': |
145
|
|
|
/** @var \TYPO3\CMS\Extbase\Validation\Validator\BooleanValidator $validator */ |
146
|
|
|
$validator = $this->objectManager->get( |
147
|
|
|
BooleanValidator::class, |
148
|
|
|
['is' => true] |
149
|
|
|
); |
150
|
|
|
break; |
151
|
|
|
default: |
152
|
|
|
if ($field == 'recaptcha') { |
153
|
|
|
/** @var \DERHANSEN\SfEventMgt\Validation\Validator\RecaptchaValidator $validator */ |
154
|
|
|
$validator = $this->objectManager->get(RecaptchaValidator::class); |
155
|
|
|
} else { |
156
|
|
|
/** @var \TYPO3\CMS\Extbase\Validation\Validator\NotEmptyValidator $validator */ |
157
|
|
|
$validator = $this->objectManager->get(NotEmptyValidator::class); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
return $validator; |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|