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

Размер файла: 17.81Kb
<?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: March 12, 2003                                         |
// | Last Modified: April 19, 2003                                        |
// +----------------------------------------------------------------------+
Class File {
	//*************************************************/
    // Just seing what we are doing
    //*************************************************/
    function File() {
        global $tpl,$mysql,$ffdb,$CONFIG,$admin;
		if ($_REQUEST['action']=="add_file") {
			$this->AddFileForm();
		}elseif (isset($_REQUEST['add_file'])) {
			$this->AddFile();
		}elseif ($_REQUEST['action']=="edit_file") {
			$this->FileList();
		}elseif ($_REQUEST['action']=="delete_file_confirm") {
			$this->ConfirmDeleteFile();
		}elseif ($_REQUEST['action']=="delete_file") {
			$this->DeleteFile();
		}elseif ($_REQUEST['action']=="edit_file_form") {
			$this->EditFileForm();
		}elseif (isset($_REQUEST['edit_file'])) {
			$this->EditFile();
		}
	}
	//*************************************************/
    // Add File Form
    //*************************************************/
	function AddFileForm() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin;
		$tpl->LoadTemplate("add_file_form","Admin/templates/$admin_skin/add_file_form.tpl");
		$mysql->Query("SELECT ffdb_categorys.*,ffdb_sub_categorys.* FROM ffdb_categorys,ffdb_sub_categorys WHERE ffdb_categorys.category_id=ffdb_sub_categorys.category_id");
		while ($show_category=mysql_fetch_array($mysql->result_id)) {
			if ($show_category[category_id]==$old_category_id) {
				$category_list .="<option value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
			} else {
				$category_list .="<option value=\"none\">- - - - $show_category[category_name] - - - -</option><option value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
			}
			$old_category_id=$show_category[category_id];
		}
		$mysql->Query("SELECT * FROM ffdb_licenses");
		$license_list .="<option selected value=\"0\">None</option>";
		while ($show_license=mysql_fetch_array($mysql->result_id)) {
			if ($show_license[license_name]!="None") {
				$license_list .="<option value=\"$show_license[license_id]\">$show_license[license_name]</option>";
			}
		}
		$add_file_form_array = array(
			'category_list'=> $category_list,
			'license_list'=>$license_list
		);
		$tpl->ParseTemplate("add_file_form",$add_file_form_array,"No");
		$tpl->PrintTemplate("add_file_form");
	}
	//*************************************************/
    // Actually Adds the File
    //*************************************************/
	function AddFile() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		if (!$_POST['author']) {
			$error .="Missing Authors Name<br>";
		}
		if (!$_POST['author_email']) {
			$error .="Missing Authors Email<br>";
		}
		if (!$_POST['author_site_name']) {
			$error .="Missing Authors Site Name<br>";
		}
		if (!$_POST['author_site_url']) {
			$error .="Missing Authors Site Url<br>";
		}
		if ($_POST['category']=="none") {
			$error .="You picked a Category instead of a sub category<br>";
		}
		if (!$_POST['file_name']) {
			$error .="Missing Filename<br>";
		}
		if (!$_POST['file_description']) {
			$error .="Missing Description<br>";
		}
		// Uploaded Image
		if ($_POST['has_image']=="Yes" && empty($_REQUEST['image_url'])) {
			if (!file_exists($CONFIG['images_path'].$_FILES['file_image']['name']."")) {
				if (!move_uploaded_file($_FILES['file_image']['tmp_name'],$CONFIG['images_path'].$_FILES['file_image']['name']."")) {
					$admin->Error("Image was not uploaded do to errors");
				} else {
					$upload_image="Yes";
					chmod($CONFIG['images_path'].$_FILES['file_image']['name'],0644);
				}
			} else {
				$error .="Image with that name has already been uploaded<br>";
			}
		} else {
			$upload_image="No";
		}
		// Now uploading file if they uploaded one
		if (empty($_POST['file_url'])) {
			if (!file_exists($CONFIG['files_path'].$_FILES['file_file']['name']."")) {
				if (!move_uploaded_file($_FILES['file_file']['tmp_name'],$CONFIG['files_path'].$_FILES['file_file']['name']."")) { 	
					$admin->Error("File Was not uploaded do to errors");
				} else {
					$upload_file="Yes";
					chmod($CONFIG['files_path'].$_FILES['file_file']['name'],0644);
				}
			} else {
				$error .="File with that name has already been uploaded<br>";
			}
		} else {
			$upload_file="No";
		}
		if ($error) {
			if ($upload_file=="Yes") {
				unlink($CONFIG['files_path'].$_FILES['file_file']['name']);
			}
			if ($upload_image=="Yes") {
				unlink($CONFIG['images_path'].$_FILES['file_image']['name']);
			}
			$admin->Error($error);
		}
		$ffdb->slash_array($_POST);
		$_POST['file_description']=nl2br($_POST['file_description']);
		$_POST['file_long_description']=nl2br($_POST['file_long_description']);
		$mysql->Query("SELECT * FROM ffdb_sub_categorys WHERE sub_category_id=".$_POST['category']."");
		$show_category=mysql_fetch_array($mysql->result_id);
		$add_file_array = array(
			'category_id'=> $show_category[category_id],
			'sub_category_id'=>$_POST['category'],
			'license_id'=>$_POST['license'],
			'author'=>$_POST['author'],
			'author_email'=>$_POST['author_email'],
			'author_site'=>$_POST['author_site_name'],
			'author_site_url'=>$_POST['author_site_url'],
			'file_name'=>$_POST['file_name'],
			'file_description'=>$_POST['file_description'],
			'file_long_description'=>$_POST['file_long_description'],
			'version'=>$_POST['file_version'],
			'upload_file'=>$upload_file,
			'file_size'=>$_FILES['file_file']['size'],
			'file_filename'=>$_FILES['file_file']['name'],
			'file_url'=>$_POST['file_url'],
			'upload_image'=>$upload_image,
			'image_size'=>$_FILES['file_image']['size'],
			'image_filename'=>$_FILES['file_image']['name'],
			'image_url'=>$_POST['image_url'],
			'date_added'=>$ffdb->time,
			'added_by'=>$admin->user_id,

		);
		$mysql->MakeInsertString($add_file_array);
		$mysql->Query("INSERT INTO ffdb_files($mysql->insert_fields) VALUES ($mysql->insert_values)");
		$mysql->Query("UPDATE ffdb_sub_categorys SET num_files=num_files+1 WHERE sub_category_id=".$_POST['category']."");
		$mysql->Query("UPDATE ffdb_categorys SET num_files=num_files+1 WHERE category_id=$show_category[category_id]");
		$mysql->Query("UPDATE ffdb_stats SET num_files=num_files+1");
		$admin->Success("File Added Successfully");
	}
	//*************************************************/
    // File List for Editing
    //*************************************************/
	function FileList() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		if (isset($_REQUEST['page'])) {
			$page=$_REQUEST['page'];
		} else {
			$page=1;
		}
		$ffdb->DivideData("SELECT * FROM ffdb_files ORDER BY file_name",$page,$CONFIG['files_per_page']);
        $ffdb->MakePages("<a href=\"admin.php?page={eachpage}&action=edit_file\">{eachpage}</a>&nbsp;","<b>({page})</b>&nbsp;","<a href=\"admin.php?page=1&action=edit_file\">« First Page</a>&nbsp;","<a href=\"admin.php?page={eachpage}&action=edit_file\">Last Page »</a>",$page,$ffdb->num_pages);
		$tpl->LoadTemplate("file_list","Admin/templates/$admin_skin/file_list.tpl");
		$mysql->Query("SELECT * FROM ffdb_files ORDER BY file_name LIMIT $ffdb->data_bottom,$ffdb->data_top");
		while ($show_file=mysql_fetch_array($mysql->result_id)) {
			$has_files=1;
			$file_list_array = array(
				'file_name'=> $show_file[file_name],
				'file_description'=> $show_file[file_description],
				'author'=> $show_file[author],
				'author_email'=> $show_file[author_email],
				'version'=> $show_file[version],
				'file_id'=>$show_file[file_id]
			);
			$tpl->ParseTemplate("file_list",$file_list_array,"Yes");
		}
		if ($has_files==1) {
			$tpl->PrintTemplate("file_list");
		} else {
			$admin->Error("There are currently no files to edit");
		}
		echo "<p align=\"center\">$ffdb->pages</p>";
	}
	//*************************************************/
    // Confirms that they want to delete the File
    //*************************************************/
	function ConfirmDeleteFile() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$admin->Confirm("Are you sure you want to delete $show_file[file_name]?<br>","admin.php?action=delete_file&id=$show_file[file_id]");
	}
	//*************************************************/
    // Deletes the File
    //*************************************************/
	function DeleteFile() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$mysql->Query("UPDATE ffdb_sub_categorys SET num_files=num_files-1 WHERE sub_category_id=$show_file[sub_category_id]");
		$mysql->Query("UPDATE ffdb_categorys SET num_files=num_files-1 WHERE category_id=$show_file[category_id]");
		$mysql->Query("UPDATE ffdb_stats SET num_files=num_files-1");
		if ($show_file[upload_file]=="Yes") {
			unlink($CONFIG['files_path'].$show_file[file_filename]);
		}
		if ($show_file[upload_image]=="Yes") {
			unlink($CONFIG['images_path'].$show_file[image_filename]);
		}
		$mysql->Query("DELETE FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$admin->Success("File Deleted Successfully");
	}
	//*************************************************/
    // Edit File Form
    //*************************************************/
	function EditFileForm() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		$mysql->Query("SELECT ffdb_categorys.*,ffdb_sub_categorys.* FROM ffdb_categorys,ffdb_sub_categorys WHERE ffdb_categorys.category_id=ffdb_sub_categorys.category_id");
		while ($show_category=mysql_fetch_array($mysql->result_id)) {
			if ($show_category[category_id]==$old_category_id) {
				if ($show_file[sub_category_id]==$show_category[sub_category_id]) {
					$category_list .="<option selected value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
				} else {
					$category_list .="<option value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
				}
			} else {
				if ($show_file[sub_category_id]==$show_category[sub_category_id]) {
					$category_list .="option value=\"none\">- - - - $show_category[category_name] - - - -</option><option selected value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
				} else {
					$category_list .="option value=\"none\">- - - - $show_category[category_name] - - - -</option><option value=\"$show_category[sub_category_id]\">-> $show_category[sub_category_name]</option>";
				}
			}
			$old_category_id=$show_category[category_id];
		}
		$mysql->Query("SELECT * FROM ffdb_licenses");
		$license_list .="<option selected value=\"0\">None</option>";
		while ($show_license=mysql_fetch_array($mysql->result_id)) {
			if ($show_license[license_id]==$show_file[license_id]) {
				$license_list .="<option selected value=\"$show_license[license_id]\">$show_license[license_name]</option>";
			} else {
				$license_list .="<option value=\"$show_license[license_id]\">$show_license[license_name]</option>";
			}
		}
		$tpl->LoadTemplate("edit_file_form","Admin/templates/$admin_skin/edit_file_form.tpl");
		$edit_file_form_array = array(
			'category_list'=> $category_list,
			'license_list'=>$license_list,
			'author'=>$show_file[author],
			'author_email'=>$show_file[author_email],
			'author_site'=>$show_file[author_site],
			'author_site_url'=>$show_file[author_site_url],
			'file_name'=>$show_file[file_name],
			'version'=>$show_file[version],
			'file_description'=>$show_file[file_description],
			'file_long_description'=>$show_file[file_long_description],
			'image_url'=>$show_file[image_url],
			'file_url'=>$show_file[file_url],
			'file_id'=>$show_file[file_id]
		);
		$tpl->ParseTemplate("edit_file_form",$edit_file_form_array,"No");
		$tpl->PrintTemplate("edit_file_form");
	}
	//*************************************************/
    // Actually edits the File
    //*************************************************/
	function EditFile() {
		global $tpl,$mysql,$ffdb,$CONFIG,$admin_skin,$admin;
		$mysql->Query("SELECT * FROM ffdb_files WHERE file_id=".$_REQUEST['id']."");
		$show_file=mysql_fetch_array($mysql->result_id);
		echo $show_file[category_id];
		$upload_image=$show_file[upload_image];
		$upload_file=$show_file[upload_file];
		$file_filename=$show_file[file_filename];
		$image_filename=$show_file[image_filename];
		if (!$_POST['author']) {
			$error .="Missing Authors Name<br>";
		}
		if (!$_POST['author_email']) {
			$error .="Missing Authors Email<br>";
		}
		if (!$_POST['author_site_name']) {
			$error .="Missing Authors Site Name<br>";
		}
		if (!$_POST['author_site_url']) {
			$error .="Missing Authors Site Url<br>";
		}
		if ($_POST['category']=="none") {
			$error .="You picked a Category instead of a sub category<br>";
		}
		if (!$_POST['file_name']) {
			$error .="Missing Filename<br>";
		}
		if (!$_POST['file_description']) {
			$error .="Missing Description<br>";
		}
		// Uploaded Image
		if ($_POST['new_image']=="Yes" && empty($_REQUEST['image_url'])) {
			if (!file_exists($CONFIG['images_path'].$_FILES['file_image']['name']."")) {
				if (!move_uploaded_file($_FILES['file_image']['tmp_name'],$CONFIG['images_path'].$_FILES['file_image']['name']."")) {
					$admin->Error("Image was not uploaded do to errors");
				} else {
					$upload_image="Yes";
					chmod($CONFIG['images_path'].$_FILES['file_image']['name'],0644);
				}
			} else {
				$error .="Image with that name has already been uploaded<br>";
			}
		}
		// Now uploading file if they uploaded one
		if (empty($_POST['file_url']) && $_POST['new_file']=="Yes") {
			if (!file_exists($CONFIG['files_path'].$_FILES['file_file']['name']."")) {
				if (!move_uploaded_file($_FILES['file_file']['tmp_name'],$CONFIG['files_path'].$_FILES['file_file']['name']."")) { 	
					$admin->Error("File Was not uploaded do to errors");
				} else {
					$upload_file="Yes";
					chmod($CONFIG['files_path'].$_FILES['file_file']['name'],0644);
				}
			} else {
				$error .="File with that name has already been uploaded<br>";
			}
		}
		if ($error) {
			if ($upload_file=="Yes" && $_POST['new_file']=="Yes") {
				unlink($CONFIG['files_path'].$_FILES['file_file']['name']);
			}
			if ($upload_image=="Yes" && $_POST['new_image']=="Yes") {
				unlink($CONFIG['images_path'].$_FILES['file_image']['name']);
			}
			$admin->Error($error);
		}
		if ($_POST['new_image']=="Yes" && $show_file[upload_image]=="Yes") {
			unlink($CONFIG['images_path'].$show_file[image_filename]);	
		}
		if ($_POST['new_file']=="Yes" && $show_file[upload_file]=="Yes") {
			unlink($CONFIG['files_path'].$show_file[file_filename]);
		}
		$ffdb->slash_array($_POST);
		$_POST['file_description']=nl2br($_POST['file_description']);
		$_POST['file_long_description']=nl2br($_POST['file_long_description']);
		$mysql->Query("SELECT * FROM ffdb_categorys WHERE category_id=".$_POST['category']."");
		$show_category=mysql_fetch_array($mysql->result_id);
		$add_file_array = array(
			'category_id'=> $show_category[category_id],
			'sub_category_id'=>$_POST['category'],
			'license_id'=>$_POST['license'],
			'author'=>$_POST['author'],
			'author_email'=>$_POST['author_email'],
			'author_site'=>$_POST['author_site_name'],
			'author_site_url'=>$_POST['author_site_url'],
			'file_name'=>$_POST['file_name'],
			'file_description'=>$_POST['file_description'],
			'file_long_description'=>$_POST['file_long_description'],
			'version'=>$_POST['file_version'],
			'upload_file'=>$upload_file,
			'file_size'=>$_FILES['file_file']['size'],
			'file_filename'=>$_FILES['file_file']['name'],
			'file_url'=>$_POST['file_url'],
			'upload_image'=>$upload_image,
			'image_size'=>$_FILES['file_image']['size'],
			'image_filename'=>$_FILES['file_image']['name'],
			'image_url'=>$_POST['image_url'],

		);
		$mysql->MakeUpdateString($add_file_array);
		$mysql->Query("UPDATE ffdb_sub_categorys SET num_files=num_files-1 WHERE sub_category_id=$show_file[sub_category_id]");
		$mysql->Query("UPDATE ffdb_sub_categorys SET num_files=num_files+1 WHERE sub_category_id=".$_POST['category']."");
		$mysql->Query("SELECT * FROM ffdb_sub_categorys WHERE sub_category_id=".$_REQUEST['category']."");
		$show_sub_category=mysql_fetch_array($mysql->result_id);
		$mysql->Query("UPDATE ffdb_categorys SET num_files=num_files+1 WHERE category_id=$show_sub_category[category_id]");
		$mysql->Query("UPDATE ffdb_categorys SET num_files=num_files-1 WHERE category_id=$show_file[category_id]");
		$mysql->Query("UPDATE ffdb_files SET $mysql->update_string WHERE file_id=".$_POST['id']."");
		$admin->Success("File Updated Successfully");
	}
}
?>