forked from laysense/laysense.cn
87 lines
3.2 KiB
PHP
87 lines
3.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Content;
|
|
|
|
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;
|
|
$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';
|
|
}
|
|
|
|
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('LAYSENSE_SITE_URL') ?: 'https://laysense.cn', '/') . $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'], '');
|
|
}
|
|
}
|