mirror of
https://github.com/Neetpone/ponepaste.git
synced 2025-03-11 14:10:06 +01:00
31 lines
730 B
PHP
31 lines
730 B
PHP
<?php
|
|
function setupCaptcha($token = null) : string {
|
|
global $redis;
|
|
$allowed = "ABCDEFGHIJKLMNOPQRSTUVYXYZabcdefghijklmnopqrstuvwxyz0123456789";
|
|
|
|
$code = '';
|
|
for ($i = 0; $i < 5; $i++) {
|
|
$code .= substr($allowed, rand() % (strlen($allowed)), 1);
|
|
}
|
|
|
|
if ($token === null) {
|
|
$token = pp_random_password();
|
|
}
|
|
|
|
$redis->setex('captcha/' . md5($token), 600, $code);
|
|
|
|
return $token;
|
|
}
|
|
|
|
function checkCaptcha(string $token, string $answer) : bool {
|
|
global $redis;
|
|
|
|
$redis_answer = $redis->get('captcha/' . md5($token));
|
|
if (!$redis_answer) {
|
|
return false;
|
|
}
|
|
|
|
$redis->del('captcha/' . $token);
|
|
|
|
return strtolower($redis_answer) === strtolower($answer);
|
|
}
|