Expand admin system information and license notices

This commit is contained in:
2026-11-25 21:39:13 +08:00
parent 2e1e43a3ce
commit acf59c9dfc
18 changed files with 1014 additions and 21 deletions
+68
View File
@@ -0,0 +1,68 @@
<?php
declare(strict_types=1);
namespace App\Content;
use Composer\InstalledVersions;
final class Licenses
{
public const PROJECT = 'AGPL-3.0-only';
public static function dependencies(): array
{
$root = dirname(__DIR__, 2);
$entries = [
'jquery' => ['name' => 'jQuery', 'version' => '4.0.0', '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',
'url' => 'https://github.com/nhn/tui.editor', 'file' => $root . '/public/assets/vendor/toastui/LICENSE'],
'geist' => ['name' => 'Geist', 'version' => '', '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',
'url' => 'https://prosemirror.net/', 'file' => null],
'tslib' => ['name' => 'tslib', 'version' => '', 'license' => '0BSD',
'url' => 'https://github.com/microsoft/tslib', 'file' => null],
];
$lock = @file_get_contents($root . '/composer.lock');
foreach (($lock === false ? [] : json_decode($lock, true)['packages'] ?? []) as $package) {
$name = $package['name'];
if (!InstalledVersions::isInstalled($name, false)) {
continue;
}
$path = InstalledVersions::getInstallPath($name);
$url = $package['source']['url'] ?? $package['homepage'] ?? '';
$entries[str_replace('/', '--', $name)] = [
'name' => $name, 'version' => SystemInfo::version($name),
'license' => implode(' / ', $package['license'] ?? []) ?: '未知',
'url' => preg_match('~^https?://~', $url) ? preg_replace('/\.git$/D', '', $url) : '',
'file' => $path === null ? null : self::notice($path),
];
}
return $entries;
}
public static function text(string $key): ?string
{
$path = $key === 'laysite' ? dirname(__DIR__, 2) . '/LICENSE' : (self::dependencies()[$key]['file'] ?? null);
$text = $path === null ? false : @file_get_contents($path);
return $text === false ? null : $text;
}
private static function notice(string $path): ?string
{
foreach (['LICENSE', 'LICENSE.md', 'LICENSE.txt', 'license.md', 'MIT-LICENSE.txt'] as $name) {
$file = $path . '/' . $name;
if (is_file($file) && is_readable($file)) {
return $file;
}
}
return null;
}
}
+12
View File
@@ -88,6 +88,18 @@ final class Media
return rtrim(Environment::path('LAYSITE_MEDIA_DIR', 'storage/media'), '/');
}
public static function usage(): array
{
$bytes = Database::connect()->query('SELECT COALESCE(SUM(size), 0) FROM media')->fetchColumn();
$directory = self::directory();
$free = is_dir($directory) ? @disk_free_space($directory) : false;
return [
'size' => SystemInfo::bytes((int) $bytes),
'free' => $free === false ? null : SystemInfo::bytes($free),
];
}
public static function validateBackup(string $path, string $name, string $mime, int $size): void
{
if ((new \finfo(FILEINFO_MIME_TYPE))->file($path) !== $mime
+85
View File
@@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
namespace App\Content;
use Composer\InstalledVersions;
final class SystemInfo
{
public static function version(string $package): string
{
return InstalledVersions::isInstalled($package)
? InstalledVersions::getPrettyVersion($package) ?? '未知'
: '未知';
}
public static function platform(): string
{
if (PHP_OS_FAMILY === 'Linux') {
foreach (['/etc/os-release', '/usr/lib/os-release'] as $path) {
if (!is_readable($path)) {
continue;
}
$release = @parse_ini_file($path, false, INI_SCANNER_RAW);
$name = $release['PRETTY_NAME'] ?? $release['NAME'] ?? '';
$version = $release['VERSION_ID'] ?? '';
if ($name !== '') {
return $version !== '' && !str_contains($name, $version) ? $name . ' ' . $version : $name;
}
}
}
return PHP_OS_FAMILY;
}
public static function sample(): array
{
return [
'cpu' => PHP_OS_FAMILY === 'Linux' ? self::cpu() : null,
'memory' => PHP_OS_FAMILY === 'Linux' ? self::memory() : null,
];
}
private static function cpu(): ?array
{
$file = @fopen('/proc/stat', 'r');
if ($file === false) {
return null;
}
$line = fgets($file);
fclose($file);
if ($line === false || !preg_match('/^cpu\h+(?:\d+\h+){7}\d+/', $line)) {
return null;
}
$fields = array_map('intval', explode(' ', preg_replace('/\s+/', ' ', trim($line))));
// Exclude the guest fields. Linux already counts them in user and nice time.
return ['total' => array_sum(array_slice($fields, 1, 8)), 'idle' => $fields[4] + $fields[5]];
}
private static function memory(): ?string
{
$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) {
return null;
}
$capacity = (int) $total[1] * 1024;
$used = max(0, $capacity - (int) $available[1] * 1024);
return sprintf('%s / %s(%.1f%%)', self::bytes($used), self::bytes($capacity), 100 * $used / $capacity);
}
public static function bytes(int|float $bytes): string
{
$units = ['B', 'KiB', 'MiB', 'GiB', 'TiB'];
$unit = 0;
while ($bytes >= 1024 && $unit < count($units) - 1) {
$bytes /= 1024;
$unit++;
}
return ($unit === 0 ? (string) $bytes : number_format($bytes, 1)) . ' ' . $units[$unit];
}
}
+35 -4
View File
@@ -11,12 +11,14 @@ use App\Content\DesignLibrary;
use App\Content\Database;
use App\Content\Html;
use App\Content\LayoutPage;
use App\Content\Licenses;
use App\Content\Media;
use App\Content\Passwords;
use App\Content\Repository;
use App\Content\SiteDesign;
use App\Content\SystemInfo;
use App\Content\View;
use App\Http\Request;
use Composer\InstalledVersions;
use RuntimeException;
use support\Response;
@@ -137,14 +139,43 @@ class AdminController
'title' => '关于系统',
'systemInfo' => [
'PHP' => PHP_VERSION,
'Webman' => InstalledVersions::getPrettyVersion('workerman/webman-framework'),
'Workerman' => InstalledVersions::getPrettyVersion('workerman/workerman'),
'Webman' => SystemInfo::version('workerman/webman-framework'),
'Workerman' => SystemInfo::version('workerman/workerman'),
'SQLite' => (string) Database::connect()->query('SELECT sqlite_version()')->fetchColumn(),
'运行平台' => PHP_OS_FAMILY,
'运行平台' => SystemInfo::platform(),
PHP_OS_FAMILY === 'Linux' ? 'Linux 内核' : '系统版本' => php_uname('r'),
],
'metrics' => SystemInfo::sample(),
'mediaUsage' => Media::usage(),
'thirdPartyLicenses' => Licenses::dependencies(),
]);
}
public function aboutMetrics(Request $request): Response
{
if ($failure = $this->guard($request)) {
return $failure;
}
return $this->json(SystemInfo::sample());
}
public function aboutLicense(Request $request, string $key): Response
{
if ($failure = $this->guard($request)) {
return $failure;
}
$text = Licenses::text($key);
if ($text === null) {
return $this->error($request, '找不到许可证。', 404);
}
return new Response(200, [
'Content-Type' => 'text/plain; charset=utf-8', 'Cache-Control' => 'no-store',
'X-Content-Type-Options' => 'nosniff', 'X-Robots-Tag' => 'noindex',
], $text);
}
public function articles(Request $request): Response
{
if ($failure = $this->guard($request)) {
+58 -7
View File
@@ -1,14 +1,65 @@
<?php
use App\Content\Licenses;
use App\Content\View;
$e = View::escape(...);
?>
<header class="admin-heading"><h1><?= $e($title) ?></h1></header>
<section class="admin-panel admin-system-info" aria-labelledby="system-info-title">
<h2 id="system-info-title">版本与运行环境</h2>
<dl>
<?php foreach (['LaySite' => $laysiteVersion] + $systemInfo as $name => $value): ?>
<div><dt><?= $e($name) ?></dt><dd><?= $e($value) ?></dd></div>
<div class="admin-about">
<section class="admin-panel admin-system-info" aria-labelledby="system-info-title" data-about-metrics
data-cpu-total="<?= $e($metrics['cpu']['total'] ?? '') ?>" data-cpu-idle="<?= $e($metrics['cpu']['idle'] ?? '') ?>">
<h2 id="system-info-title">版本与运行环境</h2>
<dl>
<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>
<p class="field-note">CPU 和内存显示当前 Linux 系统可见的用量,页面可见时自动更新。</p>
<details class="admin-options admin-about-details">
<summary>运行环境详情</summary>
<dl>
<?php foreach ($systemInfo as $name => $value): ?>
<div><dt><?= $e($name) ?></dt><dd><?= $e($value) ?></dd></div>
<?php endforeach ?>
</dl>
</details>
</section>
<section class="admin-panel admin-licenses" aria-labelledby="system-licenses-title">
<h2 id="system-licenses-title">开源许可证</h2>
<div class="admin-license-project">
<div><strong>LaySite</strong><p>GNU Affero General Public License v3.0(<?= $e(Licenses::PROJECT) ?>)</p></div>
<a href="/admin/about/license/laysite" target="_blank" rel="noopener noreferrer">查看许可证全文</a>
</div>
<details class="admin-options admin-about-details">
<summary>第三方项目与许可</summary>
<ul class="admin-license-list">
<?php foreach ($thirdPartyLicenses as $key => $project): ?>
<li>
<div>
<?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 if ($project['version'] !== ''): ?><small><?= $e($project['version']) ?></small><?php endif ?>
</div>
<div>
<span><?= $e($project['license']) ?></span>
<?php if ($project['file'] !== null): ?>
<a href="/admin/about/license/<?= $e($key) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= $e('查看 ' . $project['name'] . ' 的许可证全文') ?>">许可全文</a>
<?php endif ?>
</div>
</li>
<?php endforeach ?>
</ul>
</details>
</section>
<nav class="admin-about-links" aria-label="项目链接">
<?php foreach (['LaySite' => 'https://laysense.cn/laysite', 'Laysense' => 'https://laysense.cn'] as $name => $url): ?>
<a href="<?= $e($url) ?>" target="_blank" rel="noopener noreferrer">
<?= View::fragment('studio-icon', ['icon' => 'external']) ?><strong><?= $e($name) ?></strong><span><?= $e($url) ?></span>
</a>
<?php endforeach ?>
</dl>
</section>
</nav>
</div>
+4 -1
View File
@@ -24,6 +24,9 @@ $active = match (true) {
<script src="/assets/jquery-4.0.0.min.js" defer></script>
<script src="/assets/admin.js" defer></script>
<script src="/assets/studio.js" defer></script>
<?php if ($adminPath === '/admin/about'): ?>
<script src="/assets/admin-about.js" defer></script>
<?php endif ?>
<?php if (!empty($pageTemplateDrawer)): ?>
<link rel="stylesheet" href="/assets/page-template-drawer.css">
<script src="/assets/page-template-drawer.js" defer></script>
@@ -75,7 +78,7 @@ $active = match (true) {
<label>查找资源<input type="search" data-media-query placeholder="搜索文件名或资源路径"></label>
<p data-picker-status role="status"></p>
<div class="picker-grid" data-picker-items></div>
<a class="back-link" href="/admin/media" target="_blank" rel="noopener">打开媒体库上传</a>
<a class="back-link" href="/admin/media" target="_blank" rel="noopener noreferrer">打开媒体库上传</a>
</dialog>
<?php endif ?>
</body>
+2 -2
View File
@@ -25,13 +25,13 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;
<?php foreach (['header' => '编辑页头', 'footer' => '编辑页脚'] as $kind => $label): ?>
<?php $defaultId = $layoutDefaults[$kind . '_id'] ?? ''; $id = $page[$kind . '_id'] ?? $defaultId; ?>
<a class="button button-outline" data-page-layout-editor="<?= $e($kind) ?>" data-default-id="<?= $e($defaultId) ?>"
href="<?= $id ? '/admin/pages/' . $e(LayoutPage::slug($kind, $id)) : '/admin/new-page?type=' . $e($kind) ?>" target="_blank" rel="noopener"<?= !($page['visibility'][$kind] ?? true) ? ' hidden' : '' ?>><?= View::fragment('studio-icon', ['icon' => $kind]) ?><?= $e($label) ?></a>
href="<?= $id ? '/admin/pages/' . $e(LayoutPage::slug($kind, $id)) : '/admin/new-page?type=' . $e($kind) ?>" target="_blank" rel="noopener noreferrer"<?= !($page['visibility'][$kind] ?? true) ? ' hidden' : '' ?>><?= View::fragment('studio-icon', ['icon' => $kind]) ?><?= $e($label) ?></a>
<?php endforeach ?>
<button type="button" class="button button-outline" data-open-page-templates hidden><?= View::fragment('studio-icon', ['icon' => 'templates']) ?>整页模板</button>
<?php if ($systemLabel): ?><a class="button button-outline" href="/admin/settings"><?= View::fragment('studio-icon', ['icon' => 'settings']) ?>站点设置</a>
<?php elseif ($page['status'] === 'published'): ?><?= View::fragment('admin-view-link', ['href' => $path, 'icon' => 'external']) ?><?php endif ?>
<?php endif ?>
<?= View::fragment('admin-view-link', ['href' => $previewUrl, 'icon' => 'preview', 'label' => '预览页面']) ?>
<?= View::fragment('admin-view-link', ['href' => $previewUrl, 'icon' => 'preview', 'label' => '打开预览', 'showLabel' => true]) ?>
</div>
</header>
<?= View::fragment('admin-feedback', compact('error', 'saved')) ?>
+6 -1
View File
@@ -6,7 +6,12 @@ $label ??= match ($icon) {
'external' => '查看公开页面',
'preview' => '打开预览',
'globe' => '访问网站',
default => '打开链接',
};
$showLabel ??= false;
$hint = $e($label . '(在新标签页打开)');
?>
<a class="button button-outline admin-view-link admin-view-link-<?= $e($icon) ?>" href="<?= $e($href) ?>" target="_blank" rel="noopener" aria-label="<?= $hint ?>" title="<?= $hint ?>"><?= View::fragment('studio-icon', compact('icon')) ?></a>
<a class="button button-outline admin-view-link admin-view-link-<?= $e($icon) ?><?= $showLabel ? ' admin-view-link-labelled' : '' ?>"
href="<?= $e($href) ?>" target="_blank" rel="noopener noreferrer" aria-label="<?= $hint ?>" title="<?= $hint ?>">
<?= View::fragment('studio-icon', compact('icon')) ?><?php if ($showLabel): ?><span><?= $e($label) ?></span><?php endif ?>
</a>
+1 -1
View File
@@ -28,4 +28,4 @@ $paths = [
'archive' => '<rect x="4" y="4" width="16" height="4" rx="1"/><path d="M5 8v12h14V8m-9 4h4"/>',
];
?>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><?= $paths[$icon] ?></svg>
<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.7" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true" focusable="false"><?= $paths[$icon] ?? $paths['external'] ?></svg>
+1 -1
View File
@@ -43,7 +43,7 @@ $previewRoot = '/assets/page-templates/';
<div class="page-template-detail">
<div>
<div class="page-template-image"><img data-template-preview width="768" height="480" alt=""></div>
<a class="button button-outline admin-view-link admin-view-link-preview page-template-preview-link" data-template-live target="_blank" rel="noopener"
<a class="button button-outline admin-view-link admin-view-link-preview page-template-preview-link" data-template-live target="_blank" rel="noopener noreferrer"
aria-label="预览整页模板(在新标签页打开)" title="预览整页模板(在新标签页打开)"><?= View::fragment('studio-icon', ['icon' => 'preview']) ?></a>
</div>
<form class="admin-form" method="post" action="/admin/new-page">