AbstractSessionHandler.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #[\ReturnTypeWillChange]
  30. public function open($savePath, $sessionName)
  31. {
  32. $this->sessionName = $sessionName;
  33. if (!headers_sent() && !ini_get('session.cache_limiter') && '0' !== ini_get('session.cache_limiter')) {
  34. header(sprintf('Cache-Control: max-age=%d, private, must-revalidate', 60 * (int) ini_get('session.cache_expire')));
  35. }
  36. return true;
  37. }
  38. /**
  39. * @param string $sessionId
  40. *
  41. * @return string
  42. */
  43. abstract protected function doRead($sessionId);
  44. /**
  45. * @param string $sessionId
  46. * @param string $data
  47. *
  48. * @return bool
  49. */
  50. abstract protected function doWrite($sessionId, $data);
  51. /**
  52. * @param string $sessionId
  53. *
  54. * @return bool
  55. */
  56. abstract protected function doDestroy($sessionId);
  57. /**
  58. * @return bool
  59. */
  60. #[\ReturnTypeWillChange]
  61. public function validateId($sessionId)
  62. {
  63. $this->prefetchData = $this->read($sessionId);
  64. $this->prefetchId = $sessionId;
  65. if (\PHP_VERSION_ID < 70317 || (70400 <= \PHP_VERSION_ID && \PHP_VERSION_ID < 70405)) {
  66. // work around https://bugs.php.net/79413
  67. foreach (debug_backtrace(\DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
  68. if (!isset($frame['class']) && isset($frame['function']) && \in_array($frame['function'], ['session_regenerate_id', 'session_create_id'], true)) {
  69. return '' === $this->prefetchData;
  70. }
  71. }
  72. }
  73. return '' !== $this->prefetchData;
  74. }
  75. /**
  76. * @return string
  77. */
  78. #[\ReturnTypeWillChange]
  79. public function read($sessionId)
  80. {
  81. if (null !== $this->prefetchId) {
  82. $prefetchId = $this->prefetchId;
  83. $prefetchData = $this->prefetchData;
  84. $this->prefetchId = $this->prefetchData = null;
  85. if ($prefetchId === $sessionId || '' === $prefetchData) {
  86. $this->newSessionId = '' === $prefetchData ? $sessionId : null;
  87. return $prefetchData;
  88. }
  89. }
  90. $data = $this->doRead($sessionId);
  91. $this->newSessionId = '' === $data ? $sessionId : null;
  92. return $data;
  93. }
  94. /**
  95. * @return bool
  96. */
  97. #[\ReturnTypeWillChange]
  98. public function write($sessionId, $data)
  99. {
  100. if (null === $this->igbinaryEmptyData) {
  101. // see https://github.com/igbinary/igbinary/issues/146
  102. $this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
  103. }
  104. if ('' === $data || $this->igbinaryEmptyData === $data) {
  105. return $this->destroy($sessionId);
  106. }
  107. $this->newSessionId = null;
  108. return $this->doWrite($sessionId, $data);
  109. }
  110. /**
  111. * @return bool
  112. */
  113. #[\ReturnTypeWillChange]
  114. public function destroy($sessionId)
  115. {
  116. if (!headers_sent() && filter_var(ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOLEAN)) {
  117. if (!$this->sessionName) {
  118. throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', static::class));
  119. }
  120. $cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);
  121. /*
  122. * We send an invalidation Set-Cookie header (zero lifetime)
  123. * when either the session was started or a cookie with
  124. * the session name was sent by the client (in which case
  125. * we know it's invalid as a valid session cookie would've
  126. * started the session).
  127. */
  128. if (null === $cookie || isset($_COOKIE[$this->sessionName])) {
  129. if (\PHP_VERSION_ID < 70300) {
  130. setcookie($this->sessionName, '', 0, ini_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));
  131. } else {
  132. $params = session_get_cookie_params();
  133. unset($params['lifetime']);
  134. setcookie($this->sessionName, '', $params);
  135. }
  136. }
  137. }
  138. return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
  139. }
  140. }