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