From 092eaa3ebe2b58b031dc81b676c53e4c7c584192 Mon Sep 17 00:00:00 2001 From: Rotzbua Date: Mon, 1 Aug 2016 23:54:49 +0200 Subject: [PATCH] refactor lib2 cookie class --- htdocs/lib2/SessionDataCookie.class.php | 111 +++++++++ htdocs/lib2/SessionDataInterface.class.php | 27 +++ htdocs/lib2/SessionDataNative.class.php | 135 +++++++++++ htdocs/lib2/cookie.class.php | 265 +-------------------- 4 files changed, 280 insertions(+), 258 deletions(-) create mode 100644 htdocs/lib2/SessionDataCookie.class.php create mode 100644 htdocs/lib2/SessionDataInterface.class.php create mode 100644 htdocs/lib2/SessionDataNative.class.php diff --git a/htdocs/lib2/SessionDataCookie.class.php b/htdocs/lib2/SessionDataCookie.class.php new file mode 100644 index 000000000..5209790ea --- /dev/null +++ b/htdocs/lib2/SessionDataCookie.class.php @@ -0,0 +1,111 @@ +values = @unserialize($decoded); + if (!is_array($this->values)) { + $this->values = array(); + } + } else { + $this->values = array(); + } + } + } + + public function set($name, $value, $default = null) + { + // Store cookie value in internal array. OcSmarty will call this->header() + // to actually set the cookie. + if (!isset($this->values[$name]) || $this->values[$name] != $value) { + if ($value == $default) { + if (isset($this->values[$name])) { + unset($this->values[$name]); + $this->changed = true; + } + } else { + $this->values[$name] = $value; + $this->changed = true; + } + } + } + + public function get($name, $default = null) + { + return isset($this->values[$name]) ? $this->values[$name] : $default; + } + + public function is_set($name) + { + return isset($this->values[$name]); + } + + public function un_set($name) + { + if (isset($this->values[$name])) { + unset($this->values[$name]); + $this->changed = true; + } + } + + public function header() + { + global $opt; + + if ($this->changed === true) { + if (count($this->values) === 0) { + setcookie( + $opt['session']['cookiename'] . 'data', + false, + time() + 31536000, + $opt['session']['path'], + $opt['session']['domain'], + 0 + ); + } else { + setcookie( + $opt['session']['cookiename'] . 'data', + // TODO replace by safe function + base64_encode(serialize($this->values)), + time() + 31536000, + $opt['session']['path'], + $opt['session']['domain'], + 0 + ); + } + } + } + + public function debug() + { + print_r($this->values); + exit; + } + + public function close() + { + // TODO really nothing? + // maybe destroy cookies here + } +} diff --git a/htdocs/lib2/SessionDataInterface.class.php b/htdocs/lib2/SessionDataInterface.class.php new file mode 100644 index 000000000..44f3c9796 --- /dev/null +++ b/htdocs/lib2/SessionDataInterface.class.php @@ -0,0 +1,27 @@ +init_session(); + } + } + + private function init_session() + { + global $opt; + + if ($this->session_initialized !== true) { + session_name('SESSION'); + session_set_cookie_params($opt['session']['expire']['cookie'], $opt['session']['path'], + $opt['session']['domain']); + session_start(); + + if ($opt['session']['check_referer']) { + if (isset($_SERVER['REFERER'])) { + // TODO fix the following if statement, seems corrupted + if (strtolower(substr('http' + strstr($_SERVER['REFERER'], '://'), 0, + strlen($opt['page']['absolute_http_url']))) != strtolower($opt['page']['absolute_http_url']) + ) { + $this->createNewSession(); + } + } + } + + if ((isset($_GET['SESSION']) || isset($_POST['SESSION'])) && count($_SESSION) > 0) { + // compare and set timestamp + if (isset($_SESSION['lastcall'])) { + if (abs(time() - $_SESSION['lastcall']) > $opt['session']['expire']['url']) { + $this->createNewSession(); + } + } + + $_SESSION['lastcall'] = time(); + } + + $this->session_initialized = true; + } + } + + private function createNewSession() + { + session_regenerate_id(); + $locale = isset($_SESSION['locale']) ? $_SESSION['locale'] : ''; + foreach ($_SESSION as $k => $v) { + unset($_SESSION[$k]); + } + if ($locale != '') { + $_SESSION['locale'] = $locale; + } + } + + public function set($name, $value, $default = null) + { + if (!isset($_SESSION[$name]) || $_SESSION[$name] != $value) { + if ($value == $default) { + if (isset($_SESSION[$name])) { + unset($_SESSION[$name]); + $this->changed = true; + } + } else { + $this->init_session(); + $_SESSION[$name] = $value; + $this->changed = true; + } + } + } + + public function get($name, $default = null) + { + return isset($_SESSION[$name]) ? $_SESSION[$name] : $default; + } + + public function is_set($name) + { + return isset($_SESSION[$name]); + } + + public function un_set($name) + { + if (isset($_SESSION[$name])) { + unset($_SESSION[$name]); + $this->changed = true; + } + } + + public function header() + { + // is automatically sent + } + + public function debug() + { + print_r($_SESSION); + exit; + } + + public function close() + { + if ($this->session_initialized === true) { + if (count($_SESSION) === 0) { + try { + session_destroy(); + } catch (Exception $e) { + // @todo implement logging + } + } else { + session_write_close(); + } + } + } +} diff --git a/htdocs/lib2/cookie.class.php b/htdocs/lib2/cookie.class.php index 3b9e6cc76..4349311c9 100644 --- a/htdocs/lib2/cookie.class.php +++ b/htdocs/lib2/cookie.class.php @@ -8,264 +8,13 @@ * See doc/cookies.txt for more information in cookies. ***************************************************************************/ -$cookie = new cookie(); +require_once 'SessionDataCookie.class.php'; +require_once 'SessionDataNative.class.php'; -/** - * Class cookie - */ -class cookie -{ - /** - * @var bool - */ - public $changed = false; - /** - * @var array - */ - public $values = array(); - /** - * @var bool - */ - public $session_initialized = false; +global $opt; - /** - * cookie constructor. - */ - public function __construct() - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - if (isset($_REQUEST['SESSION']) && $_REQUEST['SESSION'] != '') { - $this->init_session(); - } - } elseif (isset($_COOKIE[$opt['session']['cookiename'] . 'data'])) { - //get the cookievars-array - $decoded = base64_decode($_COOKIE[$opt['session']['cookiename'] . 'data']); - - if ($decoded !== false) { - $this->values = @unserialize($decoded); - if (!is_array($this->values)) { - $this->values = array(); - } - } else { - $this->values = array(); - } - } - } - - /** - * - */ - public function init_session() - { - global $opt; - - if ($this->session_initialized !== true) { - session_name('SESSION'); - session_set_cookie_params( - $opt['session']['expire']['cookie'], - $opt['session']['path'], - $opt['session']['domain'] - ); - session_start(); - - if ($opt['session']['check_referer']) { - if (isset($_SERVER['REFERER'])) { - // TODO fix the following if statement, seems corrupted - if (strtolower(substr('http' + strstr($_SERVER['REFERER'], '://'), 0, strlen($opt['page']['absolute_http_url']))) != strtolower($opt['page']['absolute_http_url'])) { - $this->createNewSession(); - } - } - } - - if ((isset($_GET['SESSION']) || isset($_POST['SESSION'])) && count($_SESSION) > 0) { - // compare and set timestamp - if (isset($_SESSION['lastcall'])) { - if (abs(time() - $_SESSION['lastcall']) > $opt['session']['expire']['url']) { - $this->createNewSession(); - } - } - - $_SESSION['lastcall'] = time(); - } - - $this->session_initialized = true; - } - } - - /** - * - */ - public function createNewSession() - { - session_regenerate_id(); - $locale = isset($_SESSION['locale']) ? $_SESSION['locale'] : ''; - foreach ($_SESSION as $k => $v) { - unset($_SESSION[$k]); - } - if ($locale != '') { - $_SESSION['locale'] = $locale; - } - } - - /** - * @param $name - * @param $value - * @param null $default - */ - public function set($name, $value, $default = null) - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - if (!isset($_SESSION[$name]) || $_SESSION[$name] != $value) { - if ($value == $default) { - if (isset($_SESSION[$name])) { - unset($_SESSION[$name]); - $this->changed = true; - } - } else { - $this->init_session(); - $_SESSION[$name] = $value; - $this->changed = true; - } - } - } else { - // Store cookie value in internal array. OcSmarty will call this->header() - // to actually set the cookie. - - if (!isset($this->values[$name]) || $this->values[$name] != $value) { - if ($value == $default) { - if (isset($this->values[$name])) { - unset($this->values[$name]); - $this->changed = true; - } - } else { - $this->values[$name] = $value; - $this->changed = true; - } - } - } - } - - /** - * @param $name - * @param string $default - * - * @return mixed|string - */ - public function get($name, $default = '') - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - return isset($_SESSION[$name]) ? $_SESSION[$name] : $default; - } else { - return isset($this->values[$name]) ? $this->values[$name] : $default; - } - } - - /** - * @param $name - * - * @return bool - */ - public function is_set($name) - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - return isset($_SESSION[$name]); - } else { - return isset($this->values[$name]); - } - } - - /** - * @param $name - */ - public function un_set($name) - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - if (isset($_SESSION[$name])) { - unset($_SESSION[$name]); - $this->changed = true; - } - } else { - if (isset($this->values[$name])) { - unset($this->values[$name]); - $this->changed = true; - } - } - } - - /** - * - */ - public function header() - { - global $opt; - - if ($opt['session']['mode'] == SAVE_SESSION) { - // is automatically sent - } else { - if ($this->changed === true) { - if (count($this->values) === 0) { - setcookie( - $opt['session']['cookiename'] . 'data', - false, - time() + 31536000, - $opt['session']['path'], - $opt['session']['domain'], - 0 - ); - } else { - setcookie( - $opt['session']['cookiename'] . 'data', - base64_encode(serialize($this->values)), - time() + 31536000, - $opt['session']['path'], - $opt['session']['domain'], - 0 - ); - } - } - } - } - - /** - * - */ - public function debug() - { - global $opt; - if ($opt['session']['mode'] == SAVE_SESSION) { - print_r($_SESSION); - } else { - print_r($this->values); - } - exit; - } - - /** - * - */ - public function close() - { - global $opt; - if ($this->session_initialized === true && $opt['session']['mode'] == SAVE_SESSION) { - if (count($_SESSION) === 0) { - try { - session_destroy(); - } catch (Exception $e) { - // @todo implement logging - } - } else { - session_write_close(); - } - } - } +if ($opt['session']['mode'] == SAVE_SESSION) { + $cookie = new SessionDataNative(); +} else { + $cookie = new SessionDataCookie(); }