2014-01-23 14:40:59 +01:00
|
|
|
<?php
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
|
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
|
|
|
{
|
2014-04-04 11:13:07 +02:00
|
|
|
/**
|
|
|
|
* The session this namespace is associated to
|
|
|
|
*
|
|
|
|
* @var Session
|
|
|
|
*/
|
|
|
|
protected $session;
|
|
|
|
|
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-04-04 11:13:07 +02:00
|
|
|
/**
|
|
|
|
* Create a new session namespace
|
|
|
|
*
|
|
|
|
* @param Session $session The session this namespace is associated to
|
|
|
|
*/
|
|
|
|
public function __construct(Session $session = null)
|
|
|
|
{
|
|
|
|
$this->session = $session;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->values[$key] = $value;
|
2014-04-04 11:10:45 +02:00
|
|
|
|
|
|
|
if (in_array($key, $this->removed)) {
|
|
|
|
unset($this->removed[array_search($key, $this->values)]);
|
|
|
|
}
|
|
|
|
|
2014-01-23 14:40:59 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2014-09-17 09:45:07 +02:00
|
|
|
public function setByRef($key, &$value)
|
|
|
|
{
|
|
|
|
$this->values[$key] = $value;
|
|
|
|
|
|
|
|
if (in_array($key, $this->removed)) {
|
|
|
|
unset($this->removed[array_search($key, $this->removed)]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* Save the session this namespace is associated to
|
|
|
|
*/
|
|
|
|
public function write()
|
|
|
|
{
|
|
|
|
if (!$this->session) {
|
2014-08-27 16:03:15 +02:00
|
|
|
throw new IcingaException('Cannot save, session not set');
|
2014-04-04 11:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->session->write();
|
|
|
|
}
|
2014-01-23 14:40:59 +01:00
|
|
|
}
|