vendor/symfony/http-foundation/Session/Storage/Handler/AbstractSessionHandler.php line 103

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpFoundation\Session\Storage\Handler;
  11. use Symfony\Component\HttpFoundation\Session\SessionUtils;
  12. /**
  13.  * This abstract session handler provides a generic implementation
  14.  * of the PHP 7.0 SessionUpdateTimestampHandlerInterface,
  15.  * enabling strict and lazy session handling.
  16.  *
  17.  * @author Nicolas Grekas <p@tchwork.com>
  18.  */
  19. abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
  20. {
  21.     private $sessionName;
  22.     private $prefetchId;
  23.     private $prefetchData;
  24.     private $newSessionId;
  25.     private $igbinaryEmptyData;
  26.     /**
  27.      * @return bool
  28.      */
  29.     public function open($savePath$sessionName)
  30.     {
  31.         $this->sessionName $sessionName;
  32.         if (!headers_sent() && !ini_get('session.cache_limiter') && '0' !== ini_get('session.cache_limiter')) {
  33.             header(sprintf('Cache-Control: max-age=%d, private, must-revalidate'60 * (int) ini_get('session.cache_expire')));
  34.         }
  35.         return true;
  36.     }
  37.     /**
  38.      * @param string $sessionId
  39.      *
  40.      * @return string
  41.      */
  42.     abstract protected function doRead($sessionId);
  43.     /**
  44.      * @param string $sessionId
  45.      * @param string $data
  46.      *
  47.      * @return bool
  48.      */
  49.     abstract protected function doWrite($sessionId$data);
  50.     /**
  51.      * @param string $sessionId
  52.      *
  53.      * @return bool
  54.      */
  55.     abstract protected function doDestroy($sessionId);
  56.     /**
  57.      * @return bool
  58.      */
  59.     public function validateId($sessionId)
  60.     {
  61.         $this->prefetchData $this->read($sessionId);
  62.         $this->prefetchId $sessionId;
  63.         if (\PHP_VERSION_ID 70317 || (70400 <= \PHP_VERSION_ID && \PHP_VERSION_ID 70405)) {
  64.             // work around https://bugs.php.net/79413
  65.             foreach (debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
  66.                 if (!isset($frame['class']) && isset($frame['function']) && \in_array($frame['function'], ['session_regenerate_id''session_create_id'], true)) {
  67.                     return '' === $this->prefetchData;
  68.                 }
  69.             }
  70.         }
  71.         return '' !== $this->prefetchData;
  72.     }
  73.     /**
  74.      * @return string
  75.      */
  76.     public function read($sessionId)
  77.     {
  78.         if (null !== $this->prefetchId) {
  79.             $prefetchId $this->prefetchId;
  80.             $prefetchData $this->prefetchData;
  81.             $this->prefetchId $this->prefetchData null;
  82.             if ($prefetchId === $sessionId || '' === $prefetchData) {
  83.                 $this->newSessionId '' === $prefetchData $sessionId null;
  84.                 return $prefetchData;
  85.             }
  86.         }
  87.         $data $this->doRead($sessionId);
  88.         $this->newSessionId '' === $data $sessionId null;
  89.         if (\PHP_VERSION_ID 70000) {
  90.             $this->prefetchData $data;
  91.         }
  92.         return $data;
  93.     }
  94.     /**
  95.      * @return bool
  96.      */
  97.     public function write($sessionId$data)
  98.     {
  99.         if (\PHP_VERSION_ID 70000 && $this->prefetchData) {
  100.             $readData $this->prefetchData;
  101.             $this->prefetchData null;
  102.             if ($readData === $data) {
  103.                 return $this->updateTimestamp($sessionId$data);
  104.             }
  105.         }
  106.         if (null === $this->igbinaryEmptyData) {
  107.             // see https://github.com/igbinary/igbinary/issues/146
  108.             $this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
  109.         }
  110.         if ('' === $data || $this->igbinaryEmptyData === $data) {
  111.             return $this->destroy($sessionId);
  112.         }
  113.         $this->newSessionId null;
  114.         return $this->doWrite($sessionId$data);
  115.     }
  116.     /**
  117.      * @return bool
  118.      */
  119.     public function destroy($sessionId)
  120.     {
  121.         if (\PHP_VERSION_ID 70000) {
  122.             $this->prefetchData null;
  123.         }
  124.         if (!headers_sent() && filter_var(ini_get('session.use_cookies'), FILTER_VALIDATE_BOOLEAN)) {
  125.             if (!$this->sessionName) {
  126.                 throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
  127.             }
  128.             $cookie SessionUtils::popSessionCookie($this->sessionName$sessionId);
  129.             /*
  130.              * We send an invalidation Set-Cookie header (zero lifetime)
  131.              * when either the session was started or a cookie with
  132.              * the session name was sent by the client (in which case
  133.              * we know it's invalid as a valid session cookie would've
  134.              * started the session).
  135.              */
  136.             if (null === $cookie || isset($_COOKIE[$this->sessionName])) {
  137.                 if (\PHP_VERSION_ID 70300) {
  138.                     setcookie($this->sessionName''0ini_get('session.cookie_path'), ini_get('session.cookie_domain'), filter_var(ini_get('session.cookie_secure'), FILTER_VALIDATE_BOOLEAN), filter_var(ini_get('session.cookie_httponly'), FILTER_VALIDATE_BOOLEAN));
  139.                 } else {
  140.                     $params session_get_cookie_params();
  141.                     unset($params['lifetime']);
  142.                     setcookie($this->sessionName''$params);
  143.                 }
  144.             }
  145.         }
  146.         return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
  147.     }
  148. }