|
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>"; |
|
|
|
|
|
|
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)), |
|
|
|
|
|
|
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)); |
|
|
|
|
|
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
return true; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Used across setOption() |
|
114
|
|
|
* @param type $lang |
|
|
|
|
|
|
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
|
|
|
|