forked from laysense/laysite
48 lines
1.7 KiB
PHP
48 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Middleware;
|
|
|
|
use App\Content\Repository;
|
|
use App\Content\SystemPage;
|
|
use App\Controller\SiteController;
|
|
use Webman\Http\Request;
|
|
use Webman\Http\Response;
|
|
use Webman\MiddlewareInterface;
|
|
|
|
final class SiteStatus implements MiddlewareInterface
|
|
{
|
|
public function process(Request $request, callable $handler): Response
|
|
{
|
|
$path = $request->path();
|
|
if ($path === '/admin' || str_starts_with($path, '/admin/')) {
|
|
return $handler($request);
|
|
}
|
|
|
|
$repo = new Repository();
|
|
$site = $repo->site();
|
|
$controller = new SiteController();
|
|
$pages = $site['system_pages'] ?? [];
|
|
if (!empty($site['maintenance_enabled']) && ($page = SystemPage::published($repo, 'maintenance', $pages['maintenance'] ?? null))) {
|
|
// Media and styles must remain available for the maintenance page and the admin canvas.
|
|
if (in_array($request->method(), ['GET', 'HEAD'], true)
|
|
&& preg_match('~^/(?:assets/uploads/[a-f0-9]{32}\.[a-z0-9]+|palette/[0-9]+\.css|canvas-style\.css|favicon\.ico)$~D', $path)) {
|
|
$asset = $handler($request);
|
|
if ($asset->getStatusCode() !== 404) {
|
|
return $asset;
|
|
}
|
|
}
|
|
|
|
return $controller->renderPage($repo, $page, status: 503)->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
|
|
$response = $handler($request);
|
|
if ($response->getStatusCode() === 404 && ($page = SystemPage::published($repo, 'not-found', $pages['not-found'] ?? null))) {
|
|
return $controller->renderPage($repo, $page, status: 404)->withHeader('Cache-Control', 'no-store');
|
|
}
|
|
|
|
return $response;
|
|
}
|
|
}
|