<?php
include 'config.php';

// Token Verify
if ($_GET['auth_token'] !== AUTH_TOKEN) {
    die(json_encode(["error" => "Unauthorized Access"]));
}

// Validate type
if ($_GET['type'] !== 'app') {
    die(json_encode(["error" => "Invalid download type"]));
}

// Get Filename
$filename = $_GET['filename'];
$file_path = APP_DIR . $filename;

if (file_exists($file_path)) {
    $filesize = filesize($file_path);

    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Length: $filesize");

    flush();
    readfile($file_path);
    exit;
} else {
    echo json_encode(["error" => "File not found"]);
}
?>