Ryanhub - file viewer
filename: html/api/index.php
branch: main
back to repo
<?php

require __DIR__ . "/lib/response.php";

// derive a base path
$base = '/api';
$uri  = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = trim(substr($uri, strlen($base)), '/');

// if /api/ we serve info page
if ($path === '') {
    require "info.html";
    exit;
}

// split path into: service / endpoint / params
$parts = array_values(array_filter(explode('/', $path), fn($p) => $p !== ''));
$serviceRaw  = $parts[0] ?? null;
$endpointRaw = $parts[1] ?? null;
$params   = array_slice($parts, 2);

// quick validation to disallow directory traversal or weird names
$safe = function($s) {
    if (!is_string($s) || $s === '') return null;
    return preg_match('/^[a-z0-9_\-]+$/i', $s) ? $s : null;
};

$service = $safe($serviceRaw);
if (!$service) {
    json('Not found', 404);
    exit;
}

$endpoint = $safe($endpointRaw);

// if no endpoint provided serve info.html
if (!$endpoint) {
    $infoFile = __DIR__ . "/services/{$service}/info.html";
    if (is_file($infoFile)) {
        header('Content-Type: text/html');
        require $infoFile;
        exit;
    }
    // fallback to default endpoint
    $endpoint = 'default';
}

/* serve file based on service name and endpoint
suppose i want 
    service: test
    endpoint file: test.php
i would provide /api/test/test
.php is added onto the path
*/

header("Content-Type: application/json");
$serviceFile = __DIR__ . "/services/{$service}/{$endpoint}.php";
if (!is_file($serviceFile)) {
    json('Not found', 404);
    exit;
}

require $serviceFile;