2013-06-07 17:28:06 +02:00
|
|
|
<?php
|
2013-06-10 17:03:01 +02:00
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
|
|
|
// {{{ICINGA_LICENSE_HEADER}}}
|
2013-06-07 17:28:06 +02:00
|
|
|
|
|
|
|
namespace Icinga\Authentication;
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Base class for session, providing getter, setters and required
|
|
|
|
* interface methods
|
|
|
|
*
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
abstract class Session
|
|
|
|
{
|
|
|
|
private $sessionValues = array();
|
2013-06-11 13:53:42 +02:00
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Opens a session or creates a new one if not exists
|
|
|
|
*
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
abstract public function open();
|
2013-06-27 15:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Reads all values from the underyling session implementation
|
|
|
|
*
|
|
|
|
* @param Boolean $keepOpen True to keep the session open (depends on implementaiton)
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
abstract public function read($keepOpen = false);
|
2013-06-27 15:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Persists changes to the underlying session implementation
|
|
|
|
*
|
|
|
|
* @param Boolean $keepOpen True to keep the session open (depends on implementaiton)
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
abstract public function write($keepOpen = false);
|
|
|
|
abstract public function close();
|
|
|
|
abstract public function purge();
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Sets a $value under the provided key in the internal session data array
|
|
|
|
* Does not persist those changes, use @see Session::write in order to persist the changes
|
|
|
|
* made here.
|
|
|
|
*
|
|
|
|
* @param String $key
|
|
|
|
* @param mixed $value
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
public function set($key, $value)
|
|
|
|
{
|
|
|
|
$this->sessionValues[$key] = $value;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Returns the session value stored under $key or $defaultValue if not found.
|
|
|
|
* call @see Session:read in order to populate this array with the underyling session implementation
|
|
|
|
*
|
|
|
|
* @param String $key
|
|
|
|
* @param mixed $defaultValue
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
public function get($key, $defaultValue = null)
|
|
|
|
{
|
|
|
|
return isset($this->sessionValues[$key]) ?
|
|
|
|
$this->sessionValues[$key] : $defaultValue;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Returns the current session value state (also dirty changes not yet written to the session)
|
|
|
|
*
|
|
|
|
* @return Array
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
public function getAll()
|
|
|
|
{
|
|
|
|
return $this->sessionValues;
|
|
|
|
}
|
|
|
|
|
2013-06-27 15:18:24 +02:00
|
|
|
/**
|
|
|
|
* Writes all values provided in the key=>value array to the internal session value state.
|
|
|
|
* In order to persist these chages, call @see Session:write
|
|
|
|
*
|
|
|
|
* @param Array $values
|
|
|
|
* @param Boolean $overwrite Whether to overwrite already set values
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
public function setAll(array $values, $overwrite = false)
|
|
|
|
{
|
|
|
|
if ($overwrite) {
|
|
|
|
$this->clear();
|
|
|
|
}
|
|
|
|
foreach ($values as $key => $value) {
|
|
|
|
if (isset($this->sessionValues[$key]) && !$overwrite) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
$this->sessionValues[$key] = $value;
|
|
|
|
}
|
|
|
|
}
|
2013-06-27 15:18:24 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears all values from the session cache
|
|
|
|
*
|
|
|
|
**/
|
2013-06-07 17:28:06 +02:00
|
|
|
public function clear()
|
|
|
|
{
|
|
|
|
$this->sessionValues = array();
|
|
|
|
}
|
|
|
|
}
|