<?php
declare(strict_types=1);
require_once __DIR__ . '/../app/bootstrap.php';

use App\Core\Database;

function cron_authorize(): void {
    if (PHP_SAPI === 'cli') return;
    $token=(string)($_GET['token']??'');
    if(CRON_TOKEN===''||!hash_equals(CRON_TOKEN,$token)){http_response_code(403);exit('Forbidden');}
    header('Content-Type: text/plain; charset=utf-8');
}
function cron_log(string $job,string $status,string $message,string $started): void {
    if(Database::tableExists('cron_runs'))Database::safeExecute('INSERT INTO cron_runs (job_name,status,message,started_at,finished_at) VALUES (?,?,?,?,NOW())',[$job,$status,substr($message,0,65000),$started]);
    echo '['.date('c').'] '.$job.': '.$message.PHP_EOL;
}
cron_authorize();
