<?php
// Prevent caching
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

$file = '../data/stats.json';
$app = isset($_GET['app']) ? $_GET['app'] : 'clickmagic';

// Verify app exists in our logic to prevent arbitrary key creation
$allowed_apps = ['clickmagic'];
if (!in_array($app, $allowed_apps)) {
    die("Application introuvable.");
}

// Read current stats
if (file_exists($file)) {
    $json = file_get_contents($file);
    $data = json_decode($json, true);
} else {
    $data = [];
}

// Init if not exists
if (!isset($data[$app])) {
    $data[$app] = 0;
}

// Increment
$data[$app]++;

// Save
file_put_contents($file, json_encode($data, JSON_PRETTY_PRINT));

// Redirect
if ($app === 'clickmagic') {
    // Redirect to GitHub Release (Reliable & Trusted)
    header("Location: https://github.com/aliaspilote/digimaker_installers/releases/download/v1.0/ClickMagic_v1.zip");
    exit();
}
?>