<?php declare(strict_types=1);namespace EtrpCurrencyGeoIp\Subscriber;use EtrpCurrencyGeoIp\Service\GeoipServices;use Shopware\Core\PlatformRequest;use Shopware\Core\System\SalesChannel\SalesChannelContext;use Shopware\Storefront\Event\StorefrontRenderEvent;use Shopware\Storefront\Framework\Routing\RequestTransformer;use Shopware\Storefront\Framework\Routing\Router;use Shopware\Storefront\Page\GenericPageLoadedEvent;use Symfony\Component\EventDispatcher\EventSubscriberInterface;use Symfony\Component\HttpFoundation\Request;use Symfony\Component\HttpFoundation\Response;use Symfony\Component\HttpFoundation\Cookie;use Symfony\Component\HttpFoundation\RedirectResponse;use Symfony\Component\HttpKernel\Event\KernelEvent;use Symfony\Component\HttpKernel\Event\RequestEvent;use Symfony\Component\HttpKernel\Event\ResponseEvent;use Symfony\Component\HttpKernel\KernelEvents;class DetectCountryUser implements EventSubscriberInterface{ /** * @var GeoipServices */ private $GeoipServices; /** * @var Router */ private $router; /** * DetectCountryUser constructor. * @param GeoipServices $GeoipServices * @param Router $router */ public function __construct( GeoipServices $GeoipServices, Router $router ) { $this->GeoipServices = $GeoipServices; $this->router = $router; } /** * @return \array[] */ public static function getSubscribedEvents(): array { return [ KernelEvents::RESPONSE => ['isoCurrentUser', 9999], ['b2bCustomer', 9999] ]; } /** * @param ResponseEvent $event */ public function isoCurrentUser(ResponseEvent $event): void { if (!$event->getRequest()->attributes->get('sw-sales-channel-context')){ return; } //id canale shopware $salesChannel = $event->getRequest()->attributes->get('sw-sales-channel-context')->getSalesChannel(); $salesChannelId = $event->getRequest()->attributes->get('sw-sales-channel-id'); //var_dump($salesChannelId); $salesChannelName = $salesChannel->getTranslated()['name']; if (!isset($salesChannel)) { return; } $context = $event->getRequest()->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT); //se admin non continuo if (!$context instanceof SalesChannelContext) { return; } $ip = $event->getRequest()->getClientIp(); /** * PER TEST DA LOCALHOST * //USA - CANADA 184.151.230.135 * //IT 94.89.28.10 * //UK 195.195.237.39 */ if ('127.0.0.1' === $ip) { $ip = '94.89.28.10'; } $cookieset = $event->getRequest()->cookies->get('domainrefer'); if ($cookieset == null) { $countryIso = $this->GeoipServices->ip2country($ip); $domainForCountry = $this->domainForCountry($countryIso); } else { $domainForCountry = $cookieset; } $currentDomain = $event->getRequest()->attributes->get(RequestTransformer::SALES_CHANNEL_BASE_URL); $currentDomain = $currentDomain === "" ? "/it" : $currentDomain; $currentUri = $event->getRequest()->getRequestUri(); $currentRouteName = $event->getRequest()->attributes->get('_route'); $currentRouteLink = $event->getRequest()->attributes->get('sw-original-request-uri'); $maindomain = $event->getRequest()->attributes->get(RequestTransformer::SALES_CHANNEL_ABSOLUTE_BASE_URL); $domainPath = $domainForCountry === '/it' ? '/it' : $domainForCountry; $newUrl = $maindomain . $domainPath . $currentUri; if (str_contains($salesChannelName, 'B2B')) { $customer = $event->getRequest()->attributes->get('sw-sales-channel-context')->getCustomer(); $currentRouteName = $event->getRequest()->attributes->get('_route'); if ($customer == null) { if ($currentRouteName === 'frontend.account.recover.page') { return; }else if ($currentRouteName === 'frontend.home.page') { return; } else if ($currentRouteName === 'frontend.account.recover.password.page') { return; } else if ($currentRouteName === 'frontend.account.recover.request') { return; } else if ($currentRouteName !== 'frontend.account.login.page') { $isAllowed = $event->getRequest()->attributes->getBoolean('XmlHttpRequest', false); if ($isAllowed) { return; } $newUrl = $this->router->generate('frontend.account.login.page'); $redirectResponse = new RedirectResponse($newUrl); $redirectResponse->send(); } } else { $customerIdGroup = $customer->getGroupId(); $domainForCustomer = $this->domainForCustomer($customerIdGroup); if($domainForCustomer) { if ($currentDomain !== $domainForCustomer) { $domainPath = $domainForCustomer === '/it' ? '/it' : $domainForCustomer; $newUrl = $maindomain . $domainPath . $currentUri; $redirectResponse = new RedirectResponse($newUrl); $redirectResponse->send(); } } else if ($currentRouteName !== 'frontend.account.home.page') { $isAllowed = $event->getRequest()->attributes->getBoolean('XmlHttpRequest', false); if ($isAllowed) { return; } $newUrl = $this->router->generate('frontend.account.home.page'); $redirectResponse = new RedirectResponse($newUrl); $redirectResponse->send(); } return; } }// $response = new Response(); $event->getResponse()->headers->setCookie(Cookie::create('domainrefer', $domainForCountry)); // $event->getResponse()->headers->setCookie(Cookie::create('ip', $countryIso)); if ($currentDomain !== $domainForCountry) { $redirectResponse = new RedirectResponse($newUrl); $redirectResponse->send(); } } /** * @param string $country * @return string */ public function domainForCountry(string $country) { switch ($country) { case "UK": $domain = '/uk'; break; case "US": case "CA": $domain = '/usa'; break; case "IT": $domain = '/it'; break; default: $domain = '/en'; } return $domain; } /** * @param string $country * @return string */ public function domainForCustomer(string $categoryUser) { switch ($categoryUser) { case "ce2c04e5fa89404ebc2b9f133a81e3e1": case "0173864dfe1744ceb3fbc7dce2b843c6": $domain = '/usa'; break; case "abd998f9c25a450fa64662348294d4a3": case "57fd39492c974bc3aaa4e3c6d635c903": case "0ac146b10d064ceea5fe63032520d122": $domain = false; break; case "8d37666518ff479c8453855c061e3571": case "e88dfa92e9e54de194cea0d5332f27ca": case "01a3a5fa086343d6bed217f542874213": $domain = '/it'; break; default: $domain = '/en'; } return $domain; }}