- Weitere Adressen einer Website (z. B. www.stommer.info) leiten per 301 auf die Hauptadresse um; vorher blockierte der Browser Skripte, Schriften und Videos, die von der Hauptadresse kamen (kein Titelvideo, Ersatzschrift) - Site::origin() nimmt verbindlich das Feld domain - Videos: Abspielgeschwindigkeit 0,5x bis 1x, gilt im Titelbereich und Rundgang Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
117 lines
3.8 KiB
PHP
117 lines
3.8 KiB
PHP
<?php
|
||
|
||
namespace App\Http\Middleware;
|
||
|
||
use App\Models\Site;
|
||
use App\Models\Tenant;
|
||
use App\Models\User;
|
||
use App\Tenancy\TenantContext;
|
||
use App\Themes\Theme;
|
||
use App\Themes\ThemeFactory;
|
||
use Closure;
|
||
use Illuminate\Http\RedirectResponse;
|
||
use Illuminate\Http\Request;
|
||
use Illuminate\Support\Facades\URL;
|
||
use Symfony\Component\HttpFoundation\Response;
|
||
|
||
/**
|
||
* Determines site, theme and appearance for the website and activates the "theme::" views.
|
||
*/
|
||
class ResolveTheme
|
||
{
|
||
public function __construct(private ThemeFactory $factory) {}
|
||
|
||
public function handle(Request $request, Closure $next): Response
|
||
{
|
||
$context = app(TenantContext::class);
|
||
$context->forget();
|
||
|
||
$theme = $this->factory->forRequest($request)->activate();
|
||
app()->instance(Theme::class, $theme);
|
||
|
||
$tenant = null;
|
||
|
||
if ($theme->site->exists && $theme->site->tenant_id !== null) {
|
||
$tenant = $theme->site->tenant;
|
||
} elseif (Tenant::query()->count() === 1) {
|
||
$tenant = Tenant::query()->first();
|
||
}
|
||
|
||
if ($tenant !== null) {
|
||
if (! $tenant->is_active && ! $this->operatorMayPass($request)) {
|
||
return $this->suspended($tenant);
|
||
}
|
||
|
||
$context->set($tenant);
|
||
}
|
||
|
||
if ($redirect = $this->toMainAddress($request, $theme->site)) {
|
||
return $redirect;
|
||
}
|
||
|
||
$this->useSiteOrigin($theme->site);
|
||
|
||
return $next($request);
|
||
}
|
||
|
||
/**
|
||
* A website answers on one main address. Visitors coming in through a further address (e.g. www.…) are
|
||
* sent there permanently – otherwise the page would load its scripts, fonts and videos from the main
|
||
* address, which the browser treats as another origin and blocks (no JavaScript, fallback fonts).
|
||
* Only GET/HEAD: a form sent to the other address must not lose its input.
|
||
*/
|
||
private function toMainAddress(Request $request, Site $site): ?RedirectResponse
|
||
{
|
||
$origin = $site->exists ? $site->origin() : null;
|
||
$mainHost = $origin === null ? null : parse_url($origin, PHP_URL_HOST);
|
||
|
||
if (! is_string($mainHost) || strcasecmp($mainHost, $request->getHost()) === 0 || ! $request->isMethodSafe()) {
|
||
return null;
|
||
}
|
||
|
||
return new RedirectResponse($origin.$request->getRequestUri(), 301);
|
||
}
|
||
|
||
/**
|
||
* Links must carry the address the visitor came in on. AppServiceProvider forces APP_URL for every
|
||
* generated URL, because on Strato the document root is a release folder; with one landlord that was
|
||
* harmless, but a second website on its own domain would otherwise send every visitor to the first
|
||
* landlord's host. The forced root stays – only its value becomes the website that was resolved.
|
||
*/
|
||
private function useSiteOrigin(Site $site): void
|
||
{
|
||
$origin = $site->exists ? $site->origin() : null;
|
||
|
||
if ($origin === null) {
|
||
return;
|
||
}
|
||
|
||
URL::forceRootUrl($origin);
|
||
URL::forceScheme(parse_url($origin, PHP_URL_SCHEME) ?: 'https');
|
||
}
|
||
|
||
/**
|
||
* What a visitor of a suspended landlord sees: a plain notice instead of a dead link.
|
||
*/
|
||
private function suspended(Tenant $tenant): Response
|
||
{
|
||
return response()->view('suspended', [
|
||
'message' => $tenant->suspended_message ?: 'Diese Webseite ist zurzeit nicht erreichbar. Bitte versuchen Sie es später noch einmal.',
|
||
], 503);
|
||
}
|
||
|
||
/**
|
||
* A suspended landlord stays reachable for the operator, and the operator panel itself stays open.
|
||
*/
|
||
private function operatorMayPass(Request $request): bool
|
||
{
|
||
if ($request->is('betreiber', 'betreiber/*')) {
|
||
return true;
|
||
}
|
||
|
||
$user = $request->user();
|
||
|
||
return $user instanceof User && $user->is_operator;
|
||
}
|
||
}
|