2014-01-23 14:40:59 +01:00
|
|
|
<?php
|
2016-02-08 15:41:00 +01:00
|
|
|
/* Icinga Web 2 | (c) 2014 Icinga Development Team | GPLv2+ */
|
2014-01-23 14:40:59 +01:00
|
|
|
|
2014-03-25 11:12:55 +01:00
|
|
|
namespace Icinga\Web\Session;
|
2014-01-23 14:40:59 +01:00
|
|
|
|
2014-08-27 15:51:49 +02:00
|
|
|
use Exception;
|
|
|
|
use ArrayIterator;
|
2014-08-27 16:03:15 +02:00
|
|
|
use Icinga\Exception\IcingaException;
|
2014-08-27 15:51:49 +02:00
|
|
|
use IteratorAggregate;
|
2014-01-24 09:42:32 +01:00
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
/**
|
|
|
|
* Container for session values
|
|
|
|
*/
|
2014-03-25 10:31:52 +01:00
|
|
|
class SessionNamespace implements IteratorAggregate
|
2014-01-23 14:40:59 +01:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* The actual values stored in this container
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $values = array();
|
|
|
|
|
2014-04-04 11:10:45 +02:00
|
|
|
/**
|
|
|
|
* The names of all values removed from this container
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $removed = array();
|
|
|
|
|
2014-03-25 10:31:52 +01:00
|
|
|
/**
|
|
|
|
* Return an iterator for all values in this namespace
|
|
|
|
*
|
|
|
|
* @return ArrayIterator
|
|
|
|
*/
|
|
|
|
public function getIterator()
|
|
|
|
{
|
2014-04-04 11:10:45 +02:00
|
|
|
return new ArrayIterator($this->getAll());
|
2014-03-25 10:31:52 +01:00
|
|
|
}
|
|
|
|
|
2014-01-24 09:42:32 +01:00
|
|
|
/**
|
|
|
|
* Set a session value by property access
|
|
|
|
*
|
|
|
|
* @param string $key The value's name
|
|
|
|
* @param mixed $value The value
|
|
|
|
*/
|
2014-02-21 14:07:32 +01:00
|
|
|
public function __set($key, $value)
|
|
|
|
{
|
2014-01-24 09:42:32 +01:00
|
|
|
$this->set($key, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a session value by property access
|
|
|
|
*
|
|
|
|
* @param string $key The value's name
|
|
|
|
*
|
|
|
|
* @return mixed The value
|
|
|
|
* @throws Exception When the given value-name is not found
|
|
|
|
*/
|
2014-02-21 14:07:32 +01:00
|
|
|
public function __get($key)
|
|
|
|
{
|
2014-01-24 09:42:32 +01:00
|
|
|
if (!array_key_exists($key, $this->values)) {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException(
|
|
|
|
'Cannot access non-existent session value "%s"',
|
|
|
|
$key
|
|
|
|
);
|
2014-01-24 09:42:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this->get($key);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return whether the given session value is set
|
|
|
|
*
|
|
|
|
* @param string $key The value's name
|
|
|
|
* @return bool
|
|
|
|
*/
|
2014-02-21 14:07:32 +01:00
|
|
|
public function __isset($key)
|
|
|
|
{
|
2014-01-24 09:42:32 +01:00
|
|
|
return isset($this->values[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Unset the given session value
|
|
|
|
*
|
|
|
|
* @param string $key The value's name
|
|
|
|
*/
|
2014-02-21 14:07:32 +01:00
|
|
|
public function __unset($key)
|
|
|
|
{
|
2014-04-09 16:18:51 +02:00
|
|
|
$this->delete($key);
|
2014-01-24 09:42:32 +01:00
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
/**
|
|
|
|
* Setter for session values
|
|
|
|
*
|
|
|
|
* @param string $key Name of value
|
|
|
|
* @param mixed $value Value to set
|
|
|
|
*
|
2015-04-07 14:23:26 +02:00
|
|
|
* @return $this
|
2014-01-23 14:40:59 +01:00
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->values[$key] = $value;
|
2014-04-04 11:10:45 +02:00
|
|
|
|
2015-07-31 11:04:51 +02:00
|
|
|
if (in_array($key, $this->removed, true)) {
|
|
|
|
unset($this->removed[array_search($key, $this->removed, true)]);
|
2014-04-04 11:10:45 +02:00
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:45:07 +02:00
|
|
|
public function setByRef($key, &$value)
|
|
|
|
{
|
2014-09-17 11:05:35 +02:00
|
|
|
$this->values[$key] = & $value;
|
2014-09-17 09:45:07 +02:00
|
|
|
|
2015-07-31 11:04:51 +02:00
|
|
|
if (in_array($key, $this->removed, true)) {
|
|
|
|
unset($this->removed[array_search($key, $this->removed, true)]);
|
2014-09-17 09:45:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
/**
|
|
|
|
* Getter for session values
|
|
|
|
*
|
|
|
|
* @param string $key Name of the value to return
|
|
|
|
* @param mixed $default Default value to return
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function get($key, $default = null)
|
|
|
|
{
|
|
|
|
return isset($this->values[$key]) ? $this->values[$key] : $default;
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:45:07 +02:00
|
|
|
public function & getByRef($key, $default = null)
|
|
|
|
{
|
|
|
|
$value = $default;
|
|
|
|
if (isset($this->values[$key])) {
|
|
|
|
$value = & $this->values[$key];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2014-04-09 16:18:51 +02:00
|
|
|
/**
|
|
|
|
* Delete the given value from the session
|
|
|
|
*
|
|
|
|
* @param string $key The value's name
|
|
|
|
*/
|
|
|
|
public function delete($key)
|
|
|
|
{
|
|
|
|
$this->removed[] = $key;
|
|
|
|
unset($this->values[$key]);
|
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
/**
|
|
|
|
* Getter for all session values
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return $this->values;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Put an array into the session
|
|
|
|
*
|
|
|
|
* @param array $values Values to set
|
|
|
|
* @param bool $overwrite Overwrite existing values
|
|
|
|
*/
|
|
|
|
public function setAll(array $values, $overwrite = false)
|
|
|
|
{
|
|
|
|
foreach ($values as $key => $value) {
|
2014-04-09 16:18:51 +02:00
|
|
|
if ($this->get($key, $value) !== $value && !$overwrite) {
|
2014-01-23 14:40:59 +01:00
|
|
|
continue;
|
|
|
|
}
|
2014-04-04 11:10:45 +02:00
|
|
|
$this->set($key, $value);
|
2014-01-23 14:40:59 +01:00
|
|
|
}
|
|
|
|
}
|
2014-04-04 11:13:07 +02:00
|
|
|
|
2014-09-17 10:42:56 +02:00
|
|
|
/**
|
|
|
|
* Return whether the session namespace has been changed
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasChanged()
|
|
|
|
{
|
|
|
|
return false === empty($this->values) || false === empty($this->removed);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Clear all values from the session namespace
|
|
|
|
*/
|
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->values = array();
|
|
|
|
$this->removed = array();
|
|
|
|
}
|
2014-01-23 14:40:59 +01:00
|
|
|
}
|