1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Shieldon package. |
4
|
|
|
* |
5
|
|
|
* (c) Terry L. <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
declare(strict_types=1); |
12
|
|
|
|
13
|
|
|
namespace Shieldon\Firewall\Captcha; |
14
|
|
|
|
15
|
|
|
use Shieldon\Firewall\Captcha\CaptchaProvider; |
16
|
|
|
|
17
|
|
|
use function Shieldon\Firewall\get_request; |
18
|
|
|
use function Shieldon\Firewall\unset_superglobal; |
19
|
|
|
use function curl_error; |
20
|
|
|
use function curl_exec; |
21
|
|
|
use function curl_init; |
22
|
|
|
use function curl_setopt; |
23
|
|
|
use function json_decode; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Google reCaptcha. |
27
|
|
|
*/ |
28
|
|
|
class ReCaptcha extends CaptchaProvider |
29
|
|
|
{ |
30
|
|
|
protected $key = ''; |
31
|
|
|
protected $secret = ''; |
32
|
|
|
protected $version = 'v2'; |
33
|
|
|
protected $lang = 'en'; |
34
|
|
|
|
35
|
|
|
protected $googleServiceUrl = 'https://www.google.com/recaptcha/api/siteverify'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Constructor. |
39
|
|
|
* |
40
|
|
|
* It will implement default configuration settings here. |
41
|
|
|
* |
42
|
|
|
* @array $config |
43
|
|
|
* |
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
|
|
public function __construct(array $config = []) |
47
|
|
|
{ |
48
|
|
|
parent::__construct(); |
49
|
|
|
|
50
|
|
|
foreach ($config as $k => $v) { |
51
|
|
|
if (isset($this->{$k})) { |
52
|
|
|
$this->{$k} = $v; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Response the result from Google service server. |
59
|
|
|
* |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function response(): bool |
63
|
|
|
{ |
64
|
|
|
$postParams = get_request()->getParsedBody(); |
65
|
|
|
|
66
|
|
|
if (empty($postParams['g-recaptcha-response'])) { |
67
|
|
|
return false; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$flag = false; |
71
|
|
|
$reCaptchaToken = str_replace(["'", '"'], '', $postParams['g-recaptcha-response']); |
72
|
|
|
|
73
|
|
|
$postData = [ |
74
|
|
|
'secret' => $this->secret, |
75
|
|
|
'response' => $reCaptchaToken, |
76
|
|
|
]; |
77
|
|
|
|
78
|
|
|
$ch = curl_init(); |
79
|
|
|
curl_setopt($ch, CURLOPT_URL, $this->googleServiceUrl); |
|
|
|
|
80
|
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); |
81
|
|
|
curl_setopt($ch, CURLOPT_POST, 2); |
82
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); |
83
|
|
|
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); |
84
|
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); |
85
|
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
86
|
|
|
|
87
|
|
|
$ret = curl_exec($ch); |
|
|
|
|
88
|
|
|
|
89
|
|
|
// @codeCoverageIgnoreStart |
90
|
|
|
if (curl_errno($ch)) { |
|
|
|
|
91
|
|
|
echo 'error:' . curl_error($ch); |
|
|
|
|
92
|
|
|
} |
93
|
|
|
// @codeCoverageIgnoreEnd |
94
|
|
|
|
95
|
|
|
if (isset($ret) && $ret != false) { |
|
|
|
|
96
|
|
|
$tmp = json_decode($ret); |
|
|
|
|
97
|
|
|
if ($tmp->success == true) { |
98
|
|
|
$flag = true; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
curl_close($ch); |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Prevent detecting POST method on RESTful frameworks. |
105
|
|
|
unset_superglobal('g-recaptcha-response', 'post'); |
106
|
|
|
|
107
|
|
|
return $flag; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Output a required HTML for reCaptcha v2. |
112
|
|
|
* |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public function form(): string |
116
|
|
|
{ |
117
|
|
|
$html = '<div>'; |
118
|
|
|
$html .= '<div style="display: inline-block">'; |
119
|
|
|
if ('v3' !== $this->version) { |
120
|
|
|
$html .= '<script src="https://www.google.com/recaptcha/api.js?hl=' . $this->lang . '"></script>'; |
121
|
|
|
$html .= '<div class="g-recaptcha" data-sitekey="' . $this->key . '"></div>'; |
122
|
|
|
} else { |
123
|
|
|
$html .= '<input type="hidden" name="g-recaptcha-response" id="g-recaptcha-response" value="">'; |
124
|
|
|
$html .= '<script src="https://www.google.com/recaptcha/api.js?render=' . $this->key . '&hl=' . $this->lang . '"></script>'; |
125
|
|
|
$html .= '<script>'; |
126
|
|
|
$html .= ' grecaptcha.ready(function() {'; |
127
|
|
|
$html .= ' grecaptcha.execute("' . $this->key . '", {action: "homepage"}).then(function(token) {'; |
128
|
|
|
$html .= ' document.getElementById("g-recaptcha-response").value = token;'; |
129
|
|
|
$html .= ' }); '; |
130
|
|
|
$html .= ' });'; |
131
|
|
|
$html .= '</script>'; |
132
|
|
|
} |
133
|
|
|
$html .= '</div>'; |
134
|
|
|
$html .= '</div>'; |
135
|
|
|
|
136
|
|
|
return $html; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|