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];
}
}