Address LaySite 1.4.5 editor and release review suggestions

This commit is contained in:
2026-11-27 22:14:02 +08:00
parent 00e8a70e06
commit 074591d34f
16 changed files with 159 additions and 48 deletions
+1
View File
@@ -11,6 +11,7 @@ use Throwable;
final class Database
{
public const MIGRATION_REQUIRED = '数据库尚未完成升级。请在站点目录运行 php bin/install.php。';
private const SCHEMA_VERSION = 8;
// Avoid reusing a deleted ID that may still be open in an editing form.
public const NEXT_ARTICLE_ID = 'MAX(COALESCE((SELECT MAX(id) FROM articles), 0),
+22 -6
View File
@@ -6,6 +6,7 @@ namespace App\Content;
use App\Config\Environment;
use PDO;
use PDOException;
use RuntimeException;
final class McpSettings
@@ -16,10 +17,13 @@ final class McpSettings
public static function read(): array
{
$row = self::stored(Database::connect());
if ($row === null) {
$token = self::legacyToken();
$migrationRequired = $row === false;
$keyError = '';
if (!is_array($row)) {
$token = $migrationRequired ? null : self::legacyToken();
return ['enabled' => $token !== null, 'version' => 0, 'token' => $token, 'legacy' => $token !== null];
return ['enabled' => $token !== null, 'version' => 0, 'legacy' => $token !== null]
+ compact('token', 'migrationRequired', 'keyError');
}
$enabled = (bool) $row['enabled'];
@@ -27,12 +31,14 @@ final class McpSettings
if ($enabled && $row['encrypted_secret'] !== null) {
try {
$token = SiteSecrets::decrypt($row['encrypted_secret'], self::CONTEXT);
} catch (SiteKeyFileError $error) {
$keyError = $error->getMessage();
} catch (RuntimeException) {
// Keep the settings accessible so an administrator can replace an unreadable key.
}
}
return ['enabled' => $enabled, 'version' => (int) $row['version'], 'token' => $token, 'legacy' => false];
return ['version' => (int) $row['version'], 'legacy' => false] + compact('enabled', 'token', 'migrationRequired', 'keyError');
}
public static function save(bool $enabled, bool $rotate, int $version): void
@@ -42,6 +48,9 @@ final class McpSettings
}
Database::write(static function (PDO $pdo) use ($enabled, $rotate, $version): void {
$row = self::stored($pdo);
if ($row === false) {
throw new RuntimeException(Database::MIGRATION_REQUIRED);
}
if ($version !== (int) ($row['version'] ?? 0)) {
throw new RuntimeException('MCP 设置已更新,请刷新后再保存。');
}
@@ -58,9 +67,16 @@ final class McpSettings
});
}
private static function stored(PDO $pdo): ?array
private static function stored(PDO $pdo): array|false|null
{
return $pdo->query('SELECT enabled, encrypted_secret, version FROM mcp_settings WHERE id = 1')->fetch() ?: null;
try {
return $pdo->query('SELECT enabled, encrypted_secret, version FROM mcp_settings WHERE id = 1')->fetch() ?: null;
} catch (PDOException $error) {
if (($error->errorInfo[2] ?? '') === 'no such table: mcp_settings') {
return false;
}
throw $error;
}
}
private static function legacyToken(): ?string
+8 -3
View File
@@ -38,7 +38,9 @@ final class PageHead
continue;
}
if ($tag === 'script') {
$result['hasScript'] = true;
if (strtolower($attributes['type'] ?? '') !== 'application/ld+json') {
$result['hasScript'] = true;
}
if (!isset($attributes['src'])) {
$result['inlineScript'] = true;
$attributes['nonce'] = $nonce;
@@ -83,8 +85,11 @@ final class PageHead
|| ($node->nodeType === XML_TEXT_NODE && trim($node->textContent) === '')) {
continue;
}
if (!$node instanceof DOMElement || !isset(self::ATTRIBUTES[$node->tagName]) || count($entries) >= 40) {
throw new RuntimeException('Head 最多支持 40 个 meta、样式表、style 或 script 标签。');
if (!$node instanceof DOMElement || !isset(self::ATTRIBUTES[$node->tagName])) {
throw new RuntimeException('Head 仅支持 meta、样式表(link)、style 和 script 标签。');
}
if (count($entries) >= 40) {
throw new RuntimeException('Head 最多允许 40 个标签。');
}
$tag = $node->tagName;
$attributes = [];
+5 -2
View File
@@ -132,14 +132,17 @@ final class Repository
public function savePage(string $slug, array $data, int $version, array $sections = [], bool $replaceDraftSections = false, ?array $sectionOrder = null): void
{
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): void {
$head = $data['head_html'] ?? null;
$query = $pdo->prepare('UPDATE pages SET name = ?, title = ?, description = ?, heading = ?, accent = ?, lead = ?,
image = ?, alt = ?, image_scale = ?, alignments = ?, actions = ?, quicklinks = ?, visibility = ?, header_id = ?, footer_id = ?, palette_id = ?, status = ?, updated_at = ?,
presentation = COALESCE(?, presentation), head_html = COALESCE(?, head_html), version = version + 1 WHERE slug = ? AND version = ?');
presentation = COALESCE(?, presentation), ' . ($head === null ? '' : 'head_html = ?, ')
. 'version = version + 1 WHERE slug = ? AND version = ?');
$query->execute([
$data['name'], $data['title'], $data['description'], $data['heading'], $data['accent'], $data['lead'],
$data['image'], $data['alt'], $data['image_scale'], Database::json($data['alignments']), Database::json($data['actions']), Database::json($data['quicklinks']),
Database::json($data['visibility']), $data['header_id'], $data['footer_id'], $data['palette_id'], $data['status'], self::now(),
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null, $data['head_html'] ?? null, $slug, $version,
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null,
...($head === null ? [] : [$head]), $slug, $version,
]);
self::checkVersion($query->rowCount());
if ($data['status'] === 'draft' && str_starts_with($slug, 'system--')) {
+9
View File
@@ -0,0 +1,9 @@
<?php
declare(strict_types=1);
namespace App\Content;
final class SiteKeyFileError extends \RuntimeException
{
}
+9 -5
View File
@@ -44,8 +44,12 @@ final class SiteSecrets
private static function key(bool $create = false): string
{
$path = Database::path() . '.key';
clearstatcache(true, $path);
if (is_link($path)) {
throw new RuntimeException('密钥文件不能使用符号链接。');
throw new SiteKeyFileError('密钥文件不能使用符号链接。');
}
if (!$create && !file_exists($path)) {
throw new RuntimeException('站点密钥文件缺失。');
}
$mask = umask(0077);
try {
@@ -54,22 +58,22 @@ final class SiteSecrets
umask($mask);
}
if ($file === false) {
throw new RuntimeException('无法打开站点密钥文件。');
throw new SiteKeyFileError('无法打开站点密钥文件。');
}
try {
if (!flock($file, $create ? LOCK_EX : LOCK_SH) || (fstat($file)['mode'] & 0077) !== 0) {
throw new RuntimeException('站点密钥文件需能上锁,并且只能由运行用户访问。');
throw new SiteKeyFileError('站点密钥文件需能上锁,并且只能由运行用户访问。');
}
$key = stream_get_contents($file, self::KEY_BYTES + 1);
if ($create && $key === '') {
$key = random_bytes(self::KEY_BYTES);
if (fwrite($file, $key) !== self::KEY_BYTES || !fflush($file)) {
ftruncate($file, 0);
throw new RuntimeException('无法写入站点密钥文件。');
throw new SiteKeyFileError('无法写入站点密钥文件。');
}
}
if (!is_string($key) || strlen($key) !== self::KEY_BYTES) {
throw new RuntimeException('站点密钥文件格式无效。');
throw new SiteKeyFileError('站点密钥文件格式无效。');
}
return $key;