2014-01-23 12:09:48 +01:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-01-23 12:09:48 +01:00
|
|
|
|
2014-03-25 11:12:55 +01:00
|
|
|
namespace Icinga\Web\Session;
|
2014-01-23 14:40:59 +01:00
|
|
|
|
2014-04-04 11:13:07 +02:00
|
|
|
use Icinga\Exception\NotImplementedError;
|
|
|
|
|
2014-01-23 12:09:48 +01:00
|
|
|
/**
|
|
|
|
* Base class for handling sessions
|
|
|
|
*/
|
2014-01-23 14:40:59 +01:00
|
|
|
abstract class Session extends SessionNamespace
|
2014-01-23 12:09:48 +01:00
|
|
|
{
|
|
|
|
/**
|
2014-01-23 14:40:59 +01:00
|
|
|
* Container for session namespaces
|
2014-01-23 12:09:48 +01:00
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2014-01-23 14:40:59 +01:00
|
|
|
protected $namespaces = array();
|
2014-01-23 12:09:48 +01:00
|
|
|
|
2014-04-04 11:10:45 +02:00
|
|
|
/**
|
|
|
|
* The identifiers of all namespaces removed from this session
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $removedNamespaces = array();
|
|
|
|
|
2014-01-23 12:09:48 +01:00
|
|
|
/**
|
|
|
|
* Read all values from the underlying session implementation
|
|
|
|
*/
|
|
|
|
abstract public function read();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists changes to the underlying session implementation
|
|
|
|
*/
|
2017-01-27 14:48:59 +01:00
|
|
|
public function write()
|
|
|
|
{
|
2014-04-04 11:13:07 +02:00
|
|
|
throw new NotImplementedError('You are required to implement write() in your session implementation');
|
|
|
|
}
|
2014-01-23 12:09:48 +01:00
|
|
|
|
2014-07-16 09:33:49 +02:00
|
|
|
/**
|
|
|
|
* Return whether a session exists
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
abstract public function exists();
|
|
|
|
|
2014-01-23 12:09:48 +01:00
|
|
|
/**
|
|
|
|
* Purge session
|
|
|
|
*/
|
|
|
|
abstract public function purge();
|
|
|
|
|
2014-02-26 19:23:34 +01:00
|
|
|
/**
|
|
|
|
* Assign a new session id to this session.
|
|
|
|
*/
|
|
|
|
abstract public function refreshId();
|
|
|
|
|
2014-07-16 09:54:58 +02:00
|
|
|
/**
|
|
|
|
* Return the id of this session
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
abstract public function getId();
|
|
|
|
|
2014-01-23 12:09:48 +01:00
|
|
|
/**
|
2014-01-23 14:40:59 +01:00
|
|
|
* Get or create a new session namespace
|
2014-01-23 12:09:48 +01:00
|
|
|
*
|
2014-01-23 14:40:59 +01:00
|
|
|
* @param string $identifier The namespace's identifier
|
2014-01-23 12:09:48 +01:00
|
|
|
*
|
2014-01-23 14:40:59 +01:00
|
|
|
* @return SessionNamespace
|
2014-01-23 12:09:48 +01:00
|
|
|
*/
|
2014-01-23 14:40:59 +01:00
|
|
|
public function getNamespace($identifier)
|
2014-01-23 12:09:48 +01:00
|
|
|
{
|
2014-01-23 14:40:59 +01:00
|
|
|
if (!isset($this->namespaces[$identifier])) {
|
2015-07-31 11:04:43 +02:00
|
|
|
if (in_array($identifier, $this->removedNamespaces, true)) {
|
|
|
|
unset($this->removedNamespaces[array_search($identifier, $this->removedNamespaces, true)]);
|
2014-04-04 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2014-09-17 10:43:52 +02:00
|
|
|
$this->namespaces[$identifier] = new SessionNamespace();
|
2014-01-23 12:09:48 +01:00
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
return $this->namespaces[$identifier];
|
2014-01-23 12:09:48 +01:00
|
|
|
}
|
|
|
|
|
2014-03-25 10:31:52 +01:00
|
|
|
/**
|
|
|
|
* Return whether the given session namespace exists
|
|
|
|
*
|
|
|
|
* @param string $identifier The namespace's identifier to check
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasNamespace($identifier)
|
|
|
|
{
|
|
|
|
return isset($this->namespaces[$identifier]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove the given session namespace
|
|
|
|
*
|
|
|
|
* @param string $identifier The identifier of the namespace to remove
|
|
|
|
*/
|
|
|
|
public function removeNamespace($identifier)
|
|
|
|
{
|
|
|
|
unset($this->namespaces[$identifier]);
|
2014-04-04 11:10:45 +02:00
|
|
|
$this->removedNamespaces[] = $identifier;
|
2014-03-25 10:31:52 +01:00
|
|
|
}
|
|
|
|
|
2014-09-17 10:42:56 +02:00
|
|
|
/**
|
|
|
|
* Return whether the session has changed
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasChanged()
|
|
|
|
{
|
|
|
|
return parent::hasChanged() || false === empty($this->namespaces) || false === empty($this->removedNamespaces);
|
|
|
|
}
|
|
|
|
|
2014-01-23 12:09:48 +01:00
|
|
|
/**
|
2014-01-23 14:40:59 +01:00
|
|
|
* Clear all values and namespaces from the session cache
|
2014-01-23 12:09:48 +01:00
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
2014-09-17 10:42:56 +02:00
|
|
|
parent::clear();
|
2014-01-23 14:40:59 +01:00
|
|
|
$this->namespaces = array();
|
2014-04-04 11:10:45 +02:00
|
|
|
$this->removedNamespaces = array();
|
2014-01-23 12:09:48 +01:00
|
|
|
}
|
|
|
|
}
|