<?php
/**
 * 管理员登录图形验证码
 *
 * 安全要点：
 * - 使用 CSPRNG (random_int) 生成字符
 * - 排除形近字符 (0/O/1/I/L) 降低误识
 * - 明文仅保存在服务端 session，不通过任何响应头/字段泄漏
 * - 每次登录提交（无论成功失败）由 login.php 一次性消费
 * - 5 分钟 TTL，过期视为无效
 * - 强制 no-cache，避免图像被浏览器或代理缓存
 */
require __DIR__ . '/app/bootstrap.php';

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}

if (!extension_loaded('gd')) {
    header('Content-Type: text/plain; charset=utf-8');
    http_response_code(500);
    echo 'GD extension is not available. Please enable GD to use CAPTCHA.';
    exit;
}

$alphabet = 'ABCDEFGHJKMNPQRSTUVWXYZ23456789';
$length = 4;
$code = '';
$max = strlen($alphabet) - 1;
for ($i = 0; $i < $length; $i++) {
    $code .= $alphabet[random_int(0, $max)];
}

$_SESSION['admin_captcha'] = array(
    'code' => $code,
    'created_at' => time()
);

header('Content-Type: image/png');
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Pragma: no-cache');
header('Expires: 0');

$width = 140;
$height = 48;
$img = imagecreatetruecolor($width, $height);

// 背景渐变
$bgTop = imagecolorallocate($img, 244, 247, 252);
$bgBottom = imagecolorallocate($img, 232, 238, 252);
for ($y = 0; $y < $height; $y++) {
    $ratio = $y / $height;
    $r = (int)(244 + (232 - 244) * $ratio);
    $g = (int)(247 + (238 - 247) * $ratio);
    $b = (int)(252 + (252 - 252) * $ratio);
    $c = imagecolorallocate($img, $r, $g, $b);
    imageline($img, 0, $y, $width, $y, $c);
}

// 背景噪点
for ($i = 0; $i < 110; $i++) {
    $c = imagecolorallocate($img, random_int(180, 220), random_int(185, 220), random_int(205, 240));
    imagesetpixel($img, random_int(0, $width - 1), random_int(0, $height - 1), $c);
}

// 干扰线条
for ($i = 0; $i < 4; $i++) {
    $c = imagecolorallocate($img, random_int(140, 180), random_int(160, 200), random_int(200, 230));
    imageline($img, random_int(0, $width), random_int(0, $height), random_int(0, $width), random_int(0, $height), $c);
}

// 干扰弧
$arcColor = imagecolorallocate($img, 170, 190, 220);
imagearc($img, random_int(-10, $width + 10), random_int(-10, $height + 10), random_int(80, 140), random_int(40, 80), 0, 360, $arcColor);

// 绘制字符
$fontPath = __DIR__ . '/assets/fonts/msyh.ttf';
$useTtf = function_exists('imagettftext') && file_exists($fontPath);
$step = $width / ($length + 1);

for ($i = 0; $i < $length; $i++) {
    $char = $code[$i];
    $r = random_int(30, 90);
    $g = random_int(60, 120);
    $b = random_int(140, 200);
    $color = imagecolorallocate($img, $r, $g, $b);

    if ($useTtf) {
        $size = random_int(20, 24);
        $angle = random_int(-22, 22);
        $x = (int)($step * ($i + 0.65));
        $y = random_int(32, 38);
        imagettftext($img, $size, $angle, $x, $y, $color, $fontPath, $char);
    } else {
        $x = (int)($step * ($i + 0.6));
        $y = random_int(10, 20);
        imagestring($img, 5, $x, $y, $char, $color);
    }
}

// 前景噪点覆盖
for ($i = 0; $i < 40; $i++) {
    $c = imagecolorallocate($img, random_int(100, 160), random_int(120, 180), random_int(180, 220));
    imagesetpixel($img, random_int(0, $width - 1), random_int(0, $height - 1), $c);
}

imagepng($img);
imagedestroy($img);
exit;
