[ Index ]

PHP Cross Reference of Joomla 4.2.2 documentation

title

Body

[close]

/libraries/vendor/joomla/session/src/Storage/ -> NativeStorage.php (source)

   1  <?php
   2  /**
   3   * Part of the Joomla Framework Session Package
   4   *
   5   * @copyright  Copyright (C) 2005 - 2021 Open Source Matters, Inc. All rights reserved.
   6   * @license    GNU General Public License version 2 or later; see LICENSE
   7   */
   8  
   9  namespace Joomla\Session\Storage;
  10  
  11  use Joomla\Session\HandlerInterface;
  12  use Joomla\Session\StorageInterface;
  13  
  14  /**
  15   * Base class providing a session store
  16   *
  17   * @since  2.0.0
  18   */
  19  class NativeStorage implements StorageInterface
  20  {
  21      /**
  22       * Flag if the session is active
  23       *
  24       * @var    boolean
  25       * @since  2.0.0
  26       */
  27      private $active = false;
  28  
  29      /**
  30       * Internal flag identifying whether the session has been closed
  31       *
  32       * @var    boolean
  33       * @since  2.0.0
  34       */
  35      private $closed = false;
  36  
  37      /**
  38       * Session save handler
  39       *
  40       * @var    \SessionHandlerInterface
  41       * @since  2.0.0
  42       */
  43      private $handler;
  44  
  45      /**
  46       * Internal flag identifying whether the session has been started
  47       *
  48       * @var    boolean
  49       * @since  2.0.0
  50       */
  51      private $started = false;
  52  
  53      /**
  54       * Constructor
  55       *
  56       * @param   \SessionHandlerInterface  $handler  Session save handler
  57       * @param   array                     $options  Session options
  58       *
  59       * @since   1.0
  60       */
  61  	public function __construct(?\SessionHandlerInterface $handler = null, array $options = [])
  62      {
  63          // Disable transparent sid support and default use cookies
  64          $options += [
  65              'use_cookies'   => 1,
  66              'use_trans_sid' => 0,
  67          ];
  68  
  69          if (!headers_sent())
  70          {
  71              session_cache_limiter('none');
  72          }
  73  
  74          session_register_shutdown();
  75  
  76          $this->setOptions($options);
  77          $this->setHandler($handler);
  78      }
  79  
  80      /**
  81       * Retrieves all variables from the session store
  82       *
  83       * @return  array
  84       *
  85       * @since   2.0.0
  86       */
  87  	public function all(): array
  88      {
  89          return $_SESSION;
  90      }
  91  
  92      /**
  93       * Clears all variables from the session store
  94       *
  95       * @return  void
  96       *
  97       * @since   2.0.0
  98       */
  99  	public function clear(): void
 100      {
 101          $_SESSION = [];
 102      }
 103  
 104      /**
 105       * Writes session data and ends session
 106       *
 107       * @return  void
 108       *
 109       * @see     session_write_close()
 110       * @since   2.0.0
 111       */
 112  	public function close(): void
 113      {
 114          session_write_close();
 115  
 116          $this->closed  = true;
 117          $this->started = false;
 118      }
 119  
 120      /**
 121       * Perform session data garbage collection
 122       *
 123       * @return  integer|boolean  Number of deleted sessions on success or boolean false on failure or if the function is unsupported
 124       *
 125       * @see     session_gc()
 126       * @since   2.0.0
 127       */
 128      public function gc()
 129      {
 130          if (!$this->isStarted())
 131          {
 132              $this->start();
 133          }
 134  
 135          return session_gc();
 136      }
 137  
 138      /**
 139       * Aborts the current session
 140       *
 141       * @return  boolean
 142       *
 143       * @see     session_abort()
 144       * @since   2.0.0
 145       */
 146  	public function abort(): bool
 147      {
 148          if (!$this->isStarted())
 149          {
 150              return true;
 151          }
 152  
 153          return session_abort();
 154      }
 155  
 156      /**
 157       * Get data from the session store
 158       *
 159       * @param   string  $name     Name of a variable
 160       * @param   mixed   $default  Default value of a variable if not set
 161       *
 162       * @return  mixed  Value of a variable
 163       *
 164       * @since   2.0.0
 165       */
 166  	public function get(string $name, $default)
 167      {
 168          if (!$this->isStarted())
 169          {
 170              $this->start();
 171          }
 172  
 173          if (isset($_SESSION[$name]))
 174          {
 175              return $_SESSION[$name];
 176          }
 177  
 178          return $default;
 179      }
 180  
 181      /**
 182       * Gets the save handler instance
 183       *
 184       * @return  \SessionHandlerInterface|null
 185       *
 186       * @since   2.0.0
 187       */
 188  	public function getHandler(): ?\SessionHandlerInterface
 189      {
 190          return $this->handler;
 191      }
 192  
 193      /**
 194       * Get the session ID
 195       *
 196       * @return  string  The session ID
 197       *
 198       * @since   2.0.0
 199       */
 200  	public function getId(): string
 201      {
 202          return session_id();
 203      }
 204  
 205      /**
 206       * Get the session name
 207       *
 208       * @return  string  The session name
 209       *
 210       * @since   2.0.0
 211       */
 212  	public function getName(): string
 213      {
 214          return session_name();
 215      }
 216  
 217      /**
 218       * Check whether data exists in the session store
 219       *
 220       * @param   string  $name  Name of variable
 221       *
 222       * @return  boolean
 223       *
 224       * @since   2.0.0
 225       */
 226  	public function has(string $name): bool
 227      {
 228          if (!$this->isStarted())
 229          {
 230              $this->start();
 231          }
 232  
 233          return isset($_SESSION[$name]);
 234      }
 235  
 236      /**
 237       * Check if the session is active
 238       *
 239       * @return  boolean
 240       *
 241       * @since   2.0.0
 242       */
 243  	public function isActive(): bool
 244      {
 245          return $this->active = session_status() === \PHP_SESSION_ACTIVE;
 246      }
 247  
 248      /**
 249       * Check if the session is started
 250       *
 251       * @return  boolean
 252       *
 253       * @since   2.0.0
 254       */
 255  	public function isStarted(): bool
 256      {
 257          return $this->started;
 258      }
 259  
 260      /**
 261       * Unset a variable from the session store
 262       *
 263       * @param   string  $name  Name of variable
 264       *
 265       * @return  mixed  The value from session or NULL if not set
 266       *
 267       * @since   2.0.0
 268       */
 269  	public function remove(string $name)
 270      {
 271          if (!$this->isStarted())
 272          {
 273              $this->start();
 274          }
 275  
 276          $old = $_SESSION[$name] ?? null;
 277  
 278          unset($_SESSION[$name]);
 279  
 280          return $old;
 281      }
 282  
 283      /**
 284       * Regenerates the session ID that represents this storage.
 285       *
 286       * This method must invoke session_regenerate_id($destroy) unless this interface is used for a storage object designed for unit
 287       * or functional testing where a real PHP session would interfere with testing.
 288       *
 289       * @param   boolean  $destroy  Destroy session when regenerating?
 290       *
 291       * @return  boolean  True on success
 292       *
 293       * @see     session_regenerate_id()
 294       * @since   2.0.0
 295       */
 296  	public function regenerate(bool $destroy = false): bool
 297      {
 298          if (headers_sent() || !$this->isActive())
 299          {
 300              return false;
 301          }
 302  
 303          return session_regenerate_id($destroy);
 304      }
 305  
 306      /**
 307       * Set data into the session store
 308       *
 309       * @param   string  $name   Name of a variable
 310       * @param   mixed   $value  Value of a variable
 311       *
 312       * @return  mixed  Old value of a variable
 313       *
 314       * @since   2.0.0
 315       */
 316  	public function set(string $name, $value = null)
 317      {
 318          if (!$this->isStarted())
 319          {
 320              $this->start();
 321          }
 322  
 323          $old = $_SESSION[$name] ?? null;
 324  
 325          $_SESSION[$name] = $value;
 326  
 327          return $old;
 328      }
 329  
 330      /**
 331       * Registers session save handler as a PHP session handler
 332       *
 333       * @param   \SessionHandlerInterface  $handler  The save handler to use
 334       *
 335       * @return  $this
 336       *
 337       * @since   2.0.0
 338       * @throws  \RuntimeException
 339       */
 340  	public function setHandler(?\SessionHandlerInterface $handler = null): self
 341      {
 342          // If the handler is an instance of our HandlerInterface, check whether it is supported
 343          if ($handler instanceof HandlerInterface)
 344          {
 345              if (!$handler::isSupported())
 346              {
 347                  throw new \RuntimeException(
 348                      sprintf(
 349                          'The "%s" handler is not supported in this environment.',
 350                          \get_class($handler)
 351                      )
 352                  );
 353              }
 354          }
 355  
 356          $this->handler = $handler;
 357  
 358          if (!headers_sent() && !$this->isActive())
 359          {
 360              session_set_save_handler($this->handler, false);
 361          }
 362  
 363          return $this;
 364      }
 365  
 366      /**
 367       * Set the session ID
 368       *
 369       * @param   string  $id  The session ID
 370       *
 371       * @return  $this
 372       *
 373       * @since   2.0.0
 374       * @throws  \LogicException
 375       */
 376  	public function setId(string $id)
 377      {
 378          if ($this->isActive())
 379          {
 380              throw new \LogicException('Cannot change the ID of an active session');
 381          }
 382  
 383          session_id($id);
 384  
 385          return $this;
 386      }
 387  
 388      /**
 389       * Set the session name
 390       *
 391       * @param   string  $name  The session name
 392       *
 393       * @return  $this
 394       *
 395       * @since   2.0.0
 396       * @throws  \LogicException
 397       */
 398  	public function setName(string $name)
 399      {
 400          if ($this->isActive())
 401          {
 402              throw new \LogicException('Cannot change the name of an active session');
 403          }
 404  
 405          session_name($name);
 406  
 407          return $this;
 408      }
 409  
 410      /**
 411       * Sets session.* ini variables.
 412       *
 413       * For convenience we omit 'session.' from the beginning of the keys.
 414       * Explicitly ignores other ini keys.
 415       *
 416       * @param   array  $options  Session ini directives array(key => value).
 417       *
 418       * @return  $this
 419       *
 420       * @note    Based on \Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage::setOptions()
 421       * @see     http://php.net/session.configuration
 422       * @since   2.0.0
 423       */
 424  	public function setOptions(array $options): self
 425      {
 426          if (headers_sent() || $this->isActive())
 427          {
 428              return $this;
 429          }
 430  
 431          $validOptions = array_flip(
 432              [
 433                  'cache_limiter', 'cache_expire', 'cookie_domain', 'cookie_httponly', 'cookie_lifetime', 'cookie_path', 'cookie_secure', 'gc_divisor',
 434                  'gc_maxlifetime', 'gc_probability', 'lazy_write', 'name', 'referer_check', 'serialize_handler', 'use_strict_mode', 'use_cookies',
 435                  'use_only_cookies', 'use_trans_sid', 'upload_progress.enabled', 'upload_progress.cleanup', 'upload_progress.prefix',
 436                  'upload_progress.name', 'upload_progress.freq', 'upload_progress.min-freq', 'url_rewriter.tags', 'sid_length',
 437                  'sid_bits_per_character', 'trans_sid_hosts', 'trans_sid_tags',
 438              ]
 439          );
 440  
 441          foreach ($options as $key => $value)
 442          {
 443              if (isset($validOptions[$key]))
 444              {
 445                  ini_set('session.' . $key, $value);
 446              }
 447          }
 448  
 449          return $this;
 450      }
 451  
 452      /**
 453       * Start a session
 454       *
 455       * @return  void
 456       *
 457       * @since   2.0.0
 458       */
 459  	public function start(): void
 460      {
 461          if ($this->isStarted())
 462          {
 463              return;
 464          }
 465  
 466          if ($this->isActive())
 467          {
 468              throw new \RuntimeException('Failed to start the session: already started by PHP.');
 469          }
 470  
 471          if (ini_get('session.use_cookies') && headers_sent($file, $line))
 472          {
 473              throw new \RuntimeException(
 474                  sprintf('Failed to start the session because headers have already been sent by "%s" at line %d.', $file, $line)
 475              );
 476          }
 477  
 478          if (!session_start())
 479          {
 480              throw new \RuntimeException('Failed to start the session');
 481          }
 482  
 483          $this->isActive();
 484          $this->closed  = false;
 485          $this->started = true;
 486      }
 487  }


Generated: Wed Sep 7 05:41:13 2022 Chilli.vc Blog - For Webmaster,Blog-Writer,System Admin and Domainer