View file FFDB/file.php

File size: 11.1Kb
<?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           |
// +----------------------------------------------------------------------+
// | file.php                                                             |
// | Date Started: February 18, 2003                                      |
// | Last Modified: March 25, 2003                                         |
// +----------------------------------------------------------------------+
Class File {
	//*************************************************/
    // Just seing what we are doing
    //*************************************************/
    function File() {
        global $tpl,$mysql,$online,$ffdb;
		if ($_REQUEST['action']=="delete_file") {
			$this->ConfirmDeleteFile();
		}elseif ($_REQUEST['action']=="edit_file") {
			$this->EditFileForm();
		}elseif ($_REQUEST['action']=="download_file") {
			$this->LicenseFile();
		}elseif ($_REQUEST['action']=="download_agree") {
			$this->DownloadFile();
		}elseif ($_REQUEST['action']=="rate_file") {
			$this->RateFileForm();
		}elseif ($_REQUEST['rate_file']) {
			$this->RateFile();
		}elseif ($_REQUEST['action']=="friend") {
			$this->SendFriendForm();
		}elseif (isset($_REQUEST['send_friend'])) {
			$this->SendFriendEmail();
		} else {
			$this->DisplayFile();
		}
    }
    //*************************************************/
    // Delete File Confirmation Page
    //*************************************************/
    function ConfirmDeleteFile() {
    }
    //*************************************************/
    // Edit File Form
    //*************************************************/
    function EditFileForm() {
    }
    //*************************************************/
    // Displays the File
    //*************************************************/
    function DisplayFile() {
        global $mysql,$tpl,$ffdb,$online,$CONFIG;
        if (!$_REQUEST['id']) {
            $ffdb->Error("You did not select a File");
		}
        // Loading Our Templates
        $tpl->LoadTemplate("file","templates/$online->skin/file.tpl");
        $mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
        // Setting up the Sub Category List
        $ffdb->strip_array($show_file);
        $date_added=date($CONFIG[date_format],$show_file[date_added]);
		$last_download=date($CONFIG[date_format],$show_file[last_download]);
        if ($show_file[num_ratings]==0) {
			$rating=0;
        } else {
            $rating=round($show_file[total_ratings]/$show_file[num_ratings],2);
        }
		if ($show_file[upload_image]=="Yes") {
			$image="<img src=\"".$CONFIG['images_url']."$show_file[image_filename]\">";
		} elseif ($show_file[image_url]) {
			$image="<img src=\"$show_file[image_url]\">";
		} else {
			$image="";
		}
		if ($show_file[file_size]>1024 && $show_file[file_size]<1048576) {
			$show_file[file_size]=$show_file[file_size]/1024;
			$show_file[file_size]=round($show_file[file_size],2);
			$size_type="KB";
		}elseif ($show_file[file_size]>1048576) {
			$show_file[file_size]=$show_file[file_size]/1024;
			$show_file[file_size]=round($show_file[file_size],2);
			$size_type="MB";
		} elseif($show_file[file_size]>1) {
			$size_type="Bytes";
		}
		$show_file[file_size]=$show_file[file_size]." $size_type";
        $file_array = array(
			'file_name'=> $show_file[file_name],
			'date_added'=> $date_added,
			'last_download'=> $last_download,
			'image'=> $image,
			'downloads' => $show_file[downloads],
			'file_long_description' => $show_file[file_long_description],
			'version' => $show_file[version],
			'filesize' => $show_file[file_size],
			'author' => $show_file[author],
			'email' => $show_file[author_email],
			'site_name' => $show_file[author_site],
			'site_url' => $show_file[author_site_url],
			'rating' => $rating,
			'file_id' => $show_file[file_id],
			'num_comments' => $show_file[num_comments]
		);
        $tpl->ParseTemplate("file",$file_array,"Yes");
        $tpl->PrintTemplate("file");
        $online->CurrentlyBrowsing("File");
    }
	//*************************************************/
    // Looks like our friend is downloading the file
	// time to see if this file has a license or not
    //*************************************************/
	function LicenseFile() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		if (!$_REQUEST['id']) {
            $ffdb->Error("You did not select a File");
		}
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		if ($show_file[license_id]>0) {
			$ffdb->strip_array($show_license);
			$tpl->LoadTemplate("license","templates/$online->skin/license.tpl");
			$mysql->Query("SELECT * FROM ffdb_licenses WHERE license_id=$show_file[license_id]");
			$show_license=mysql_fetch_array($mysql->result_id);
			$file_array = array(
				'license_name'=> $show_license[license_name],
				'license_text'=> $show_license[license_text],
				'file_id'=> $show_file[file_id]
			);
			$tpl->ParseTemplate("license",$file_array,"No");
			$tpl->PrintTemplate("license");
		} else {
			$this->DownloadFile();
		}

	}
	//*************************************************/
    // Downloads the file
    //*************************************************/
	function DownloadFile() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		if (!$_REQUEST['id']) {
            $ffdb->Error("You did not select a File");
		}
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		if ($show_file[upload_file]=="Yes") {
			$file_location=$CONFIG[files_url].$show_file[file_filename];
		} else {
			$file_location=$show_file[file_url];
		}
		$update_file_array = array(
			'downloads'          => $show_file[downloads]+1,
			'last_download'             => time()
		);
		$mysql->MakeUpdateString($update_file_array);
		$mysql->Query("UPDATE ffdb_files SET $mysql->update_string WHERE file_id=$show_file[file_id]");
		header("Location: $file_location");
	}
	//*************************************************/
    // Looks like someone is rating a file
    //*************************************************/
	function RateFileForm() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$tpl->LoadTemplate("rate_file_form","templates/$online->skin/rate_file_form.tpl");
		$rate_file_form_array = array(
			'file_name'=> $show_file[file_name],
			'file_id'=> $show_file[file_id]
		);
		$tpl->ParseTemplate("rate_file_form",$rate_file_form_array,"Yes");
		$tpl->PrintTemplate("rate_file_form");
	}
	//*************************************************/
    // Actually Rates the File
    //*************************************************/
	function RateFile() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		if (empty($online->username)) {
			$ffdb->Error("You need to be logged in to use this feature");
		}
		$mysql->Query("SELECT * FROM ffdb_ratings WHERE file_id=".$_REQUEST['id']." AND user_id=$online->user_id");
		$show_rating=mysql_fetch_array($mysql->result_id);
		if (isset($show_rating[file_id])) {
			$ffdb->Error("You have already rated this file with the ip address $show_rating[ip]");
		}
		// Inserting the rating into the ratings table so this person can't rate the same
		// file twice unless.  Of course some may want some kind of voting spree type thing
		// but not me and what I say is just the way it is
		$insert_rating_array = array(
			'file_id'          => $_POST['id'],
			'user_id'          => $online->user_id,
			'ip'               => $_SERVER['REMOTE_ADDR'],
			'rating'           => $_POST['rating'],
			'time'			   => $ffdb->time
		);
		$mysql->MakeInsertString($insert_rating_array);
		$mysql->Query("INSERT INTO ffdb_ratings ($mysql->insert_fields) VALUES ($mysql->insert_values)");
		// Updating the Files rating info
		$mysql->Query("UPDATE ffdb_files SET total_ratings=total_ratings+".$_POST['rating'].", num_ratings=num_ratings+1 WHERE file_id=".$_POST['id']."");
		$mysql->Query("UPDATE ffdb_stats SET num_ratings=num_ratings+1");
		$mysql->Query("UPDATE ffdb_users SET num_ratings=num_ratings+1 WHERE user_id=$online->user_id");
		$ffdb->Success("You have successfully rated this file");
	}
	//*************************************************/
    // Send to Friend Form
    //*************************************************/
	function SendFriendForm() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		if (empty($online->username)) {
			$ffdb->Error("You need to be logged in to use this feature");
		}
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$tpl->LoadTemplate("send_to_friend_form","templates/$online->skin/send_to_friend_form.tpl");
		$send_to_friend_form_array = array(
			'file_name'=> $show_file[file_name],
			'file_id'=> $show_file[file_id],
			'username'=> $online->username,
			'email'=> $online->email,
		);
		$tpl->ParseTemplate("send_to_friend_form",$send_to_friend_form_array,"Yes");
		$tpl->PrintTemplate("send_to_friend_form");
	}
	//*************************************************/
    // Sends email to friend
    //*************************************************/
	function SendFriendEmail() {
		global $mysql,$tpl,$ffdb,$online,$CONFIG;
		if (empty($online->username)) {
			$ffdb->Error("You need to be logged in to use this feature");
		}
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$tpl->LoadTemplate("send_to_friend_email","templates/$online->skin/send_to_friend_email.tpl");
		$send_to_friend_email_array = array(
			'filename'=> $show_file[file_name],
			'file_id'=> $show_file[file_id],
			'username' =>$online->username,
			'script_url' => $CONFIG['script_url'],
			'archive_name' => $CONFIG['archive_name']
		);
		$tpl->ParseTemplate("send_to_friend_email",$send_to_friend_email_array,"No");
		$message=$tpl->SaveTemplate("send_to_friend_email");
		$subject=$CONFIG['archive_name']."Email";
		$from=$CONFIG['archive_email'];
		$reply_to=$online->email;
		$header="From: ".$_POST['email']." \nReply-To: $reply_to";
		require("lib/mailer.php");
		$email=new Mailer;
		$email->SendEmail($_REQUEST['email'],$subject,$message,$header);
		$ffdb->Success("Email Sent to friend");
	}
	
}
?>