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; } }