Files
safe-paste/public/router.php

45 lines
1.0 KiB
PHP

<?php
$uri = $_SERVER['REQUEST_URI'];
$path = parse_url($uri, PHP_URL_PATH);
// Strip trailing slash
$path = rtrim($path, '/') ?: '/';
// Route: /view/{id}
if (preg_match('#^/view/([a-f0-9]+)$#i', $path, $m)) {
$_GET['id'] = $m[1];
require __DIR__ . '/view.php';
exit;
}
// Route: /api/paste/{id}/unlock
if (preg_match('#^/api/paste/([a-f0-9]+)/unlock$#i', $path, $m) && $_SERVER['REQUEST_METHOD'] === 'POST') {
$_GET['id'] = $m[1];
$_GET['action'] = 'unlock';
require __DIR__ . '/api.php';
exit;
}
// Route: /api/paste/{id}
if (preg_match('#^/api/paste/([a-f0-9]+)$#i', $path, $m)) {
$_GET['id'] = $m[1];
$_GET['action'] = 'get';
require __DIR__ . '/api.php';
exit;
}
// Route: /api/paste
if ($path === '/api/paste') {
$_GET['action'] = 'create';
require __DIR__ . '/api.php';
exit;
}
// Route: static assets
if (preg_match('#\.(css|js|png|jpg|ico|svg|woff2?)$#', $path)) {
return false; // let PHP built-in server serve the file
}
// Route: / or /index.php
require __DIR__ . '/index.php';