GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 6cc562...95167d )
by Sebastian
02:09
created

ReCaptchaField::getBadge()   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
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kmedia\ReCaptcha;
4
5
use Locale;
6
use SilverStripe\Control\Controller;
7
use SilverStripe\Forms\FormField;
8
use SilverStripe\i18n\i18n;
9
use SilverStripe\ORM\FieldType\DBHTMLText;
10
use SilverStripe\View\Requirements;
11
12
class ReCaptchaField extends FormField
13
{
14
    /**
15
     * Captcha theme, currently options are light and dark
16
     * @config ReCaptchaField.theme
17
     * @default light
18
     * @var string
19
     */
20
    private static $theme = 'light';
21
    /**
22
     * Captcha size, currently options are normal, compact and invisible
23
     * @config ReCaptchaField.size
24
     * @default normal
25
     * @var string
26
     */
27
    private static $size = 'normal';
28
    /**
29
     * Captcha badge, currently options are bottomright, bottomleft and inline
30
     * @config ReCaptchaField.size
31
     * @default bottomright
32
     * @var string
33
     */
34
    private static $badge = 'bottomright';
35
    /**
36
     * Recaptcha Site Key - Configurable via Injector config
37
     */
38
    protected $siteKey;
39
    /**
40
     * Recaptcha Secret Key - Configurable via Injector config
41
     */
42
    protected $secretKey;
43
44
    /**
45
     * Getter for siteKey
46
     * @return string
47
     */
48
    public function getSiteKey()
49
    {
50
        return $this->siteKey;
51
    }
52
53
    /**
54
     * Setter for siteKey to allow injector config to override the value
55
     */
56
    public function setSiteKey($siteKey)
57
    {
58
        $this->siteKey = $siteKey;
59
    }
60
61
    /**
62
     * Getter for secretKey
63
     * @return string
64
     */
65
    public function getSecretKey()
66
    {
67
        return $this->secretKey;
68
    }
69
70
    /**
71
     * Setter for secretKey to allow injector config to override the value
72
     * @param string $secretKey
73
     */
74
    public function setSecretKey($secretKey)
75
    {
76
        $this->secretKey = $secretKey;
77
    }
78
79
    /**
80
     * Getter for theme
81
     * @return string
82
     */
83
    public function getTheme()
84
    {
85
        return $this->config()->theme;
86
    }
87
88
    /**
89
     * Getter for size
90
     * @return string
91
     */
92
    public function getSize()
93
    {
94
        return $this->config()->size;
95
    }
96
97
    /**
98
     * Getter for badge
99
     * @return string
100
     */
101
    public function getBadge()
102
    {
103
        return $this->config()->badge;
104
    }
105
106
    /**
107
     * Adds the requirements and returns the form field.
108
     * @param array $properties
109
     * @return DBHTMLText
110
     */
111
    public function Field($properties = array())
112
    {
113
        if (empty($this->siteKey) || empty($this->secretKey)) {
114
            user_error('You must set SS_RECAPTCHA_SITE_KEY and SS_RECAPTCHA_SECRET_KEY environment.', E_USER_ERROR);
115
        }
116
117
        Requirements::customScript("var SS_LOCALE='" . Locale::getPrimaryLanguage(i18n::get_locale()) . "',ReCaptchaFormId='" . $this->getFormID() . "';");
118
        Requirements::javascript('kmedia/silverstripe-recaptcha:javascript/domReady.js');
119
        Requirements::javascript('kmedia/silverstripe-recaptcha:javascript/ReCaptchaField.js');
120
121
        return parent::Field($properties);
122
    }
123
124
    /**
125
     * Getter for the form's id
126
     * @return string
127
     */
128
    public function getFormID()
129
    {
130
        return $this->form ? $this->getTemplateHelper()->generateFormID($this->form) : null;
131
    }
132
133
    public function validate($validator)
134
    {
135
        $recaptchaResponse = Controller::curr()->getRequest()->requestVar('g-recaptcha-response');
136
        $response = json_decode($this->siteVerify($recaptchaResponse), true);
0 ignored issues
show
Bug introduced by
It seems like $this->siteVerify($recaptchaResponse) can also be of type false; however, parameter $json of json_decode() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

136
        $response = json_decode(/** @scrutinizer ignore-type */ $this->siteVerify($recaptchaResponse), true);
Loading history...
137
138
        return $this->verify($response, $validator);
139
    }
140
141
    private function verify($response, $validator)
142
    {
143
        if (is_array($response)) {
144
            if (array_key_exists('success', $response) && $response['success'] == false) {
145
                $validator->validationError(
146
                    $this->name,
147
                    _t('Kmedia\\ReCaptcha.EMPTY',
148
                        'Please answer the captcha, if you do not see the captcha please enable JavaScript.'),
149
                    'validation'
150
                );
151
                return false;
152
            }
153
        } else {
154
            $validator->validationError($this->name,
155
                _t('Kmedia\\ReCaptcha.VALIDATE_ERROR', 'Captcha could not be validated.'),
156
                'validation');
157
            return false;
158
        }
159
        return true;
160
    }
161
162
    private function siteVerify($token)
163
    {
164
        $url = 'https://www.google.com/recaptcha/api/siteverify?secret='
165
            . $this->secretKey . '&response=' . rawurlencode($token)
166
            . '&remoteip=' . rawurlencode($_SERVER['REMOTE_ADDR']);
167
168
        $ch = curl_init();
169
170
        if ($ch === false) {
171
            user_error('An error occurred when initializing cURL.', E_USER_ERROR);
172
            return false;
173
        }
174
175
        curl_setopt_array($ch, [
176
            CURLOPT_URL => $url,
177
            CURLOPT_TIMEOUT => 10,
178
            CURLOPT_RETURNTRANSFER => true,
179
            CURLOPT_SSL_VERIFYPEER => true,
180
        ]);
181
182
        $result = curl_exec($ch);
183
184
        if ($result === false) {
185
            user_error('An error occurred while cURL was being executed: ' . curl_error($ch), E_USER_ERROR);
186
            return false;
187
        }
188
189
        curl_close($ch);
190
        return (string)$result;
191
    }
192
}
193