123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <?php
- namespace Symfony\Component\HttpFoundation\Session\Storage;
- use Symfony\Component\HttpFoundation\Session\SessionBagInterface;
- use Symfony\Component\HttpFoundation\Session\SessionUtils;
- use Symfony\Component\HttpFoundation\Session\Storage\Handler\StrictSessionHandler;
- use Symfony\Component\HttpFoundation\Session\Storage\Proxy\AbstractProxy;
- use Symfony\Component\HttpFoundation\Session\Storage\Proxy\SessionHandlerProxy;
- class_exists(MetadataBag::class);
- class_exists(StrictSessionHandler::class);
- class_exists(SessionHandlerProxy::class);
- class NativeSessionStorage implements SessionStorageInterface
- {
-
- protected $bags = [];
-
- protected $started = false;
-
- protected $closed = false;
-
- protected $saveHandler;
-
- protected $metadataBag;
-
- private $emulateSameSite;
-
- public function __construct(array $options = [], $handler = null, ?MetadataBag $metaBag = null)
- {
- if (!\extension_loaded('session')) {
- throw new \LogicException('PHP extension "session" is required.');
- }
- $options += [
- 'cache_limiter' => '',
- 'cache_expire' => 0,
- 'use_cookies' => 1,
- 'lazy_write' => 1,
- 'use_strict_mode' => 1,
- ];
- session_register_shutdown();
- $this->setMetadataBag($metaBag);
- $this->setOptions($options);
- $this->setSaveHandler($handler);
- }
-
- public function getSaveHandler()
- {
- return $this->saveHandler;
- }
-
- public function start()
- {
- if ($this->started) {
- return true;
- }
- if (\PHP_SESSION_ACTIVE === session_status()) {
- throw new \RuntimeException('Failed to start the session: already started by PHP.');
- }
- if (filter_var(\ini_get('session.use_cookies'), \FILTER_VALIDATE_BOOLEAN) && headers_sent($file, $line)) {
- throw new \RuntimeException(sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line));
- }
- $sessionId = $_COOKIE[session_name()] ?? null;
-
- if ($sessionId && $this->saveHandler instanceof AbstractProxy && 'files' === $this->saveHandler->getSaveHandlerName() && !preg_match('/^[a-zA-Z0-9,-]{22,250}$/', $sessionId)) {
-
- session_id(session_create_id());
- }
-
- if (!session_start()) {
- throw new \RuntimeException('Failed to start the session.');
- }
- if (null !== $this->emulateSameSite) {
- $originalCookie = SessionUtils::popSessionCookie(session_name(), session_id());
- if (null !== $originalCookie) {
- header(sprintf('%s; SameSite=%s', $originalCookie, $this->emulateSameSite), false);
- }
- }
- $this->loadSession();
- return true;
- }
-
- public function getId()
- {
- return $this->saveHandler->getId();
- }
-
- public function setId(string $id)
- {
- $this->saveHandler->setId($id);
- }
-
- public function getName()
- {
- return $this->saveHandler->getName();
- }
-
- public function setName(string $name)
- {
- $this->saveHandler->setName($name);
- }
-
- public function regenerate(bool $destroy = false, ?int $lifetime = null)
- {
-
- if (\PHP_SESSION_ACTIVE !== session_status()) {
- return false;
- }
- if (headers_sent()) {
- return false;
- }
- if (null !== $lifetime && $lifetime != \ini_get('session.cookie_lifetime')) {
- $this->save();
- ini_set('session.cookie_lifetime', $lifetime);
- $this->start();
- }
- if ($destroy) {
- $this->metadataBag->stampNew();
- }
- $isRegenerated = session_regenerate_id($destroy);
- if (null !== $this->emulateSameSite) {
- $originalCookie = SessionUtils::popSessionCookie(session_name(), session_id());
- if (null !== $originalCookie) {
- header(sprintf('%s; SameSite=%s', $originalCookie, $this->emulateSameSite), false);
- }
- }
- return $isRegenerated;
- }
-
- public function save()
- {
-
- $session = $_SESSION;
- foreach ($this->bags as $bag) {
- if (empty($_SESSION[$key = $bag->getStorageKey()])) {
- unset($_SESSION[$key]);
- }
- }
- if ($_SESSION && [$key = $this->metadataBag->getStorageKey()] === array_keys($_SESSION)) {
- unset($_SESSION[$key]);
- }
-
- $previousHandler = set_error_handler(function ($type, $msg, $file, $line) use (&$previousHandler) {
- if (\E_WARNING === $type && str_starts_with($msg, 'session_write_close():')) {
- $handler = $this->saveHandler instanceof SessionHandlerProxy ? $this->saveHandler->getHandler() : $this->saveHandler;
- $msg = sprintf('session_write_close(): Failed to write session data with "%s" handler', \get_class($handler));
- }
- return $previousHandler ? $previousHandler($type, $msg, $file, $line) : false;
- });
- try {
- session_write_close();
- } finally {
- restore_error_handler();
-
- if ($_SESSION) {
- $_SESSION = $session;
- }
- }
- $this->closed = true;
- $this->started = false;
- }
-
- public function clear()
- {
-
- foreach ($this->bags as $bag) {
- $bag->clear();
- }
-
- $_SESSION = [];
-
- $this->loadSession();
- }
-
- public function registerBag(SessionBagInterface $bag)
- {
- if ($this->started) {
- throw new \LogicException('Cannot register a bag when the session is already started.');
- }
- $this->bags[$bag->getName()] = $bag;
- }
-
- public function getBag(string $name)
- {
- if (!isset($this->bags[$name])) {
- throw new \InvalidArgumentException(sprintf('The SessionBagInterface "%s" is not registered.', $name));
- }
- if (!$this->started && $this->saveHandler->isActive()) {
- $this->loadSession();
- } elseif (!$this->started) {
- $this->start();
- }
- return $this->bags[$name];
- }
- public function setMetadataBag(?MetadataBag $metaBag = null)
- {
- if (null === $metaBag) {
- $metaBag = new MetadataBag();
- }
- $this->metadataBag = $metaBag;
- }
-
- public function getMetadataBag()
- {
- return $this->metadataBag;
- }
-
- public function isStarted()
- {
- return $this->started;
- }
-
- public function setOptions(array $options)
- {
- if (headers_sent() || \PHP_SESSION_ACTIVE === session_status()) {
- return;
- }
- $validOptions = array_flip([
- 'cache_expire', 'cache_limiter', 'cookie_domain', 'cookie_httponly',
- 'cookie_lifetime', 'cookie_path', 'cookie_secure', 'cookie_samesite',
- 'gc_divisor', 'gc_maxlifetime', 'gc_probability',
- 'lazy_write', 'name', 'referer_check',
- 'serialize_handler', 'use_strict_mode', 'use_cookies',
- 'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled',
- 'upload_progress.cleanup', 'upload_progress.prefix', 'upload_progress.name',
- 'upload_progress.freq', 'upload_progress.min_freq', 'url_rewriter.tags',
- 'sid_length', 'sid_bits_per_character', 'trans_sid_hosts', 'trans_sid_tags',
- ]);
- foreach ($options as $key => $value) {
- if (isset($validOptions[$key])) {
- if (str_starts_with($key, 'upload_progress.')) {
- trigger_deprecation('symfony/http-foundation', '5.4', 'Support for the "%s" session option is deprecated. The settings prefixed with "session.upload_progress." can not be changed at runtime.', $key);
- continue;
- }
- if ('url_rewriter.tags' === $key) {
- trigger_deprecation('symfony/http-foundation', '5.4', 'Support for the "%s" session option is deprecated. Use "trans_sid_tags" instead.', $key);
- }
- if ('cookie_samesite' === $key && \PHP_VERSION_ID < 70300) {
-
-
- $this->emulateSameSite = $value;
- continue;
- }
- if ('cookie_secure' === $key && 'auto' === $value) {
- continue;
- }
- ini_set('url_rewriter.tags' !== $key ? 'session.'.$key : $key, $value);
- }
- }
- }
-
- public function setSaveHandler($saveHandler = null)
- {
- if (!$saveHandler instanceof AbstractProxy
- && !$saveHandler instanceof \SessionHandlerInterface
- && null !== $saveHandler
- ) {
- throw new \InvalidArgumentException('Must be instance of AbstractProxy; implement \SessionHandlerInterface; or be null.');
- }
-
- if (!$saveHandler instanceof AbstractProxy && $saveHandler instanceof \SessionHandlerInterface) {
- $saveHandler = new SessionHandlerProxy($saveHandler);
- } elseif (!$saveHandler instanceof AbstractProxy) {
- $saveHandler = new SessionHandlerProxy(new StrictSessionHandler(new \SessionHandler()));
- }
- $this->saveHandler = $saveHandler;
- if (headers_sent() || \PHP_SESSION_ACTIVE === session_status()) {
- return;
- }
- if ($this->saveHandler instanceof SessionHandlerProxy) {
- session_set_save_handler($this->saveHandler, false);
- }
- }
-
- protected function loadSession(?array &$session = null)
- {
- if (null === $session) {
- $session = &$_SESSION;
- }
- $bags = array_merge($this->bags, [$this->metadataBag]);
- foreach ($bags as $bag) {
- $key = $bag->getStorageKey();
- $session[$key] = isset($session[$key]) && \is_array($session[$key]) ? $session[$key] : [];
- $bag->initialize($session[$key]);
- }
- $this->started = true;
- $this->closed = false;
- }
- }
|