Migrate Laysense to unmodified LaySite 1.5.2
This commit is contained in:
@@ -11,6 +11,9 @@ use App\Content\DesignLibrary;
|
||||
use App\Content\Database;
|
||||
use App\Content\Html;
|
||||
use App\Content\LayoutPage;
|
||||
use App\Content\PageHead;
|
||||
use App\Content\PageDraft;
|
||||
use App\Content\SystemPage;
|
||||
use App\Content\Licenses;
|
||||
use App\Content\Media;
|
||||
use App\Content\Passwords;
|
||||
@@ -126,7 +129,7 @@ class AdminController
|
||||
|
||||
return $this->render($request, 'admin-dashboard', [
|
||||
'title' => '内容管理', 'pages' => $repo->pages(), 'articles' => $repo->articles(public: false, limit: 5),
|
||||
'site' => $repo->site(),
|
||||
'site' => $repo->site(), 'deleted' => $request->get('deleted') === '1',
|
||||
'pageTemplateDrawer' => true,
|
||||
]);
|
||||
}
|
||||
@@ -236,9 +239,25 @@ class AdminController
|
||||
return $this->render($request, 'admin-article-categories', [
|
||||
'title' => '文章分类', 'categories' => $repo->categories(), 'name' => $name, 'slug' => $slug,
|
||||
'error' => $error, 'saved' => $request->get('saved') === '1',
|
||||
'deleted' => $request->get('deleted') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
|
||||
public function deleteCategory(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
try {
|
||||
$deleted = (new Repository())->deleteCategory($slug);
|
||||
} catch (RuntimeException $exception) {
|
||||
return $this->error($request, $this->validationMessage($exception), 422);
|
||||
}
|
||||
|
||||
return $deleted ? $this->success($request, '/admin/article-categories?deleted=1')
|
||||
: $this->error($request, '找不到此文章分类。', 404);
|
||||
}
|
||||
|
||||
public function article(Request $request, ?string $id = null): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -247,10 +266,10 @@ class AdminController
|
||||
$repo = new Repository();
|
||||
$categories = $repo->categories();
|
||||
$preset = $request->get('category');
|
||||
$preset = is_string($preset) && isset($categories[$preset]) ? $preset : 'news';
|
||||
$preset = is_string($preset) && isset($categories[$preset]) ? $preset : (isset($categories['news']) ? 'news' : null);
|
||||
$article = $id !== null ? $repo->articleById((int) $id) : [
|
||||
'title' => '', 'slug' => '', 'summary' => '', 'body' => '', 'body_format' => 'markdown',
|
||||
'categories' => [$preset => $categories[$preset]], 'card' => [], 'tags' => [], 'status' => 'draft', 'version' => 0,
|
||||
'categories' => $preset === null ? [] : [$preset => $categories[$preset]], 'card' => [], 'tags' => [], 'status' => 'draft', 'version' => 0,
|
||||
];
|
||||
if (!$article) {
|
||||
return $this->error($request, '找不到这篇文章。', 404);
|
||||
@@ -266,7 +285,7 @@ class AdminController
|
||||
if (mb_strlen($data['title']) < 1 || mb_strlen($data['title']) > 120
|
||||
|| !preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/D', $data['slug']) || strlen($data['slug']) > 100
|
||||
|| mb_strlen($data['summary']) > 250 || strlen($data['body']) > 200000
|
||||
|| $data['categories'] === [] || array_diff($data['categories'], array_keys($categories))
|
||||
|| array_diff($data['categories'], array_keys($categories))
|
||||
|| !in_array($data['body_format'], ['markdown', 'html'], true)
|
||||
|| !in_array($data['status'], ['draft', 'published'], true)) {
|
||||
throw new RuntimeException('请检查标题、网址标识、分类、摘要和正文长度。');
|
||||
@@ -320,9 +339,11 @@ class AdminController
|
||||
return $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
$site = $repo->site();
|
||||
$originalLayoutIds = array_intersect_key($page, ['header_id' => true, 'footer_id' => true]);
|
||||
$layoutKind = $page['layout_kind'];
|
||||
$layout = $layoutKind === null ? null : DesignLibrary::find($page['layout_id'], $layoutKind);
|
||||
$default = $layout && ($site[$layoutKind . '_id'] ?? null) === $layout['id'];
|
||||
$headEditor = !$layout && array_key_exists('head_html', $page);
|
||||
$error = '';
|
||||
if ($request->method() === 'POST') {
|
||||
$data = [];
|
||||
@@ -333,15 +354,43 @@ class AdminController
|
||||
$data[$field] = CanvasStyle::lineBreaks($data[$field]);
|
||||
}
|
||||
$data['name'] = $request->post('name') === null ? $page['name'] : $this->input($request, 'name');
|
||||
if ($layout) {
|
||||
$data['title'] = $data['name'];
|
||||
foreach (['header', 'footer'] as $kind) {
|
||||
$id = $this->input($request, 'original_' . $kind . '_id');
|
||||
if (ctype_digit($id)) {
|
||||
$data['originalLayoutIds'][$kind] = (int) $id;
|
||||
}
|
||||
}
|
||||
$data['status'] = $layout || $request->post('status') === null || $slug === 'home' ? $page['status'] : $this->input($request, 'status');
|
||||
$publication = $request->post('status');
|
||||
$data['status'] = $layout || $publication === null ? $page['status'] : $this->input($request, 'status');
|
||||
$scale = $this->input($request, 'image_scale');
|
||||
$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 {
|
||||
$previousType = $layoutKind ?? $page['page_type'];
|
||||
$type = $request->post('page_type') === null ? $previousType : $this->input($request, 'page_type');
|
||||
if (!isset((LayoutPage::TYPES + SystemPage::TYPES)[$type])) {
|
||||
throw new RuntimeException('请选择可用的页面类型。');
|
||||
}
|
||||
$target = $request->post('page_slug') === null ? $slug : $this->input($request, 'page_slug');
|
||||
$data['slug'] = $target === $slug ? $slug : PageDraft::slug($target);
|
||||
$data['type'] = $type;
|
||||
$public = in_array($type, ['page', 'home'], true);
|
||||
if (in_array($type, ['header', 'footer'], true)) {
|
||||
$data['status'] = 'published';
|
||||
} elseif ($public && !in_array($previousType, ['page', 'home'], true)) {
|
||||
$data['status'] = 'draft';
|
||||
}
|
||||
if ($head !== null) {
|
||||
if (!$headEditor) {
|
||||
throw new RuntimeException(Database::MIGRATION_REQUIRED);
|
||||
}
|
||||
$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) {
|
||||
@@ -392,7 +441,7 @@ class AdminController
|
||||
$data['quicklinks'] = $request->post('quicklinks') === null ? $page['quicklinks'] : SiteDesign::parseLinks($this->input($request, 'quicklinks'));
|
||||
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 ||
|
||||
($public && $data['status'] === 'published' && $data['description'] === '') || mb_strlen($data['description']) > 300 ||
|
||||
(($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'] < CanvasStyle::MIN_MEDIA_SCALE || $data['image_scale'] > 100)) ||
|
||||
@@ -408,9 +457,24 @@ class AdminController
|
||||
throw new RuntimeException('区块顺序无效。');
|
||||
}
|
||||
}
|
||||
$repo->savePage($slug, $data, $this->version($request), $sections, sectionOrder: $order === '' ? null : $order);
|
||||
$saved = $repo->savePage($slug, $data, $this->version($request), $sections, sectionOrder: $order === '' ? null : $order);
|
||||
$editor = '/admin/pages/' . $saved['slug'] . '?saved=1';
|
||||
if ($request->expectsJson()) {
|
||||
if ($target !== $slug || $type !== $previousType) {
|
||||
return $this->json(['saved' => true, 'redirect' => $editor]);
|
||||
}
|
||||
return $this->json([
|
||||
'saved' => true, 'version' => $saved['version'], 'name' => $saved['name'], 'status' => $saved['status'],
|
||||
'layoutIds' => array_intersect_key($saved, $originalLayoutIds),
|
||||
'sections' => array_map(static function (array $section) use ($sections): array {
|
||||
$key = $section['key'];
|
||||
return ['key' => $key, 'version' => $section['version']]
|
||||
+ (isset($sections[$key]) ? ['body' => $section['body'], 'feed' => $section['feed']] : []);
|
||||
}, $saved['sections']),
|
||||
]);
|
||||
}
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug . '?saved=1');
|
||||
return $this->success($request, $editor);
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $this->validationMessage($exception);
|
||||
}
|
||||
@@ -418,13 +482,30 @@ 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' => $headEditor, 'designs' => DesignLibrary::layouts(), '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]),
|
||||
'palettes' => DesignLibrary::palettes(), 'defaultPaletteId' => $site['palette_id'] ?? null,
|
||||
'originalLayoutIds' => $originalLayoutIds,
|
||||
], $error !== '' ? 422 : 200);
|
||||
}
|
||||
|
||||
public function deletePage(Request $request, string $slug): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
try {
|
||||
$deleted = (new Repository())->deletePage($slug, $this->version($request));
|
||||
} catch (RuntimeException $exception) {
|
||||
return $this->error($request, $this->validationMessage($exception), 422);
|
||||
}
|
||||
|
||||
return $deleted ? $this->success($request, '/admin?deleted=1')
|
||||
: $this->error($request, '找不到此页面。', 404);
|
||||
}
|
||||
|
||||
public function section(Request $request, string $slug, string $key): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -592,7 +673,7 @@ class AdminController
|
||||
protected function guard(Request $request): ?Response
|
||||
{
|
||||
if (!$this->signedIn($request)) {
|
||||
return $request->isAjax()
|
||||
return $request->expectsJson()
|
||||
? $this->json(['message' => '登录已过期。请在新标签页重新登录,再返回继续编辑。'], 401)
|
||||
: View::redirect('/admin/login');
|
||||
}
|
||||
@@ -729,8 +810,8 @@ class AdminController
|
||||
{
|
||||
$categories = $this->selectedCategories($selected);
|
||||
$limit = filter_var($value, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 30]]);
|
||||
if (!in_array($mode, ['merged', 'tabs', 'network', 'catalog'], true) || !$categories || array_diff($categories, array_keys($available)) || $limit === false) {
|
||||
throw new RuntimeException('请选择文章分类,并将展示数量设为 1 至 30。');
|
||||
if (!in_array($mode, ['merged', 'tabs', 'network', 'catalog'], true) || array_diff($categories, array_keys($available)) || $limit === false) {
|
||||
throw new RuntimeException('请检查展示方式、分类和展示数量(1 至 30)。');
|
||||
}
|
||||
|
||||
return Database::json(compact('categories', 'mode', 'limit'));
|
||||
@@ -749,7 +830,7 @@ class AdminController
|
||||
|
||||
protected function success(Request $request, string $path): Response
|
||||
{
|
||||
return $request->isAjax() ? $this->json(['redirect' => $path]) : View::redirect($path);
|
||||
return $request->expectsJson() ? $this->json(['redirect' => $path]) : View::redirect($path);
|
||||
}
|
||||
|
||||
protected function json(array $payload, int $status = 200): Response
|
||||
@@ -765,7 +846,7 @@ class AdminController
|
||||
protected function render(Request $request, string $view, array $data, int $status = 200): Response
|
||||
{
|
||||
$token = $this->token($request);
|
||||
if ($request->isAjax() && $status >= 400) {
|
||||
if ($request->expectsJson() && $status >= 400) {
|
||||
return $this->json([
|
||||
'message' => $data['error'] ?? $data['title'],
|
||||
...(!empty($data['field']) ? ['field' => $data['field']] : []),
|
||||
|
||||
@@ -7,8 +7,12 @@ namespace App\Controller;
|
||||
use App\Content\Assets;
|
||||
use App\Content\DesignFieldError;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\McpSettings;
|
||||
use App\Content\PageHead;
|
||||
use App\Content\PageTemplates;
|
||||
use App\Content\Repository;
|
||||
use App\Content\SiteDesign;
|
||||
use App\Content\SiteIcon;
|
||||
use App\Content\SystemPage;
|
||||
use App\Content\View;
|
||||
use App\Http\Request;
|
||||
@@ -24,10 +28,26 @@ final class DesignController extends AdminController
|
||||
}
|
||||
|
||||
return $this->render($request, 'admin-design', [
|
||||
'title' => '站点设计', 'items' => DesignLibrary::all('palette'),
|
||||
'title' => '站点设计', 'items' => DesignLibrary::palettes(), 'defaultPaletteId' => (new Repository())->site()['palette_id'] ?? null,
|
||||
'deleted' => $request->get('deleted') === '1',
|
||||
]);
|
||||
}
|
||||
|
||||
public function deletePalette(Request $request, string $id): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
return $failure;
|
||||
}
|
||||
try {
|
||||
$deleted = DesignLibrary::deletePalette((int) $id, $this->version($request));
|
||||
} catch (RuntimeException $error) {
|
||||
return $this->error($request, $this->validationMessage($error), 422);
|
||||
}
|
||||
|
||||
return $deleted ? $this->success($request, '/admin/design?deleted=1#palettes')
|
||||
: $this->error($request, '找不到这项配色方案。', 404);
|
||||
}
|
||||
|
||||
public function edit(Request $request, string $kind, string $key): Response
|
||||
{
|
||||
if ($failure = $this->guard($request)) {
|
||||
@@ -83,11 +103,19 @@ final class DesignController extends AdminController
|
||||
$error = '';
|
||||
$name = $site['name'];
|
||||
$cdn = $site['cdn']['origin'] ?? '';
|
||||
$head = $site['head_html'] ?? '';
|
||||
$paletteId = $site['palette_id'] ?? null;
|
||||
$systemPages = $site['system_pages'] ?? [];
|
||||
$maintenance = $site['maintenance_enabled'] ?? false;
|
||||
$version = (int) ($site['revisions']['general'] ?? 0);
|
||||
if ($request->method() === 'POST') {
|
||||
try {
|
||||
if ($request->post('settings_section') === 'mcp') {
|
||||
McpSettings::save($request->post('mcp_enabled') === '1',
|
||||
$request->post('rotate_mcp_token') === '1', $this->version($request));
|
||||
|
||||
return $this->success($request, '/admin/settings?saved=1#mcp-settings');
|
||||
}
|
||||
$name = $this->input($request, 'name');
|
||||
$cdn = Assets::cdn($this->input($request, 'cdn'));
|
||||
if ($name === '' || mb_strlen($name) > 100) {
|
||||
@@ -98,7 +126,22 @@ 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('请选择有效的站点图标操作。'),
|
||||
};
|
||||
$palette = $request->post('palette_id');
|
||||
if (is_string($palette)) {
|
||||
$paletteId = $palette;
|
||||
}
|
||||
SiteDesign::saveGeneral($name, $cdn, $this->version($request), $systemPages, $maintenance,
|
||||
$submittedHead === null ? null : PageHead::normalize($submittedHead), $icon, $palette);
|
||||
|
||||
return $this->success($request, '/admin/settings?saved=1');
|
||||
} catch (RuntimeException $ex) {
|
||||
@@ -116,6 +159,9 @@ 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,
|
||||
'palettes' => DesignLibrary::palettes(), 'paletteId' => $paletteId,
|
||||
'head' => $head, 'siteIcon' => $site['favicon'] ?? '', 'headEditor' => true, 'faviconEditor' => true,
|
||||
'mcp' => McpSettings::read(),
|
||||
'saved' => $request->get('saved') === '1',
|
||||
], $error === '' ? 200 : 422);
|
||||
}
|
||||
@@ -135,13 +181,13 @@ final class DesignController extends AdminController
|
||||
return $this->error($request, '找不到这项设计配置。', 404);
|
||||
}
|
||||
$repo = new Repository();
|
||||
$page = $repo->page('home');
|
||||
$page = $repo->homepage() ?? PageTemplates::find('cobalt-product');
|
||||
$page[$kind . '_id'] = (int) $id;
|
||||
|
||||
return (new SiteController())->renderPage($repo, $page, true);
|
||||
}
|
||||
|
||||
public function palette(Request $request, string $id): Response
|
||||
public function palette(Request $request, string $id, string $scope = ''): Response
|
||||
{
|
||||
$palette = DesignLibrary::find((int) $id, 'palette');
|
||||
if (!$palette) {
|
||||
@@ -151,11 +197,19 @@ final class DesignController extends AdminController
|
||||
foreach ($palette['payload'] as $variable => $value) {
|
||||
$declarations[] = '--' . $variable . ':' . $value;
|
||||
}
|
||||
$variables = implode(';', $declarations);
|
||||
if ($scope === 'region') {
|
||||
$region = ':is([data-site-area], main)[data-region-palette="' . $palette['id'] . '"]';
|
||||
$css = "$region {" . $variables . ';color:var(--ink);background-color:var(--white)}'
|
||||
. " $region .site-footer {--paper:inherit;--ink:inherit;--muted:inherit;background-color:var(--paper)}";
|
||||
} else {
|
||||
$css = ':root, #main-content.page-theme {' . $variables . '}'
|
||||
. ' #main-content:not(.page-theme) .hero-home {background:var(--paper)}';
|
||||
}
|
||||
|
||||
return new Response(200, [
|
||||
'Content-Type' => 'text/css; charset=utf-8', 'Cache-Control' => 'public, max-age=60',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
], ':root, #main-content.page-theme {' . implode(';', $declarations) . '} .page-theme .hero, .hero-home {background:var(--paper)}'
|
||||
. ' #main-content .button-primary, #main-content .button-primary:hover {color:var(--button-ink);background:var(--accent)}');
|
||||
], $css);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Config\Environment;
|
||||
use App\Content\BlockTemplates;
|
||||
use App\Content\Database;
|
||||
use App\Content\DesignLibrary;
|
||||
use App\Content\McpSettings;
|
||||
use App\Content\PageDraft;
|
||||
use App\Content\PageTemplates;
|
||||
use App\Content\Repository;
|
||||
@@ -28,7 +28,15 @@ final class McpController
|
||||
if ($origin !== null && $origin !== '' && $origin !== rtrim(View::absolute(''), '/')) {
|
||||
return new Response(403, ['Cache-Control' => 'no-store']);
|
||||
}
|
||||
$secret = Environment::get('LAYSITE_MCP_TOKEN');
|
||||
try {
|
||||
$settings = McpSettings::read();
|
||||
} catch (RuntimeException) {
|
||||
return new Response(503, ['Cache-Control' => 'no-store']);
|
||||
}
|
||||
$secret = $settings['token'];
|
||||
if ($settings['migrationRequired'] || ($settings['enabled'] && $secret === null)) {
|
||||
return new Response(503, ['Cache-Control' => 'no-store']);
|
||||
}
|
||||
$header = $request->header('authorization', '');
|
||||
if (!is_string($secret) || strlen($secret) < 32 || !is_string($header) || !hash_equals('Bearer ' . $secret, $header)) {
|
||||
return new Response(401, ['WWW-Authenticate' => 'Bearer realm="LaySite MCP"', 'Cache-Control' => 'no-store']);
|
||||
|
||||
@@ -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;
|
||||
@@ -19,7 +19,11 @@ final class SiteController
|
||||
{
|
||||
public function home(Request $request): Response
|
||||
{
|
||||
return $this->page($request, 'home');
|
||||
$repo = new Repository();
|
||||
$page = $repo->homepage();
|
||||
$response = $page ? $this->renderPage($repo, $page, root: true) : $this->missing($request);
|
||||
|
||||
return $response->withHeader('Cache-Control', 'no-store');
|
||||
}
|
||||
|
||||
public function license(Request $request): Response
|
||||
@@ -35,8 +39,8 @@ final class SiteController
|
||||
{
|
||||
$repo = new Repository();
|
||||
$page = $repo->page($slug);
|
||||
if (!$page || $page['layout_kind'] !== null || $page['page_type'] !== 'page'
|
||||
|| $page['status'] !== 'published' || $slug === 'home' && $request->path() !== '/') {
|
||||
if (!$page || $page['layout_kind'] !== null || !in_array($page['page_type'], ['page', 'home'], true)
|
||||
|| $page['status'] !== 'published') {
|
||||
return $this->missing($request);
|
||||
}
|
||||
if ($slug === 'articles' && $request->get('page') !== null) {
|
||||
@@ -52,17 +56,17 @@ final class SiteController
|
||||
return $this->renderPage($repo, $page);
|
||||
}
|
||||
|
||||
public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false, int $status = 200): Response
|
||||
public function renderPage(Repository $repo, array $page, bool $preview = false, bool $canvas = false, int $status = 200, bool $root = false): Response
|
||||
{
|
||||
$slug = $page['slug'];
|
||||
$isHome = $slug === 'home';
|
||||
$showHero = $page['visibility']['hero'] ?? true;
|
||||
$page = $repo->withSectionFeeds($page);
|
||||
|
||||
return View::render('page', [
|
||||
'site' => $repo->site(),
|
||||
'page' => $page,
|
||||
'path' => ($page['page_type'] ?? 'page') !== 'page' ? null : ($isHome ? '/' : '/' . $slug),
|
||||
'path' => empty($page['layout_kind']) && in_array($page['page_type'] ?? 'page', ['page', 'home'], true)
|
||||
? ($root ? '/' : '/' . $slug) : null,
|
||||
'title' => $page['title'],
|
||||
'description' => $page['description'],
|
||||
'socialImage' => $showHero ? $page['image'] : '',
|
||||
@@ -116,7 +120,7 @@ final class SiteController
|
||||
}
|
||||
|
||||
$allPath = '/articles/feed/' . $slug . '/' . $key;
|
||||
$returnPath = ($slug === 'home' ? '/' : '/' . $slug) . '#' . $key;
|
||||
$returnPath = '/' . $slug . '#' . $key;
|
||||
$categoryLinks = [];
|
||||
foreach (array_keys($categories) as $value) {
|
||||
$categoryLinks[$value] = $allPath . '?category=' . rawurlencode((string) $value);
|
||||
@@ -190,8 +194,11 @@ final class SiteController
|
||||
$repo = new Repository();
|
||||
$xml = ['<?xml version="1.0" encoding="UTF-8"?>', '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'];
|
||||
$pages = $repo->pages(publishedOnly: true);
|
||||
if (in_array('home', array_column($pages, 'page_type'), true)) {
|
||||
$xml[] = $this->sitemapUrl('/', null);
|
||||
}
|
||||
foreach ($pages as $page) {
|
||||
$xml[] = $this->sitemapUrl($page['slug'] === 'home' ? '/' : '/' . $page['slug'], $page['updated_at']);
|
||||
$xml[] = $this->sitemapUrl('/' . $page['slug'], $page['updated_at']);
|
||||
}
|
||||
foreach ($repo->sitemapArticles() as $article) {
|
||||
$xml[] = $this->sitemapUrl('/articles/' . $article['slug'], $article['updated_at']);
|
||||
@@ -209,7 +216,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
|
||||
@@ -229,7 +238,8 @@ final class SiteController
|
||||
$entry = preg_match('~^/(?:admin|account)(?:/|$)~D', $normal)
|
||||
&& Route::dispatch($request->method(), $normal)[0] === Dispatcher::FOUND;
|
||||
$single = preg_match('~^/[a-z0-9]+(?:-[a-z0-9]+)*$~D', $normal);
|
||||
if ($entry || ($single && $normal !== '/home' && ((new Repository())->page(substr($normal, 1))['status'] ?? null) === 'published')
|
||||
$page = $single ? (new Repository())->page(substr($normal, 1)) : null;
|
||||
if ($entry || ($page && $page['layout_kind'] === null && in_array($page['page_type'], ['page', 'home'], true) && $page['status'] === 'published')
|
||||
|| preg_match('~^/articles(?:/[a-z0-9-]+){1,2}$~D', $normal)) {
|
||||
$query = $request->queryString();
|
||||
|
||||
@@ -239,7 +249,7 @@ final class SiteController
|
||||
|
||||
return View::render('missing', [
|
||||
'site' => (new Repository())->site(), 'title' => '页面未找到', 'description' => '',
|
||||
'path' => null, 'noindex' => true,
|
||||
'path' => null, 'noindex' => true, 'atHomepage' => $path === '/',
|
||||
], 404);
|
||||
}
|
||||
|
||||
|
||||
@@ -41,36 +41,28 @@ final class StudioController extends AdminController
|
||||
if (!isset($types[$data['type']]) || $data['name'] === '' || mb_strlen($data['name']) > 100) {
|
||||
throw new RuntimeException('请填写页面名称并选择页面类型。');
|
||||
}
|
||||
if (isset(SystemPage::TYPES[$data['type']])) {
|
||||
$type = $data['type'];
|
||||
$slug = 'system--' . $type . '-' . bin2hex(random_bytes(8));
|
||||
$maintenance = $type === 'maintenance';
|
||||
(new Repository())->createPage($slug, $data['name'], [
|
||||
'heading' => $maintenance ? '站点维护中' : '页面未找到',
|
||||
'lead' => $maintenance ? '我们正在维护站点,请稍后再访问。' : '你访问的页面可能已移动或删除。',
|
||||
'actions' => $maintenance ? [] : [['/', '返回首页', 'solid', false]],
|
||||
'visibility' => $maintenance ? ['header' => false, 'footer' => false] : [],
|
||||
], $type);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug);
|
||||
}
|
||||
if (in_array($data['type'], ['header', 'footer'], true)) {
|
||||
$slug = LayoutPage::create($data['type'], $data['name']);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $slug);
|
||||
}
|
||||
if (!$this->slug($data['slug']) || in_array($data['slug'], PageDraft::RESERVED, true)) {
|
||||
throw new RuntimeException('请填写页面名称及可用的网址标识。');
|
||||
}
|
||||
$slug = PageDraft::slug($data['slug']);
|
||||
$type = $data['type'];
|
||||
$key = $this->input($request, 'template');
|
||||
$template = $key === '' ? [] : PageTemplates::find($key);
|
||||
if ($template === null) {
|
||||
throw new RuntimeException('找不到所选整页模板。');
|
||||
}
|
||||
if ($key === '' && isset(SystemPage::TYPES[$type])) {
|
||||
$maintenance = $type === 'maintenance';
|
||||
$template = [
|
||||
'heading' => $maintenance ? '站点维护中' : '页面未找到',
|
||||
'lead' => $maintenance ? '我们正在维护站点,请稍后再访问。' : '你访问的页面可能已移动或删除。',
|
||||
'actions' => $maintenance ? [] : [['/', '返回首页', 'solid', false]],
|
||||
'visibility' => $maintenance ? ['header' => false, 'footer' => false] : [],
|
||||
];
|
||||
} elseif ($key === '' && in_array($type, ['header', 'footer'], true)) {
|
||||
$template['visibility'] = ['hero' => false];
|
||||
}
|
||||
$template['title'] = $data['name'];
|
||||
(new Repository())->createPage($data['slug'], $data['name'], $template);
|
||||
(new Repository())->createPage($slug, $data['name'], $template, $type);
|
||||
|
||||
return $this->success($request, '/admin/pages/' . $data['slug']);
|
||||
return $this->success($request, '/admin/pages/' . $slug);
|
||||
} catch (RuntimeException $error) {
|
||||
$data['error'] = $this->validationMessage($error);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user