<?php
/**
* PAjFF session handling
*
* <p>This part handles sessions. It creates new ones or recreates old ones.
* A "garbage collector" is included to clear unused main sessions as they are
* the only ones that can't be closed. :)</p>
*
* {@link http://sourceforge.net/projects/pajff Project home}
*
* @package PAjFF
* @license http://www.gnu.org/copyleft/gpl.html GNU/GPL
* @author Atanas Markov hide@address.com
* @version 0.0.2
* @copyright 2006 by Atanas Markov
*
* @subpackage pajff_session
**/
include_once('pajff_common.php');
/**
* Does an event with this id exist.
* Useful when the user has pressed a close button many times. :)
*
* @param sting $id event id
*
*/
function pajffSessionExists($id){
if ($id) {
global $pajffdefaultdatabase;
$pajffdefaultdatabase->setQuery( "SELECT * "
. "\nFROM pajff_ses"
. "\nWHERE id='$id'" );
$tmplist = $pajffdefaultdatabase->loadObjectList();
if ($tmplist){
return true; //event found
} else {
return false;
}
} else {
return false;
}
}
/**
* Session class
*
* @package PAjFF
* @subpackage pajff_session
*/
class pajff_session{
/**
* Database to store sessions in
* Still Joomla-aware
*
* @var database
*/
protected $database;
/**
* Parrent session ID
*
* @var string
*/
protected $parentsession;
/**
* Session ID
*
* @var string
*/
protected $sessionid;
/**
* A string to represent last session activity time. Used in the garbage
* collector.
*
* @var string
*/
protected $lastactivity;
/**
* Gabage collector. It erases unused sessions older than 3 hrs
*
*/
protected function garbageCollect(){
//delete old sessions. maybe older than 3-4 hours that haven't been deleted by the program
$oldtime = time() - (3 * 60 * 60); //3 hrs ago
$this->database->setQuery( "SELECT * "
. "\nFROM pajff_ses"
. "\nWHERE lastactivity < $oldtime" );
$tmplist = $this->database->loadObjectList();
if ($tmplist){
foreach ($tmplist as $tmpses) {
$this->database->setQuery( "delete from pajff_ses WHERE id='$tmpses->id'" );
if (!$this->database->query()) {
}
$this->database->setQuery( "delete from pajff_sesvars WHERE sesid='$tmpses->id'" );
if (!$this->database->query()) {
}
}
}
}
/**
* Loads a session from database
*
* @param string $id
*/
public function loadSession($id){
if ($id) {
//An old session is to be loaded
$this->database->setQuery( "SELECT * "
. "\nFROM pajff_ses"
. "\nWHERE id='$id'" );
$tmplist = $this->database->loadObjectList();
if ($tmplist){
$tmpses = $tmplist[0];
} else {
$tmpses = new stdClass();
}
$this->sessionid = $tmpses->id;
$this->parentsession= $tmpses->parentid;
if ($tmpses->id) {
//load variables from database into session
$this->database->setQuery( "SELECT varname, varvalue "
. "\nFROM pajff_sesvars"
. "\nWHERE sesid='$id'" );
$tmplist = $this->database->loadObjectList();
foreach ($tmplist as $tmpvar) {
$key= $tmpvar->varname;
$value= $tmpvar->varvalue;
$this->$key = unserialize(base64_decode($value));
}
}
}
}
/**
* Clear a session from database
*
* @param string $id
*/
public function clearSession($id=null){
if ($id) {
$this->database->setQuery( "delete from pajff_ses WHERE id='$id'" );
if (!$this->database->query()) {
}
$this->database->setQuery( "delete from pajff_sesvars WHERE sesid='$id'" );
if (!$this->database->query()) {
}
} else {
$this->database->setQuery( "delete from pajff_ses WHERE id='$this->sessionid'" );
if (!$this->database->query()) {
}
$this->database->setQuery( "delete from pajff_sesvars WHERE sesid='$this->sessionid'" );
if (!$this->database->query()) {
}
}
}
/**
* Save a session into database
*
*/
public function saveSession(){
$this->clearSession();
$this->database->setQuery( "insert into pajff_ses ".
"\n(id, parentid, lastactivity)".
"\nvalues ('$this->sessionid','$this->parentsession','$this->lastactivity')" );
if (!$this->database->query()) {
//error handler to be placed here
}
$valarr = get_object_vars($this);
foreach ($valarr as $key => $value) {
if ($key!='database'&&$key!='sessionform') {
//do not save the database object itself :)
$this->database->setQuery( "insert into pajff_sesvars ".
"\n(sesid, varname, varvalue)".
"\nvalues ('$this->sessionid','$key','".base64_encode(serialize($value))."')" );
if (!$this->database->query()) {
//error handler to be placed here
print "err";
print_r($this->database);
}
}
}
}
/**
* Create a new session object. It may be a new session or an old one
*
* @param pajff_session $parentsession
* @param database $database
* @param string $id
*
* Recreate an old session using default database
* <code>
* $session= new pajff_session(null,null,'123');
* </code>
* Create a child session using default database
* <code>
* $session= new pajff_session($session,null);
* </code>
*/
public function __construct($parentsession=null, $database=null, $id=null) {
if ($database){
$this->database = $database;
} else {
global $pajffdefaultdatabase;
$this->database = $pajffdefaultdatabase;
}
global $pajffcharset;
if ($pajffcharset) {
$this->database->setQuery( "set names '$pajffcharset'" );
$this->database->query();
}
$this->sessionid = null;
if (is_a($parentsession, 'pajff_session')) {
$this->parentsession = $parentsession->sessionID();
} else {
$this->parentsession = $parentsession;
}
if ($id) {
//load an old session
$this->loadSession($id);
}
if (!$this->sessionid) {
//new session id
//Session id is a random number + microtime
$this->sessionid = pajff_microtime_fmt().mt_rand();
}
$this->lastactivity = time();
$this->garbageCollect();
}
/**
* If anything to be done on destruction. '
* Not used
*
*/
public function __destruct() {
//Not implemented yet. Must decide whether to save the session and so on :(
}
/**
* A method to return session ID
*
* @return string
*/
public function sessionID(){
//get the session id
return $this->sessionid;
}
/**
* A method to return parrent session ID
*
* @return string
*/
public function parentID(){
return $this->parentsession;
}
}
//print "pajff_session";
?>