127 lines
5.2 KiB
PHP
127 lines
5.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
use App\Config\Environment;
|
|
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 && empty($data['page']['layout_kind'])) {
|
|
$page = $data['page'] ?? [];
|
|
$repo = new Repository();
|
|
$data['header'] = DesignLibrary::layout('header', $data['site'], $page);
|
|
$data['footer'] = DesignLibrary::layout('footer', $data['site'], $page);
|
|
$data['visitor'] = null;
|
|
$identity = DesignLibrary::identity($data['header']);
|
|
$data['member'] = $identity !== null;
|
|
$personalized = $data['member']
|
|
&& ($page['visibility']['header'] ?? true) && empty($data['previewMode']);
|
|
if ($personalized) {
|
|
$profile = VisitorSession::profile(request());
|
|
if (($profile['layout'] ?? null) === $data['header']['id']
|
|
&& ($profile['config'] ?? null) === $identity) {
|
|
$data['visitor'] = $profile;
|
|
}
|
|
}
|
|
foreach (['header', 'footer'] as $kind) {
|
|
$id = $data[$kind]['id'];
|
|
if ($id === 0 || (!($page['visibility'][$kind] ?? true) && empty($data['canvas']))) {
|
|
continue;
|
|
}
|
|
$region = $repo->layoutPage($id);
|
|
if ($region) {
|
|
$account = $kind === 'header' && $data['member']
|
|
? self::fragment('member-account', ['layoutId' => $id, 'visitor' => $data['visitor']]) : '';
|
|
$markup = self::fragment('page', [
|
|
'page' => $repo->withSectionFeeds($region), 'site' => $data['site'], 'canvas' => false,
|
|
]);
|
|
$data[$kind . 'Markup'] = Html::sharedFragment($markup, $region['slug'], $account);
|
|
}
|
|
}
|
|
}
|
|
$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(Environment::get('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'], '');
|
|
}
|
|
}
|