<?php
// Cron via URL for shared hosting. Call this URL every 5-10 min (e.g. cron-job.org).
// Usage: sync_traffic_web.php?key=YOUR_SECRET_KEY

chdir(__DIR__);
require_once __DIR__ . '/config.php';

$secret = getSetting($pdo, 'cron_secret_key', '');
$key = $_GET['key'] ?? $_POST['key'] ?? '';

if ($secret === '' || $key === '' || $key !== $secret) {
    http_response_code(403);
    header('Content-Type: text/plain; charset=utf-8');
    die('Forbidden');
}

ob_start();
try {
    include __DIR__ . '/sync_traffic.php';
} catch (Throwable $e) {
    ob_end_clean();
    http_response_code(500);
    header('Content-Type: text/plain; charset=utf-8');
    echo 'Error: ' . $e->getMessage();
    exit;
}
ob_end_clean();

header('Content-Type: text/plain; charset=utf-8');
echo 'OK';
