<?php
if (!defined("ADMIN_FILE") || !is_admin_modul("Files")) die("Illegal File Access");

include("config/config_files.php");

function files_navi() {
	global $admin_file;
	GraphicAdmin();
	OpenTable();
	echo "<h1>"._FILES."</h1>"
	."<h5>[ <a href=\"".$admin_file.".php?op=files\">"._HOME."</a>"
	." | <a href=\"".$admin_file.".php?op=files_add\">"._ADD."</a>"
	." | <a href=\"".$admin_file.".php?op=files&status=1\">"._ADDNEWFILES."</a>"
	." | <a href=\"".$admin_file.".php?op=files&status=2\">"._ADDBROCFILES."</a>"
	." | <a href=\"".$admin_file.".php?op=files_conf\">"._PREFERENCES."</a> ]</h5>";
	CloseTable();
}

function files() {
	global $prefix, $db, $admin_file;
	head();
	files_navi();
	$storynum = 50;
	$num = isset($_GET['num']) ? intval($_GET['num']) : "1";
	$offset = ($num-1) * $storynum;
	$offset = intval($offset);
	if ($_GET['status'] == 1) {
		$status = "0";
		$field = "op=files&status=1&";
		$refer = "&refer=1";
	} elseif ($_GET['status'] == 2) {
		$status = "2";
		$field = "op=files&status=2&";
		$refer = "";
	} else {
		$status = "1";
		$field = "op=files&";
		$refer = "";
	}
	$result = $db->sql_query("SELECT f.lid, f.cid, f.title, f.date, f.ip_sender, c.id, c.title FROM ".$prefix."_files AS f LEFT JOIN ".$prefix."_categories AS c ON (f.cid=c.id) WHERE status='".$status."' ORDER BY f.date DESC LIMIT $offset, $storynum");
	if ($db->sql_numrows($result) > 0) {
		OpenTable();
		echo "<script language=\"JavaScript\" type=\"text/javascript\" src=\"ajax/SortTable.js\"></script><table width=\"100%\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" class=\"sort\" id=\"sort_id\"><tr><th>"._ID."</th><th>"._TITLE."</th><th>"._CATEGORY."</th>"
		."<th>"._DATE."</th><th>"._IP."</th><th>"._FUNCTIONS."</th></tr>";
		while (list($id, $cid, $title, $date, $ip_sender, $ccid, $ctitle) = $db->sql_fetchrow($result)) {
			$ip_sender = ($ip_sender) ? $ip_sender : ""._NO."";
			$ctitle = ($cid) ? $ctitle : ""._NO."";
			$broc = ($status == 2) ? ad_broc("".$admin_file.".php?op=files_ignore&id=".$id."") : "";
			echo "<tr class=\"bgcolor1\"><td align=\"center\">".$id."</td>"
			."<td>".$title."</td>"
			."<td align=\"center\">".$ctitle."</td>"
			."<td align=\"center\">".$date."</td>"
			."<td align=\"center\">".$ip_sender."</td>"
			."<td align=\"center\">".ad_edit("".$admin_file.".php?op=files_add&id=".$id."")." ".ad_delete("".$admin_file.".php?op=files_delete&id=".$id."".$refer."", $title)." ".$broc."</td></tr>";
		}
		echo "</table>";
		CloseTable();
		num_article("Files", $storynum, $field, "lid", "_files", "cid", "status='".$status."'");
	} else {
		warning(""._NO_INFO."", "", "", 2);
	}
	foot();
}

function files_add() {
	global $prefix, $db, $admin_file, $stop;
	if (isset($_REQUEST['id'])) {
		$fid = intval($_REQUEST['id']);
		$result = $db->sql_query("SELECT cid, title, description, url, date, filesize, version, name, email, homepage FROM ".$prefix."_files WHERE lid='$fid'");
		list($cid, $title, $description, $url, $date, $filesize, $version, $name, $email, $homepage) = $db->sql_fetchrow($result);
	} else {
		$fid = $_POST['fid'];
		$cid = $_POST['cid'];
		$title = $_POST['title'];
		$description = save_text($_POST['description']);
		$url = $_POST['url'];
		$date = $_POST['date'];
		$filesize = $_POST['filesize'];
		$version = $_POST['version'];
		$name = $_POST['name'];
		$email = $_POST['email'];
		$homepage = $_POST['homepage'];
	}
	head();
	files_navi();
	if ($stop) warning($stop, "", "", 1);
	if ($description) preview($title, $description, "", "", "Files");
	$link_url = ($url) ? "<a href=\"".$url."\" target=\"_blank\" title=\""._TESTLINK."\">"._FILELINK."</a>": ""._FILELINK."";
	OpenTable();
	echo "<form name=\"post\" enctype=\"multipart/form-data\" action=\"".$admin_file.".php\" method=\"post\">"
	."<div class=\"left\">"._TITLE.":</div><div class=\"center\"><input type=\"text\" name=\"title\" value=\"".$title."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._CATEGORY.":</div><div class=\"center\"><select name=\"cid\" class=\"admin\">".getcat("Files", $cid)."</select></div>"
	."<div class=\"left\">"._DESCRIPTION.":</div><div class=\"center\">".textarea("1", "description", replace_break($description), "Files", "10")."</div>"
	."<div class=\"left\">"._POSTEDBY.":</div><div class=\"center\"><input type=\"text\" name=\"name\" value=\"".$name."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._AUEMAIL.":</div><div class=\"center\"><input type=\"text\" name=\"email\" value=\"".$email."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._FAUURL.":</div><div class=\"center\"><input type=\"text\" name=\"homepage\" value=\"".$homepage."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._FILE_USER.":</div><div class=\"center\"><input type=\"file\" name=\"userfile\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._FILE_SITE.":</div><div class=\"center\"><input type=\"text\" name=\"sitefile\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">".$link_url.":</div><div class=\"center\"><input type=\"text\" name=\"url\" value=\"".$url."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._FILEVERSION.":</div><div class=\"center\"><input type=\"text\" name=\"version\" value=\"".$version."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._SIZENOTE.":</div><div class=\"center\"><input type=\"text\" name=\"filesize\" value=\"".$filesize."\" size=\"65\" class=\"admin\"></div>"
	."<div class=\"left\">"._CHNGSTORY.":</div><div class=\"center\" style=\"white-space: nowrap;\">".datetime(1, $date)."</div>"
	."<div class=\"button\"><select name=\"posttype\">"
	."<option value=\"preview\">"._PREVIEW."</option>"
	."<option value=\"save\">"._SEND."</option></select>"
	."<input type=\"hidden\" name=\"fid\" value=\"$fid\">"
	."<input type=\"hidden\" name=\"op\" value=\"files_save\">"
	." <input type=\"submit\" value=\""._OK."\"></div></form>";
	CloseTable();
	foot();
}

function files_save() {
	global $prefix, $db, $admin_file, $stop, $conff;
	$fid = intval($_POST['fid']);
	$cid = intval($_POST['cid']);
	$title = text_filter($_POST['title']);
	$description = save_text($_POST['description']);
	$url = ($_POST['url'] == "http://") ? "" : text_filter($_POST['url']);
	$date = save_datetime();
	$filesize = intval($_POST['filesize']);
	$version = text_filter($_POST['version']);
	$name = text_filter($_POST['name']);
	$email = text_filter($_POST['email']);
	$homepage = ($_POST['homepage'] == "http://") ? "" : text_filter($_POST['homepage']);
	$ip_sender = text_filter($_POST['ip_sender']);
	if (!$title) $stop = ""._CERROR."";
	if (!$description) $stop = ""._CERROR1."";
	if (!$fid && $db->sql_numrows($db->sql_query("SELECT title FROM ".$prefix."_files WHERE title='$title'")) > 0) $stop = ""._MEDIAEXIST."";
	$filename = upload($conff['path'], $conff['typefile'], $conff['max_size'], "Files", "", "");
	$url = ($filename) ? "".$conff['path']."/".$filename."" : $url;
	$filesize =  ($filename) ? filesize($url) : $filesize;
	if ($stop) {
		$stop = $stop;
	} elseif (!$url  && $_POST['posttype'] == "save") {
		$stop = ""._UPLOADEROR2."";
	}
	if (!$stop && $_POST['posttype'] == "save") {
		if ($fid) {
			$db->sql_query("UPDATE ".$prefix."_files SET cid='$cid', title='$title', description='$description', url='$url', date='$date', filesize='$filesize', version='$version', name='$name', email='$email', homepage='$homepage', status='1' WHERE lid='$fid'");
		} else {
			$ip = getip();
			$db->sql_query("INSERT INTO ".$prefix."_files (lid, cid, title, description, url, date, filesize, version, name, email, homepage, ip_sender, status) VALUES (NULL, '$cid', '$title', '$description', '$url', '$date', '$filesize', '$version', '$name', '$email', '$homepage', '$ip', '1')");
		}
		Header("Location: ".$admin_file.".php?op=files");
	} else {
		files_add();
	}
}

function files_conf() {
	global $prefix, $db, $admin_file;
	head();
	files_navi();
	include("config/config_files.php");
	$permtest = end_chmod("config/config_files.php", 666);
	if ($permtest) warning($permtest, "", "", 1);
	OpenTable();
	echo "<h2>"._GENSITEINFO."</h2>"
	."<form action='".$admin_file.".php' method='post'>"
	."<div class=\"left\">"._F_0.":</div><div class=\"center\"><input type='text' name='temp' value='".$conff['temp']."' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._F_1.":</div><div class=\"center\"><input type='text' name='path' value='".$conff['path']."' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._FSIZE.""._FIN.":</div><div class=\"center\"><input type='text' name='max_size' value='".$conff['max_size']."' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._FTYPE.":</div><div class=\"center\"><input type='text' name='typefile' value='".$conff['typefile']."' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._C_13.":</div><div class=\"center\"><input type='text' name='listnum' value='".$conff['listnum']."' maxlength='25' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._C_10.":</div><div class=\"center\"><input type='text' name='tabcol' value='".$conff['tabcol']."' maxlength='25' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._F_5.":</div><div class=\"center\"><input type='text' name='num' value='".$conff['num']."' maxlength='25' size='65' class=\"admin\"></div>"
	."<div class=\"left\">"._STREAM.":</div><div class=\"center\"><select name='stream' class=\"admin\">"
	."<option value='0'";
	if ($conff['stream'] == "0") echo " selected";
	echo ">"._STREAM_NO."</option>"
	."<option value='1'";
	if ($conff['stream'] == "1") echo " selected";
	echo ">"._STREAM_1."</option>"
	."<option value='2'";
	if ($conff['stream'] == "2") echo " selected";
	echo ">"._STREAM_2."</option>"
	."</select></div>"
	."<div class=\"left\">"._F_6."</div><div class=\"center\">".radio_form($conff['comm'], "comm")."</div>"
	."<div class=\"left\">"._F_8."</div><div class=\"center\">".radio_form($conff['addfiles'], "addfiles")."</div>"
	."<div class=\"left\">"._F_9."</div><div class=\"center\">".radio_form($conff['addquest'], "addquest")."</div>"
	."<div class=\"left\">"._C_15."</div><div class=\"center\">".radio_form($conff['subkat'], "subkat")."</div>"
	."<div class=\"left\">"._F_11."</div><div class=\"center\">".radio_form($conff['broc'], "broc")."</div>"
	."<div class=\"left\">"._F_12."</div><div class=\"center\">".radio_form($conff['down'], "down")."</div>"
	."<div class=\"left\">"._UPFILE."</div><div class=\"center\">".radio_form($conff['upload'], "upload")."</div>"
	."<div class=\"left\">"._C_17."</div><div class=\"center\">".radio_form($conff['date'], "date")."</div>"
	."<div class=\"left\">"._C_18."</div><div class=\"center\">".radio_form($conff['read'], "read")."</div>"
	."<div class=\"left\">"._C_19."</div><div class=\"center\">".radio_form($conff['rate'], "rate")."</div>"
	."<div class=\"left\">"._C_20."</div><div class=\"center\">".radio_form($conff['letter'], "letter")."</div>"
	."<div class=\"left\">"._C_32."</div><div class=\"center\">".radio_form($conff['catdesc'], "catdesc")."</div>"
	."<div class=\"button\"><input type='hidden' name='op' value='files_save_conf'><input type='submit' value='" . _SAVECHANGES . "'></div></form>";
	CloseTable();
	foot();
}

function files_save_conf() {
	global $admin_file;
	$protect = array("\n" => "", "\t" => "", "\r" => "", " " => "");
	$xtypefile = (!$_POST['typefile']) ? "zip,gzip,rar" : strtolower(strtr($_POST['typefile'], $protect));
	$content = "\$conff = array();\n";
	$content .= "\$conff['temp'] = \"".$_POST['temp']."\";\n";
	$content .= "\$conff['path'] = \"".$_POST['path']."\";\n";
	$content .= "\$conff['max_size'] = \"".$_POST['max_size']."\";\n";
	$content .= "\$conff['typefile'] = \"$xtypefile\";\n";
	$content .= "\$conff['listnum'] = \"".$_POST['listnum']."\";\n";
	$content .= "\$conff['tabcol'] = \"".$_POST['tabcol']."\";\n";
	$content .= "\$conff['num'] = \"".$_POST['num']."\";\n";
	$content .= "\$conff['comm'] = \"".$_POST['comm']."\";\n";
	$content .= "\$conff['addfiles'] = \"".$_POST['addfiles']."\";\n";
	$content .= "\$conff['addquest'] = \"".$_POST['addquest']."\";\n";
	$content .= "\$conff['subkat'] = \"".$_POST['subkat']."\";\n";
	$content .= "\$conff['broc'] = \"".$_POST['broc']."\";\n";
	$content .= "\$conff['down'] = \"".$_POST['down']."\";\n";
	$content .= "\$conff['upload'] = \"".$_POST['upload']."\";\n";
	$content .= "\$conff['date'] = \"".$_POST['date']."\";\n";
	$content .= "\$conff['read'] = \"".$_POST['read']."\";\n";
	$content .= "\$conff['rate'] = \"".$_POST['rate']."\";\n";
	$content .= "\$conff['letter'] = \"".$_POST['letter']."\";\n";
	$content .= "\$conff['catdesc'] = \"".$_POST['catdesc']."\";\n";
	$content .= "\$conff['stream'] = \"".$_POST['stream']."\";\n";
	save_conf("config/config_files.php", $content);
	Header("Location: ".$admin_file.".php?op=files_conf");
}

switch ($op) {
	case "files":
	files();
	break;

	case "files_add":
	files_add();
	break;

	case "files_save":
	files_save();
	break;

	case "files_delete":
	$id = intval($_REQUEST['id']);
	list($filelink) = $db->sql_fetchrow($db->sql_query("SELECT url FROM ".$prefix."_files WHERE lid=$id"));
	$filel = array_reverse(explode("/", $filelink));
	if (file_exists("".$conff['temp']."/".$filel[0]."")) {
		$delf = "".$conff['temp']."/".$filel[0]."";
		unlink($delf);
	}
	if (file_exists("".$conff['path']."/".$filel[0]."")) {
		$delf = "".$conff['path']."/".$filel[0]."";
		unlink($delf);
	}
	$db->sql_query("DELETE FROM ".$prefix."_comment WHERE cid='$id' AND modul='Files'");
	$db->sql_query("DELETE FROM ".$prefix."_files WHERE lid='$id'");
	referer("".$admin_file.".php?op=files");
	break;

	case "files_ignore":
	$id = intval($_GET['id']);
	$db->sql_query("UPDATE ".$prefix."_files SET status='1' WHERE lid='$id'");
	Header("Location: ".$admin_file.".php?op=files&status=2");
	break;

	case "files_conf":
	files_conf();
	break;

	case "files_save_conf":
	files_save_conf();
	break;
}
?>