diff --git a/README.md b/README.md index 19b0baa..a238be3 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,11 @@ php start.php start ## 页面与站点设计 - 在「页面」新建空白草稿或从整页模板创建。画布支持编辑、媒体拖拽调整宽度、移动端预览、区块复制与删除。 -- 「图层」可定位或移除首屏,也可调整独立区块的顺序。保存页面后生效。移除的首屏可通过撤销或「页面设置 → 页面显示」恢复。 -- 选中元素可编辑四边的内外边距。选中文字后可修改字号、粗体、斜体和删除线。Enter 或 Shift+Enter 在当前元素内换行。正文用 Ctrl+Enter(macOS 上为 ⌘+Enter)或工具栏「换段」新建段落,列表中则新增列表项。 +- 「图层」可展开到最内层的元素。可拖拽调整区块及内容元素的顺序,也可用箭头在同层移动或用定位图标选中画布中的元素。生成的区块外壳和动态文章列表保留其结构。移除的首屏可通过撤销或「页面设置 → 页面显示」恢复。 +- 选中最外层区块的「编辑 HTML」可修改正文、标题和简介源码。首屏还可以转为完整自定义 HTML,并随时恢复为模板首屏。应用前服务端会清理 HTML,应用后可撤销,保存页面后才会写入站点。 +- 点击面包屑或目录文案可就地编辑,双击或打开属性可修改链接。「页面设置 → 首屏与目录」可管理整组链接。模板内其他导航与常规内容一起编辑。 +- 发布的普通页面提供「查看公开页面」,显示访客实际看到的版本。草稿、页头、页脚和系统页面提供后台预览,显示已保存的内容。未保存的改动可在画布上查看。 +- 选中元素可编辑四边的内外边距。选中文字后可修改字号、颜色、粗体、斜体和删除线。选中图片可拖拽调整宽度,也可在工具栏选择块级、行内或行内块布局。Enter 或 Shift+Enter 在当前元素内换行。正文用 Ctrl+Enter(macOS 上为 ⌘+Enter)或工具栏「换段」新建段落,列表中则新增列表项。 - 画布支持 Ctrl/⌘+Z 撤销和 Ctrl/⌘+Shift+Z 重做。页面设置的输入框保留浏览器原生撤销。 - 选中按钮后可复制。在「属性」中选择纯色填充、线框、下划线或无样式,并选择是否在新标签页打开。首屏支持最多八个按钮。 - 在「网站页面 → 新建页面」选择普通页面、页头、页脚、维护页或 404 页。它们共用同一个画布。页头页脚版式在「组件 → 页头/页脚」,也可添加封面、视频、文章等常规区块,或在页面设置中显示首屏。页脚组件默认附带可编辑的 Powered by LaySite 链接。 @@ -106,7 +109,7 @@ php bin/backup.php export /srv/backups/laysite-2026-11.tar php bin/backup.php import /srv/backups/laysite-2026-11.tar --replace ``` -可在「账户 → 关于系统」点击「检查更新」,比较本机版本与 `git.laysense.com/laysense/laysite` 的 `main` 分支版本。 +可在「账户 → 关于系统」点击「检查更新」,比较本机版本与 `git.laysense.com/laysense/laysite` 的最新 `vX.Y.Z` 发布标签。已合并但尚未打标签的版本不会出现在检查结果里。检查结果缓存 5 分钟。 使用 Git 标签选择部署版本。更新命令先备份,再检出所选版本、安装锁定依赖并运行 SQLite 初始化或迁移。更新时暂停 Webman,更新结束后重新启动。保持源代码工作区干净,并将备份放到部署路径之外。 diff --git a/VERSION b/VERSION index f0bb29e..88c5fb8 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.0 +1.4.0 diff --git a/app/Content/CanvasStyle.php b/app/Content/CanvasStyle.php index a28bfaa..8ef9822 100644 --- a/app/Content/CanvasStyle.php +++ b/app/Content/CanvasStyle.php @@ -8,7 +8,7 @@ use RuntimeException; final class CanvasStyle { - public const PAGE = ['hero', 'heading', 'accent', 'lead', 'image', 'actions', 'navigation']; + public const PAGE = ['hero', 'heading', 'accent', 'lead', 'eyebrow', 'image', 'actions', 'navigation', 'breadcrumbs']; public const SECTION = ['section', 'title', 'lead', 'body']; private const PROPERTIES = [ @@ -40,13 +40,34 @@ final class CanvasStyle public static function normalize(mixed $input, array $roles): array { - if (!is_array($input) || count($input) > count($roles) + 28) { + if (!is_array($input) || count($input) > count($roles) + 50) { throw new RuntimeException('画布样式无效。'); } $styles = []; foreach ($input as $role => $style) { + if ($roles === self::PAGE && $role === 'breadcrumbLinks') { + if (!is_string($style)) { + throw new RuntimeException('面包屑无效。'); + } + $styles[$role] = SiteDesign::linkText(SiteDesign::parseLinks($style)); + continue; + } + if ($roles === self::PAGE && $role === 'heroEyebrow') { + if (!is_string($style) || mb_strlen($style) > 100) { + throw new RuntimeException('首屏眉题过长。'); + } + $styles[$role] = $style; + continue; + } + if ($roles === self::PAGE && $role === 'heroHtml') { + if (!is_string($style) || strlen($style) > 300000) { + throw new RuntimeException('首屏 HTML 无效或过长。'); + } + $styles[$role] = Html::sanitize($style); + continue; + } if ((!in_array($role, $roles, true) && !($roles === self::PAGE && is_string($role) - && preg_match('/^(?:action-[0-7]|nav-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) { + && preg_match('/^(?:action-[0-7]|(?:nav|crumb)-(?:[0-9]|1[0-9]))$/D', $role))) || !is_array($style)) { throw new RuntimeException('画布样式无效。'); } $classes = self::classList($style['classes'] ?? ''); @@ -85,6 +106,10 @@ final class CanvasStyle [$property, $size] = array_pad(explode('-', $token, 2), 2, ''); if (isset(self::TYPOGRAPHY[$token])) { $classes[$property] = $class; + } elseif ($property === 'color' && ($size === 'theme' || preg_match('/^[a-f0-9]{6}$/D', $size))) { + $classes[$property] = $class; + } elseif (!$textOnly && $property === 'display' && in_array($size, ['block', 'inline', 'inline-block'], true)) { + $classes[$property] = $class; } elseif ((!$textOnly || $property === 'size') && isset(self::PROPERTIES[$property]) && preg_match('/^(?:0|-?[1-9]\d{0,2})$/D', $size)) { [, $min, $max] = self::PROPERTIES[$property]; @@ -131,6 +156,17 @@ final class CanvasStyle return $markup; } + public static function colorRules(string $markup): string + { + preg_match_all('/\bstudio-style-color-([a-f0-9]{6})(?![\w-])/', $markup, $matches); + $css = ''; + foreach (array_unique($matches[1]) as $hex) { + $css .= ".studio-style-color-{$hex}{color:#{$hex}!important}\n"; + } + + return $css; + } + public static function stylesheet(): string { $css = ''; @@ -139,6 +175,9 @@ final class CanvasStyle $css .= '.' . self::PREFIX . "$key-$value{{$property}:{$value}px!important}\n"; } } + for ($width = 10; $width <= 100; $width++) { + $css .= ".studio-media-{$width}{--studio-media-factor:calc({$width} / 100)}\n"; + } foreach (self::TYPOGRAPHY as $key => $value) { $css .= '.' . self::PREFIX . "$key{{$value}!important}\n"; } diff --git a/app/Content/Updates.php b/app/Content/Updates.php index dc21d16..e6818c4 100644 --- a/app/Content/Updates.php +++ b/app/Content/Updates.php @@ -10,26 +10,133 @@ use RuntimeException; final class Updates { public const REPOSITORY = 'https://git.laysense.com/laysense/laysite'; + private const TAGS = 'https://git.laysense.com/api/v1/repos/laysense/laysite/tags'; public static function current(): string { - return trim(file_get_contents(dirname(__DIR__, 2) . '/VERSION')); + $version = trim((string) @file_get_contents(dirname(__DIR__, 2) . '/VERSION')); + + return preg_match('/^\d+\.\d+\.\d+$/D', $version) ? $version : '0.0.0'; } public static function check(): array { - $curl = curl_init(self::REPOSITORY . '/raw/branch/main/VERSION'); + $directory = dirname(__DIR__, 2) . '/runtime'; + if (!is_dir($directory) && !@mkdir($directory, 0750, true) && !is_dir($directory)) { + throw new RuntimeException('无法创建版本检查缓存。'); + } + $path = $directory . '/laysite-update.json'; + $record = self::cached($path); + if (($record['until'] ?? 0) <= time()) { + $lock = @fopen($path . '.lock', 'c'); + if (!$lock) { + throw new RuntimeException('无法打开版本检查缓存。'); + } + try { + if (!flock($lock, LOCK_EX | LOCK_NB)) { + if (!array_key_exists('latest', $record)) { + throw new RuntimeException('版本检查正在进行,请稍后重试。'); + } + } else { + $record = self::cached($path); + if (($record['until'] ?? 0) <= time()) { + try { + $record = ['latest' => self::latestTag(), 'until' => time() + 300]; + } catch (RuntimeException $exception) { + $record = ['error' => $exception->getMessage(), 'until' => time() + 30]; + } + self::writeCache($path, $record); + } + } + } finally { + fclose($lock); + } + } + if (isset($record['error'])) { + throw new RuntimeException($record['error']); + } + $current = self::current(); + $latest = $record['latest']; + + return ['current' => $current, 'latest' => $latest, 'comparison' => $latest === null ? null : version_compare($latest, $current)]; + } + + private static function cached(string $path): array + { + $record = json_decode((string) @file_get_contents($path), true); + + if (!is_array($record) || !is_int($record['until'] ?? null)) { + return []; + } + if (is_string($record['error'] ?? null)) { + return $record; + } + $latest = $record['latest'] ?? null; + + return array_key_exists('latest', $record) && ($latest === null || (is_string($latest) + && preg_match('/^\d+\.\d+\.\d+$/D', $latest))) ? $record : []; + } + + private static function writeCache(string $path, array $record): void + { + $temporary = @tempnam(dirname($path), 'laysite-update-'); + if ($temporary === false) { + throw new RuntimeException('无法写入版本检查缓存。'); + } + try { + if (file_put_contents($temporary, Database::json($record)) === false || !rename($temporary, $path)) { + throw new RuntimeException('无法写入版本检查缓存。'); + } + } finally { + if (is_file($temporary)) { + unlink($temporary); + } + } + } + + private static function latestTag(): ?string + { + $latest = null; + $deadline = hrtime(true) + 3_000_000_000; + $page = 1; + do { + $remaining = (int) (($deadline - hrtime(true)) / 1_000_000); + if ($remaining <= 0) { + throw new RuntimeException('读取上游版本超时,请稍后重试。'); + } + $tags = self::tags($page++, $remaining); + foreach ($tags as $tag) { + $name = $tag['name'] ?? null; + if (!is_string($name)) { + throw new RuntimeException('上游仓库返回的版本列表无效。'); + } + if (preg_match('/^v(\d+\.\d+\.\d+)$/D', $name, $match) + && ($latest === null || version_compare($match[1], $latest, '>'))) { + $latest = $match[1]; + } + } + } while (count($tags) === 50); + + return $latest; + } + + private static function tags(int $page, int $timeout): array + { + $curl = curl_init(self::TAGS . '?limit=50&page=' . $page); if ($curl === false) { throw new RuntimeException('无法连接上游仓库,请稍后重试。'); } $body = ''; + $overflow = false; curl_setopt_array($curl, [ - CURLOPT_CONNECTTIMEOUT_MS => 2500, - CURLOPT_TIMEOUT_MS => 6000, + CURLOPT_CONNECTTIMEOUT_MS => min(1500, $timeout), + CURLOPT_TIMEOUT_MS => $timeout, CURLOPT_FOLLOWLOCATION => false, + CURLOPT_HTTPHEADER => ['Accept: application/json'], CURLOPT_USERAGENT => 'LaySite version check', - CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body): int { - if (strlen($body) + strlen($chunk) > 128) { + CURLOPT_WRITEFUNCTION => static function (CurlHandle $handle, string $chunk) use (&$body, &$overflow): int { + if (strlen($body) + strlen($chunk) > 256 * 1024) { + $overflow = true; return 0; } $body .= $chunk; @@ -37,15 +144,19 @@ final class Updates return strlen($chunk); }, ]); - if (curl_exec($curl) === false || curl_getinfo($curl, CURLINFO_RESPONSE_CODE) !== 200) { - throw new RuntimeException('无法读取上游版本,请稍后重试。'); + if (curl_exec($curl) === false) { + throw new RuntimeException($overflow ? '上游版本数据超出长度限制。' + : (curl_errno($curl) === CURLE_OPERATION_TIMEDOUT ? '读取上游版本超时,请稍后重试。' : '无法连接上游仓库,请稍后重试。')); } - if (!preg_match('/^v?(\d+\.\d+\.\d+)$/D', trim($body), $version)) { - throw new RuntimeException('上游仓库返回的版本格式无效。'); + $status = curl_getinfo($curl, CURLINFO_RESPONSE_CODE); + if ($status !== 200) { + throw new RuntimeException('上游版本列表暂不可用(HTTP ' . $status . ')。'); + } + $tags = json_decode($body); + if (!is_array($tags) || !array_is_list($tags)) { + throw new RuntimeException('上游仓库返回的版本列表无效。'); } - $current = self::current(); - $latest = $version[1]; - return ['current' => $current, 'latest' => $latest, 'comparison' => version_compare($latest, $current)]; + return array_map(static fn ($tag): array => is_object($tag) ? get_object_vars($tag) : [], $tags); } } diff --git a/app/Content/View.php b/app/Content/View.php index ebf4198..fa5854a 100644 --- a/app/Content/View.php +++ b/app/Content/View.php @@ -49,8 +49,11 @@ 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)) : ''; $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']}'"); $articleEditor = $admin && !empty($data['articleEditor']); $editorImages = $articleEditor ? ' data:' : ''; $editorStyles = $articleEditor ? "style-src-attr 'unsafe-inline'; " : ''; @@ -60,7 +63,7 @@ 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 $assetSources; {$editorStyles}script-src $assetSources; font-src $assetSources; frame-src 'self'; frame-ancestors " + . "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'", ]; if ($admin || !empty($data['previewMode'])) { diff --git a/app/Controller/AdminController.php b/app/Controller/AdminController.php index a747879..81846c2 100644 --- a/app/Controller/AdminController.php +++ b/app/Controller/AdminController.php @@ -387,7 +387,7 @@ class AdminController if ($data['name'] === '' || mb_strlen($data['name']) > 100 || !in_array($data['status'], ['draft', 'published'], true) || $data['title'] === '' || mb_strlen($data['title']) > 160 || (!$layout && $page['page_type'] === 'page' && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 || - (($data['visibility']['hero'] ?? true) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 || + (($data['visibility']['hero'] ?? true) && !array_key_exists('heroHtml', $data['presentation']) && $data['heading'] === '') || mb_strlen($data['heading'] . $data['accent']) > 110 || mb_strlen($data['lead']) > 600 || mb_strlen($data['alt']) > 200 || ($data['image_scale'] !== 0 && ($data['image_scale'] < 10 || $data['image_scale'] > 100)) || strlen($data['image']) > 1000 || ($data['image'] !== '' && !Html::url($data['image'], true))) { @@ -414,7 +414,7 @@ class AdminController 'title' => '编辑 ' . $page['name'], 'page' => $page, 'categories' => $repo->categories(), 'wideAdmin' => true, 'error' => $error, 'saved' => $request->get('saved') === '1', 'pageTemplateDrawer' => !$layout, 'designs' => DesignLibrary::all(), 'layout' => $layout, 'layoutDefault' => $default, 'layoutBase' => $layout && ($site['layout_defaults'][$layoutKind] ?? null) === $layout['id'], - 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']), + 'siteName' => $site['name'], 'layoutHasMember' => $layoutKind === 'header' && LayoutPage::hasMember($page['sections']), 'layoutDefaults' => array_intersect_key($site, ['header_id' => true, 'footer_id' => true]), ], $error !== '' ? 422 : 200); } diff --git a/app/view/admin-page.php b/app/view/admin-page.php index ffa88f8..fe5130a 100644 --- a/app/view/admin-page.php +++ b/app/view/admin-page.php @@ -31,7 +31,9 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null; = View::fragment('studio-icon', ['icon' => 'settings']) ?>站点设置 = View::fragment('admin-view-link', ['href' => $path, 'icon' => 'external']) ?> - = View::fragment('admin-view-link', ['href' => $previewUrl, 'icon' => 'preview', 'label' => '打开预览', 'showLabel' => true]) ?> + + = View::fragment('admin-view-link', ['href' => $previewUrl, 'icon' => 'preview', 'label' => '预览已保存内容']) ?> + = View::fragment('admin-feedback', compact('error', 'saved')) ?> @@ -62,10 +64,15 @@ $systemLabel = SystemPage::TYPES[$page['page_type']] ?? null;