ez-api/index.php

48 lines
1.3 KiB
PHP
Raw Normal View History

2024-11-01 13:02:42 +08:00
<?php
2024-10-29 13:13:37 +08:00
include 'core/config.php';
include 'core/clean.php';
2024-10-30 12:59:20 +08:00
2024-11-01 13:02:42 +08:00
// TODO: cookie management
2024-10-29 13:13:37 +08:00
session_start();
2024-11-01 13:02:42 +08:00
// 获取参数并清理
2024-10-29 13:13:37 +08:00
$api_class = isset($_GET['class']) ? sanitizeInput($_GET['class']) : '';
2024-11-01 13:02:42 +08:00
$api = isset($_GET['api']) ? sanitizeInput($_GET['api']) : '';
2024-10-29 13:13:37 +08:00
2024-11-02 12:58:24 +08:00
2024-11-01 13:02:42 +08:00
if (empty($api_class)) {
2024-11-02 12:58:24 +08:00
http_response_code(200);
2024-11-01 13:02:42 +08:00
echo json_encode(array('status' => 'Home page'));
exit();
}
// 验证输入是否符合预期格式
elseif (!preg_match('/^[a-zA-Z0-9_]+$/', $api_class) || !preg_match('/^[a-zA-Z0-9_]+$/', $api)) {
2024-11-02 12:58:24 +08:00
header("Content-Type: application/json");
2024-11-01 13:02:42 +08:00
http_response_code(400); // Bad Request
echo json_encode(array('error' => 'Invalid input'));
exit();
}
// 构建文件路径
$file_path = 'includes/' . $api_class . '/' . $api . '.php';
// 检查文件是否存在
if (!file_exists($file_path)) {
2024-11-02 12:58:24 +08:00
header("Content-Type: application/json");
2024-11-01 13:02:42 +08:00
http_response_code(404); // Not Found
echo json_encode(array('error' => '404 Not Found', 'message' => 'The requested resource could not be found'));
exit();
}
// 如果不是 public API则启用路由
2024-10-29 13:13:37 +08:00
if ($api_class != 'public') {
2024-11-02 12:58:24 +08:00
header("Content-Type: application/json");
include 'core/router.php';
} else {
header("Content-Type: application/json");
2024-10-29 13:13:37 +08:00
}
2024-11-01 13:02:42 +08:00
// 包含文件
include $file_path;
?>