Browse Source

Started porting Session to Storage.

pull/5/head
Etenil 15 years ago
parent
commit
6d6edc2d26
  1. 6
      loader.php
  2. 96
      system/Session.php
  3. 26
      tests/php/Session/TestSession.php

6
loader.php

@ -12,6 +12,10 @@ define('SESSION_MAX_AGE', 24 * 3600);
// Loads up all system libraries.
require(LIB_PATH . "Lang/i18n.php");
require(LIB_PATH . "Storage/loader.php");
load_storage(array('sqlite'));
require(LIB_PATH . "Session.php");
require(LIB_PATH . "Utils.php");
require(LIB_PATH . "Cache.php");
@ -32,8 +36,6 @@ require(LIB_PATH . "Tpl/TplPageBuilder.php");
require(LIB_PATH . "Widget/WidgetBase.php");
require(LIB_PATH . "Widget/WidgetWrapper.php");
require(LIB_PATH . "Storage/loader.php");
load_storage(array('sqlite'));
// Starting session.
Session::start(APP_NAME);

96
system/Session.php

@ -21,6 +21,20 @@
if(!class_exists('Session')):
class SessionVar extends StorageBase
{
protected $name;
protected $value;
protected $session;
protected function type_init()
{
$this->name = StorageType::varchar(128);
$this->value = StorageType::text();
$this->session = StorageType::varchar(128);
}
}
class Session
{
protected $db;
@ -45,68 +59,21 @@ class Session
if(defined('SESSION_MAX_AGE')) {
$this->max_age = SESSION_MAX_AGE;
}
// Do we create the schema?
$create = false;
if(!file_exists($db_file)) {
$create = true;
}
$this->db = new SQLite3($db_file);
$this->db->busyTimeout(500);
$this->container = $this->db->escapeString($name);
if($create) { // Creating the session schema.
$this->db->exec('CREATE TABLE IF NOT EXISTS sessions(hash VARCHAR(64) PRIMARY KEY, timestamp INTEGER)');
$this->db->exec(
'CREATE TABLE IF NOT EXISTS session_containers('.
'id INTEGER PRIMARY KEY AUTOINCREMENT, '.
'hash VARCHAR(64) REFERENCES sessions(hash) ON DELETE CASCADE, '.
'name VARCHAR(128))'
);
$this->db->exec(
'CREATE TABLE IF NOT EXISTS session_vars('.
'container INTEGER REFERENCES session_containers(id) ON DELETE CASCADE, '.
'name VARCHAR(128), '.
'value TEXT)'
);
}
if(self::$sid == null && isset($_COOKIE['PHPFASTSESSID'])) {
$sessid = $this->db->escapeString($_COOKIE['PHPFASTSESSID']);
$session = $this->db->querySingle('SELECT * FROM sessions WHERE hash="'.$sessid.'"', true);
if(count($session) == 0) {
$this->regenerate();
}
else if($session['timestamp'] + $this->max_age < time()) {
echo 'expired! ' . ($session['timestamp'] + $this->max_age) . ' < ' . time();
$sql = 'DELETE FROM sessions WHERE hash="'.$sessid.'"';
echo $sql;
$this->db->exec($sql);
$this->regenerate();
}
else {
self::$sid = $sessid;
}
}
else if(self::$sid == null) {
$this->regenerate();
}
// Does the container exist?
$num_container = $this->db->querySingle('SELECT id FROM session_containers WHERE hash="'.self::$sid.'" AND name="'.$this->container.'"');
if(!$num_container) {
$this->db->exec('INSERT INTO session_containers(hash, name) VALUES("'.self::$sid.'", "'.$this->container.'")');
$this->container_id = $this->db->lastInsertRowID();
$this->db = new StorageEngineSqlite($db_file);
// fallback...
if(!$this->container_id) {
$this->container_id = $this->db->querySingle('SELECT id FROM session_containers WHERE hash="'.self::$sid.'" AND name="'.$this->container.'"');
}
} else {
$this->container_id = $this->db->escapeString($num_container);
if($create) {
$var = new SessionVar();
$this->db->create($var);
}
$this->regenerate();
}
protected function regenerate()
@ -115,25 +82,12 @@ class Session
$hash_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
$hash = "";
$exists = true;
$sessions_tbl = $this->db->query('SELECT hash FROM sessions');
$sessions = array();
while($row = $sessions_tbl->fetchArray()) {
$sessions[] = $row['hash'];
for($i = 0; $i < 64; $i++) {
$r = mt_rand(0, strlen($hash_chars) - 1);
$hash.= $hash_chars[$r];
}
while($exists) {
for($i = 0; $i < 64; $i++) {
$r = rand(0, strlen($hash_chars) - 1);
$hash.= $hash_chars[$r];
}
$exists = in_array($hash, $sessions);
}
self::$sid = $this->db->escapeString($hash);
$sql = 'INSERT INTO sessions(hash, timestamp) VALUES("'.self::$sid.'", "'.time().'")';
$this->db->exec($sql);
self::$sid = $hash;
setcookie('PHPFASTSESSID', self::$sid, time() + $this->max_age);
}

26
tests/php/Session/TestSession.php

@ -0,0 +1,26 @@
<?php
/**
* @file TestStorage.php
* This file is part of Movim.
*
* @brief Tests the Storage module.
*
* @author Guillaume Pasquet <etenil@etenilsrealm.nl>
*
* @version 1.0
* @date 27 April 2011
*
* Copyright (C)2011 Movim Project.
*
* %license%
*/
define('DB_DEBUG', true);
define('DB_LOGFILE', 'queries.log');
class TestSession
{
}
?>
Loading…
Cancel
Save