add memcache session storage, fix forgottent arg to storage->delete(), a new config key session_storage_class

This commit is contained in:
<bdauvergne@entrouvert.com> 1207751790 +0200 0001-01-01 00:00:00 +00:00
parent 0a71c6645d
commit c82f81938a
5 changed files with 75 additions and 18 deletions

View File

@ -26,7 +26,8 @@ class LassoSPKitConfig {
'default_return_url' => null,
'lasso_lib' => 'lasso.php',
'showExtension' => 1, /* Shall we show the extension of scripts in public apis */
'memcache_servers' => 'localhost:11211' /* Blank separated list of host:port pairs */
'memcache_servers' => 'localhost:11211', /* Blank separated list of host:port pairs */
'session_storage_class' => 'LassoSPKitSessionFile'
);
private static $instance = null;
private static $file;

View File

@ -21,7 +21,7 @@ class LassoSPKitSessionFile {
lassospkit_errlog("cannot read $filepath");
}
} else {
self::delete();
self::delete($session);
}
}
}

View File

@ -0,0 +1,47 @@
<?php
require_once('lassospkit_datadir.inc.php');
require_once('lassospkit_debug.inc.php');
require_once('lassospkit_memcache.inc.php');
$LassoSPKitSessionMemCache_cookiename = LassoSPKitConfig::get('cookiename');
class LassoSPKitSessionMemCache {
function getkey($session) {
return $session->id . "_cookie_session";
}
function retrieve($session, $timeout) {
global $LassoSPKitSessionMemCache_cookiename;
$content = null;
if (isset($_COOKIE[$LassoSPKitSessionMemCache_cookiename])) {
$session->id = $_COOKIE[$LassoSPKitSessionMemCache_cookiename];
$valid = ereg("^[[:alnum:]]+$",$session->id);
if ($valid) {
$memcache_key = self::getkey($session);
$content = LassoSPKitMemCache::get($memcache_key);
}
if (! $content) {
self::delete($session);
}
}
if (! $content) {
$session->id = md5("lasso" . rand());
setcookie($LassoSPKitSessionMemCache_cookiename, $session->id, time()+3600, '/');
}
return $content;
}
function store($session, $content) {
if ($session->id) {
$memcache_key = self::getkey($session);
$ret = LassoSPKitMemCache::set($memcache_key, $content, LassoSPKitUtilsSession::$timeout);
if ($ret === FALSE) {
lassospkit_errlog("cannot write into Memcache for key cookie_session_" . $session->id);
}
}
}
function delete($session) {
$memcache_key = self::getkey($session);
LassoSPKitMemCache::delete($memcache_key);
}
}

View File

@ -17,7 +17,7 @@ class LassoSPKitSessionPHP {
if (! isset($_SESSION[$LassoSPKitSessionFile_key . '_time']) ||
$_SESSION[$LassoSPKitSessionFile_key . '_time'] - time() > $timeout) {
$content = null;
self::delete();
self::delete($session);
}
}
return $content;

View File

@ -3,6 +3,7 @@ require_once("lassospkit_config.inc.php");
require_once("lassospkit_datadir.inc.php");
require_once("lassospkit_session_file.inc.php");
require_once("lassospkit_session_php.inc.php");
require_once("lassospkit_session_memcache.inc.php");
/** This object encapsulate the communication between the frontend and the
backend of the LassoSPkit.
@ -14,6 +15,8 @@ require_once("lassospkit_session_php.inc.php");
*/
LassoSPKitUtilsSession::$cookiename = LassoSPKitConfig::get('cookiename');
LassoSPKitUtilsSession::$session_storage_class = LassoSPKitConfig::get('session_storage_class');
LassoSPKitUtilsSession::$storage = new LassoSPKitUtilsSession::$session_storage_class();
if (LassoSPKitUtilsSession::$use_session && ! isset($_SESSION)) {
session_start();
} else {
@ -60,16 +63,19 @@ class LassoSPKitUtilsSession {
'sloParams'=>0,
'defederationParams'=>0);
public static $use_session = 0;
static $session_storage_class;
static $storage;
static $timeout = 3600;
var $id = null;
function __construct() {
$content = null;
if (self::$use_session) {
$content = LassoSPKitSessionPHP::retrieve($this, self::$timeout);
} else {
$content = LassoSPKitSessionFile::retrieve($this, self::$timeout);
}
# if (self::$use_session) {
# $content = LassoSPKitSessionPHP::retrieve($this, self::$timeout);
# } else {
# $content = LassoSPKitSessionFile::retrieve($this, self::$timeout);
# }
$content = self::$storage->retrieve($this, self::$timeout);
if ($content) {
$t = @unserialize($content);
if ($t && is_array($t)) {
@ -90,11 +96,12 @@ class LassoSPKitUtilsSession {
unset($this->vars[$k]);
}
$content = serialize($this->vars);
if (self::$use_session) {
LassoSPKitSessionPHP::store($this, $content);
} else {
LassoSPKitSessionFile::store($this, $content);
}
self::$storage->store($this, $content);
# if (self::$use_session) {
# LassoSPKitSessionPHP::store($this, $content);
# } else {
# LassoSPKitSessionFile::store($this, $content);
# }
}
/** Get the singleton object to communicate
@ -145,11 +152,13 @@ class LassoSPKitUtilsSession {
/* Helper static functions */
function delete() {
if (self::$use_session) {
LassoSPKitSessionPHP::delete($this);
} else {
LassoSPKitSessionFile::delete($this);
}
# if (self::$use_session) {
# LassoSPKitSessionPHP::delete($this);
# } else {
# LassoSPKitSessionFile::delete($this);
# }
self::$storage->delete($this);
}
/** Clear the session object of all communication
from the LassoSPKit. */