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

Размер файла: 18.4Kb
<?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           |
// +----------------------------------------------------------------------+
// | functions.php                                                        |
// | Date Started: February 18, 2003                                      |
// | Last Modified: April 13, 2003                                        |
// +----------------------------------------------------------------------+
$version="2.0.2";
if(phpversion() <= "4.2.0"){
	if(is_array($_SERVER)){
		extract($_SERVER, EXTR_PREFIX_SAME, "server");
	}
	if(is_array($_GET)){
		extract($_GET, EXTR_PREFIX_SAME, "get");
	}
	if(is_array($_POST)){
		extract($_POST, EXTR_PREFIX_SAME, "post");
	}
	if(is_array($_COOKIE)){
		extract($_COOKIE, EXTR_PREFIX_SAME, "cookie");
	}
	if(is_array($_FILES)){
		extract($_FILES, EXTR_PREFIX_SAME, "file");
	}
	if(is_array($_ENV)){
		extract($_ENV, EXTR_PREFIX_SAME, "env");
	}
	if(is_array($_REQUEST)){
		extract($_REQUEST, EXTR_PREFIX_SAME, "request");
	}
	if(is_array($_SESSION)){
		extract($_SESSION, EXTR_PREFIX_SAME, "session");
	}
}
class Mysql {
	var $mysql_username;
	var $mysql_password;
	var $mysql_db;
	var $mysql_host;
	var $result_id;
	var $link_id;
	var $dbconnect;
	var $num_rows;
	var $query_count=0;
//*************************************************/
// Connects to MySQL
//*************************************************/
	function Connect() {
		global $CONFIG;
		$this->mysql_username=$CONFIG['mysql_username'];
		$this->mysql_password=$CONFIG['mysql_password'];
		$this->mysql_db=$CONFIG['mysql_db'];
		$this->mysql_host=$CONFIG['mysql_host'];
		$this->link_id=mysql_connect($this->mysql_host,$this->mysql_username,$this->mysql_password);
		if (!$this->link_id) {
			$ffdb->Error("Unable to connect to mysql server");
			exit();
		}
		$this->dbconnect=mysql_select_db($CONFIG['mysql_db']);
		if (!$this->dbconnect) {
			$ffdb->Error("Unable to select database");
			exit();
		}
	}
//*************************************************/
// Queries Database
//*************************************************/
	function Query($sql) {
		$this->result_id=mysql_query($sql) or die("SQL Used: $sql<br>All the sql you used so far has been $this->sql".mysql_error()."<br>");
		$this->query_count++;
		$this->sql .="$sql<br>";
	}
//*************************************************/
// Gets the Number of MySQL Rows
//*************************************************/
	function NumRows() {
		$this->num_rows=mysql_num_rows($this->result_id);
	}
//*************************************************/
// Free the MySQL Slaves !
//*************************************************/
	function FreeResult() {
		mysql_free_result($this->result_id);
	}
//*************************************************/
// Closing MySQL link I guess the program is done?
//*************************************************/
	function Close() {
		mysql_close($this->link_id);
	}
//*************************************************/
// Makes the Insert Strings
//*************************************************/
	function MakeInsertString($data) {
		$this->insert_fields="";
		$this->insert_values="";
		foreach ($data as $k => $v) {
			$this->insert_fields.="$k,";
			$this->insert_values.="'$v',";
		}
		$this->insert_fields=substr($this->insert_fields,0,strlen($this->insert_fields)-1);
		$this->insert_values=substr($this->insert_values,0,strlen($this->insert_values)-1);
	}
//*************************************************/
// Makes the Update Strings
//*************************************************/
	function MakeUpdateString($data) {
		$this->update_string="";
		foreach ($data as $k => $v) {
			$this->update_string.=$k."='".$v."',";
		}
		$this->update_string=substr($this->update_string,0,strlen($this->update_string)-1);
	}
}
//*************************************************/
// Functions Class Used for useful Things
//*************************************************/
class Ffdb {
	var $most_users;
	var $most_users_time;
	var $members;
	var $files;
	var $categorys;
	var $sub_catorys;
	function Ffdb() {
		$this->time=time();
	}
	function Stats() {
		global $mysql;
		$mysql->Query("SELECT * FROM ffdb_stats");
		$show_stats=mysql_fetch_array($mysql->result_id);
		$this->most_users=$show_stats[most_users_online];
		$this->most_users_time=$show_stats[most_users_online_time];
	}
//*************************************************/
// Adds slashes to an array
//*************************************************/
	function slash_array(&$array){
		reset($array);
		while(list($key,$val)=each($array)){
			if(is_string($val)) {
				$array[$key]=addslashes($val);

			} elseif(is_array($val)) {
				$array[$key]=$this->slash_array($val);
			}
		}
		return $array;
	}
//*************************************************/
// Strips Slashes from an array
//*************************************************/
	function strip_array(&$array) {
		reset($array);
		while(list($key,$val)=each($array)) {
			if(is_string($val)) {
				$array[$key]=stripslashes($val);
			} elseif(is_array($val)) {
				$array[$key]=$this->strip_array($val);
			}
		}
		return $array;
	}
//*************************************************/
// HTML Special Chars an array
//*************************************************/
	function html_array(&$array){
		reset($array);
		while(list($key,$val)=each($array)){
			if(is_string($val)) {
				$array[$key]=HTMLSpecialChars($val);

			} elseif(is_array($val)) {
				$array[$key]=$this->html_array($val);
			}
		}
		return $array;
	}
//*************************************************/
// Turns \n into <BR> for an entire Array
//*************************************************/
	function newline_array(&$array){
		reset($array);
		while(list($key,$val)=each($array)){
			if(is_string($val)) {
				$array[$key]=str_replace("\n","<BR>", $val);

			} elseif(is_array($val)) {
				$array[$key]=$this->newline_array($val);
			}
		}
		return $array;
	}
//*************************************************/
// Turns <BR>'s into \n used for forms
//*************************************************/
	function newline_array_back(&$array){
		reset($array);
		while(list($key,$val)=each($array)){
			if(is_string($val)) {
				$array[$key]=str_replace("<BR>","\n", $val);

			} elseif(is_array($val)) {
				$array[$key]=$this->newline_array($val);
			}
		}
		return $array;
	}
//*********************************************/
// Divides mysql data finds the number of pages
// and the query data
//*********************************************/
	function DivideData($query,$page,$data_per_page) {
		global $mysql;
		$mysql->Query($query);
		$mysql->NumRows();
		if ($mysql->num_rows!=0) {
			$this->data_bottom=($page-1)*$data_per_page;
			$this->data_top=$data_per_page;
			$num_data=$mysql->num_rows;
			$num_pages=$num_data/$data_per_page;
			$this->num_pages=ceil($num_pages);
		} else {
			$this->num_pages=0;
			$this->data_bottom=0;
			$this->data_top=0;
			$this->hasdata="No";
		}
	}
//*********************************************/
// Makes a nice looking pagelist 
//*********************************************/
	function MakePages($page_string,$current_page_string,$first_page_string,$last_page_string,$page,$num_pages) {
		$this->pages="";
		for ($eachpage=1; $eachpage<=$num_pages; $eachpage++) {
			if ($page == $eachpage) {
				$this->pages .=$current_page_string;
				$this->pages=str_replace("{page}",$page,$this->pages);
			} else {
				if ($eachpage>=$page-3 && $eachpage>1 && $num_page_first_displays<3) {
					$num_page_first_displays++;
					$this->pages .= "$page_string";
				}elseif ($eachpage<$page && $page>4) {
					if ($first_page !="Yes") {
						$first_page="Yes";
						$this->pages .= "$first_page_string";
					}
					if ($first_last !="Yes") {
						$first_last="Yes";
						$this->pages .="... ";
					}
					
				}elseif ($eachpage<=$page+3 && $eachpage>=$page-3) {
					$this->pages .= "$page_string"; 
				}elseif ($eachpage==$num_pages) {
					if ($last_last !="Yes" && $page>4) {
						$last_last="Yes";
						$this->pages .="... ";
					}
					$this->pages .="$last_page_string";
					
				}elseif ($eachpage+1>=$page && $page==$num_pages) {
					$old_each_page=$eachpage;
					$eachpage-=3;
					$this->pages .= "$page_string";
					$eachpage-=2;
					$this->pages .= "$page_string";
					$eachpage-=1;
					$this->pages .= "$page_string";
					$eachpage=$old_each_page;
				}
				$this->pages=str_replace("{page}",$page,$this->pages);
				$this->pages=str_replace("{eachpage}",$eachpage,$this->pages);
			}
		}
	}
//**************************************************************/
// Starts our little clock or Timer if you will
//*************************************************************/
	function StartTimer() {
        global $start_time;
        $mtime = microtime ();
        $mtime = explode (' ', $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $start_time = $mtime;
    }
//**************************************************************/
// Timer has stopped what was your time?
//*************************************************************/
    function StopTimer() {
        global $start_time;
        $mtime = microtime ();
        $mtime = explode (' ', $mtime);
        $mtime = $mtime[1] + $mtime[0];
        $end_time = $mtime;
        $this->total_time = round (($end_time - $start_time), 5);
    }
//**************************************************************/
// Error Template that's used in the entire script
//*************************************************************/
	function Error($error) {
		global $tpl, $online;
		$tpl->LoadTemplate("Error","templates/$online->skin/error.tpl");
		$template_array = array(
			'error'            => $error
		);
		$tpl->ParseTemplate("Error",$template_array,"No");
		$tpl->PrintTemplate("Error");
		$this->Footer();
		exit();
	}
//**************************************************************/
// Success Template that is used in the entire script
//*************************************************************/
	function Success($success) {
		global $tpl, $online;
		$tpl->LoadTemplate("Success","templates/$online->skin/success.tpl");
		$template_array = array(
			'success'            => $success
		);
		$tpl->ParseTemplate("Success",$template_array,"No");
		$tpl->PrintTemplate("Success");
	}
//**************************************************************/
// Displays the Header and the stuff for the header
//*************************************************************/
	function Header($user_id) {
		global $tpl, $mysql,$CONFIG,$online;
		if (isset($user_id)) {
			$online->user_id=$user_id;
		}
		// If the user is in a category
		if (preg_match("/action=category/",$online->page_location)) {
			$mysql->Query("SELECT * FROM ffdb_categorys WHERE category_id=".$_REQUEST['id']."");
			$show_category=mysql_fetch_array($mysql->result_id);
			$navigation="<a href=\"filedb.php\">".$CONFIG['archive_name']."</a> » $show_category[category_name]";
		// Sub Category Thing
		}elseif (preg_match("/action=sub_category/",$online->page_location)) {
			$mysql->Query("SELECT ffdb_categorys.*,ffdb_sub_categorys.* FROM ffdb_categorys,ffdb_sub_categorys WHERE ffdb_categorys.category_id=ffdb_sub_categorys.category_id AND ffdb_sub_categorys.sub_category_id=".$_REQUEST['id']."");
			$show_category=mysql_fetch_array($mysql->result_id);
			$navigation="<a href=\"filedb.php\">".$CONFIG['archive_name']."</a> » <a href=\"filedb.php?action=category&id=$show_category[category_id]\">$show_category[category_name]</a> » $show_category[sub_category_name]";
		}elseif (preg_match("/action=file/",$online->page_location)) {
			$mysql->Query("SELECT ffdb_categorys.*, ffdb_sub_categorys.*, ffdb_files.* FROM ffdb_categorys,ffdb_sub_categorys,ffdb_files WHERE ffdb_categorys.category_id=ffdb_sub_categorys.category_id AND ffdb_files.sub_category_id=ffdb_sub_categorys.sub_category_id AND ffdb_files.file_id=".$_REQUEST['id']."");
			$show_category=mysql_fetch_array($mysql->result_id);
			$navigation="<a href=\"filedb.php\">".$CONFIG['archive_name']."</a> » <a href=\"filedb.php?action=category&id=$show_category[category_id]\">$show_category[category_name]</a> » <a href=\"filedb.php?action=sub_category&id=$show_category[sub_category_id]\">$show_category[sub_category_name]</a> » $show_category[file_name]";
		} else {
			$navigation="".$CONFIG['archive_name']."";
		}
		$tpl->LoadTemplate("Header","templates/$online->skin/header.tpl");
		if ($online->user_id !=$CONFIG['guest_id'] && isset($online->user_id)) {
			$option_list ="[ <a href=\"filedb.php?action=user_cp\">User CP</a> | <a href=\"filedb.php?action=logout\">Log Out</a> |";
		} else {
			$option_list ="[ <a href=\"filedb.php?action=login\">Login</a> | <a href=\"filedb.php?action=register\">Register</a> |";
		}
		$option_list .=" <a href=\"filedb.php?action=memberlist\">Member List</a> | <a href=\"filedb.php?action=stats\">Stats</a> | <a href=\"filedb.php?action=search\">Search</a> | <a href=\"filedb.php\"> Home</a> ]<br>";
			$header_array = array(
				'option_list'          => $option_list,
				'navigation'		   =>$navigation
			);
		$tpl->ParseTemplate("Header",$header_array,"No");
		$tpl->PrintTemplate("Header");
	}
//**************************************************************/
// Displays the footer and the stuff for the footer
//*************************************************************/
	function Footer() {
		global $tpl, $mysql, $online, $version;
		$tpl->LoadTemplate("Footer","templates/$online->skin/footer.tpl");
		$this->StopTimer();
		$footer_array = array(
			'num_queries'          => $mysql->query_count,
			'generated'            => $this->total_time,
		);
		$tpl->ParseTemplate("Footer",$footer_array,"No");
		$tpl->PrintTemplate("Footer");

		// Well you found the copyright don't remove it I just didn't want to make some kind of 
		// 1337 thing to prevent you from changing it.  Removing it defeats the purpose of it 
		// being free if you want it removed contact me and we can arrange something like
		// paying for the copyright to be removed
		echo "<p align=\"center\"><p align=\"center\"><font face=\"Verdana\"><font size=\"1\">Powered by 
				<a target=\"_blank\" href=\"http://fscripts.com/\">Fantastic File Database</a> v$version © 
				2003 </font> <a target=\"_blank\" href=\"http://fscripts.com\"><font size=\"1\">Fantastic Scripts</font></a></font></p>";
		//echo $mysql->sql;
	}
}
//**************************************************************/
// Template Class Basically does our template stuff you know
//*************************************************************/
class Templates {

//**************************************************************/
// Loads up the template and assigns some variables so we know
// what we are doing later with other functions
//**************************************************************/
	function LoadTemplate($template_name,$file) {
		// Gives the Template an ID #
		$this->template_id++;
		// Gives Filename #id filename
		$this->filename[$this->template_id]=$file;
		// Template name by ID #
		$this->templatename[$this->template_id]=$template_name;
		// Used to get id# back
		$this->get_template_id[$template_name]=$this->template_id;
		// Open File Get the content and such
		$this->content[$this->template_id]=file($this->filename[$this->template_id]);
		$this->content[$this->template_id]=implode("",$this->content[$this->template_id]);
	}

//**************************************************************/
// Parses the Templates turns {$var} into $var
//**************************************************************/
	function ParseTemplate($template_name,$data,$list_loop) {
		$template_id=$this->get_template_id[$template_name];
		if (!$template_id) {
			$this->Error("No Template with that name exists and you choose $template_name");
		}
		if (isset($data)) {
			if ($list_loop=="Yes") {
				$this->final_content[$template_id].=$this->content[$template_id];
				foreach ($data as $k => $v) {
					$this->final_content[$template_id]=str_replace("{".$k."}", $v,$this->final_content[$template_id]);
				}
			} else {
				$this->final_content[$template_id]=$this->content[$template_id];
				foreach ($data as $k => $v) {
					$this->final_content[$template_id]=str_replace("{".$k."}", $v,$this->final_content[$template_id]);
				}
			}
		} else {
			$this->final_content[$template_id]=$this->content[$template_id];
		}
	}
//**************************************************************/
// Prints out the final outcome and will only print if the
// data has been parsed first
//**************************************************************/
	function PrintTemplate($template_name) {
		$template_id=$this->get_template_id[$template_name];
		if (empty($this->final_content[$template_id])) {
			$this->Error("You need to Parse Before Printing you tryed to print $template_name");
		} else {
			echo $this->final_content[$template_id];
			return;
		}
	}
//**************************************************************/
// Saves the Final Template data basically used to save it in
// variables for later use although it's not needed I thought
// might as well have it anyways
//**************************************************************/
	function SaveTemplate($template_name) {
		$template_id=$this->get_template_id[$template_name];
		if (!$this->final_content[$template_id]) {
			$this->Error("You need to Parse Before Printing");
		} else {
			return $this->final_content[$template_id];
		}
	}
//**************************************************************/
// Error Function just used for errors inside the template
// class
//**************************************************************/
	function Error($error) {
		echo $error;
		exit();
	}
}
?>