CaptchaValidator::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
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 TYPO3\CMS\Core\Http\RequestFactory;
17
use TYPO3\CMS\Core\Utility\GeneralUtility;
18
use TYPO3\CMS\Core\Utility\HttpUtility;
19
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Config...urationManagerInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
20
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Utility\LocalizationUtility was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
21
use TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator;
0 ignored issues
show
Bug introduced by
The type TYPO3\CMS\Extbase\Valida...dator\AbstractValidator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
23
/**
24
 * Validator for either reCaptcha or hCaptcha
25
 */
26
class CaptchaValidator extends AbstractValidator
27
{
28
    /**
29
     * This validator always needs to be executed even if the given value is empty, because else
30
     * the captcha can be bypassed.
31
     */
32
    protected $acceptsEmptyValues = false;
33
34
    protected RequestFactory $requestFactory;
35
36
    public function __construct(
37
        protected readonly ConfigurationManagerInterface $configurationManager
38
    ) {
39
        $this->requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
40
    }
41
42
    /**
43
     * @param Registration $value Registration
44
     */
45
    protected function isValid(mixed $value): void
46
    {
47
        $settings = $this->configurationManager->getConfiguration(
48
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
49
            'SfEventMgt',
50
            'Pieventregistration'
51
        );
52
53
        $configurationService = new CaptchaConfigurationService($settings['registration']['captcha'] ?? []);
54
55
        if (!$configurationService->getEnabled()) {
56
            return;
57
        }
58
59
        $parsedBody = $this->getRequest()->getParsedBody();
60
        $captchaFormFieldValue = $parsedBody[$configurationService->getResponseField()] ?? null;
61
        if ($captchaFormFieldValue === null) {
62
            $this->addError(
63
                LocalizationUtility::translate('LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.missing_captcha'),
64
                1631943016
65
            );
66
            return;
67
        }
68
69
        $url = HttpUtility::buildUrl(
70
            [
71
                'host' => $configurationService->getVerificationServer(),
72
                'query' => \http_build_query(
73
                    [
74
                        'secret' => $configurationService->getPrivateKey(),
75
                        'response' => $captchaFormFieldValue,
76
                        'remoteip' => $this->getRequest()->getAttribute('normalizedParams')->getRemoteAddress(),
77
                    ]
78
                ),
79
            ]
80
        );
81
82
        $response = $this->requestFactory->request($url, 'POST');
83
84
        $body = (string)$response->getBody();
85
        $responseArray = json_decode($body, true);
86
        if (!is_array($responseArray) || empty($responseArray) || $responseArray['success'] === false) {
87
            $this->addError(
88
                $this->translateErrorMessage('LLL:EXT:sf_event_mgt/Resources/Private/Language/locallang.xlf:validation.possible_robot'),
89
                1631940277
90
            );
91
        }
92
    }
93
}
94