File: /data/www/webtemplate/wtv2/codedoc/wtSession.php
<?php
/**
* WebTemplate Core
*
* @version 2.0
* @module WebTemplate Core
*/
/**
* The class to contain the session functions
*
* @class WTSession
*/
$GLOBALS["WTSESSIONTIMEOUT"] = 2 * 60 * 60;
class WTSession
{
var $m_key;
var $m_sessionData;
function __construct()
{
if(!$GLOBALS["WTSITEID"]) {
$this->m_sessionData = Array();
return;
}
// if(array_key_exists("WTSESSION" . "_" . $GLOBALS["WTSITEID"], $_COOKIE)) {
// $this->m_key = $_COOKIE["WTSESSION" . "_" . $GLOBALS["WTSITEID"]];
if(array_key_exists("WTSESSION" . "_" . $GLOBALS["WTSITEID"], $_SESSION)) {
$this->m_key = $_SESSION["WTSESSION" . "_" . $GLOBALS["WTSITEID"]];
$sql = "SELECT
sessionData
FROM
dbWTSession.tblSession
WHERE
sessionKey = '" . mysql_escape_string($this->m_key) . "'
AND siteID = {$GLOBALS["WTSITEID"]}";
$query = mysql_query($sql);
if($row = mysql_fetch_row($query)) {
$this->m_sessionData = unserialize($row[0]);
} else {
$this->m_key = '';
}
}
if($this->m_key == '') {
$expires = time() + $GLOBALS["WTSESSIONTIMEOUT"];
$keyok = false;
while(!$keyok) {
$this->m_key = md5(uniqid(rand(), true));
$sql = "SELECT
COUNT(*)
FROM
dbWTSession.tblSession
WHERE
sessionKey = '" . mysql_escape_string($this->m_key) . "'";
$query = mysql_query($sql);
if($row = mysql_fetch_row($query)) {
$keyok = $row[0] == 0;
} else {
$keyok = true;
}
}
// need to create a new cookie
$sql = "INSERT INTO dbWTSession.tblSession (
sessionKey,
sessionData,
sessionExpires,
siteID,
sessionIP)
VALUES (
'{$this->m_key}',
'',
$expires,
{$GLOBALS["WTSITEID"]},
'{$_SERVER['REMOTE_ADDR']}')";
if(!mysql_query($sql)) {
print $sql;
print mysql_error();
}
$this->m_sessionData = Array();
// setcookie("WTSESSION" . "_" . $GLOBALS["WTSITEID"], $this->m_key, 0, "/");
$_SESSION["WTSESSION" . "_" . $GLOBALS["WTSITEID"]] = $this->m_key;
}
}
function __destruct()
{
$expires = time() + $GLOBALS["WTSESSIONTIMEOUT"];
$sql = "UPDATE
dbWTSession.tblSession
SET
sessionData = '" . mysql_escape_string(serialize($this->m_sessionData)) . "',
sessionExpires = $expires
WHERE
sessionKey = '" . mysql_escape_string($this->m_key) . "'";
mysql_query($sql);
}
function setSession($key)
{
$sql = "SELECT
sessionData
FROM
dbWTSession.tblSession
WHERE
sessionKey = '" . mysql_escape_string($key) . "'";
$query = mysql_query($sql);
if($row = mysql_fetch_row($query)) {
$this->m_sessionData = unserialize($row[0]);
$this->m_key = $key;
// setcookie("WTSESSION" . "_" . $GLOBALS["WTSITEID"], $this->m_key, 0, "/");
$_SESSION["WTSESSION" . "_" . $GLOBALS["WTSITEID"]] = $this->m_key;
return true;
}
return false;
}
/**
* Set a session variable
*
* @method setValue
* @private
* @param {String} name The name of the session variable
* @param {String} value The value of the session variable
*/
function setValue($name, $value)
{
$this->m_sessionData[$name] = $value;
}
/**
* Get all session data as an associative array
*
* @method getSessionData
* @static
*/
function getSessionData() {
return $GLOBALS["WTSESSION"]->m_sessionData;
}
/**
* Get the value of a session variable
*
* @method getValue
* @private
* @param {String} name The name of the session variable
*/
function getValue($name)
{
if($this->m_sessionData && array_key_exists($name, $this->m_sessionData)) {
return $this->m_sessionData[$name];
}
return null;
}
/**
* Set a session variable
*
* @method set
* @static
* @param {String} name The name of the session variable
* @param {String} value The value of the session variable
*/
public static function set($name, $value)
{
$GLOBALS["WTSESSION"]->setValue($name, $value);
}
/**
* Get the value of a session variable
*
* @method get
* @static
* @param {String} name The name of the session variable
*/
public static function get($name)
{
return $GLOBALS["WTSESSION"]->getValue($name);
}
public static function getKey()
{
return $GLOBALS["WTSESSION"]->m_key;
}
public static function processSessionURI($path)
{
$pathArray = explode("/", $path);
$key = $pathArray[1];
$GLOBALS["WTSESSION"]->setSession($key);
$requestUri = str_replace("__session/$key/", "", $_SERVER["REQUEST_URI"]);
header("Location: $requestUri");
exit("");
}
}
$GLOBALS["WTSESSION"] = new WTSession();
?>