Deploy FeWo mit PipelineManager / deploy (push) Successful in 3m39s
- ForceHttps-Middleware (abschaltbar per FEWO_FORCE_HTTPS) - Datenschutzerklaerung im Buchungsformular verlinkt - ResolveTheme: angebot/bewertung/angebote/kalender/automation/health bleiben auf der aufgerufenen Adresse - Berichte MVP-TECHNICAL-GATE / MVP-COMPLIANCE-ACCEPTANCE, Regeln fuer parallele Agenten (ZUSAMMENARBEIT 7) Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
135 lines
4.6 KiB
PHP
135 lines
4.6 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 ($this->staysOnRequestedAddress($request)) {
|
||
URL::forceRootUrl($request->getSchemeAndHttpHost());
|
||
URL::forceScheme($request->getScheme());
|
||
|
||
return $next($request);
|
||
}
|
||
|
||
if ($redirect = $this->toMainAddress($request, $theme->site)) {
|
||
return $redirect;
|
||
}
|
||
|
||
$this->useSiteOrigin($theme->site);
|
||
|
||
return $next($request);
|
||
}
|
||
|
||
/**
|
||
* Signed links (offers, reviews, newsletter) are bound to the host they were issued for, and portals and
|
||
* automations call feeds and endpoints under the address they were given. These paths are answered on the
|
||
* address they came in on – a redirect to the main address would invalidate old signatures and feed URLs
|
||
* after a change of the main address. Links on such a page keep the requested host (session, CSRF).
|
||
*/
|
||
private function staysOnRequestedAddress(Request $request): bool
|
||
{
|
||
return $request->is('angebot/*', 'bewertung/*', 'angebote/*', 'kalender/*', 'automation/*', 'health/*');
|
||
}
|
||
|
||
/**
|
||
* 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;
|
||
}
|
||
}
|