<?PHP
/*
* phpMyAuth
* Jason Gerfen [hide@address.com]
*
* class.sessions.php - Custom session to db handler
*/
class dbSession
{
var $id;
var $data;
var $max_time;
var $dbconn;
function __construct( $max_time )
{
global $defined;
if( !empty( $max_time ) ) {
@ini_set( 'session.gc_maxlifetime', $max_time );
} else {
@ini_set( 'session.gc_maxlifetime', 3600 );
}
session_set_save_handler(
array( &$this, 'open' ),
array( &$this, 'close' ),
array( &$this, 'read' ),
array( &$this, 'write' ),
array( &$this, 'destroy' ),
array( &$this, 'gc' )
);
@ini_set( 'session.name', 'phpMyPurchasing' );
@ini_set( 'cache_limiter', 'private' );
@ini_set( 'cache_expire', $max_time );
@ini_set( 'use_cookies', "1" );
@register_shutdown_function( 'session_write_close' );
@session_start();
}
function register( $name, $data )
{
return $_SESSION[$name] = $data;
}
function regen( $flag = false )
{
if( $flag !== false ) {
$this->register( 'id', session_id() );
@session_regenerate_id( $flag );
$this->id = session_id();
$this->destroy( $_SESSION['id'] );
}
return;
}
function reindex()
{
global $handles;
return $handles['db']->dbFixTable( "sessions", $this->dbconn );
}
function open( $path, $name )
{
global $defined;
global $handles;
if( ( $this->dbconn = $handles['db']->dbConnect( $defined['dbhost'], $defined['username'], $defined['password'], $defined['dbname'] ) ) !== -1 ) {
return true;
} else {
return false;
}
}
function close()
{
global $defined;
global $handles;
$this->reindex();
$handles['db']->dbFreeData( $this->dbconn );
$handles['db']->dbCloseConn( $this->dbconn );
return true;
}
function read( $id )
{
global $handles;
$query = "SELECT * FROM `sessions` WHERE `session_id` = \"" . $id . "\" AND `http_user_agent` = \"" . md5( $_SERVER["HTTP_USER_AGENT"] ) . "\" LIMIT 1";
$result = $handles['db']->dbQuery( $handles['val']->ValidateSQL( $query, $this->dbconn ), $this->dbconn );
if( ( is_resource( $result ) ) && ( $handles['db']->dbNumRowsAffected( $this->dbconn ) > 0 ) ) {
$fields = $handles['db']->dbArrayResultsAssoc( $result );
if( version_compare( PHP_VERSION, '5.2.11' ) >= 0 ) {
return stripslashes( unserialize( $fields[0]['session_data'] ) );
} else {
return stripslashes( $fields[0]['session_data'] );
}
}
return "";
}
function write( $id, $data )
{
global $handles;
// fix for the serialize function in version less then 5.3
if( version_compare( PHP_VERSION, '5.2.11' ) >= 0 ) {
$query = "INSERT INTO `sessions` ( `session_id`, `http_user_agent`, `session_data`, `session_expire` ) VALUES ( \"" . $id . "\", \"" . md5( $_SERVER["HTTP_USER_AGENT"] ) . "\", \"" . mysql_real_escape_string( serialize( $data ), $this->dbconn ) . "\", \"" . time() . "\" ) ON DUPLICATE KEY UPDATE `session_id` = \"" . $id . "\", `session_data` = \"" . mysql_real_escape_string( serialize( $data ), $this->dbconn ) . "\", `session_expire` = \"" . time() . "\"";
} else {
$query = "INSERT INTO `sessions` ( `session_id`, `http_user_agent`, `session_data`, `session_expire` ) VALUES ( \"" . $id . "\", \"" . md5( $_SERVER["HTTP_USER_AGENT"] ) . "\", \"" . mysql_real_escape_string( $data, $this->dbconn ) . "\", \"" . time() . "\" ) ON DUPLICATE KEY UPDATE `session_id` = \"" . $id . "\", `session_data` = \"" . mysql_real_escape_string( $data, $this->dbconn ) . "\", `session_expire` = \"" . time() . "\"";
}
$result = $handles['db']->dbQuery( $handles['val']->ValidateSQL( $query, $this->dbconn ), $this->dbconn );
if( ( is_resource( $result ) ) && ( $handles['db']->dbNumRowsAffected( $this->dbconn ) > 0 ) ) {
$this->reindex();
return true;
} else {
return false;
}
}
function destroy( $id )
{
global $handles;
$query = "DELETE FROM `sessions` WHERE `session_id` = \"" . $id . "\" LIMIT 1";
$result = $handles['db']->dbQuery($handles['val']->ValidateSQL( $query, $this->dbconn ), $this->dbconn);
if( ( is_resource( $result ) ) && ( $handles['db']->dbNumRowsAffected( $this->dbconn ) > 0 ) ) {
$this->reindex();
return true;
} else {
return false;
}
}
function gc( $max_time )
{
global $handles;
$query = "DELETE FROM `sessions` WHERE `session_expire` > \"" . time() - $this->max_time . "\"";
$result = $handles['db']->dbQuery($handles['val']->ValidateSQL($query, $this->dbconn), $this->dbconn);
if( ( is_resource( $result ) ) && ( $handles['db']->dbNumRowsAffected( $this->dbconn ) !== -1 ) ) {
$this->reindex();
return true;
}
return false;
}
}
?>