|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GoogleExt; |
|
4
|
|
|
|
|
5
|
|
|
use Extender\request; |
|
6
|
|
|
use JSON\json; |
|
7
|
|
|
|
|
8
|
|
|
class recaptcha |
|
9
|
|
|
{ |
|
10
|
|
|
public $secret = '6LeLW-MUAAAAADaHQWVpUV5CqjNymO0cu_gbL0vv'; |
|
11
|
|
|
public $siteKey = '6LeLW-MUAAAAALgiXAKP0zo2oslXXbCy57CjFcie'; |
|
12
|
|
|
public static $secretKey; |
|
13
|
|
|
private static $_instance = null; |
|
14
|
|
|
|
|
15
|
|
|
public static function getInstance() |
|
16
|
|
|
{ |
|
17
|
|
|
if (null === self::$_instance) { |
|
18
|
|
|
self::$_instance = new self(); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
return self::$_instance; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
public function set_secret($key) |
|
25
|
|
|
{ |
|
26
|
|
|
$this->secret = $key; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function setSecret($key) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->set_secret($key); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public static function verifyCaptchaOld($callback = null, $error = null) |
|
|
|
|
|
|
35
|
|
|
{ |
|
36
|
|
|
$opt['url'] = 'https://www.google.com/recaptcha/api/siteverify?secret=' . self::getInstance()->secret . '&response=' . $_REQUEST['g-recaptcha-response']; |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$req = request::static_request($opt); |
|
39
|
|
|
|
|
40
|
|
|
exit(var_dump($req)); |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
if (isset($req['response']['body']['success']) && false !== $req['response']['body']['success']) { |
|
|
|
|
|
|
43
|
|
|
if ($req['response']['body']['success']) { |
|
44
|
|
|
if (is_callable($callback)) { |
|
45
|
|
|
return call_user_func($callback); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
} elseif (is_callable($error)) { |
|
49
|
|
|
return call_user_func($error); |
|
50
|
|
|
} else { |
|
51
|
|
|
if (isset($req['response']['body'])) { |
|
52
|
|
|
$req = $req['response']['body']; |
|
53
|
|
|
} |
|
54
|
|
|
json::json($req); |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public static function verifyCaptcha($callback) |
|
59
|
|
|
{ |
|
60
|
|
|
if (isset($_SERVER['HTTP_CF_CONNECTING_IP'])) { |
|
61
|
|
|
$_SERVER['REMOTE_ADDR'] = $_SERVER['HTTP_CF_CONNECTING_IP']; |
|
62
|
|
|
} |
|
63
|
|
|
if (preg_match('/^192|^127/s', $_SERVER['REMOTE_ADDR'])) { |
|
64
|
|
|
//return call_user_func($callback, true); |
|
65
|
|
|
} |
|
66
|
|
|
if (!isset($_POST['g-recaptcha-response'])) { |
|
67
|
|
|
if (ob_get_level()) { |
|
68
|
|
|
ob_end_clean(); |
|
69
|
|
|
} |
|
70
|
|
|
\JSON\json::json(['error' => true, 'message' => 'Recaptcha token required']); |
|
71
|
|
|
exit; |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
// Verify captcha |
|
74
|
|
|
$post_data = http_build_query( |
|
75
|
|
|
[ |
|
76
|
|
|
'secret' => self::getInstance()->secret, |
|
77
|
|
|
'response' => $_POST['g-recaptcha-response'], |
|
78
|
|
|
'remoteip' => $_SERVER['REMOTE_ADDR'], |
|
79
|
|
|
] |
|
80
|
|
|
); |
|
81
|
|
|
$opts = [ |
|
82
|
|
|
'http' => [ |
|
83
|
|
|
'method' => 'POST', |
|
84
|
|
|
'header' => 'Content-type: application/x-www-form-urlencoded', |
|
85
|
|
|
'content' => $post_data, |
|
86
|
|
|
], |
|
87
|
|
|
]; |
|
88
|
|
|
$context = stream_context_create($opts); |
|
89
|
|
|
$response = file_get_contents('https://www.google.com/recaptcha/api/siteverify', false, $context); |
|
90
|
|
|
$result = json_decode($response, true); |
|
91
|
|
|
|
|
92
|
|
|
if (!$result['success']) { |
|
93
|
|
|
$result['error'] = true; |
|
94
|
|
|
unset($result['success']); |
|
95
|
|
|
$result['message'] = 'Captcha not valid, please reload the page or submit the form again'; |
|
96
|
|
|
$result['title'] = 'reCaptcha information'; |
|
97
|
|
|
json::json($result); |
|
98
|
|
|
exit; |
|
|
|
|
|
|
99
|
|
|
} else { |
|
100
|
|
|
return call_user_func($callback, true); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.