custom/plugins/AcrisCookieConsentCS/src/Subscriber/ResponseSubscriber.php line 81

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Acris\CookieConsent\Subscriber;
  3. use Acris\CookieConsent\Components\CookiesAcceptService;
  4. use Acris\CookieConsent\Components\CookieService;
  5. use Acris\CookieConsent\Components\RegisteredCookiesService;
  6. use Acris\CookieConsent\Custom\CookieEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  8. use Shopware\Core\PlatformRequest;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Response;
  14. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  15. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  16. use Symfony\Component\HttpKernel\KernelEvents;
  17. class ResponseSubscriber implements EventSubscriberInterface
  18. {
  19.     const EXPECTED_COOKIES = [
  20.         'sf_redirect',
  21.         'googtrans',
  22.         'language',
  23.         'PHPSESSID'
  24.     ];
  25.     /**
  26.      * @var CookiesAcceptService
  27.      */
  28.     private $cookiesAcceptService;
  29.     /**
  30.      * @var ContainerInterface
  31.      */
  32.     private $container;
  33.     /**
  34.      * @var CookieService
  35.      */
  36.     private $cookieService;
  37.     /**
  38.      * @var SystemConfigService
  39.      */
  40.     private $systemConfigService;
  41.     /**
  42.      * @var RegisteredCookiesService
  43.      */
  44.     private $registeredCookiesService;
  45.     public function __construct(ContainerInterface $containerCookieService $cookieServiceCookiesAcceptService $cookiesAcceptServiceSystemConfigService $systemConfigServiceRegisteredCookiesService $registeredCookiesService)
  46.     {
  47.         $this->cookiesAcceptService $cookiesAcceptService;
  48.         $this->container $container;
  49.         $this->cookieService $cookieService;
  50.         $this->systemConfigService $systemConfigService;
  51.         $this->registeredCookiesService $registeredCookiesService;
  52.     }
  53.     public static function getSubscribedEvents()
  54.     {
  55.         return [
  56.             KernelEvents::RESPONSE => [
  57.                 ['checkResponseCookies'90000],
  58.                 ['setResponseCache'1500]
  59.             ]
  60.         ];
  61.     }
  62.     public function checkResponseCookies(ResponseEvent $responseEvent): void
  63.     {
  64.         $request $responseEvent->getRequest();
  65.         /** @var SalesChannelContext $salesChannelContext */
  66.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  67.         if (!$salesChannelContext instanceof SalesChannelContext) return;
  68.         if(!$this->systemConfigService->get('AcrisCookieConsentCS.config.active'$salesChannelContext->getSalesChannel()->getId())) return;
  69.         $response $responseEvent->getResponse();
  70.         $session $this->container->get('session');
  71.         $cookiesAccepted $this->cookiesAcceptService->getCookiesAccepted($session$request);
  72.         $deniedCookieGroups $session->get("acrisCookieGroupsDenied", []);
  73.         $deniedCookies $session->get("acrisCookiesDenied", []);
  74.         $availableCookies $this->cookieService->getAllCookies($salesChannelContext);
  75.         $this->registeredCookiesService->getKnownShopCookies($salesChannelContext$availableCookies);
  76.         foreach($request->cookies->all() as $cookieName => $cookieValue) {
  77.             if(in_array($cookieNameself::EXPECTED_COOKIES)) continue;
  78.             /** @var CookieEntity $availableCookie */
  79.             foreach ($availableCookies->getElements() as $availableCookie) {
  80.                 // check if cookie is known
  81.                 try {
  82.                     if($cookieName === $availableCookie->getCookieId() || preg_match("#^(" $availableCookie->getCookieId() . ")$#"$cookieName)) {
  83.                         if(!$availableCookie->isDefault() && ($availableCookie->getCookieGroup() === null || !$availableCookie->getCookieGroup()->isDefault())) {
  84.                             // check if cookies are accepted by user
  85.                             if(!$cookiesAccepted || !$availableCookie->isActive()) {
  86.                                 $this->clearCookie($response$cookieName);
  87.                             } elseif(!empty($deniedCookieGroups) || !empty($deniedCookies)) {
  88.                                 // check if cookie group is denied by user
  89.                                 if(in_array($availableCookie->getCookieGroupId(), $deniedCookieGroups)) {
  90.                                     $this->clearCookie($response$cookieName);
  91.                                     break;
  92.                                 }
  93.                                 // check if cookie is denied by user
  94.                                 if(in_array($availableCookie->getId(), $deniedCookies)) {
  95.                                     $this->clearCookie($response$cookieName);
  96.                                     break;
  97.                                 }
  98.                             }
  99.                         }
  100.                         continue 2;
  101.                     }
  102.                 } catch (\Throwable $e) { }
  103.             }
  104.             // is new cookie which is not known - so clear it and add it to the database
  105.             $this->clearCookie($response$cookieName);
  106.         }
  107.         $this->insertResponseCookieIfNotKnown($response$salesChannelContext$availableCookies);
  108.     }
  109.     /**
  110.      * @param Response $response
  111.      * @param string $cookieName
  112.      */
  113.     public function clearCookie($response$cookieName): void
  114.     {
  115.         unset($_COOKIE[$cookieName]);
  116.         if($response->headers) {
  117.             $response->headers->clearCookie($cookieName);
  118.         }
  119.     }
  120.     /**
  121.      * @param Response $response
  122.      * @param SalesChannelContext $salesChannelContext
  123.      */
  124.     private function insertResponseCookieIfNotKnown(Response $responseSalesChannelContext $salesChannelContextEntityCollection $availableCookies): void
  125.     {
  126.         if($response->headers) {
  127.             foreach ($response->headers->getCookies() as $cookie) {
  128.                 if($cookie->getValue() !== NULL) {
  129.                     $this->cookieService->insertCookieIfNotKnown($salesChannelContext$cookie->getName(), false$availableCookies);
  130.                 }
  131.             }
  132.         }
  133.     }
  134.     public function setResponseCache(ResponseEvent $event)
  135.     {
  136.         $request $event->getRequest();
  137.         if(empty($request->attributes) === true) {
  138.             return;
  139.         }
  140.         /** @var SalesChannelContext $salesChannelContext */
  141.         $salesChannelContext $request->attributes->get(PlatformRequest::ATTRIBUTE_SALES_CHANNEL_CONTEXT_OBJECT);
  142.         if (!$salesChannelContext instanceof SalesChannelContext) return;
  143.         if(!$this->systemConfigService->get('AcrisCookieConsentCS.config.active'$salesChannelContext->getSalesChannel()->getId())) return;
  144.         try {
  145.             $session $event->getRequest()->getSession();
  146.             $this->cookiesAcceptService->updateCookieFromSessionData($session$request);
  147.             $session->save();
  148.         } catch (\Throwable $e) { }
  149.     }
  150. }