summaryrefslogtreecommitdiff
path: root/cgi/captcha.php
blob: 6881063bdb65fdf9a55aa24433d6a60ce96508e6 (plain)
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
<?php

use Gregwar\Captcha\CaptchaBuilder;

const CAPTCHA_PHRASE_LENGTH = 7;

require_once __DIR__ . "/../vendor/autoload.php";

session_start();

$mode = $_GET["m"] ?? "newcaptcha";

if ($mode === "newcaptcha") {
    header("Pragma: no-cache");
    header("Cache-Control: no-cache");
    header("Location: ?m=captcha&r=" . urldecode(bin2hex(random_bytes(10))));
    exit;
}

$charset = 'ABCDEFGHKLMNPQRTWXYZ234789abdefghkmnpqr';
$phrase = "";
for ($i = 0; $i < CAPTCHA_PHRASE_LENGTH; ++$i)
    $phrase .= $charset[random_int(0, strlen($charset) - 1)];

$builder = new CaptchaBuilder($phrase);
$builder->setInterpolation(false);
$builder->build(256, 64);
$_SESSION["captcha_phrase"] = $builder->getPhrase();

if ($mode === "captcha") {
    echo '<body background="?m=captchaimg&r=' . bin2hex(random_bytes(10)) . '">';
    exit;
}

if ($mode === "captchaimg") {
    header("Content-Type: image/jpeg");
    $builder->save(null, 40);
    exit;
}