forked from laysense/laysite
Prepare LaySite 1.4.5 site icons, Head settings and release images
This commit is contained in:
@@ -11,7 +11,7 @@ use Throwable;
|
||||
|
||||
final class Database
|
||||
{
|
||||
private const SCHEMA_VERSION = 7;
|
||||
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),
|
||||
COALESCE((SELECT MAX(article_id) FROM article_deletions), 0)) + 1';
|
||||
@@ -92,6 +92,9 @@ final class Database
|
||||
if ($version < 6 && !in_array('page_type', array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name'), true)) {
|
||||
$pdo->exec("ALTER TABLE pages ADD COLUMN page_type TEXT NOT NULL DEFAULT 'page' CHECK (page_type IN ('page', 'maintenance', 'not-found'))");
|
||||
}
|
||||
if ($version < 8 && !in_array('head_html', array_column($pdo->query('PRAGMA table_info(pages)')->fetchAll(), 'name'), true)) {
|
||||
$pdo->exec("ALTER TABLE pages ADD COLUMN head_html TEXT NOT NULL DEFAULT ''");
|
||||
}
|
||||
if ($version < 5) {
|
||||
LayoutPage::migrate($pdo);
|
||||
}
|
||||
|
||||
+24
-2
@@ -15,6 +15,8 @@ use Webman\Http\UploadFile;
|
||||
final class Media
|
||||
{
|
||||
public const MAX_BYTES = 64 * 1024 * 1024;
|
||||
private const MAX_CODE_BYTES = 2 * 1024 * 1024;
|
||||
private const CODE_TYPES = ['css' => 'text/css', 'js' => 'text/javascript'];
|
||||
|
||||
private const EXTENSIONS = [
|
||||
'image/png' => 'png', 'image/jpeg' => 'jpg', 'image/gif' => 'gif', 'image/webp' => 'webp',
|
||||
@@ -22,6 +24,7 @@ final class Media
|
||||
'video/mp4' => 'mp4', 'video/webm' => 'webm', 'video/quicktime' => 'mov',
|
||||
'audio/mpeg' => 'mp3', 'audio/ogg' => 'ogg', 'audio/wav' => 'wav',
|
||||
'application/pdf' => 'pdf', 'font/woff' => 'woff', 'font/woff2' => 'woff2',
|
||||
'text/css' => 'css', 'text/javascript' => 'js',
|
||||
];
|
||||
|
||||
public static function all(bool $includeArchived = false): array
|
||||
@@ -42,7 +45,7 @@ final class Media
|
||||
throw new RuntimeException('请选择大小不超过 64 MB 的有效文件。');
|
||||
}
|
||||
$source = $file->getPathname();
|
||||
$mime = (new \finfo(FILEINFO_MIME_TYPE))->file($source);
|
||||
$mime = self::mime($source, (string) $file->getUploadName(), $size);
|
||||
$extension = self::EXTENSIONS[$mime] ?? null;
|
||||
if ($extension === null) {
|
||||
throw new RuntimeException('文件类型不受支持。');
|
||||
@@ -105,7 +108,7 @@ final class Media
|
||||
|
||||
public static function validateBackup(string $path, string $name, string $mime, int $size): void
|
||||
{
|
||||
if ((new \finfo(FILEINFO_MIME_TYPE))->file($path) !== $mime
|
||||
if (self::mime($path, $name, $size) !== $mime
|
||||
|| !isset(self::EXTENSIONS[$mime]) || self::EXTENSIONS[$mime] !== pathinfo($name, PATHINFO_EXTENSION)) {
|
||||
throw new RuntimeException('备份资源类型与清单不一致。');
|
||||
}
|
||||
@@ -114,6 +117,25 @@ final class Media
|
||||
}
|
||||
}
|
||||
|
||||
private static function mime(string $path, string $name, int $size): string|false
|
||||
{
|
||||
$mime = (new \finfo(FILEINFO_MIME_TYPE))->file($path);
|
||||
$extension = strtolower(pathinfo($name, PATHINFO_EXTENSION));
|
||||
if (!isset(self::CODE_TYPES[$extension])) {
|
||||
return in_array($mime, self::CODE_TYPES, true) ? false : $mime;
|
||||
}
|
||||
if ($size > self::MAX_CODE_BYTES || !is_string($mime)
|
||||
|| (!str_starts_with($mime, 'text/') && !in_array($mime, ['application/javascript', 'application/json'], true))) {
|
||||
throw new RuntimeException('CSS 或 JavaScript 文件须为有效的 UTF-8 文本,且不超过 2 MB。');
|
||||
}
|
||||
$source = file_get_contents($path, length: self::MAX_CODE_BYTES + 1);
|
||||
if (!is_string($source) || strlen($source) !== $size || !mb_check_encoding($source, 'UTF-8') || str_contains($source, "\0")) {
|
||||
throw new RuntimeException('CSS 或 JavaScript 文件须为有效的 UTF-8 文本,且不超过 2 MB。');
|
||||
}
|
||||
|
||||
return self::CODE_TYPES[$extension];
|
||||
}
|
||||
|
||||
private static function safeSvg(string $source, int $size): string
|
||||
{
|
||||
if ($size > 512 * 1024) {
|
||||
|
||||
@@ -0,0 +1,148 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use DOMDocument;
|
||||
use DOMElement;
|
||||
use RuntimeException;
|
||||
|
||||
final class PageHead
|
||||
{
|
||||
public const MAX_LENGTH = 32768;
|
||||
private const ATTRIBUTES = [
|
||||
'meta' => ['name', 'property', 'content'],
|
||||
'link' => ['rel', 'href', 'media', 'integrity', 'crossorigin', 'referrerpolicy'],
|
||||
'style' => ['media'],
|
||||
'script' => ['src', 'type', 'async', 'defer', 'integrity', 'crossorigin', 'referrerpolicy'],
|
||||
];
|
||||
|
||||
public static function normalize(mixed $value): string
|
||||
{
|
||||
if (!is_string($value) || strlen($value) > self::MAX_LENGTH || !mb_check_encoding($value, 'UTF-8') || str_contains($value, "\0")) {
|
||||
throw new RuntimeException('Head 代码须使用 UTF-8,最多 32 KB。');
|
||||
}
|
||||
$value = trim($value);
|
||||
self::parse($value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
public static function render(array $sources, string $nonce, bool $canvas): array
|
||||
{
|
||||
$result = ['markup' => '', 'styles' => [], 'scripts' => [], 'inlineScript' => false, 'hasScript' => false];
|
||||
foreach ($sources as $source) {
|
||||
foreach (self::parse($source) as [$tag, $attributes, $text, $origin]) {
|
||||
if ($canvas && $tag === 'script') {
|
||||
continue;
|
||||
}
|
||||
if ($tag === 'script') {
|
||||
$result['hasScript'] = true;
|
||||
if (!isset($attributes['src'])) {
|
||||
$result['inlineScript'] = true;
|
||||
$attributes['nonce'] = $nonce;
|
||||
} elseif ($origin !== null) {
|
||||
$result['scripts'][$origin] = true;
|
||||
}
|
||||
}
|
||||
if ($tag === 'style') {
|
||||
$attributes['nonce'] = $nonce;
|
||||
} elseif ($tag === 'link' && $origin !== null) {
|
||||
$result['styles'][$origin] = true;
|
||||
}
|
||||
$markup = '<' . $tag;
|
||||
foreach ($attributes as $name => $value) {
|
||||
$markup .= " $name" . (in_array($name, ['async', 'defer'], true) ? '' : '="' . View::escape($value) . '"');
|
||||
}
|
||||
$result['markup'] .= $markup . '>' . ($text === null ? '' : $text . '</' . $tag . '>') . "\n";
|
||||
}
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
private static function parse(string $source): array
|
||||
{
|
||||
if ($source === '') {
|
||||
return [];
|
||||
}
|
||||
$document = new DOMDocument('1.0', 'UTF-8');
|
||||
$document->loadHTML('<!doctype html><html><head><meta charset="utf-8">' . $source . '</head><body></body></html>',
|
||||
LIBXML_NONET | LIBXML_NOERROR | LIBXML_NOWARNING);
|
||||
$head = $document->getElementsByTagName('head')->item(0);
|
||||
$body = $document->getElementsByTagName('body')->item(0);
|
||||
if ($head === null || ($body !== null && ($body->childElementCount > 0 || trim($body->textContent) !== ''))) {
|
||||
throw new RuntimeException('Head 中只能填写文档头部的标签。');
|
||||
}
|
||||
|
||||
$entries = [];
|
||||
$origins = [];
|
||||
foreach ($head->childNodes as $node) {
|
||||
if ($node === $head->firstChild || $node->nodeType === XML_COMMENT_NODE
|
||||
|| ($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 标签。');
|
||||
}
|
||||
$tag = $node->tagName;
|
||||
$attributes = [];
|
||||
foreach ($node->attributes as $attribute) {
|
||||
if (!in_array($attribute->name, self::ATTRIBUTES[$tag], true)) {
|
||||
throw new RuntimeException('Head 标签包含不支持的属性:' . $attribute->name . '。');
|
||||
}
|
||||
$attributes[$attribute->name] = $attribute->value;
|
||||
}
|
||||
$text = in_array($tag, ['script', 'style'], true) ? $node->textContent : null;
|
||||
if ($text !== null && preg_match('~<(?:!--|/?\s*(?:script|style)\b)~i', $text)) {
|
||||
throw new RuntimeException('内联代码含有可能中断 HTML 解析的标记,请将代码放到独立文件中。');
|
||||
}
|
||||
$origin = null;
|
||||
if ($tag === 'meta' && ((!isset($attributes['name']) && !isset($attributes['property'])) || !isset($attributes['content']))) {
|
||||
throw new RuntimeException('meta 标签须填写 name 或 property,以及 content。');
|
||||
}
|
||||
if ($tag === 'link') {
|
||||
if (strtolower($attributes['rel'] ?? '') !== 'stylesheet') {
|
||||
throw new RuntimeException('Head 中的 link 标签仅用于加载样式表。');
|
||||
}
|
||||
$origin = self::origin($attributes['href'] ?? '');
|
||||
} elseif ($tag === 'script') {
|
||||
if (!in_array(strtolower($attributes['type'] ?? ''), ['', 'text/javascript', 'module', 'application/ld+json'], true)) {
|
||||
throw new RuntimeException('script 类型须为 JavaScript、module 或 application/ld+json。');
|
||||
}
|
||||
if (isset($attributes['src'])) {
|
||||
if (trim($text) !== '') {
|
||||
throw new RuntimeException('有 src 的 script 标签应留空内容。');
|
||||
}
|
||||
$origin = self::origin($attributes['src']);
|
||||
}
|
||||
}
|
||||
if ($origin !== null) {
|
||||
$origins[$origin] = true;
|
||||
if (count($origins) > 8) {
|
||||
throw new RuntimeException('一处 Head 设置最多可以引用 8 个 HTTPS 来源。');
|
||||
}
|
||||
}
|
||||
$entries[] = [$tag, $attributes, $text, $origin];
|
||||
}
|
||||
|
||||
return $entries;
|
||||
}
|
||||
|
||||
private static function origin(string $url): ?string
|
||||
{
|
||||
if (strlen($url) <= 2048 && Html::url($url, true)) {
|
||||
if (str_starts_with($url, '/')) {
|
||||
return null;
|
||||
}
|
||||
$parts = parse_url($url);
|
||||
if (filter_var($url, FILTER_VALIDATE_URL) && isset($parts['host']) && !isset($parts['user']) && !isset($parts['pass'])
|
||||
&& preg_match('/^[a-z0-9.-]+$/iD', $parts['host'])) {
|
||||
return 'https://' . strtolower($parts['host']) . (isset($parts['port']) ? ':' . $parts['port'] : '');
|
||||
}
|
||||
}
|
||||
|
||||
throw new RuntimeException('样式表和脚本地址须使用以 / 开头的站内路径或完整 HTTPS 网址。');
|
||||
}
|
||||
}
|
||||
@@ -133,12 +133,13 @@ final class Repository
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($slug, $data, $version, $sections, $replaceDraftSections, $sectionOrder): void {
|
||||
$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), version = version + 1 WHERE slug = ? AND version = ?');
|
||||
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 = ?');
|
||||
$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, $slug, $version,
|
||||
isset($data['presentation']) ? CanvasStyle::encode($data['presentation']) : null, $data['head_html'] ?? null, $slug, $version,
|
||||
]);
|
||||
self::checkVersion($query->rowCount());
|
||||
if ($data['status'] === 'draft' && str_starts_with($slug, 'system--')) {
|
||||
|
||||
@@ -12,7 +12,7 @@ use Throwable;
|
||||
final class SiteBackup
|
||||
{
|
||||
private const FORMAT = 'laysite-site';
|
||||
private const VERSION = 4;
|
||||
private const VERSION = 5;
|
||||
private const TABLES = [
|
||||
'site_settings', 'design_presets', 'pages', 'sections', 'article_categories', 'articles',
|
||||
'article_category_links', 'article_redirects', 'article_deletions', 'media',
|
||||
@@ -140,7 +140,7 @@ final class SiteBackup
|
||||
private static function prepare(mixed $manifest): array
|
||||
{
|
||||
if (!is_array($manifest) || ($manifest['format'] ?? '') !== self::FORMAT
|
||||
|| !in_array($manifest['version'] ?? null, [1, 2, 3, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) {
|
||||
|| !in_array($manifest['version'] ?? null, [1, 2, 3, 4, self::VERSION], true) || !is_array($manifest['tables'] ?? null)) {
|
||||
throw new RuntimeException('备份格式不受支持,请使用相同或更新版本的 LaySite。');
|
||||
}
|
||||
$tables = $manifest['tables'];
|
||||
@@ -172,6 +172,8 @@ final class SiteBackup
|
||||
throw new RuntimeException('备份的站点名称无效。');
|
||||
}
|
||||
$site = ['name' => $settings['name'], 'cdn' => ['origin' => Assets::cdn($settings['cdn']['origin'] ?? '')], 'revisions' => ['general' => time()]];
|
||||
$site['head_html'] = PageHead::normalize($settings['head_html'] ?? '');
|
||||
$site['favicon'] = SiteIcon::normalize($settings['favicon'] ?? '');
|
||||
$layoutDefaults = $settings['layout_defaults'] ?? [];
|
||||
if (!is_array($layoutDefaults)) {
|
||||
throw new RuntimeException('备份的基础页头页脚配置无效。');
|
||||
@@ -210,6 +212,7 @@ final class SiteBackup
|
||||
$row['visibility'] = Database::json(array_intersect_key(self::decode($row['visibility'] ?? '{}'), array_flip(['header', 'hero', 'footer'])));
|
||||
$row['alignments'] = Database::json(self::decode($row['alignments'] ?? '{}'));
|
||||
$row['presentation'] = CanvasStyle::encode(CanvasStyle::normalize(self::decode($row['presentation'] ?? '{}'), CanvasStyle::PAGE));
|
||||
$row['head_html'] = $layout === null ? PageHead::normalize($row['head_html'] ?? '') : '';
|
||||
if (($row['theme'] ?? '') !== '' && !isset(PageTemplates::groups()[$row['theme']])) {
|
||||
$row['theme'] = '';
|
||||
}
|
||||
|
||||
@@ -31,9 +31,10 @@ final class SiteDesign
|
||||
return implode("\n", array_map(static fn (array $link): string => $link[1] . ' | ' . $link[0], $links));
|
||||
}
|
||||
|
||||
public static function saveGeneral(string $name, string $origin, int $version, array $systemPages, bool $maintenance): void
|
||||
public static function saveGeneral(string $name, string $origin, int $version, array $systemPages, bool $maintenance,
|
||||
?string $head = null, ?string $icon = null): void
|
||||
{
|
||||
Database::write(function (PDO $pdo) use ($name, $origin, $version, $systemPages, $maintenance): void {
|
||||
Database::write(function (PDO $pdo) use ($name, $origin, $version, $systemPages, $maintenance, $head, $icon): void {
|
||||
$site = json_decode((string) $pdo->query('SELECT payload FROM site_settings WHERE id = 1')->fetchColumn(), true, flags: JSON_THROW_ON_ERROR);
|
||||
if (($site['revisions']['general'] ?? 0) !== $version) {
|
||||
throw new RuntimeException('站点设置已更新,请重新打开后再保存。');
|
||||
@@ -51,6 +52,12 @@ final class SiteDesign
|
||||
$site['cdn'] = ['origin' => $origin];
|
||||
$site['system_pages'] = $systemPages;
|
||||
$site['maintenance_enabled'] = $maintenance;
|
||||
if ($head !== null) {
|
||||
$site['head_html'] = $head;
|
||||
}
|
||||
if ($icon !== null) {
|
||||
$site['favicon'] = $icon;
|
||||
}
|
||||
$site['revisions']['general'] = $version + 1;
|
||||
$pdo->prepare('UPDATE site_settings SET payload = ? WHERE id = 1')->execute([Database::json($site)]);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use RuntimeException;
|
||||
use support\Response;
|
||||
|
||||
final class SiteIcon
|
||||
{
|
||||
public const SIZE = 64;
|
||||
public const PATH = '/.laysite/favicon.png';
|
||||
|
||||
public static function normalize(mixed $value): string
|
||||
{
|
||||
if ($value === '') {
|
||||
return '';
|
||||
}
|
||||
$bytes = is_string($value) && strlen($value) <= 44000 ? base64_decode($value, true) : false;
|
||||
$size = $bytes === false || $bytes === '' ? false : @getimagesizefromstring($bytes);
|
||||
if (!$size || $size[2] !== IMAGETYPE_PNG || $size[0] !== self::SIZE || $size[1] !== self::SIZE) {
|
||||
throw new RuntimeException('请裁剪并生成一个 64 × 64 的 PNG 图标。');
|
||||
}
|
||||
|
||||
return base64_encode($bytes);
|
||||
}
|
||||
|
||||
public static function url(string $icon): string
|
||||
{
|
||||
return $icon === '' ? Assets::DEFAULT_ICON : self::PATH . '?v=' . substr(hash('sha256', $icon), 0, 16);
|
||||
}
|
||||
|
||||
public static function response(string $icon, bool $ico): Response
|
||||
{
|
||||
if ($icon === '') {
|
||||
return View::redirect(Assets::DEFAULT_ICON, 301);
|
||||
}
|
||||
$bytes = base64_decode($icon, true);
|
||||
if ($bytes === false) {
|
||||
return View::redirect(Assets::DEFAULT_ICON, 302);
|
||||
}
|
||||
if ($ico) {
|
||||
$bytes = pack('vvvCCCCvvVV', 0, 1, 1, self::SIZE, self::SIZE, 0, 0, 1, 32, strlen($bytes), 22) . $bytes;
|
||||
}
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => $ico ? 'image/x-icon' : 'image/png',
|
||||
'Cache-Control' => 'public, max-age=300',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
], $bytes);
|
||||
}
|
||||
}
|
||||
+14
-4
@@ -50,12 +50,21 @@ final class View
|
||||
}
|
||||
$html = self::fragment($template, $data);
|
||||
$data['canvasColors'] = $admin ? '' : CanvasStyle::colorRules($html . ($data['headerMarkup'] ?? '') . ($data['footerMarkup'] ?? ''));
|
||||
$data['styleNonce'] = $data['canvasColors'] !== '' || !empty($data['canvas']) ? base64_encode(random_bytes(18)) : '';
|
||||
$headSources = $admin ? [] : array_filter([$data['site']['head_html'] ?? '', $data['page']['head_html'] ?? '']);
|
||||
$data['styleNonce'] = $data['canvasColors'] !== '' || !empty($data['canvas']) || $headSources !== []
|
||||
? base64_encode(random_bytes(18)) : '';
|
||||
$head = PageHead::render($headSources, $data['styleNonce'], !empty($data['canvas']));
|
||||
$data['headMarkup'] = $head['markup'];
|
||||
$page = Assets::rewrite(self::fragment($admin ? 'admin-layout' : 'layout', $data + ['content' => $html]), $cdn);
|
||||
$assetSources = $cdn === '' ? "'self'" : "'self' " . $cdn;
|
||||
$styleSources = $assetSources . ($data['styleNonce'] === '' ? '' : " 'nonce-{$data['styleNonce']}'");
|
||||
$styleSources .= $head['styles'] === [] ? '' : ' ' . implode(' ', array_keys($head['styles']));
|
||||
$scriptSources = $assetSources . ($head['scripts'] === [] ? '' : ' ' . implode(' ', array_keys($head['scripts'])));
|
||||
if ($head['inlineScript']) {
|
||||
$scriptSources .= " 'nonce-{$data['styleNonce']}'";
|
||||
}
|
||||
$articleEditor = $admin && !empty($data['articleEditor']);
|
||||
$editorImages = $articleEditor ? ' data:' : '';
|
||||
$editorImages = ($articleEditor ? ' data:' : '') . ($admin && !empty($data['faviconEditor']) ? ' blob:' : '');
|
||||
$editorStyles = $articleEditor ? "style-src-attr 'unsafe-inline'; " : '';
|
||||
$headers = [
|
||||
'Content-Type' => 'text/html; charset=utf-8',
|
||||
@@ -63,8 +72,9 @@ final class View
|
||||
'Referrer-Policy' => 'strict-origin-when-cross-origin',
|
||||
'X-Frame-Options' => !empty($data['previewMode']) ? 'SAMEORIGIN' : 'DENY',
|
||||
'Content-Security-Policy' => "default-src 'self'; img-src 'self' https:$editorImages; media-src 'self' https:; "
|
||||
. "style-src $styleSources; {$editorStyles}script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
. (!empty($data['previewMode']) ? "'self'" : "'none'") . "; form-action 'self'; base-uri 'none'; object-src 'none'",
|
||||
. "style-src $styleSources; {$editorStyles}script-src $scriptSources; font-src $assetSources; frame-src 'self'; frame-ancestors "
|
||||
. (!empty($data['previewMode']) ? "'self'" : "'none'") . "; form-action 'self'; base-uri 'none'; object-src 'none'"
|
||||
. ($head['hasScript'] && !empty($data['previewMode']) ? '; sandbox allow-scripts allow-popups' : ''),
|
||||
];
|
||||
if ($admin || !empty($data['previewMode'])) {
|
||||
$headers['Cache-Control'] = 'no-store';
|
||||
|
||||
@@ -11,6 +11,7 @@ use App\Content\DesignLibrary;
|
||||
use App\Content\Database;
|
||||
use App\Content\Html;
|
||||
use App\Content\LayoutPage;
|
||||
use App\Content\PageHead;
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Media;
|
||||
use App\Content\Passwords;
|
||||
@@ -341,7 +342,14 @@ class AdminController
|
||||
$data['image_scale'] = $request->post('image_scale') === null ? (int) $page['image_scale']
|
||||
: (ctype_digit($scale) ? (int) $scale : -1);
|
||||
$page = array_replace($page, $data, ['version' => $this->version($request)]);
|
||||
$head = !$layout ? $request->post('head_html') : null;
|
||||
if (is_string($head)) {
|
||||
$page['head_html'] = $head;
|
||||
}
|
||||
try {
|
||||
if ($head !== null) {
|
||||
$data['head_html'] = PageHead::normalize($head);
|
||||
}
|
||||
$data['actions'] = $request->post('hero_actions') === null ? $page['actions']
|
||||
: Buttons::validate(json_decode($this->input($request, 'hero_actions'), true));
|
||||
foreach (['header', 'footer', 'palette'] as $kind) {
|
||||
@@ -418,7 +426,7 @@ class AdminController
|
||||
|
||||
return $this->render($request, 'admin-page', [
|
||||
'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'canvasSourceEditor' => true, 'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default,
|
||||
'pageTemplateDrawer' => !$layout, 'headEditor' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default,
|
||||
'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'],
|
||||
'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']),
|
||||
'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]),
|
||||
|
||||
@@ -8,8 +8,10 @@ use App\Content\Assets;
|
||||
use App\Content\DesignFieldError;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\McpSettings;
|
||||
use App\Content\PageHead;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Content\SiteIcon;
|
||||
use App\Content\SystemPage;
|
||||
use App\Content\View;
|
||||
use App\Http\Request;
|
||||
@@ -84,6 +86,7 @@ final class DesignController extends AdminController
|
||||
$error = '';
|
||||
$name = $site['name'];
|
||||
$cdn = $site['cdn']['origin'] ?? '';
|
||||
$head = $site['head_html'] ?? '';
|
||||
$systemPages = $site['system_pages'] ?? [];
|
||||
$maintenance = $site['maintenance_enabled'] ?? false;
|
||||
$version = (int) ($site['revisions']['general'] ?? 0);
|
||||
@@ -105,7 +108,18 @@ final class DesignController extends AdminController
|
||||
'not-found' => $this->input($request, 'not_found_page'),
|
||||
], static fn (string $slug): bool => $slug !== '');
|
||||
$maintenance = $request->post('maintenance_enabled') === '1';
|
||||
SiteDesign::saveGeneral($name, $cdn, $this->version($request), $systemPages, $maintenance);
|
||||
$submittedHead = $request->post('head_html');
|
||||
if (is_string($submittedHead)) {
|
||||
$head = $submittedHead;
|
||||
}
|
||||
$icon = match ($request->post('favicon_action', 'keep')) {
|
||||
'keep' => null,
|
||||
'reset' => '',
|
||||
'replace' => SiteIcon::normalize($request->post('favicon_png') ?: null),
|
||||
default => throw new RuntimeException('请选择有效的站点图标操作。'),
|
||||
};
|
||||
SiteDesign::saveGeneral($name, $cdn, $this->version($request), $systemPages, $maintenance,
|
||||
$submittedHead === null ? null : PageHead::normalize($submittedHead), $icon);
|
||||
|
||||
return $this->success($request, '/admin/settings?saved=1');
|
||||
} catch (RuntimeException $ex) {
|
||||
@@ -123,6 +137,7 @@ final class DesignController extends AdminController
|
||||
return $this->render($request, 'studio-settings', [
|
||||
'title' => '站点设置', 'name' => $name, 'cdn' => $cdn, 'version' => $version, 'error' => $error,
|
||||
'systemPages' => $systemPages, 'systemChoices' => $systemChoices, 'maintenance' => $maintenance,
|
||||
'head' => $head, 'siteIcon' => $site['favicon'] ?? '', 'headEditor' => true, 'faviconEditor' => true,
|
||||
'mcp' => McpSettings::read(),
|
||||
'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
|
||||
@@ -4,11 +4,11 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Content\Assets;
|
||||
use App\Content\CanvasStyle;
|
||||
use App\Content\Html;
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteIcon;
|
||||
use App\Content\View;
|
||||
use FastRoute\Dispatcher;
|
||||
use support\Request;
|
||||
@@ -209,7 +209,9 @@ final class SiteController
|
||||
|
||||
public function favicon(Request $request): Response
|
||||
{
|
||||
return View::redirect(Assets::DEFAULT_ICON, 301);
|
||||
$site = (new Repository())->site();
|
||||
|
||||
return SiteIcon::response($site['favicon'] ?? '', $request->path() === '/favicon.ico');
|
||||
}
|
||||
|
||||
public function canvasStyle(Request $request): Response
|
||||
|
||||
@@ -6,6 +6,7 @@ namespace App\Middleware;
|
||||
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteIcon;
|
||||
use App\Content\SystemPage;
|
||||
use App\Controller\SiteController;
|
||||
use Webman\Http\Request;
|
||||
@@ -29,7 +30,8 @@ final class SiteStatus implements MiddlewareInterface
|
||||
if (!empty($site['maintenance_enabled']) && ($page = SystemPage::published($repo, 'maintenance', $pages['maintenance'] ?? null))) {
|
||||
// Media and styles must remain available for the maintenance page and the admin canvas.
|
||||
if (in_array($request->method(), ['GET', 'HEAD'], true)
|
||||
&& preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+\.css|canvas-style\.css|favicon\.ico)$~D', $path)) {
|
||||
&& ($path === SiteIcon::PATH
|
||||
|| preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+\.css|canvas-style\.css|favicon\.ico)$~D', $path))) {
|
||||
$asset = $handler($request);
|
||||
if ($asset->getStatusCode() !== 404) {
|
||||
return $asset;
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
<?= View::fragment('admin-feedback', ['error' => $error, 'saved' => false]) ?>
|
||||
<section class="admin-panel">
|
||||
<h2>从备份还原</h2>
|
||||
<p>还原会替换现有页面、文章、设计配置及媒体目录记录。先导出当前站点,再选择需要还原的文件。</p>
|
||||
<p>还原会替换页面、文章、设计配置及媒体记录,并应用备份中的 Head 代码。先导出当前站点,再选择可信的备份。</p>
|
||||
<form class="admin-form" method="post" action="/admin/backup" enctype="multipart/form-data">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<label>LaySite 备份<input type="file" name="backup" accept=".tar,application/x-tar" required></label>
|
||||
|
||||
@@ -31,12 +31,21 @@ $active = match (true) {
|
||||
<link rel="stylesheet" href="/assets/page-template-drawer.css">
|
||||
<script src="/assets/page-template-drawer.js" defer></script>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($canvasSourceEditor)): ?>
|
||||
<?php if (!empty($canvasSourceEditor) || !empty($headEditor)): ?>
|
||||
<link rel="stylesheet" href="/assets/vendor/codemirror/codemirror.min.css">
|
||||
<script src="/assets/vendor/codemirror/codemirror.min.js" defer></script>
|
||||
<script src="/assets/vendor/codemirror/xml.min.js" defer></script>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($canvasSourceEditor)): ?>
|
||||
<script src="/assets/vendor/js-beautify/beautify-html.min.js" defer></script>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($headEditor)): ?>
|
||||
<script src="/assets/vendor/codemirror/css.min.js" defer></script>
|
||||
<script src="/assets/vendor/codemirror/javascript.min.js" defer></script>
|
||||
<script src="/assets/vendor/codemirror/htmlmixed.min.js" defer></script>
|
||||
<script src="/assets/head-editor.js" defer></script>
|
||||
<?php endif ?>
|
||||
<?php if (!empty($faviconEditor)): ?><script src="/assets/favicon-editor.js" defer></script><?php endif ?>
|
||||
<?php if (!empty($articleEditor)): ?>
|
||||
<link rel="stylesheet" href="/assets/vendor/toastui/toastui-editor.min.css">
|
||||
<script src="<?= $e(Assets::TOAST_UI_EDITOR) ?>" defer></script>
|
||||
|
||||
@@ -281,6 +281,9 @@ $spacingPresets = [0, 4, 8, 12, 16, 24, 32, 48, 64, 96, 128];
|
||||
<label>发布状态<select name="status"><option value="draft" <?= $page['status'] === 'draft' ? 'selected' : '' ?>>草稿</option>
|
||||
<option value="published" <?= $page['status'] === 'published' ? 'selected' : '' ?>>已发布</option></select></label>
|
||||
<?php endif ?>
|
||||
<?php if (!$layout): ?>
|
||||
<?= View::fragment('head-settings', ['label' => '本页 Head', 'head' => $page['head_html']]) ?>
|
||||
<?php endif ?>
|
||||
<div class="form-submit"><button class="button button-primary" type="submit"><?= View::fragment('studio-icon', ['icon' => 'save']) ?>保存页面</button></div>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use App\Content\SiteIcon;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<fieldset class="settings-stack favicon-settings" data-favicon-editor data-default-icon="<?= $e(SiteIcon::url('')) ?>">
|
||||
<legend>站点图标</legend>
|
||||
<input type="hidden" name="favicon_action" value="keep">
|
||||
<input type="hidden" name="favicon_png" value="">
|
||||
<div class="favicon-editor-row">
|
||||
<div class="favicon-preview">
|
||||
<img src="<?= $e(SiteIcon::url($siteIcon)) ?>" width="<?= SiteIcon::SIZE ?>" height="<?= SiteIcon::SIZE ?>" alt="当前站点图标" data-favicon-current>
|
||||
<canvas width="<?= SiteIcon::SIZE ?>" height="<?= SiteIcon::SIZE ?>" role="img" aria-label="图标裁剪预览" data-favicon-canvas hidden></canvas>
|
||||
</div>
|
||||
<div class="favicon-editor-controls">
|
||||
<label>从电脑选择图片<input type="file" data-favicon-file accept=".png,.jpg,.jpeg,.webp,.gif,.avif,.ico,.svg"></label>
|
||||
<p class="field-note">裁剪时预览效果,保存后自动生成 PNG 和 ICO 图标。</p>
|
||||
<div class="actions">
|
||||
<button class="button button-outline" type="button" data-favicon-library><?= View::fragment('studio-icon', ['icon' => 'media']) ?>从媒体库选择</button>
|
||||
<button class="button button-outline" type="button" data-favicon-reset<?= $siteIcon === '' ? ' hidden' : '' ?>>恢复默认</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="favicon-crop-controls" data-favicon-controls hidden>
|
||||
<label>缩放<input type="range" min="1" max="4" step=".01" value="1" data-favicon-zoom></label>
|
||||
<label>水平位置<input type="range" min="0" max="100" value="50" data-favicon-x></label>
|
||||
<label>垂直位置<input type="range" min="0" max="100" value="50" data-favicon-y></label>
|
||||
</div>
|
||||
<p class="field-note" data-favicon-status role="status"></p>
|
||||
</fieldset>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
use App\Content\PageHead;
|
||||
use App\Content\View;
|
||||
|
||||
$e = View::escape(...);
|
||||
?>
|
||||
<details class="admin-options head-settings" data-head-settings>
|
||||
<summary><?= $e($label) ?></summary>
|
||||
<div class="head-settings-fields">
|
||||
<label id="head-source-label" for="head-source">Head HTML</label>
|
||||
<textarea id="head-source" name="head_html" data-head-source rows="10" spellcheck="false"
|
||||
autocapitalize="off" maxlength="<?= PageHead::MAX_LENGTH ?>"><?= $e($head) ?></textarea>
|
||||
<p class="field-note">可添加 meta、样式表、style 和 script。全站代码先加载,本页代码随后加载。保存后,画布载入样式,脚本在独立预览和公开页面运行。</p>
|
||||
<div class="actions">
|
||||
<button class="button button-outline" type="button" data-head-asset="text/css"><?= View::fragment('studio-icon', ['icon' => 'add']) ?>插入样式表</button>
|
||||
<button class="button button-outline" type="button" data-head-asset="text/javascript"><?= View::fragment('studio-icon', ['icon' => 'add']) ?>插入脚本</button>
|
||||
</div>
|
||||
</div>
|
||||
</details>
|
||||
+5
-2
@@ -3,6 +3,7 @@ use App\Content\View;
|
||||
use App\Content\Assets;
|
||||
use App\Content\CanvasStyle;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\SiteIcon;
|
||||
|
||||
$e = View::escape(...);
|
||||
$canonical = $path !== null && empty($previewMode) ? View::absolute($path) : null;
|
||||
@@ -15,6 +16,7 @@ $palette = DesignLibrary::find((int) ($page['palette_id'] ?? 0), 'palette');
|
||||
$showHeader = $page['visibility']['header'] ?? true;
|
||||
$showFooter = $page['visibility']['footer'] ?? true;
|
||||
$canvasStyles = $content . ($headerMarkup ?? '') . ($footerMarkup ?? '');
|
||||
$favicon = $site['favicon'] ?? '';
|
||||
?>
|
||||
<!doctype html>
|
||||
<html lang="zh-CN">
|
||||
@@ -22,7 +24,7 @@ $canvasStyles = $content . ($headerMarkup ?? '') . ($footerMarkup ?? '');
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title><?= $e($title) ?></title>
|
||||
<link rel="icon" href="<?= $e(Assets::DEFAULT_ICON) ?>" type="image/webp">
|
||||
<link rel="icon" href="<?= $e(SiteIcon::url($favicon)) ?>" type="<?= $favicon === '' ? 'image/webp' : 'image/png' ?>"<?= $favicon === '' ? '' : ' sizes="64x64"' ?>>
|
||||
<meta name="description" content="<?= $e($description) ?>">
|
||||
<meta name="theme-color" content="#f5f7fb">
|
||||
<?php if ($canonical): ?>
|
||||
@@ -46,11 +48,12 @@ $canvasStyles = $content . ($headerMarkup ?? '') . ($footerMarkup ?? '');
|
||||
<meta name="twitter:card" content="summary_large_image">
|
||||
<link rel="stylesheet" href="/assets/site.css">
|
||||
<?php if (!empty($canvas) || str_contains($canvasStyles, 'studio-style-') || str_contains($canvasStyles, 'studio-media-')): ?><link rel="stylesheet" href="<?= $e(CanvasStyle::stylesheetUrl()) ?>"><?php endif ?>
|
||||
<?php if (!empty($styleNonce)): ?><style nonce="<?= $e($styleNonce) ?>" data-canvas-colors><?= $canvasColors ?></style><?php endif ?>
|
||||
<?php if (!empty($canvas) || $canvasColors !== ''): ?><style nonce="<?= $e($styleNonce) ?>" data-canvas-colors><?= $canvasColors ?></style><?php endif ?>
|
||||
<?php if ($theme !== ''): ?><link rel="stylesheet" href="/assets/page-themes.css"><?php endif ?>
|
||||
<?php if ($palette): ?><link rel="stylesheet" data-page-palette href="/palette/<?= $e($palette['id']) ?>.css?v=<?= $e($palette['version']) ?>"><?php endif ?>
|
||||
<script src="<?= $e(Assets::JQUERY) ?>" defer></script>
|
||||
<script src="/assets/site.js" defer></script>
|
||||
<?= $headMarkup ?>
|
||||
</head>
|
||||
<body>
|
||||
<a class="skip-link" href="#main-content">跳到内容</a>
|
||||
|
||||
@@ -8,8 +8,8 @@ $e = View::escape(...);
|
||||
<form class="admin-form admin-panel" method="post" action="/admin/media" enctype="multipart/form-data">
|
||||
<input type="hidden" name="csrf" value="<?= $e($token) ?>">
|
||||
<div class="form-grid media-upload-fields">
|
||||
<label>文件<input type="file" name="file" accept="image/*,video/*,audio/*,.pdf,.woff,.woff2" required>
|
||||
<small>支持常见图片、SVG 图标、音视频、PDF、WOFF/WOFF2,单文件最大 64 MB。</small>
|
||||
<label>文件<input type="file" name="file" accept="image/*,video/*,audio/*,.pdf,.woff,.woff2,.css,.js" required>
|
||||
<small>图片、音视频、PDF 和网页字体最大 64 MB,CSS 和 JavaScript 最大 2 MB。</small>
|
||||
</label>
|
||||
<label>资源名称<input name="label" maxlength="160" placeholder="默认使用文件名"></label>
|
||||
</div>
|
||||
@@ -19,7 +19,7 @@ $e = View::escape(...);
|
||||
<h2 id="library-heading">资源列表</h2>
|
||||
<div class="form-grid admin-form media-filters">
|
||||
<label>搜索<input type="search" data-library-query placeholder="搜索名称或路径"></label>
|
||||
<label>类型<select data-library-type><option value="">全部资源</option><option value="image/">图片与图标</option><option value="video/">视频</option><option value="audio/">音频</option><option value="other">其他文件</option></select></label>
|
||||
<label>类型<select data-library-type><option value="">全部资源</option><option value="image/">图片与图标</option><option value="video/">视频</option><option value="audio/">音频</option><option value="text/">CSS 与 JavaScript</option><option value="other">其他文件</option></select></label>
|
||||
</div>
|
||||
<p data-library-count role="status"></p>
|
||||
<div class="media-grid">
|
||||
|
||||
@@ -12,6 +12,8 @@ $e = View::escape(...);
|
||||
<input type="hidden" name="version" value="<?= $e($version) ?>">
|
||||
<label>站点名称<input name="name" value="<?= $e($name) ?>" maxlength="100" required></label>
|
||||
<label>静态资源 CDN<input name="cdn" value="<?= $e($cdn) ?>" placeholder="https://cdn.example.com"><small>填写后,公开页面的 /assets/ 文件从该地址加载。</small></label>
|
||||
<?= View::fragment('favicon-settings', compact('siteIcon')) ?>
|
||||
<?= View::fragment('head-settings', ['label' => '全站 Head', 'head' => $head]) ?>
|
||||
<fieldset class="settings-stack">
|
||||
<legend>系统页面</legend>
|
||||
<?php foreach (['maintenance' => ['maintenance_page', '维护页', '选择已发布的维护页'], 'not-found' => ['not_found_page', '404 页', '使用系统 404 页']] as $type => [$field, $label, $placeholder]): ?>
|
||||
|
||||
Reference in New Issue
Block a user