guestbook/admin.php
Zhang Pu 06d21f90db init: guestbook v1.0.0 首次发布
- 加固版留言系统 / 磁盘清理助手
- MIT License
2026-11-02 11:12:15 +08:00

208 lines
10 KiB
PHP
Raw 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
/**
* 后台管理(加固版)
* - 所有写操作(删除/隐藏/回复)改为 POST + CSRF token(原版用 GET 触发删除,可被 CSRF)
* - 操作写审计表 admin_logs
* - 样式路径用相对路径,部署到子目录也不丢样式
*/
declare(strict_types=1);
require_once __DIR__ . '/config.php';
check_admin();
$notice = '';
$error = '';
/* ---------------- 写操作:全部 POST + CSRF ---------------- */
if (is_post()) {
require_csrf();
$action = get_post('action');
$id = (int)get_post('id', '0');
try {
$db = get_db();
if ($action === 'delete' && $id > 0) {
$st = $db->prepare('DELETE FROM messages WHERE id = ?');
$st->bindValue(1, $id, SQLITE3_INTEGER);
$st->execute();
admin_log('delete', $id, '删除留言');
$notice = '留言已删除';
} elseif ($action === 'toggle_status' && $id > 0) {
$q = $db->prepare('SELECT status FROM messages WHERE id = ?');
$q->bindValue(1, $id, SQLITE3_INTEGER);
$row = $q->execute()->fetchArray(SQLITE3_ASSOC);
if ($row) {
$new = ((int)$row['status'] === 1) ? 0 : 1;
$u = $db->prepare('UPDATE messages SET status = ? WHERE id = ?');
$u->bindValue(1, $new, SQLITE3_INTEGER);
$u->bindValue(2, $id, SQLITE3_INTEGER);
$u->execute();
admin_log('toggle_status', $id, '新状态=' . $new);
$notice = $new === 1 ? '已显示' : '已隐藏';
}
} elseif ($action === 'reply' && $id > 0) {
$reply = get_post('reply');
if (mb_len($reply) > MAX_CONTENT_LEN) {
$error = '回复内容过长';
} elseif ($reply !== '') {
$u = $db->prepare('UPDATE messages SET reply = ?, replied_at = CURRENT_TIMESTAMP WHERE id = ?');
$u->bindValue(1, $reply, SQLITE3_TEXT);
$u->bindValue(2, $id, SQLITE3_INTEGER);
$u->execute();
admin_log('reply', $id, '回复留言');
$notice = '回复成功';
}
}
} catch (Throwable $e) {
log_error('后台操作失败: ' . $e->getMessage());
$error = '操作失败,请稍后再试';
}
}
/* ---------------- 列表 ---------------- */
$messages = [];
$total_pages = 1;
$page = max(1, (int)get_get('page', '1'));
try {
$db = get_db();
$total = (int)$db->querySingle('SELECT COUNT(*) FROM messages');
$total_pages = max(1, (int)ceil($total / PAGE_SIZE));
if ($page > $total_pages) { $page = $total_pages; }
$offset = PAGE_SIZE * ($page - 1);
$st = $db->prepare('SELECT * FROM messages ORDER BY created_at DESC, id DESC LIMIT ? OFFSET ?');
$st->bindValue(1, PAGE_SIZE, SQLITE3_INTEGER);
$st->bindValue(2, $offset, SQLITE3_INTEGER);
$res = $st->execute();
while ($row = $res->fetchArray(SQLITE3_ASSOC)) { $messages[] = $row; }
} catch (Throwable $e) {
log_error('后台列表读取失败: ' . $e->getMessage());
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>留言管理 - <?= e(SITE_NAME) ?></title>
<link rel="stylesheet" href="assets/style.css">
<style>
.admin-nav { margin-bottom: 20px; padding: 10px; background: #fff; border-radius: 4px; }
.admin-nav a { color: #2196f3; text-decoration: none; margin-right: 15px; }
.status-badge { padding: 2px 8px; border-radius: 4px; font-size: 12px; }
.status-visible { background: #4caf50; color: white; }
.status-hidden { background: #f44336; color: white; }
.message-card { margin-bottom: 20px; padding: 15px; border: 1px solid #ddd; border-radius: 8px; background: #fafafa; }
.message-card .reply { background: #e3f2fd; padding: 10px; margin-top: 10px; border-radius: 4px; border-left: 3px solid #2196f3; }
.actions { margin-top: 10px; }
.actions button, .actions a.btn { margin-right: 5px; padding: 5px 10px; cursor: pointer; border: none; border-radius: 4px; text-decoration: none; display: inline-block; font-size: 14px; }
.btn-hide { background: #f44336; color: white; }
.btn-show { background: #4caf50; color: white; }
.btn-delete { background: #2196f3; color: white; }
.btn-reply { background: #ff9800; color: white; }
.logout-form { display: inline; }
</style>
</head>
<body>
<div class="container">
<header>
<h1>留言管理后台</h1>
<div class="admin-nav">
<a href="admin.php">所有留言</a>
<a href="index.php">返回前台</a>
<a href="https://zhangpu.dev/">zhangpu.dev</a>
<form method="POST" action="logout.php" class="logout-form">
<?= csrf_field() ?>
<button type="submit" style="background:none;border:none;color:#f44336;cursor:pointer;">退出登录</button>
</form>
</div>
</header>
<main>
<?php if ($notice !== ''): ?><div class="success"><?= e($notice) ?></div><?php endif; ?>
<?php if ($error !== ''): ?><div class="error"><?= e($error) ?></div><?php endif; ?>
<?php if (!$messages): ?>
<div class="no-messages">暂无留言</div>
<?php else: ?>
<?php foreach ($messages as $msg): ?>
<div class="message-card">
<div class="message-header">
<strong class="name"><?= e($msg['name']) ?></strong>
<?php if (!empty($msg['email'])): ?>
<span class="email">(<?= e($msg['email']) ?>)</span>
<?php endif; ?>
<span class="time"><?= e(date('Y-m-d H:i', strtotime((string)$msg['created_at']))) ?></span>
<span class="ip">IP: <?= e($msg['ip_address']) ?></span>
<span class="status-badge <?= ((int)$msg['status'] === 1) ? 'status-visible' : 'status-hidden' ?>">
<?= ((int)$msg['status'] === 1) ? '显示' : '隐藏' ?>
</span>
</div>
<div class="content"><?= nl2br(e($msg['content'])) ?></div>
<?php if (!empty($msg['reply'])): ?>
<div class="reply">
<strong>管理员回复:</strong>
<?= nl2br(e($msg['reply'])) ?>
<?php if (!empty($msg['replied_at'])): ?>
<span class="reply-time">(<?= e(date('Y-m-d H:i', strtotime((string)$msg['replied_at']))) ?>)</span>
<?php endif; ?>
</div>
<?php endif; ?>
<div class="actions">
<form method="POST" action="admin.php" style="display:inline;">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int)$msg['id'] ?>">
<input type="hidden" name="action" value="toggle_status">
<button type="submit" class="btn <?= ((int)$msg['status'] === 1) ? 'btn-hide' : 'btn-show' ?>">
<?= ((int)$msg['status'] === 1) ? '隐藏' : '显示' ?>
</button>
</form>
<form method="POST" action="admin.php" style="display:inline;">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int)$msg['id'] ?>">
<input type="hidden" name="action" value="delete">
<button type="submit" class="btn btn-delete"
onclick="return confirm('确定删除这条留言?删除后不可恢复。');">删除</button>
</form>
<button type="button" class="btn btn-reply" onclick="toggleReply(<?= (int)$msg['id'] ?>)">回复</button>
</div>
<div id="reply-form-<?= (int)$msg['id'] ?>" class="reply-form" style="display:none; margin-top:15px; padding:10px; border:1px solid #ddd; border-radius:4px;">
<form method="POST" action="admin.php">
<?= csrf_field() ?>
<input type="hidden" name="id" value="<?= (int)$msg['id'] ?>">
<input type="hidden" name="action" value="reply">
<textarea name="reply" rows="3" style="width:100%; padding:8px; border:1px solid #ddd; border-radius:4px;" maxlength="<?= MAX_CONTENT_LEN ?>"><?= e($msg['reply'] ?? '') ?></textarea>
<div style="margin-top:10px;">
<button type="submit" class="btn" style="background:#4caf50;">保存回复</button>
<button type="button" class="btn" onclick="toggleReply(<?= (int)$msg['id'] ?>)">取消</button>
</div>
</form>
</div>
</div>
<?php endforeach; ?>
<?php endif; ?>
<?php if ($total_pages > 1): ?>
<div class="pagination" style="margin-top:20px; text-align:center;">
<span>第 <?= (int)$page ?> / <?= (int)$total_pages ?> 页</span>
<?php if ($page > 1): ?><a href="?page=<?= (int)$page - 1 ?>">&lt; 上一页</a><?php endif; ?>
<?php if ($page < $total_pages): ?><a href="?page=<?= (int)$page + 1 ?>">下一页 &gt;</a><?php endif; ?>
</div>
<?php endif; ?>
</main>
<footer><p>&copy; <?= date('Y') ?> <?= e(SITE_NAME) ?> 管理后台</p></footer>
</div>
<script>
function toggleReply(id) {
var form = document.getElementById('reply-form-' + id);
if (form) { form.style.display = (form.style.display === 'none') ? 'block' : 'none'; }
}
</script>
</body>
</html>