custom/plugins/AcrisCookieConsentCS/src/Components/CookieService.php line 90

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * ACRIS Shopware Plugin
  4.  * Copyright (c) - ACRIS E-Commerce GmbH
  5.  *
  6.  * Die Urheberrechte verbleiben bei der ACRIS E-Commerce GmbH.
  7.  * Sie dürfen vom Auftraggeber während und nach der Beendigung
  8.  * der Zusammenarbeit ausschließlich für die vereinbarten Zwe-
  9.  * cke verändert werden. Der Auftraggeber ist nicht berechtigt
  10.  * ohne ausdrückliche Zustimmung des Auftragnehmers das Werk zu
  11.  * vervielfältigen und / oder zu verbreiten. Änderungen bzw. Be-
  12.  * arbeitungen von Leistungen des Auftragnehmers, wie insbeson-
  13.  * dere deren Weiterentwicklung durch den Auftragnehmer (Käufer)
  14.  * oder durch für diesen tätige Dritte, sind nur mit ausdrückli-
  15.  * cher Zustimmung der ACRIS E-Commerce GmbH zulässig.
  16.  *
  17.  * Es gelten die AGB der ACRIS E-Commerce GmbH in der aktuell
  18.  * gültigen Fassung.
  19.  */
  20. namespace Acris\CookieConsent\Components;
  21. use Acris\CookieConsent\AcrisCookieConsentCS as AcrisCookieConsent;
  22. use Acris\CookieConsent\Custom\CookieEntity;
  23. use Acris\CookieConsent\Custom\CookieGroupEntity;
  24. use Shopware\Core\Defaults;
  25. use Shopware\Core\Framework\Context;
  26. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  27. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  28. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  29. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  30. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  31. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\NotFilter;
  32. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\OrFilter;
  33. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  34. use Shopware\Core\System\SalesChannel\SalesChannelCollection;
  35. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  36. use Shopware\Core\System\SystemConfig\SystemConfigService;
  37. use Symfony\Component\DependencyInjection\ContainerInterface;
  38. class CookieService
  39. {
  40.     const EXPECTED_COOKIES = [
  41.         'sf_redirect',
  42.         'googtrans',
  43.         'language',
  44.         'PHPSESSID',
  45.         'acrisImportExportCookie'
  46.     ];
  47.     /**
  48.      * @var ContainerInterface
  49.      */
  50.     private $container;
  51.     /**
  52.      * @var AcrisCookieConsent
  53.      */
  54.     private $acrisCookieConsent;
  55.     /**
  56.      * @var EntityRepositoryInterface
  57.      */
  58.     private $cookieRepository;
  59.     /**
  60.      * @var EntityRepositoryInterface
  61.      */
  62.     private $cookieGroupRepository;
  63.     /**
  64.      * @var SystemConfigService
  65.      */
  66.     private $systemConfigService;
  67.     /**
  68.      * @param ContainerInterface $container
  69.      */
  70.     public function __construct(ContainerInterface $containerEntityRepositoryInterface $cookieRepositoryEntityRepositoryInterface $cookieGroupRepositoryAcrisCookieConsent $acrisCookieConsentSystemConfigService $systemConfigService)
  71.     {
  72.         $this->container $container;
  73.         $this->acrisCookieConsent $acrisCookieConsent;
  74.         $this->cookieRepository $cookieRepository;
  75.         $this->cookieGroupRepository $cookieGroupRepository;
  76.         $this->systemConfigService $systemConfigService;
  77.     }
  78.     /**
  79.      * @param Context $context
  80.      * @param string $salesChannelId
  81.      * @return array
  82.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  83.      */
  84.     public function getAllKnownCookieIds(Context $contextstring $salesChannelId): array
  85.     {
  86.         /** @var EntityCollection $cookies */
  87.         $cookies $this->cookieRepository->search((new Criteria())->addAssociation('salesChannels'), $context)->getEntities();
  88.         $cookieIds = [];
  89.         if(!$cookies) return [];
  90.         /** @var CookieEntity $cookie */
  91.         foreach ($cookies->getElements() as $cookie) {
  92.             if($cookie->isDefault() !== true && $this->isCookieAvailableForSalesChannels($salesChannelId$cookie->getSalesChannels()) === false) {
  93.                 continue;
  94.             }
  95.             $cookieIds[] = $cookie->getCookieId();
  96.         }
  97.         return $cookieIds;
  98.     }
  99.     /**
  100.      * @param Context $context
  101.      * @param string $salesChannelId
  102.      * @return array
  103.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  104.      */
  105.     public function getAvailableCookieGroups(Context $contextstring $salesChannelId): array
  106.     {
  107.         /** @var EntitySearchResult $cookieGroups */
  108.         $cookieGroups $this->cookieGroupRepository->search((new Criteria())->addAssociation('cookies')->addAssociation('cookies.salesChannels')->addSorting(new FieldSorting('isDefault'FieldSorting::DESCENDING)), $context);
  109.         $cookieGroupArray = [];
  110.         /** @var CookieGroupEntity $cookieGroup */
  111.         foreach ($cookieGroups->getElements() as $cookieGroup) {
  112.             $cookies $cookieGroup->getCookies();
  113.             if(!empty($cookies)) {
  114.                 $cookiesArray = [];
  115.                 foreach ($cookies->getElements() as $cookie) {
  116.                     if($cookie->isActive() === true) {
  117.                         // check if cookie is allowed for shop
  118.                         if(!$cookie->isDefault()) {
  119.                             if($this->isCookieAvailableForSalesChannels($salesChannelId$cookie->getSalesChannels()) === false) continue;
  120.                         }
  121.                         $cookiesArray[$cookie->getId()] = $cookie->getVars();
  122.                     }
  123.                 }
  124.                 if(!empty($cookiesArray)) {
  125.                     $cookieGroupArray[$cookieGroup->getId()] = $cookieGroup->getVars();
  126.                     $cookieGroupArray[$cookieGroup->getId()]['cookies'] = $cookiesArray;
  127.                 }
  128.             }
  129.         }
  130.         return $cookieGroupArray;
  131.     }
  132.     /**
  133.      * @param string $salesChannelId
  134.      * @param SalesChannelCollection|null $salesChannelCollection
  135.      * @return bool
  136.      */
  137.     protected function isCookieAvailableForSalesChannels(string $salesChannelId, ?SalesChannelCollection $salesChannelCollection): bool
  138.     {
  139.         if($salesChannelCollection && $salesChannelCollection->count()) {
  140.             foreach ($salesChannelCollection->getElements() as $salesChannelEntity) {
  141.                 if($salesChannelEntity->getId() === $salesChannelId) {
  142.                     return true;
  143.                 }
  144.             }
  145.             return false;
  146.         }
  147.         return true;
  148.     }
  149.     public function getAllCookies(SalesChannelContext $salesChannelContext): EntityCollection
  150.     {
  151.         return $this->cookieRepository->search((new Criteria())->addAssociation('salesChannels'), $salesChannelContext->getContext())->getEntities();
  152.     }
  153.     /**
  154.      * @param SalesChannelContext $salesChannelContext
  155.      * @return array
  156.      * @throws \Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException
  157.      */
  158.     public function getNotFunctionalCookieGroupIds(SalesChannelContext $salesChannelContext): array
  159.     {
  160.         $notFunctionalGroups $this->cookieGroupRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('isDefault'false)), $salesChannelContext->getContext())->getIds();
  161.         $notFunctionalGroupArray = [];
  162.         foreach ($notFunctionalGroups as $notFunctionalGroupId) {
  163.             $notFunctionalGroupArray[$notFunctionalGroupId] = $notFunctionalGroupId;
  164.         }
  165.         return $notFunctionalGroupArray;
  166.     }
  167.     /**
  168.      * @param SalesChannelContext $salesChannelContext
  169.      * @param string $cookieName
  170.      * @param false $force
  171.      * @param null $availableCookies
  172.      * @param array $additionalCookieData
  173.      * @param false $isShopwareCookie
  174.      */
  175.     public function insertCookieIfNotKnown(SalesChannelContext $salesChannelContextstring $cookieName$force false$availableCookies null$additionalCookieData = [], $isShopwareCookie false): void
  176.     {
  177.         if($isShopwareCookie === false && !$this->systemConfigService->get('AcrisCookieConsentCS.config.autoDetectionActive'$salesChannelContext->getSalesChannel()->getId())) {
  178.             return;
  179.         }
  180.         if(!$cookieName) return;
  181.         if(in_array($cookieNameself::EXPECTED_COOKIES)) return;
  182.         if(!$force) {
  183.             if($availableCookies === null) {
  184.                 $availableCookies $this->getAllCookies($salesChannelContext);
  185.             }
  186.             if($this->isCookieKnownForSalesChannel($cookieName$availableCookies$salesChannelContext->getSalesChannel()->getId(), $salesChannelContext->getContext()) === true) {
  187.                 return;
  188.             }
  189.         }
  190.         $knownCookies parse_ini_file($this->acrisCookieConsent->getPath() . "/Components/Resources/optionalKnownCookies.ini"true);
  191.         foreach ($knownCookies as $cookieId => $knownCookie) {
  192.             if($knownCookie['groupIdentification'] && $cookieName === $cookieId || preg_match("#^(" $cookieId ")$#"$cookieName)) {
  193.                 $groupId $this->findGroupIdByIdentification($salesChannelContext$knownCookie['groupIdentification']);
  194.                 if(!$groupId && !empty($additionalCookieData) && array_key_exists('cookieGroupId'$additionalCookieData) === true && $additionalCookieData['cookieGroupId']) {
  195.                     $groupId $additionalCookieData['cookieGroupId'];
  196.                 }
  197.                 if(!$groupId) continue;
  198.                 $data = ['cookieId'=>$cookieId,'translations' => [
  199.                     'en-GB' => [
  200.                         'title' => $knownCookie['title-en'],
  201.                         'description' => $knownCookie['description-en'],
  202.                     ],'de-DE' => [
  203.                         'title' => $knownCookie['title-de'],
  204.                         'description' => $knownCookie['description-de'],
  205.                     ],[
  206.                         'title' => $knownCookie['title-en'],
  207.                         'description' => $knownCookie['description-en'],
  208.                         'languageId' => Defaults::LANGUAGE_SYSTEM
  209.                     ]
  210.                     ],'provider'=>$knownCookie['provider'],'unknown'=>false,'active'=>true,'cookieGroupId'=>$groupId'salesChannels' => [=> ['id' => $salesChannelContext->getSalesChannel()->getId()]]];
  211.                 if(array_key_exists('defaultValue'$additionalCookieData) && $additionalCookieData['defaultValue']) {
  212.                     $data['defaultValue'] = $additionalCookieData['defaultValue'];
  213.                 }
  214.                 if(array_key_exists("googleCookieConsentMode"$knownCookie)) {
  215.                     $googleCookieConsentMode = [];
  216.                     if(str_contains($knownCookie["googleCookieConsentMode"], "|")){
  217.                         $googleCookieConsentModes explode("|"$knownCookie["googleCookieConsentMode"]);
  218.                         foreach($googleCookieConsentModes as $cookieConsentMode){
  219.                             $googleCookieConsentMode[] = $cookieConsentMode;
  220.                         }
  221.                     } else {
  222.                         $googleCookieConsentMode[] = $knownCookie["googleCookieConsentMode"];
  223.                     }
  224.                     $data["googleCookieConsentMode"] = $googleCookieConsentMode;
  225.                 }
  226.                 $this->cookieRepository->create([$data], $salesChannelContext->getContext());
  227.                 return;
  228.             }
  229.         }
  230.         $cookieData array_merge(['cookieId'=>$cookieName,'unknown'=>true,'active'=>false'salesChannels' => [=> ['id' => $salesChannelContext->getSalesChannel()->getId()]]], $additionalCookieData);
  231.         $this->cookieRepository->create([$cookieData], $salesChannelContext->getContext());
  232.     }
  233.     public function findGroupIdByIdentification(SalesChannelContext $salesChannelContextstring $identification): string
  234.     {
  235.         $cookieGroups $this->cookieGroupRepository->searchIds((new Criteria())->addFilter(new EqualsFilter('identification'$identification))->setLimit(1), $salesChannelContext->getContext())->getIds();
  236.         empty($cookieGroups) ? $cookieGroupId "" $cookieGroupId $cookieGroups[0];
  237.         return $cookieGroupId;
  238.     }
  239.     public function isCookieKnownForSalesChannel(string $cookieNameEntityCollection $availableCookiesstring $currentSalesChannelIdContext $context): bool
  240.     {
  241.         /** @var CookieEntity $availableCookie */
  242.         foreach ($availableCookies->getElements() as $availableCookie) {
  243.             try {
  244.                 if($cookieName === $availableCookie->getCookieId() || preg_match("#^(" $availableCookie->getCookieId() . ")$#"$cookieName)) {
  245.                     if($availableCookie->getSalesChannels()) {
  246.                         foreach ($availableCookie->getSalesChannels()->getElements() as $salesChannel) {
  247.                             if($currentSalesChannelId === $salesChannel->getId()) {
  248.                                 return true;
  249.                             }
  250.                         }
  251.                     }
  252.                     $this->addSalesChannelToCookie($availableCookie$currentSalesChannelId$context);
  253.                     return true;
  254.                 }
  255.             } catch (\Throwable $e) { }
  256.         }
  257.         return false;
  258.     }
  259.     public function getDefaultCookies(String $salesChannelIdContext $context): EntityCollection {
  260.         $criteria = new Criteria();
  261.         $criteria->addAssociation('salesChannels')
  262.                  ->addFilter(new EqualsFilter('active'true))
  263.                  ->addFilter(new NotFilter(NotFilter::CONNECTION_AND,
  264.                             [new EqualsFilter('defaultValue'null)]))
  265.                  ->addFilter(new OrFilter([new EqualsFilter('salesChannels.id'null), new EqualsFilter('salesChannels.id'$salesChannelId)]))
  266.                  ->addFilter(new EqualsFilter('cookieGroup.isDefault'true));
  267.         return $this->cookieRepository->search($criteria$context)->getEntities();
  268.     }
  269.     private function addSalesChannelToCookie(CookieEntity $availableCookiestring $currentSalesChannelIdContext $context): void
  270.     {
  271.         $salesChannelsArray = [];
  272.         if($availableCookie->getSalesChannels()) {
  273.             foreach ($availableCookie->getSalesChannels() as $salesChannel) {
  274.                 $salesChannelsArray[] = ['id'=> $salesChannel->getId()];
  275.             }
  276.         }
  277.         $salesChannelsArray[] = ['id'=> $currentSalesChannelId];
  278.         $this->cookieRepository->upsert([['id'=>$availableCookie->getId(), 'salesChannels' => $salesChannelsArray]], $context);
  279.     }
  280. }