/
class_captcha.php
506 lines (448 loc) · 11.9 KB
/
class_captcha.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
/**
* MyBB 1.8
* Copyright 2014 MyBB Group, All Rights Reserved
*
* Website: http://www.mybb.com
* License: http://www.mybb.com/about/license
*
* This class is based from reCAPTCHA's PHP library, adapted for use in MyBB.
*
* Copyright (c) 2007 reCAPTCHA -- http://recaptcha.net
* AUTHORS:
* Mike Crawford
* Ben Maurer
*
* 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.
*
*/
class captcha
{
/**
* Type of CAPTCHA.
*
* 1 = Default CAPTCHA
* 4 = NoCATPCHA reCAPTCHA
* 5 = reCAPTCHA invisible
* 6 = hCaptcha
* 7 = hCaptcha invisible
* 8 = reCAPTCHA v3
*
* @var int
*/
public $type = 0;
/**
* CAPTCHA constants declaration
*
* @var int
*/
const DEFAULT_CAPTCHA = 1;
const NOCAPTCHA_RECAPTCHA = 4;
const RECAPTCHA_INVISIBLE = 5;
const HCAPTCHA = 6;
const HCAPTCHA_INVISIBLE = 7;
const RECAPTCHA_V3 = 8;
/**
* The template to display the CAPTCHA in
*
* @var string
*/
public $captcha_template = '';
/**
* CAPTCHA Server URL
*
* @var string
*/
public $server = '';
/**
* CAPTCHA Verify Server
*
* @var string
*/
public $verify_server = '';
/**
* HTML of the built CAPTCHA
*
* @var string
*/
public $html = '';
/**
* The errors that occurred when handling data.
*
* @var array
*/
public $errors = array();
/**
* @param bool $build
* @param string $template
*/
function __construct($build = false, $template = "")
{
global $mybb, $plugins;
$this->type = $mybb->settings['captchaimage'];
$args = array(
'this' => &$this,
'build' => &$build,
'template' => &$template,
);
$plugins->run_hooks('captcha_build_start', $args);
// Prepare the build template
if($template)
{
$this->captcha_template = $template;
if($this->type == captcha::NOCAPTCHA_RECAPTCHA)
{
$this->captcha_template .= "_nocaptcha";
}
elseif($this->type == captcha::RECAPTCHA_INVISIBLE)
{
$this->captcha_template .= "_recaptcha_invisible";
}
elseif($this->type == captcha::HCAPTCHA)
{
$this->captcha_template .= "_hcaptcha";
}
elseif($this->type == captcha::HCAPTCHA_INVISIBLE)
{
$this->captcha_template .= "_hcaptcha_invisible";
}
elseif($this->type == captcha::RECAPTCHA_V3)
{
$this->captcha_template .= "_recaptcha_invisible";
}
}
// Work on which CAPTCHA we've got installed
if(in_array($this->type, array(captcha::NOCAPTCHA_RECAPTCHA, captcha::RECAPTCHA_INVISIBLE, captcha::RECAPTCHA_V3)) && $mybb->settings['recaptchapublickey'] && $mybb->settings['recaptchaprivatekey'])
{
// We want to use noCAPTCHA or reCAPTCHA invisible, set the server options
$this->server = "//www.google.com/recaptcha/api.js";
$this->verify_server = "https://www.google.com/recaptcha/api/siteverify";
if($build == true)
{
$this->build_recaptcha();
}
}
elseif(in_array($this->type, array(captcha::HCAPTCHA, captcha::HCAPTCHA_INVISIBLE)) && $mybb->settings['hcaptchapublickey'] && $mybb->settings['hcaptchaprivatekey'])
{
// We want to use hCaptcha or hCaptcha invisible, set the server options
$this->server = "//js.hcaptcha.com/1/api.js";
$this->verify_server = "https://hcaptcha.com/siteverify";
if($build == true)
{
$this->build_hcaptcha();
}
}
elseif($this->type == captcha::DEFAULT_CAPTCHA)
{
if(!function_exists("imagecreatefrompng"))
{
// We want to use the default CAPTCHA, but it's not installed
return;
}
elseif($build == true)
{
$this->build_captcha();
}
}
$plugins->run_hooks('captcha_build_end', $args);
}
/**
* @param bool $return Not used
*/
function build_captcha($return = false)
{
global $db, $lang, $templates, $theme, $mybb;
// This will build a MyBB CAPTCHA
$randomstr = random_str(5);
$imagehash = md5(random_str(12));
$insert_array = array(
"imagehash" => $imagehash,
"imagestring" => $randomstr,
"dateline" => TIME_NOW
);
$db->insert_query("captcha", $insert_array);
eval("\$this->html = \"".$templates->get($this->captcha_template)."\";");
//eval("\$this->html = \"".$templates->get("member_register_regimage")."\";");
}
function build_recaptcha()
{
global $lang, $mybb, $templates;
// This will build a reCAPTCHA
$server = $this->server;
$public_key = $mybb->settings['recaptchapublickey'];
eval("\$this->html = \"".$templates->get($this->captcha_template, 1, 0)."\";");
//eval("\$this->html = \"".$templates->get("member_register_regimage_recaptcha")."\";");
}
function build_hcaptcha()
{
global $lang, $mybb, $templates, $theme;
// This will build a hCaptcha
$server = $this->server;
$public_key = $mybb->settings['hcaptchapublickey'];
$captcha_theme = $mybb->settings['hcaptchatheme'];
$captcha_size = $mybb->settings['hcaptchasize'];
eval("\$this->html = \"".$templates->get($this->captcha_template, 1, 0)."\";");
}
/**
* @return string
*/
function build_hidden_captcha()
{
global $db, $mybb, $templates;
$field = array();
if($this->type == captcha::DEFAULT_CAPTCHA)
{
// Names
$hash = "imagehash";
$string = "imagestring";
// Values
$field['hash'] = $db->escape_string($mybb->get_input('imagehash'));
$field['string'] = $db->escape_string($mybb->get_input('imagestring'));
}
elseif($this->type == 3)
{
// Are You a Human can't be built as a hidden captcha
return '';
}
eval("\$this->html = \"".$templates->get("post_captcha_hidden")."\";");
return $this->html;
}
/**
* @return bool
*/
function validate_captcha()
{
global $db, $lang, $mybb, $session, $plugins;
$plugins->run_hooks('captcha_validate_start', $this);
if($this->type == captcha::DEFAULT_CAPTCHA)
{
// We have a normal CAPTCHA to handle
$imagehash = $db->escape_string($mybb->get_input('imagehash'));
$imagestring = $db->escape_string(my_strtolower($mybb->get_input('imagestring')));
switch($db->type)
{
case 'mysql':
case 'mysqli':
$field = 'imagestring';
break;
default:
$field = 'LOWER(imagestring)';
break;
}
$query = $db->simple_select("captcha", "*", "imagehash = '{$imagehash}' AND {$field} = '{$imagestring}'");
$imgcheck = $db->fetch_array($query);
if(!$imgcheck)
{
$this->set_error($lang->invalid_captcha_verify);
$db->delete_query("captcha", "imagehash = '{$imagehash}'");
}
}
elseif(in_array($this->type, array(captcha::NOCAPTCHA_RECAPTCHA, captcha::RECAPTCHA_INVISIBLE)))
{
$response = $mybb->get_input('g-recaptcha-response');
if(!$response || strlen($response) == 0)
{
$this->set_error($lang->invalid_nocaptcha);
}
else
{
// We have a noCAPTCHA or reCAPTCHA invisible to handle
// Contact Google and see if our reCAPTCHA was successful
$response = fetch_remote_file($this->verify_server, array(
'secret' => $mybb->settings['recaptchaprivatekey'],
'remoteip' => $session->ipaddress,
'response' => $response
));
if($response == false)
{
$this->set_error($lang->invalid_nocaptcha_transmit);
}
else
{
$answer = json_decode($response, true);
if($answer['success'] != 'true')
{
// We got it wrong! Oh no...
$this->set_error($lang->invalid_nocaptcha);
}
}
}
}
elseif($this->type == captcha::RECAPTCHA_V3)
{
$response = $mybb->get_input('g-recaptcha-response');
if(!$response || strlen($response) == 0)
{
$this->set_error($lang->invalid_nocaptcha);
}
else
{
// We have a reCAPTCHA invisible to handle
// Contact Google and see if our reCAPTCHA was successful
$response = fetch_remote_file($this->verify_server, array(
'secret' => $mybb->settings['recaptchaprivatekey'],
'score' => $mybb->settings['recaptchascore'],
'remoteip' => $session->ipaddress,
'response' => $response
));
if($response === false)
{
$this->set_error($lang->invalid_nocaptcha_transmit);
}
else
{
$answer = json_decode($response, true);
if($answer['success'] != 'true' || $answer['score'] < $mybb->settings['recaptchascore'])
{
// We got it wrong! Oh no...
$this->set_error($lang->invalid_nocaptcha);
}
}
}
}
elseif(in_array($this->type, array(captcha::HCAPTCHA, captcha::HCAPTCHA_INVISIBLE)))
{
$response = $mybb->get_input('h-captcha-response');
if(!$response || strlen($response) == 0)
{
$this->set_error($lang->invalid_hcaptcha);
}
else
{
// We have an hCaptcha or hCaptcha invisible to handle
// Contact hCaptcha and see if our hCaptcha was successful
$response = fetch_remote_file($this->verify_server, array(
'secret' => $mybb->settings['hcaptchaprivatekey'],
'remoteip' => $session->ipaddress,
'response' => $response
));
if($response == false)
{
$this->set_error($lang->invalid_hcaptcha_transmit);
}
else
{
$answer = json_decode($response, true);
if($answer['success'] != 'true')
{
// We got it wrong! Oh no...
$this->set_error($lang->invalid_hcaptcha);
}
}
}
}
$plugins->run_hooks('captcha_validate_end', $this);
if(count($this->errors) > 0)
{
return false;
}
else
{
return true;
}
}
function invalidate_captcha()
{
global $db, $mybb, $plugins;
if($this->type == captcha::DEFAULT_CAPTCHA)
{
// We have a normal CAPTCHA to handle
$imagehash = $db->escape_string($mybb->get_input('imagehash'));
if($imagehash)
{
$db->delete_query("captcha", "imagehash = '{$imagehash}'");
}
}
// Not necessary for reCAPTCHA or Are You a Human
$plugins->run_hooks('captcha_invalidate_end', $this);
}
/**
* Add an error to the error array.
*
* @param string $error
* @param string $data
*/
function set_error($error, $data='')
{
$this->errors[$error] = array(
"error_code" => $error,
"data" => $data
);
}
/**
* Returns the error(s) that occurred when handling data
* in a format that MyBB can handle.
*
* @return array An array of errors in a MyBB format.
*/
function get_errors()
{
global $lang;
$errors = array();
foreach($this->errors as $error)
{
$lang_string = $error['error_code'];
if(!$lang_string)
{
if($lang->invalid_captcha_verify)
{
$lang_string = 'invalid_captcha_verify';
}
else
{
$lang_string = 'unknown_error';
}
}
if(!isset($lang->$lang_string))
{
$errors[] = $error['error_code'];
continue;
}
if(!empty($error['data']) && !is_array($error['data']))
{
$error['data'] = array($error['data']);
}
if(is_array($error['data']))
{
array_unshift($error['data'], $lang->$lang_string);
$errors[] = call_user_func_array(array($lang, "sprintf"), $error['data']);
}
else
{
$errors[] = $lang->$lang_string;
}
}
return $errors;
}
/**
* @param array $data
*
* @return string
*/
private function _qsencode($data)
{
$req = '';
foreach($data as $key => $value)
{
$req .= $key.'='.urlencode(stripslashes($value)).'&';
}
$req = substr($req, 0, (strlen($req) - 1));
return $req;
}
}