LAST UPDATE, Functional Session Handling with Database Storage

This commit is contained in:
Erik Amaru Ortiz
2013-09-24 16:07:58 -04:00
parent b4c98570e8
commit aca0d1762a
9 changed files with 1506 additions and 24 deletions

View File

@@ -18,6 +18,8 @@ class PmSessionHandler implements SessionHandlerInterface
private $user = '';
private $password = '';
private $dbtable = 'SESSION_STORAGE';
/**
* $httponly Session accessibility boolean key
* By default the session cookie is not accessable via javascript.
@@ -27,16 +29,6 @@ class PmSessionHandler implements SessionHandlerInterface
public function __construct($user, $password, $dsn)
{
// set our custom session functions.
/*session_set_save_handler(
array($this, 'open'),
array($this, 'close'),
array($this, 'read'),
array($this, 'write'),
array($this, 'destroy'),
array($this, 'gc')
);*/
$this->dbUser = $user;
$this->dbPassword = $password;
$this->dsn = $dsn;
@@ -45,6 +37,8 @@ class PmSessionHandler implements SessionHandlerInterface
// This line prevents unexpected effects when using objects as save handlers.
register_shutdown_function('session_write_close');
error_log(" * Session: a new instance was created!!");
}
function start_session($sessionName, $secure)
@@ -83,7 +77,7 @@ class PmSessionHandler implements SessionHandlerInterface
// It also generates a new encryption key in the database.
session_regenerate_id(true);
error_log(" *** start_session was executed!!");
error_log(" * Session: start_session was executed!!");
}
public function open($savePath, $sessionName)
@@ -106,7 +100,7 @@ class PmSessionHandler implements SessionHandlerInterface
)
);
error_log(" *** open was executed!!");
error_log(" * Session: open was executed!!");
return true;
}
@@ -122,7 +116,7 @@ class PmSessionHandler implements SessionHandlerInterface
// close the connection when your script ends.
$this->db = null;
error_log(" *** close was executed!!");
error_log(" * Session: close was executed!!");
return true;
}
@@ -132,17 +126,17 @@ class PmSessionHandler implements SessionHandlerInterface
$time = time();
if(! isset($this->wstmt)) {
$sql = "REPLACE INTO sessions(ID, SET_TIME, DATA, SESSION_KEY) VALUES (?, ?, ?, ?)";
$sql = "REPLACE INTO {$this->dbtable} (ID, SET_TIME, DATA, SESSION_KEY) VALUES (?, ?, ?, ?)";
$this->wstmt = $this->db->prepare($sql);
}
$key = 'sample key #' . rand();
$key = 'K' . rand();
$data = base64_encode(serialize($data));
//$this->wstmt->bind_param('siss', $id, $time, $data, $key);
$this->wstmt->execute(array($id, $time, $data, $key));
error_log(" *** write was executed!!");
error_log(" * Session: write was executed!!");
return true;
}
@@ -150,14 +144,14 @@ class PmSessionHandler implements SessionHandlerInterface
public function read($id)
{
if(! isset($this->rstmt)) {
$this->rstmt = $this->db->prepare("SELECT DATA FROM sessions WHERE ID = ? LIMIT 1");
$this->rstmt = $this->db->prepare("SELECT DATA FROM {$this->dbtable} WHERE ID = ? LIMIT 1");
}
$this->rstmt->execute(array($id));
$data = $this->rstmt->fetch();
$data = unserialize(base64_decode($data['DATA']));
error_log(" *** read was executed!!");
error_log(" * Session: read was executed!!");
return $data;
}
@@ -165,12 +159,12 @@ class PmSessionHandler implements SessionHandlerInterface
public function destroy($id)
{
if(! isset($this->dstmt)) {
$this->dstmt = $this->db->prepare("DELETE FROM sessions WHERE ID = ?");
$this->dstmt = $this->db->prepare("DELETE FROM {$this->dbtable} WHERE ID = ?");
}
$this->dstmt->execute(array($id));
error_log(" *** destroy was executed!!");
error_log(" * Session: destroy was executed!!");
return true;
}
@@ -180,12 +174,12 @@ class PmSessionHandler implements SessionHandlerInterface
$time = time() - $maxlifetime;
if(! isset($this->gcstmt)) {
$thi->gcstmt = $this->db->prepare("DELETE FROM sessions WHERE SET_TIME < ?");
$thi->gcstmt = $this->db->prepare("DELETE FROM {$this->dbtable} WHERE SET_TIME < ?");
}
$thi->gcstmt->execute(array($time));
error_log(" *** gc was executed!!");
error_log(" * Session: gc was executed!!");
return true;
}

View File

@@ -0,0 +1,19 @@
<?php
require_once 'classes/model/om/BaseSessionStorage.php';
/**
* Skeleton subclass for representing a row from the 'SESSION_STORAGE' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
* @package classes.model
*/
class SessionStorage extends BaseSessionStorage {
} // SessionStorage

View File

@@ -0,0 +1,23 @@
<?php
// include base peer class
require_once 'classes/model/om/BaseSessionStoragePeer.php';
// include object class
include_once 'classes/model/SessionStorage.php';
/**
* Skeleton subclass for performing query and update operations on the 'SESSION_STORAGE' table.
*
*
*
* You should add additional methods to this class to meet the
* application requirements. This class will only be generated as
* long as it does not already exist in the output directory.
*
* @package classes.model
*/
class SessionStoragePeer extends BaseSessionStoragePeer {
} // SessionStoragePeer

View File

@@ -0,0 +1,80 @@
<?php
require_once 'propel/map/MapBuilder.php';
include_once 'creole/CreoleTypes.php';
/**
* This class adds structure of 'SESSION_STORAGE' table to 'workflow' DatabaseMap object.
*
*
*
* These statically-built map classes are used by Propel to do runtime db structure discovery.
* For example, the createSelectSql() method checks the type of a given column used in an
* ORDER BY clause to know whether it needs to apply SQL to make the ORDER BY case-insensitive
* (i.e. if it's a text column type).
*
* @package workflow.classes.model.map
*/
class SessionStorageMapBuilder
{
/**
* The (dot-path) name of this class
*/
const CLASS_NAME = 'classes.model.map.SessionStorageMapBuilder';
/**
* The database map.
*/
private $dbMap;
/**
* Tells us if this DatabaseMapBuilder is built so that we
* don't have to re-build it every time.
*
* @return boolean true if this DatabaseMapBuilder is built, false otherwise.
*/
public function isBuilt()
{
return ($this->dbMap !== null);
}
/**
* Gets the databasemap this map builder built.
*
* @return the databasemap
*/
public function getDatabaseMap()
{
return $this->dbMap;
}
/**
* The doBuild() method builds the DatabaseMap
*
* @return void
* @throws PropelException
*/
public function doBuild()
{
$this->dbMap = Propel::getDatabaseMap('workflow');
$tMap = $this->dbMap->addTable('SESSION_STORAGE');
$tMap->setPhpName('SessionStorage');
$tMap->setUseIdGenerator(false);
$tMap->addPrimaryKey('ID', 'Id', 'string', CreoleTypes::VARCHAR, true, 128);
$tMap->addColumn('SET_TIME', 'SetTime', 'string', CreoleTypes::VARCHAR, true, 10);
$tMap->addColumn('DATA', 'Data', 'string', CreoleTypes::LONGVARCHAR, true, null);
$tMap->addColumn('SESSION_KEY', 'SessionKey', 'string', CreoleTypes::VARCHAR, true, 128);
$tMap->addColumn('CLIENT_ADDRESS', 'ClientAddress', 'string', CreoleTypes::VARCHAR, false, 32);
} // doBuild()
} // SessionStorageMapBuilder

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -2997,4 +2997,16 @@
<column name="SEQ_NAME" type="VARCHAR" size="50" required="true" primaryKey="true" default=""/>
<column name="SEQ_VALUE" type="INTEGER" required="true" default="0"/>
</table>
<table name="SESSION_STORAGE">
<column name="ID" type="VARCHAR" size="128" required="true" primaryKey="true" />
<column name="SET_TIME" type="VARCHAR" size="10" required="true" />
<column name="DATA" type="LONGVARCHAR" required="true" />
<column name="SESSION_KEY" type="VARCHAR" size="128" required="true" />
<column name="CLIENT_ADDRESS" type="VARCHAR" size="32" required="false" default="0.0.0.0" />
<index name="indexSessionStorage">
<index-column name="ID"/>
</index>
</table>
</database>

View File

@@ -568,7 +568,7 @@ CREATE TABLE `TASK`
`TAS_PRIORITY_VARIABLE` VARCHAR(100) default '' NOT NULL,
`TAS_ASSIGN_TYPE` VARCHAR(30) default 'BALANCED' NOT NULL,
`TAS_ASSIGN_VARIABLE` VARCHAR(100) default '@@SYS_NEXT_USER_TO_BE_ASSIGNED' NOT NULL,
`TAS_GROUP_VARIABLE` VARCHAR(100) default '',
`TAS_GROUP_VARIABLE` VARCHAR(100),
`TAS_MI_INSTANCE_VARIABLE` VARCHAR(100) default '@@SYS_VAR_TOTAL_INSTANCE' NOT NULL,
`TAS_MI_COMPLETE_VARIABLE` VARCHAR(100) default '@@SYS_VAR_TOTAL_INSTANCES_COMPLETE' NOT NULL,
`TAS_ASSIGN_LOCATION` VARCHAR(20) default 'FALSE' NOT NULL,
@@ -1455,5 +1455,22 @@ CREATE TABLE `SEQUENCES`
`SEQ_VALUE` INTEGER default 0 NOT NULL,
PRIMARY KEY (`SEQ_NAME`)
)ENGINE=InnoDB DEFAULT CHARSET='utf8' COMMENT='Sequences, Controls the numerical sequence of a table';
#-----------------------------------------------------------------------------
#-- SESSION_STORAGE
#-----------------------------------------------------------------------------
DROP TABLE IF EXISTS `SESSION_STORAGE`;
CREATE TABLE `SESSION_STORAGE`
(
`ID` VARCHAR(128) NOT NULL,
`SET_TIME` VARCHAR(10) NOT NULL,
`DATA` MEDIUMTEXT NOT NULL,
`SESSION_KEY` VARCHAR(128) NOT NULL,
`CLIENT_ADDRESS` VARCHAR(32) default '0.0.0.0',
PRIMARY KEY (`ID`),
KEY `indexSessionStorage`(`ID`)
)ENGINE=InnoDB ;
# This restores the fkey checks, after having unset them earlier
SET FOREIGN_KEY_CHECKS = 1;

View File

@@ -285,7 +285,7 @@ try {
}
ini_set('session.gc_maxlifetime', $timelife);
ini_set('session.cookie_lifetime', $timelife);
session_start();
//session_start();
$e_all = defined( 'E_DEPRECATED' ) ? E_ALL & ~ E_DEPRECATED : E_ALL;
$e_all = defined( 'E_STRICT' ) ? $e_all & ~ E_STRICT : $e_all;
@@ -678,6 +678,14 @@ try {
//require_once ("propel/Propel.php");
//require_once ("creole/Creole.php");
//var_dump(include(PATH_CORE . "config/databases.php")); die;
include_once PATH_GULLIVER_HOME . 'core/Session/PmSessionHandler.php';
//$dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
$handler = new PmSessionHandler(DB_USER, DB_PASS, DB_ADAPTER.':host='.DB_HOST.';dbname='.DB_NAME);
session_start();
if (defined( 'DEBUG_SQL_LOG' ) && DEBUG_SQL_LOG) {
define( 'PM_PID', mt_rand( 1, 999999 ) );
require_once 'Log.php';