vendor/contao/core-bundle/src/EventListener/ClearSessionDataListener.php line 28

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /*
  4.  * This file is part of Contao.
  5.  *
  6.  * (c) Leo Feyer
  7.  *
  8.  * @license LGPL-3.0-or-later
  9.  */
  10. namespace Contao\CoreBundle\EventListener;
  11. use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
  12. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  13. use Symfony\Component\HttpKernel\Event\ResponseEvent;
  14. use Symfony\Component\Security\Core\Security;
  15. /**
  16.  * @internal
  17.  */
  18. class ClearSessionDataListener
  19. {
  20.     /**
  21.      * Clear the Contao session data if not a POST request.
  22.      */
  23.     public function __invoke(ResponseEvent $event): void
  24.     {
  25.         if (!$event->isMasterRequest()) {
  26.             return;
  27.         }
  28.         $request $event->getRequest();
  29.         if ($request->isMethod('POST')) {
  30.             return;
  31.         }
  32.         if (!$request->hasSession() || !$request->getSession()->isStarted()) {
  33.             return;
  34.         }
  35.         if ($event->getResponse()->isSuccessful()) {
  36.             $this->clearLoginData($request->getSession());
  37.         }
  38.         $this->clearLegacyAttributeBags('FE_DATA');
  39.         $this->clearLegacyAttributeBags('BE_DATA');
  40.         $this->clearLegacyFormData();
  41.     }
  42.     private function clearLegacyAttributeBags(string $key): void
  43.     {
  44.         if (!isset($_SESSION[$key])) {
  45.             return;
  46.         }
  47.         if (($bag $_SESSION[$key]) instanceof AttributeBag && !$bag->count()) {
  48.             unset($_SESSION[$key]);
  49.         }
  50.     }
  51.     private function clearLegacyFormData(): void
  52.     {
  53.         if (isset($_SESSION['FORM_DATA']['SUBMITTED_AT'])) {
  54.             $waitingTime max(30, (int) ini_get('max_execution_time')) * 2;
  55.             // Leave the data available for $waitingTime seconds (for redirect confirmation pages)
  56.             if ($_SESSION['FORM_DATA']['SUBMITTED_AT'] + $waitingTime time()) {
  57.                 return;
  58.             }
  59.         }
  60.         unset($_SESSION['FORM_DATA'], $_SESSION['FILES']);
  61.     }
  62.     private function clearLoginData(SessionInterface $session): void
  63.     {
  64.         $session->remove(Security::AUTHENTICATION_ERROR);
  65.         $session->remove(Security::LAST_USERNAME);
  66.     }
  67. }