Launch LaySite visual editor and MCP workspace
This commit is contained in:
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use App\Http\VisitorSession;
|
||||
use support\Response;
|
||||
|
||||
final class View
|
||||
{
|
||||
public static function render(string $template, array $data, int $status = 200, bool $admin = false): Response
|
||||
{
|
||||
$cdn = $admin || !empty($data['previewMode']) ? '' : ($data['site']['cdn']['origin'] ?? '');
|
||||
$data['cdn'] = $cdn;
|
||||
$personalized = false;
|
||||
if (!$admin) {
|
||||
$page = $data['page'] ?? [];
|
||||
$data['header'] = DesignLibrary::layout('header', $data['site'], $page);
|
||||
$data['footer'] = DesignLibrary::layout('footer', $data['site'], $page);
|
||||
$data['visitor'] = null;
|
||||
$provider = $data['header']['payload']['provider'];
|
||||
$personalized = $provider !== [] && in_array('account', $data['header']['payload']['components'], true)
|
||||
&& ($page['visibility']['header'] ?? true) && empty($data['previewMode']);
|
||||
if ($personalized) {
|
||||
$profile = VisitorSession::profile(request());
|
||||
if (($profile['layout'] ?? null) === $data['header']['id']
|
||||
&& ($profile['config'] ?? null) === hash('sha256', json_encode($provider, JSON_THROW_ON_ERROR))) {
|
||||
$data['visitor'] = $profile;
|
||||
}
|
||||
}
|
||||
}
|
||||
$html = self::fragment($template, $data);
|
||||
$page = Assets::rewrite(self::fragment($admin ? 'admin-layout' : 'layout', $data + ['content' => $html]), $cdn);
|
||||
$assetSources = $cdn === '' ? "'self'" : "'self' " . $cdn;
|
||||
$articleEditor = $admin && !empty($data['articleEditor']);
|
||||
$editorImages = $articleEditor ? ' data:' : '';
|
||||
$editorStyles = $articleEditor ? "style-src-attr 'unsafe-inline'; " : '';
|
||||
$headers = [
|
||||
'Content-Type' => 'text/html; charset=utf-8',
|
||||
'X-Content-Type-Options' => 'nosniff',
|
||||
'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 "
|
||||
. (!empty($data['previewMode']) ? "'self'" : "'none'") . "; form-action 'self'; base-uri 'none'; object-src 'none'",
|
||||
];
|
||||
if ($admin || !empty($data['previewMode'])) {
|
||||
$headers['Cache-Control'] = 'no-store';
|
||||
$headers['X-Robots-Tag'] = 'noindex, nofollow';
|
||||
} elseif (($data['noindex'] ?? false) || $status >= 400) {
|
||||
$headers['X-Robots-Tag'] = 'noindex';
|
||||
}
|
||||
|
||||
if ($personalized) {
|
||||
$headers['Cache-Control'] = 'private, no-store';
|
||||
$headers['Vary'] = 'Cookie';
|
||||
}
|
||||
return new Response($status, $headers, $page);
|
||||
}
|
||||
|
||||
public static function fragment(string $template, array $data = []): string
|
||||
{
|
||||
extract($data, EXTR_SKIP);
|
||||
ob_start();
|
||||
try {
|
||||
require dirname(__DIR__) . '/view/' . $template . '.php';
|
||||
|
||||
return (string) ob_get_contents();
|
||||
} finally {
|
||||
ob_end_clean();
|
||||
}
|
||||
}
|
||||
|
||||
public static function escape(string|int|null $value): string
|
||||
{
|
||||
return htmlspecialchars((string) $value, ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8');
|
||||
}
|
||||
|
||||
public static function absolute(string $path): string
|
||||
{
|
||||
return rtrim(getenv('LAYSITE_SITE_URL') ?: 'http://127.0.0.1:8787', '/') . $path;
|
||||
}
|
||||
|
||||
public static function date(?string $value, string $format = 'Y年n月j日'): string
|
||||
{
|
||||
return $value ? date($format, strtotime($value)) : '';
|
||||
}
|
||||
|
||||
public static function alignmentClass(?string $alignment): string
|
||||
{
|
||||
return in_array($alignment, Html::ALIGNMENTS, true) ? ' studio-align-' . $alignment : '';
|
||||
}
|
||||
|
||||
public static function archivePage(string $path, int $page): string
|
||||
{
|
||||
if ($page === 1) {
|
||||
return $path;
|
||||
}
|
||||
|
||||
return $path . (str_contains($path, '?') ? '&' : '?') . 'page=' . $page;
|
||||
}
|
||||
|
||||
public static function redirect(string $path, int $status = 303): Response
|
||||
{
|
||||
return new Response($status, ['Location' => $path, 'Cache-Control' => $status === 301 ? 'public, max-age=300' : 'no-store'], '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user