SOURCE DOWNLOAD

木漏茶室源码下载中心

整站 PHP 源码:可浏览文件、复制路径、下载单文件或打包 ZIP。

发布版本 v6.25.231 升级后自动生成独立源码包,下载中心只发送这份冻结包 已排除 config.php / 用户数据 / 上传文件 / 升级备份
komorebi-forum 183 个节点 · PHP 8.3.37 · 完整源码 ZIP 1.31 MB
下载完整源码 ZIP 源码包大小:1.31 MB 安装教程 作者主页 免费开源 免费下载 ↓
COMMUNITY DEPLOYMENTS

已经跑起来的木漏茶室

从源码下载中心下载、完成安装并回传验证的站外站点。每个站点只计 1 次部署下载量;已记录站点会实时读取名称、版本、Logo 与在线状态,本机、内网和证书异常站点无法完成回传。

实时检测
2 个站点已部署

已展示全部 2 个站点。

pages/post.php 53.4 KB · 发布包 v6.25.231
下载
<?php
/**
 * 帖子详情:密码门 / 正文 / 附件 / 评论 / 点赞收藏举报
 */
$id = (int) input('id');
$post = DB::one(
    'SELECT p.*, u.nickname, u.username, u.avatar, u.gender, u.verified, u.verify_style, u.verify_title, u.verify_type, u.role, u.points, u.show_location, u.show_ip, u.created_at AS u_created,
            c.name AS cat_name
     FROM ' . DB::table('posts') . ' p
     LEFT JOIN ' . DB::table('users') . ' u ON u.id = p.user_id
     LEFT JOIN ' . DB::table('categories') . ' c ON c.id = p.cat_id
     WHERE p.id = ?',
    [$id]
);

$postViewer = current_user();
$postAuthorViewer = $postViewer && (int) $postViewer['id'] === (int) ($post['user_id'] ?? 0);
if (!$post || ((int) $post['status'] !== 1 && !is_admin($postViewer) && !$postAuthorViewer)) {
    $GLOBALS['PAGE'] = 'post';
    render_header('帖子不存在');
    empty_state('这帖茶烟已散', '它可能被删除,或从未存在过');
    render_footer();
    return;
}

$me = current_user();
// 代登录时必须按被代登录账号的角色判断前台权限,不能沿用真实管理员的身份。
if (impersonating()) {
    $impUser = user_by_id((int) ($_SESSION['imp_uid'] ?? 0));
    if ($impUser) {
        $me = $impUser;
    }
}
$isOwner = $me && (int) $me['id'] === (int) $post['user_id'];
$actorSuper = is_superadmin(current_user());
$authorSuper = is_superadmin($post);
// 普通管理员不能管理超级管理员的帖子;超管的密码帖对普通管理员同样要求输入密码
$isModerator = $me && user_is_category_moderator((int) $me['id'], (int) $post['cat_id']);
$canManage = $isOwner || (($isModerator || is_admin(current_user())) && (!$authorSuper || $actorSuper));
$hasPassword = trim((string) $post['password']) !== '';
$granted = !$hasPassword || post_access_granted($id) || $isOwner;
$isLocked = (int) ($post['is_locked'] ?? 0) === 1;
// 锁定后所有用户都不能评论,只有管理员和超级管理员可以继续评论、回复和管理
$canComment = (int) $post['status'] === 1 && (!$isLocked || is_admin($me) || $isModerator);
// 发布者锁定后不能编辑或删除帖子,必须先解锁;管理员和超级管理员不受此限制
$canModifyPost = $canManage && (!$isLocked || is_admin($me) || $isModerator);
// 发布者免密进入自己的帖子;普通管理员只能免密查看普通用户的密码帖,
// 管理员不能免密查看其他管理员或超级管理员的密码帖;超级管理员可免密查看管理员和普通用户的帖子。
$canBypassPassword = $me && $hasPassword && !$isOwner && can_bypass_post_password($post, $me);
$authorOnly = (int) ($post['author_only'] ?? 0) === 1;
$authorOnlyBlocked = $authorOnly && !post_privileged_can_view($post, $me);

// ---------- 表单处理 ----------
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    check_csrf();
    if ($authorOnlyBlocked) {
        flash('此帖仅作者本人可见,无法进行该操作', 'err');
        redirect(page_url('post', ['id' => $id]));
    }
    $act = input('act');

    if ($act === 'review_approve' || $act === 'review_reject') {
        if (!is_admin($me)) {
            flash('只有管理员或超级管理员可以审核帖子', 'err');
            redirect(page_url('post', ['id' => $id]));
        }
        if ((int) $post['status'] !== 2) {
            flash('这篇帖子不在待审核状态', 'err');
            redirect(page_url('post', ['id' => $id]));
        }
        if ($act === 'review_approve') {
            post_review_approve($id, $me);
            flash('帖子已审核通过并发布');
        } else {
            $reason = trim((string) ($_POST['reason'] ?? ''));
            if ($reason === '') {
                flash('请填写驳回理由', 'err');
                redirect(page_url('post', ['id' => $id]) . '#reviewBox');
            }
            if (!post_review_reject($id, $reason, $me)) {
                flash('驳回失败,请稍后重试', 'err');
            } else {
                flash('帖子已驳回,并已通知发布者');
            }
        }
        redirect(page_url('post', ['id' => $id]));
    }

    if ($act === 'unlock') {
        $pw = (string) ($_POST['password'] ?? '');
        if ($pw !== '' && password_verify($pw, $post['password'])) {
            grant_post_access($id);
            flash('密码正确,茶室已为你开门');
            redirect(page_url('post', ['id' => $id]));
        }
        flash('密码不正确,无法查看内容', 'err');
        redirect(page_url('post', ['id' => $id]));
    }

    if ($act === 'bypass') {
        if ($canBypassPassword) {
            grant_post_access($id);
            flash('已免密码查看该帖');
        } else {
            flash('你没有免密码查看这篇帖子的权限', 'err');
        }
        redirect(page_url('post', ['id' => $id]));
    }

    if (!$granted) {
        flash('请先输入访问密码', 'err');
        redirect(page_url('post', ['id' => $id]));
    }

    // 附件 / 网盘信息积分解锁:买家扣分,发布者入账,并写入双方积分流水
    if ($act === 'unlock_content') {
        $kind = input('kind') === 'netdisk' ? 'netdisk' : 'attach';
        $rule = post_content_rule($post, $kind);
        if (!$me) {
            flash('请先登录后再解锁内容', 'err');
            redirect(page_url('login'));
        }
        if ($rule['visibility'] !== 'points') {
            flash('这类内容不需要积分解锁', 'err');
        } elseif (post_content_unlocked($id, (int) $me['id'], $kind)) {
            flash('你已经解锁过这部分内容', 'err');
        } elseif (post_content_visible($post, $kind, $me)) {
            flash('你已经有权限查看这部分内容', 'err');
        } else {
            $price = (int) $rule['points'];
            $buyerPoints = (int) ($me['points'] ?? 0);
            if (!is_superadmin($me) && $buyerPoints < $price) {
                flash('积分不足,当前只有 ' . $buyerPoints . ' 积分,需要 ' . $price . ' 积分', 'err');
            } else {
                try {
                    DB::insert('post_unlocks', [
                        'post_id'    => $id,
                        'user_id'    => (int) $me['id'],
                        'kind'       => $kind,
                        'points'     => $price,
                        'created_at' => now(),
                    ]);
                    if ($price > 0 && !is_superadmin($me)) {
                        add_points($me['id'], -$price);
                        add_points($post['user_id'], $price);
                        DB::insert('point_logs', [
                            'user_id'    => (int) $me['id'],
                            'action'     => $kind === 'netdisk' ? 'unlock_netdisk' : 'unlock_attach',
                            'delta'      => -$price,
                            'note'       => '解锁帖子《' . post_display_title($post, 40) . '》的' . ($kind === 'netdisk' ? '网盘信息' : '附件') . ' #' . $id,
                            'ref_id'     => $id,
                            'created_at' => now(),
                        ]);
                        DB::insert('point_logs', [
                            'user_id'    => (int) $post['user_id'],
                            'action'     => $kind === 'netdisk' ? 'unlock_income_netdisk' : 'unlock_income_attach',
                            'delta'      => $price,
                            'note'       => display_name($me) . ' 解锁了你的帖子《' . post_display_title($post, 40) . '》的' . ($kind === 'netdisk' ? '网盘信息' : '附件') . ' #' . $id,
                            'ref_id'     => $id,
                            'created_at' => now(),
                        ]);
                        notify($post['user_id'], $me['id'], 'points', $id, display_name($me) . ' 解锁了你的帖子内容,+' . $price . ' 积分');
                    }
                    flash('已解锁,' . $price . ' 积分已转给发布者');
                } catch (Exception $e) {
                    flash('解锁记录已存在,请刷新后查看', 'err');
                }
            }
        }
        redirect(page_url('post', ['id' => $id]) . ($kind === 'netdisk' ? '#netdisk' : '#attachments'));
    }

    if ($act === 'lock') {
        if (!$canManage || ($authorSuper && !$actorSuper)) {
            flash('你没有锁定或解锁这篇帖子的权限', 'err');
        } else {
            $newLocked = (int) $post['is_locked'] === 1 ? 0 : 1;
            DB::run('UPDATE ' . DB::table('posts') . ' SET is_locked = ? WHERE id = ?', [$newLocked, $id]);
            flash($newLocked ? '帖子已锁定,只有管理员和超级管理员可以评论' : '帖子已解锁,所有登录用户都可以评论');
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'like') {
        if (!$me) {
            flash('登录后才能点赞', 'err');
        } elseif (feature_denied($me, 'like')) {
            flash('「点赞」功能已被管理员关闭', 'err');
        } else {
            $has = DB::val('SELECT COUNT(*) FROM ' . DB::table('likes') . ' WHERE post_id = ? AND user_id = ?', [$id, $me['id']]);
            if ($has) {
                DB::delete('likes', 'post_id = ? AND user_id = ?', [$id, $me['id']]);
            } else {
                DB::insert('likes', ['post_id' => $id, 'user_id' => $me['id'], 'created_at' => now()]);
                reward_interaction_points($me['id'], 'like', $id, '点赞');
                notify($post['user_id'], $me['id'], 'like', $id, '有人赞了你的帖子《' . post_display_title($post, 40) . '》');
            }
        }
        redirect(page_url('post', ['id' => $id]) . '#top');
    }

    if ($act === 'fav') {
        if (!$me) {
            flash('登录后才能收藏', 'err');
        } elseif (feature_denied($me, 'favorite')) {
            flash('「收藏」功能已被管理员关闭', 'err');
        } else {
            $has = DB::val('SELECT COUNT(*) FROM ' . DB::table('favorites') . ' WHERE post_id = ? AND user_id = ?', [$id, $me['id']]);
            if ($has) {
                DB::delete('favorites', 'post_id = ? AND user_id = ?', [$id, $me['id']]);
            } else {
                DB::insert('favorites', ['post_id' => $id, 'user_id' => $me['id'], 'created_at' => now()]);
                reward_interaction_points($me['id'], 'fav', $id, '收藏');
                notify($post['user_id'], $me['id'], 'fav', $id, '有人收藏了你的帖子《' . post_display_title($post, 40) . '》');
            }
        }
        redirect(page_url('post', ['id' => $id]) . '#top');
    }

    if ($act === 'comment') {
        if (!$me) {
            flash('登录后才能评论', 'err');
        } elseif (feature_denied($me, 'comment')) {
            flash('「评论与回复」功能已被管理员关闭', 'err');
        } elseif (!$canComment) {
            flash('帖子已锁定,只有管理员和超级管理员可以评论', 'err');
        } elseif (user_is_muted($me) || user_is_site_muted($me)) {
            flash('你已被管理员禁言,暂时不能评论', 'err');
        } else {
            $content = trim((string) ($_POST['content'] ?? ''));
            if ($content === '') {
                flash('评论内容不能为空', 'err');
            } elseif (mb_strlen($content) > 600) {
                flash('评论最多 600 字', 'err');
            } else {
                $adGuard = plugin_content_check('post_comment', $content);
                if ($adGuard) {
                    flash($adGuard['message'], 'err');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                $sens = sensitive_filter($content);
                if ($sens['action'] === 'block') {
                    flash('评论包含不允许发布的敏感词:' . $sens['hit'], 'err');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                $content = $sens['text'];
                $reviewHold = $sens['action'] === 'review';
                $commentId = DB::insert('comments', [
                    'post_id'    => $id,
                    'user_id'    => $me['id'],
                    'content'    => $content,
                    'status'    => $reviewHold ? 2 : 1,
                    'created_at' => now(),
                ]);
                if ($reviewHold) {
                    flash('评论已提交审核,审核通过后才会公开显示');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                reward_points($me['id'], 'comment', $id);
                analytics_bump('comments');
                notification_insert((int) $post['user_id'], (int) $me['id'], 'comment', $id,
                    '有人评论了你的帖子《' . post_display_title($post, 40) . '》:' . mb_substr($content, 0, 60),
                    page_url('post', ['id' => $id]) . '#comment-' . $commentId);
                notify_mentions($content, (int) $me['id'], $id, page_url('post', ['id' => $id]) . '#comment-' . $commentId);
            }
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'reply') {
        if (!$me) {
            flash('登录后才能回复评论', 'err');
        } elseif (feature_denied($me, 'comment')) {
            flash('「评论与回复」功能已被管理员关闭', 'err');
        } elseif (!$canComment) {
            flash('帖子已锁定,只有管理员和超级管理员可以回复评论', 'err');
        } elseif (user_is_muted($me) || user_is_site_muted($me)) {
            flash('你已被管理员禁言,暂时不能回复评论', 'err');
        } else {
            $cid = (int) input('cid');
            $content = trim((string) ($_POST['content'] ?? ''));
            $parent = DB::one('SELECT * FROM ' . DB::table('comments') . ' WHERE id = ? AND post_id = ?', [$cid, $id]);
            if (!$parent) {
                flash('被回复的评论不存在', 'err');
            } elseif ($content === '') {
                flash('回复内容不能为空', 'err');
            } elseif (mb_strlen($content) > 600) {
                flash('回复最多 600 字', 'err');
            } else {
                $rootId = (int) $parent['parent_id'] > 0 ? (int) $parent['parent_id'] : (int) $parent['id'];
                $adGuard = plugin_content_check('post_reply', $content);
                if ($adGuard) {
                    flash($adGuard['message'], 'err');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                $sens = sensitive_filter($content);
                if ($sens['action'] === 'block') {
                    flash('回复包含不允许发布的敏感词:' . $sens['hit'], 'err');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                $content = $sens['text'];
                $reviewHold = $sens['action'] === 'review';
                $replyId = DB::insert('comments', [
                    'post_id'    => $id,
                    'user_id'    => $me['id'],
                    'parent_id'  => $rootId,
                    'content'    => $content,
                    'status'    => $reviewHold ? 2 : 1,
                    'created_at' => now(),
                ]);
                if ($reviewHold) {
                    flash('回复已提交审核,审核通过后才会公开显示');
                    redirect(page_url('post', ['id' => $id]) . '#comments');
                }
                analytics_bump('comments');
                if ((int) $parent['user_id'] !== (int) $me['id']) {
                    notification_insert((int) $parent['user_id'], (int) $me['id'], 'reply', $id,
                        display_name($me) . ' 回复了你的评论:' . mb_substr($content, 0, 60),
                        page_url('post', ['id' => $id]) . '#reply-' . $replyId);
                }
                notify_mentions($content, (int) $me['id'], $id, page_url('post', ['id' => $id]) . '#reply-' . $replyId);
                flash('回复已发布');
            }
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'editcomment') {
        if (!$me) {
            flash('登录后才能编辑评论', 'err');
        } elseif (feature_denied($me, 'comment')) {
            flash('「评论与回复」功能已被管理员关闭', 'err');
        } else {
            $cid = (int) input('cid');
            $content = trim((string) ($_POST['content'] ?? ''));
            $comment = DB::one('SELECT * FROM ' . DB::table('comments') . ' WHERE id = ? AND post_id = ?', [$cid, $id]);
            if (!$comment) {
                flash('评论不存在', 'err');
            } elseif ((int) $comment['user_id'] !== (int) $me['id']) {
                flash('只能修改自己发表的评论', 'err');
            } elseif (!$canComment) {
                flash('帖子已锁定,请先解锁后再修改评论', 'err');
            } elseif ($content === '') {
                flash('评论内容不能为空', 'err');
            } elseif (mb_strlen($content) > 600) {
                flash('评论最多 600 字', 'err');
            } else {
                $adGuard = plugin_content_check('post_comment', $content, ['comment_id' => $cid]);
                if ($adGuard) {
                    flash($adGuard['message'], 'err');
                } else {
                    $sens = sensitive_filter($content);
                    if ($sens['action'] === 'block') {
                        flash('评论包含不允许发布的敏感词:' . $sens['hit'], 'err');
                    } else {
                        DB::update('comments', ['content' => $sens['text'], 'status' => $sens['action'] === 'review' ? 2 : 1], 'id = ? AND post_id = ?', [$cid, $id]);
                        flash($sens['action'] === 'review' ? '评论已提交审核,审核通过后才会公开显示' : '评论已修改');
                    }
                }
            }
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'delcomment' && $canManage) {
        if (!$canComment) {
            flash('帖子已锁定,请先解锁后再删除评论', 'err');
            redirect(page_url('post', ['id' => $id]) . '#comments');
        }
        $cid = (int) input('cid');
        $comment = DB::one('SELECT * FROM ' . DB::table('comments') . ' WHERE id = ? AND post_id = ?', [$cid, $id]);
        if (!$comment) {
            flash('评论不存在', 'err');
        } elseif (is_superadmin(user_by_id((int) $comment['user_id'])) && !is_superadmin(current_user())) {
            flash('超级管理员的评论不能由管理员删除', 'err');
        } else {
            $hasSuperReply = false;
            foreach (DB::all('SELECT user_id FROM ' . DB::table('comments') . ' WHERE parent_id = ? AND post_id = ?', [$cid, $id]) as $replyRow) {
                if (is_superadmin(user_by_id((int) $replyRow['user_id']))) { $hasSuperReply = true; break; }
            }
            if ($hasSuperReply && !is_superadmin(current_user())) {
                flash('这条评论下有超级管理员的回复,管理员不能删除', 'err');
                redirect(page_url('post', ['id' => $id]) . '#comments');
            }
            DB::delete('comments', 'parent_id = ? AND post_id = ?', [$cid, $id]);
            DB::delete('comments', 'id = ? AND post_id = ?', [$cid, $id]);
            flash('评论已删除');
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'delreply') {
        $cid = (int) input('cid');
        $reply = DB::one('SELECT * FROM ' . DB::table('comments') . ' WHERE id = ? AND post_id = ? AND parent_id <> 0', [$cid, $id]);
        $replyAuthorSuper = $reply ? is_superadmin(user_by_id((int) $reply['user_id'])) : false;
        $canDeleteReply = $reply && (($me && (int) $reply['user_id'] === (int) $me['id'])
            || ($canManage && (!$replyAuthorSuper || $actorSuper)));
        if (!$reply) {
            flash('回复不存在', 'err');
        } elseif (!$canComment) {
            flash('帖子已锁定,请先解锁后再删除回复', 'err');
        } elseif (!$canDeleteReply) {
            flash('没有权限删除这条回复', 'err');
        } else {
            DB::delete('comments', 'id = ? AND post_id = ?', [$cid, $id]);
            flash('回复已删除');
        }
        redirect(page_url('post', ['id' => $id]) . '#comments');
    }

    if ($act === 'report') {
        if (!$me) {
            flash('登录后才能举报', 'err');
        } elseif (feature_denied($me, 'report')) {
            flash('「举报」功能已被管理员关闭', 'err');
        } else {
            $reason = mb_substr(trim((string) ($_POST['reason'] ?? '')), 0, 200);
            $reportReason = $reason !== '' ? $reason : '未填写原因';
            DB::insert('reports', [
                'target_type' => 'post',
                'target_id'   => $id,
                'user_id'     => $me['id'],
                'reason'      => $reportReason,
                'status'      => 0,
                'created_at'  => now(),
            ]);
            notify_admins_report($me['id'], 'post', $id, $reportReason, post_display_title($post, 40));
            flash('举报已提交,管理员会尽快处理');
        }
        redirect(page_url('post', ['id' => $id]));
    }

    if ($act === 'delete') {
        if (!$canManage) {
            flash('你没有删除这篇帖子的权限', 'err');
            redirect(page_url('post', ['id' => $id]));
        }
        if (!$canModifyPost) {
            flash('帖子已锁定,请先解锁后再删除', 'err');
            redirect(page_url('post', ['id' => $id]));
        }
        post_delete($id);
        flash('帖子已删除');
        redirect(page_url('home'));
    }

    if ($act === 'admin' && is_admin(current_user())) {
        if ($authorSuper && !$actorSuper) {
            flash('超级管理员的帖子不能由管理员置顶、加精或删除', 'err');
            redirect(page_url('post', ['id' => $id]));
        }
        $op = input('op');
        if ($op === 'top') {
            $newTop = (int) $post['is_top'] === 1 ? 0 : 1;
            DB::run('UPDATE ' . DB::table('posts') . ' SET is_top = ?, top_at = ? WHERE id = ?', [$newTop, $newTop ? now() : '', $id]);
            flash('置顶状态已更新');
        } elseif ($op === 'feature') {
            DB::run('UPDATE ' . DB::table('posts') . ' SET is_featured = ? WHERE id = ?', [(int) $post['is_featured'] === 1 ? 0 : 1, $id]);
            flash('精华状态已更新');
        } elseif ($op === 'delete') {
            post_delete($id);
            flash('帖子已删除');
            redirect(page_url('home'));
        }
        redirect(page_url('post', ['id' => $id]));
    }
}

// 浏览量:登录用户每个账号每帖只计 1 次;游客同一会话只计 1 次
$viewCountedNow = false;
if ($granted) {
    if ($me) {
        $viewRow = DB::one('SELECT id, COALESCE(view_count, 1) AS view_count FROM ' . DB::table('post_views') . ' WHERE post_id = ? AND user_id = ?', [$id, $me['id']]);
        if (!$viewRow) {
            DB::insert('post_views', ['post_id' => $id, 'user_id' => $me['id'], 'created_at' => now(), 'last_viewed_at' => now(), 'view_count' => 1]);
            $viewCountedNow = true;
        } else {
            DB::run('UPDATE ' . DB::table('post_views') . ' SET view_count = ?, last_viewed_at = ? WHERE id = ?',
                [(int) $viewRow['view_count'] + 1, now(), (int) $viewRow['id']]);
        }
    } elseif (empty($_SESSION['viewed_posts'][$id])) {
        $_SESSION['viewed_posts'][$id] = 1;
        $viewCountedNow = true;
    }
    if ($viewCountedNow) {
        DB::run('UPDATE ' . DB::table('posts') . ' SET views = views + 1 WHERE id = ?', [$id]);
        $post['views'] = (int) $post['views'] + 1;
    }
}

// 阅读积分:仅登录用户、仅新增阅读记录时尝试,内部仍按每日上限控制
if ($granted && $me && $viewCountedNow && $_SERVER['REQUEST_METHOD'] === 'GET' && !$isOwner && (int) $post['status'] === 1) {
    reward_read_points($me['id'], $id);
}
$readerRows = DB::all("SELECT v.created_at AS first_viewed_at,
                              COALESCE(NULLIF(v.last_viewed_at, ''), v.created_at) AS last_viewed_at,
                              COALESCE(v.view_count, 1) AS view_count,
                              u.id AS reader_id, u.nickname, u.username, u.avatar, u.gender, u.verified, u.verify_style, u.verify_title, u.verify_type
                       FROM " . DB::table('post_views') . " v
                       LEFT JOIN " . DB::table('users') . " u ON u.id = v.user_id
                       WHERE v.post_id = ? ORDER BY last_viewed_at DESC LIMIT 80", [$id]);
$readerReadTotal = 0;
foreach ($readerRows as $readerRow) { $readerReadTotal += (int) ($readerRow['view_count'] ?? 1); }

$postIp = trim((string) ($post['ip'] ?? ''));
$originBypass = $isOwner || is_admin(current_user());
$showLocation = setting('post_show_location', '1') === '1' && $postIp !== ''
    && ($originBypass || profile_location_mode($post) > 0);
$showIp = setting('post_show_ip', '1') === '1' && $postIp !== ''
    && ($originBypass || (int) ($post['show_ip'] ?? 1) === 1);
$postLocation = $showLocation ? ip_location_text($postIp) : '';

$likeCount = DB::count('likes', 'post_id = ?', [$id]);
$favCount = DB::count('favorites', 'post_id = ?', [$id]);
$liked = $me ? (bool) DB::val('SELECT COUNT(*) FROM ' . DB::table('likes') . ' WHERE post_id = ? AND user_id = ?', [$id, $me['id']]) : false;
$faved = $me ? (bool) DB::val('SELECT COUNT(*) FROM ' . DB::table('favorites') . ' WHERE post_id = ? AND user_id = ?', [$id, $me['id']]) : false;
$attachVisible = $granted && post_content_visible($post, 'attach', $me);
$netdiskVisible = $granted && post_content_visible($post, 'netdisk', $me);
$attachments = $attachVisible ? DB::all('SELECT * FROM ' . DB::table('attachments') . ' WHERE post_id = ? ORDER BY id ASC', [$id]) : [];
$hiddenAttachCount = (!$attachVisible && $granted) ? DB::count('attachments', 'post_id = ?', [$id]) : 0;
$canSaveMedia = $me && $granted && ($isOwner || post_privileged_can_view($post, $me) || (int) ($post['allow_save_media'] ?? 1) === 1);
$postContent = media_rewrite_html($post['content']);
if ($canSaveMedia && (int) $post['is_html'] === 1) {
    $postContent = post_media_save_html($postContent, $id, true);
}
$blockedIds = $me ? blocked_user_ids((int) $me['id']) : [];
$comments = $granted && (int) $post['status'] === 1
    ? DB::all('SELECT c.*, u.nickname, u.username, u.avatar, u.gender, u.verified, u.verify_style, u.verify_title, u.verify_type, u.role, u.comment_bubble FROM ' . DB::table('comments') . ' c
               LEFT JOIN ' . DB::table('users') . ' u ON u.id = c.user_id
               WHERE c.post_id = ? AND c.parent_id = 0 AND c.status = 1 ORDER BY c.id ASC LIMIT 300', [$id])
    : [];
$commentReplies = $granted && (int) $post['status'] === 1
    ? DB::all('SELECT c.*, u.nickname, u.username, u.avatar, u.gender, u.verified, u.verify_style, u.verify_title, u.verify_type, u.role, u.comment_bubble,
                      pu.nickname AS parent_nickname
               FROM ' . DB::table('comments') . ' c
               LEFT JOIN ' . DB::table('users') . ' u ON u.id = c.user_id
               LEFT JOIN ' . DB::table('comments') . ' p ON p.id = c.parent_id
               LEFT JOIN ' . DB::table('users') . ' pu ON pu.id = p.user_id
               WHERE c.post_id = ? AND c.parent_id <> 0 AND c.status = 1 ORDER BY c.id ASC LIMIT 500', [$id])
    : [];
$repliesByParent = [];
foreach ($commentReplies as $reply) {
    $repliesByParent[(int) $reply['parent_id']][] = $reply;
}
$commentTotal = count($comments) + count($commentReplies);

$postDisplayTitle = post_display_title($post, 40);
$GLOBALS['PAGE'] = 'post';
render_header($postDisplayTitle);
?>
<a id="top"></a>

<?php if ($authorOnlyBlocked): ?>
<section class="lock-gate">
  <div class="lock-avatar"><?= avatar_html($post) ?></div>
  <div class="lock-info">
    <a class="author" href="<?= e(page_url('user', ['id' => $post['user_id']])) ?>"><?= e(display_name($post)) ?></a>
    <span class="time"><?= e($post['created_at']) ?></span>
  </div>
  <div class="lock-icon">🙈</div>
  <h2>此帖仅作者本人可见</h2>
  <p>发布者把这篇帖子设为仅自己可见,正文、附件和网盘信息不会向普通读者展示。</p>
  <?php if ($me): ?>
  <p class="lock-tip">如需查看,可以给作者发消息:<a href="<?= e(page_url('friends', ['to' => $post['user_id']])) ?>">联系 <?= e(display_name($post)) ?></a></p>
  <?php else: ?>
  <p class="lock-tip"><a href="<?= e(page_url('login')) ?>">登录</a>后也不会显示,只有作者本人和有权限的管理员可以查看。</p>
  <?php endif; ?>
</section>
<?php render_footer(); return; endif; ?>

<?php if (!$granted): ?>
<section class="lock-gate">
  <div class="lock-avatar"><?= avatar_html($post) ?></div>
  <div class="lock-info">
    <a class="author" href="<?= e(page_url('user', ['id' => $post['user_id']])) ?>"><?= e(display_name($post)) ?></a>
    <span class="time"><?= e($post['created_at']) ?></span>
  </div>
  <div class="lock-icon">❄</div>
  <h2>此帖设有访问密码</h2>
  <p>输入正确密码后,才能读取帖子内容。未输入或密码错误时,只会显示作者与发布时间。</p>
  <form method="post" class="lock-form">
    <?= csrf_field() ?>
    <input type="hidden" name="act" value="unlock">
    <input type="password" name="password" placeholder="请输入访问密码" autocomplete="off" required>
    <button class="btn-primary" type="submit">解锁阅读</button>
  </form>
  <?php if ($canBypassPassword): ?>
  <form method="post" class="lock-form lock-bypass-form">
    <?= csrf_field() ?>
    <input type="hidden" name="act" value="bypass">
    <button class="btn-ghost" type="submit">免密码查看</button>
  </form>
  <p class="lock-tip">管理员免密入口 · <?= $actorSuper ? '普通用户和管理员的密码帖可免密查看,超级管理员的密码帖仍需输入密码' : '仅普通用户的密码帖可免密查看,其他管理员和超级管理员的密码帖仍需输入密码' ?></p>
  <?php endif; ?>
  <?php if ($me): ?>
  <p class="lock-tip">忘记密码?可以给作者发消息:<a href="<?= e(page_url('friends', ['to' => $post['user_id']])) ?>">联系 <?= e(display_name($post)) ?></a></p>
  <?php endif; ?>
</section>
<?php render_footer(); return; endif; ?>

<article class="post-detail">
  <nav class="crumbs">
    <a href="<?= e(page_url('home')) ?>">首页</a>
    <?php if ($post['cat_name']): ?><span>/</span><a href="<?= e(page_url('home', ['cat' => $post['cat_id']])) ?>"><?= e($post['cat_name']) ?></a><?php endif; ?>
  </nav>

  <header class="post-head">
    <div class="post-tags">
      <?php if ((int) $post['is_top'] === 1): ?><span class="tag top">置顶</span><?php endif; ?>
      <?php if ((int) $post['is_featured'] === 1): ?><span class="tag feat">精华</span><?php endif; ?>
      <?php if ($hasPassword): ?><span class="tag lock">🔒 密码帖</span><?php endif; ?>
      <?php if ($authorOnly): ?><span class="tag lock">🙈 仅作者可见</span><?php endif; ?>
      <?php if ((int) $post['status'] !== 1): ?><span class="tag lock"><?= e(post_status_text($post['status'])) ?></span><?php endif; ?>
      <?php if ($isLocked): ?><span class="tag locked">🔒 已锁定</span><?php endif; ?>
      <?php if ($post['cat_name']): ?><span class="tag cat"><?= e($post['cat_name']) ?></span><?php endif; ?>
    </div>
    <?php if (post_has_title($post)): ?><h1><?= e($post['title']) ?></h1><?php endif; ?>
    <?php $postTags = tags_of_post($post); ?>
    <?php if ($postTags): ?><div class="post-tags"><?php foreach ($postTags as $t): ?><a class="tag" href="<?= e(tag_url($t)) ?>">#<?= e($t) ?></a><?php endforeach; ?></div><?php endif; ?>
    <div class="post-byline">
      <a class="byline-avatar-link" href="<?= e(page_url('user', ['id' => $post['user_id']])) ?>" aria-label="进入<?= e(display_name($post)) ?>的个人主页">
        <?= avatar_html($post, 'byline-avatar') ?>
      </a>
      <div>
        <a class="author" href="<?= e(page_url('user', ['id' => $post['user_id']])) ?>">
          <?= e(display_name($post)) ?>
          <?php echo verify_badge($post); ?>
        </a>
        <div class="byline-sub">
          <span><?= e(gender_text($post['gender'])) ?></span>
          <span class="dot">·</span>
          <span><?= e($post['created_at']) ?></span>
          <span class="dot">·</span>
          <button class="read-link" type="button" id="readersOpen">阅读 <b><?= (int) $post['views'] ?></b></button>
        </div>
      </div>
      <?php if ($canManage): ?>
      <div class="post-manage">
        <?php if ($canModifyPost && ($isOwner || is_admin(current_user()))): ?>
        <a class="btn-mini" href="<?= e(page_url('compose', ['id' => $id])) ?>">编辑</a>
        <?php endif; ?>
        <form method="post" class="inline-form">
          <?= csrf_field() ?><input type="hidden" name="act" value="lock">
          <button class="btn-mini <?= $isLocked ? 'lock-on' : 'ghost' ?>" type="submit"><?= $isLocked ? '🔓 解锁' : '🔒 锁定' ?></button>
        </form>
        <?php if (is_admin(current_user()) && (!$authorSuper || $actorSuper)): ?>
        <form method="post" class="inline-form">
          <?= csrf_field() ?><input type="hidden" name="act" value="admin"><input type="hidden" name="op" value="top">
          <button class="btn-mini ghost" type="submit"><?= (int) $post['is_top'] === 1 ? '取消置顶' : '置顶' ?></button>
        </form>
        <form method="post" class="inline-form">
          <?= csrf_field() ?><input type="hidden" name="act" value="admin"><input type="hidden" name="op" value="feature">
          <button class="btn-mini ghost" type="submit"><?= (int) $post['is_featured'] === 1 ? '取消精华' : '加精' ?></button>
        </form>
        <?php endif; ?>
        <?php if ($canModifyPost): ?>
        <form method="post" class="inline-form" data-confirm="确定删除这篇帖子?">
          <?= csrf_field() ?><input type="hidden" name="act" value="delete">
          <button class="btn-mini danger" type="submit">删除</button>
        </form>
        <?php endif; ?>
      </div>
      <?php endif; ?>
    </div>
    <?php if ($showLocation): ?>
      <div class="post-origin">
        <span class="po-loc">📍 IP 属地:<?= e($postLocation) ?></span>
        <?php if ($showIp): ?><span class="po-ip">IP:<?= e(mask_ip($postIp)) ?></span><?php endif; ?>
      </div>
    <?php elseif ($showIp): ?>
      <div class="post-origin"><span class="po-ip">IP:<?= e(mask_ip($postIp)) ?></span></div>
    <?php endif; ?>
  </header>

  <?php if ((int) $post['status'] === 2 || (int) $post['status'] === 3): ?>
    <section class="review-banner <?= (int) $post['status'] === 2 ? 'pending' : 'rejected' ?>" id="reviewBox">
      <div class="review-banner-head">
        <strong><?= (int) $post['status'] === 2 ? '这篇帖子正在审核中' : '这篇帖子审核未通过' ?></strong>
        <span><?= (int) $post['status'] === 2 ? '审核通过后才会在论坛公开展示' : '仅作者与管理员可见' ?></span>
      </div>
      <?php if ((int) $post['status'] === 3 && trim((string) ($post['review_note'] ?? '')) !== ''): ?>
        <p class="review-reason">驳回理由:<?= e($post['review_note']) ?></p>
      <?php endif; ?>
      <?php if (is_admin($me) && (int) $post['status'] === 2): ?>
        <div class="review-actions">
          <form method="post" class="inline-form">
            <?= csrf_field() ?><input type="hidden" name="act" value="review_approve">
            <button class="btn-primary review-action-btn" type="submit">审核通过</button>
          </form>
          <details class="review-reject-box">
            <summary class="btn-mini danger review-action-btn">审核失败</summary>
            <form method="post">
              <?= csrf_field() ?><input type="hidden" name="act" value="review_reject">
              <textarea name="reason" maxlength="200" required placeholder="填写驳回理由,发布者会在通知中心看到"></textarea>
              <button class="btn-mini danger review-action-btn" type="submit">确认驳回</button>
            </form>
          </details>
        </div>
      <?php endif; ?>
    </section>
  <?php endif; ?>

  <div class="post-content rich-content">
    <?php if ((int) $post['is_html'] === 1): ?>
      <?= $postContent ?>
    <?php else: ?>
      <?= nl2br(e($post['content'])) ?>
    <?php endif; ?>
  </div>

  <?php if ($post['netdisk_link']): ?>
    <?php if ($netdiskVisible): ?>
    <div class="netdisk-card" id="netdisk">
      <div class="nd-icon">☁</div>
      <div class="nd-body">
        <strong>网盘文件:<?= e($post['netdisk_name'] !== '' ? $post['netdisk_name'] : '本帖附带网盘链接') ?></strong>
        <span><?= e($post['netdisk_link']) ?><?php if (($post['netdisk_size'] ?? '') !== ''): ?> · <?= e($post['netdisk_size']) ?><?php endif; ?></span>
      </div>
      <div class="nd-actions">
        <button class="btn-mini" type="button" onclick="KM.copy('<?= e($post['netdisk_link']) ?>')">复制链接</button>
        <?php if (($post['netdisk_code'] ?? '') !== ''): ?>
          <button class="btn-mini ghost nd-code-btn" type="button" data-copy="<?= e($post['netdisk_code']) ?>" title="点击复制提取码">提取码:<?= e($post['netdisk_code']) ?></button>
        <?php endif; ?>
        <a class="btn-mini ghost" href="<?= e($post['netdisk_link']) ?>" target="_blank" rel="noopener">打开</a>
      </div>
    </div>
    <?php elseif ($granted): ?>
      <?php $ndRule = post_content_rule($post, 'netdisk'); ?>
      <div class="content-gate" id="netdisk">
        <div class="cg-icon">☁</div>
        <div class="cg-main">
          <strong><?= $ndRule['visibility'] === 'reply' ? '网盘信息:回复本帖后可见' : '网盘信息:支付 ' . (int) $ndRule['points'] . ' 积分后可见' ?></strong>
          <p><?= $ndRule['visibility'] === 'reply' ? '在本帖留下评论或回复后,刷新页面即可看到网盘名称、链接与提取码。' : '解锁后永久可见,支付的积分会进入发布者账户。' ?></p>
          <?php if (!$me): ?>
            <a class="btn-mini" href="<?= e(page_url('login')) ?>">登录后查看</a>
          <?php elseif ($ndRule['visibility'] === 'reply'): ?>
            <a class="btn-mini" href="#comments">去回复本帖</a>
          <?php else: ?>
            <form method="post" class="inline-form">
              <?= csrf_field() ?><input type="hidden" name="act" value="unlock_content"><input type="hidden" name="kind" value="netdisk">
              <button class="btn-primary btn-mini" type="submit">支付 <?= (int) $ndRule['points'] ?> 积分解锁</button>
            </form>
            <small>当前积分:<?= $me ? e(points_text($me)) : '未登录' ?></small>
          <?php endif; ?>
        </div>
      </div>
    <?php endif; ?>
  <?php endif; ?>

  <?php if ($hiddenAttachCount && !$attachVisible && $granted): ?>
    <?php $attachRule = post_content_rule($post, 'attach'); ?>
    <div class="content-gate" id="attachments">
      <div class="cg-icon">📎</div>
      <div class="cg-main">
        <strong><?= $attachRule['visibility'] === 'reply' ? '附件:回复本帖后可见' : '附件:支付 ' . (int) $attachRule['points'] . ' 积分后可见' ?></strong>
        <p><?= $attachRule['visibility'] === 'reply' ? '在本帖留下评论或回复后,刷新页面即可看到附件列表并下载。' : '解锁后永久可见,支付的积分会进入发布者账户。' ?></p>
        <?php if (!$me): ?>
          <a class="btn-mini" href="<?= e(page_url('login')) ?>">登录后查看</a>
        <?php elseif ($attachRule['visibility'] === 'reply'): ?>
          <a class="btn-mini" href="#comments">去回复本帖</a>
        <?php else: ?>
          <form method="post" class="inline-form">
            <?= csrf_field() ?><input type="hidden" name="act" value="unlock_content"><input type="hidden" name="kind" value="attach">
            <button class="btn-primary btn-mini" type="submit">支付 <?= (int) $attachRule['points'] ?> 积分解锁</button>
          </form>
          <small>当前积分:<?= $me ? e(points_text($me)) : '未登录' ?></small>
        <?php endif; ?>
      </div>
    </div>
  <?php endif; ?>

  <?php if ($attachments): ?>
  <div class="attachments" id="attachments">
    <h3>附件 · <?= count($attachments) ?></h3>
    <?php foreach ($attachments as $a): ?>
      <?php
        $attKind = media_kind_by_name($a['name']);
        $attMediaUrl = base_url('api.php') . '?action=attachment_media&id=' . (int) $a['id'];
        $attAudioMeta = $attKind === 'audio' ? audio_track_meta_stored($a) : null;
        $canMatchAudio = $me && ((int) ($a['user_id'] ?? 0) === (int) $me['id'] || is_superadmin($me));
      ?>
      <div class="att-item">
        <?php if ($attKind === 'video'): ?>
          <div class="att-media"><video controls playsinline preload="metadata" src="<?= e($attMediaUrl) ?>"></video></div>
        <?php elseif ($attKind === 'audio'): ?>
          <div class="att-media">
            <audio data-km-audio="1" data-km-card-style="panel" data-audio-title="<?= e($attAudioMeta['title']) ?>" data-audio-artist="<?= e($attAudioMeta['artist']) ?>"
              data-audio-cover="<?= e($attAudioMeta['cover']) ?>" data-audio-caption="<?= e($a['name']) ?>" data-audio-lyrics-b64="<?= e(base64_encode((string) ($attAudioMeta['lyrics'] ?? ''))) ?>" data-audio-download="<?= e(base_url('api.php')) ?>?action=attachment&amp;id=<?= (int) $a['id'] ?>"
              <?php if ($canSaveMedia): ?>data-km-save-post="1" data-km-save-att="<?= (int) $a['id'] ?>" data-post-id="<?= $id ?>"<?php endif; ?>
              controls playsinline preload="metadata" src="<?= e($attMediaUrl) ?>"></audio>
          </div>
          <?php if ($canMatchAudio): ?>
          <div class="audio-match-tools">
            <button class="btn-mini" type="button" data-audio-match-auto data-target="attachment" data-id="<?= (int) $a['id'] ?>">一键匹配歌词封面</button>
            <button class="btn-mini ghost" type="button" data-audio-match-open data-target="attachment" data-id="<?= (int) $a['id'] ?>" data-title="<?= e($attAudioMeta['title']) ?>" data-artist="<?= e($attAudioMeta['artist']) ?>">手动搜索</button>
          </div>
          <?php endif; ?>
        <?php endif; ?>
        <span class="att-name"><?= e($a['name']) ?></span>
        <span class="att-size"><?= format_size($a['size']) ?></span>
        <span class="att-time">上传于 <?= e(exact_time($a['created_at'] ?? '')) ?></span>
        <a class="btn-mini" href="<?= e(base_url('api.php')) ?>?action=attachment&amp;id=<?= (int) $a['id'] ?>">下载</a>
      </div>
    <?php endforeach; ?>
  </div>
  <?php endif; ?>

  <?php if ((int) $post['status'] === 1): ?>
  <div class="post-actions">
    <form method="post" class="inline-form">
      <?= csrf_field() ?><input type="hidden" name="act" value="like">
      <button class="act-btn <?= $liked ? 'on' : '' ?>" type="submit">♡ 点赞 <b><?= $likeCount ?></b></button>
    </form>
    <form method="post" class="inline-form">
      <?= csrf_field() ?><input type="hidden" name="act" value="fav">
      <button class="act-btn <?= $faved ? 'on' : '' ?>" type="submit">☆ 收藏 <b><?= $favCount ?></b></button>
    </form>
    <button class="act-btn" type="button" onclick="KM.copy(location.href)">分享</button>
    <button class="act-btn" type="button" onclick="KM.openReport(<?= $id ?>)">举报</button>
  </div>
  <?php endif; ?>
</article>

  <div class="readers-modal" id="readersModal" hidden>
    <div class="readers-card">
      <div class="readers-head">
        <h3>阅读记录 <small>登录用户 <?= count($readerRows) ?> 人 · 共 <?= $readerReadTotal ?> 次</small></h3>
        <button class="readers-close" type="button" id="readersClose" aria-label="关闭">×</button>
      </div>
      <?php if (!$readerRows): ?>
        <p class="readers-empty">还没有登录用户阅读记录,未登录访客不会显示在这里。</p>
      <?php else: ?>
        <div class="readers-list">
          <?php foreach ($readerRows as $rv): ?>
            <a class="readers-row" href="<?= e(page_url('user', ['id' => $rv['reader_id'] ?? 0])) ?>">
              <?= avatar_html($rv) ?>
              <div class="readers-main">
                <strong><?= e(display_name($rv)) ?><?= verify_badge($rv) ?></strong>
                <span class="readers-times">阅读 <?= (int) ($rv['view_count'] ?? 1) ?> 次</span>
                <span class="readers-stamp">首次阅读:<?= e($rv['first_viewed_at']) ?></span>
                <span class="readers-stamp">最新阅读:<?= e($rv['last_viewed_at']) ?></span>
              </div>
            </a>
          <?php endforeach; ?>
        </div>
      <?php endif; ?>
    </div>
  </div>


<?php if ((int) $post['status'] === 1): ?>
<section class="comments" id="comments">
  <h3 class="section-title">评论 <small><?= $commentTotal ?></small></h3>
  <?php if (!$comments): ?>
    <?php empty_state('还没有评论', '做第一个留下茶话的人'); ?>
  <?php else: ?>
    <?php foreach ($comments as $c): ?>
      <?php if (in_array((int) $c['user_id'], $blockedIds, true)) { continue; } ?>
      <div class="comment" id="comment-<?= (int) $c['id'] ?>">
        <?= avatar_html($c, 'c-avatar') ?>
        <div class="c-body">
          <div class="c-head">
            <a class="author" href="<?= e(page_url('user', ['id' => $c['user_id']])) ?>"><?= e(display_name($c)) ?></a><?= verify_badge($c) ?>
            <span class="time"><?= e(time_ago($c['created_at'])) ?></span>
            <?php if ($me && (int) $c['user_id'] === (int) $me['id'] && $canComment): ?>
            <button type="button" class="link-btn c-edit-btn" data-edit-comment="<?= (int) $c['id'] ?>">编辑</button>
            <?php endif; ?>
            <?php if ($me && !user_is_muted($me) && !user_is_site_muted($me) && $canComment): ?>
            <button type="button" class="link-btn c-reply-btn" data-reply-comment="<?= (int) $c['id'] ?>">回复</button>
            <?php endif; ?>
            <?php if ($canManage && $canComment && (!is_superadmin($c) || is_superadmin(current_user()))): ?>
            <form method="post" class="inline-form c-del" data-confirm="删除这条评论?">
              <?= csrf_field() ?>
              <input type="hidden" name="act" value="delcomment">
              <input type="hidden" name="cid" value="<?= (int) $c['id'] ?>">
              <button type="submit" class="link-btn">删除</button>
            </form>
            <?php endif; ?>
          </div>
          <p class="c-text comment-bubble bubble-<?= e(comment_bubble_of($c)) ?>"><?= nl2br(e($c['content'])) ?></p>
          <?php if ($me && (int) $c['user_id'] === (int) $me['id'] && $canComment): ?>
          <form method="post" class="comment-edit-form" id="comment-edit-<?= (int) $c['id'] ?>" hidden>
            <?= csrf_field() ?>
            <input type="hidden" name="act" value="editcomment">
            <input type="hidden" name="cid" value="<?= (int) $c['id'] ?>">
            <textarea name="content" rows="3" maxlength="600" required><?= e($c['content']) ?></textarea>
            <div class="comment-edit-actions">
              <button class="btn-mini" type="submit">保存修改</button>
              <button class="btn-mini ghost" type="button" data-cancel-edit>取消</button>
            </div>
          </form>
          <?php endif; ?>
          <?php if ($me && !user_is_muted($me) && !user_is_site_muted($me) && $canComment): ?>
          <form method="post" class="comment-reply-form" id="comment-reply-<?= (int) $c['id'] ?>" hidden>
            <?= csrf_field() ?>
            <input type="hidden" name="act" value="reply">
            <input type="hidden" name="cid" value="<?= (int) $c['id'] ?>">
            <textarea name="content" rows="2" maxlength="600" placeholder="回复 @<?= e(display_name($c)) ?>…" required></textarea>
            <div class="comment-edit-actions">
              <button class="btn-mini" type="submit">发布回复</button>
              <button class="btn-mini ghost" type="button" data-cancel-reply>取消</button>
            </div>
          </form>
          <?php endif; ?>
          <?php if (!empty($repliesByParent[(int) $c['id']])): ?>
          <div class="comment-replies">
            <?php foreach ($repliesByParent[(int) $c['id']] as $r): ?>
              <?php if (in_array((int) $r['user_id'], $blockedIds, true)) { continue; } ?>
              <?php
                $replyAuthorSuper = is_superadmin(user_by_id((int) $r['user_id']));
                $canDeleteReplyView = $me && $canComment && ((int) $r['user_id'] === (int) $me['id']
                    || ($canManage && (!$replyAuthorSuper || $actorSuper)));
              ?>
              <div class="comment-reply" id="reply-<?= (int) $r['id'] ?>">
                <?= avatar_html($r, 'c-avatar c-avatar-sm') ?>
                <div class="c-body">
                  <div class="c-head">
                    <a class="author" href="<?= e(page_url('user', ['id' => $r['user_id']])) ?>"><?= e(display_name($r)) ?></a><?= verify_badge($r) ?>
                    <span class="reply-to">回复 @<?= e($r['parent_nickname'] ?: '原评论') ?></span>
                    <span class="time"><?= e(time_ago($r['created_at'])) ?></span>
                    <?php if ($me && (int) $r['user_id'] === (int) $me['id'] && $canComment): ?>
                    <button type="button" class="link-btn" data-edit-comment="<?= (int) $r['id'] ?>">编辑</button>
                    <?php endif; ?>
                    <?php if ($me && !user_is_muted($me) && !user_is_site_muted($me) && $canComment): ?>
                    <button type="button" class="link-btn" data-reply-comment="<?= (int) $r['id'] ?>">回复</button>
                    <?php endif; ?>
                    <?php if ($canDeleteReplyView): ?>
                    <form method="post" class="inline-form c-del" data-confirm="删除这条回复?">
                      <?= csrf_field() ?>
                      <input type="hidden" name="act" value="delreply">
                      <input type="hidden" name="cid" value="<?= (int) $r['id'] ?>">
                      <button type="submit" class="link-btn">删除</button>
                    </form>
                    <?php endif; ?>
                  </div>
                  <p class="c-text comment-bubble bubble-<?= e(comment_bubble_of($r)) ?>"><?= nl2br(e($r['content'])) ?></p>
                  <?php if ($me && (int) $r['user_id'] === (int) $me['id'] && $canComment): ?>
                  <form method="post" class="comment-edit-form" id="comment-edit-<?= (int) $r['id'] ?>" hidden>
                    <?= csrf_field() ?>
                    <input type="hidden" name="act" value="editcomment">
                    <input type="hidden" name="cid" value="<?= (int) $r['id'] ?>">
                    <textarea name="content" rows="3" maxlength="600" required><?= e($r['content']) ?></textarea>
                    <div class="comment-edit-actions">
                      <button class="btn-mini" type="submit">保存修改</button>
                      <button class="btn-mini ghost" type="button" data-cancel-edit>取消</button>
                    </div>
                  </form>
                  <?php endif; ?>
                  <?php if ($me && !user_is_muted($me) && !user_is_site_muted($me) && $canComment): ?>
                  <form method="post" class="comment-reply-form" id="comment-reply-<?= (int) $r['id'] ?>" hidden>
                    <?= csrf_field() ?>
                    <input type="hidden" name="act" value="reply">
                    <input type="hidden" name="cid" value="<?= (int) $r['id'] ?>">
                    <textarea name="content" rows="2" maxlength="600" placeholder="回复 @<?= e(display_name($r)) ?>…" required></textarea>
                    <div class="comment-edit-actions">
                      <button class="btn-mini" type="submit">发布回复</button>
                      <button class="btn-mini ghost" type="button" data-cancel-reply>取消</button>
                    </div>
                  </form>
                  <?php endif; ?>
                </div>
              </div>
            <?php endforeach; ?>
          </div>
          <?php endif; ?>
        </div>
      </div>
    <?php endforeach; ?>
  <?php endif; ?>

  <?php if ($isLocked && !$canComment): ?>
  <div class="comment-login locked-comment-note">🔒 帖子已锁定,只有管理员和超级管理员可以评论</div>
  <?php elseif ($me): ?>
  <form method="post" class="comment-form">
    <?= csrf_field() ?>
    <input type="hidden" name="act" value="comment">
    <textarea name="content" rows="3" maxlength="600" placeholder="留下你的茶话…(最多 600 字)" required></textarea>
    <div class="cf-foot">
      <small>评论 +2 积分</small>
      <button class="btn-primary" type="submit">发表评论</button>
    </div>
  </form>
  <?php else: ?>
  <div class="comment-login">
    <a href="<?= e(page_url('login', ['next' => page_url('post', ['id' => $id])])) ?>">登录</a> 后即可参与评论
  </div>
  <?php endif; ?>
</section>
<?php endif; ?>

<?php require_once APP_ROOT . '/inc/audio_match_ui.php'; render_audio_match_ui(); ?>

<div class="modal" id="reportModal" hidden>
  <div class="modal-mask" data-close></div>
  <div class="modal-card">
    <h3>举报这篇帖子</h3>
    <form method="post">
      <?= csrf_field() ?>
      <input type="hidden" name="act" value="report">
      <input type="hidden" name="target" value="<?= $id ?>">
      <textarea name="reason" rows="3" maxlength="200" placeholder="请简述举报原因,例如广告、侵权、违规内容" required></textarea>
      <div class="modal-actions">
        <button type="button" class="btn-mini ghost" data-close>取消</button>
        <button type="submit" class="btn-mini danger">提交举报</button>
      </div>
    </form>
  </div>
</div>

<?php render_footer(); ?>
主题