Просмотр файла FFDB/online.php

Размер файла: 12.29Kb
<?php
// +----------------------------------------------------------------------+
// |                      Fantastic File Database                         |
// +----------------------------------------------------------------------+
// |                          By Tony Baird                               |
// |                  Copyright (c) 2003 Fantastic Scripts                |
// |                          http://fscripts.com                         |
// +----------------------------------------------------------------------+
// | Fantastic File Database Can be modified freely as long as copyright  | 
// | is intact and this is left at the top of every source file           |
// +----------------------------------------------------------------------+
// | online.php                                                           |
// | Date Started: February 18, 2003                                      |
// | Last Modified: March 31, 2003                                        |
// +----------------------------------------------------------------------+
Class Online {
	var $last_visit;
	var $username;
	var $user_level;
	var $user_id;
	var $ip;
	var $page_location;
	var $in_category;
	var $in_sub_category;
	var $in_file;

//*************************************************/
// Executed Everytime sets them as online and
// figures out which Online method we are using
//*************************************************/
	function Online() {
		global $mysql,$CONFIG,$ffdb;
		// Ok we need to refine the session stuff
		session_name("ffdb");
		session_start();
		$this->session_id=session_id();
		$this->ip=$_SERVER['REMOTE_ADDR'];
		$this->page_location=$_SERVER['REQUEST_URI'];
		if (preg_match("/action=category/",$_SERVER['REQUEST_URI'])) {
			$this->in_category=substr($_SERVER['REQUEST_URI'],-1);
		}elseif (preg_match("/action=sub_category/",$_SERVER['REQUEST_URI'])) {
			$this->in_sub_category=substr($_SERVER['REQUEST_URI'],-1);
		}elseif (preg_match("/action=file/",$_SERVER['REQUEST_URI'])) {
			$this->in_file=substr($_SERVER['REQUEST_URI'],-1);
		}
		if (empty($_COOKIE['username'])) {
			$this->user_id=$CONFIG[guest_id];
			$this->skin_id=$CONFIG['default_skin'];
		} else {
			$mysql->Query("SELECT * FROM ffdb_users WHERE username='".$_COOKIE['username']."' AND password='".$_COOKIE['password']."'");
			$show_user=mysql_fetch_array($mysql->result_id);
			if (empty($show_user[username])) {
				setcookie("username",$_POST['username'],0);
				setcookie("password",md5($_POST['password']),0);
				$ffdb->Header();
				$ffdb->Error("Password incorrect based on cookie please relogin");
			}
			$this->last_visit=$show_user[last_visit];
			$this->username=$show_user[username];
			$this->email=$show_user[email];
			$this->user_level=$show_user[user_level];
			$this->user_id=$show_user[user_id];
			$this->password=$show_user[password];
			$this->skin_id=$show_user[skin_id];
		}
		$mysql->Query("SELECT ffdb_sessions.*,ffdb_users.* FROM ffdb_sessions,ffdb_users WHERE ffdb_sessions.user_id=ffdb_users.user_id AND ffdb_sessions.session='$this->session_id'");
		$show_online_user=mysql_fetch_array($mysql->result_id);
		if (empty($show_online_user[session])) {
			$this->MakeSession();
		} else {
			$this->UpdateSession();
		}
		$mysql->Query("SELECT * FROM ffdb_skins WHERE skin_id=$this->skin_id");
		$show_skin=mysql_fetch_array($mysql->result_id);
		$this->skin=$show_skin[skin_directory];
		$mysql->FreeResult();
	}
//*************************************************/
// Make Session function makes the mysql session
// if the user doesn't have one
//*************************************************/

	function MakeSession() {
		global $mysql,$CONFIG,$ffdb;
		// Ok this needs to be checked incase people are viewing the pages with multiple browsers
		$mysql->Query("SELECT * FROM ffdb_sessions WHERE ip='$this->ip'");
		$show_ip=mysql_fetch_array($mysql->result_id);
		if (isset($show_ip[session])) {
			$this->old_session=$show_ip[session];
			$this->UpdateSession();
		} else {
			$insert_array = array(
				'session'          => $this->session_id,
				'user_id'          => $this->user_id,
				'ip'               => $this->ip,
				'last_seen'        => $ffdb->time+$CONFIG[online_time],
				'browser'		   => $_SERVER['HTTP_USER_AGENT'],
				'in_category'      => $this->in_category,
				'in_sub_category'  => $this->in_sub_category,
				'in_file'          => $this->in_file,
				'page_location'	   => $_SERVER['REQUEST_URI']
			);
			$mysql->MakeInsertString($insert_array);
			$mysql->Query("INSERT INTO ffdb_sessions ($mysql->insert_fields) VALUES ($mysql->insert_values)");
			if ($this->user_id !=$CONFIG['guest_id']) {
				$update_user_array = array(
					'last_visit'          => $ffdb->time,
					'last_ip'             => $this->ip
				);
				$mysql->MakeUpdateString($update_user_array);
				$mysql->Query("UPDATE ffdb_users SET $mysql->update_string WHERE user_id=$this->user_id");
			}
		}
	}	
//*************************************************/
// Updates a Users sessions if they have one
//*************************************************/
	function UpdateSession() {
		global $mysql,$CONFIG,$ffdb;
		if (isset($this->old_session)) {
			$where_session=$this->old_session;
		} else {
			$where_session=$this->session_id;
		}
		$update_array = array(
			'ip'               => $this->ip,
			'user_id'          => $this->user_id,
			'last_seen'        => $ffdb->time+$CONFIG[online_time],
			'browser'		   => $_SERVER['HTTP_USER_AGENT'],
			'in_category'      => $this->in_category,
			'in_sub_category'  => $this->in_sub_category,
			'in_file'          => $this->in_file,
			'page_location'	   => $_SERVER['REQUEST_URI']
		);
		$mysql->MakeUpdateString($update_array);
		$mysql->Query("UPDATE ffdb_sessions SET $mysql->update_string WHERE session='$where_session'");
	}
//*************************************************/
// Main Page online display
//*************************************************/
	function MainOnline() {
		global $tpl, $mysql, $online, $CONFIG,$ffdb;
		$ffdb->Stats();
		$tpl->LoadTemplate("Online Main","templates/$online->skin/online_main.tpl");
		$mysql->Query("SELECT ffdb_sessions.*,ffdb_users.* FROM ffdb_sessions,ffdb_users WHERE ffdb_sessions.user_id=ffdb_users.user_id");
		$num_guests=0;
		$num_members=0;
		while ($show_online=mysql_fetch_array($mysql->result_id)) {
			if ($show_online[last_seen]<$ffdb->time) {
				$inactive="Yes";
			} else {
				if ($show_online[user_id]==$CONFIG['guest_id']) {
					$num_guests++;
				} else {
					$num_members++;
					$member_online_list .="<a href=\"filedb.php?action=member&id=$show_online[user_id]\">$show_online[username]</a>,";
				}
			}
		}
		$currently_active=$num_guests+$num_members;
		if ($currently_active>$ffdb->most_users) {
			$update_settings_array = array(
				'most_users_online_time'       => $ffdb->time,
				'most_users_online'  => $currently_active
			);
			$mysql->MakeUpdateString($update_settings_array);
			$mysql->Query("UPDATE ffdb_stats SET $mysql->update_string");

		}
		$member_online_list=substr($member_online_list,0,strlen($member_online_list)-1);
		$most_online_date=date($CONFIG[date_format],$ffdb->most_users_time);

		$online_main_array = array(
			'num_members'          => $num_members,
			'num_guests'          => $num_guests,
			'currently_active'          => $currently_active,
			'member_online_list'          => $member_online_list,
			'record_most_users_online'     => $ffdb->most_users,
			'record_most_users_online_date'     => $most_online_date
		);
		$tpl->ParseTemplate("Online Main",$online_main_array,"No");
		$tpl->PrintTemplate("Online Main");
		if ($inactive=="Yes") {
			$this->DeleteInactive();
		}
	}

//*************************************************/
// Category Page displaying of who's online there
//*************************************************/
	function CurrentlyBrowsing($type) {
		global $tpl, $mysql, $online, $ffdb,$CONFIG;
		$tpl->LoadTemplate("currently_browsing","templates/$online->skin/currently_browsing.tpl");
		switch ($type) {
			case "Category":
				$where_online="in_category=".$_REQUEST['id']."";
				break;
			case "SubCategory":
				$where_online="in_sub_category=".$_REQUEST['id']."";
				break;
			case "File":
				$where_online="in_file=".$_REQUEST['id']."";
				break;
		}
		$mysql->Query("SELECT ffdb_sessions.*,ffdb_users.* FROM ffdb_sessions,ffdb_users WHERE ffdb_sessions.user_id=ffdb_users.user_id AND $where_online");
		$num_guests=0;
		$num_members=0;
		while ($show_online=mysql_fetch_array($mysql->result_id)) {
			if ($show_online[last_seen]<$ffdb->time) {
				$inactive="Yes";
			} else {
				if ($show_online[user_id]==$CONFIG['guest_id']) {
					$num_guests++;
				} else {
					$num_members++;
					$member_online_list .="<a href=\"filedb.php?action=member&id=$show_online[user_id]\">$show_online[username]</a>,";
				}
			}
		}
		$currently_active=$num_guests+$num_members;
		$member_online_list=substr($member_online_list,0,strlen($member_online_list)-1);
		if (empty($member_online_list)) {
			$member_online_list="None";
		}
		$currently_browsing_array = array(
			'num_guests'=> $num_guests,
			'num_members'=> $num_members,
			'currently_active'=> $currently_active,
			'member_online_list'=> $member_online_list,
			'type'=> $type
		);
		$tpl->ParseTemplate("currently_browsing",$currently_browsing_array,"No");
		$tpl->PrintTemplate("currently_browsing");
		if ($inactive=="Yes") {
			$this->DeleteInactive();
		}
	}
	//*************************************************/
	// Nothing Special just deletes the inactive users
	//*************************************************/
	function DeleteInactive() {
		global $mysql, $ffdb;
		$mysql->Query("DELETE FROM ffdb_sessions WHERE last_seen<$ffdb->time");
	}
	function OnlineList() {
		global $tpl, $mysql, $online, $ffdb,$CONFIG;
		$tpl->LoadTemplate("online_list","templates/$online->skin/online_list.tpl");
		$mysql->Query("SELECT ffdb_sessions.*,ffdb_users.* FROM ffdb_sessions,ffdb_users WHERE ffdb_sessions.user_id=ffdb_users.user_id AND ffdb_sessions.user_id");
		while ($show_list=mysql_fetch_array($mysql->result_id)) {
			// Ok Time to figure out where this user is
			if (preg_match("/action=category/",$show_list[page_location])) {
				$location_id=substr($show_list[page_location],-1);
				$location="Viewing <a href=\"filedb.php?action=category&id=$location_id\">Category #$location_id</a>";
			}elseif (preg_match("/action=sub_category/",$show_list[page_location])) {
				$location_id=substr($show_list[page_location],-1);
				$location="Viewing <a href=\"filedb.php?action=sub_category&id=$location_id\">Sub Category #$location_id</a>";
			}elseif (preg_match("/action=file/",$show_list[page_location])) {
				$location_id=substr($show_list[page_location],-1);
				$location="Viewing <a href=\"filedb.php?action=file&id=$location_id\">File #$location_id</a>";
			}elseif (preg_match("/action=online/",$show_list[page_location])) {
				$location_id=substr($show_list[page_location],-1);
				$location="Viewing <a href=\"filedb.php?action=online\">Who's Online</a>";
			}elseif ($show_list[page_location]=="".$CONFIG['script_dir']."filedb.php") {
				$location="<a href=\"filedb.php\">Viewing Main Page</a>";
			} else {
				$location="Unknown";
			}
			$last_seen=date($CONFIG['date_format'],$show_list[last_seen]);
			// Ok Time to setup the list template with it's variables
			if ($show_list[user_id]==$CONFIG['guest_id']) {
				$username=$show_list[username];
			} else {
				$username="<a href=\"filedb.php?action=member&id=$show_list[user_id]\">$show_list[username]</a>";
			}
			$online_list_array = array(
				'location'=> $location,
				'username'=> $username,
				'user_id'=> $show_list[user_id],
				'last_seen'=>$last_seen
			);
			$tpl->ParseTemplate("online_list",$online_list_array,"Yes");
		}
		$tpl->LoadTemplate("online_list_top","templates/$online->skin/online_list_top.tpl");
		$tpl->LoadTemplate("online_list_bottom","templates/$online->skin/online_list_bottom.tpl");
		$tpl->ParseTemplate("online_list_bottom",$online_list_bottom_array,"No");
		$tpl->ParseTemplate("online_list_top",$online_list_top_array,"No");
		$tpl->PrintTemplate("online_list_top");
		$tpl->PrintTemplate("online_list");
		$tpl->PrintTemplate("online_list_bottom");

	}
}

?>