From 9206b3025442d8cce59e34b91fa2f45309622475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jannis=20Mo=C3=9Fhammer?= Date: Fri, 7 Jun 2013 17:28:06 +0200 Subject: [PATCH] Add basic, untested skeleton for Session and PHPSession As we decided to remove Zend_Session due to locking issues, we have to implement our session handling here. This is the basic, untested code which will be tested in the next step refs #4265 --- library/Icinga/Authentication/PHPSession.php | 118 +++++++++++++++++++ library/Icinga/Authentication/Session.php | 49 ++++++++ 2 files changed, 167 insertions(+) create mode 100644 library/Icinga/Authentication/PHPSession.php create mode 100644 library/Icinga/Authentication/Session.php diff --git a/library/Icinga/Authentication/PHPSession.php b/library/Icinga/Authentication/PHPSession.php new file mode 100644 index 000000000..230971092 --- /dev/null +++ b/library/Icinga/Authentication/PHPSession.php @@ -0,0 +1,118 @@ + false, + 'use_cookies' => true, + 'use_only_cooies' => true, + 'cookie_httponly' => true, + 'use_only_cookies' => true, + 'hash_function' => true, + 'hash_bits_per_character' => 5, + ); + + + public function __construct(array $options = null) + { + if ($options !== null) { + $options = array_merge(PhpSession::DEFAULT_COOKIEOPTIONS, $options); + } + foreach ($options as $sessionVar => $value) { + if (ini_set("session.".$sessionVar, $value) === false) { + Logger::warn("Could not set php.ini settint %s = %s", $sessionVar, $value); + } + } + } + + private function sessionCanBeChanged() + { + if ($this->isFlushed) { + Logger::error("Tried to work on a closed session, session changes will be ignored"); + return false; + } + return true; + } + + private function sessionCanBeOpened() + { + if ($this->isOpen) { + Logger::warn("Tried to open a session more than once"); + return false; + } + return $this->sessionCanBeChanged(); + } + + public function open() + { + if (!$this->sessionCanBeOpened()) { + return false; + } + + session_name(PhpSession::SESSION_NAME); + session_start(); + $this->isOpen = true; + $this->setAll($_SESSION); + return true; + } + + private function ensureOpen() + { + // try to open first + if (!$this->isOpen) { + if (!$this->open()) { + return false; + } + } + return true; + } + + public function read($keepOpen = false) + { + if (!$this->ensureOpen) { + return false; + } + if ($keepOpen) { + return true; + } + $this->close(); + return true; + } + + public function write($keepOpen = false) + { + if (!$this->ensureOpen) { + return false; + } + foreach ($this->getAll() as $key => $value) { + $_SESSION[$key] = $value; + } + if ($keepOpen) { + return; + } + $this->close(); + } + + public function close() + { + if (!$this->isFlushed) { + session_write_close(); + } + $this->isFlushed = true; + } + + public function purge() + { + if (!$this->ensureOpen() && !$this->isFlushed) { + session_destroy(); + } + } +} diff --git a/library/Icinga/Authentication/Session.php b/library/Icinga/Authentication/Session.php new file mode 100644 index 000000000..3e0ee0026 --- /dev/null +++ b/library/Icinga/Authentication/Session.php @@ -0,0 +1,49 @@ +sessionValues[$key] = $value; + } + + public function get($key, $defaultValue = null) + { + return isset($this->sessionValues[$key]) ? + $this->sessionValues[$key] : $defaultValue; + } + + public function getAll() + { + return $this->sessionValues; + } + + 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; + } + } + + public function clear() + { + $this->sessionValues = array(); + } +}