-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathapi.php
More file actions
209 lines (169 loc) · 5.87 KB
/
api.php
File metadata and controls
209 lines (169 loc) · 5.87 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
ob_start();
require_once 'vendor/autoload.php';
require_once 'config/database.php';
require_once 'config/upload.php';
// 初始化
$db = Database::getInstance();
$pdo = $db->getConnection();
$config = Database::getConfig($pdo);
// ============================================
// 工具函数
// ============================================
/**
* 从数据库获取配置值
*/
function getConfigValue($pdo, $key) {
$stmt = $pdo->prepare("SELECT value FROM configs WHERE `key` = ?");
$stmt->execute([$key]);
return (int)$stmt->fetch(PDO::FETCH_ASSOC)['value'];
}
/**
* 检查域名是否被允许
*/
function isDomainAllowed($host) {
global $config;
if (empty($host)) return false;
$siteDomains = array_map('trim', explode(',', $config['site_domain']));
// 通配符允许所有域名
if (in_array('*', $siteDomains)) return true;
// 检查域名是否匹配
foreach ($siteDomains as $domain) {
if ($host === parse_url($domain, PHP_URL_HOST)) {
return true;
}
}
return false;
}
/**
* 记录日志
*/
function logMessage($message) {
file_put_contents('上传日志.txt', "[" . date('Y-m-d H:i:s') . "] $message" . PHP_EOL, FILE_APPEND);
}
/**
* 返回JSON响应并退出
*/
function respondAndExit($response) {
ob_end_clean();
header('Content-Type: application/json; charset=utf-8');
echo json_encode($response, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
// ============================================
// 上传限制
// ============================================
/**
* 检查上传次数限制
*/
function isUploadAllowed($maxUploadsPerDay) {
if ($maxUploadsPerDay <= 0) return true;
$uploadDir = 'i/.upload_limits/';
if (!is_dir($uploadDir)) mkdir($uploadDir, 0777, true);
$clientIp = getClientIp();
$currentDate = date('Y-m-d');
$limitFile = $uploadDir . md5($clientIp) . '.json';
// 读取或初始化记录
$uploadData = file_exists($limitFile)
? json_decode(file_get_contents($limitFile), true) ?: []
: [];
// 新的一天重置计数
if (!isset($uploadData['date']) || $uploadData['date'] !== $currentDate) {
$uploadData = ['date' => $currentDate, 'count' => 0, 'ip' => $clientIp];
}
// 检查限制
if ($uploadData['count'] >= $maxUploadsPerDay) {
return "上传次数已达今日限制({$maxUploadsPerDay}次),请明天再试";
}
// 更新计数
$uploadData['count']++;
$uploadData['last_upload'] = date('Y-m-d H:i:s');
file_put_contents($limitFile, json_encode($uploadData, JSON_PRETTY_PRINT));
// 10%概率清理过期文件
if (rand(1, 10) === 1) {
foreach (glob($uploadDir . '*.json') as $file) {
$data = json_decode(@file_get_contents($file), true);
if (isset($data['date']) && $data['date'] !== $currentDate) {
@unlink($file);
}
}
}
return true;
}
// ============================================
// 验证函数
// ============================================
/**
* 验证Token和请求来源
*/
function validateToken() {
global $pdo;
// 获取Token
$token = '';
if (preg_match('/Bearer\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'] ?? '', $matches)) {
$token = $matches[1];
} else {
$token = $_POST['token'] ?? '';
}
// 验证域名
$refererHost = parse_url($_SERVER['HTTP_REFERER'] ?? '', PHP_URL_HOST);
if (isDomainAllowed($refererHost)) return;
// 验证Token
if (!empty($token)) {
$stmt = $pdo->prepare("SELECT id FROM users WHERE token = ?");
$stmt->execute([$token]);
if ($stmt->fetch()) return;
}
respondAndExit(['result' => 'error', 'code' => 403, 'message' => 'Token验证失败 或 域名未授权']);
}
/**
* 设置CORS响应头
*/
function setCorsHeaders() {
global $config;
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
$siteDomains = array_map('trim', explode(',', $config['site_domain']));
if (in_array('*', $siteDomains)) {
header("Access-Control-Allow-Origin: *");
} else if (!empty($origin) && isDomainAllowed(parse_url($origin, PHP_URL_HOST))) {
header("Access-Control-Allow-Origin: $origin");
header("Access-Control-Allow-Credentials: true");
} else {
header("Access-Control-Allow-Origin: null");
}
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
}
// ============================================
// 主流程
// ============================================
try {
setCorsHeaders();
if ($_SERVER['REQUEST_METHOD'] !== 'POST' || empty($_FILES)) {
respondAndExit(['result' => 'error', 'code' => 204, 'message' => '无文件上传']);
}
// 验证上传次数
$maxUploadsPerDay = getConfigValue($pdo, 'max_uploads_per_day');
$uploadCheck = isUploadAllowed($maxUploadsPerDay);
if ($uploadCheck !== true) {
respondAndExit(['result' => 'error', 'message' => $uploadCheck]);
}
// 验证文件大小
$maxFileSize = getConfigValue($pdo, 'max_file_size');
foreach ($_FILES as $file) {
if ($file['size'] > $maxFileSize) {
$maxFileSizeMB = $maxFileSize / (1024 * 1024);
respondAndExit(['result' => 'error', 'message' => "文件大小超过限制,最大允许 {$maxFileSizeMB}MB"]);
}
}
// 验证权限
validateToken();
// 处理上传
foreach ($_FILES as $file) {
handleUploadedFile($file, $_POST['token'] ?? '', $_SERVER['HTTP_REFERER'] ?? '');
}
} catch (Exception $e) {
logMessage('错误: ' . $e->getMessage());
respondAndExit(['result' => 'error', 'code' => 500, 'message' => '发生错误: ' . $e->getMessage()]);
}
?>