AbstractSessionHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. * {@inheritdoc}
  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. * {@inheritdoc}
  58. */
  59. public function validateId($sessionId)
  60. {
  61. $this->prefetchData = $this->read($sessionId);
  62. $this->prefetchId = $sessionId;
  63. return '' !== $this->prefetchData;
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function read($sessionId)
  69. {
  70. if (null !== $this->prefetchId) {
  71. $prefetchId = $this->prefetchId;
  72. $prefetchData = $this->prefetchData;
  73. $this->prefetchId = $this->prefetchData = null;
  74. if ($prefetchId === $sessionId || '' === $prefetchData) {
  75. $this->newSessionId = '' === $prefetchData ? $sessionId : null;
  76. return $prefetchData;
  77. }
  78. }
  79. $data = $this->doRead($sessionId);
  80. $this->newSessionId = '' === $data ? $sessionId : null;
  81. return $data;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function write($sessionId, $data)
  87. {
  88. if (null === $this->igbinaryEmptyData) {
  89. // see https://github.com/igbinary/igbinary/issues/146
  90. $this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize([]) : '';
  91. }
  92. if ('' === $data || $this->igbinaryEmptyData === $data) {
  93. return $this->destroy($sessionId);
  94. }
  95. $this->newSessionId = null;
  96. return $this->doWrite($sessionId, $data);
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function destroy($sessionId)
  102. {
  103. if (!headers_sent() && filter_var(ini_get('session.use_cookies'), FILTER_VALIDATE_BOOLEAN)) {
  104. if (!$this->sessionName) {
  105. throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', \get_class($this)));
  106. }
  107. $cookie = SessionUtils::popSessionCookie($this->sessionName, $sessionId);
  108. if (null === $cookie) {
  109. if (\PHP_VERSION_ID < 70300) {
  110. 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));
  111. } else {
  112. $params = session_get_cookie_params();
  113. unset($params['lifetime']);
  114. setcookie($this->sessionName, '', $params);
  115. }
  116. }
  117. }
  118. return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
  119. }
  120. }