SOURCE DOWNLOAD

木漏茶室源码下载中心

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

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

已经跑起来的木漏茶室

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

实时检测
2 个站点已部署

已展示全部 2 个站点。

admin/postfiles.php 15.1 KB · 发布包 v6.25.232
下载
<?php
/**
 * 后台 · 帖子文件管理
 * 汇总帖子附件(图片 / 音乐 / 视频 / 任意文件)及正文内嵌的上传媒体。
 * 删除任意文件时,若它属于某篇帖子,会连同该帖子一起删除。
 */
$me = current_user();

function postfile_kind_label($kind)
{
    return ['image' => '图片', 'video' => '视频', 'audio' => '音乐', 'file' => '文件'][$kind] ?? '文件';
}

function postfile_kind_of($name)
{
    $kind = media_kind_by_name((string) $name);
    return $kind !== '' ? $kind : 'file';
}

function postfile_preview_html($path, $name, $kind)
{
    $url = upload_url($path);
    $label = postfile_kind_label($kind);
    if ($kind === 'image') {
        return '<a class="postfile-media image" href="' . e($url) . '" target="_blank" rel="noopener"><img src="' . e($url) . '" alt="' . e($name) . '" loading="lazy"></a>';
    }
    if ($kind === 'video') {
        return '<div class="postfile-media video"><video controls playsinline preload="metadata" src="' . e($url) . '"></video></div>';
    }
    if ($kind === 'audio') {
        return '<div class="postfile-media audio"><span class="postfile-audio-icon">♪</span><audio controls preload="metadata" src="' . e($url) . '"></audio><em>' . e($label) . '</em></div>';
    }
    return '<div class="postfile-media file"><span class="postfile-file-icon">📄</span><strong>' . e(strtoupper(pathinfo((string) $name, PATHINFO_EXTENSION)) ?: 'FILE') . '</strong><em>' . e($label) . '</em></div>';
}

/** 从帖子正文中提取上传到本站的图片、视频、音频和文件链接 */
function postfile_inline_files(array $post, array $attachmentPaths = [])
{
    $content = html_entity_decode((string) ($post['content'] ?? ''), ENT_QUOTES, 'UTF-8');
    if ($content === '' || stripos($content, 'uploads/') === false) {
        return [];
    }
    preg_match_all('~<(?:img|video|audio|source)[^>]+src=["\']([^"\']+)["\']|href=["\']([^"\']+)["\']~i', $content, $matches, PREG_SET_ORDER);
    $files = [];
    foreach ($matches as $match) {
        $raw = trim((string) ($match[1] !== '' ? $match[1] : ($match[2] ?? '')));
        if ($raw === '') { continue; }
        $path = parse_url($raw, PHP_URL_PATH);
        $path = is_string($path) && $path !== '' ? $path : $raw;
        $path = rawurldecode($path);
        $pos = strpos($path, '/uploads/');
        if ($pos !== false) {
            $rel = substr($path, $pos + 1);
        } elseif (strpos($path, 'uploads/') === 0) {
            $rel = $path;
        } else {
            continue;
        }
        $rel = ltrim(str_replace('\\', '/', $rel), '/');
        if ($rel === '' || strpos($rel, '..') !== false || isset($files[$rel]) || isset($attachmentPaths[$rel])) {
            continue;
        }
        if (!is_file(APP_ROOT . '/' . $rel)) { continue; }
        $name = basename($rel);
        $kind = postfile_kind_of($name);
        $files[$rel] = [
            'path' => $rel,
            'name' => $name,
            'size' => (int) @filesize(APP_ROOT . '/' . $rel),
            'kind' => $kind,
        ];
    }
    return $files;
}

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    check_csrf();
    $act = input('act');
    $id = (int) input('id');
    $pid = (int) input('pid');
    $q = trim((string) input('q'));

    if ($act === 'delete' || $act === 'delete_inline') {
        $row = null;
        if ($act === 'delete') {
            $row = DB::one('SELECT a.*, p.user_id AS post_user_id, p.title
                            FROM ' . DB::table('attachments') . ' a
                            LEFT JOIN ' . DB::table('posts') . ' p ON p.id = a.post_id
                            WHERE a.id = ?', [$id]);
            if (!$row) {
                flash('文件不存在或已被删除', 'err');
                redirect(admin_url('postfiles', array_filter(['q' => $q])));
            }
        }
        if ($pid <= 0 && $row && !empty($row['post_id'])) {
            $pid = (int) $row['post_id'];
        }
        $post = $pid > 0 ? DB::one('SELECT * FROM ' . DB::table('posts') . ' WHERE id = ?', [$pid]) : null;
        $ownerIds = [];
        if ($row) {
            $ownerIds[] = (int) ($row['user_id'] ?? 0);
        }
        if ($post) {
            $ownerIds[] = (int) ($post['user_id'] ?? 0);
        }
        foreach (array_unique(array_filter($ownerIds)) as $ownerId) {
            $owner = user_by_id((int) $ownerId);
            if ($owner && !admin_can_delete_owner($owner)) {
                flash('该文件属于管理员或超级管理员,普通管理员不能删除', 'err');
                redirect(admin_url('postfiles', array_filter(['q' => $q])));
            }
        }

        if ($post) {
            $title = (string) $post['title'];
            post_delete((int) $post['id']);
            audit_log('delete_post_file', '帖子 #' . (int) $post['id'] . ($row ? ' · 附件 #' . (int) $row['id'] : ' · 正文媒体'));
            flash('已删除文件,并连同帖子《' . $title . '》一起删除');
        } elseif ($row) {
            if (!empty($row['path']) && strpos((string) $row['path'], 'uploads/') === 0) {
                delete_upload((string) $row['path']);
            }
            DB::delete('attachments', 'id = ?', [(int) $row['id']]);
            flash('已删除附件文件(该附件已脱离帖子)');
        } else {
            flash('帖子不存在或已被删除', 'err');
        }
        redirect(admin_url('postfiles', array_filter(['q' => $q])));
    }
    redirect(admin_url('postfiles'));
}

$q = trim((string) input('q', ''));
$where = ['1'];
$params = [];
if ($q !== '') {
    $where[] = '(a.name LIKE ? OR p.title LIKE ? OR au.username LIKE ? OR au.nickname LIKE ? OR pu.username LIKE ? OR pu.nickname LIKE ?)';
    array_push($params, '%' . $q . '%', '%' . $q . '%', '%' . $q . '%', '%' . $q . '%', '%' . $q . '%', '%' . $q . '%');
}
$whereSql = implode(' AND ', $where);
$total = (int) DB::val('SELECT COUNT(*) FROM ' . DB::table('attachments') . ' a
                        LEFT JOIN ' . DB::table('posts') . ' p ON p.id = a.post_id
                        LEFT JOIN ' . DB::table('users') . ' au ON au.id = a.user_id
                        LEFT JOIN ' . DB::table('users') . ' pu ON pu.id = p.user_id
                        WHERE ' . $whereSql, $params, 0);
$pager = paginate($total, 24);
$rows = DB::all('SELECT a.*, p.title, p.status AS post_status, p.user_id AS post_user_id, p.created_at AS post_created_at
                 FROM ' . DB::table('attachments') . ' a
                 LEFT JOIN ' . DB::table('posts') . ' p ON p.id = a.post_id
                 LEFT JOIN ' . DB::table('users') . ' au ON au.id = a.user_id
                 LEFT JOIN ' . DB::table('users') . ' pu ON pu.id = p.user_id
                 WHERE ' . $whereSql . '
                 ORDER BY a.id DESC LIMIT ' . $pager['limit'] . ' OFFSET ' . $pager['offset'], $params);

// 正文内嵌媒体:只在帖子内容中引用本站 uploads/ 文件时补充展示,避免和附件表重复。
$attachmentPaths = [];
foreach (DB::all('SELECT post_id, path FROM ' . DB::table('attachments')) as $att) {
    $attachmentPaths[(int) $att['post_id']][(string) $att['path']] = true;
}
$inlinePosts = DB::all('SELECT p.*, u.nickname, u.username, u.avatar, u.role, u.verified, u.verify_style, u.verify_title, u.verify_type
                        FROM ' . DB::table('posts') . ' p
                        LEFT JOIN ' . DB::table('users') . ' u ON u.id = p.user_id
                        WHERE p.content LIKE ? AND p.status IN (1,2,3)
                        ORDER BY p.id DESC LIMIT 150', ['%uploads/%']);
$inlineItems = [];
foreach ($inlinePosts as $p) {
    $paths = $attachmentPaths[(int) $p['id']] ?? [];
    foreach (postfile_inline_files($p, $paths) as $file) {
        $inlineItems[] = ['post' => $p, 'file' => $file];
    }
}
?>
<div class="admin-head">
  <div>
    <h1>帖子文件管理</h1>
    <p>共 <?= $total ?> 个帖子附件 · 图片、音乐、视频和任意文件都可在线预览;删除文件时会连同所属帖子一起删除。</p>
  </div>
  <form method="get" class="admin-search">
    <input type="hidden" name="page" value="admin"><input type="hidden" name="s" value="postfiles">
    <input type="search" name="q" value="<?= e($q) ?>" placeholder="搜索文件名 / 帖子 / 上传者">
    <button class="btn-mini" type="submit">搜索</button>
  </form>
</div>

<div class="admin-file-note">
  <strong>帖子文件管理</strong>
  <span>来源:帖子附件 / 正文内嵌媒体。管理员可删除普通用户和管理员的文件;超级管理员文件仅超级管理员可删除。删除后文件、附件记录和整篇帖子会一起移除。</span>
</div>

<section class="panel">
  <div class="panel-head row">
    <h2>帖子附件</h2>
    <span class="muted"><?= (int) $total ?> 个文件</span>
  </div>
  <?php if (!$rows): ?>
    <?php empty_state('还没有帖子附件', '用户发帖时在“附件上传”中选择的文件会出现在这里'); ?>
  <?php else: ?>
    <div class="postfile-grid">
      <?php foreach ($rows as $r): ?>
        <?php
          $author = user_by_id((int) ($r['user_id'] ?: $r['post_user_id']));
          $kind = postfile_kind_of((string) $r['name']);
          $authorSuper = is_superadmin($author);
          $manageable = $author && admin_can_delete_owner($author);
          $postTitle = (string) ($r['title'] ?? '');
          $downloadUrl = base_url('api.php') . '?action=attachment&id=' . (int) $r['id'];
        ?>
        <article class="postfile-card">
          <div class="postfile-preview">
            <?= postfile_preview_html((string) $r['path'], (string) $r['name'], $kind) ?>
          </div>
          <div class="postfile-body">
            <div class="postfile-title">
              <strong><?= e($r['name']) ?></strong>
              <span class="status-pill"><?= e(postfile_kind_label($kind)) ?></span>
              <span class="postfile-source">帖子附件</span>
            </div>
            <div class="postfile-owner">
              <?= $author ? avatar_html($author, 'at-avatar') : '<span class="at-avatar"></span>' ?>
              <span><b><?= e($author ? display_name($author) : '未知用户') ?></b><small><?= e($author ? '@' . $author['username'] : '') ?></small></span>
              <?php if ($authorSuper): ?><span class="tag lock">超级管理员</span><?php endif; ?>
            </div>
            <div class="postfile-meta">
              <span><?= e(format_size((int) $r['size'])) ?></span>
              <span><?= e(substr((string) $r['created_at'], 0, 16)) ?></span>
              <?php if ($postTitle !== ''): ?>
                <a href="<?= e(page_url('post', ['id' => (int) $r['post_id']])) ?>">所属帖子:<?= e(mb_substr($postTitle, 0, 28)) ?></a>
                <span class="status-pill <?= (int) ($r['post_status'] ?? 0) === 1 ? 'ok' : 'bad' ?>"><?= e(post_status_text((int) ($r['post_status'] ?? 0))) ?></span>
              <?php else: ?>
                <span class="muted">帖子已删除 · 附件成为孤立文件</span>
              <?php endif; ?>
            </div>
            <div class="postfile-actions">
              <a class="btn-mini ghost" href="<?= e($downloadUrl) ?>">下载原文件</a>
              <?php if ($postTitle !== ''): ?><a class="btn-mini ghost" href="<?= e(page_url('post', ['id' => (int) $r['post_id']])) ?>">打开帖子</a><?php endif; ?>
              <?php if ($manageable): ?>
                <form method="post" class="inline-form" data-confirm="删除这个文件并连同所属帖子一起删除?文件、附件记录和帖子都会移除,此操作不可恢复。">
                  <?= csrf_field() ?>
                  <input type="hidden" name="act" value="delete">
                  <input type="hidden" name="id" value="<?= (int) $r['id'] ?>">
                  <input type="hidden" name="q" value="<?= e($q) ?>">
                  <button class="link-btn danger" type="submit">删除文件及帖子</button>
                </form>
              <?php else: ?>
                <span class="muted">受保护 · 仅超级管理员可操作</span>
              <?php endif; ?>
            </div>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
    <?= pager_html($pager, 'admin', array_filter(['s' => 'postfiles', 'q' => $q !== '' ? $q : null])) ?>
  <?php endif; ?>
</section>

<?php if ($inlineItems): ?>
<section class="panel postfile-inline-panel">
  <div class="panel-head row">
    <h2>正文内嵌媒体</h2>
    <span class="muted"><?= count($inlineItems) ?> 个文件 · 来自帖子正文</span>
  </div>
  <div class="admin-file-note compact">
    <strong>正文文件提示</strong>
    <span>这些文件是在编辑器里直接插入帖子正文的图片、音乐或视频,不一定出现在附件表;删除任意一项同样会连同整篇帖子删除。</span>
  </div>
  <div class="postfile-grid">
    <?php foreach (array_slice($inlineItems, 0, 120) as $item): ?>
      <?php
        $p = $item['post'];
        $f = $item['file'];
        $author = user_by_id((int) $p['user_id']);
        $manageable = $author && admin_can_delete_owner($author);
        $kind = $f['kind'];
      ?>
      <article class="postfile-card inline">
        <div class="postfile-preview">
          <?= postfile_preview_html($f['path'], $f['name'], $kind) ?>
        </div>
        <div class="postfile-body">
          <div class="postfile-title">
            <strong><?= e($f['name']) ?></strong>
            <span class="status-pill"><?= e(postfile_kind_label($kind)) ?></span>
            <span class="postfile-source inline">正文内嵌</span>
          </div>
          <div class="postfile-owner">
            <?= $author ? avatar_html($author, 'at-avatar') : '<span class="at-avatar"></span>' ?>
            <span><b><?= e($author ? display_name($author) : '未知用户') ?></b><small><?= e($author ? '@' . $author['username'] : '') ?></small></span>
          </div>
          <div class="postfile-meta">
            <span><?= e(format_size((int) $f['size'])) ?></span>
            <span><?= e(substr((string) $p['created_at'], 0, 16)) ?></span>
            <a href="<?= e(page_url('post', ['id' => (int) $p['id']])) ?>">所属帖子:<?= e(mb_substr((string) $p['title'], 0, 28)) ?></a>
            <span class="status-pill <?= (int) $p['status'] === 1 ? 'ok' : 'bad' ?>"><?= e(post_status_text((int) $p['status'])) ?></span>
          </div>
          <div class="postfile-actions">
            <a class="btn-mini ghost" href="<?= e(page_url('post', ['id' => (int) $p['id']])) ?>">打开帖子</a>
            <?php if ($manageable): ?>
              <form method="post" class="inline-form" data-confirm="删除这个正文媒体并连同整篇帖子一起删除?此操作不可恢复。">
                <?= csrf_field() ?>
                <input type="hidden" name="act" value="delete_inline">
                <input type="hidden" name="pid" value="<?= (int) $p['id'] ?>">
                <button class="link-btn danger" type="submit">删除文件及帖子</button>
              </form>
            <?php else: ?>
              <span class="muted">受保护 · 仅超级管理员可操作</span>
            <?php endif; ?>
          </div>
        </div>
      </article>
    <?php endforeach; ?>
  </div>
</section>
<?php endif; ?>
主题