<?php
//v1.0.4 www.bcoos.net 2006/10 GPL
// www.bcoos.net 2006-09 1.0.2 GPL
//v1.4 www.exoops.info 2006/03 
// $Id: sessions.class.php,v 1.3 2003/03/19 06:57:37 half-dead Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// 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 $bcoosConfig;
	$this->cookie     = $bcoosConfig['session_name'];
	$this->expiretime = $bcoosConfig['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, $_SESSION, $bcoosConfig;
$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 ($bcoosConfig['use_sessions'] == 1) {
		session_register($bcoosConfig['session_name']);
		$_SESSION[$bcoosConfig['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->fetchRow($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, $_SESSION, $bcoosConfig;
$db->query("UPDATE ".$db->prefix("session")." SET time=".time()." WHERE uid=".$this->uid);
if ($bcoosConfig['use_sessions'] == 1) {
	$_SESSION[$bcoosConfig['session_name']] = $this->hash;
	} else {
		cookie($this->cookie, $this->hash, $this->expiretime);
	}
}
//---------------------------------------------------------------------------------------//
} // END XOOPSUSERSESSION
// ------------------------------------------------------------------------- //
}
?>
