<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\EventSubscriber;
use App\Admin\Entity\StdEmailsLog;
use App\Cookie\Entity\StdCookies;
use App\Event\StdDeleteUserDataEvent;
use App\Service\StudioService;
use App\Utils\Studio;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\HttpFoundation\Response;
use App\Cookie\Helpers\StdCookieHelper;
use App\Admin\Entity\StdWebUsers;
use App\Event\StdWebUsersLoggedInEvent;
class StudioSubscriber implements EventSubscriberInterface
{
private $entityManager;
private $security;
private $session;
private $request;
private $studioService;
public function __construct(EntityManagerInterface $entityManager, Security $security, RequestStack $requestStack, StudioService $studioService)
{
$this->entityManager = $entityManager;
$this->security = $security;
$this->request = $requestStack->getCurrentRequest();
$this->studioService = $studioService;
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::REQUEST => ['onKernelRequest',10],
KernelEvents::RESPONSE => ['onKernelResponse'],
StdWebUsersLoggedInEvent::NAME => 'onUserLogin',
StdDeleteUserDataEvent::NAME => 'onDeleteUserData',
];
}
public function onKernelRequest(RequestEvent $event): void
{
global $studio;
$this->session = $event->getRequest()->getSession();
/*is_null($studio) && */$studio = new Studio($this->entityManager, $this->security, $this->session);
//Validate Studio Features
$this->studioService->validateFeatures();
// Handle session stdCookie
if (getenv('COOKIE_DATABASE_ENABLED'))
{
if (!$this->session->has('stdCookie') && $event->getRequest()->cookies->get(getenv('COOKIE_STD_NAME'),'') != '')
{
if ($cookie = $this->entityManager->getRepository(StdCookies::class)->findOneBy(['uniqueKey'=>$event->getRequest()->cookies->get(getenv('COOKIE_STD_NAME'),'')]))
{
$expires = (new \DateTime())->add(new \DateInterval(getenv('COOKIE_STD_EXPIRE')));
$cookie->setValidUntil($expires);
$this->entityManager->persist($cookie);
$this->entityManager->flush();
$studio->setCookie($cookie);
$this->session->set('setStdCookie', true);
}
}elseif ($this->session->has('stdCookie')) {
$studio->setCookie($this->session->get('stdCookie'));
}
}
}
public function onKernelResponse(ResponseEvent $event)
{
if ($event->isMainRequest()
&& ($stdCookie = $event->getRequest()->getSession()->get('stdCookie'))
&& $event->getRequest()->getSession()->get('setStdCookie', false))
{
$event->getResponse()->headers->setCookie(\Symfony\Component\HttpFoundation\Cookie::create(getenv('COOKIE_STD_NAME'), $stdCookie->getUniqueKey(), $stdCookie->getValidUntil(), '/', null, false, false, false, null));
$event->getRequest()->getSession()->remove('setStdCookie');
}
}
private function handleWebuserCookies(StdWebUsers $webUser, ?Response $response)
{
if (getenv('COOKIE_DATABASE_ENABLED')
&& ($stdCookie = StdCookieHelper::userLogin($webUser))
&& $response)
{
$response->headers->setCookie(\Symfony\Component\HttpFoundation\Cookie::create(getenv('COOKIE_STD_NAME'), $stdCookie->getUniqueKey(), $stdCookie->getValidUntil(), '/', null, false, false, false, null));
}
}
public function onUserLogin(StdWebUsersLoggedInEvent $event)
{
$webUser = $event->getWebUser();
$response = $event->getResponse();
$this->handleWebuserCookies($webUser, $response);
}
public function onDeleteUserData(StdDeleteUserDataEvent $event)
{
$address=$event->getAddress();
// find std_emails_log to delete
$logsToRemove=$this->entityManager->getRepository(StdEmailsLog::class)->findByToaddress($address);
foreach($logsToRemove as $log){
$this->entityManager->remove($log);
}
$this->entityManager->flush();
}
}