<?php
// proxy.php
// Deploy to /var/www/version-proxy/ on Billing VPS (38.68.14.188)
// Serves as CORS-safe API bridge between the dashboard HTML and:
//   - Pterodactyl Panel API (panel.firefrostgaming.com)
//   - CurseForge API (api.curseforge.com)

$config = require 'config.php';
$action = $_GET['action'] ?? '';

// Allow requests from any origin (dashboard can be hosted anywhere)
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');

function makeRequest($url, $headers) {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
    $result = curl_exec($ch);
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);

    if ($httpCode >= 400) {
        http_response_code($httpCode);
        echo json_encode(['error' => "HTTP $httpCode"]);
        exit;
    }
    return $result;
}

$panelHeaders = [
    "Authorization: Bearer {$config['panel_key']}",
    "Accept: application/json"
];

if ($action === 'servers') {
    // List all servers
    header('Content-Type: application/json');
    echo makeRequest("{$config['panel_url']}/api/client/servers", $panelHeaders);

} elseif ($action === 'files') {
    // List files in server root
    header('Content-Type: application/json');
    $id = urlencode($_GET['server'] ?? '');
    echo makeRequest("{$config['panel_url']}/api/client/servers/{$id}/files/list", $panelHeaders);

} elseif ($action === 'read') {
    // Read a specific file from server filesystem
    // Note: Pterodactyl returns raw text here, not JSON
    header('Content-Type: text/plain');
    $id = urlencode($_GET['server'] ?? '');
    $file = urlencode($_GET['file'] ?? '');
    echo makeRequest("{$config['panel_url']}/api/client/servers/{$id}/files/contents?file={$file}", $panelHeaders);

} elseif ($action === 'curseforge') {
    // Get latest CurseForge mod files (API key stays server-side)
    header('Content-Type: application/json');
    $projectId = urlencode($_GET['project'] ?? '');
    $cfHeaders = [
        "x-api-key: {$config['cf_key']}",
        "Accept: application/json"
    ];
    echo makeRequest("https://api.curseforge.com/v1/mods/{$projectId}/files?pageSize=1&sortField=5&sortOrder=desc", $cfHeaders);

} else {
    header('Content-Type: application/json');
    http_response_code(400);
    echo json_encode(['error' => 'Invalid action']);
}
