<?php
// $Id: sessions.class.php,v 1.1 2004/07/11 10:27:18 Farsus Exp $
// -------------------------------------------------------------------------//
//			                 RUNCMS                                         //
//                                                                          //
//	 reliable - Unique - Nocost &  Simplicity & ease off use                //
//                       < http://www.runcms.org >                          //
// -------------------------------------------------------------------------//
// Original Author: Kazumi Ono
// Author Website : http://www.mywebaddons.com/ , http://www.myweb.ne.jp
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //

if (!defined("EXOOPS_SESSION_CLASS_INCLUDED")) {
	define("EXOOPS_SESSION_CLASS_INCLUDED", 1);

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
class XoopsUserSession {

	var $db;
	var $uid;
	var $uname;
	var $hash;
	var $pass;
	var $cookie;
	var $expiretime;
	var $sessionID;


	function XoopsUserSession($sessionID='') {
	global $xoopsConfig;

	$this->cookie     = $xoopsConfig['session_name'];
	$this->expiretime = $xoopsConfig['session_expire'];

	if ( !empty($sessionID) ) {
		$this->sessionID = $sessionID;
	}
	}

//---------------------------------------------------------------------------------------//
/**
* creates new session for user
* and sets a cookie containing the session id
*
* @param type $var description
* @return type description
*/
function store() {
global $db, $HTTP_SESSION_VARS, $xoopsConfig;

$db->query("DELETE FROM ".$db->prefix("session")." WHERE uid=".$this->uid);
$this->hash = md5($this->uid.':'.$this->pass.':'._HTTP_USER_AGENT.':'._REMOTE_ADDR);
if ($db->query("INSERT INTO ".$db->prefix("session")." SET uid=".$this->uid.", uname='".$this->uname."', time=".time().", ip='"._REMOTE_ADDR."', hash='".$this->hash."'")) {
	if ($xoopsConfig['use_sessions'] == 1) {
		session_register($xoopsConfig['session_name']);
		$HTTP_SESSION_VARS[$xoopsConfig['session_name']] = $this->hash;
		} else {
			cookie($this->cookie, $this->hash, $this->expiretime);
		}
	return true;
}


return false;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function setUid($value) {
	$this->uid = $value;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function setUname($value) {
	$this->uname = $value;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function setPass($value) {
	$this->pass = $value;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function uid() {
	return $this->uid;
}

//---------------------------------------------------------------------------------------//
/**
* Description
*
* @param type $var description
* @return type description
*/
function isValid() {
global $db;

$mintime = (time()-$this->expiretime);
$db->query("DELETE FROM ".$db->prefix("session")." WHERE time<$mintime");

$sql = "
	SELECT
	u.uid,
	u.uname,
	u.pass,
	s.hash
	FROM ".$db->prefix("session")." s
	INNER JOIN ".$db->prefix("users")." u ON u.uid=s.uid
	WHERE s.hash='".$this->sessionID."'";

if ( !$result = $db->query($sql) ) {
	return false;
}

list($uid, $uname, $pass, $hash) = $db->fetch_row($result);
if ( !empty($uid) ) {
	if ($this->sessionID == md5($uid.':'.$pass.':'._HTTP_USER_AGENT.':'._REMOTE_ADDR)) {
		$this->uid   = $uid;
		$this->uname = $uname;
		$this->pass  = $pass;
		$this->hash  = $hash;
		return true;
	}
}

return false;
}

//---------------------------------------------------------------------------------------//
/**
* updates the session table
*
* @param type $var description
* @return type description
*/
function update() {
global $db, $HTTP_SESSION_VARS, $xoopsConfig;

$db->query("UPDATE ".$db->prefix("session")." SET time=".time()." WHERE uid=".$this->uid);
if ($xoopsConfig['use_sessions'] == 1) {
	$HTTP_SESSION_VARS[$xoopsConfig['session_name']] = $this->hash;
	} else {
		cookie($this->cookie, $this->hash, $this->expiretime);
	}
}
//---------------------------------------------------------------------------------------//
} // END XOOPSUSERSESSION

// ------------------------------------------------------------------------- //
}
?>
