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 个站点。

pages/ip_history.php 13.7 KB · 发布包 v6.25.232
下载
<?php
/**
 * 登录记录 · 每个用户只能查看本人的成功登录记录
 * 展示 IP、设备 ID、设备码、详细地址,不展示密码、令牌或其他账号数据。
 */
require_login();
$me = current_user();
$GLOBALS['PAGE'] = 'ip_history';

$viewUid = (int) input('uid', 0);
$isOther = $viewUid > 0 && $viewUid !== (int) $me['id'];
if ($isOther && !is_admin($me)) {
    flash('只有管理员可以查看其他用户的登录记录', 'err');
    redirect(page_url('profile'));
}
$target = $isOther ? user_by_id($viewUid) : $me;
if (!$target) {
    flash('用户不存在', 'err');
    redirect(is_admin($me) ? page_url('admin', ['s' => 'users']) : page_url('profile'));
}

$uid = (int) $target['id'];
$currentIp = client_ip();
$currentDeviceId = client_device_id();
$currentDeviceCode = client_device_code();
$currentDeviceLabel = client_device_label();
$currentAddress = ip_location_detail_text($currentIp);

$table = DB::table('login_logs');
$rows = DB::all(
    'SELECT ip, device_id, device_code, MAX(ua) AS ua, MAX(address) AS address,'
    . ' COUNT(*) AS hits, MIN(created_at) AS first_at, MAX(created_at) AS last_at, MAX(id) AS last_id'
    . ' FROM ' . $table
    . ' WHERE user_id = ? AND result = ? AND ip <> ?'
    . ' GROUP BY ip, device_id, device_code'
    . ' ORDER BY last_id DESC LIMIT 60',
    [$uid, 'success', '']
);

$totalLogins = (int) DB::val('SELECT COUNT(*) FROM ' . $table . ' WHERE user_id = ? AND result = ? AND ip <> ?', [$uid, 'success', ''], 0);
$uniqueIps = count($rows);
$uniqueDevices = 0;
$deviceKeys = [];
foreach ($rows as $row) {
    $key = trim((string) $row['device_id']);
    if ($key === '') {
        $key = 'legacy:' . substr(sha1((string) $row['ip'] . '|' . (string) $row['ua']), 0, 16);
    }
    if (!isset($deviceKeys[$key])) {
        $deviceKeys[$key] = true;
        $uniqueDevices++;
    }
}

$displayRows = [];
foreach ($rows as $row) {
    $ip = trim((string) $row['ip']);
    $ua = (string) ($row['ua'] ?? '');
    $deviceId = trim((string) ($row['device_id'] ?? ''));
    $deviceCode = trim((string) ($row['device_code'] ?? ''));
    $legacy = false;
    if ($deviceId === '') {
        $legacy = true;
        $deviceId = 'LEGACY-' . substr(sha1($ip . '|' . $ua), 0, 16);
        $deviceCode = device_code_from_id($deviceId);
    } elseif ($deviceCode === '') {
        $deviceCode = device_code_from_id($deviceId);
    }

    $address = trim((string) ($row['address'] ?? ''));
    if ($address === '') {
        $address = ip_location_detail_text($ip);
        try {
            DB::run(
                'UPDATE ' . $table . " SET address = ? WHERE user_id = ? AND ip = ? AND address = ''",
                [mb_substr($address, 0, 255), $uid, $ip]
            );
            if ($legacy) {
                DB::run(
                    'UPDATE ' . $table . " SET device_id = ?, device_code = ? WHERE user_id = ? AND ip = ? AND device_id = ''",
                    [$deviceId, $deviceCode, $uid, $ip]
                );
            }
        } catch (Throwable $e) {
            // 回写失败不影响本次展示
        }
    }

    $loc = ip_location($ip);
    $province = trim((string) ($loc['region'] ?? ''));
    $city = trim((string) ($loc['city'] ?? ''));
    $row['ip'] = $ip;
    $row['device_id'] = $deviceId;
    $row['device_code'] = $deviceCode;
    $row['device_label'] = client_device_label($ua);
    $row['province'] = $province !== '' ? $province : '未知';
    $row['city'] = $city !== '' ? $city : $row['province'];
    $row['address'] = $address;
    $row['legacy'] = $legacy;
    $displayRows[] = $row;
}

$eventRows = DB::all(
    'SELECT * FROM ' . $table . ' WHERE user_id = ? AND result = ? AND ip <> ? ORDER BY id DESC LIMIT 100',
    [$uid, 'success', '']
);
foreach ($eventRows as &$event) {
    $eventIp = trim((string) ($event['ip'] ?? ''));
    $eventDeviceId = trim((string) ($event['device_id'] ?? ''));
    if ($eventDeviceId === '') {
        $eventDeviceId = 'LEGACY-' . substr(sha1($eventIp . '|' . (string) ($event['ua'] ?? '')), 0, 16);
    }
    $event['device_label'] = client_device_label((string) ($event['ua'] ?? ''));
    $event['province'] = trim((string) ($loc = ip_location($eventIp))['region'] ?? '');
    $event['city'] = trim((string) ($loc['city'] ?? ''));
    if ($event['province'] === '') { $event['province'] = '未知'; }
    if ($event['city'] === '') { $event['city'] = $event['province']; }
    $event['address'] = trim((string) ($event['address'] ?? ''));
    if ($event['address'] === '') { $event['address'] = ip_location_detail_text($eventIp); }
}
unset($event);

$copyText = function (array $row) {
    return "IP:" . $row['ip'] . "\n"
        . "设备 ID:" . $row['device_id'] . "\n"
        . "设备码:" . $row['device_code'] . "\n"
        . "设备型号:" . $row['device_label'] . "\n"
        . "登录省份:" . $row['province'] . "\n"
        . "登录城市:" . $row['city'] . "\n"
        . "详细地址:" . $row['address'] . "\n"
        . "首次登录:" . $row['first_at'] . "\n"
        . "最近登录:" . $row['last_at'] . "\n"
        . "登录次数:" . (int) $row['hits'];
};

render_header('登录记录');
?>
<section class="panel ip-history-hero">
  <div class="ip-history-head">
    <div>
      <span class="hero-kicker">ACCOUNT SECURITY · LOGIN HISTORY</span>
      <h1>登录记录</h1>
      <p class="muted"><?= $isOther ? '这里记录该用户账号的成功登录,包含 IP、设备型号、设备 ID、设备码、登录省份与城市及详细地址。' : '这里记录你本人账号的成功登录,包含 IP、设备型号、设备 ID、设备码、登录省份与城市及详细地址。' ?>点击任意 IP、设备 ID、设备码或详细地址即可自动复制。</p>
    </div>
    <a class="btn-ghost" href="<?= e($isOther ? page_url('admin', ['s' => 'users']) : page_url('profile')) ?>"><?= $isOther ? '返回用户管理' : '返回个人资料' ?></a>
  </div>

  <div class="ip-history-account">
    <span class="ip-history-avatar"><?= avatar_html($target, 'ip-history-avatar-img') ?></span>
    <div class="ip-history-account-copy">
      <strong><?= e(display_name($target)) ?><?= verify_badge($target) ?></strong>
      <span>@<?= e((string) $target['username']) ?> · 账号 ID <?= (int) $target['id'] ?></span>
      <span class="ip-history-role"><?= e(role_label((string) ($target['role'] ?? 'user'))) ?></span>
    </div>
    <div class="ip-history-account-meta">
      <span><?= $isOther ? '最近登录设备' : '当前设备' ?>:<?= e($isOther ? (string) ($displayRows[0]['device_label'] ?? '暂无记录') : $currentDeviceLabel) ?></span>
      <span>累计成功登录:<b><?= (int) $totalLogins ?></b> 次</span>
    </div>
  </div>

  <?php if (!$isOther): ?>
  <div class="ip-history-current">
    <div class="ip-history-current-cell">
      <label>当前 IP</label>
      <button type="button" class="ip-history-copy" data-copy="<?= e($currentIp) ?>"><span><?= e($currentIp) ?></span><em>点击复制</em></button>
    </div>
    <div class="ip-history-current-cell">
      <label>当前设备 ID</label>
      <button type="button" class="ip-history-copy" data-copy="<?= e($currentDeviceId) ?>"><span><?= e($currentDeviceId) ?></span><em>点击复制</em></button>
    </div>
    <div class="ip-history-current-cell">
      <label>当前设备码</label>
      <button type="button" class="ip-history-copy" data-copy="<?= e($currentDeviceCode) ?>"><span><?= e($currentDeviceCode) ?></span><em>点击复制</em></button>
    </div>
    <div class="ip-history-current-cell is-wide">
      <label>当前详细地址</label>
      <button type="button" class="ip-history-copy" data-copy="<?= e($currentAddress) ?>"><span><?= e($currentAddress) ?></span><em>点击复制</em></button>
    </div>
  </div>
  <?php endif; ?>
</section>

<section class="panel ip-history-panel">
  <div class="ip-history-panel-head">
    <div>
      <h2 class="section-title">登录过的 IP 与设备</h2>
      <p class="muted">同一 IP 与同一设备会自动合并,显示首次与最近一次登录时间。</p>
    </div>
    <div class="ip-history-stats">
      <span><b><?= count($displayRows) ?></b> 组记录</span>
      <span><b><?= (int) $uniqueDevices ?></b> 台设备</span>
    </div>
  </div>

  <?php if (!$displayRows): ?>
    <div class="ip-history-empty">
      <strong>还没有登录记录</strong>
      <p class="muted">升级后首次登录会开始记录 IP、设备 ID、设备码与详细地址。</p>
    </div>
  <?php else: ?>
    <div class="ip-history-list">
      <?php foreach ($displayRows as $row): ?>
        <article class="ip-history-row">
          <div class="ip-history-row-head">
            <div class="ip-history-ip">
              <span class="ip-history-label">IP 地址</span>
              <button type="button" class="ip-history-copy is-strong" data-copy="<?= e($row['ip']) ?>"><span><?= e($row['ip']) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-row-badges">
              <?php if (!empty($row['legacy'])): ?><span class="ip-history-badge is-legacy">升级前记录</span><?php endif; ?>
              <span class="ip-history-badge">登录 <?= (int) $row['hits'] ?> 次</span>
            </div>
          </div>

          <div class="ip-history-grid">
            <div class="ip-history-cell">
              <label>设备 ID</label>
              <button type="button" class="ip-history-copy" data-copy="<?= e($row['device_id']) ?>"><span><?= e($row['device_id']) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-cell">
              <label>设备码</label>
              <button type="button" class="ip-history-copy" data-copy="<?= e($row['device_code']) ?>"><span><?= e($row['device_code']) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-cell">
              <label>设备型号</label>
              <span class="ip-history-value"><?= e($row['device_label']) ?></span>
            </div>
            <div class="ip-history-cell">
              <label>登录省份</label>
              <span class="ip-history-value"><?= e($row['province']) ?></span>
            </div>
            <div class="ip-history-cell">
              <label>登录城市</label>
              <span class="ip-history-value"><?= e($row['city']) ?></span>
            </div>
            <div class="ip-history-cell is-wide">
              <label>详细地址</label>
              <button type="button" class="ip-history-copy" data-copy="<?= e($row['address']) ?>"><span><?= e($row['address']) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-cell">
              <label>首次登录</label>
              <span class="ip-history-value"><?= e((string) $row['first_at']) ?></span>
            </div>
            <div class="ip-history-cell">
              <label>最近登录</label>
              <span class="ip-history-value"><?= e((string) $row['last_at']) ?></span>
            </div>
          </div>

          <div class="ip-history-row-foot">
            <button type="button" class="btn-mini ghost ip-history-copy-all" data-copy="<?= e($copyText($row)) ?>">复制整条记录</button>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>

  <p class="muted ip-history-note">设备 ID 来自浏览器长期 Cookie;清除浏览器数据后会生成新的设备 ID。升级前的旧记录没有设备 ID,会按 IP + 浏览器归档并标注“升级前记录”。</p>
</section>

<section class="panel ip-history-panel">
  <div class="ip-history-panel-head">
    <div>
      <h2 class="section-title">每次登录明细</h2>
      <p class="muted">最近 100 次成功登录逐条列出,包含 IP、设备型号、设备 ID、登录省份与城市。点击 IP 或设备 ID 可复制。</p>
    </div>
  </div>
  <?php if (!$eventRows): ?>
    <div class="ip-history-empty"><strong>还没有逐条登录记录</strong><p class="muted">升级后首次登录会开始记录。</p></div>
  <?php else: ?>
    <div class="ip-history-list">
      <?php foreach ($eventRows as $event): ?>
        <article class="ip-history-row">
          <div class="ip-history-row-head">
            <div class="ip-history-ip">
              <span class="ip-history-label">IP 地址</span>
              <button type="button" class="ip-history-copy is-strong" data-copy="<?= e($event['ip']) ?>"><span><?= e($event['ip']) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-row-badges"><span class="ip-history-badge"><?= e((string) ($event['created_at'] ?? '')) ?></span></div>
          </div>
          <div class="ip-history-grid">
            <div class="ip-history-cell">
              <label>设备型号</label>
              <span class="ip-history-value"><?= e($event['device_label']) ?></span>
            </div>
            <div class="ip-history-cell">
              <label>设备 ID</label>
              <button type="button" class="ip-history-copy" data-copy="<?= e($eventDeviceId) ?>"><span><?= e($eventDeviceId) ?></span><em>点击复制</em></button>
            </div>
            <div class="ip-history-cell">
              <label>登录省份</label>
              <span class="ip-history-value"><?= e($event['province']) ?></span>
            </div>
            <div class="ip-history-cell">
              <label>登录城市</label>
              <span class="ip-history-value"><?= e($event['city']) ?></span>
            </div>
            <div class="ip-history-cell is-wide">
              <label>详细地址</label>
              <button type="button" class="ip-history-copy" data-copy="<?= e($event['address']) ?>"><span><?= e($event['address']) ?></span><em>点击复制</em></button>
            </div>
          </div>
        </article>
      <?php endforeach; ?>
    </div>
  <?php endif; ?>
</section>
<?php render_footer(); ?>
主题