vendor/shopware/storefront/Controller/CookieController.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Controller;
  3. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  4. use Shopware\Core\Framework\Routing\Annotation\Since;
  5. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  6. use Shopware\Core\System\SystemConfig\SystemConfigService;
  7. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV2;
  8. use Shopware\Storefront\Framework\Captcha\GoogleReCaptchaV3;
  9. use Shopware\Storefront\Framework\Cookie\CookieProviderInterface;
  10. use Symfony\Component\HttpFoundation\Response;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * Returns the cookie-configuration.html.twig template including all cookies returned by the "getCookieGroup"-method
  14.  *
  15.  * Cookies are returned within groups, groups require the "group" attribute
  16.  * A group is structured as described above the "getCookieGroup"-method
  17.  *
  18.  * @Route(defaults={"_routeScope"={"storefront"}})
  19.  *
  20.  * @deprecated tag:v6.5.0 - reason:becomes-internal - Will be internal
  21.  */
  22. class CookieController extends StorefrontController
  23. {
  24.     /**
  25.      * @var CookieProviderInterface
  26.      */
  27.     private $cookieProvider;
  28.     /**
  29.      * @var SystemConfigService
  30.      */
  31.     private $systemConfigService;
  32.     /**
  33.      * @internal
  34.      */
  35.     public function __construct(CookieProviderInterface $cookieProviderSystemConfigService $systemConfigService)
  36.     {
  37.         $this->cookieProvider $cookieProvider;
  38.         $this->systemConfigService $systemConfigService;
  39.     }
  40.     /**
  41.      * @Since("6.1.0.0")
  42.      * @Route("/cookie/offcanvas", name="frontend.cookie.offcanvas", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  43.      */
  44.     public function offcanvas(SalesChannelContext $context): Response
  45.     {
  46.         $cookieGroups $this->cookieProvider->getCookieGroups();
  47.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  48.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  49.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  50.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-configuration.html.twig', ['cookieGroups' => $cookieGroups]);
  51.         $response->headers->set('x-robots-tag''noindex,follow');
  52.         return $response;
  53.     }
  54.     /**
  55.      * @Since("6.1.0.0")
  56.      * @Route("/cookie/permission", name="frontend.cookie.permission", options={"seo"="false"}, methods={"GET"}, defaults={"XmlHttpRequest"=true})
  57.      */
  58.     public function permission(SalesChannelContext $context): Response
  59.     {
  60.         $cookieGroups $this->cookieProvider->getCookieGroups();
  61.         $cookieGroups $this->filterGoogleAnalyticsCookie($context$cookieGroups);
  62.         $cookieGroups $this->filterComfortFeaturesCookie($context->getSalesChannelId(), $cookieGroups);
  63.         $cookieGroups $this->filterGoogleReCaptchaCookie($context->getSalesChannelId(), $cookieGroups);
  64.         $response $this->renderStorefront('@Storefront/storefront/layout/cookie/cookie-permission.html.twig', ['cookieGroups' => $cookieGroups]);
  65.         $response->headers->set('x-robots-tag''noindex,follow');
  66.         return $response;
  67.     }
  68.     private function filterGoogleAnalyticsCookie(SalesChannelContext $context, array $cookieGroups): array
  69.     {
  70.         if ($context->getSalesChannel()->getAnalytics() && $context->getSalesChannel()->getAnalytics()->isActive()) {
  71.             return $cookieGroups;
  72.         }
  73.         $filteredGroups = [];
  74.         foreach ($cookieGroups as $cookieGroup) {
  75.             if ($cookieGroup['snippet_name'] === 'cookie.groupStatistical') {
  76.                 $cookieGroup['entries'] = array_filter($cookieGroup['entries'], function ($item) {
  77.                     return $item['snippet_name'] !== 'cookie.groupStatisticalGoogleAnalytics';
  78.                 });
  79.                 // Only add statistics cookie group if it has entries
  80.                 if (\count($cookieGroup['entries']) > 0) {
  81.                     $filteredGroups[] = $cookieGroup;
  82.                 }
  83.                 continue;
  84.             }
  85.             $filteredGroups[] = $cookieGroup;
  86.         }
  87.         return $filteredGroups;
  88.     }
  89.     private function filterComfortFeaturesCookie(string $salesChannelId, array $cookieGroups): array
  90.     {
  91.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  92.             if ($cookieGroup['snippet_name'] !== 'cookie.groupComfortFeatures') {
  93.                 continue;
  94.             }
  95.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  96.                 if ($entry['snippet_name'] !== 'cookie.groupComfortFeaturesWishlist') {
  97.                     continue;
  98.                 }
  99.                 if (!$this->systemConfigService->get('core.cart.wishlistEnabled'$salesChannelId)) {
  100.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  101.                 }
  102.             }
  103.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  104.                 unset($cookieGroups[$groupIndex]);
  105.             }
  106.         }
  107.         return $cookieGroups;
  108.     }
  109.     private function filterGoogleReCaptchaCookie(string $salesChannelId, array $cookieGroups): array
  110.     {
  111.         foreach ($cookieGroups as $groupIndex => $cookieGroup) {
  112.             if ($cookieGroup['snippet_name'] !== 'cookie.groupRequired') {
  113.                 continue;
  114.             }
  115.             foreach ($cookieGroup['entries'] as $entryIndex => $entry) {
  116.                 if ($entry['snippet_name'] !== 'cookie.groupRequiredCaptcha') {
  117.                     continue;
  118.                 }
  119.                 $activeGreCaptchaV2 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV2::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  120.                 $activeGreCaptchaV3 $this->systemConfigService->get('core.basicInformation.activeCaptchasV2.' GoogleReCaptchaV3::CAPTCHA_NAME '.isActive'$salesChannelId) ?? false;
  121.                 if (!$activeGreCaptchaV2 && !$activeGreCaptchaV3) {
  122.                     unset($cookieGroups[$groupIndex]['entries'][$entryIndex]);
  123.                 }
  124.             }
  125.             if (\count($cookieGroups[$groupIndex]['entries']) === 0) {
  126.                 unset($cookieGroups[$groupIndex]);
  127.             }
  128.         }
  129.         return $cookieGroups;
  130.     }
  131. }