AbstractSessionHandler.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. /**
  12. * This abstract session handler provides a generic implementation
  13. * of the PHP 7.0 SessionUpdateTimestampHandlerInterface,
  14. * enabling strict and lazy session handling.
  15. *
  16. * @author Nicolas Grekas <p@tchwork.com>
  17. */
  18. abstract class AbstractSessionHandler implements \SessionHandlerInterface, \SessionUpdateTimestampHandlerInterface
  19. {
  20. private $sessionName;
  21. private $prefetchId;
  22. private $prefetchData;
  23. private $newSessionId;
  24. private $igbinaryEmptyData;
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function open($savePath, $sessionName)
  29. {
  30. $this->sessionName = $sessionName;
  31. if (!headers_sent() && !ini_get('session.cache_limiter') && '0' !== ini_get('session.cache_limiter')) {
  32. header(sprintf('Cache-Control: max-age=%d, private, must-revalidate', 60 * (int) ini_get('session.cache_expire')));
  33. }
  34. return true;
  35. }
  36. /**
  37. * @param string $sessionId
  38. *
  39. * @return string
  40. */
  41. abstract protected function doRead($sessionId);
  42. /**
  43. * @param string $sessionId
  44. * @param string $data
  45. *
  46. * @return bool
  47. */
  48. abstract protected function doWrite($sessionId, $data);
  49. /**
  50. * @param string $sessionId
  51. *
  52. * @return bool
  53. */
  54. abstract protected function doDestroy($sessionId);
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function validateId($sessionId)
  59. {
  60. $this->prefetchData = $this->read($sessionId);
  61. $this->prefetchId = $sessionId;
  62. return '' !== $this->prefetchData;
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function read($sessionId)
  68. {
  69. if (null !== $this->prefetchId) {
  70. $prefetchId = $this->prefetchId;
  71. $prefetchData = $this->prefetchData;
  72. $this->prefetchId = $this->prefetchData = null;
  73. if ($prefetchId === $sessionId || '' === $prefetchData) {
  74. $this->newSessionId = '' === $prefetchData ? $sessionId : null;
  75. return $prefetchData;
  76. }
  77. }
  78. $data = $this->doRead($sessionId);
  79. $this->newSessionId = '' === $data ? $sessionId : null;
  80. return $data;
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function write($sessionId, $data)
  86. {
  87. if (null === $this->igbinaryEmptyData) {
  88. // see https://github.com/igbinary/igbinary/issues/146
  89. $this->igbinaryEmptyData = \function_exists('igbinary_serialize') ? igbinary_serialize(array()) : '';
  90. }
  91. if ('' === $data || $this->igbinaryEmptyData === $data) {
  92. return $this->destroy($sessionId);
  93. }
  94. $this->newSessionId = null;
  95. return $this->doWrite($sessionId, $data);
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function destroy($sessionId)
  101. {
  102. if (!headers_sent() && ini_get('session.use_cookies')) {
  103. if (!$this->sessionName) {
  104. throw new \LogicException(sprintf('Session name cannot be empty, did you forget to call "parent::open()" in "%s"?.', get_class($this)));
  105. }
  106. $sessionCookie = sprintf(' %s=', urlencode($this->sessionName));
  107. $sessionCookieWithId = sprintf('%s%s;', $sessionCookie, urlencode($sessionId));
  108. $sessionCookieFound = false;
  109. $otherCookies = array();
  110. foreach (headers_list() as $h) {
  111. if (0 !== stripos($h, 'Set-Cookie:')) {
  112. continue;
  113. }
  114. if (11 === strpos($h, $sessionCookie, 11)) {
  115. $sessionCookieFound = true;
  116. if (11 !== strpos($h, $sessionCookieWithId, 11)) {
  117. $otherCookies[] = $h;
  118. }
  119. } else {
  120. $otherCookies[] = $h;
  121. }
  122. }
  123. if ($sessionCookieFound) {
  124. header_remove('Set-Cookie');
  125. foreach ($otherCookies as $h) {
  126. header($h, false);
  127. }
  128. } else {
  129. setcookie($this->sessionName, '', 0, ini_get('session.cookie_path'), ini_get('session.cookie_domain'), ini_get('session.cookie_secure'), ini_get('session.cookie_httponly'));
  130. }
  131. }
  132. return $this->newSessionId === $sessionId || $this->doDestroy($sessionId);
  133. }
  134. }