ez-api/index.php
2024-11-03 12:54:31 +08:00

49 lines
1.5 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// 这个玩意,写着写着就发现其实根本就没什么人用,不管你用了什么新技术,新架构,多么快速,多么方便,学生还是更愿意给人力驱动的表白墙投稿,所以后面写的就放飞自我了,写着玩~~
include 'core/config.php';
include 'core/clean.php';
// TODO: cookie management
session_start();
// 获取参数并清理
$api_class = isset($_GET['class']) ? sanitizeInput($_GET['class']) : '';
$api = isset($_GET['api']) ? sanitizeInput($_GET['api']) : '';
if (empty($api_class)) {
http_response_code(200);
echo json_encode(array('status' => 'Home page'));
exit();
}
// 验证输入是否符合预期格式
elseif (!preg_match('/^[a-zA-Z0-9_]+$/', $api_class) || !preg_match('/^[a-zA-Z0-9_]+$/', $api)) {
header("Content-Type: application/json");
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)) {
header("Content-Type: application/json");
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则启用路由
if ($api_class != 'public') {
header("Content-Type: application/json");
include 'core/router.php';
} else {
header("Content-Type: application/json");
}
// 包含文件
include $file_path;
?>