Completed
Push — payment ( a2694a )
by Torben
42:06
created

RecaptchaValidator::isValid()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 50
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 50
rs 6.3636
cc 8
eloc 31
nc 6
nop 1
1
<?php
2
namespace DERHANSEN\SfEventMgt\Validation\Validator;
3
4
/**
5
 * This file is part of the TYPO3 CMS project.
6
 *
7
 * It is free software; you can redistribute it and/or modify it under
8
 * the terms of the GNU General Public License, either version 2
9
 * of the License, or any later version.
10
 *
11
 * For the full copyright and license information, please read the
12
 * LICENSE.txt file that was distributed with this source code.
13
 *
14
 * The TYPO3 project - inspiring people to share!
15
 */
16
17
use TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface;
18
use TYPO3\CMS\Fluid\Core\ViewHelper\Exception\InvalidVariableException;
19
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;
20
21
/**
22
 * Validator for recaptcha.
23
 */
24
class RecaptchaValidator extends \TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
25
{
26
27
    /**
28
     * This validator always needs to be executed even if the given value is empty.
29
     * See AbstractValidator::validate()
30
     *
31
     * @var boolean
32
     */
33
    protected $acceptsEmptyValues = false;
34
35
    /**
36
     * Object Manager
37
     *
38
     * @var \TYPO3\CMS\Extbase\Object\ObjectManager
39
     * @inject
40
     */
41
    protected $objectManager;
42
43
    /**
44
     * Checks if the given value is a valid recaptcha.
45
     *
46
     * @param mixed $value The value that should be validated
47
     * @throws InvalidVariableException
48
     */
49
    public function isValid($value)
50
    {
51
        $response = \TYPO3\CMS\Core\Utility\GeneralUtility::_GP('g-recaptcha-response');
52
        if ($response !== null) {
53
            // Only check if a response is set
54
            $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Configuration\\ConfigurationManager');
55
            $fullTs = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
56
            $reCaptchaSettings = $fullTs['plugin.']['tx_sfeventmgt.']['settings.']['reCaptcha.'];
57
58
            if (isset($reCaptchaSettings) &&
59
                is_array($reCaptchaSettings) &&
60
                isset($reCaptchaSettings['secretKey']) &&
61
                $reCaptchaSettings['secretKey']
62
            ) {
63
                $ch = curl_init();
64
65
                $fields = [
66
                    'secret' => $reCaptchaSettings['secretKey'],
67
                    'response' => $response
68
                ];
69
70
                // url-ify the data for the POST
71
                $fieldsString = '';
72
                foreach ($fields as $key => $value) {
73
                    $fieldsString .= $key . '=' . $value . '&';
74
                }
75
                rtrim($fieldsString, '&');
76
77
                // set the url, number of POST vars, POST data
78
                curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify');
79
                curl_setopt($ch, CURLOPT_POST, count($fields));
80
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
81
                curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsString);
82
83
                // execute post
84
                $resultCH = json_decode(curl_exec($ch));
85
                if (!(bool)$resultCH->success) {
86
                    $this->addError(
87
                        LocalizationUtility::translate('validation.possible_robot', 'sf_event_mgt'),
88
                        1231423345
89
                    );
90
                }
91
            } else {
92
                throw new InvalidVariableException(
93
                    LocalizationUtility::translate('error.no_secretKey', 'sf_event_mgt'),
94
                    1358349150
95
                );
96
            }
97
        }
98
    }
99
}
100