2021-07-10 19:18:17 +01:00
|
|
|
<?php
|
2023-02-24 06:26:40 -05:00
|
|
|
function setupCaptcha($token = null) : string {
|
2022-08-25 01:51:54 -04:00
|
|
|
global $redis;
|
2022-08-27 02:48:10 -04:00
|
|
|
$allowed = "ABCDEFGHIJKLMNOPQRSTUVYXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
2022-08-25 01:51:54 -04:00
|
|
|
|
|
|
|
$code = '';
|
|
|
|
for ($i = 0; $i < 5; $i++) {
|
2022-08-27 02:48:10 -04:00
|
|
|
$code .= substr($allowed, rand() % (strlen($allowed)), 1);
|
2022-08-25 01:51:54 -04:00
|
|
|
}
|
|
|
|
|
2023-02-24 06:26:40 -05:00
|
|
|
if ($token === null) {
|
|
|
|
$token = pp_random_password();
|
|
|
|
}
|
2022-08-25 01:51:54 -04:00
|
|
|
|
|
|
|
$redis->setex('captcha/' . md5($token), 600, $code);
|
|
|
|
|
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkCaptcha(string $token, string $answer) : bool {
|
|
|
|
global $redis;
|
|
|
|
|
2022-08-27 02:48:10 -04:00
|
|
|
$redis_answer = $redis->get('captcha/' . md5($token));
|
2022-08-25 01:51:54 -04:00
|
|
|
if (!$redis_answer) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$redis->del('captcha/' . $token);
|
|
|
|
|
2022-08-27 02:48:10 -04:00
|
|
|
return strtolower($redis_answer) === strtolower($answer);
|
2022-08-25 01:51:54 -04:00
|
|
|
}
|