-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinit.php
More file actions
349 lines (301 loc) · 11.6 KB
/
Copy pathinit.php
File metadata and controls
349 lines (301 loc) · 11.6 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
<?php
session_start();
define('API_VERSION', '1.0.0');
$config = require __DIR__ . '/config.php';
$dataDir = __DIR__ . '/data';
if (!is_dir($dataDir)) {
mkdir($dataDir, 0755, true);
}
$dbPath = $dataDir . '/data.db';
if (!file_exists($dbPath)) {
$pdo = new PDO('sqlite:' . $dbPath);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->exec("
CREATE TABLE IF NOT EXISTS \"main\" (
\"id\" INTEGER NOT NULL UNIQUE,
\"content\" TEXT NOT NULL,
\"content_type\" TEXT NOT NULL DEFAULT 'text',
\"user_name\" TEXT NOT NULL,
\"add_time\" TEXT NOT NULL,
\"quote_source\" TEXT,
\"is_hidden\" BLOB DEFAULT 'false',
PRIMARY KEY(\"id\" AUTOINCREMENT)
)
");
$pdo->exec("
CREATE TABLE IF NOT EXISTS sqlean_define(
name text primary key,
type text,
body text
)
");
$pdo = null;
}
$logFile = $dataDir . '/log/admin_actions.log';
$logDir = dirname($logFile);
if (!is_dir($logDir)) {
mkdir($logDir, 0755, true);
}
if (!file_exists($logFile)) {
touch($logFile);
}
require_once __DIR__ . '/class/SqliteStorage.php';
require_once __DIR__ . '/class/WebSecurity.php';
require_once __DIR__ . '/function.php';
initWebSecurity();
// 设置时区
date_default_timezone_set('Asia/Shanghai');
// 获取当前域名
$current_domain = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]";
// 解析请求参数
$show_all = isset($_GET['all']);
$show_docs = isset($_GET['docs']);
$page = max(1, intval($_GET['page'] ?? 1));
$per_page = 20;
// 管理员路由处理
$request_uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
if (strpos($request_uri, '/admin') === 0) {
// 管理员登录检查
$is_admin = isset($_SESSION['admin_logged_in']) && $_SESSION['admin_logged_in'] === true;
$storage = createStorageStrategy($config);
// 登录页面
if ($request_uri === '/admin/login') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'] ?? '';
$password = $_POST['password'] ?? '';
if ($username === $config['admin']['name'] && $password === $config['admin']['password']) {
$_SESSION['admin_logged_in'] = true;
$_SESSION['admin_username'] = $username;
logAdminAction('用户登录');
header('Location: /admin');
exit;
} else {
$error = '用户名或密码错误';
}
}
echo renderTemplate('base.php', [
'title' => '管理员登录',
'content' => renderTemplate('admin_login.php', ['error' => $error ?? null]),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 退出登录
if ($request_uri === '/admin/logout') {
logAdminAction('用户退出');
session_destroy();
header('Location: /admin/login');
exit;
}
// 检查是否已登录
if (!$is_admin && $request_uri !== '/admin/login') {
header('Location: /admin/login');
exit;
}
// 管理面板首页
if ($request_uri === '/admin') {
$quotes = $storage->getQuotes();
$total_quotes = count($quotes);
$text_quotes = count(array_filter($quotes, function ($quote) {
return $quote['content_type'] === 'text';
}));
$image_quotes = count(array_filter($quotes, function ($quote) {
return $quote['content_type'] === 'image';
}));
$hidden_quotes = count(array_filter($quotes, function ($quote) {
return $quote['is_hidden'];
}));
echo renderTemplate('base.php', [
'title' => '管理面板',
'content' => renderTemplate('admin_panel.php', [
'total_quotes' => $total_quotes,
'text_quotes' => $text_quotes,
'image_quotes' => $image_quotes,
'hidden_quotes' => $hidden_quotes
]),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 添加内容页面
if ($request_uri === '/admin/add') {
echo renderTemplate('base.php', [
'title' => '添加内容',
'content' => renderTemplate('admin_add.php'),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 保存内容
if ($request_uri === '/admin/save') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!validateCsrfToken($_POST['csrf_token'] ?? '')) {
die('CSRF token validation failed');
}
$data = [
'content' => $_POST['content'],
'content_type' => $_POST['content_type'] ?? 'text',
'user_name' => $_POST['user_name'],
'quote_source' => $_POST['quote_source'] ?? '',
'is_hidden' => isset($_POST['is_hidden']) ? 1 : 0,
'add_time' => time()
];
if ($storage instanceof SqliteStorage) {
$stmt = $storage->getDb()->prepare('INSERT INTO main (content, content_type, user_name, quote_source, is_hidden, add_time)
VALUES (:content, :content_type, :user_name, :quote_source, :is_hidden, :add_time)');
$stmt->execute($data);
$id = $storage->getDb()->lastInsertId();
logAdminAction('添加内容', 'ID: ' . $id);
}
header('Location: /admin/list');
exit;
}
}
// 内容列表
if ($request_uri === '/admin/list') {
$search = $_GET['search'] ?? '';
$content_type = $_GET['content_type'] ?? '';
$visibility = $_GET['visibility'] ?? '';
$quotes = $storage->getQuotes();
// 应用筛选
if (!empty($search)) {
$quotes = array_filter($quotes, function ($quote) use ($search) {
return stripos($quote['content'], $search) !== false ||
stripos($quote['user_name'], $search) !== false;
});
}
if (!empty($content_type)) {
$quotes = array_filter($quotes, function ($quote) use ($content_type) {
return $quote['content_type'] === $content_type;
});
}
if (!empty($visibility)) {
$quotes = array_filter($quotes, function ($quote) use ($visibility) {
return ($visibility === 'visible' && !$quote['is_hidden']) ||
($visibility === 'hidden' && $quote['is_hidden']);
});
}
// 分页
$total = count($quotes);
$total_pages = ceil($total / $per_page);
$offset = ($page - 1) * $per_page;
$paged_quotes = array_slice($quotes, $offset, $per_page);
echo renderTemplate('base.php', [
'title' => '内容列表',
'content' => renderTemplate('admin_list.php', [
'quotes' => $paged_quotes,
'current_domain' => $current_domain,
'total_pages' => $total_pages,
'page' => $page
]),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 批量操作
if ($request_uri === '/admin/batch_action') {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!validateCsrfToken($_POST['csrf_token'] ?? '')) {
die('CSRF token validation failed');
}
$ids = $_POST['ids'] ?? [];
$action = $_POST['batch_action'] ?? '';
if (!empty($ids) && $action) {
$placeholders = implode(',', array_fill(0, count($ids), '?'));
switch ($action) {
case 'show':
$stmt = $storage->getDb()->prepare("UPDATE main SET is_hidden = 0 WHERE id IN ($placeholders)");
$stmt->execute($ids);
logAdminAction('批量显示内容', 'ID: ' . implode(',', $ids));
break;
case 'hide':
$stmt = $storage->getDb()->prepare("UPDATE main SET is_hidden = 1 WHERE id IN ($placeholders)");
$stmt->execute($ids);
logAdminAction('批量隐藏内容', 'ID: ' . implode(',', $ids));
break;
case 'delete':
$stmt = $storage->getDb()->prepare("DELETE FROM main WHERE id IN ($placeholders)");
$stmt->execute($ids);
logAdminAction('批量删除内容', 'ID: ' . implode(',', $ids));
break;
}
}
header('Location: /admin/list');
exit;
}
}
// 删除内容
if (preg_match('#^/admin/delete/(\d+)$#', $request_uri, $matches)) {
$id = $matches[1];
if ($storage instanceof SqliteStorage) {
$stmt = $storage->getDb()->prepare('DELETE FROM main WHERE id = ?');
$stmt->execute([$id]);
logAdminAction('删除内容', 'ID: ' . $id);
}
header('Location: /admin/list');
exit;
}
// 编辑内容页面
if (preg_match('#^/admin/edit/(\d+)$#', $request_uri, $matches)) {
$id = $matches[1];
$quote = $storage->getQuoteById($id);
if (!$quote) {
header('Location: /admin/list');
exit;
}
echo renderTemplate('base.php', [
'title' => '编辑内容',
'content' => renderTemplate('admin_edit.php', ['quote' => $quote]),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 更新内容
if (preg_match('#^/admin/update/(\d+)$#', $request_uri, $matches)) {
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!validateCsrfToken($_POST['csrf_token'] ?? '')) {
die('CSRF token validation failed');
}
$id = $matches[1];
$data = [
'id' => $id,
'content' => $_POST['content'],
'content_type' => $_POST['content_type'] ?? 'text',
'user_name' => $_POST['user_name'],
'quote_source' => $_POST['quote_source'] ?? '',
'is_hidden' => isset($_POST['is_hidden']) ? 1 : 0
];
if ($storage instanceof SqliteStorage) {
$stmt = $storage->getDb()->prepare('UPDATE main SET
content = :content,
content_type = :content_type,
user_name = :user_name,
quote_source = :quote_source,
is_hidden = :is_hidden
WHERE id = :id');
$stmt->execute($data);
logAdminAction('更新内容', 'ID: ' . $id);
}
header('Location: /admin/list');
exit;
}
}
// 日志查看路由
if ($request_uri === '/admin/logs') {
echo renderTemplate('base.php', [
'title' => '管理员日志',
'content' => renderTemplate('admin_log.php', [
'config' => $config,
]),
'last_update_time' => date('Y-m-d H:i:s', time())
]);
exit;
}
// 日志下载路由
if ($request_uri === '/admin/logs/download') {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="admin_actions_' . date('Ymd_His') . '.log"');
readfile($config['admin']['log_file']);
exit;
}
}