Codice PHP:
class session
{
var $sessionid = "";
var $useragent = "";
var $ipaddress = "";
var $dateline = "";
var $leveluser = "";
var $islogged = false;
var $sessiontype = "";
var $typeid = "";
var $_SS = array();
function __construct(){
}
function RegisterSession($sessionid, $sesstype = 0 , $id=0)
{
switch($sesstype)
{
case SESSION_GUEST : $this->AddSessionVisitors($sessionid, $sesstype,$id ); break;
case SESSION_USER : $this->AddSession($sessionid, $sesstype,$id ); break;
case SESSION_STAFF : $this->AddSession($sessionid, $sesstype,$id ); break;
case SESSION_ADMIN : $this->AddSession($sessionid, $sesstype,$id ); break;
}
}
function SessionStart($sesstype )
{
if(session_id()== "")
session_start();
global $dbcore , $smarty , $RCW , $cookie , $log , $settings , $_SS ;
$this->sessionid = isset($_SESSION['sessionid'])?$_SESSION['sessionid']:'';
$this->id = isset($_SESSION['userid'])?$_SESSION['userid']:'';
$this->ipaddress = $_SERVER['REMOTE_ADDR'];
$this->useragent = trim(substr($_SERVER['HTTP_USER_AGENT'], 0, 60));
$this->sessiontype = $sesstype ;
$this->islogged = isset($_SESSION['newsession'])?$_SESSION['newsession']:'';
$countsess = $dbcore->Query("SELECT COUNT(sessionid) AS total FROM `sessions` WHERE sessionid = '".$dbcore->escape($this->sessionid)."'");
if($countsess[0]['total'] >0)
{
}else{
$this->RegisterSession($this->sessionid, $this->sessiontype,$this->id );
}
if(!$this->readsession($this->sessionid, $this->sessiontype , $this->id ))
{
unset($_SESSION['newsession']);
unset($_SESSION['sessionid']);
unset($_SESSION['userid']);
$cookie->DeleteCookie('userautenticate');
$cookie->DeleteCookie('userid');
echo "invalid sessionidxxxx ";
if (defined("SESSIONEXPIRY")){
echo "[Session Expired Debug]: Unable to Read a valid session for session id: " . $sessionid;
}
return false;
}else{
return true;
}
if (($this->sessiontype == SESSION_ADMIN ) && $RCW['user']['isadmin'] != "1")
{
$this->flushsession($this->sessionid ,$this->sessiontype);
if(defined("SESSIONEXPIRY"))
{
echo "[Session Expired Debug]: User isnt admin and is trying to login to Admin CP.. bailing out..";
}
return false;
}else{
return true;
}
if($settings['sessioninactivity'] != "0" &&
((NEWTIME - $this->_SS["lastactivity"]) > $settings['sessioninactivity'] &&
$_SS["lastactivity"] != 0)){
$this->flushsession($this->_SS['sessionid'], $this->_SS['sessiontype']);
/* echo 'invalid sessionid '; */
if (defined("SESSIONEXPIRY"))
{
echo "[Session Expired Debug]: Session expired due to inactivity,
Date difference between current system date (" . date("d M Y h:i:s A", NEWTIME) . ")
and last activity (" . date("d M Y h:i:s A", $this->_SS['lastactivity']) .")
is " . (NEWTIME - $this->_SS['lastactivity']) . "
seconds whereas inactivity timing setting under Admin CP > Settings
is " . $settings['sessioninactivity'] . " seconds.";
}
return false;
}else{
if ((NEWTIME - $RCW['user']['lastactivity']) > 180)
{
$log->UpdateLastActivity($RCW['user']['userid']);
}
if ((NEWTIME - $RCW["user"]["lastvisit"]) > 1800)
{
$log->updateLastVisit($RCW['user']['userid']);
}
return true;
}
}
function readsession($sessionid, $sesstype , $typeid = 0)
{
global $dbcore , $smarty , $RCW , $cookie , $_SS , $settings;
$row = $dbcore->Query("SELECT * FROM `sessions` WHERE sessionid = :value",
array(':value' => $sessionid)
);
$_SS = $row ;
if ($_SS[0]["ipaddress"] != $this->ipaddress)
{
return false;
}
if ($_SS[0]["useragent"] != $this->useragent)
{
return false;
}
if ($_SS[0]["ipaddress"] == $this->ipaddress and $_SS[0]["useragent"] != $this->useragent)
{
return false;
}
$this->sessionid = isset($_SS[0]["sessionid"])?$_SS[0]["sessionid"]:'';
$RCW["sessionid"] = isset($_SS[0]["sessionid"])?$_SS[0]["sessionid"]:'';
$RCW["sessionstarted"] = true;
$this->lastactivity = isset($_SS[0]["lastactivity"])?$_SS[0]["lastactivity"]:'';
$this->isloggedin = isset($_SS[0]["islogged"])?$_SS[0]["islogged"]:0;
$this->sessiontype = isset($_SS[0]["sessiontype"])?$_SS[0]["sessiontype"]:'';
$this->dateline = isset($_SS[0]["date"])?$_SS[0]["date"]:'';
$this->typeid = isset($_SS[0]["typeid"])?$_SS[0]["typeid"]:'';
$this->_SS = $_SS;
$RCW["session"] = &$this->_SS[0];
unset($_SS);
$sessionidlist = array();
$sessionidlist[] = $sessionid;
return true;
}
function updateSession($sessionid)
{
global $dbcore , $smarty , $RCW , $cookie , $log , $settings;
$dbcore->Query("UPDATE `sessions` SET `typeid` = '". $dbCore->escape($this->typeid) ."' WHERE `sessionid` = '". $dbcore->escape($sessionid) ."';");
}
function AddSession($sessionid , $sesstype, $typeid = 0 )
{
global $dbcore , $smarty , $RCW , $cookie , $techaut , $settings;
if($sesstype == SESSION_ADMIN)
{
if(!empty($typeid ))
{
$time = NEWTIME - $settings['sessioninactivity'];
$dbcore->Query("DELETE FROM `sessions`
WHERE `ipaddress` = '".$dbcore->escape($this->ipaddress). "'
AND `typeid` = '".intval($typeid)."'
AND `sessiontype` = '" . $dbcore->escape($sesstype) . "'
");
$isloggedin = true ;
} else {
return false;
}
}else if($sesstype == SESSION_STAFF){
if(!empty($typeid )){
$time = NEWTIME - $settings['sessioninactivity'];
$dbcore->Query("DELETE FROM `sessions`
WHERE `sessiontype` = '" . intval($sesstype) . "'
AND `lastactivity` < '" . $dbcore->escape($time) . "';");
$isloggedin = true ;
} else {
return false;
}
}else if($sesstype == SESSION_USER){
if(!empty($typeid )){
$time = NEWTIME - $settings['sessioninactivity'];
$dbcore->Query("DELETE FROM `sessions`
WHERE `sessiontype` = '" . intval($sesstype) . "'
AND `lastactivity` < '" . $dbcore->escape($time) . "';");
$isloggedin = true ;
} else {
return false;
}
}
$res = $dbcore->Query("REPLACE INTO `sessions`
(`sessionid`,`typeid`, `ipaddress`, `lastactivity`, `useragent`, `islogged`, `sessiontype`, `date`) VALUES(
'".$dbcore->escape($sessionid)."','".$dbcore->escape($typeid )."',
'".$dbcore->escape($this->ipaddress)."',
'". NEWTIME ."',
'". $dbcore->escape(substr($this->useragent, 0,60))."',
'" . iff( isset($isloggedin),1,0 ). "',
'". $dbcore->escape($this->sessiontype) ."',
'". time() ."')") ;
if(!$res){
/* echo 'la sessione non e stata salvata '; */
} else {
/* echo 'this session added '.$sessionid; */
}
}
function AddSessionVisitors($sessionid , $sesstype, $typeid = 0 )
{
global $dbcore , $smarty , $RCW , $cookie , $techaut , $settings;
if($sesstype == SESSION_GUEST)
{
$isloggedin = false ;
}
$res = $dbcore->Query("REPLACE INTO `sessions`
(`sessionid`,`typeid`, `ipaddress`, `lastactivity`, `useragent`, `islogged`, `sessiontype`, `date`) VALUES(
'".$dbcore->escape($sessionid)."','".$dbcore->escape($typeid )."',
'".$dbcore->escape($this->ipaddress)."',
'". NEWTIME ."',
'". $dbcore->escape(substr($this->useragent, 0,60))."',
'" . iff( isset($isloggedin),1,0 ). "',
'". $dbcore->escape($this->sessiontype) ."',
'". time() ."')") ;
if(!$res){
/* echo 'la sessione non e stata salvata '; */
} else {
/* echo 'this session added '.$sessionid; */
}
}
function LogoutSession($sesstype)
{
global $dbcore;
global $SESS;
global $cookie;
if(session_id()== "")
session_start();
$this->sessionid = isset($_SESSION['sessionid'])?$_SESSION['sessionid']:0;
if(empty($this->sessionid))
{
return false;
}else{
$this->flushsession( $sesstype ,$this->sessionid );
return true;
}
}
function flushsession($sesstype=0, $sessionid )
{
global $dbcore;
global $RCW;
global $cookie;
if (empty($sessionid)) {
return false;
}
$res = $dbcore->SQLDelete("sessions" ,"sessionid", "$sessionid" );
if(! $res){
echo 'this session not canelled '.$sessionid;
}
if(session_id()== "")
session_start();
session_unset($this->sessionid);
unset($this->lastactivity);
session_unset($this->islogged);
session_unset($this->sessiontype);
session_unset($this->dateline);
session_unset($this->typeid);
unset($this->SESS);
session_unset($RCW["sessionid"]);
session_unset($RCW['session']);
unset($_SESSION['newsession']);
unset($_SESSION['userid']);
$cookie->DeleteCookie('userautenticate');
$cookie->DeleteCookie('userid');
session_destroy();
}
function Destroyinactivesessions()
{
global $dbcore , $RCW , $cookie , $settings ;
$time = NEWTIME - $settings['sessioninactivity'];
$dbcore->query("DELETE FROM `sessions` WHERE `lastactivity` < '" . $dbcore->escape($time) . "'");
return true;
}
}