77 lines
2.0 KiB
PHP
77 lines
2.0 KiB
PHP
<?php
|
|
// phpcs:ignoreFile
|
|
/**
|
|
* LASUCA API Router
|
|
*
|
|
* Simple router for REST API endpoints.
|
|
* All requests to /api/* should be routed here via .htaccess or server config.
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
// CORS headers for mobile/cross-origin requests
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type, Authorization, X-Requested-With');
|
|
header('Access-Control-Max-Age: 86400');
|
|
|
|
// Handle preflight requests
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(204);
|
|
exit();
|
|
}
|
|
|
|
// Load dependencies
|
|
$rootDir = dirname(__DIR__);
|
|
|
|
if (file_exists($rootDir . '/vendor/autoload.php')) {
|
|
require_once $rootDir . '/vendor/autoload.php';
|
|
}
|
|
|
|
require_once $rootDir . '/inc/dbconfig.php';
|
|
require_once $rootDir . '/inc/opendb.php';
|
|
require_once $rootDir . '/api/helpers/response.php';
|
|
require_once $rootDir . '/api/helpers/jwt.php';
|
|
require_once $rootDir . '/inc/auth.php';
|
|
|
|
// Parse the request path
|
|
$requestUri = $_SERVER['REQUEST_URI'] ?? '/';
|
|
$scriptName = $_SERVER['SCRIPT_NAME'] ?? '';
|
|
|
|
// Remove query string
|
|
$path = parse_url($requestUri, PHP_URL_PATH);
|
|
|
|
// Remove /api prefix and normalize
|
|
$basePath = dirname($scriptName);
|
|
if ($basePath !== '/') {
|
|
$path = substr($path, strlen($basePath));
|
|
}
|
|
$path = '/' . trim(str_replace('/api', '', $path), '/');
|
|
|
|
$method = $_SERVER['REQUEST_METHOD'];
|
|
|
|
// Simple route matching
|
|
$routes = [
|
|
'POST /auth/login' => 'auth/login.php',
|
|
'POST /auth/refresh' => 'auth/refresh.php',
|
|
'POST /auth/logout' => 'auth/logout.php',
|
|
'GET /auth/me' => 'auth/me.php',
|
|
'GET /health' => 'health.php',
|
|
];
|
|
|
|
$routeKey = $method . ' ' . $path;
|
|
|
|
if (isset($routes[$routeKey])) {
|
|
$handlerPath = $rootDir . '/api/endpoints/' . $routes[$routeKey];
|
|
|
|
if (file_exists($handlerPath)) {
|
|
require $handlerPath;
|
|
} else {
|
|
api_error('Endpoint not implemented', 501);
|
|
}
|
|
} else {
|
|
api_error('Not found', 404);
|
|
}
|