43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Model;
|
|
|
|
use Magento\Framework\App\Request\Http;
|
|
|
|
/**
|
|
* Detects MageSail tunnel proxy (header / query / server) on the current request.
|
|
*/
|
|
class TunnelRequestDetector
|
|
{
|
|
private const HEADER_STORE_CODE = 'X-MageSail-Store-Code';
|
|
private const QUERY_PARAM_STORE = 'magesail_store';
|
|
private const SERVER_HEADER = 'HTTP_X_MAGESAIL_STORE_CODE';
|
|
|
|
public function getTunnelStoreCode(Http $request): ?string
|
|
{
|
|
$value = $request->getParam(self::QUERY_PARAM_STORE);
|
|
if ($value !== null && trim((string) $value) !== '') {
|
|
return trim((string) $value);
|
|
}
|
|
$value = $request->getHeader(self::HEADER_STORE_CODE);
|
|
if ($value !== false && $value !== null && trim((string) $value) !== '') {
|
|
return trim((string) $value);
|
|
}
|
|
$value = $request->getServer(self::SERVER_HEADER);
|
|
if ($value !== null && $value !== '' && trim((string) $value) !== '') {
|
|
return trim((string) $value);
|
|
}
|
|
$uri = (string) $request->getRequestUri();
|
|
if ($uri !== '' && str_contains($uri, self::QUERY_PARAM_STORE . '=')) {
|
|
$query = str_contains($uri, '?') ? substr($uri, strpos($uri, '?') + 1) : '';
|
|
parse_str($query, $params);
|
|
$v = $params[self::QUERY_PARAM_STORE] ?? '';
|
|
if ($v !== '' && trim((string) $v) !== '') {
|
|
return trim((string) $v);
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
}
|