mirror of
https://github.com/zhang-pu/guestbook.git
synced 2026-12-01 09:18:27 +08:00
158 lines
7 KiB
PHP
158 lines
7 KiB
PHP
<?php
|
||
/**
|
||
* 前台:留言表单 + 留言列表(加固版)
|
||
*/
|
||
declare(strict_types=1);
|
||
require_once __DIR__ . '/config.php';
|
||
|
||
$message_sent = false;
|
||
$form_error = '';
|
||
$name = $email = $content = '';
|
||
|
||
if (is_post()) {
|
||
require_csrf(); // 原版没有 CSRF
|
||
$name = get_post('name');
|
||
$email = get_post('email');
|
||
$content = get_post('content');
|
||
|
||
if ($name === '') {
|
||
$form_error = '请输入姓名';
|
||
} elseif (mb_len($name) > MAX_NAME_LEN) {
|
||
$form_error = '姓名不能超过 ' . MAX_NAME_LEN . ' 个字符';
|
||
} elseif ($content === '') {
|
||
$form_error = '请输入留言内容';
|
||
} elseif (mb_len($content) < MIN_CONTENT_LEN) {
|
||
$form_error = '留言内容至少 ' . MIN_CONTENT_LEN . ' 个字符';
|
||
} elseif (mb_len($content) > MAX_CONTENT_LEN) {
|
||
$form_error = '留言内容不能超过 ' . MAX_CONTENT_LEN . ' 个字符';
|
||
} elseif ($email !== '' && (mb_len($email) > MAX_EMAIL_LEN || !filter_var($email, FILTER_VALIDATE_EMAIL))) {
|
||
$form_error = '邮箱格式不正确';
|
||
} elseif (!rate_limit('msg:' . client_ip(), RATE_LIMIT_MESSAGE, RATE_LIMIT_MESSAGE_WINDOW)) {
|
||
$form_error = '提交太频繁,请稍后再试';
|
||
log_error('留言限流触发 ip=' . client_ip());
|
||
} else {
|
||
try {
|
||
$db = get_db();
|
||
$st = $db->prepare('INSERT INTO messages (name, email, content, ip_address, status) VALUES (?, ?, ?, ?, 1)');
|
||
$st->bindValue(1, $name, SQLITE3_TEXT);
|
||
$st->bindValue(2, $email !== '' ? $email : null, SQLITE3_TEXT);
|
||
$st->bindValue(3, $content, SQLITE3_TEXT);
|
||
$st->bindValue(4, client_ip(), SQLITE3_TEXT);
|
||
$st->execute();
|
||
$message_sent = true;
|
||
$name = $email = $content = '';
|
||
} catch (Throwable $e) {
|
||
log_error('留言写入失败: ' . $e->getMessage());
|
||
$form_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 WHERE status = 1');
|
||
$total_pages = max(1, (int)ceil($total / PAGE_SIZE));
|
||
if ($page > $total_pages) { $page = $total_pages; }
|
||
$offset = PAGE_SIZE * ($page - 1);
|
||
// 注意:必须带上 reply / replied_at,否则前台不显示管理员回复(2026-10-30 修)
|
||
$st = $db->prepare('SELECT id, name, content, created_at, reply, replied_at FROM messages WHERE status = 1 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">
|
||
</head>
|
||
<body>
|
||
<div class="container">
|
||
<header>
|
||
<h1><?= e(SITE_NAME) ?></h1>
|
||
<nav>
|
||
<a href="index.php">首页</a>
|
||
<a href="https://zhangpu.dev/">zhangpu.dev</a>
|
||
<a href="login.php">管理</a>
|
||
</nav>
|
||
</header>
|
||
|
||
<main>
|
||
<section class="form-section">
|
||
<h2>发表留言</h2>
|
||
<?php if ($message_sent): ?>
|
||
<div class="success">恭喜!您的留言已成功提交。</div>
|
||
<?php endif; ?>
|
||
<?php if ($form_error !== ''): ?>
|
||
<div class="error"><?= e($form_error) ?></div>
|
||
<?php endif; ?>
|
||
<form method="POST" action="index.php" class="guestbook-form">
|
||
<?= csrf_field() ?>
|
||
<div class="form-group">
|
||
<label for="name">姓名 *:</label>
|
||
<input type="text" id="name" name="name" required maxlength="<?= MAX_NAME_LEN ?>" value="<?= e($name) ?>">
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="email">邮箱(可选,不公开展示):</label>
|
||
<input type="email" id="email" name="email" maxlength="<?= MAX_EMAIL_LEN ?>" value="<?= e($email) ?>">
|
||
</div>
|
||
<div class="form-group">
|
||
<label for="content">留言内容 *(<?= MIN_CONTENT_LEN ?>-<?= MAX_CONTENT_LEN ?>字):</label>
|
||
<textarea id="content" name="content" rows="5" required maxlength="<?= MAX_CONTENT_LEN ?>"><?= e($content) ?></textarea>
|
||
</div>
|
||
<button type="submit" class="btn">提交留言</button>
|
||
</form>
|
||
</section>
|
||
|
||
<section class="messages-section">
|
||
<h2>留言列表</h2>
|
||
<?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>
|
||
<span class="time"><?= e(date('Y-m-d H:i', strtotime((string)$msg['created_at']))) ?></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>
|
||
<?php endforeach; ?>
|
||
<?php endif; ?>
|
||
|
||
<?php if ($total_pages > 1): ?>
|
||
<div class="pagination">
|
||
<span>第 <?= (int)$page ?>/<?= (int)$total_pages ?> 页</span>
|
||
<?php if ($page > 1): ?><a href="?page=<?= (int)$page - 1 ?>">上一页</a><?php endif; ?>
|
||
<?php if ($page < $total_pages): ?><a href="?page=<?= (int)$page + 1 ?>">下一页</a><?php endif; ?>
|
||
</div>
|
||
<?php endif; ?>
|
||
</section>
|
||
</main>
|
||
|
||
<footer>
|
||
<p>© <?= date('Y') ?> <?= e(SITE_NAME) ?></p>
|
||
</footer>
|
||
</div>
|
||
<script src="assets/script.js"></script>
|
||
</body>
|
||
</html>
|