Passed
Branch 3.x (80fbf8)
by Enjoys
01:46
created

reCaptcha::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/*
4
 * The MIT License
5
 *
6
 * Copyright 2020 Enjoys.
7
 *
8
 * Permission is hereby granted, free of charge, to any person obtaining a copy
9
 * of this software and associated documentation files (the "Software"), to deal
10
 * in the Software without restriction, including without limitation the rights
11
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the Software is
13
 * furnished to do so, subject to the following conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be included in
16
 * all copies or substantial portions of the Software.
17
 *
18
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24
 * THE SOFTWARE.
25
 */
26
27
declare(strict_types=1);
28
29
namespace Enjoys\Forms\Captcha\reCaptcha;
30
31
use Enjoys\Forms\Captcha\CaptchaBase;
32
use Enjoys\Forms\Captcha\CaptchaInterface;
33
use Enjoys\Forms\Element;
34
use GuzzleHttp\Client;
35
36
37
38
/**
39
 * Description of reCaptcha
40
 *
41
 * @author Enjoys
42
 */
43
class reCaptcha extends CaptchaBase implements CaptchaInterface
44
{
45
    private $privateKey = '6LdUGNEZAAAAAPPz685RwftPySFeCLbV1xYJJjsk'; //localhost
46
    private $publicKey = '6LdUGNEZAAAAANA5cPI_pCmOqbq-6_srRkcGOwRy'; //localhost
47
    private $verifyUrl = 'https://www.google.com/recaptcha/api/siteverify';
48
    private $errorCodes = [
49
        'missing-input-secret' => 'The secret parameter is missing.',
50
        'invalid-input-secret' => 'The secret parameter is invalid or malformed.',
51
        'missing-input-response' => 'The response parameter is missing.',
52
        'invalid-input-response' => 'The response parameter is invalid or malformed.',
53
        'bad-request' => 'The request is invalid or malformed.',
54
        'timeout-or-duplicate' => 'The response is no longer valid: either is too old or has been used previously.',
55
    ];
56
57
   
58
59
    public function __construct()
60
    {
61
        $this->setName('recaptcha2');
62
        $this->setRuleMessage(null);
63
    }
64
65
66
    /**
67
     * 
68
     * @param \Enjoys\Forms\Element $element
69
     * @return string
70
     */
71
    public function renderHtml(Element $element): string
72
    {
73
        $html = "<script src=\"https://www.google.com/recaptcha/api.js\" async defer></script>";
74
        $html .= "<div class=\"g-recaptcha\" data-sitekey=\"{$this->getOption('publickey', $this->getOption('publickey', $this->publicKey))}\"> </div>";
0 ignored issues
show
Bug introduced by
$this->publicKey of type string is incompatible with the type Enjoys\Traits\type expected by parameter $defaults of Enjoys\Forms\Captcha\CaptchaBase::getOption(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

74
        $html .= "<div class=\"g-recaptcha\" data-sitekey=\"{$this->getOption('publickey', $this->getOption('publickey', /** @scrutinizer ignore-type */ $this->publicKey))}\"> </div>";
Loading history...
75
        return $html;
76
    }
77
78
    /**
79
     * 
80
     * @param \Enjoys\Forms\Element $element
81
     * @return bool
82
     */
83
    public function validate(Element $element): bool
84
    {
85
        $client = $this->getOption('httpClient', $this->getGuzzleClient());
86
87
        $data = array(
88
            'secret' => $this->getOption('privatekey', $this->getOption('privatekey', $this->privateKey)),
0 ignored issues
show
Bug introduced by
$this->privateKey of type string is incompatible with the type Enjoys\Traits\type expected by parameter $defaults of Enjoys\Forms\Captcha\CaptchaBase::getOption(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

88
            'secret' => $this->getOption('privatekey', $this->getOption('privatekey', /** @scrutinizer ignore-type */ $this->privateKey)),
Loading history...
89
            'response' => $this->getRequest()->post('g-recaptcha-response', $this->getRequest()->get('g-recaptcha-response'))
90
        );
91
92
93
94
        $response = $client->request('POST', $this->getOption('verify_url', $this->verifyUrl), [
95
            'form_params' => $data
96
        ]);
97
98
        $responseBody = \json_decode($response->getBody()->getContents());
99
100
101
        if ($responseBody->success === false) {
102
            $errors = [];
103
            foreach ($responseBody->{'error-codes'} as $error) {
104
                $errors[] = $this->errorCodes[$error];
105
            }
106
            $element->setRuleError(implode(', ', $errors));
0 ignored issues
show
Bug introduced by
The method setRuleError() does not exist on Enjoys\Forms\Element. It seems like you code against a sub-type of said class. However, the method does not exist in Enjoys\Forms\Elements\Optgroup or Enjoys\Forms\Elements\Submit or Enjoys\Forms\Elements\Option or Enjoys\Forms\Elements\Button or Enjoys\Forms\Elements\Image or Enjoys\Forms\Elements\Reset or Enjoys\Forms\Elements\Header. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
            $element->/** @scrutinizer ignore-call */ 
107
                      setRuleError(implode(', ', $errors));
Loading history...
107
            return false;
108
        }
109
        return true;
110
    }
111
112
    /**
113
     * Used across setOption()
114
     * @param type $lang
0 ignored issues
show
Bug introduced by
The type Enjoys\Forms\Captcha\reCaptcha\type 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...
115
     */
116
    public function setLanguage($lang)
117
    {
118
        $file_language = __DIR__ . '/lang/' . \strtolower($lang) . '.php';
119
120
        if (file_exists($file_language)) {
121
            $this->errorCodes = include $file_language;
122
        }
123
    }
124
    
125
 
126
    private function getGuzzleClient()
127
    {
128
        return new Client();
129
    }
130
}
131