1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2024-12-11 15:26:08 +01:00
vichan/8chan-captcha/functions.php
Fredrick Brennan 691bc1c3fa Close ctrlcctrlv/infinity#481
Also improve CAPTCHA portability into other parrts of the software by removing weird CAPTCHA-specific config file
2015-04-25 19:50:07 +08:00

37 lines
1.0 KiB
PHP

<?php
require_once 'cool-php-captcha-0.3.1/captcha.php';
function generate_captcha($extra = '1234567890') {
global $config;
$text = rand_string($config['captcha']['length'], $extra);
$captcha = new SimpleCaptcha();
$cookie = rand_string(20, "abcdefghijklmnopqrstuvwxyz");
ob_start();
$captcha->CreateImage($text);
$image = ob_get_contents();
ob_end_clean();
$html = '<image src="data:image/png;base64,'.base64_encode($image).'">';
$query = prepare("INSERT INTO `captchas` (`cookie`, `extra`, `text`, `created_at`) VALUES (?, ?, ?, ?)");
$query->execute( [$cookie, $extra, $text, time()]);
return array("cookie" => $cookie, "html" => $html);
}
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 $config;
prepare("DELETE FROM `captchas` WHERE `created_at` < ?")->execute([time() - $config['captcha']['expires_in']]);
}