Passed
Push — 7.x ( 46400b...fb51d4 )
by Torben
03:19
created

CaptchaValidator::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
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;
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...
21
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...
22
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...
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()
43
    {
44
        $this->configurationManager = GeneralUtility::makeInstance(ConfigurationManagerInterface::class);
45
        $this->requestFactory = GeneralUtility::makeInstance(RequestFactory::class);
46
47
        $this->settings = $this->configurationManager->getConfiguration(
48
            ConfigurationManagerInterface::CONFIGURATION_TYPE_SETTINGS,
49
            'SfEventMgt',
50
            'Pieventregistration'
51
        );
52
    }
53
54
    /**
55
     * @param Registration $value Registration
56
     */
57
    protected function isValid(mixed $value): void
58
    {
59
        $configurationService = new CaptchaConfigurationService($this->settings['registration']['captcha'] ?? []);
60
61
        if (!$configurationService->getEnabled()) {
62
            return;
63
        }
64
65
        $parsedBody = $this->getRequest()->getParsedBody();
66
        $captchaFormFieldValue = $parsedBody[$configurationService->getResponseField()] ?? null;
67
        if ($captchaFormFieldValue === null) {
68
            $this->addError(
69
                LocalizationUtility::translate('validation.missing_captcha', 'SfEventMgt'),
70
                1631943016
71
            );
72
            return;
73
        }
74
75
        $url = HttpUtility::buildUrl(
76
            [
77
                'host' => $configurationService->getVerificationServer(),
78
                'query' => \http_build_query(
79
                    [
80
                        'secret' => $configurationService->getPrivateKey(),
81
                        'response' => $captchaFormFieldValue,
82
                        'remoteip' => $this->getRequest()->getAttribute('normalizedParams')->getRemoteAddress(),
83
                    ]
84
                ),
85
            ]
86
        );
87
88
        $response = $this->requestFactory->request($url, 'POST');
89
90
        $body = (string)$response->getBody();
91
        $responseArray = json_decode($body, true);
92
        if (!is_array($responseArray) || empty($responseArray) || $responseArray['success'] === false) {
93
            $this->addError(
94
                $this->translateErrorMessage('validation.possible_robot', 'SfEventMgt'),
95
                1631940277
96
            );
97
        }
98
    }
99
100
    protected function getRequest(): ServerRequestInterface
101
    {
102
        return $GLOBALS['TYPO3_REQUEST'];
103
    }
104
}
105