Files
oc-server3/htdocs/lib/OpencachingDE/Util/Cookie.php

220 lines
6.5 KiB
PHP

<?php
/****************************************************************************
_ _ _
___ _ __ ___ _ _ __ __ _ __| |_ (_)_ _ __ _ __| |___
/ _ \ '_ \/ -_) ' \/ _/ _` / _| ' \| | ' \/ _` |_/ _` / -_)
\___/ .__/\___|_||_\__\__,_\__|_||_|_|_||_\__, (_)__,_\___|
|_| |___/
For license information see doc/license.txt --- Unicode Reminder メモ
****************************************************************************/
namespace OpencachingDE\Util;
use OpencachingDE\Logging\LoggingInterface;
class Cookie
{
private $logger;
private $changed = false;
private $values = array();
private $session_initalized = false;
public function __construct(LoggingInterface $logger)
{
$this->logger = $logger;
global $opt;
if ($opt['session']['mode'] == SAVE_SESSION) {
if (isset($_REQUEST['SESSION']) && $_REQUEST['SESSION'] != '') {
$this->initSession();
}
} else {
if (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();
}
}
}
}
private function initSession()
{
if ($this->session_initalized == true) {
return;
}
global $opt;
session_name('SESSION');
session_set_cookie_params($opt['session']['expire']['cookie'], $opt['session']['path'], $opt['session']['domain']);
session_start();
if ($opt['session']['check_referer']
&& isset($_SERVER['REFERER'])
&& strtolower(substr($_SERVER['REFERER'], 0, strlen($opt['page']['absolute_url']))) != strtolower($opt['page']['absolute_url'])
) {
$this->createNewSession();
}
if (isset($_REQUEST['SESSION']) && count($_SESSION) > 0) {
// comapre and set timestamp
if (isset($_SESSION['lastcall']) && (abs(time() - $_SESSION['lastcall']) > $opt['session']['expire']['url'])) {
$this->createNewSession();
}
$_SESSION['lastcall'] = time();
}
$this->session_initalized = 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;
}
}
private function setSession($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->initSession();
$_SESSION[$name] = $value;
$this->changed = true;
}
}
}
private function setCookie($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 set($name, $value, $default = null)
{
global $opt;
if ($opt['session']['mode'] == SAVE_SESSION) {
$this->setSession($name, $value, $default);
} else {
$this->setCookie($name, $value, $default);
}
}
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;
}
}
function is_set($name)
{
global $opt;
if ($opt['session']['mode'] == SAVE_SESSION) {
return isset($_SESSION[$name]);
} else {
return isset($this->values[$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 autmatically sent
} else {
if ($this->changed == true) {
if (count($this->values) == 0) {
$this->logger->notice('Cookie data empty.');
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);
}
}
}
}
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 ($opt['session']['mode'] == SAVE_SESSION) {
if ($this->session_initalized == true) {
if (count($_SESSION) == 0) {
@session_destroy();
} else {
session_write_close();
}
}
}
}
}