<?php
declare(strict_types=1); require __DIR__.'/bootstrap.php'; header('Content-Type: application/json; charset=utf-8');
if(!user()){http_response_code(401);echo json_encode(['ok'=>false]);exit;}if($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);exit;}if(!hash_equals($_SESSION['csrf']??'',$_SERVER['HTTP_X_CSRF_TOKEN']??'')){http_response_code(419);echo json_encode(['ok'=>false,'error'=>'csrf']);exit;}
try{$path=upload_image('image',preg_replace('/[^a-z0-9_-]/i','',(string)($_POST['kind']??'book')));if(!$path)throw new RuntimeException('No image received.');echo json_encode(['ok'=>true,'url'=>$path],JSON_UNESCAPED_SLASHES);}catch(Throwable $e){http_response_code(422);echo json_encode(['ok'=>false,'error'=>$e->getMessage()]);}
