<?php
namespace App\Listener;
use App\Entity\User;
use App\Entity\UserToken;
use App\Services\UserTokenManager;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Session\SessionInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
class GenerateCookieListener
{
protected $session;
private $crypt;
/**
* @var EntityManager
*/
protected $em;
/**
* @var TokenStorageInterface
*/
protected $tokenStorage;
protected $userTokenManager;
public function __construct(SessionInterface $session, EntityManagerInterface $em, TokenStorageInterface $tokenStorage, UserTokenManager $userTokenManager)
{
$this->session = $session;
$this->em = $em;
$this->userTokenManager = $userTokenManager;
$this->tokenStorage = $tokenStorage;
}
public function onKernelResponse(FilterResponseEvent $event)
{
$request = $event->getRequest();
$response = $event->getResponse();
if (HttpKernel::MASTER_REQUEST != $event->getRequestType()) {
return $response;
}
$cookie_value = $request->cookies->get('cart_session');
if (strlen($cookie_value)!=36) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < 36; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
} else {
$randomString = $cookie_value;
}
if (isset($randomString)) {
$request->getSession()->set('cart_session', $randomString);
$response = $event->getResponse();
if ($request->query->get('cjevent')) {
$response->headers->setCookie(new Cookie("cjevent", $request->query->get('cjevent'), time()+365*24*60*60));
}
$response->headers->setCookie(new Cookie("cart_session", $randomString, time()+14*24*60*60));
$response->headers->setCookie(new Cookie("cart_session_expire", time()+14*24*60*60, time()+14*24*60*60));
if ($this->tokenStorage->getToken() and $this->tokenStorage->getToken()->getUser() instanceof User and $this->tokenStorage->getToken()->getUser() and $this->tokenStorage->getToken()->getUser()->hasRole('ROLE_ADMIN')) {
if ($this->tokenStorage->getToken()->getUser() instanceof User) {
/** @var $user User */
$user = $this->tokenStorage->getToken()->getUser();
if ($user->hasRole('ROLE_ADMIN')) {
$response->headers->setCookie(new Cookie("adminUser", time()+365*24*60*60, time()+365*24*60*60));
}
$currentCookie = $request->cookies->get('user_token');
$cookieDb = $this->userTokenManager->getTokenByCookie($currentCookie);
if (strlen($currentCookie) > 10 and !$cookieDb and !strstr($this->curPageURL(), '/login')) {
$this->session->invalidate();
exit();
} else {
$toSet = $this->userTokenManager->getUserTokenToSet();
$cookieInstance = $this->userTokenManager->prepareCookieUserToken($toSet);
if ($toSet && $toSet instanceof UserToken && $cookieInstance instanceof Cookie) {
$response->headers->setCookie($cookieInstance);
}
}
}
}
$response->sendHeaders();
}
}
private function curPageURL() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["HTTP_HOST"]."".$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}
}