src/Listener/GenerateCookieListener.php line 71

Open in your IDE?
  1. <?php
  2. namespace App\Listener;
  3. use App\Entity\User;
  4. use App\Entity\UserToken;
  5. use App\Services\UserTokenManager;
  6. use Doctrine\ORM\EntityManager;
  7. use Doctrine\ORM\EntityManagerInterface;
  8. use Symfony\Component\HttpFoundation\RedirectResponse;
  9. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  10. use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
  11. use Symfony\Component\HttpFoundation\Cookie;
  12. use Symfony\Component\HttpKernel\HttpKernel;
  13. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  14. class GenerateCookieListener
  15. {
  16.     protected $session;
  17.     private $crypt;
  18.     /**
  19.      * @var EntityManager
  20.      */
  21.     protected $em;
  22.     /**
  23.      * @var TokenStorageInterface
  24.      */
  25.     protected $tokenStorage;
  26.     protected $userTokenManager;
  27.     public function __construct(SessionInterface $sessionEntityManagerInterface $emTokenStorageInterface $tokenStorageUserTokenManager $userTokenManager)
  28.     {
  29.         $this->session $session;
  30.         $this->em $em;
  31.         $this->userTokenManager $userTokenManager;
  32.         $this->tokenStorage $tokenStorage;
  33.     }
  34.     public function onKernelResponse(FilterResponseEvent $event)
  35.     {
  36.         $request  $event->getRequest();
  37.         $response $event->getResponse();
  38.         if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
  39.             return $response;
  40.         }
  41.         $cookie_value $request->cookies->get('cart_session');
  42.         if (strlen($cookie_value)!=36) {
  43.             $characters '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  44.             $randomString '';
  45.             for ($i 0$i 36$i++) {
  46.                 $randomString .= $characters[rand(0strlen($characters) - 1)];
  47.             }
  48.         } else {
  49.             $randomString $cookie_value;
  50.         }
  51.         if (isset($randomString)) {
  52.             $request->getSession()->set('cart_session'$randomString);
  53.             $response $event->getResponse();
  54.             if ($request->query->get('cjevent')) {
  55.                 $response->headers->setCookie(new Cookie("cjevent"$request->query->get('cjevent'), time()+365*24*60*60));
  56.             }
  57.             $response->headers->setCookie(new Cookie("cart_session"$randomStringtime()+14*24*60*60));
  58.             $response->headers->setCookie(new Cookie("cart_session_expire"time()+14*24*60*60time()+14*24*60*60));
  59.             if ($this->tokenStorage->getToken() and $this->tokenStorage->getToken()->getUser() instanceof User and $this->tokenStorage->getToken()->getUser() and $this->tokenStorage->getToken()->getUser()->hasRole('ROLE_ADMIN')) {
  60.                 if ($this->tokenStorage->getToken()->getUser() instanceof User) {
  61.                     /** @var $user User */
  62.                     $user $this->tokenStorage->getToken()->getUser();
  63.                     if ($user->hasRole('ROLE_ADMIN')) {
  64.                         $response->headers->setCookie(new Cookie("adminUser"time()+365*24*60*60time()+365*24*60*60));
  65.                     }
  66.                     $currentCookie $request->cookies->get('user_token');
  67.                     $cookieDb $this->userTokenManager->getTokenByCookie($currentCookie);
  68.                     if (strlen($currentCookie) > 10 and !$cookieDb and !strstr($this->curPageURL(), '/login')) {
  69.                         $this->session->invalidate();
  70.                         exit();
  71.                     } else {
  72.                         $toSet $this->userTokenManager->getUserTokenToSet();
  73.                         $cookieInstance $this->userTokenManager->prepareCookieUserToken($toSet);
  74.                         if ($toSet && $toSet instanceof UserToken && $cookieInstance instanceof Cookie) {
  75.                             $response->headers->setCookie($cookieInstance);
  76.                         }
  77.                     }
  78.                 }
  79.             }
  80.             $response->sendHeaders();
  81.         }
  82.     }
  83.     private function curPageURL() {
  84.         $pageURL 'http';
  85.         if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
  86.         $pageURL .= "://";
  87.         if ($_SERVER["SERVER_PORT"] != "80") {
  88.             $pageURL .= $_SERVER["HTTP_HOST"]."".$_SERVER["REQUEST_URI"];
  89.         } else {
  90.             $pageURL .= $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
  91.         }
  92.         return $pageURL;
  93.     }
  94. }