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()); } ?>