2023-03-17 14:33:48 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Middleware;
|
|
|
|
|
|
2025-10-15 13:28:21 +00:00
|
|
|
use App\Models\InstanceSettings;
|
2023-03-17 14:33:48 +00:00
|
|
|
use Illuminate\Http\Middleware\TrustHosts as Middleware;
|
2025-10-15 20:00:21 +00:00
|
|
|
use Illuminate\Support\Facades\Cache;
|
2025-10-15 13:28:21 +00:00
|
|
|
use Spatie\Url\Url;
|
2023-03-17 14:33:48 +00:00
|
|
|
|
|
|
|
|
class TrustHosts extends Middleware
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Get the host patterns that should be trusted.
|
|
|
|
|
*
|
|
|
|
|
* @return array<int, string|null>
|
|
|
|
|
*/
|
|
|
|
|
public function hosts(): array
|
|
|
|
|
{
|
2025-10-15 13:28:21 +00:00
|
|
|
$trustedHosts = [];
|
2025-10-15 20:00:21 +00:00
|
|
|
|
|
|
|
|
// Trust the configured FQDN from InstanceSettings (cached to avoid DB query on every request)
|
2025-10-15 20:15:55 +00:00
|
|
|
// Use empty string as sentinel value instead of null so negative results are cached
|
2025-10-15 20:00:21 +00:00
|
|
|
$fqdnHost = Cache::remember('instance_settings_fqdn_host', 300, function () {
|
|
|
|
|
try {
|
|
|
|
|
$settings = InstanceSettings::get();
|
|
|
|
|
if ($settings && $settings->fqdn) {
|
|
|
|
|
$url = Url::fromString($settings->fqdn);
|
|
|
|
|
$host = $url->getHost();
|
|
|
|
|
|
2025-10-15 20:15:55 +00:00
|
|
|
return $host ?: '';
|
2025-10-15 13:28:21 +00:00
|
|
|
}
|
2025-10-15 20:00:21 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
// If instance settings table doesn't exist yet (during installation),
|
2025-10-15 20:15:55 +00:00
|
|
|
// return empty string (sentinel) so this result is cached
|
2025-10-15 13:28:21 +00:00
|
|
|
}
|
2025-10-15 20:00:21 +00:00
|
|
|
|
2025-10-15 20:15:55 +00:00
|
|
|
return '';
|
2025-10-15 20:00:21 +00:00
|
|
|
});
|
|
|
|
|
|
2025-10-15 20:15:55 +00:00
|
|
|
// Convert sentinel value back to null for consumption
|
|
|
|
|
$fqdnHost = $fqdnHost !== '' ? $fqdnHost : null;
|
|
|
|
|
|
2025-10-15 20:00:21 +00:00
|
|
|
if ($fqdnHost) {
|
|
|
|
|
$trustedHosts[] = $fqdnHost;
|
2025-10-15 13:28:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Trust all subdomains of APP_URL as fallback
|
|
|
|
|
$trustedHosts[] = $this->allSubdomainsOfApplicationUrl();
|
|
|
|
|
|
|
|
|
|
return array_filter($trustedHosts);
|
2023-03-17 14:33:48 +00:00
|
|
|
}
|
|
|
|
|
}
|