Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GeoIP fix for case when request comes for store without a trailing slash #569

Merged
merged 5 commits into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 31 additions & 27 deletions Controller/GeoIP/GetAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,28 @@
* @copyright Copyright (c) 2016 Fastly, Inc. (http://www.fastly.com)
* @license BSD, see LICENSE_FASTLY_CDN.txt
*/

namespace Fastly\Cdn\Controller\GeoIP;

use Fastly\Cdn\Helper\StoreMessage;
use Fastly\Cdn\Model\Config;
use Fastly\Cdn\Model\Resolver\GeoIP\CountryCodeProviderInterface;
use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\ResponseInterface;
use Magento\Framework\Controller\ResultInterface;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\Url\DecoderInterface;
use Magento\Framework\Url\EncoderInterface;
use Magento\Framework\UrlInterface;
use Magento\Framework\View\Result\Layout;
use Magento\Framework\View\Result\LayoutFactory;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Model\StoreManagerInterface;
use Psr\Log\LoggerInterface;
use Magento\Framework\App\Action\Action;
use Fastly\Cdn\Helper\StoreMessage;
use Fastly\Cdn\Model\Resolver\GeoIP\CountryCodeProviderInterface;

/**
* Class GetAction
* Class GetAction for esi hole
*
* @package Fastly\Cdn\Controller\GeoIP
*/
class GetAction extends Action
{
Expand Down Expand Up @@ -111,16 +111,16 @@ public function __construct(
CountryCodeProviderInterface $countryCodeProvider
) {
parent::__construct($context);
$this->config = $config;
$this->storeRepository = $storeRepository;
$this->storeManager = $storeManager;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->logger = $logger;
$this->storeMessage = $storeMessage;
$this->urlEncoder = $urlEncoder;
$this->urlDecoder = $urlDecoder;
$this->config = $config;
$this->storeRepository = $storeRepository;
$this->storeManager = $storeManager;
$this->resultLayoutFactory = $resultLayoutFactory;
$this->logger = $logger;
$this->storeMessage = $storeMessage;
$this->urlEncoder = $urlEncoder;
$this->urlDecoder = $urlDecoder;

$this->url = $context->getUrl();
$this->url = $context->getUrl();

$this->countryCodeProvider = $countryCodeProvider;
}
Expand Down Expand Up @@ -155,7 +155,7 @@ public function execute()
$currentStoreCode = $currentStore->getCode();

$queryParams = [
'___store' => $targetStoreCode,
'___store' => $targetStoreCode,
'___from_store' => $currentStoreCode
];
if ($targetUrl) {
Expand Down Expand Up @@ -192,14 +192,17 @@ public function execute()
}

/**
* @param $targetUrl
* @param $targetStoreCode
* @param $currentStoreCode
* GetTargetUrl uenc params modificiations
*
* @param string $targetUrl
* @param string $targetStoreCode
* @param string $currentStoreCode
* @return string
*/
private function getTargetUrl($targetUrl, $targetStoreCode, $currentStoreCode): string
{
$decodedTargetUrl = $this->urlDecoder->decode($targetUrl);
$path = parse_url($decodedTargetUrl, PHP_URL_PATH);

/* Fix geoip redirection issue to the same page */
$currentStore = $this->storeManager->getStore();
Expand All @@ -208,15 +211,16 @@ private function getTargetUrl($targetUrl, $targetStoreCode, $currentStoreCode):
$targetBaseUrl = $targetStore->getBaseUrl();
$decodedTargetUrl = \str_ireplace($currentBaseUrl, $targetBaseUrl, $decodedTargetUrl);

$search = '/' . $currentStoreCode . '/';
$replace = '/' . $targetStoreCode . '/';
if (\preg_match("#^/$currentStoreCode(?:/|\?|$)#", $path)) {

if (strpos($decodedTargetUrl, $search) !== false) {
$searchPattern = '/\/' . $currentStoreCode . '\//';
$targetUrl = $this->urlEncoder->encode(preg_replace($searchPattern, $replace, $decodedTargetUrl, 1));
return explode('%', $targetUrl)[0];
$decodedTargetUrl = \preg_replace(
"#/$currentStoreCode#",
"/$targetStoreCode",
$decodedTargetUrl,
1
);
}
$targetUrl = $this->urlEncoder->encode($decodedTargetUrl);
return $targetUrl;
$encodedUrl = $this->urlEncoder->encode($decodedTargetUrl);
return \explode('%', $encodedUrl)[0];
}
}