![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Cnc/GeoIp/Model/ |
<?php /** * Copyright (c) 2021 Kaliop Digital Commerce (https://digitalcommerce.kaliop.com) All Rights Reserved. * https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) * Krzysztof Majkowski <[email protected]> */ namespace Cnc\GeoIp\Model; use Cnc\GeoIp\Api\CheckRedirectInterface; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\Exception\LocalizedException; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Framework\UrlInterface; use Magento\Store\Model\StoreManagerInterface; class CheckRedirect implements CheckRedirectInterface { const GEOIP_HEADER = 'HTTP_CF_IPCOUNTRY'; const XML_PATH_IS_ENABLED = 'general/geoip/enabled'; const XML_PATH_REDIRECT_CONFIG = [ 'cnc_fr_eu' => 'general/geoip/cnc_fr_eu', 'en' => 'general/geoip/en', 'cnc_en_gb' => 'general/geoip/cnc_en_gb', 'cnc_en_us' => 'general/geoip/cnc_en_us', ]; const STORE_WEBSITE_MAP = [ 'cnc_fr_eu' => 1, 'en' => 1, 'cnc_en_gb' => 2, 'cnc_en_us' => 3, ]; /** * @var UrlInterface */ private $urlBuilder; /** * @var StoreManagerInterface */ private $storeManager; /** * @var ScopeConfigInterface */ private $scopeConfig; /** * CheckRedirect constructor. * @param UrlInterface $urlBuilder * @param StoreManagerInterface $storeManager * @param ScopeConfigInterface $scopeConfig */ public function __construct( UrlInterface $urlBuilder, StoreManagerInterface $storeManager, ScopeConfigInterface $scopeConfig ) { $this->urlBuilder = $urlBuilder; $this->storeManager = $storeManager; $this->scopeConfig = $scopeConfig; } /** * @return false[] * @throws LocalizedException * @throws NoSuchEntityException */ public function redirect(): array { $isEnabled = $this->scopeConfig->getValue(self::XML_PATH_IS_ENABLED); $data = [ 'redirect' => false ]; if ($isEnabled && isset($_SERVER[self::GEOIP_HEADER])) { $currentStoreCode = $this->storeManager->getStore()->getCode(); $currentCountry = $_SERVER[self::GEOIP_HEADER]; foreach (self::XML_PATH_REDIRECT_CONFIG as $storeCode => $xmlPath) { if ($storeCode == $currentStoreCode) { continue; } $countries = $this->scopeConfig->getValue($xmlPath); $countries = ($countries ? explode(',', $countries) : []); if (in_array($currentCountry, $countries)) { $data['redirect'] = true; $data['customer_country'] = $currentCountry; if ($this->storeManager->getWebsite()->getId() != self::STORE_WEBSITE_MAP[$storeCode]) { $store = $this->storeManager->getStore($storeCode); $url = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB); $data['url'] = $url; } else { $url = $this->urlBuilder->getUrl( 'stores/store/redirect', [ '___store' => $storeCode, '___from_store' => $currentStoreCode, ] ); $data['url'] = $url; } break; } } } return $data; } }