<?php
/**
* Simple Session
*
* @package SimpleSession
* @copyright 2010
* @version 0.2
*/
class SimpleSession extends SimpleLogin {
/**
* Session garbage collector divisor
*
* @access private
* @var integer
*/
private $gc_divisor = 100;
/**
* Session garbage collector lifetime
*
* @access private
* @var integer
*/
private $gc_maxlifetime = 3600;
/**
* Session garbage collector probability
*
* @access private
* @var integer
*/
private $gc_probability = 10;
/**
* Encryption available
*
* @access private
* @var boolean
*/
private $encryption;
/**
* Encryption key
*
* @access private
* @var string
*/
private $encryption_key;
/**
* SimpleMySQL object
*
* @access protected
* @var object
*/
protected $mysql = null;
/**
* Session name
*
* @access protected
* @var string
*/
protected $name = '__SESSID';
/**
* Session lifetime
*
* @access protected
* @var integer
*/
protected $lifetime = 1800;
/**
* Session cookie path
*
* @access protected
* @var string
*/
protected $cookie_path = '/';
/**
* SimpleSessionVars object
*
* @access private
* @var object
*/
public $data = null;
/**
* Session ID
*
* @access public
* @var string
*/
public $id;
/**
* SimpleSession object constructor
*
* @access public
* @param object SimpleMySQL instance
* @return void
*
* @see SimpleSessionVars
*/
public function __construct(SimpleMySQL $MySQL) {
$this->encryption = false;
$this->encryption = false;
else {
$this->encryption = true;
$key_1 = md5(SESSION_DATA_ENCRYPTION_KEY_1
); $key_2 = md5(SESSION_DATA_ENCRYPTION_KEY_2
);
$this->encryption_key = $key_1.$key_2;
}
// Save MySQL object and clean its previous results if any
$this->mysql->clean();
// Set session garbage collector properties
ini_set('session.gc_divisor', $this->gc_divisor); ini_set('session.gc_probability', $this->gc_probability); ini_set('session.gc_maxlifetime', $this->gc_maxlifetime);
// Set session id hashing options
ini_set('session.hash_function', 1); ini_set('session.hash_bits_per_character', 5);
// Set handler functions
array(&$this, '__close'), array(&$this, '__write'), array(&$this, '__destroy'), );
// Call session_write_close() from the object destructor
}
/**
* SimpleSession object destructor
*
* @access public
* @return boolean
*/
public function __destruct() {
// Call session_write_close() then return true
return true;
}
public function __open($save_path, $session_name) {
return true;
}
public function __close() {
return true;
}
public function __read($session_id) {
$session_id = $this->mysql->escape($session_id);
$session_data = '';
// Build query to retrieve current session data
$query_sentence = "
SELECT
session_data
FROM
{$this->mysql->tables['sessions']}
WHERE
session_id = '%s'
";
$this->mysql->query(sprintf($query_sentence, $session_id));
// Check if session data is available
if ($this->mysql->affected_rows > 0) {
$session_data = $this->mysql->query_result[0]->session_data;
$session_data = $this->decrypt($session_data);
$this->mysql->clean();
}
// If no session data was found, return an empty string
return (string)$session_data;
}
public function __write($session_id, $session_data) {
$session_id = $this->mysql->escape($session_id);
$session_data = $this->encrypt($session_data);
// Build query to write/rewrite current session data into database
$query_sentence = "
REPLACE INTO
{$this->mysql->tables['sessions']}
(session_id, session_data, session_touched)
VALUES
('%s', '%s', UNIX_TIMESTAMP())
";
$this->mysql->query(sprintf($query_sentence, $session_id, $session_data)); $this->mysql->clean();
return true;
}
public function __destroy($session_id) {
$session_id = $this->mysql->escape($session_id);
// Build query to delete the current session from database
$query_sentence = "
DELETE FROM
{$this->mysql->tables['sessions']}
WHERE
session_id = '%s'
";
$this->mysql->query(sprintf($query_sentence, $session_id));
return true;
}
public function __gc($session_max_lifetime) {
$query_sentence = "
DELETE FROM
{$this->mysql->tables['sessions']}
WHERE
session_touched + {$session_max_lifetime} < UNIX_TIMESTAMP()
";
$this->mysql->query($query_sentence);
return true;
}
/**
* Session start
*
* @access public
* @return unknown
*/
public function start() {
header("Expires: Mon, 28 Aug 1989 02:22:00 GMT"); header("Last-Modified: " .gmdate("D, d M Y H:i:s"). " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false);
// Create SimpleSessionData object to manipulate session data
// I.e.: $SimpleSession->data->foo = 'bar' => ($_SESSION['foo'] = 'bar')
$this->data = new SimpleSessionData;
}
/**
* Session destroy
*
* @access public
* @return boolean
*/
public function destroy() {
// Unset all session variables
$this->id = null;
// Delete session cookie, if found
if (isset($_COOKIE[$this->name])) @setcookie($this->name, '', time() - $this->lifetime, $this->cookie_path);
// Destroy the current session
}
/**
* Session regenerate ID
*
* @access public
* @param boolean $delete_old_session
* @return boolean
*/
public function regenerate_id($delete_old_session = false) {
else return false;
}
/**
* Session data encrypter
*
* @access public
* @param string $session_data
* @return string
*/
private function encrypt($session_data = '') {
if (empty($session_data)) return $session_data;
if (!$this->encryption)
$key = substr($this->encryption_key, 0, $ks);
// Encrypt data
}
/**
* Session data decrypter
*
* @access public
* @param string $session_data
* @return string
*/
private function decrypt($session_data = '') {
if (empty($session_data)) return $session_data;
if (!$this->encryption)
return $session_data;
$key = substr($this->encryption_key, 0, $ks);
// Decrypt data
$decrypted_data = rtrim($decrypted_data);
return (string)$decrypted_data;
}
}
/**
* Simple Session Data
*
* @package SimpleSession
* @subpackage SimpleSessionData
* @copyright 2010
* @version 0.1
*/
class SimpleSessionData {
public function __set($var, $value) {
$_SESSION[$var] = $value;
}
public function __get($var) {
if (!isset($_SESSION[$var])) return null;
else return $_SESSION[$var];
}
public function __isset($var) {
return isset($_SESSION[$var]); }
public function __unset($var) {
}
}
?>