Files
kupshop/bundles/External/ZNZBundle/EventSubscriber/RequestSubscriber.php
2025-08-02 16:30:27 +02:00

47 lines
1.6 KiB
PHP

<?php
declare(strict_types=1);
namespace External\ZNZBundle\EventSubscriber;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
class RequestSubscriber implements EventSubscriberInterface
{
private const COOKIES_CHECK = ['frontend', 'FPID', 'cookiehub'];
private const COOKIES_DESTROY = ['PHPSESSID', '_hjAbsoluteSessionInProgress', '_ga', 'FPLC', '_cid', 'minicart_html_invalidate_https', 'PAGECACHE_FORMKEY', 'gp_s', 'gp_e', '_fbp', '_gcl_au', 'FPID', 'cookiehub', 'customer_name_invalidate_https', '_gid', '_gtm_mh_isnnbc', 'frontend', 'frontend_cid', 'am_promo_notification', 'PAGECACHE_ENV', '_gasvr', 'FPAU'];
public static function getSubscribedEvents(): array
{
return [
KernelEvents::RESPONSE => [
['destroyOldCookies', 255],
],
];
}
public function destroyOldCookies(ResponseEvent $event): void
{
$destroy = false;
foreach (self::COOKIES_CHECK as $cookie) {
if ($destroy = $event->getRequest()->cookies->has($cookie)) {
break;
}
}
if (!$destroy) {
return;
}
$domain = '.'.$event->getRequest()->getHost();
foreach (self::COOKIES_DESTROY as $cookie) {
if ($event->getRequest()->cookies->has($cookie)) {
$event->getResponse()->headers->setCookie(Cookie::create($cookie, '', 1, domain: $domain));
}
}
}
}