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/world.php 55.7 KB · 发布包 v6.25.231
下载
<?php
/**
 * 世界圈:像朋友圈一样发布文字 / 图片 / 视频 / 语音,支持点赞与评论、编辑
 */
require_login();
feature_guard('world');
$me = current_user();

// 个人主页进入时只看这个人的世界圈
$worldFilterUid = (int) input('user');
$worldFilterUser = $worldFilterUid > 0 ? user_by_id($worldFilterUid) : null;
$worldUserParam = $worldFilterUser ? ['user' => (int) $worldFilterUser['id']] : [];
function world_url(array $extra = [])
{
    global $worldUserParam;
    return page_url('world', array_merge($worldUserParam, $extra));
}

function world_store_media($field = 'media')
{
    if (!isset($_FILES[$field]) || !is_array($_FILES[$field]) || (int) $_FILES[$field]['error'] === UPLOAD_ERR_NO_FILE) {
        return [null, ''];
    }
    if ((int) $_FILES[$field]['error'] !== UPLOAD_ERR_OK) {
        throw new Exception('文件上传失败,错误码 ' . (int) $_FILES[$field]['error']);
    }
    $finfo = new finfo(FILEINFO_MIME_TYPE);
    $mime = (string) $finfo->file($_FILES[$field]['tmp_name']);
    $ext = strtolower(pathinfo((string) $_FILES[$field]['name'], PATHINFO_EXTENSION));
    $kind = media_kind_by_ext($ext);
    if ($kind === '') {
        if (strpos($mime, 'image/') === 0) { $kind = 'image'; }
        elseif (strpos($mime, 'video/') === 0) { $kind = 'video'; }
        elseif (strpos($mime, 'audio/') === 0) { $kind = 'audio'; }
    } elseif ($kind === 'video' && strpos($mime, 'audio/') === 0) {
        $kind = 'audio';
    }
    if ($kind === 'image') {
        $media = upload_store($field, 'world', ['allow' => image_upload_extensions(), 'max' => upload_limit_bytes('upload_world_mb', 1000), 'max_label' => '世界圈文件', 'image' => true]);
        $view = image_make_compatible($media['path']);
        if ($view && $view !== $media['path']) {
            $media['path'] = $view;
            $media['name'] = preg_replace('/\.\w+$/', '.jpg', (string) $media['name']);
        }
        return ['image', $media];
    }
    if ($kind === 'video') {
        $media = upload_store($field, 'world', ['allow' => chat_video_extensions(), 'mime_media' => true, 'max' => upload_limit_bytes('upload_world_mb', 1000), 'max_label' => '世界圈文件']);
        $compatible = video_make_compatible($media['path']) ?: $media['path'];
        if ($compatible !== $media['path']) {
            $media['path'] = $compatible;
            $media['name'] = preg_replace('/\.\w+$/', '.' . pathinfo($compatible, PATHINFO_EXTENSION), (string) $media['name']);
        }
        return ['video', $media];
    }
    if ($kind === 'audio') {
        $media = upload_store($field, 'world', ['allow' => chat_audio_extensions(), 'mime_audio' => true, 'max' => upload_limit_bytes('upload_world_mb', 1000), 'max_label' => '世界圈文件']);
        $compatible = audio_make_compatible($media['path']);
        if ($compatible) {
            if ($compatible !== $media['path']) {
                $media['path'] = $compatible;
                $media['name'] = preg_replace('/\.\w+$/', '.' . pathinfo($compatible, PATHINFO_EXTENSION), (string) $media['name']);
            }
        }
        return ['voice', $media];
    }
    throw new Exception('只支持图片、视频或音乐文件');
}


// 世界圈草稿:发布框使用 kind=world;编辑已有动态使用 kind=world_edit_<mid>
$worldDraft = null;
$worldDraftData = null;
$showWorldDraftPrompt = false;
$worldEditDrafts = [];
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    if (input('discard') === '1') {
        $d = DB::one('SELECT * FROM ' . DB::table('drafts') . " WHERE user_id = ? AND kind = 'world' ORDER BY updated_at DESC, id DESC LIMIT 1", [(int) $me['id']]);
        if ($d) {
            if (!empty($d['media_path'])) { delete_upload($d['media_path']); }
            draft_clear((int) $me['id'], 'world');
        }
        flash('已放弃上次未完成的世界圈动态');
        redirect(world_url());
    }
    $worldDraft = DB::one('SELECT * FROM ' . DB::table('drafts') . " WHERE user_id = ? AND kind = 'world' ORDER BY updated_at DESC, id DESC LIMIT 1", [(int) $me['id']]);
    if ($worldDraft && input('resume') === '1') { $worldDraftData = $worldDraft; }
    $showWorldDraftPrompt = (bool) ($worldDraft && input('resume') !== '1');
    $editDraftRows = DB::all('SELECT * FROM ' . DB::table('drafts') . " WHERE user_id = ? AND kind LIKE 'world_edit_%' ORDER BY updated_at DESC", [(int) $me['id']]);
    foreach ($editDraftRows as $ed) { $worldEditDrafts[(int) substr($ed['kind'], 11)] = $ed; }
}

/** 校验世界圈草稿媒体:必须存在于当前用户对应草稿记录中 */
function world_draft_media_for($userId, $kind, $path)
{
    $path = trim((string) $path);
    if ($path === '' || strpos($path, 'uploads/world/') !== 0 || strpos($path, '..') !== false) { return null; }
    $full = realpath(APP_ROOT . '/' . $path);
    $root = realpath(APP_ROOT . '/uploads/world');
    if (!$full || !$root || strpos($full, $root) !== 0 || !is_file($full)) { return null; }
    $row = DB::one('SELECT * FROM ' . DB::table('drafts') . ' WHERE user_id = ? AND kind = ? AND media_path = ? LIMIT 1', [(int) $userId, (string) $kind, $path]);
    return $row ?: null;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    check_csrf();
    $act = input('act');

    if ($act === 'world_draft' || $act === 'world_edit_draft') {
        header('Content-Type: application/json');
        $isEdit = $act === 'world_edit_draft';
        $mid = (int) input('mid');
        $kind = 'world';
        if ($isEdit) {
            $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND user_id = ?', [$mid, (int) $me['id']]);
            if (!$moment) { echo json_encode(['ok' => false, 'msg' => '没有权限保存这条动态的草稿']); exit; }
            $kind = 'world_edit_' . $mid;
        }
        $content = mb_substr(trim((string) input('content')), 0, 1000);
        $mediaPath = trim((string) input('draft_media_path'));
        $mediaType = trim((string) input('draft_media_type'));
        $mediaName = trim((string) input('draft_media_name'));
        $oldDraft = DB::one('SELECT * FROM ' . DB::table('drafts') . ' WHERE user_id = ? AND kind = ? ORDER BY updated_at DESC, id DESC LIMIT 1', [(int) $me['id'], $kind]);
        if ($mediaPath !== '' && !world_draft_media_for((int) $me['id'], $kind, $mediaPath)) {
            $mediaPath = ''; $mediaType = ''; $mediaName = '';
        }
        if ($oldDraft && !empty($oldDraft['media_path']) && $oldDraft['media_path'] !== $mediaPath) {
            delete_upload($oldDraft['media_path']);
        }
        if ($content === '' && $mediaPath === '') {
            draft_clear((int) $me['id'], $kind);
            echo json_encode(['ok' => true, 'empty' => true, 'saved_at' => date('H:i:s')]);
            exit;
        }
        draft_save((int) $me['id'], '', $content, 0, $kind, [
            'media_type' => $mediaType, 'media_path' => $mediaPath, 'media_name' => $mediaName,
        ]);
        echo json_encode(['ok' => true, 'saved_at' => date('H:i:s')]);
        exit;
    }

    if ($act === 'world_draft_media') {
        header('Content-Type: application/json');
        $isEdit = input('draft_scope') === 'edit';
        $mid = (int) input('mid');
        $kind = 'world';
        if ($isEdit) {
            $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND user_id = ?', [$mid, (int) $me['id']]);
            if (!$moment) { echo json_encode(['ok' => false, 'msg' => '没有权限保存这条动态的草稿']); exit; }
            $kind = 'world_edit_' . $mid;
        }
        try {
            list($mediaType, $media) = world_store_media('media');
            if (!$media) { throw new Exception('没有接收到媒体文件'); }
            $old = DB::one('SELECT * FROM ' . DB::table('drafts') . ' WHERE user_id = ? AND kind = ? ORDER BY updated_at DESC, id DESC LIMIT 1', [(int) $me['id'], $kind]);
            $content = mb_substr(trim((string) input('content')), 0, 1000);
            draft_save((int) $me['id'], '', $content, 0, $kind, [
                'media_type' => $mediaType, 'media_path' => $media['path'], 'media_name' => $media['name'],
            ]);
            if ($old && !empty($old['media_path']) && $old['media_path'] !== $media['path']) {
                delete_upload($old['media_path']);
            }
            $url = upload_url($media['path']);
            $audioMeta = null;
            if ($mediaType === 'image') { $preview = $url; }
            elseif ($mediaType === 'video') { $preview = video_playable_url($url); }
            else {
                $preview = audio_playable_url($url);
                $meta = audio_track_meta($media['path'], (string) $media['name']);
                $audioMeta = [
                    'title' => (string) $meta['title'],
                    'artist' => (string) $meta['artist'],
                    'cover' => (string) $meta['cover'],
                    'lyrics_b64' => base64_encode((string) ($meta['lyrics'] ?? '')),
                ];
            }
            $audioToken = ($mediaType === 'voice')
                ? compose_audio_token_make((string) $media['path'], (int) $me['id'], 21600, (string) $media['name'])
                : '';
            echo json_encode(['ok' => true, 'type' => $mediaType, 'path' => $media['path'], 'name' => $media['name'], 'preview' => $preview, 'download' => $url, 'audio' => $audioMeta, 'audio_token' => $audioToken]);
        } catch (Exception $e) {
            echo json_encode(['ok' => false, 'msg' => $e->getMessage()]);
        }
        exit;
    }

    if ($act === 'publish') {
        if (user_is_muted($me)) {
            flash('你当前处于禁言状态,暂时不能发布动态', 'err');
            redirect(world_url());
        }
        $content = trim((string) input('content'));
        $content = mb_substr($content, 0, 1000);
        $location = '';
        if (profile_location_mode($me) > 0) {
            $location = trim(profile_location_public_text($me));
            if ($location === '' || $location === '未设置所在地') {
                $location = ip_location_text(client_ip());
            }
        }
        $location = mb_substr($location, 0, 80);
        $adGuard = plugin_content_check('moment', $content);
        if ($adGuard) {
            flash($adGuard['message'], 'err');
            redirect(world_url());
        }

        try {
            $publishDraft = DB::one('SELECT * FROM ' . DB::table('drafts') . " WHERE user_id = ? AND kind = 'world' ORDER BY updated_at DESC, id DESC LIMIT 1", [(int) $me['id']]);
            list($mediaType, $media) = world_store_media('media');
            $draftPath = trim((string) input('draft_media_path'));
            if (!$media && $draftPath !== '' && $publishDraft && $publishDraft['media_path'] === $draftPath
                && world_draft_media_for((int) $me['id'], 'world', $draftPath)) {
                $mediaType = (string) $publishDraft['media_type'];
                $media = ['path' => $draftPath, 'name' => (string) $publishDraft['media_name']];
            }
            if ($content === '' && !$media) {
                throw new Exception('写点什么,或者选择一张图片 / 一段视频 / 一段语音');
            }
            $voiceText = '';
            if (($mediaType ?? '') === 'voice') {
                $voiceText = mb_substr(trim((string) input('voice_text')), 0, 1000);
            }
            $audioExtra = ['lyrics' => '', 'cover_path' => '', 'matched_title' => '', 'matched_artist' => ''];
            if (($mediaType ?? '') === 'voice') {
                $audioLyrics = base64_decode((string) input('draft_audio_lyrics_b64'), true);
                $audioCover = trim((string) input('draft_audio_cover_path'));
                $audioTitle = mb_substr(trim((string) input('draft_audio_title')), 0, 200, 'UTF-8');
                $audioArtist = mb_substr(trim((string) input('draft_audio_artist')), 0, 200, 'UTF-8');
                if (!is_string($audioLyrics)) { $audioLyrics = ''; }
                if (strpos($audioCover, 'uploads/audio-covers/') !== 0 || !is_file(APP_ROOT . '/' . $audioCover)) {
                    $audioCover = '';
                }
                $audioExtra = ['lyrics' => $audioLyrics, 'cover_path' => $audioCover,
                    'matched_title' => $audioTitle, 'matched_artist' => $audioArtist];
            }
            DB::insert('moments', [
                'user_id'    => (int) $me['id'],
                'content'    => $content,
                'voice_text' => $voiceText,
                'lyrics'     => $audioExtra['lyrics'],
                'cover_path' => $audioExtra['cover_path'],
                'matched_title' => $audioExtra['matched_title'],
                'matched_artist' => $audioExtra['matched_artist'],
                'media_type' => $mediaType ?? '',
                'media_path' => $media['path'] ?? '',
                'media_name' => $media['name'] ?? '',
                'location'   => $location,
                'status'     => 1,
                'created_at' => now(),
            ]);
            draft_clear((int) $me['id'], 'world');
            if ($publishDraft && !empty($publishDraft['media_path'])
                && (!is_array($media) || $publishDraft['media_path'] !== ($media['path'] ?? ''))) {
                delete_upload($publishDraft['media_path']);
            }
            flash('动态已发布');
        } catch (Exception $e) {
            flash('发布失败:' . $e->getMessage(), 'err');
        }
        redirect(world_url());
    }

    if ($act === 'edit') {
        $mid = (int) input('mid');
        $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND status = 1', [$mid]);
        if (!$moment) {
            flash('这条动态不存在或已删除', 'err');
        } elseif ((int) $moment['user_id'] !== (int) $me['id'] && !(is_admin($me) && (!is_superadmin(user_by_id((int) $moment['user_id'])) || is_superadmin($me)))) {
            flash('没有权限编辑这条动态', 'err');
        } else {
            $content = trim((string) input('content'));
            $content = mb_substr($content, 0, 1000);
            $mediaType = $moment['media_type'];
            $mediaPath = $moment['media_path'];
            $mediaName = $moment['media_name'];
            $removeMedia = input('remove_media') === '1';
            $editKind = 'world_edit_' . $mid;
            $editDraft = DB::one('SELECT * FROM ' . DB::table('drafts') . ' WHERE user_id = ? AND kind = ? ORDER BY updated_at DESC, id DESC LIMIT 1', [(int) $me['id'], $editKind]);
            $draftPath = trim((string) input('draft_media_path'));
            try {
                list($newType, $newMedia) = world_store_media('media');
                if ($newMedia) {
                    if (!empty($moment['media_path'])) {
                        delete_upload($moment['media_path']);
                    }
                    $mediaType = $newType;
                    $mediaPath = $newMedia['path'];
                    $mediaName = $newMedia['name'];
                } elseif (!$removeMedia && $draftPath !== '' && $editDraft && $editDraft['media_path'] === $draftPath
                    && world_draft_media_for((int) $me['id'], $editKind, $draftPath)) {
                    if (!empty($moment['media_path']) && $moment['media_path'] !== $draftPath) {
                        delete_upload($moment['media_path']);
                    }
                    $mediaType = (string) $editDraft['media_type'];
                    $mediaPath = $draftPath;
                    $mediaName = (string) $editDraft['media_name'];
                } elseif ($removeMedia && !empty($moment['media_path'])) {
                    delete_upload($moment['media_path']);
                    $mediaType = '';
                    $mediaPath = '';
                    $mediaName = '';
                }
                if ($content === '' && $mediaPath === '') {
                    throw new Exception('文字和媒体不能同时为空');
                }
                $adGuard = plugin_content_check('moment', $content, ['moment_id' => $mid]);
                if ($adGuard) {
                    throw new Exception($adGuard['message']);
                }
                DB::update('moments', [
                    'content'    => $content,
                    'media_type' => $mediaType,
                    'media_path' => $mediaPath,
                    'media_name' => $mediaName,
                ], 'id = ?', [$mid]);
                draft_clear((int) $me['id'], $editKind);
                if ($editDraft && !empty($editDraft['media_path']) && $editDraft['media_path'] !== $mediaPath) {
                    delete_upload($editDraft['media_path']);
                }
                flash('动态已更新');
            } catch (Exception $e) {
                flash('编辑失败:' . $e->getMessage(), 'err');
            }
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'like') {
        $mid = (int) input('mid');
        $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND status = 1', [$mid]);
        if ($moment) {
            $has = DB::val('SELECT COUNT(*) FROM ' . DB::table('moment_likes') . ' WHERE moment_id = ? AND user_id = ?', [$mid, $me['id']]);
            if ($has) {
                DB::delete('moment_likes', 'moment_id = ? AND user_id = ?', [$mid, $me['id']]);
            } else {
                DB::insert('moment_likes', ['moment_id' => $mid, 'user_id' => $me['id'], 'created_at' => now()]);
                notify((int) $moment['user_id'], (int) $me['id'], 'moment_like', $mid,
                    display_name($me) . ' 赞了你的动态');
            }
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'comment') {
        $mid = (int) input('mid');
        $content = trim((string) input('content'));
        $content = mb_substr($content, 0, 500);
        $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND status = 1', [$mid]);
        if (!$moment) {
            flash('这条动态不存在或已删除', 'err');
        } elseif (user_is_muted($me)) {
            flash('你当前处于禁言状态,暂时不能评论', 'err');
        } elseif ($content === '') {
            flash('评论内容不能为空', 'err');
        } else {
            $adGuard = plugin_content_check('moment_comment', $content);
            if ($adGuard) {
                flash($adGuard['message'], 'err');
                redirect(world_url() . '#m' . $mid);
            }
            DB::insert('moment_comments', [
                'moment_id'  => $mid,
                'user_id'    => (int) $me['id'],
                'content'    => $content,
                'created_at' => now(),
            ]);
            notify((int) $moment['user_id'], (int) $me['id'], 'moment_comment', $mid,
                display_name($me) . ' 评论了你的动态:' . mb_substr($content, 0, 60));
            flash('评论成功');
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'reply') {
        $mid = (int) input('mid');
        $cid = (int) input('cid');
        $content = trim((string) input('content'));
        $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND status = 1', [$mid]);
        $parent = DB::one('SELECT * FROM ' . DB::table('moment_comments') . ' WHERE id = ? AND moment_id = ?', [$cid, $mid]);
        if (!$moment) {
            flash('这条动态不存在或已删除', 'err');
        } elseif (!$parent) {
            flash('被回复的评论不存在', 'err');
        } elseif (user_is_muted($me)) {
            flash('你当前处于禁言状态,暂时不能回复评论', 'err');
        } elseif ($content === '') {
            flash('回复内容不能为空', 'err');
        } elseif (mb_strlen($content) > 500) {
            flash('回复最多 500 字', 'err');
        } else {
            $rootId = (int) $parent['parent_id'] > 0 ? (int) $parent['parent_id'] : (int) $parent['id'];
            $adGuard = plugin_content_check('moment_reply', $content);
            if ($adGuard) {
                flash($adGuard['message'], 'err');
                redirect(world_url() . '#m' . $mid);
            }
            DB::insert('moment_comments', [
                'moment_id' => $mid,
                'user_id'   => (int) $me['id'],
                'parent_id' => $rootId,
                'content'   => $content,
                'created_at'=> now(),
            ]);
            if ((int) $parent['user_id'] !== (int) $me['id']) {
                notify((int) $parent['user_id'], (int) $me['id'], 'moment_comment', $mid,
                    display_name($me) . ' 回复了你的评论:' . mb_substr($content, 0, 60));
            }
            flash('回复成功');
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'edit_comment') {
        $cid = (int) input('cid');
        $mid = (int) input('mid');
        $comment = DB::one('SELECT * FROM ' . DB::table('moment_comments') . ' WHERE id = ? AND moment_id = ?', [$cid, $mid]);
        $content = trim((string) input('content'));
        if (!$comment) {
            flash('评论不存在', 'err');
        } elseif ((int) $comment['user_id'] !== (int) $me['id']) {
            flash('只能修改自己发表的评论', 'err');
        } elseif ($content === '') {
            flash('评论内容不能为空', 'err');
        } elseif (mb_strlen($content) > 500) {
            flash('评论最多 500 字', 'err');
        } else {
            $adGuard = plugin_content_check('moment_comment', $content, ['comment_id' => $cid]);
            if ($adGuard) {
                flash($adGuard['message'], 'err');
            } else {
                DB::update('moment_comments', ['content' => $content], 'id = ? AND moment_id = ?', [$cid, $mid]);
                flash('评论已修改');
            }
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'report') {
        $mid = (int) input('mid');
        $reason = mb_substr(trim((string) input('reason')), 0, 200);
        $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ? AND status = 1', [$mid]);
        if (!$moment) {
            flash('这条动态不存在或已删除', 'err');
        } elseif (feature_denied($me, 'report')) {
            flash('「举报」功能已被管理员关闭', 'err');
        } elseif ((int) $moment['user_id'] === (int) $me['id']) {
            flash('不能举报自己的动态', 'err');
        } else {
            $reportReason = $reason !== '' ? $reason : '未填写原因';
            DB::insert('reports', [
                'target_type' => 'moment',
                'target_id'   => $mid,
                'user_id'     => (int) $me['id'],
                'reason'      => $reportReason,
                'status'      => 0,
                'created_at'  => now(),
            ]);
            $excerpt = trim((string) $moment['content']);
            if ($excerpt === '') {
                $excerpt = $moment['media_type'] === 'video' ? '视频动态' : ($moment['media_type'] === 'image' ? '图片动态' : '语音动态');
            }
            notify_admins_report($me['id'], 'moment', $mid, $reportReason, $excerpt);
            flash('举报已提交,管理员会尽快处理');
        }
        redirect(world_url() . '#m' . $mid);
    }

    if ($act === 'delete_moment' || $act === 'delete_comment') {
        if ($act === 'delete_moment') {
            $mid = (int) input('mid');
            $moment = DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ?', [$mid]);
            $momentAuthorSuper = $moment ? is_superadmin(user_by_id((int) $moment['user_id'])) : false;
            $canDeleteMoment = $moment && ((int) $moment['user_id'] === (int) $me['id'] || (is_admin($me) && (!$momentAuthorSuper || is_superadmin($me))));
            if ($canDeleteMoment) {
                DB::delete('moment_comments', 'moment_id = ?', [$mid]);
                DB::delete('moment_likes', 'moment_id = ?', [$mid]);
                DB::delete('moments', 'id = ?', [$mid]);
                if (!empty($moment['media_path'])) {
                    delete_upload($moment['media_path']);
                }
                flash((int) $moment['user_id'] === (int) $me['id'] ? '你的动态已删除' : '违规动态已删除');
            } else {
                flash('没有权限删除这条动态', 'err');
            }
            redirect(world_url());
        }
        $cid = (int) input('cid');
        $comment = DB::one('SELECT * FROM ' . DB::table('moment_comments') . ' WHERE id = ?', [$cid]);
        $commentAuthorSuper = $comment ? is_superadmin(user_by_id((int) $comment['user_id'])) : false;
        $commentMoment = $comment ? DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ?', [(int) $comment['moment_id']]) : null;
        $commentMomentSuper = $commentMoment ? is_superadmin(user_by_id((int) $commentMoment['user_id'])) : false;
        $hasSuperReply = false;
        if ($comment) {
            foreach (DB::all('SELECT user_id FROM ' . DB::table('moment_comments') . ' WHERE parent_id = ?', [$cid]) as $replyRow) {
                if (is_superadmin(user_by_id((int) $replyRow['user_id']))) { $hasSuperReply = true; break; }
            }
        }
        $canDeleteComment = $comment && ((int) $comment['user_id'] === (int) $me['id']
            || (is_admin($me) && (!$commentAuthorSuper || is_superadmin($me)) && (!$commentMomentSuper || is_superadmin($me)) && (!$hasSuperReply || is_superadmin($me))));
        if ($canDeleteComment) {
            DB::delete('moment_comments', 'parent_id = ?', [$cid]);
            DB::delete('moment_comments', 'id = ?', [$cid]);
            flash('评论已删除');
        } else {
            flash('没有权限删除这条评论', 'err');
        }
        redirect(world_url() . '#m' . (int) ($comment['moment_id'] ?? 0));
    }

    if ($act === 'delete_reply') {
        $cid = (int) input('cid');
        $reply = DB::one('SELECT * FROM ' . DB::table('moment_comments') . ' WHERE id = ? AND parent_id <> 0', [$cid]);
        $replyAuthorSuper = $reply ? is_superadmin(user_by_id((int) $reply['user_id'])) : false;
        $replyMoment = $reply ? DB::one('SELECT * FROM ' . DB::table('moments') . ' WHERE id = ?', [(int) $reply['moment_id']]) : null;
        $replyMomentSuper = $replyMoment ? is_superadmin(user_by_id((int) $replyMoment['user_id'])) : false;
        $canDeleteReply = $reply && ((int) $reply['user_id'] === (int) $me['id']
            || (is_admin($me) && (!$replyAuthorSuper || is_superadmin($me)) && (!$replyMomentSuper || is_superadmin($me))) );
        if (!$reply) {
            flash('回复不存在', 'err');
        } elseif (!$canDeleteReply) {
            flash('没有权限删除这条回复', 'err');
        } else {
            DB::delete('moment_comments', 'id = ?', [$cid]);
            flash('回复已删除');
        }
        redirect(world_url() . '#m' . (int) ($reply['moment_id'] ?? 0));
    }

    redirect(world_url());
}

$worldWhere = 'm.status = 1';
if ($worldFilterUser) {
    $worldWhere .= ' AND m.user_id = ' . (int) $worldFilterUser['id'];
}
$pager = paginate((int) DB::val('SELECT COUNT(*) FROM ' . DB::table('moments') . ' m WHERE ' . $worldWhere, [], 0), 10);
$rows = DB::all('SELECT m.*, u.nickname, u.username, u.avatar, u.verified, u.verify_style, u.verify_title, u.role, u.province, u.city, u.district, u.town, u.village, u.last_ip, u.show_location
                 FROM ' . DB::table('moments') . ' m
                 LEFT JOIN ' . DB::table('users') . ' u ON u.id = m.user_id
                 WHERE ' . $worldWhere . ' ORDER BY m.id DESC LIMIT ' . (int) $pager['limit'] . ' OFFSET ' . (int) $pager['offset']);

$GLOBALS['PAGE'] = 'world';
render_header('世界圈');
?>
<section class="world-hero">
  <span class="hero-kicker"><?= $worldFilterUser ? 'MOMENTS' : 'WORLD CIRCLE' ?></span>
  <h1><?= $worldFilterUser ? e(display_name($worldFilterUser)) . ' 的世界圈' : '世界圈' ?></h1>
  <p><?= $worldFilterUser ? '查看 ' . e(display_name($worldFilterUser)) . ' 发布的全部动态。' : '世界那么大,快来分享你的故事吧。' ?></p>
  <?php if ($worldFilterUser): ?>
    <div class="world-hero-actions">
      <a class="btn-mini ghost" href="<?= e(page_url('world')) ?>">返回全站世界圈</a>
      <a class="btn-mini ghost" href="<?= e(page_url('profile')) ?>">返回我的</a>
    </div>
  <?php endif; ?>
</section>

<?php if (!$worldFilterUser || (int) $worldFilterUser['id'] === (int) $me['id']): ?>
<section class="world-publish">
  <?php if ($showWorldDraftPrompt): ?>
  <div class="modal" id="worldDraftPrompt">
    <div class="modal-mask"></div>
    <div class="modal-card draft-resume-card">
      <h3>检测到上次未完成的世界圈动态</h3>
      <p class="muted">上次编辑于 <?= e($worldDraft['updated_at']) ?>,可能包含已上传的图片 / 视频 / 音频。要恢复继续编辑吗?</p>
      <div class="modal-actions">
        <a class="btn-mini ghost" href="<?= e(world_url(['discard' => 1])) ?>">重新开始</a>
        <a class="btn-primary" href="<?= e(world_url(['resume' => 1])) ?>">继续编辑</a>
      </div>
    </div>
  </div>
  <?php endif; ?>
  <?php if (!user_is_muted($me)): ?>
  <?php $composerOpen = (bool) ($worldDraftData || $showWorldDraftPrompt); ?>
  <button class="btn-primary world-toggle" type="button" id="worldComposerToggle" aria-expanded="<?= $composerOpen ? 'true' : 'false' ?>"><?= $composerOpen ? '收起发布' : '发布动态' ?></button>
  <div class="panel world-composer" id="worldComposer" <?= $composerOpen ? '' : 'hidden' ?>>
    <form method="post" enctype="multipart/form-data" class="world-form" id="worldComposerForm" data-upload-progress data-upload-progress-label="正在上传世界圈文件" data-upload-max-bytes="<?= upload_limit_bytes('upload_world_mb', 1000) ?>">
      <?= csrf_field() ?>
      <input type="hidden" name="act" value="publish">
      <textarea name="content" rows="3" maxlength="1000" placeholder="这一刻的想法……"><?= e($worldDraftData['content'] ?? '') ?></textarea>
      <input type="hidden" name="media_kind" id="worldMediaKind" value="">
      <div class="world-tools">
        <div class="upload-drop world-drop" data-drop-upload>
          <input type="file" name="media" id="worldMedia" accept="image/*,video/*,audio/*,.gif,.flac,.ape,.wma,.aiff,.aif,.amr,.opus,.m4a,.m4b,.aac,.ogg,.oga,.wav,.mp3,.mp2,.mid,.midi,.dsf,.dff,.mka,.wv,.tta,.spx,.caf,.au,.snd,.ac3,.eac3,.dts,.ra,.ram,.3ga,.m4r,.alac,.voc,.awb,.xmf,.tak,.mogg,.sln" hidden>
          <div class="ud-ico">🖼</div>
          <strong>点击选择图片 / 视频 / 任意格式音频,或拖到这里</strong>
          <small data-file-name>支持 GIF 动态图与 FLAC / APE / WMA / AMR / OPUS 等音频;PC 可直接拖动;单文件上限 <?php $worldMax = upload_limit_bytes('upload_world_mb', 1000); echo $worldMax > 0 ? format_size($worldMax) : '不限'; ?></small>
        </div>
        <button class="btn-mini ghost" type="button" id="worldVoiceBtn">🎙 录语音</button>
        <span class="muted" id="worldVoiceStatus"></span>
      </div>
      <?php $draftHasMedia = !empty($worldDraftData['media_path']); ?>
      <?php $draftIsAudio = $draftHasMedia && ($worldDraftData['media_type'] ?? '') === 'voice'; ?>
      <div class="world-draft-media <?= $draftIsAudio ? 'is-audio' : '' ?>" id="worldDraftMedia" <?= $draftHasMedia ? '' : 'hidden' ?>>
        <div class="world-draft-preview" id="worldDraftPreview">
          <?php if ($draftHasMedia): ?>
            <?php if ($worldDraftData['media_type'] === 'image'): ?>
              <img src="<?= e(upload_url($worldDraftData['media_path'])) ?>" alt="">
            <?php elseif ($worldDraftData['media_type'] === 'video'): ?>
              <video src="<?= e(video_playable_url(upload_url($worldDraftData['media_path']))) ?>" muted playsinline preload="metadata"></video>
            <?php else:
                $draftAudioMeta = audio_track_meta($worldDraftData['media_path'], (string) ($worldDraftData['media_name'] ?? ''));
            ?>
              <audio class="world-draft-audio-player" data-km-audio="1" data-audio-title="<?= e($draftAudioMeta['title']) ?>" data-audio-artist="<?= e($draftAudioMeta['artist']) ?>" data-audio-cover="<?= e($draftAudioMeta['cover']) ?>" data-audio-caption="<?= e($worldDraftData['media_name'] ?? '') ?>" data-audio-hide-caption="1" data-audio-lyrics-b64="<?= e(base64_encode((string) ($draftAudioMeta['lyrics'] ?? ''))) ?>" data-audio-download="<?= e(upload_url($worldDraftData['media_path'])) ?>" controls playsinline preload="metadata" src="<?= e(audio_playable_url(upload_url($worldDraftData['media_path']))) ?>"></audio>
            <?php endif; ?>
          <?php endif; ?>
        </div>
        <div class="world-draft-info">
          <strong id="worldDraftName"><?= e($worldDraftData['media_name'] ?? '') ?></strong>
          <span class="muted" id="worldDraftStatus">已恢复上次上传的文件,退出后仍会保留</span>
          <button class="link-btn danger" type="button" id="worldDraftRemove">移除这个文件</button>
        </div>
      </div>
      <input type="hidden" name="voice_text" id="worldVoiceText" value="">
      <input type="hidden" name="draft_media_path" id="worldDraftPath" value="<?= e($worldDraftData['media_path'] ?? '') ?>">
      <input type="hidden" name="draft_media_type" id="worldDraftType" value="<?= e($worldDraftData['media_type'] ?? '') ?>">
      <input type="hidden" name="draft_media_name" id="worldDraftNameInput" value="<?= e($worldDraftData['media_name'] ?? '') ?>">
      <div class="world-draft-tip" id="worldDraftTip">编辑内容会自动保存,意外退出后可恢复文字和已上传文件</div>
      <input type="hidden" name="draft_audio_lyrics_b64" id="worldAudioLyrics" value="">
      <input type="hidden" name="draft_audio_cover_path" id="worldAudioCover" value="">
      <input type="hidden" name="draft_audio_title" id="worldAudioTitle" value="">
      <input type="hidden" name="draft_audio_artist" id="worldAudioArtist" value="">
      <button class="btn-primary" type="submit" id="worldPublishBtn">发布</button>
    </form>
  </div>
  <?php else: ?>
  <p class="muted">你当前处于禁言状态,暂时不能发布动态或评论。</p>
  <?php endif; ?>
</section>
<?php endif; ?>

<section class="world-feed">
  <?php if (!$rows): ?>
    <?php empty_state('世界圈还很安静', '发第一条动态,让大家听见你的此刻'); ?>
  <?php else: ?>
    <?php foreach ($rows as $m): ?>
      <?php
        $mid = (int) $m['id'];
        $likeCount = (int) DB::val('SELECT COUNT(*) FROM ' . DB::table('moment_likes') . ' WHERE moment_id = ?', [$mid], 0);
        $liked = (bool) DB::val('SELECT COUNT(*) FROM ' . DB::table('moment_likes') . ' WHERE moment_id = ? AND user_id = ?', [$mid, $me['id']]);
        $commentRows = DB::all('SELECT c.*, u.nickname, u.username, u.avatar, u.verified, u.verify_style, u.verify_title, u.role, u.comment_bubble,
                                     pu.nickname AS parent_nickname
                              FROM ' . DB::table('moment_comments') . ' c
                              LEFT JOIN ' . DB::table('users') . ' u ON u.id = c.user_id
                              LEFT JOIN ' . DB::table('moment_comments') . ' p ON p.id = c.parent_id
                              LEFT JOIN ' . DB::table('users') . ' pu ON pu.id = p.user_id
                              WHERE c.moment_id = ? ORDER BY c.id ASC LIMIT 100', [$mid]);
        $comments = [];
        $worldRepliesByParent = [];
        foreach ($commentRows as $commentRow) {
            if ((int) $commentRow['parent_id'] === 0) { $comments[] = $commentRow; }
            else { $worldRepliesByParent[(int) $commentRow['parent_id']][] = $commentRow; }
        }
        $commentTotal = count($commentRows);
        $momentAuthorSuper = is_superadmin($m);
        $canModerateMoment = is_admin($me) && (!$momentAuthorSuper || is_superadmin($me));
        $canDelete = (int) $m['user_id'] === (int) $me['id'] || $canModerateMoment;
        $canEdit = $canDelete;
        $editDraft = $worldEditDrafts[$mid] ?? null;
        $editHasDraftMedia = !empty($editDraft['media_path']);
        $locText = '';
        if (profile_location_mode($m) > 0) {
            $locText = trim((string) ($m['location'] ?? ''));
            if ($locText === '') {
                $locText = trim(profile_location_public_text($m));
                if ($locText === '未设置所在地') { $locText = ''; }
            }
        }
      ?>
      <article class="world-card" id="m<?= $mid ?>">
        <header class="world-head">
          <a href="<?= e(page_url('user', ['id' => (int) $m['user_id']])) ?>" class="world-avatar">
            <?= avatar_html($m) ?>
          </a>
          <div class="world-meta">
            <div class="world-name">
              <a href="<?= e(page_url('user', ['id' => (int) $m['user_id']])) ?>"><?= e(display_name($m)) ?></a><?= verify_badge($m) ?>
            </div>
            <span class="world-time"><?= e(date('Y-m-d H:i:s', strtotime((string) $m['created_at']))) ?></span>
          </div>
          <div class="world-owner-actions">
            <?php if ($canEdit): ?>
              <button class="link-btn" type="button" data-world-edit="edit-m<?= $mid ?>">编辑</button>
            <?php endif; ?>
            <?php if ($canDelete): ?>
              <form method="post" class="inline-form" data-confirm="删除这条动态及其评论?">
                <?= csrf_field() ?><input type="hidden" name="act" value="delete_moment"><input type="hidden" name="mid" value="<?= $mid ?>">
                <button class="link-btn danger" type="submit">删除</button>
              </form>
            <?php endif; ?>
          </div>
        </header>

        <?php if (trim((string) $m['content']) !== ''): ?>
          <p class="world-text"><?= nl2br(e($m['content'])) ?></p>
        <?php endif; ?>

        <?php if (!empty($m['media_path'])): ?>
          <?php if ($m['media_type'] === 'image'): ?>
            <a class="world-media" href="<?= e(upload_url($m['media_path'])) ?>" target="_blank" rel="noopener">
              <img src="<?= e(upload_url($m['media_path'])) ?>" alt="<?= e($m['media_name']) ?>" loading="lazy">
            </a>
          <?php elseif ($m['media_type'] === 'video'): ?>
            <div class="world-video-wrap" data-world-video-wrap>
              <video class="world-video" controls muted autoplay loop playsinline preload="metadata" data-world-video src="<?= e(video_playable_url(upload_url($m['media_path']))) ?>"></video>
              <button class="world-sound-tip" type="button" data-world-sound>点击开启声音</button>
            </div>
          <?php elseif ($m['media_type'] === 'voice'):
              $worldAudioMeta = audio_track_meta_stored($m);
          ?>
            <div class="world-voice">
              <audio class="world-audio" data-km-audio="1" data-km-card-style="panel" data-audio-title="<?= e($worldAudioMeta['title']) ?>" data-audio-artist="<?= e($worldAudioMeta['artist']) ?>" data-audio-cover="<?= e($worldAudioMeta['cover']) ?>" data-audio-caption="<?= e($m['media_name']) ?>" data-audio-hide-caption="1" data-audio-lyrics-b64="<?= e(base64_encode((string) ($worldAudioMeta['lyrics'] ?? ''))) ?>" data-audio-download="<?= e(upload_url($m['media_path'])) ?>" data-km-save-world="1" data-km-save-mid="<?= (int) $mid ?>" data-km-save-url="<?= e(upload_url($m['media_path'])) ?>" controls playsinline preload="metadata" src="<?= e(audio_playable_url(upload_url($m['media_path']))) ?>"></audio>
            </div>
            <?php if ($canEdit): ?>
            <div class="audio-match-tools">
              <button class="btn-mini" type="button" data-audio-match-auto data-target="moment" data-id="<?= (int) $mid ?>">一键匹配歌词封面</button>
              <button class="btn-mini ghost" type="button" data-audio-match-open data-target="moment" data-id="<?= (int) $mid ?>" data-title="<?= e($worldAudioMeta['title']) ?>" data-artist="<?= e($worldAudioMeta['artist']) ?>">手动搜索</button>
            </div>
            <?php endif; ?>
            <?php $worldVoiceText = trim((string) ($m['voice_text'] ?? '')); ?>
            <?php if ($worldVoiceText !== ''): ?><p class="voice-transcript world-voice-transcript">📝 <?= nl2br(e($worldVoiceText)) ?></p><?php endif; ?>
          <?php endif; ?>
        <?php endif; ?>

        <?php if ($locText !== ''): ?>
          <div class="world-loc">📍 <?= e($locText) ?></div>
        <?php endif; ?>

        <?php if ($canEdit): ?>
        <form method="post" enctype="multipart/form-data" class="world-edit-form" id="edit-m<?= $mid ?>" data-upload-progress data-upload-progress-label="正在上传世界圈文件" data-upload-max-bytes="<?= upload_limit_bytes('upload_world_mb', 1000) ?>" hidden>
          <?= csrf_field() ?>
          <input type="hidden" name="act" value="edit"><input type="hidden" name="mid" value="<?= $mid ?>">
          <textarea name="content" rows="3" maxlength="1000" placeholder="修改这条动态……"><?= e($editDraft['content'] ?? $m['content']) ?></textarea>
          <?php if (!empty($m['media_path'])): ?>
            <div class="world-edit-current" data-world-current <?= $editHasDraftMedia ? 'hidden' : '' ?>>
              <?php if ($m['media_type'] === 'image'): ?>
                <img class="world-edit-thumb" src="<?= e(upload_url($m['media_path'])) ?>" alt="">
              <?php elseif ($m['media_type'] === 'video'): ?>
                <video class="world-edit-thumb" src="<?= e(video_playable_url(upload_url($m['media_path']))) ?>" muted playsinline preload="metadata"></video>
              <?php else: ?>
                <span class="world-edit-thumb world-edit-audio">音频</span>
              <?php endif; ?>
              <div>
                <div class="world-edit-media">当前媒体:<?= e($m['media_name'] ?: $m['media_type']) ?></div>
                <button class="link-btn danger" type="button" data-world-remove-media>删除这个媒体</button>
              </div>
              <input type="hidden" name="remove_media" value="0" data-world-remove-input>
            </div>
            <div class="world-edit-removed" data-world-removed hidden>已标记删除,保存修改后生效。 <button class="link-btn" type="button" data-world-undo-media>撤销</button></div>
          <?php endif; ?>
          <?php $editDraftIsAudio = $editHasDraftMedia && ($editDraft['media_type'] ?? '') === 'voice'; ?>
          <div class="world-draft-media world-edit-draft <?= $editDraftIsAudio ? 'is-audio' : '' ?>" data-world-edit-draft <?= $editHasDraftMedia ? '' : 'hidden' ?>>
            <div class="world-draft-preview" data-world-edit-draft-preview>
              <?php if ($editHasDraftMedia): ?>
                <?php if ($editDraft['media_type'] === 'image'): ?>
                  <img src="<?= e(upload_url($editDraft['media_path'])) ?>" alt="">
                <?php elseif ($editDraft['media_type'] === 'video'): ?>
                  <video src="<?= e(video_playable_url(upload_url($editDraft['media_path']))) ?>" muted playsinline preload="metadata"></video>
                <?php else:
                    $editAudioMeta = audio_track_meta($editDraft['media_path'], (string) ($editDraft['media_name'] ?? ''));
                ?>
                  <audio class="world-draft-audio-player" data-km-audio="1" data-audio-title="<?= e($editAudioMeta['title']) ?>" data-audio-artist="<?= e($editAudioMeta['artist']) ?>" data-audio-cover="<?= e($editAudioMeta['cover']) ?>" data-audio-caption="<?= e($editDraft['media_name'] ?? '') ?>" data-audio-hide-caption="1" data-audio-lyrics-b64="<?= e(base64_encode((string) ($editAudioMeta['lyrics'] ?? ''))) ?>" data-audio-download="<?= e(upload_url($editDraft['media_path'])) ?>" controls playsinline preload="metadata" src="<?= e(audio_playable_url(upload_url($editDraft['media_path']))) ?>"></audio>
                <?php endif; ?>
              <?php endif; ?>
            </div>
            <div class="world-draft-info">
              <strong data-world-edit-draft-name><?= e($editDraft['media_name'] ?? '') ?></strong>
              <span class="muted">上次未保存上传的文件,已自动恢复</span>
              <button class="link-btn danger" type="button" data-world-edit-draft-remove>移除草稿文件</button>
            </div>
          </div>
          <input type="hidden" name="draft_media_path" data-world-edit-draft-path value="<?= e($editDraft['media_path'] ?? '') ?>">
          <input type="hidden" name="draft_media_type" data-world-edit-draft-type value="<?= e($editDraft['media_type'] ?? '') ?>">
          <input type="hidden" name="draft_media_name" data-world-edit-draft-name-input value="<?= e($editDraft['media_name'] ?? '') ?>">
          <label class="world-edit-media">更换媒体(图片 / 视频 / 任意音频)
            <input type="file" name="media" accept="image/*,video/*,audio/*,.gif,.flac,.ape,.wma,.aiff,.aif,.amr,.opus,.m4a,.m4b,.aac,.ogg,.oga,.wav,.mp3,.mp2,.mid,.midi,.dsf,.dff,.mka,.wv,.tta,.spx,.caf,.au,.snd,.ac3,.eac3,.dts,.ra,.ram,.3ga,.m4r,.alac,.voc,.awb,.xmf,.tak,.mogg,.sln" data-world-edit-file>
          </label>
          <button class="link-btn world-edit-clear" type="button" data-world-clear-media>清空已选文件</button>
          <div class="world-edit-actions">
            <button class="btn-mini" type="submit">保存修改</button>
            <button class="btn-mini ghost" type="button" data-world-edit-cancel="edit-m<?= $mid ?>">取消</button>
          </div>
        </form>
        <?php endif; ?>

        <div class="world-actions">
          <form method="post" class="inline-form">
            <?= csrf_field() ?><input type="hidden" name="act" value="like"><input type="hidden" name="mid" value="<?= $mid ?>">
            <button class="world-like <?= $liked ? 'on' : '' ?>" type="submit">♡ <?= $likeCount ?></button>
          </form>
          <?php if ((int) $m['user_id'] !== (int) $me['id'] && !feature_denied($me, 'report')): ?>
            <button class="world-like world-report-btn" type="button" data-world-report="<?= $mid ?>">⚑ 举报</button>
          <?php endif; ?>
          <span class="muted">💬 <?= $commentTotal ?> 条评论</span>
        </div>

        <?php if ($comments): ?>
          <div class="world-comments">
            <?php foreach ($comments as $c): ?>
              <div class="world-comment" id="wc-<?= (int) $c['id'] ?>">
                <?= avatar_html($c) ?>
                <div>
                  <div class="world-comment-name">
                    <strong><?= e(display_name($c)) ?></strong><?= verify_badge($c) ?>
                    <span><?= e(date('Y-m-d H:i:s', strtotime((string) $c['created_at']))) ?></span>
                    <?php if ((int) $c['user_id'] === (int) $me['id']): ?>
                      <button type="button" class="link-btn world-edit-btn" data-edit-comment="<?= (int) $c['id'] ?>">编辑</button>
                    <?php endif; ?>
                    <?php if (!user_is_muted($me)): ?>
                      <button type="button" class="link-btn world-reply-btn" data-reply-comment="<?= (int) $c['id'] ?>">回复</button>
                    <?php endif; ?>
                  </div>
                  <p class="comment-bubble bubble-<?= e(comment_bubble_of($c)) ?>"><?= nl2br(e($c['content'])) ?></p>
                  <?php if ((int) $c['user_id'] === (int) $me['id']): ?>
                  <form method="post" class="comment-edit-form world-comment-edit" id="comment-edit-<?= (int) $c['id'] ?>" hidden>
                    <?= csrf_field() ?>
                    <input type="hidden" name="act" value="edit_comment">
                    <input type="hidden" name="cid" value="<?= (int) $c['id'] ?>">
                    <input type="hidden" name="mid" value="<?= $mid ?>">
                    <textarea name="content" rows="3" maxlength="500" 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 (!user_is_muted($me)): ?>
                  <form method="post" class="comment-reply-form world-comment-reply" 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'] ?>">
                    <input type="hidden" name="mid" value="<?= $mid ?>">
                    <textarea name="content" rows="2" maxlength="500" 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($worldRepliesByParent[(int) $c['id']])): ?>
                  <div class="world-comment-replies">
                    <?php foreach ($worldRepliesByParent[(int) $c['id']] as $r): ?>
                      <?php
                        $replyAuthorSuper = is_superadmin(user_by_id((int) $r['user_id']));
                        $canDeleteReplyView = ((int) $r['user_id'] === (int) $me['id'])
                            || (is_admin($me) && (!$replyAuthorSuper || is_superadmin($me)) && (!is_superadmin($m) || is_superadmin($me)));
                      ?>
                      <div class="world-comment world-comment-reply-item" id="wc-<?= (int) $r['id'] ?>">
                        <?= avatar_html($r) ?>
                        <div>
                          <div class="world-comment-name">
                            <strong><?= e(display_name($r)) ?></strong><?= verify_badge($r) ?>
                            <span class="reply-to">回复 @<?= e($r['parent_nickname'] ?: '原评论') ?></span>
                            <span><?= e(date('Y-m-d H:i:s', strtotime((string) $r['created_at']))) ?></span>
                            <?php if ((int) $r['user_id'] === (int) $me['id']): ?>
                              <button type="button" class="link-btn world-edit-btn" data-edit-comment="<?= (int) $r['id'] ?>">编辑</button>
                            <?php endif; ?>
                            <?php if (!user_is_muted($me)): ?>
                              <button type="button" class="link-btn world-reply-btn" data-reply-comment="<?= (int) $r['id'] ?>">回复</button>
                            <?php endif; ?>
                            <?php if ($canDeleteReplyView): ?>
                            <form method="post" class="inline-form" data-confirm="删除这条回复?">
                              <?= csrf_field() ?><input type="hidden" name="act" value="delete_reply"><input type="hidden" name="cid" value="<?= (int) $r['id'] ?>">
                              <button class="link-btn danger" type="submit">删除</button>
                            </form>
                            <?php endif; ?>
                          </div>
                          <p class="comment-bubble bubble-<?= e(comment_bubble_of($r)) ?>"><?= nl2br(e($r['content'])) ?></p>
                          <?php if ((int) $r['user_id'] === (int) $me['id']): ?>
                          <form method="post" class="comment-edit-form world-comment-edit" id="comment-edit-<?= (int) $r['id'] ?>" hidden>
                            <?= csrf_field() ?>
                            <input type="hidden" name="act" value="edit_comment">
                            <input type="hidden" name="cid" value="<?= (int) $r['id'] ?>">
                            <input type="hidden" name="mid" value="<?= $mid ?>">
                            <textarea name="content" rows="3" maxlength="500" 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 (!user_is_muted($me)): ?>
                          <form method="post" class="comment-reply-form world-comment-reply" 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'] ?>">
                            <input type="hidden" name="mid" value="<?= $mid ?>">
                            <textarea name="content" rows="2" maxlength="500" 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>
                <?php if (is_admin($me) && (!is_superadmin($c) || is_superadmin($me)) && (!is_superadmin($m) || is_superadmin($me))): ?>
                  <form method="post" class="inline-form" data-confirm="删除这条评论?">
                    <?= csrf_field() ?><input type="hidden" name="act" value="delete_comment"><input type="hidden" name="cid" value="<?= (int) $c['id'] ?>">
                    <button class="link-btn danger" type="submit">删除</button>
                  </form>
                <?php endif; ?>
              </div>
            <?php endforeach; ?>
          </div>
        <?php endif; ?>

        <?php if (!user_is_muted($me)): ?>
        <form method="post" class="world-comment-form">
          <?= csrf_field() ?>
          <input type="hidden" name="act" value="comment"><input type="hidden" name="mid" value="<?= $mid ?>">
          <input type="text" name="content" maxlength="500" placeholder="说点什么……" required>
          <button class="btn-mini" type="submit">评论</button>
        </form>
        <?php endif; ?>
      </article>
    <?php endforeach; ?>
    <?= pager_html($pager, 'world', $worldUserParam) ?>
  <?php endif; ?>
</section>


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

<div class="modal" id="worldReportModal" hidden>
  <div class="modal-mask" data-world-report-close></div>
  <div class="modal-card">
    <h3>举报这条世界圈动态</h3>
    <form method="post">
      <?= csrf_field() ?>
      <input type="hidden" name="act" value="report">
      <input type="hidden" name="mid" id="worldReportMid" value="0">
      <textarea name="reason" rows="3" maxlength="200" placeholder="请简述举报原因,例如广告、侵权、违规内容" required></textarea>
      <div class="modal-actions">
        <button type="button" class="btn-mini ghost" data-world-report-close>取消</button>
        <button type="submit" class="btn-mini danger">提交举报</button>
      </div>
    </form>
  </div>
</div>
<script>
(function () {
  var modal = document.getElementById('worldReportModal');
  if (!modal) return;
  var midInput = document.getElementById('worldReportMid');
  document.addEventListener('click', function (e) {
    var trigger = e.target.closest('[data-world-report]');
    if (trigger) {
      e.preventDefault();
      if (midInput) midInput.value = trigger.getAttribute('data-world-report') || '0';
      modal.hidden = false;
      var reason = modal.querySelector('textarea[name="reason"]');
      if (reason) { reason.value = ''; reason.focus(); }
      return;
    }
    if (e.target.closest('[data-world-report-close]')) {
      modal.hidden = true;
    }
  });
})();
</script>
<?php render_footer(); ?>
主题