modularize session handling, start to be a little bit redundant with storage classes

This commit is contained in:
<bdauvergne@entrouvert.com> 1207742680 +0200 0001-01-01 00:00:00 +00:00
parent 86061ce83f
commit e858c68969
2 changed files with 82 additions and 0 deletions

View File

@ -0,0 +1,48 @@
<?php
require_once('lassospkit_datadir.inc.php');
require_once('lassospkit_debug.inc.php');
$LassoSPKitSessionFile_cookiename = LassoSPKitConfig::get('cookiename');
class LassoSPKitSessionFile {
function retrieve($session, $timeout) {
global $LassoSPKitSessionFile_cookiename;
$content = null;
if (isset($_COOKIE[$LassoSPKitSessionFile_cookiename])) {
$session->id = $_COOKIE[$LassoSPKitSessionFile_cookiename];
$valid = ereg("^[[:alnum:]]+$",$session->id);
if ($valid) {
$filepath = lassospkit_datadir() . "/cookie_session_" . $session->id;
if (file_exists($filepath) && time()-filemtime($filepath) < $timeout) {
$content = @file_get_contents($filepath);
if ($content === FALSE) {
lassospkit_errlog("cannot read $filepath");
}
} else {
self::delete();
}
}
}
if (! $content) {
$session->id = md5("lasso" . rand());
setcookie($LassoSPKitSessionFile_cookiename, $session->id, time()+3600, '/');
}
return $content;
}
function store($session, $content) {
global $LassoSPKitSessionFile_cookiename;
if ($session->id) {
$ret = @file_put_contents(lassospkit_datadir() . "/cookie_session_" . $session->id, $content);
if ($ret === FALSE) {
lassospkit_errlog("cannot write into " . lassospkit_datadir() . "/cookie_session_" . $session->id);
}
}
}
function delete($session) {
$filepath = lassospkit_datadir() . "/cookie_session_" . $session->id;
@unlink($filepath);
}
}

View File

@ -0,0 +1,34 @@
<?php
require_once('lassospkit_datadir.inc.php');
require_once('lassospkit_debug.inc.php');
$LassoSPKitSessionFile_key = "__LassoSPKitSessionObject";
class LassoSPKitSessionPHP {
function retrieve($session, $timeout) {
global $LassoSPKitSessionPHP_key;
$content = null;
if (! isset($_SESSION)) {
throw new Exception("LassoSPKit cannot work without PHP sessions if use_session is TRUE.");
}
if (isset($_SESSION[$LassoSPKitSessionFile_key])) {
$content = $_SESSION[$LassoSPKitSessionFile_key];
if (! isset($_SESSION[$LassoSPKitSessionFile_key . '_time']) ||
$_SESSION[$LassoSPKitSessionFile_key . '_time'] - time() > $timeout) {
$content = null;
self::delete();
}
}
return $content;
}
function store($session, $content) {
global $LassoSPKitSessionPHP_key;
$_SESSION[$LassoSPKitSessionFile_key] = $content;
$_SESSION[$LassoSPKitSessionFile_key . '_time'] = time();
}
function delete($session) {
unset($_SESSION[$LassoSPKitSessionFile_key]);
unset($_SESSION[$LassoSPKitSessionFile_key . '_time']);
}
}