-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecycle_bin.php
More file actions
277 lines (239 loc) · 10.7 KB
/
Copy pathrecycle_bin.php
File metadata and controls
277 lines (239 loc) · 10.7 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
<?php
declare(strict_types=1);
require_once __DIR__ . '/auth.php';
require_once __DIR__ . '/functions.php';
require_login();
require_admin();
$pdo = db();
$currentUser = current_user();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if (!verify_csrf($_POST['csrf_token'] ?? null)) {
set_flash('danger', '非法请求,请刷新后重试。');
redirect('recycle_bin.php');
}
$action = (string) ($_POST['action'] ?? '');
$isRestoreAction = in_array($action, ['restore', 'batch_restore'], true);
$isPurgeAction = in_array($action, ['purge', 'batch_purge'], true);
if (!$isRestoreAction && !$isPurgeAction) {
set_flash('danger', '不支持的操作。');
redirect('recycle_bin.php');
}
$fileIds = [];
if (in_array($action, ['batch_restore', 'batch_purge'], true)) {
$submittedIds = $_POST['file_ids'] ?? [];
if (!is_array($submittedIds)) {
set_flash('danger', '参数错误。');
redirect('recycle_bin.php');
}
foreach ($submittedIds as $submittedId) {
$id = (int) $submittedId;
if ($id > 0) {
$fileIds[$id] = $id;
}
}
$fileIds = array_values($fileIds);
} else {
$fileId = (int) ($_POST['id'] ?? 0);
if ($fileId > 0) {
$fileIds[] = $fileId;
}
}
if (!$fileIds) {
set_flash('danger', '请先选择要操作的文件。');
redirect('recycle_bin.php');
}
$placeholders = [];
$params = [];
foreach ($fileIds as $index => $id) {
$key = ':id' . $index;
$placeholders[] = $key;
$params[$key] = $id;
}
$inClause = implode(', ', $placeholders);
if ($isRestoreAction) {
$selectStmt = $pdo->prepare("SELECT id, original_name FROM files WHERE deleted_at IS NOT NULL AND id IN ($inClause)");
$selectStmt->execute($params);
$targetFiles = $selectStmt->fetchAll();
if (!$targetFiles) {
set_flash('danger', '未找到可恢复的文件。');
redirect('recycle_bin.php');
}
$pdo->beginTransaction();
try {
$updateStmt = $pdo->prepare("UPDATE files SET deleted_at = NULL, deleted_by = NULL, updated_at = NOW() WHERE deleted_at IS NOT NULL AND id IN ($inClause)");
$updateStmt->execute($params);
foreach ($targetFiles as $targetFile) {
log_file_action($pdo, (int) ($currentUser['id'] ?? 0), (int) $targetFile['id'], 'delete', '从回收站恢复:' . (string) $targetFile['original_name']);
}
$pdo->commit();
$restoredCount = (int) $updateStmt->rowCount();
if ($restoredCount <= 0) {
set_flash('danger', '恢复失败,文件状态已变化。');
} else {
set_flash('success', '已恢复 ' . $restoredCount . ' 个文件。');
}
} catch (Throwable $e) {
$pdo->rollBack();
set_flash('danger', '恢复失败:' . $e->getMessage());
}
redirect('recycle_bin.php');
}
$selectStmt = $pdo->prepare("SELECT id, original_name, stored_path FROM files WHERE deleted_at IS NOT NULL AND id IN ($inClause)");
$selectStmt->execute($params);
$targetFiles = $selectStmt->fetchAll();
if (!$targetFiles) {
set_flash('danger', '未找到可彻底删除的文件。');
redirect('recycle_bin.php');
}
$pdo->beginTransaction();
try {
foreach ($targetFiles as $targetFile) {
log_file_action($pdo, (int) ($currentUser['id'] ?? 0), (int) $targetFile['id'], 'delete', '回收站彻底删除:' . (string) $targetFile['original_name']);
}
$deleteStmt = $pdo->prepare("DELETE FROM files WHERE deleted_at IS NOT NULL AND id IN ($inClause)");
$deleteStmt->execute($params);
$deletedCount = (int) $deleteStmt->rowCount();
if ($deletedCount <= 0) {
throw new RuntimeException('文件状态已变化,请刷新后重试。');
}
$pdo->commit();
foreach ($targetFiles as $targetFile) {
$fullPath = STORAGE_DIR . '/' . (string) $targetFile['stored_path'];
if (is_file($fullPath)) {
@unlink($fullPath);
}
}
set_flash('success', '已彻底删除 ' . $deletedCount . ' 个文件。');
} catch (Throwable $e) {
$pdo->rollBack();
set_flash('danger', '彻底删除失败:' . $e->getMessage());
}
redirect('recycle_bin.php');
}
$keyword = trim((string) ($_GET['q'] ?? ''));
$files = get_recycle_bin_files($pdo, $keyword);
require __DIR__ . '/partials/header.php';
?>
<div class="card card-shadow border-0">
<div class="card-body">
<div class="d-flex flex-wrap justify-content-between align-items-center gap-2 mb-3">
<h5 class="mb-0">回收站</h5>
<small class="text-muted">仅管理员可查看和操作</small>
</div>
<form class="row g-2 mb-3" method="get">
<div class="col-md-9">
<input type="text" name="q" value="<?= e($keyword) ?>" class="form-control" placeholder="搜索回收站文件名">
</div>
<div class="col-md-3 d-grid">
<button type="submit" class="btn btn-primary">查询</button>
</div>
</form>
<form method="post" id="batchActionForm" class="d-flex flex-wrap align-items-center gap-2 mb-3" onsubmit="return handleBatchSubmit(event);">
<input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
<div class="form-check me-2">
<input class="form-check-input" type="checkbox" id="selectAllItems">
<label class="form-check-label" for="selectAllItems">全选</label>
</div>
<button class="btn btn-sm btn-outline-success" type="submit" name="action" value="batch_restore">批量恢复</button>
<button class="btn btn-sm btn-outline-danger" type="submit" name="action" value="batch_purge">批量彻底删除</button>
</form>
<div class="table-responsive">
<table class="table table-hover align-middle">
<thead class="table-light">
<tr>
<th style="width: 40px;"></th>
<th>文件名</th>
<th>大小</th>
<th>上传者</th>
<th>删除者</th>
<th>删除时间</th>
<th style="width: 220px;">操作</th>
</tr>
</thead>
<tbody>
<?php if (!$files): ?>
<tr>
<td colspan="7" class="text-center text-muted py-4">回收站为空</td>
</tr>
<?php else: ?>
<?php foreach ($files as $file): ?>
<tr>
<td>
<input
type="checkbox"
class="form-check-input js-batch-item"
name="file_ids[]"
value="<?= (int) $file['id'] ?>"
form="batchActionForm"
>
</td>
<td><?= e($file['original_name']) ?></td>
<td><?= e(format_file_size((int) $file['file_size'])) ?></td>
<td><?= e((string) $file['uploader_name']) ?></td>
<td><?= e((string) ($file['deleted_by_name'] ?? '未知')) ?></td>
<td><?= e((string) $file['deleted_at']) ?></td>
<td>
<div class="d-flex gap-2 flex-wrap">
<form method="post">
<input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
<input type="hidden" name="action" value="restore">
<input type="hidden" name="id" value="<?= (int) $file['id'] ?>">
<button class="btn btn-sm btn-outline-success" type="submit">恢复</button>
</form>
<form method="post" onsubmit="return confirm('确认彻底删除?此操作不可恢复。');">
<input type="hidden" name="csrf_token" value="<?= e(csrf_token()) ?>">
<input type="hidden" name="action" value="purge">
<input type="hidden" name="id" value="<?= (int) $file['id'] ?>">
<button class="btn btn-sm btn-outline-danger" type="submit">彻底删除</button>
</form>
</div>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
</table>
</div>
</div>
</div>
<script>
function handleBatchSubmit(event) {
const checkedItems = document.querySelectorAll('.js-batch-item:checked');
if (checkedItems.length === 0) {
alert('请至少选择一个文件。');
event.preventDefault();
return false;
}
const submitter = event.submitter;
if (submitter && submitter.value === 'batch_purge') {
const ok = confirm(`确认彻底删除选中的 ${checkedItems.length} 个文件?此操作不可恢复。`);
if (!ok) {
event.preventDefault();
return false;
}
}
return true;
}
(function () {
const selectAll = document.getElementById('selectAllItems');
const items = Array.from(document.querySelectorAll('.js-batch-item'));
if (!selectAll || items.length === 0) {
return;
}
const syncSelectAllState = () => {
const checkedCount = items.filter((item) => item.checked).length;
selectAll.checked = checkedCount > 0 && checkedCount === items.length;
selectAll.indeterminate = checkedCount > 0 && checkedCount < items.length;
};
selectAll.addEventListener('change', () => {
items.forEach((item) => {
item.checked = selectAll.checked;
});
syncSelectAllState();
});
items.forEach((item) => {
item.addEventListener('change', syncSelectAllState);
});
})();
</script>
<?php require __DIR__ . '/partials/footer.php';