Session.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. /**
  3. * @package Grav\Common
  4. *
  5. * @copyright Copyright (c) 2015 - 2021 Trilby Media, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Common;
  9. use Grav\Common\Form\FormFlash;
  10. use Grav\Events\SessionStartEvent;
  11. use Grav\Plugin\Form\Forms;
  12. use JsonException;
  13. use function is_string;
  14. /**
  15. * Class Session
  16. * @package Grav\Common
  17. */
  18. class Session extends \Grav\Framework\Session\Session
  19. {
  20. /** @var bool */
  21. protected $autoStart = false;
  22. /**
  23. * @return \Grav\Framework\Session\Session
  24. * @deprecated 1.5 Use ->getInstance() method instead.
  25. */
  26. public static function instance()
  27. {
  28. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->getInstance() method instead', E_USER_DEPRECATED);
  29. return static::getInstance();
  30. }
  31. /**
  32. * Initialize session.
  33. *
  34. * Code in this function has been moved into SessionServiceProvider class.
  35. *
  36. * @return void
  37. */
  38. public function init()
  39. {
  40. if ($this->autoStart && !$this->isStarted()) {
  41. $this->start();
  42. $this->autoStart = false;
  43. }
  44. }
  45. /**
  46. * @param bool $auto
  47. * @return $this
  48. */
  49. public function setAutoStart($auto)
  50. {
  51. $this->autoStart = (bool)$auto;
  52. return $this;
  53. }
  54. /**
  55. * Returns attributes.
  56. *
  57. * @return array Attributes
  58. * @deprecated 1.5 Use ->getAll() method instead.
  59. */
  60. public function all()
  61. {
  62. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->getAll() method instead', E_USER_DEPRECATED);
  63. return $this->getAll();
  64. }
  65. /**
  66. * Checks if the session was started.
  67. *
  68. * @return bool
  69. * @deprecated 1.5 Use ->isStarted() method instead.
  70. */
  71. public function started()
  72. {
  73. user_error(__CLASS__ . '::' . __FUNCTION__ . '() is deprecated since Grav 1.5, use ->isStarted() method instead', E_USER_DEPRECATED);
  74. return $this->isStarted();
  75. }
  76. /**
  77. * Store something in session temporarily.
  78. *
  79. * @param string $name
  80. * @param mixed $object
  81. * @return $this
  82. */
  83. public function setFlashObject($name, $object)
  84. {
  85. $this->__set($name, serialize($object));
  86. return $this;
  87. }
  88. /**
  89. * Return object and remove it from session.
  90. *
  91. * @param string $name
  92. * @return mixed
  93. */
  94. public function getFlashObject($name)
  95. {
  96. $serialized = $this->__get($name);
  97. $object = is_string($serialized) ? unserialize($serialized, ['allowed_classes' => true]) : $serialized;
  98. $this->__unset($name);
  99. if ($name === 'files-upload') {
  100. $grav = Grav::instance();
  101. // Make sure that Forms 3.0+ has been installed.
  102. if (null === $object && isset($grav['forms'])) {
  103. user_error(
  104. __CLASS__ . '::' . __FUNCTION__ . '(\'files-upload\') is deprecated since Grav 1.6, use $form->getFlash()->getLegacyFiles() instead',
  105. E_USER_DEPRECATED
  106. );
  107. /** @var Uri $uri */
  108. $uri = $grav['uri'];
  109. /** @var Forms|null $form */
  110. $form = $grav['forms']->getActiveForm(); // @phpstan-ignore-line
  111. $sessionField = base64_encode($uri->url);
  112. /** @var FormFlash|null $flash */
  113. $flash = $form ? $form->getFlash() : null; // @phpstan-ignore-line
  114. $object = $flash && method_exists($flash, 'getLegacyFiles') ? [$sessionField => $flash->getLegacyFiles()] : null;
  115. }
  116. }
  117. return $object;
  118. }
  119. /**
  120. * Store something in cookie temporarily.
  121. *
  122. * @param string $name
  123. * @param mixed $object
  124. * @param int $time
  125. * @return $this
  126. * @throws JsonException
  127. */
  128. public function setFlashCookieObject($name, $object, $time = 60)
  129. {
  130. setcookie($name, json_encode($object, JSON_THROW_ON_ERROR), $this->getCookieOptions($time));
  131. return $this;
  132. }
  133. /**
  134. * Return object and remove it from the cookie.
  135. *
  136. * @param string $name
  137. * @return mixed|null
  138. * @throws JsonException
  139. */
  140. public function getFlashCookieObject($name)
  141. {
  142. if (isset($_COOKIE[$name])) {
  143. $cookie = $_COOKIE[$name];
  144. setcookie($name, '', $this->getCookieOptions(-42000));
  145. return json_decode($cookie, false, 512, JSON_THROW_ON_ERROR);
  146. }
  147. return null;
  148. }
  149. /**
  150. * @return void
  151. */
  152. protected function onSessionStart(): void
  153. {
  154. $event = new SessionStartEvent($this);
  155. $grav = Grav::instance();
  156. $grav->dispatchEvent($event);
  157. }
  158. }