/*
require php 4.2.0 +
*/
if (!class_exists('HttpSession')){
include('UsrProfile.inc');
class HttpSession extends UsrProfile{
function OpenSession($path,$name){
$errh = "HttpSession::OpenSession:";
$this->mlog($errh,ML_DEBUG_CALL);
$this->Connect();
return $this->OnSessionOpen();
}
function CloseSession(){
$errh = "HttpSession::CloseSession:";
$this->mlog($errh,ML_DEBUG_CALL);
$this->OnSessionClose();
$this->CloseConnection();
return true;
}
function ReadSession($id){
$errh = "HttpSession::ReadSession:";
$this->mlog($errh,ML_DEBUG_CALL);
if (!$this->IsConnected()){
$this->mlog("$errh Not connected.");
return '';
}
if (1 != $this->IsSessionExists($id)){
// Если записи нет или она просрочена, загружаем дефолтный профиль.
$this->SelectProfile(UID_GUEST);
$this->OnSessionRead();
return '';
}
// Указанная сессия уже существует. Загрузим и распакуем профиль.
$qtext = sprintf("SELECT vars,uid FROM sessions WHERE SID='%s'",$this->Quote($id));
if (!$this->Select($qtext)){
return '';
}
if (!$vars = $this->NextHashRef()){
return '';
}
// Выбираем указанный профиль
if (!$this->SelectProfile($vars['UID'])){
$this->SelectProfile(UID_GUEST);
$vars['VARS'] = '';
}
$this->OnSessionRead();
return $vars['VARS'];
}
function WriteSession($id,$dt){
$errh = "HttpSession::WriteSession:";
$this->mlog($errh,ML_DEBUG_CALL);
if (!$this->IsConnected()){
$this->mlog("$errh Not connected.");
return false;
}
if (!$this->OnSessionWrite()){
return false;
}
$qtext = sprintf("LASTACS=%d,UID=%d,VARS='%s'",intval(time() / 60),
$this->GetProfileUID(),$this->Quote($dt));
$exists = $this->IsSessionExists($id);
if (-1 == $exists || 0 == $exists){
$qtext = sprintf("%s,CREATED=%d",$qtext,intval(time() / 60));
}
if (0 != $exists){
// Обновление
$qtext = sprintf("UPDATE sessions SET %s WHERE SID='%s'",
$qtext,$this->Quote($id));
}else{
// Вставка
$qtext = sprintf("INSERT INTO sessions SET SID='%s',%s",
$this->Quote($id),$qtext);
}
if (!$this->DoQuery($qtext)){
return false;
}
return true;
}
function DestroySession($id){
$errh = "HttpSession::DestroySession:";
$this->mlog($errh,ML_DEBUG_CALL);
if (!$this->IsConnected()){
$this->mlog("$errh Not connected.");
return false;
}
if (!$this->OnSessionDestroy()){
return false;
}
$qtext = sprintf("DELETE FROM sessions WHERE SID='%s'",
$this->Quote($id));
if (!$this->DoQuery($qtext)){
return false;
}
return true;
}
function GarbageCollector($maxttl){
$errh = "HttpSession::GarbageCollector:";
$this->mlog($errh,ML_DEBUG_CALL);
if (!$this->IsConnected()){
$this->mlog("$errh Not connected.");
return false;
}
$qtext =sprintf("DELETE FROM sessions WHERE LASTACS<%d",
intval((time() - ini_get('session.gc_maxlifetime')) / 60));
if (!$this->DoQuery($qtext)){
return false;
}
return true;
}
function IsVarExists($varname){
if (isset($_SESSION[$varname])){
return true;
}
return false;
}
function SetVar($varname,$value){
$_SESSION[$varname] = $value;
return true;
}
function GetVar($varname){
return $_SESSION[$varname];
}
function IsSessionExists($id){
$errh = "HttpSession::IsSessionExists:";
$this->mlog($errh,ML_DEBUG_CALL);
if (!$this->IsConnected()){
$this->mlog("$errh Not connected.");
return false;
}
$maxttl = intval(ini_get('session.gc_maxlifetime') / 60);
$expire = intval(time() / 60) - $maxttl;
$qtext = sprintf("SELECT LASTACS FROM sessions WHERE SID='%s'",
$this->Quote($id));
if (!$this->Select($qtext)){
return 0;
}
// Проверим кол-во записей в выборке.
if (0 == $this->ResultRows()){
// Запись под указанным SID не найдена.
return 0;
}
$result = $this->NextHashRef();
if (intval((time() - ini_get('session.gc_maxlifetime')) / 60)
> $result['LASTACS'])
{
// Запись просрочена.
return -1;
}
return 1;
}
function BeginSession(){
$errh = "HttpSession::BeginSession:";
$this->mlog($errh,ML_DEBUG_CALL);
ini_set('session.save_handler','user');
ini_set('session.gc_probability',15);
ini_set('session.name','SID');
session_set_save_handler(
array(& $this, 'OpenSession'),
array(& $this, 'CloseSession'),
array(& $this, 'ReadSession'),
array(& $this, 'WriteSession'),
array(& $this, 'DestroySession'),
array(& $this, 'GarbageCollector')
);
session_start();
$this->OnReady();
return true;
}
// virtual:
function OnSessionOpen(){
$errh = "HttpSession::OnSessionOpen:";
$this->mlog($errh,ML_DEBUG_CALL);
return true;
}
function OnSessionClose(){
$errh = "HttpSession::OnSessionClose:";
$this->mlog($errh,ML_DEBUG_CALL);
// We need to update profile record at every page
$this->UpdateProfile();
return true;
}
function OnSessionRead(){
$errh = "HttpSession::OnSessionRead:";
$this->mlog($errh,ML_DEBUG_CALL);
// Counters increment
$this->_profile_fields['PAGES_CNT'] ++;
$this->_profile_fields['ACS_TIME'] = intval(time() / 60);
return true;
}
function OnSessionWrite(){
$errh = "HttpSession::OnSessionWrite:";
$this->mlog($errh,ML_DEBUG_CALL);
return true;
}
function OnSessionDestroy(){
$errh = "HttpSession::OnSessionDestroy:";
$this->mlog($errh,ML_DEBUG_CALL);
return true;
}
function OnReady(){
$errh = "HttpSession::OnReady:";
$this->mlog($errh,ML_DEBUG_CALL);
return true;
}
}}