forked from alissonperez/reCaptcha-PHP-Class
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass.recaptcha.php
345 lines (313 loc) · 9.77 KB
/
class.recaptcha.php
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* Google reCatpcha API Base class
*
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
abstract class reCaptchaBase {
/**
* public key for encryption
*
* @var string
*/
protected $public_key;
/**
* protected key for encryption
*
* @var string
*/
protected $protected_key;
/**
* curl resource instance
*
* @var resource
*/
private $ch;
/**
* Google reCaptcha API URL
*
* @var string
*/
static protected $api_server = 'www.google.com/recaptcha/api';
/**
* Constructor
* define private and public key for requests.
*
* @param string $public_key key to use as public key
* @param string $private_key key to use as private key
* @param bool $init_curl init curl instance
*
* @throws InvalidArgumentException
*
* @return reCaptcha
*/
public function __construct($public_key, $private_key, $init_curl = true)
{
$this->public_key = $public_key;
$this->private_key = $private_key;
if (empty($this->public_key) || empty($this->private_key)) {
throw new InvalidArgumentException('Invalid private / public key.');
}
if ($init_curl) {
$this->ch = self::_initCurl();
}
}
/**
* Destructor
* clean up opened curl instance
*
* @return void
*/
public function __destruct()
{
curl_close($this->ch);
}
/**
* init global curl object
*
* @return resource curl instance
*/
private static function _initCurl()
{
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_HEADER => false,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POST => true
));
return $ch;
}
/**
* send post call to remote api server
*
* @param string $url url to send call to
* @param array $data post parameter for call
*
* @throws RuntimeException
*
* @return string result of post call
*/
protected function _call($url, $data = array())
{
curl_setopt_array($this->ch, array(
CURLOPT_URL => $url,
CURLOPT_POSTFIELDS => $data
));
$result = curl_exec($this->ch);
if ($result === false) {
throw new RuntimeException(curl_error($this->ch), curl_errno($this->ch));
}
return $result;
}
}
/**
* Google reCaptcha API Class
*
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
class reCaptcha extends reCaptchaBase {
/**
* error message of API if response validation failed
*
* @var string
*/
private $error;
/**
* 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 $error The error given by reCAPTCHA (optional, default is null)
* @param boolean $use_ssl Should the request be made over ssl? (optional, default is false)
* @param array $options List of options to set to reCatpcha JavaScript engine
* @return string - The HTML to be embedded in the user's form.
*/
public function form($error = false, $use_ssl = false, $options = array())
{
$out = '';
$url = 'http' . ($use_ssl ? 's' : '') . '://' . self::$api_server;
if (!empty($options)) {
$out .= '<script type="text/javascript">' .
'var RecaptchaOptions = ' . json_encode($options) . ';</script>';
}
$out .= '<script type="text/javascript" src="' . $url . '/challenge?k=' . $this->public_key .
($error ? '&error=' . urlencode($error) : '') . '"></script>';
$out .= '<noscript>' .
'<iframe src="' . $url . '/noscript?k=' . $this->public_key .
($error ? '&error=' . urlencode($error) : '') .
'" height="300" width="500" frameborder="0"></iframe><br/>' .
'<textarea name="recaptcha_challenge_field" rows="3" cols="40"></textarea>' .
'<input type="hidden" name="recaptcha_response_field" value="manual_challenge">' .
'</noscript>';
return $out;
}
/**
* Calls an HTTP POST function to verify if the user's guess was correct
*
* @param string $remote_ip client remote ip, needed for validation
* @param string $challenge api chalange
* @param string $response api response
* @param array $extra_params an array of extra variables to post to the server
*
* @throws InvalidArgumentException
* @throws UnexpectedValueException
*
* @return bool True if valid answer
*/
public function check_answer($remote_ip, $challenge, $response, $extra_params = array())
{
$this->error = '';
if (empty($remote_ip)) {
throw new InvalidArgumentException('For security reasons, you must pass the remote IP to reCAPTCHA');
}
// discard spam submissions
if (empty($challenge) || empty($response)) {
$this->error = 'incorrect-captcha-sol';
return false;
}
$res = $this->_call('http://' . self::$api_server . '/verify',
array_merge(array(
'privatekey' => $this->private_key,
'remoteip' => $remote_ip,
'challenge' => $challenge,
'response' => $response
), $extra_params)
);
if (empty($res)) {
throw new \UnexpectedValueException('Invalid API Response.');
}
$answers = explode("\n", $res, 2);
if (empty($answers)) {
throw new \UnexpectedValueException('Invalid API Response.');
}
if (trim($answers[0]) == 'true') {
return true;
}
$this->error = $answers[1];
return false;
}
/**
* get API error string
*
* @return string
*/
public function getError() {
return $this->error;
}
}
/**
* Google MailHide API Class
*
* @license BSD http://www.opensource.org/licenses/bsd-license.php
*/
class MailHide extends reCaptchaBase
{
/**
* Custom constructor to disable init of curl
*
* @param string $public_key key to use as public key
* @param string $private_key key to use as private key
*
* @throw InvalidArgumentException if private/public key missing
*
* @return reCaptcha
*/
public function __construct($public_key, $private_key) {
return parent::__construct($public_key, $private_key, false);
}
/**
* generated AES padding
*
* @param string $val string to fill
*
* @return string filled string
*/
private function _aes_pad($val)
{
$block_size = 16;
$numpad = $block_size - (strlen($val) % $block_size);
return str_pad($val, strlen($val) + $numpad, chr($numpad));
}
/**
* AES Encrypt value by given key
*
* @param string $val value to encrypt
* @param string $ky key to use
*
* @return string encrypted result
*/
private function _aes_encrypt($val, $ky)
{
$mode = MCRYPT_MODE_CBC;
$enc = MCRYPT_RIJNDAEL_128;
$val = $this->aes_pad($val);
return mcrypt_encrypt($enc, $ky, $val, $mode, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0");
}
/**
* convert encrypted mail to base64 encoded
*
* @param string $x input to convert
*
* @return string converted base64
*/
private function _get_mailhide_urlbase64($x)
{
return strtr(base64_encode($x), '+/', '-_');
}
/**
* gets the reCAPTCHA Mailhide url for a given email, public key and private key
*
* @param string $email email to use
*
* @return string url of google recaptcha api
*/
private function _get_mailhide_url($email)
{
$ky = pack('H*', $this->private_key);
$crypt_mail = $this->_aes_encrypt($email, $ky);
return 'http://www.google.com/recaptcha/mailhide/d?k=' . $this->public_key .
'&c=' . $this->_get_mailhide_urlbase64($crypt_mail);
}
/**
* gets the parts of the email to expose to the user.
* eg, given johndoe@example,com return ["john", "example.com"].
* the email is then displayed as [email protected]
*
* @param string $email email to explode
*
* @return array of elements.
*/
private function _get_mailhide_email_parts($email)
{
$arr = preg_split("/@/", $email);
if (strlen($arr[0]) <= 4) {
$arr[0] = substr($arr[0], 0, 1);
} elseif (strlen($arr[0]) <= 6) {
$arr[0] = substr($arr[0], 0, 3);
} else {
$arr[0] = substr($arr[0], 0, 4);
}
return $arr;
}
/**
* Gets html to display an email address given a public an private key.
* to get a key, go to:
*
* @see http://www.google.com/recaptcha/mailhide/apikey
*
* @param string $email email to convert
*
* @return string converted version
*/
public function email($email)
{
$email_parts = $this->_get_mailhide_email_parts($email);
$url = $this->_get_mailhide_url($email);
return htmlentities($email_parts[0]) .
'<a href="' . htmlentities($url) . '" 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($email_parts[1]);
}
}