Session.php 5.0 KB

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