1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-02 12:57:35 +01:00
vichan/securimage.php

70 lines
1.8 KiB
PHP
Raw Normal View History

2021-11-13 01:24:34 +01:00
<?php
require_once('inc/bootstrap.php');
$expires_in = 120;
function rand_string($length, $charset) {
$ret = "";
while ($length--) {
$ret .= mb_substr($charset, rand(0, mb_strlen($charset, 'utf-8')-1), 1, 'utf-8');
}
return $ret;
}
function cleanup() {
global $expires_in;
prepare("DELETE FROM `captchas` WHERE `created_at` < ?")->execute([time() - $expires_in]);
}
$mode = @$_GET['mode'];
switch ($mode) {
case 'get':
header("Content-type: application/json");
$extra = $config['captcha']['native']['extra'];
$cookie = rand_string(20, $extra);
$i = new Securimage($config['captcha']['native']['securimage_options']);
2021-11-13 01:24:34 +01:00
$i->createCode();
ob_start();
$i->show();
$rawimg = ob_get_contents();
$b64img = 'data:image/png;base64,'.base64_encode($rawimg);
$html = '<img src="'.$b64img.'">';
ob_end_clean();
$cdata = $i->getCode();
$query = prepare("INSERT INTO `captchas` (`cookie`, `extra`, `text`, `created_at`) VALUES (?, ?, ?, ?)");
$query->execute([$cookie, $extra, $cdata->code_display, $cdata->creationTime]);
if (isset($_GET['raw'])) {
$_SESSION['captcha_cookie'] = $cookie;
header('Content-Type: image/png');
echo $rawimg;
} else {
echo json_encode(["cookie" => $cookie, "captchahtml" => $html, "expires_in" => $expires_in]);
}
break;
case 'check':
cleanup();
if (!isset ($_GET['mode']) || !isset ($_GET['cookie']) || !isset ($_GET['text'])) {
2021-11-13 01:24:34 +01:00
die();
}
$query = prepare("SELECT * FROM `captchas` WHERE `cookie` = ?");
$query->execute([$_GET['cookie']]);
2021-11-13 01:24:34 +01:00
$ary = $query->fetchAll();
2022-11-28 22:10:20 +00:00
if (!$ary) { // captcha expired
2021-11-13 01:24:34 +01:00
echo "0";
2022-11-28 22:10:20 +00:00
break;
2021-11-13 01:24:34 +01:00
} else {
$query = prepare("DELETE FROM `captchas` WHERE `cookie` = ?");
$query->execute([$_GET['cookie']]);
2021-11-13 01:24:34 +01:00
}
if ($ary[0]['text'] !== $_GET['text']) {
echo "0";
} else {
echo "1";
}
break;
}