72 lines
2.3 KiB
PHP
72 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace MageSail\Magesail\Plugin\FrontController;
|
|
|
|
use Magento\Framework\App\FrontController;
|
|
use Magento\Framework\App\RequestInterface;
|
|
|
|
/**
|
|
* Run before Store RequestPreprocessor (sortOrder 10 < 50).
|
|
* Ensure magesail_store is on the request from superglobals so base-URL redirect skip works
|
|
* even if the Request object was built before query params were fully available.
|
|
*/
|
|
class EnsureTunnelParamPlugin
|
|
{
|
|
private const QUERY_PARAM_STORE = 'magesail_store';
|
|
private const SERVER_HEADER = 'HTTP_X_MAGESAIL_STORE_CODE';
|
|
|
|
public function aroundDispatch(
|
|
FrontController $subject,
|
|
\Closure $proceed,
|
|
RequestInterface $request
|
|
) {
|
|
$this->ensureTunnelParamOnRequest($request);
|
|
return $proceed($request);
|
|
}
|
|
|
|
/**
|
|
* If tunnel indicator is in $_GET or REQUEST_URI or header, set it on the request
|
|
* so later plugins (BaseUrlChecker, RequestPreprocessor skip) see it.
|
|
*/
|
|
private function ensureTunnelParamOnRequest(RequestInterface $request): void
|
|
{
|
|
$value = $this->getTunnelStoreFromSuperglobals();
|
|
if ($value === null) {
|
|
return;
|
|
}
|
|
if (!$request instanceof \Magento\Framework\App\Request\Http) {
|
|
return;
|
|
}
|
|
if ($request->getParam(self::QUERY_PARAM_STORE) !== null) {
|
|
return;
|
|
}
|
|
$request->setParam(self::QUERY_PARAM_STORE, $value);
|
|
}
|
|
|
|
private function getTunnelStoreFromSuperglobals(): ?string
|
|
{
|
|
if (!empty($_GET[self::QUERY_PARAM_STORE])) {
|
|
$v = trim((string) $_GET[self::QUERY_PARAM_STORE]);
|
|
if ($v !== '') {
|
|
return $v;
|
|
}
|
|
}
|
|
$uri = $_SERVER['REQUEST_URI'] ?? '';
|
|
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);
|
|
}
|
|
}
|
|
$h = $_SERVER[self::SERVER_HEADER] ?? '';
|
|
if ($h !== '' && trim((string) $h) !== '') {
|
|
return trim((string) $h);
|
|
}
|
|
return null;
|
|
}
|
|
}
|