Files
laysense.cn/app/Content/Licenses.php
T

80 lines
3.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Content;
use Composer\InstalledVersions;
final class Licenses
{
public const PROJECT = 'AGPL-3.0-or-later';
public const PROJECT_NAME = 'GNU Affero General Public License v3.0';
public const PROJECT_PATH = '/.laysite/license';
public static function projectText(): ?string
{
$root = dirname(__DIR__, 2);
$notice = @file_get_contents($root . '/NOTICE');
$license = @file_get_contents($root . '/LICENSE');
return $notice === false || $license === false ? null : rtrim($notice) . "\n\n" . $license;
}
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 = 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;
}
}