64 lines
1.8 KiB
PHP
64 lines
1.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Model;
|
|
|
|
use Magento\Framework\App\Request\Http;
|
|
|
|
/**
|
|
* Hostname-only tunnel detection against persisted tunnel base URLs (nginx global map without MageSail headers).
|
|
*/
|
|
class TunnelHostnameMatcher
|
|
{
|
|
public function __construct(
|
|
private readonly TunnelStoreState $tunnelStoreState
|
|
) {
|
|
}
|
|
|
|
/**
|
|
* When HTTP_HOST matches unsecure or secure tunnel base URL host, return the persisted store code.
|
|
*/
|
|
public function getStoreCodeIfRequestHostMatchesState(Http $request): ?string
|
|
{
|
|
$state = $this->tunnelStoreState->read();
|
|
if ($state === null) {
|
|
return null;
|
|
}
|
|
$requestHost = self::hostFromHttpHost($request->getServer('HTTP_HOST'));
|
|
if ($requestHost === null) {
|
|
return null;
|
|
}
|
|
foreach ([$state['unsecure_base_url'], $state['secure_base_url']] as $baseUrl) {
|
|
if ($baseUrl === '') {
|
|
continue;
|
|
}
|
|
$configuredHost = self::hostFromBaseUrl($baseUrl);
|
|
if ($configuredHost !== null && strcasecmp($requestHost, $configuredHost) === 0) {
|
|
return $state['store_code'];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
public static function hostFromHttpHost(?string $httpHost): ?string
|
|
{
|
|
if ($httpHost === null || $httpHost === '') {
|
|
return null;
|
|
}
|
|
$parsed = parse_url('http://' . $httpHost);
|
|
if ($parsed === false || !isset($parsed['host'])) {
|
|
return null;
|
|
}
|
|
return $parsed['host'];
|
|
}
|
|
|
|
public static function hostFromBaseUrl(string $baseUrl): ?string
|
|
{
|
|
$parsed = parse_url($baseUrl);
|
|
if ($parsed === false || !isset($parsed['host'])) {
|
|
return null;
|
|
}
|
|
return $parsed['host'];
|
|
}
|
|
}
|