![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/mautic.corals.io/app/bundles/CoreBundle/Helper/ |
<?php declare(strict_types=1); namespace Mautic\CoreBundle\Helper; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\HttpFoundation\Cookie; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\RequestStack; use Symfony\Component\HttpKernel\Event\ResponseEvent; use Symfony\Component\HttpKernel\KernelEvents; class CookieHelper implements EventSubscriberInterface { private ?Request $request = null; /** * @var array<string, Cookie> */ private array $cookies = []; public function __construct( private ?string $path, private ?string $domain, private bool $secure, private bool $httponly, private RequestStack $requestStack ) { } /** * @param mixed $default * * @return mixed */ public function getCookie(string $key, $default = null) { if (null === $this->getRequest()) { return $default; } return $this->getRequest()->cookies->get($key, $default); } /** * @param int|string|float|bool|object|null $value */ public function setCookie(string $name, $value, ?int $expire = 1800, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, ?string $sameSite = Cookie::SAMESITE_LAX): void { if (null !== $value) { $value = (string) $value; } $cookie = Cookie::create( $name, $value, null !== $expire ? time() + $expire : 0, $path ?? $this->path, $domain ?? $this->domain, $secure ?? $this->secure, $httponly ?? $this->httponly, false, $sameSite ); $this->cookies[$name] = $cookie; } /** * Deletes a cookie by expiring it. */ public function deleteCookie(string $name, ?string $path = null, ?string $domain = null, ?bool $secure = null, ?bool $httponly = null, ?string $sameSite = Cookie::SAMESITE_LAX): void { $this->setCookie($name, '', -86400, $path, $domain, $secure, $httponly, $sameSite); } public function onResponse(ResponseEvent $event): void { foreach ($this->cookies as $cookie) { $event->getResponse()->headers->setCookie($cookie); } } public static function getSubscribedEvents(): array { return [ KernelEvents::RESPONSE => 'onResponse', ]; } private function getRequest(): ?Request { if (null !== $this->request) { return $this->request; } return $this->request = $this->requestStack->getMainRequest(); } }