Spaces:
Build error
Build error
Create api/index.php
Browse files- api/index.php +52 -0
api/index.php
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?php
|
| 2 |
+
header('Content-Type: application/json');
|
| 3 |
+
header('Access-Control-Allow-Origin: *');
|
| 4 |
+
header('Access-Control-Allow-Methods: GET, POST, OPTIONS');
|
| 5 |
+
header('Access-Control-Allow-Headers: Content-Type');
|
| 6 |
+
|
| 7 |
+
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
| 8 |
+
http_response_code(204);
|
| 9 |
+
exit;
|
| 10 |
+
}
|
| 11 |
+
|
| 12 |
+
// ── Parse the route from the URL ──────────────────────────────────────────────
|
| 13 |
+
// Expected format: /api/register, /api/upload, /api/save etc.
|
| 14 |
+
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
|
| 15 |
+
$path = rtrim($path, '/');
|
| 16 |
+
$parts = explode('/', $path);
|
| 17 |
+
|
| 18 |
+
// Last segment is the route e.g. "register" from "/api/register"
|
| 19 |
+
$route = end($parts);
|
| 20 |
+
|
| 21 |
+
$validRoutes = [
|
| 22 |
+
'register',
|
| 23 |
+
'upload',
|
| 24 |
+
'save',
|
| 25 |
+
'file',
|
| 26 |
+
'files',
|
| 27 |
+
'delete',
|
| 28 |
+
'stats',
|
| 29 |
+
'serve'
|
| 30 |
+
];
|
| 31 |
+
|
| 32 |
+
if (!in_array($route, $validRoutes)) {
|
| 33 |
+
http_response_code(404);
|
| 34 |
+
echo json_encode([
|
| 35 |
+
'error' => 'Unknown route',
|
| 36 |
+
'available' => array_map(fn($r) => "/api/{$r}", $validRoutes),
|
| 37 |
+
'docs' => [
|
| 38 |
+
'POST /api/register' => 'Create a new user site. Body: {"username":"alice"}',
|
| 39 |
+
'POST /api/upload' => 'Upload files. Form data: username + files[]',
|
| 40 |
+
'POST /api/save' => 'Save file from editor. Body: {"username":"alice","filepath":"index.php","content":"..."}',
|
| 41 |
+
'GET /api/file' => 'Get file content. Params: ?username=alice&filepath=index.php',
|
| 42 |
+
'GET /api/files' => 'List all files. Params: ?username=alice',
|
| 43 |
+
'POST /api/delete' => 'Delete file or site. Body: {"username":"alice","filepath":"index.php"} or {"username":"alice","delete_all":true}',
|
| 44 |
+
'GET /api/stats' => 'Disk usage and user count',
|
| 45 |
+
'GET /site/username/file' => 'View a user site e.g. /site/alice/index.php',
|
| 46 |
+
]
|
| 47 |
+
]);
|
| 48 |
+
exit;
|
| 49 |
+
}
|
| 50 |
+
|
| 51 |
+
// ── Route to the correct file ─────────────────────────────────────────────────
|
| 52 |
+
require_once __DIR__ . "/{$route}.php";
|