File size: 2,381 Bytes
ca533c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
// 设置时区
date_default_timezone_set('Asia/Shanghai');

// 处理错误输出
ini_set('display_errors', 'Off');
error_reporting(E_ALL);
ini_set('log_errors', 'On');
ini_set('error_log', '/dev/stderr');

// 确保输出正确的 Content-Type
if (strpos($_SERVER['REQUEST_URI'], '/api.php') === 0 || 
    strpos($_SERVER['REQUEST_URI'], '/file_api.php') === 0 || 
    strpos($_SERVER['REQUEST_URI'], '/upload.php') === 0) {
    header('Content-Type: application/json');
}

// 处理 UUID 格式的 URL
if (preg_match('/^\/([a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12})$/', $_SERVER['REQUEST_URI'], $matches)) {
    $_GET['id'] = $matches[1];
    require __DIR__ . '/index.php';
    return true;
}

// 处理静态文件
if (preg_match('/\.(css|js|png|jpg|jpeg|gif|ico|webp)$/', $_SERVER['REQUEST_URI'])) {
    return false; // 让 PHP 内置服务器处理静态文件
}

// 处理 API 请求
if (strpos($_SERVER['REQUEST_URI'], '/api.php') === 0) {
    ob_start(); // 开始输出缓冲
    require __DIR__ . '/api.php';
    $output = ob_get_clean(); // 获取并清除缓冲
    if (json_decode($output) === null) {
        // 如果不是有效的 JSON,返回错误
        echo json_encode(['status' => 'error', 'message' => 'Invalid response']);
    } else {
        echo $output;
    }
    return true;
}

if (strpos($_SERVER['REQUEST_URI'], '/file_api.php') === 0) {
    ob_start(); // 开始输出缓冲
    require __DIR__ . '/file_api.php';
    $output = ob_get_clean(); // 获取并清除缓冲
    if (json_decode($output) === null && !headers_sent()) {
        // 如果不是有效的 JSON 且头部未发送,返回错误
        echo json_encode(['status' => 'error', 'message' => 'Invalid response']);
    } else {
        echo $output;
    }
    return true;
}

if (strpos($_SERVER['REQUEST_URI'], '/upload.php') === 0) {
    ob_start(); // 开始输出缓冲
    require __DIR__ . '/upload.php';
    $output = ob_get_clean(); // 获取并清除缓冲
    if (json_decode($output) === null) {
        // 如果不是有效的 JSON,返回错误
        echo json_encode(['status' => 'error', 'message' => 'Invalid response']);
    } else {
        echo $output;
    }
    return true;
}

// 默认路由到 index.php
require __DIR__ . '/index.php';
return true;