Skip to content

Commit

Permalink
refactor lib2 cookie class
Browse files Browse the repository at this point in the history
  • Loading branch information
Rotzbua authored and teiling88 committed Sep 20, 2016
1 parent e0f96ee commit 092eaa3
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 258 deletions.
111 changes: 111 additions & 0 deletions htdocs/lib2/SessionDataCookie.class.php
@@ -0,0 +1,111 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
*
* Session data handling with cookies
* See doc/cookies.txt for more information in cookies.
***************************************************************************/
require_once 'SessionDataInterface.class.php';

class SessionDataCookie implements SessionDataInterface
{
public $changed = false;
public $values = array();

public function __construct()
{
global $opt;

if (isset($_COOKIE[$opt['session']['cookiename'] . 'data'])) {
//get the cookievars-array
$decoded = base64_decode($_COOKIE[$opt['session']['cookiename'] . 'data']);

if ($decoded !== false) {
// TODO replace by safe function
$this->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
}
}
27 changes: 27 additions & 0 deletions htdocs/lib2/SessionDataInterface.class.php
@@ -0,0 +1,27 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
*
* Interface for session data handling
***************************************************************************/

interface SessionDataInterface
{
public function __construct();

public function set($name, $value, $default = null);

public function get($name, $default = null);

public function is_set($name);

public function un_set($name);

public function header();

public function debug();

public function close();
}
135 changes: 135 additions & 0 deletions htdocs/lib2/SessionDataNative.class.php
@@ -0,0 +1,135 @@
<?php
/***************************************************************************
* For license information see doc/license.txt
*
* Unicode Reminder メモ
*
* Session data handling with build-in php session
***************************************************************************/
require_once 'SessionDataInterface.class.php';

/**
* Class SessionDataNative
* Not for productive usage!! Implementation not finished yet
*/
class SessionDataNative implements SessionDataInterface
{
public $changed = false;
public $values = array();
public $session_initialized = false;

public function __construct()
{
if (isset($_REQUEST['SESSION']) && $_REQUEST['SESSION'] != '') {
$this->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();
}
}
}
}

0 comments on commit 092eaa3

Please sign in to comment.