1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
|
|
|
|
4
|
|
|
* This is a PHP library that handles calling reCAPTCHA. |
5
|
|
|
* - Documentation and latest version |
6
|
|
|
* http://recaptcha.net/plugins/php/ |
7
|
|
|
* - Get a reCAPTCHA API Key |
8
|
|
|
* http://recaptcha.net/api/getkey |
9
|
|
|
* - Discussion group |
10
|
|
|
* http://groups.google.com/group/recaptcha |
11
|
|
|
* |
12
|
|
|
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net |
13
|
|
|
* AUTHORS: |
14
|
|
|
* Mike Crawford |
15
|
|
|
* Ben Maurer |
16
|
|
|
* |
17
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
18
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
19
|
|
|
* in the Software without restriction, including without limitation the rights |
20
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
21
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
22
|
|
|
* furnished to do so, subject to the following conditions: |
23
|
|
|
* |
24
|
|
|
* The above copyright notice and this permission notice shall be included in |
25
|
|
|
* all copies or substantial portions of the Software. |
26
|
|
|
* |
27
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
28
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
29
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
30
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
31
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
32
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
33
|
|
|
* THE SOFTWARE. |
34
|
|
|
*/ |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The reCAPTCHA server URL's |
38
|
|
|
*/ |
39
|
|
|
define('RECAPTCHA_API_SERVER', 'http://api.recaptcha.net'); |
40
|
|
|
define('RECAPTCHA_API_SECURE_SERVER', 'https://api-secure.recaptcha.net'); |
41
|
|
|
define('RECAPTCHA_VERIFY_SERVER', 'api-verify.recaptcha.net'); |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Encodes the given data into a query string format |
45
|
|
|
* @param $data - array of string elements to be encoded |
|
|
|
|
46
|
|
|
* @return string - encoded request |
47
|
|
|
*/ |
48
|
|
|
function _recaptcha_qsencode($data) { |
49
|
|
|
$req = ''; |
50
|
|
|
foreach ($data as $key => $value) { |
51
|
|
|
$req .= $key . '=' . urlencode(stripslashes($value)) . '&'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
// Cut the last '&' |
55
|
|
|
$req = substr($req, 0, strlen($req) - 1); |
56
|
|
|
return $req; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Submits an HTTP POST to a reCAPTCHA server |
61
|
|
|
* @param string $host |
62
|
|
|
* @param string $path |
63
|
|
|
* @param array $data |
64
|
|
|
* @param int port |
|
|
|
|
65
|
|
|
* @return array response |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
function _recaptcha_http_post($host, $path, $data, $port = 80) { |
68
|
|
|
|
69
|
|
|
$req = _recaptcha_qsencode($data); |
70
|
|
|
|
71
|
|
|
$http_request = "POST $path HTTP/1.0\r\n"; |
72
|
|
|
$http_request .= "Host: $host\r\n"; |
73
|
|
|
$http_request .= "Content-Type: application/x-www-form-urlencoded;\r\n"; |
74
|
|
|
$http_request .= 'Content-Length: ' . strlen($req) . "\r\n"; |
75
|
|
|
$http_request .= "User-Agent: reCAPTCHA/PHP\r\n"; |
76
|
|
|
$http_request .= "\r\n"; |
77
|
|
|
$http_request .= $req; |
78
|
|
|
|
79
|
|
|
$response = ''; |
80
|
|
|
if (false == ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) )) { |
|
|
|
|
81
|
|
|
die('Could not open socket'); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
fwrite($fs, $http_request); |
85
|
|
|
|
86
|
|
|
while (!feof($fs)) { |
87
|
|
|
$response .= fgets($fs, 1160); // One TCP-IP packet |
88
|
|
|
} fclose($fs); |
89
|
|
|
$response = explode("\r\n\r\n", $response, 2); |
90
|
|
|
|
91
|
|
|
return $response; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Gets the challenge HTML (javascript and non-javascript version). |
96
|
|
|
* This is called from the browser, and the resulting reCAPTCHA HTML widget |
97
|
|
|
* is embedded within the HTML form it was called from. |
98
|
|
|
* @param string $pubkey A public key for reCAPTCHA |
99
|
|
|
* @param string $error The error given by reCAPTCHA (optional, default is null) |
|
|
|
|
100
|
|
|
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false) |
101
|
|
|
|
102
|
|
|
* @return string - The HTML to be embedded in the user's form. |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
function recaptcha_get_html($pubkey, $error = null, $use_ssl = false) { |
105
|
|
|
|
106
|
|
|
if ($pubkey == null || $pubkey == '') { |
107
|
|
|
die("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>"); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
if ($use_ssl) { |
111
|
|
|
$server = RECAPTCHA_API_SECURE_SERVER; |
112
|
|
|
} else { |
113
|
|
|
$server = RECAPTCHA_API_SERVER; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$errorpart = ''; |
117
|
|
|
if ($error) { |
118
|
|
|
$errorpart = '&error=' . $error; |
119
|
|
|
} |
120
|
|
|
return '<script type="text/javascript" src="' . $server . '/challenge?k=' . $pubkey . $errorpart . '"></script> |
121
|
|
|
|
122
|
|
|
<noscript> |
123
|
|
|
<iframe src="' . $server . '/noscript?k=' . $pubkey . $errorpart . '" height="300" width="500" frameborder="0"></iframe><br/> |
124
|
|
|
<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea> |
125
|
|
|
<input type="hidden" name="recaptcha_response_field" value="manual_challenge"/> |
126
|
|
|
</noscript>'; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* A ReCaptchaResponse is returned from recaptcha_check_answer() |
131
|
|
|
*/ |
132
|
|
|
class ReCaptchaResponse |
133
|
|
|
{ |
|
|
|
|
134
|
|
|
|
135
|
|
|
public $is_valid; |
136
|
|
|
|
137
|
|
|
public $error; |
138
|
|
|
|
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Calls an HTTP POST function to verify if the user's guess was correct |
143
|
|
|
* @param string $privkey |
144
|
|
|
* @param string $remoteip |
145
|
|
|
* @param string $challenge |
146
|
|
|
* @param string $response |
147
|
|
|
* @param array $extra_params an array of extra variables to post to the server |
148
|
|
|
* @return ReCaptchaResponse |
|
|
|
|
149
|
|
|
*/ |
150
|
|
|
function recaptcha_check_answer($privkey, $remoteip, $challenge, $response, $extra_params = []) { |
151
|
|
|
|
152
|
|
|
if ($privkey == null || $privkey == '') { |
153
|
|
|
die("To use reCAPTCHA you must get an API key from <a href='http://recaptcha.net/api/getkey'>http://recaptcha.net/api/getkey</a>"); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($remoteip == null || $remoteip == '') { |
157
|
|
|
die('For security reasons, you must pass the remote ip to reCAPTCHA'); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
//discard spam submissions |
161
|
|
|
if ($challenge == null || strlen($challenge) == 0 || $response == null || strlen($response) == 0) { |
162
|
|
|
$recaptcha_response = new ReCaptchaResponse(); |
163
|
|
|
$recaptcha_response->is_valid = false; |
164
|
|
|
$recaptcha_response->error = 'incorrect-captcha-sol'; |
165
|
|
|
return $recaptcha_response; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
$response = _recaptcha_http_post( |
169
|
|
|
RECAPTCHA_VERIFY_SERVER, |
170
|
|
|
'/verify', |
171
|
|
|
[ |
172
|
|
|
'privatekey' => $privkey, |
173
|
|
|
'remoteip' => $remoteip, |
174
|
|
|
'challenge' => $challenge, |
175
|
|
|
'response' => $response, |
176
|
|
|
] + $extra_params |
177
|
|
|
); |
178
|
|
|
|
179
|
|
|
$answers = explode("\n", $response[1]); |
180
|
|
|
$recaptcha_response = new ReCaptchaResponse(); |
181
|
|
|
|
182
|
|
|
if (trim($answers[0]) == 'true') { |
183
|
|
|
$recaptcha_response->is_valid = true; |
184
|
|
|
} else { |
185
|
|
|
$recaptcha_response->is_valid = false; |
186
|
|
|
$recaptcha_response->error = $answers[1]; |
187
|
|
|
} |
188
|
|
|
return $recaptcha_response; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* gets a URL where the user can sign up for reCAPTCHA. If your application |
193
|
|
|
* has a configuration page where you enter a key, you should provide a link |
194
|
|
|
* using this function. |
195
|
|
|
* @param string $domain The domain where the page is hosted |
|
|
|
|
196
|
|
|
* @param string $appname The name of your application |
|
|
|
|
197
|
|
|
*/ |
198
|
|
|
function recaptcha_get_signup_url($domain = null, $appname = null) { |
199
|
|
|
return 'http://recaptcha.net/api/getkey?' . _recaptcha_qsencode(['domain' => $domain, 'app' => $appname]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
function _recaptcha_aes_pad($val) { |
203
|
|
|
$block_size = 16; |
204
|
|
|
$numpad = $block_size - (strlen($val) % $block_size); |
205
|
|
|
return str_pad($val, strlen($val) + $numpad, chr($numpad)); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/* Mailhide related code */ |
209
|
|
|
|
210
|
|
|
function _recaptcha_aes_encrypt($val, $ky) { |
211
|
|
|
if (!function_exists('mcrypt_encrypt')) { |
212
|
|
|
die('To use reCAPTCHA Mailhide, you need to have the mcrypt php module installed.'); |
213
|
|
|
} |
214
|
|
|
$mode = MCRYPT_MODE_CBC; |
215
|
|
|
$enc = MCRYPT_RIJNDAEL_128; |
216
|
|
|
$val = _recaptcha_aes_pad($val); |
217
|
|
|
return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"); |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
function _recaptcha_mailhide_urlbase64($x) { |
221
|
|
|
return strtr(base64_encode($x), '+/', '-_'); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/* gets the reCAPTCHA Mailhide url for a given email, public key and private key */ |
225
|
|
|
|
226
|
|
|
function recaptcha_mailhide_url($pubkey, $privkey, $email) { |
227
|
|
|
if ($pubkey == '' || $pubkey == null || $privkey == '' || $privkey == null) { |
228
|
|
|
die( |
229
|
|
|
'To use reCAPTCHA Mailhide, you have to sign up for a public and private key, ' . |
230
|
|
|
"you can do so at <a href='http://mailhide.recaptcha.net/apikey'>http://mailhide.recaptcha.net/apikey</a>" |
231
|
|
|
); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$ky = pack('H*', $privkey); |
235
|
|
|
$cryptmail = _recaptcha_aes_encrypt($email, $ky); |
236
|
|
|
|
237
|
|
|
return 'http://mailhide.recaptcha.net/d?k=' . $pubkey . '&c=' . _recaptcha_mailhide_urlbase64($cryptmail); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* gets the parts of the email to expose to the user. |
242
|
|
|
* eg, given johndoe@example,com return ["john", "example.com"]. |
243
|
|
|
* the email is then displayed as [email protected] |
244
|
|
|
*/ |
245
|
|
|
function _recaptcha_mailhide_email_parts($email) { |
246
|
|
|
$arr = preg_split('/@/', $email); |
247
|
|
|
|
248
|
|
|
if (strlen($arr[0]) <= 4) { |
249
|
|
|
$arr[0] = substr($arr[0], 0, 1); |
250
|
|
|
} else if (strlen($arr[0]) <= 6) { |
|
|
|
|
251
|
|
|
$arr[0] = substr($arr[0], 0, 3); |
252
|
|
|
} else { |
253
|
|
|
$arr[0] = substr($arr[0], 0, 4); |
254
|
|
|
} |
255
|
|
|
return $arr; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Gets html to display an email address given a public an private key. |
260
|
|
|
* to get a key, go to: |
261
|
|
|
* |
262
|
|
|
* http://mailhide.recaptcha.net/apikey |
|
|
|
|
263
|
|
|
*/ |
264
|
|
|
function recaptcha_mailhide_html($pubkey, $privkey, $email) { |
265
|
|
|
$emailparts = _recaptcha_mailhide_email_parts($email); |
266
|
|
|
$url = recaptcha_mailhide_url($pubkey, $privkey, $email); |
267
|
|
|
|
268
|
|
|
return htmlentities($emailparts[0]) . "<a href='" . htmlentities($url) . |
269
|
|
|
"' onclick=\"window.open('" . htmlentities($url) . "', '', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=500,height=300'); return false;\" title=\"Reveal this e-mail address\">...</a>@" . htmlentities($emailparts[1]); |
270
|
|
|
} |