Address system information and license review suggestions

This commit is contained in:
2026-11-25 22:16:38 +08:00
parent 080f146fef
commit df3722ae36
11 changed files with 78 additions and 39 deletions
+2
View File
@@ -9,6 +9,8 @@ use RuntimeException;
final class Assets final class Assets
{ {
public const DEFAULT_ICON = '/assets/system/laysite-mark.webp'; public const DEFAULT_ICON = '/assets/system/laysite-mark.webp';
public const JQUERY = '/assets/jquery-4.0.0.min.js';
public const TOAST_UI_EDITOR = '/assets/vendor/toastui/toastui-editor.min.js';
public static function cdn(string $value): string public static function cdn(string $value): string
{ {
+15 -7
View File
@@ -24,18 +24,21 @@ final class Licenses
public static function dependencies(): array public static function dependencies(): array
{ {
$root = dirname(__DIR__, 2); $root = dirname(__DIR__, 2);
$jquery = @file_get_contents($root . '/public' . Assets::JQUERY, length: 256);
$editor = @file_get_contents($root . '/public' . Assets::TOAST_UI_EDITOR);
$version = '([0-9][0-9A-Za-z.+-]*)';
$entries = [ $entries = [
'jquery' => ['name' => 'jQuery', 'version' => '4.0.0', 'license' => 'MIT', 'jquery' => ['name' => 'jQuery', 'version' => self::declaredVersion($jquery, '~jQuery v' . $version . '~'), 'license' => 'MIT',
'url' => 'https://jquery.com/', 'file' => $root . '/public/assets/jquery-LICENSE.txt'], 'url' => 'https://jquery.com/', 'file' => $root . '/public/assets/jquery-LICENSE.txt'],
'toast-ui-editor' => ['name' => 'TOAST UI Editor', 'version' => '3.2.2', 'license' => 'MIT', 'toast-ui-editor' => ['name' => 'TOAST UI Editor', 'version' => self::declaredVersion($editor, '~@version\h+' . $version . '~'), 'license' => 'MIT',
'url' => 'https://github.com/nhn/tui.editor', 'file' => $root . '/public/assets/vendor/toastui/LICENSE'], 'url' => 'https://github.com/nhn/tui.editor', 'file' => $root . '/public/assets/vendor/toastui/LICENSE'],
'geist' => ['name' => 'Geist', 'version' => '', 'license' => 'OFL-1.1', 'geist' => ['name' => 'Geist', 'license' => 'OFL-1.1',
'url' => 'https://github.com/vercel/geist-font', 'file' => $root . '/public/assets/geist-OFL.txt'], 'url' => 'https://github.com/vercel/geist-font', 'file' => $root . '/public/assets/geist-OFL.txt'],
'dompurify' => ['name' => 'DOMPurify', 'version' => '2.3.3', 'license' => 'Apache-2.0 / MPL-2.0', 'dompurify' => ['name' => 'DOMPurify', 'version' => self::declaredVersion($editor, '~@license DOMPurify\h+' . $version . '~'), 'license' => 'Apache-2.0 / MPL-2.0',
'url' => 'https://github.com/cure53/DOMPurify/tree/2.3.3', 'file' => null], 'url' => 'https://github.com/cure53/DOMPurify', 'file' => null],
'prosemirror' => ['name' => 'ProseMirror', 'version' => '', 'license' => 'MIT', 'prosemirror' => ['name' => 'ProseMirror', 'license' => 'MIT',
'url' => 'https://prosemirror.net/', 'file' => null], 'url' => 'https://prosemirror.net/', 'file' => null],
'tslib' => ['name' => 'tslib', 'version' => '', 'license' => '0BSD', 'tslib' => ['name' => 'tslib', 'license' => '0BSD',
'url' => 'https://github.com/microsoft/tslib', 'file' => null], 'url' => 'https://github.com/microsoft/tslib', 'file' => null],
]; ];
$lock = @file_get_contents($root . '/composer.lock'); $lock = @file_get_contents($root . '/composer.lock');
@@ -57,6 +60,11 @@ final class Licenses
return $entries; return $entries;
} }
private static function declaredVersion(string|false $source, string $pattern): string
{
return $source !== false && preg_match($pattern, $source, $match) ? $match[1] : '';
}
public static function text(string $key): ?string public static function text(string $key): ?string
{ {
$path = self::dependencies()[$key]['file'] ?? null; $path = self::dependencies()[$key]['file'] ?? null;
+4 -1
View File
@@ -92,7 +92,10 @@ final class Media
{ {
$bytes = Database::connect()->query('SELECT COALESCE(SUM(size), 0) FROM media')->fetchColumn(); $bytes = Database::connect()->query('SELECT COALESCE(SUM(size), 0) FROM media')->fetchColumn();
$directory = self::directory(); $directory = self::directory();
$free = is_dir($directory) ? @disk_free_space($directory) : false; while ($directory !== '' && $directory !== dirname($directory) && !@is_dir($directory)) {
$directory = dirname($directory);
}
$free = @is_dir($directory) ? @disk_free_space($directory) : false;
return [ return [
'size' => SystemInfo::bytes((int) $bytes), 'size' => SystemInfo::bytes((int) $bytes),
+5 -2
View File
@@ -38,7 +38,7 @@ final class SystemInfo
{ {
return [ return [
'cpu' => PHP_OS_FAMILY === 'Linux' ? self::cpu() : null, 'cpu' => PHP_OS_FAMILY === 'Linux' ? self::cpu() : null,
'memory' => PHP_OS_FAMILY === 'Linux' ? self::memory() : null, 'memory' => self::memory(),
]; ];
} }
@@ -58,8 +58,11 @@ final class SystemInfo
return ['total' => array_sum(array_slice($fields, 1, 8)), 'idle' => $fields[4] + $fields[5]]; return ['total' => array_sum(array_slice($fields, 1, 8)), 'idle' => $fields[4] + $fields[5]];
} }
private static function memory(): ?string public static function memory(): ?string
{ {
if (PHP_OS_FAMILY !== 'Linux') {
return null;
}
$info = @file_get_contents('/proc/meminfo'); $info = @file_get_contents('/proc/meminfo');
if ($info === false || !preg_match('/^MemTotal:\s+(\d+) kB$/m', $info, $total) if ($info === false || !preg_match('/^MemTotal:\s+(\d+) kB$/m', $info, $total)
|| !preg_match('/^MemAvailable:\s+(\d+) kB$/m', $info, $available) || (int) $total[1] === 0) { || !preg_match('/^MemAvailable:\s+(\d+) kB$/m', $info, $available) || (int) $total[1] === 0) {
+1 -1
View File
@@ -145,7 +145,7 @@ class AdminController
'运行平台' => SystemInfo::platform(), '运行平台' => SystemInfo::platform(),
PHP_OS_FAMILY === 'Linux' ? 'Linux 内核' : '系统版本' => php_uname('r'), PHP_OS_FAMILY === 'Linux' ? 'Linux 内核' : '系统版本' => php_uname('r'),
], ],
'metrics' => SystemInfo::sample(), 'memory' => SystemInfo::memory(),
'mediaUsage' => Media::usage(), 'mediaUsage' => Media::usage(),
'thirdPartyLicenses' => Licenses::dependencies(), 'thirdPartyLicenses' => Licenses::dependencies(),
]); ]);
+2 -1
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Middleware; namespace App\Middleware;
use App\Content\Assets;
use Webman\Http\Request; use Webman\Http\Request;
use Webman\Http\Response; use Webman\Http\Response;
use Webman\MiddlewareInterface; use Webman\MiddlewareInterface;
@@ -16,7 +17,7 @@ final class PublicAssets implements MiddlewareInterface
$path = $request->path(); $path = $request->path();
if (str_starts_with($path, '/assets/')) { if (str_starts_with($path, '/assets/')) {
$response->withHeader('Access-Control-Allow-Origin', '*'); $response->withHeader('Access-Control-Allow-Origin', '*');
if ($path === '/assets/jquery-4.0.0.min.js') { if ($path === Assets::JQUERY) {
$response->withHeader('Cache-Control', 'public, max-age=31536000, immutable'); $response->withHeader('Cache-Control', 'public, max-age=31536000, immutable');
} }
} }
+10 -11
View File
@@ -6,26 +6,23 @@ $e = View::escape(...);
?> ?>
<header class="admin-heading"><h1><?= $e($title) ?></h1></header> <header class="admin-heading"><h1><?= $e($title) ?></h1></header>
<div class="admin-about"> <div class="admin-about">
<section class="admin-panel admin-system-info" aria-labelledby="system-info-title" data-about-metrics <section class="admin-panel admin-system-info" aria-labelledby="system-info-title">
data-cpu-total="<?= $e($metrics['cpu']['total'] ?? '') ?>" data-cpu-idle="<?= $e($metrics['cpu']['idle'] ?? '') ?>">
<h2 id="system-info-title">版本与运行环境</h2> <h2 id="system-info-title">版本与运行环境</h2>
<dl> <dl>
<div><dt>LaySite</dt><dd><?= $e($laysiteVersion) ?></dd></div> <div><dt>LaySite</dt><dd><?= $e($laysiteVersion) ?></dd></div>
<div><dt>CPU 占用</dt><dd data-about-cpu>暂不可用</dd></div>
<div><dt>内存占用</dt><dd data-about-memory><?= $e($metrics['memory'] ?? '暂不可用') ?></dd></div>
<div><dt>媒体库占用</dt><dd><?= $e($mediaUsage['size']) ?><small>已登记文件,含归档资源</small></dd></div>
<?php if ($mediaUsage['free'] !== null): ?>
<div><dt>媒体磁盘可用</dt><dd><?= $e($mediaUsage['free']) ?></dd></div>
<?php endif ?>
</dl> </dl>
<p class="field-note">CPU 和内存显示当前 Linux 系统可见的用量,页面可见时自动更新。</p> <details class="admin-options admin-about-details" data-about-metrics>
<details class="admin-options admin-about-details">
<summary>运行环境详情</summary> <summary>运行环境详情</summary>
<dl> <dl>
<?php foreach ($systemInfo as $name => $value): ?> <?php foreach ($systemInfo as $name => $value): ?>
<div><dt><?= $e($name) ?></dt><dd><?= $e($value) ?></dd></div> <div><dt><?= $e($name) ?></dt><dd><?= $e($value) ?></dd></div>
<?php endforeach ?> <?php endforeach ?>
<div><dt>CPU 占用</dt><dd data-about-cpu>暂不可用</dd></div>
<div><dt>内存占用</dt><dd data-about-memory><?= $e($memory ?? '暂不可用') ?></dd></div>
<div><dt>媒体库占用</dt><dd><?= $e($mediaUsage['size']) ?><small>已登记文件,含归档资源</small></dd></div>
<div><dt>媒体磁盘可用</dt><dd><?= $e($mediaUsage['free'] ?? '暂不可用') ?></dd></div>
</dl> </dl>
<p class="field-note">CPU 和内存显示 Linux 系统可见的用量,在详情展开且页面可见时自动更新。</p>
</details> </details>
</section> </section>
<section class="admin-panel admin-licenses" aria-labelledby="system-licenses-title"> <section class="admin-panel admin-licenses" aria-labelledby="system-licenses-title">
@@ -42,12 +39,14 @@ $e = View::escape(...);
<div> <div>
<?php if ($project['url'] !== ''): ?><a href="<?= $e($project['url']) ?>" target="_blank" rel="noopener noreferrer"><?= $e($project['name']) ?></a> <?php if ($project['url'] !== ''): ?><a href="<?= $e($project['url']) ?>" target="_blank" rel="noopener noreferrer"><?= $e($project['name']) ?></a>
<?php else: ?><?= $e($project['name']) ?><?php endif ?> <?php else: ?><?= $e($project['name']) ?><?php endif ?>
<?php if ($project['version'] !== ''): ?><small><?= $e($project['version']) ?></small><?php endif ?> <?php if (!empty($project['version'])): ?><small><?= $e($project['version']) ?></small><?php endif ?>
</div> </div>
<div> <div>
<span><?= $e($project['license']) ?></span> <span><?= $e($project['license']) ?></span>
<?php if ($project['file'] !== null): ?> <?php if ($project['file'] !== null): ?>
<a href="/admin/about/license/<?= $e($key) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= $e('查看 ' . $project['name'] . ' 的许可证全文') ?>">许可全文</a> <a href="/admin/about/license/<?= $e($key) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= $e('查看 ' . $project['name'] . ' 的许可证全文') ?>">许可全文</a>
<?php elseif ($project['url'] !== ''): ?>
<a href="<?= $e($project['url']) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= $e('前往 ' . $project['name'] . ' 项目主页查看许可证') ?>">见项目主页</a>
<?php endif ?> <?php endif ?>
</div> </div>
</li> </li>
+2 -2
View File
@@ -21,7 +21,7 @@ $active = match (true) {
<title><?= $e($title) ?> CMS</title> <title><?= $e($title) ?> CMS</title>
<link rel="stylesheet" href="/assets/site.css"> <link rel="stylesheet" href="/assets/site.css">
<link rel="stylesheet" href="/assets/admin.css"> <link rel="stylesheet" href="/assets/admin.css">
<script src="/assets/jquery-4.0.0.min.js" defer></script> <script src="<?= $e(Assets::JQUERY) ?>" defer></script>
<script src="/assets/admin.js" defer></script> <script src="/assets/admin.js" defer></script>
<script src="/assets/studio.js" defer></script> <script src="/assets/studio.js" defer></script>
<?php if ($adminPath === '/admin/about'): ?> <?php if ($adminPath === '/admin/about'): ?>
@@ -33,7 +33,7 @@ $active = match (true) {
<?php endif ?> <?php endif ?>
<?php if (!empty($articleEditor)): ?> <?php if (!empty($articleEditor)): ?>
<link rel="stylesheet" href="/assets/vendor/toastui/toastui-editor.min.css"> <link rel="stylesheet" href="/assets/vendor/toastui/toastui-editor.min.css">
<script src="/assets/vendor/toastui/toastui-editor.min.js" defer></script> <script src="<?= $e(Assets::TOAST_UI_EDITOR) ?>" defer></script>
<script src="/assets/vendor/toastui/zh-cn.js" defer></script> <script src="/assets/vendor/toastui/zh-cn.js" defer></script>
<script src="/assets/article-editor.js" defer></script> <script src="/assets/article-editor.js" defer></script>
<?php endif ?> <?php endif ?>
+1 -1
View File
@@ -47,7 +47,7 @@ $showFooter = $page['visibility']['footer'] ?? true;
<?php if (!empty($canvas) || str_contains($content . ($headerMarkup ?? '') . ($footerMarkup ?? ''), 'studio-style-')): ?><link rel="stylesheet" href="<?= $e(CanvasStyle::stylesheetUrl()) ?>"><?php endif ?> <?php if (!empty($canvas) || str_contains($content . ($headerMarkup ?? '') . ($footerMarkup ?? ''), 'studio-style-')): ?><link rel="stylesheet" href="<?= $e(CanvasStyle::stylesheetUrl()) ?>"><?php endif ?>
<?php if ($theme !== ''): ?><link rel="stylesheet" href="/assets/page-themes.css"><?php endif ?> <?php if ($theme !== ''): ?><link rel="stylesheet" href="/assets/page-themes.css"><?php endif ?>
<?php if ($palette): ?><link rel="stylesheet" data-page-palette href="/palette/<?= $e($palette['id']) ?>.css?v=<?= $e($palette['version']) ?>"><?php endif ?> <?php if ($palette): ?><link rel="stylesheet" data-page-palette href="/palette/<?= $e($palette['id']) ?>.css?v=<?= $e($palette['version']) ?>"><?php endif ?>
<script src="/assets/jquery-4.0.0.min.js" defer></script> <script src="<?= $e(Assets::JQUERY) ?>" defer></script>
<script src="/assets/site.js" defer></script> <script src="/assets/site.js" defer></script>
</head> </head>
<body> <body>
+34 -12
View File
@@ -7,22 +7,26 @@ jQuery(function ($) {
const $cpu = $metrics.find('[data-about-cpu]'); const $cpu = $metrics.find('[data-about-cpu]');
const $memory = $metrics.find('[data-about-memory]'); const $memory = $metrics.find('[data-about-memory]');
const percent = new Intl.NumberFormat('zh-CN', {style: 'percent', maximumFractionDigits: 1}); const percent = new Intl.NumberFormat('zh-CN', {style: 'percent', maximumFractionDigits: 1});
const {cpuTotal, cpuIdle} = $metrics[0].dataset; let previous = null;
let previous = cpuTotal === '' ? null : {total: Number(cpuTotal), idle: Number(cpuIdle)}; let timer = null;
let pending = null;
let sessionExpired = false; let sessionExpired = false;
if (previous) $cpu.text('采样中…');
function active() {
return $metrics[0].open && !document.hidden && !sessionExpired;
}
function poll() { function poll() {
if (document.hidden) { timer = null;
previous = null; if (!active()) return;
window.setTimeout(poll, 5000);
return;
}
$.ajax({url: '/admin/about/metrics', dataType: 'json', timeout: 4000}) let delay = 5000;
pending = $.ajax({url: '/admin/about/metrics', dataType: 'json', timeout: 4000});
pending
.done(({cpu, memory}) => { .done(({cpu, memory}) => {
if (!cpu || !previous || cpu.total <= previous.total) { if (!cpu || !previous || cpu.total <= previous.total) {
$cpu.text(cpu ? '采样中…' : '暂不可用'); $cpu.text(cpu ? '采样中…' : '暂不可用');
if (cpu) delay = 1000;
} else { } else {
const total = cpu.total - previous.total; const total = cpu.total - previous.total;
const idle = cpu.idle - previous.idle; const idle = cpu.idle - previous.idle;
@@ -31,7 +35,8 @@ jQuery(function ($) {
previous = cpu; previous = cpu;
$memory.text(memory ?? '暂不可用'); $memory.text(memory ?? '暂不可用');
}) })
.fail(({status}) => { .fail(({status}, reason) => {
if (reason === 'abort') return;
previous = null; previous = null;
sessionExpired = status === 401; sessionExpired = status === 401;
const message = sessionExpired ? '登录已过期' : '暂不可用'; const message = sessionExpired ? '登录已过期' : '暂不可用';
@@ -39,9 +44,26 @@ jQuery(function ($) {
$memory.text(message); $memory.text(message);
}) })
.always(() => { .always(() => {
if (!sessionExpired) window.setTimeout(poll, 5000); pending = null;
if (active()) timer = window.setTimeout(poll, delay);
}); });
} }
window.setTimeout(poll, 1000); function sync() {
if (active()) {
if (timer === null && pending === null) {
$cpu.text('采样中…');
poll();
}
return;
}
window.clearTimeout(timer);
timer = null;
previous = null;
pending?.abort();
}
$metrics.on('toggle', sync);
$(document).on('visibilitychange', sync);
sync();
}); });
+2 -1
View File
@@ -13,7 +13,8 @@
.admin-system-info dt { color: var(--muted); } .admin-system-info dt { color: var(--muted); }
.admin-system-info dd { margin: 0; font-family: var(--mono); overflow-wrap: anywhere; } .admin-system-info dd { margin: 0; font-family: var(--mono); overflow-wrap: anywhere; }
.admin-system-info dd small { display: block; font-family: var(--sans); color: var(--muted); } .admin-system-info dd small { display: block; font-family: var(--sans); color: var(--muted); }
.admin-system-info .field-note { margin-block-end: 1rem; } .admin-system-info .admin-about-details { margin-block-start: .75rem; }
.admin-system-info .field-note { margin-block: .5rem 0; }
.admin-about-details { background: var(--paper); } .admin-about-details { background: var(--paper); }
.admin-license-project { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: .65rem 1rem; margin-bottom: 1rem; } .admin-license-project { display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between; gap: .65rem 1rem; margin-bottom: 1rem; }
.admin-license-project p { margin: .2rem 0 0; color: var(--muted); } .admin-license-project p { margin: .2rem 0 0; color: var(--muted); }