diff --git a/app/Content/Assets.php b/app/Content/Assets.php index 73bfc7c..e5ee0a9 100644 --- a/app/Content/Assets.php +++ b/app/Content/Assets.php @@ -9,6 +9,8 @@ use RuntimeException; final class Assets { 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 { diff --git a/app/Content/Licenses.php b/app/Content/Licenses.php index d77b49f..ecefc46 100644 --- a/app/Content/Licenses.php +++ b/app/Content/Licenses.php @@ -24,18 +24,21 @@ final class Licenses public static function dependencies(): array { $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 = [ - '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'], - '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'], - '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'], - 'dompurify' => ['name' => 'DOMPurify', 'version' => '2.3.3', 'license' => 'Apache-2.0 / MPL-2.0', - 'url' => 'https://github.com/cure53/DOMPurify/tree/2.3.3', 'file' => null], - 'prosemirror' => ['name' => 'ProseMirror', 'version' => '', 'license' => 'MIT', + 'dompurify' => ['name' => 'DOMPurify', 'version' => self::declaredVersion($editor, '~@license DOMPurify\h+' . $version . '~'), 'license' => 'Apache-2.0 / MPL-2.0', + 'url' => 'https://github.com/cure53/DOMPurify', 'file' => null], + 'prosemirror' => ['name' => 'ProseMirror', 'license' => 'MIT', '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], ]; $lock = @file_get_contents($root . '/composer.lock'); @@ -57,6 +60,11 @@ final class Licenses 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 { $path = self::dependencies()[$key]['file'] ?? null; diff --git a/app/Content/Media.php b/app/Content/Media.php index 8a7b1e1..d6f61cc 100644 --- a/app/Content/Media.php +++ b/app/Content/Media.php @@ -92,7 +92,10 @@ final class Media { $bytes = Database::connect()->query('SELECT COALESCE(SUM(size), 0) FROM media')->fetchColumn(); $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 [ 'size' => SystemInfo::bytes((int) $bytes), diff --git a/app/Content/SystemInfo.php b/app/Content/SystemInfo.php index ef70646..342b50e 100644 --- a/app/Content/SystemInfo.php +++ b/app/Content/SystemInfo.php @@ -38,7 +38,7 @@ final class SystemInfo { return [ '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]]; } - private static function memory(): ?string + public static function memory(): ?string { + if (PHP_OS_FAMILY !== 'Linux') { + return null; + } $info = @file_get_contents('/proc/meminfo'); 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) { diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index 1e645c9..887a60f 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -145,7 +145,7 @@ class AdminController '运行平台' => SystemInfo::platform(), PHP_OS_FAMILY === 'Linux' ? 'Linux 内核' : '系统版本' => php_uname('r'), ], - 'metrics' => SystemInfo::sample(), + 'memory' => SystemInfo::memory(), 'mediaUsage' => Media::usage(), 'thirdPartyLicenses' => Licenses::dependencies(), ]); diff --git a/app/Middleware/PublicAssets.php b/app/Middleware/PublicAssets.php index 5ea2d2d..3ca44a7 100644 --- a/app/Middleware/PublicAssets.php +++ b/app/Middleware/PublicAssets.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace App\Middleware; +use App\Content\Assets; use Webman\Http\Request; use Webman\Http\Response; use Webman\MiddlewareInterface; @@ -16,7 +17,7 @@ final class PublicAssets implements MiddlewareInterface $path = $request->path(); if (str_starts_with($path, '/assets/')) { $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'); } } diff --git a/app/view/admin-about.php b/app/view/admin-about.php index 6a2fa5e..3c8e81b 100644 --- a/app/view/admin-about.php +++ b/app/view/admin-about.php @@ -6,26 +6,23 @@ $e = View::escape(...); ?>

-
+

版本与运行环境

LaySite
-
CPU 占用
暂不可用
-
内存占用
-
媒体库占用
已登记文件,含归档资源
- -
媒体磁盘可用
-
-

CPU 和内存显示当前 Linux 系统可见的用量,页面可见时自动更新。

-
+
运行环境详情
$value): ?>
+
CPU 占用
暂不可用
+
内存占用
+
媒体库占用
已登记文件,含归档资源
+
媒体磁盘可用
+

CPU 和内存显示 Linux 系统可见的用量,在详情展开且页面可见时自动更新。

@@ -42,12 +39,14 @@ $e = View::escape(...);
- +
许可全文 + + 见项目主页
diff --git a/app/view/admin-layout.php b/app/view/admin-layout.php index c4429a9..0844549 100644 --- a/app/view/admin-layout.php +++ b/app/view/admin-layout.php @@ -21,7 +21,7 @@ $active = match (true) { <?= $e($title) ?> CMS - + @@ -33,7 +33,7 @@ $active = match (true) { - + diff --git a/app/view/layout.php b/app/view/layout.php index dfab9b2..bdcc3c0 100644 --- a/app/view/layout.php +++ b/app/view/layout.php @@ -47,7 +47,7 @@ $showFooter = $page['visibility']['footer'] ?? true; - + diff --git a/public/assets/admin-about.js b/public/assets/admin-about.js index f2de7e7..c32e15a 100644 --- a/public/assets/admin-about.js +++ b/public/assets/admin-about.js @@ -7,22 +7,26 @@ jQuery(function ($) { const $cpu = $metrics.find('[data-about-cpu]'); const $memory = $metrics.find('[data-about-memory]'); const percent = new Intl.NumberFormat('zh-CN', {style: 'percent', maximumFractionDigits: 1}); - const {cpuTotal, cpuIdle} = $metrics[0].dataset; - let previous = cpuTotal === '' ? null : {total: Number(cpuTotal), idle: Number(cpuIdle)}; + let previous = null; + let timer = null; + let pending = null; let sessionExpired = false; - if (previous) $cpu.text('采样中…'); + + function active() { + return $metrics[0].open && !document.hidden && !sessionExpired; + } function poll() { - if (document.hidden) { - previous = null; - window.setTimeout(poll, 5000); - return; - } + timer = null; + if (!active()) 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}) => { if (!cpu || !previous || cpu.total <= previous.total) { $cpu.text(cpu ? '采样中…' : '暂不可用'); + if (cpu) delay = 1000; } else { const total = cpu.total - previous.total; const idle = cpu.idle - previous.idle; @@ -31,7 +35,8 @@ jQuery(function ($) { previous = cpu; $memory.text(memory ?? '暂不可用'); }) - .fail(({status}) => { + .fail(({status}, reason) => { + if (reason === 'abort') return; previous = null; sessionExpired = status === 401; const message = sessionExpired ? '登录已过期' : '暂不可用'; @@ -39,9 +44,26 @@ jQuery(function ($) { $memory.text(message); }) .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(); }); diff --git a/public/assets/admin.css b/public/assets/admin.css index c4de650..ee4f218 100644 --- a/public/assets/admin.css +++ b/public/assets/admin.css @@ -13,7 +13,8 @@ .admin-system-info dt { color: var(--muted); } .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 .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-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); }