1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Extension "sf_event_mgt" for TYPO3 CMS. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the |
9
|
|
|
* LICENSE.txt file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace DERHANSEN\SfEventMgt\Validation\Validator; |
13
|
|
|
|
14
|
|
|
use DERHANSEN\SfEventMgt\Domain\Model\Registration; |
15
|
|
|
use DERHANSEN\SfEventMgt\Service\CaptchaConfigurationService; |
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
17
|
|
|
use TYPO3\CMS\Core\Http\RequestFactory; |
18
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
19
|
|
|
use TYPO3\CMS\Core\Utility\HttpUtility; |
20
|
|
|
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface; |
21
|
|
|
use TYPO3\CMS\Extbase\Utility\LocalizationUtility; |
22
|
|
|
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Validator for either reCaptcha or hCaptcha |
26
|
|
|
*/ |
27
|
|
|
class CaptchaValidator extends AbstractValidator |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* This validator always needs to be executed even if the given value is empty. |
31
|
|
|
* See AbstractValidator::validate() |
32
|
|
|
* |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $acceptsEmptyValues = false; |
36
|
|
|
|
37
|
|
|
protected ConfigurationManagerInterface $configurationManager; |
38
|
|
|
protected RequestFactory $requestFactory; |
39
|
|
|
|
40
|
|
|
protected array $settings; |
41
|
|
|
|
42
|
|
|
public function __construct(array $options = []) |
43
|
|
|
{ |
44
|
|
|
parent::__construct($options); |
45
|
|
|
$this->configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class); |
46
|
|
|
$this->requestFactory = GeneralUtility::makeInstance(RequestFactory::class); |
47
|
|
|
|
48
|
|
|
$this->settings = $this->configurationManager->getConfiguration( |
49
|
|
|
ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS, |
50
|
|
|
'SfEventMgt', |
51
|
|
|
'Pieventregistration' |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param Registration $value Registration |
57
|
|
|
*/ |
58
|
|
|
protected function isValid($value) |
59
|
|
|
{ |
60
|
|
|
$configurationService = new CaptchaConfigurationService($this->settings['registration']['captcha'] ?? []); |
61
|
|
|
|
62
|
|
|
if (!$configurationService->getEnabled()) { |
63
|
|
|
return; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** @var ServerRequestInterface $request */ |
67
|
|
|
$request = $GLOBALS['TYPO3_REQUEST']; |
68
|
|
|
$parsedBody = $request->getParsedBody(); |
69
|
|
|
$captchaFormFieldValue = $parsedBody[$configurationService->getResponseField()] ?? null; |
70
|
|
|
if (null === $captchaFormFieldValue) { |
71
|
|
|
$this->addError( |
72
|
|
|
LocalizationUtility::translate('validation.missing_captcha', 'SfEventMgt'), |
73
|
|
|
1631943016 |
74
|
|
|
); |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$url = HttpUtility::buildUrl( |
79
|
|
|
[ |
80
|
|
|
'host' => $configurationService->getVerificationServer(), |
81
|
|
|
'query' => \http_build_query( |
82
|
|
|
[ |
83
|
|
|
'secret' => $configurationService->getPrivateKey(), |
84
|
|
|
'response' => $captchaFormFieldValue, |
85
|
|
|
'remoteip' => $request->getAttribute('normalizedParams')->getRemoteAddress(), |
86
|
|
|
] |
87
|
|
|
), |
88
|
|
|
] |
89
|
|
|
); |
90
|
|
|
|
91
|
|
|
$response = $this->requestFactory->request($url, 'POST'); |
92
|
|
|
|
93
|
|
|
$body = (string)$response->getBody(); |
94
|
|
|
$responseArray = json_decode($body, true); |
95
|
|
|
if (!is_array($responseArray) || empty($responseArray) || $responseArray['success'] === false) { |
96
|
|
|
$this->addError( |
97
|
|
|
$this->translateErrorMessage('validation.possible_robot', 'SfEventMgt'), |
98
|
|
|
1631940277 |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|