<?php
class user {
	var $userid = 0;
	var $name = "Guest";
	var $pass = "";
	var $sessionid = "";
	
	function main() {
		global $_COOKIE, $sql, $ip, $session;
		
		//Put the calue into a variable we can use...
		$tmp = $session;
				
		if($tmp['online'] == "") $this->_new_session(); else {
			$this->_update_session();
		}
		
		$this->_rem_old();
				
		if($tmp['userid'] > 0) {
			$user_info = $sql->query_first("SELECT * FROM iworks_users WHERE userid='$tmp[userid]' AND userpassword='$tmp[userpassword]'");
			$array1 = $tmp['ip'];
			$array2 = $ip;

			$array1 = explode(".",$array1);
			$array2 = explode(".",$array2);
			
			$strTMP1 = $array1[0].".".$array1[1];
			$strTMP2 = $array2[0].".".$array2[1];
			
			$systemerrors .= "<BR>$strTMP1 != $strTMP2<BR>";
			
			if($strTMP1 != $strTMP2) {
				setcookie('slSesIdent','');
				$sql->query("DELETE FROM iworks_useronline WHERE session='$tmp[session]'");
				echo "Hacking attempt!";
				exit();
			}
		} else {
			$user_info['username'] = "Guest";
			$user_info['userid'] = "0";
			$user_info['messagescount'] = "0";
		}
		
		return $user_info;
	}
	
	function _check_sid() {
		if(!$_COOKIE['slSesIdent']) { 
			$sid = $this->_encodestring(microtime());
			$this->$sessionid = $sid;
			return $sid;
		} else {
			$this->$sessionid = $_COOKIE['slSesIdent'];
		}
	}
	
	function _get_session() {
		global $sql, $ip;
		return $sql->query_first("SELECT * FROM iworks_useronline WHERE session='".$this->$sessionid."' ORDER BY userid DESC LIMIT 1");
	}
	
	function _update_session($forced=0) {
		global $sql, $ip, $file, $sid;
		$tmp['userid'] = $this->userid;
		$tmp['name'] = $this->name;
		$tmp['pass'] = $this->pass;
		
		if($forced == 1) $updatepass = ", userpassword='$tmp[pass]', userid='".$tmp['userid']."', username='".$tmp['name']."' ";
		
		$sql->query("UPDATE iworks_useronline SET online='".time()."', ip='$ip', location='$file'$updatepass WHERE session='".$sid."'");
	}
	
	function _new_session() {
		global $sql, $ip, $sid;
		$sql->query("INSERT INTO iworks_useronline (online,ip,session) VALUES ('".time()."','$ip','".$sid."')");
	}
	
	function _rem_old() {
		global $sql;
		$timeout = 40;
		$deltime = time()-($timeout*60);
		
		$active_users_chk = $sql->query("SELECT * FROM iworks_useronline WHERE online<'$deltime'"); 
		while($row = $sql->fetch_array($active_users_chk)) {
			// Update their lastvisit data...
			$sql->query("UPDATE iworks_users SET lastvisit='".time()."' WHERE userid='".$row['userid']."' AND ip='$row[ip]'");
			// ...Then remove them from the list...
			$iplist .= ",'".$row[ip]."'";
			//$sql->query("DELETE FROM iworks_useronline WHERE userid='".$row['userid']."' AND ip='$row[ip]'");
		}
		$sql->free_result($active_users_chk);
		
		$sql->query("DELETE FROM iworks_useronline WHERE ip IN ('0'".$iplist.")");
	}
	
	function _encodestring($string) {
		$string = base64_encode(pack("H*", sha1("durrrrrrr78943217890324".$string)));
		$string = md5($string);
		
		return $string;
	}
	
	function _logout() {
		global $sql, $user_info, $session;
		$sql->query("DELETE FROM iworks_useronline WHERE session='$session[session]'");
		header("LOCATION: index.fpd");
		exit();
	}
	
	function _login($username,$password) {
		global $sql, $sid;
		$md5ver = md5($password);
		$newver = $this->_encodestring($password);
		
		$usercheck = $sql->query_first("SELECT username, userpassword, userid FROM iworks_users WHERE username='$username' and userpassword='$newver'");
				
		if($usercheck['userid']) {
			//Set the new user data...
			$this->userid = $usercheck['userid'];
			$this->name = $usercheck['username'];
			$this->pass = $usercheck['userpassword'];
			
			//Now force an update for the session...
			$this->_update_session(1);
			
			header("LOCATION: index.fpd");
			exit();
		}
		
		if(!$usercheck['userid']) {
			$oldver = $sql->query_first("SELECT userid FROM iworks_users WHERE username='$username' and userpassword='$md5ver'");
			if($oldver['userid'] > 0) {
				$sql->query("UPDATE iworks_users SET userpassword='$newver' WHERE userid='$oldver[userid]'");
				header("LOCATION: login.fpd?msg=Please re-type your password...");
				exit();
			}
		} 
		
		if(!$usercheck['userid']) {
			header("LOCATION: login.fpd?msg=Log in failed...please try again");
			exit();
		}
		
	}
}
?>
