forked from sperti/recaptchav2phplib
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecaptchalib.php
More file actions
163 lines (136 loc) · 5 KB
/
Copy pathrecaptchalib.php
File metadata and controls
163 lines (136 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<?php
/*
* This is a PHP library that handles calling reCAPTCHA v2.
* - Get a reCAPTCHA API Key
* https://www.google.com/recaptcha/admin
* - Discussion group
* http://groups.google.com/group/recaptcha
*
* Copyright (c) 2007 reCAPTCHA -- https://www.google.com/recaptcha/intro/index.html
* AUTHOR:
* Claudio Sperti
* Roman Stefko
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/**
* The reCAPTCHA server URL's
*/
define("RECAPTCHA_VERIFY_SERVER", "https://www.google.com/recaptcha/api/siteverify");
/**
* Submits an HTTP POST to a reCAPTCHA server
* @param string $url
* @param array $data
* @return json response
*/
function _recaptcha_https_post($url, $data) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$result = curl_exec($ch);
$resultJson = json_decode($result);
return $resultJson;
}
/**
* Gets the challenge HTML (javascript and non-javascript version).
* This is called from the browser, and the resulting reCAPTCHA HTML widget
* is embedded within the HTML form it was called from.
* @param string $pubkey A public key for reCAPTCHA
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @return string - The HTML to be embedded in the user's form.
*/
function recaptcha_get_html ($pubkey)
{
if ($pubkey == null || $pubkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin'>https://www.google.com/recaptcha/admin</a>");
}
return '<script src="https://www.google.com/recaptcha/api.js" async defer></script>
<div class="g-recaptcha" data-sitekey="' . $pubkey . '"></div>';
}
/**
* A ReCaptchaResponse is returned from recaptcha_check_answer()
*/
class ReCaptchaResponse {
var $is_valid;
var $error_code;
var $error;
function set_error($error_code)
{
$this->error_code = $error_code;
switch ($error_code)
{
case "missing-input-secret":
$this->error = "The secret parameter is missing.";
break;
case "invalid-input-secret":
$this->error = "The secret parameter is invalid or malformed.";
break;
case "missing-input-response":
$this->error = "The response parameter is missing.";
break;
case "invalid-input-response":
$this->error = "The response parameter is invalid or malformed.";
break;
case "bad-request":
$this->error = "The request is invalid or malformed.";
break;
default:
$this->error = $error_code;
break;
}
}
}
/**
* Calls an HTTPS POST function to verify if the user's guess was correct
* @param string $privkey
* @param string $remoteip
* @param string $response
* @param array $extra_params an array of extra variables to post to the server
* @return ReCaptchaResponse
*/
function recaptcha_check_answer ($privkey, $remoteip, $response, $extra_params = array())
{
if ($privkey == null || $privkey == '') {
die ("To use reCAPTCHA you must get an API key from <a href='https://www.google.com/recaptcha/admin'>https://www.google.com/recaptcha/admin</a>");
}
// Discard spam submissions
$recaptcha_response = new ReCaptchaResponse();
if ($response == null || strlen($response) == 0) {
$recaptcha_response->is_valid = false;
$recaptcha_response->set_error('missing-input-response');
return $recaptcha_response;
}
$response = _recaptcha_https_post (
RECAPTCHA_VERIFY_SERVER,
array (
'secret' => $privkey,
'remoteip' => $remoteip,
'response' => $response
) + $extra_params
);
$recaptcha_response->is_valid = $response->success;
if (property_exists($response, "error-codes"))
$recaptcha_response->set_error($response->{'error-codes'}[0]);
return $recaptcha_response;
}
?>