<?php

/********************************************************************************
	- MemHT Portal -
	
	Copyright (C) 2007-2008 by Miltenovik Manojlo
	http://www.memht.com
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your opinion) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License along
	with this program; if not, see <http://www.gnu.org/licenses/> (GPLv2)
	or write to the Free Software Foundation, Inc., 51 Franklin Street,
	Fifth Floor, Boston, MA02110-1301, USA.
		
********************************************************************************/

if (!defined("_LOAD_PAGE_")) {
	die("<table style='padding: 2px; border: 1px solid #999; background-color: #EEE; font-family: Verdana; font-size: 10px;' align='center'><tr><td><b>Error:</b> This file cannot be opened directly!</td></tr></table>");
}

if (isset($_GET['op'])) { $op = inCode($_GET['op']); } else { $op = ""; }
if (isset($_GET['id'])) { $id = intval($_GET['id']); } else { $id = ""; }
if (isset($_GET['ok'])) { $ok = inCode($_GET['ok']); } else { $ok = false; }
if (isset($_POST['tags'])) { $tags = inCode($_POST['tags']); } else { $tags = ""; }
if (isset($_POST['cat_post'])) { $cat_post = inCode($_POST['cat_post']); } else { $cat_post = ""; }
if (isset($_POST['nome_post'])) { $nome_post = eregi_replace("[/_']","-",inCode($_POST['nome_post'])); } else { $nome_post = ""; }
if (isset($_POST['url_post'])) { $url_post = inCode($_POST['url_post']); } else { $url_post = ""; }
if (isset($_POST['dimensione_post'])) { $dimensione_post = intval($_POST['dimensione_post']); } else { $dimensione_post = ""; }
if (isset($_POST['demolink_post'])) { $demolink_post = inCode($_POST['demolink_post']); } else { $demolink_post = ""; }
if (isset($_POST['version_post'])) { $version_post = inCode($_POST['version_post']); } else { $version_post = ""; }
if (isset($_POST['descrizione_post'])) { $descrizione_post = inCode($_POST['descrizione_post']); } else { $descrizione_post = ""; }
if (isset($_POST['permission'])) { $permission = intval($_POST['permission']); } else { $permission = 0; }
if (isset($_POST['group'])) { $group = intval($_POST['group']); } else { $group = 0; }
$usecomments = (isset($_POST['usecomments'])) ? intval($_POST['usecomments']) : 0 ;
if (isSet($_POST['enabled'])) { $enabled = intval($_POST['enabled']); } else { $enabled = 0; }

if (isset($_GET['pg'])) { $pg = inCode($_GET['pg']); } else { $pg = 1; }
$ofsppg = 20; //Items per page
$ofsbgn = ($pg*$ofsppg)-$ofsppg;

$level = 100;
$spcs = "";
function godeep($id,$level,$spcs,$now=0) {
	global $dblink;
		
	$parent = intval($id);
	if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=$parent ORDER BY nome") AND $level>0) {
		$spcs .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
		foreach ($result as $row) {
			$id = intval($row['id']);
			$nome = outCode($row['nome']);
			
			$selected = ($now==$id) ? " selected" : "" ;
			echo "<option value='$id'{$selected}>{$spcs}$nome</div>";
			$level--;
			godeep($id,$level,$spcs,$now);
		}
	}
}

function mainDownload() {
	global $dblink;
	
	$result = $dblink->get_list("SELECT * FROM memht_download_categorie WHERE parent=0 ORDER BY nome");
	foreach ($result as $row) {
		$cid = intval($row['id']);
		$name = outCode($row['nome']);

		$num_subs = $dblink->get_num("SELECT id FROM memht_download_categorie WHERE parent=$cid");
		$name .= " ($num_subs)";
				
		echo "<div><img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='admin.php?page=download&op=category&id=$cid' title='$name'><b>$name</b></a> <a href='admin.php?page=download&op=editCategory&id=$cid' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=download&op=deleteCategory&id=$cid' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></div>";
	}	
}

function openCategory($id,$ofsbgn,$ofsppg,$pg) {
	global $dblink,$siteConfig;
	
	$cid = intval($id);

	$row = $dblink->get_row("SELECT * FROM memht_download_categorie WHERE id=$cid");
	$name = outCode($row['nome']);
	$description = outCode($row['descrizione']);
	$parent = intval($row['parent']);
	
	echo "<div><img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='admin.php?page=download&op=category&id=$cid' title='$name'><b>$name</b></a> <a href='admin.php?page=download&op=editCategory&id=$cid' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=download&op=deleteCategory&id=$cid' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></div>\n";
	if ($description!="") { echo "<div class='box'>$description</div>\n"; }	
	
	$result = $dblink->get_list("SELECT * FROM memht_download_categorie WHERE parent=$cid ORDER BY nome");
	foreach ($result as $row) {
		$sid = intval($row['id']);
		$name = outCode($row['nome']);
	
		$num_subs = $dblink->get_num("SELECT id FROM memht_download_categorie WHERE parent=$sid");
		$num_file = $dblink->get_num("SELECT id FROM memht_download WHERE cat='$sid'");
		$name .= " ($num_subs/$num_file)";
			
		echo "<div>&nbsp;&nbsp;&nbsp;<img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='admin.php?page=download&op=category&id=$sid' title='$name'>$name</a> <a href='admin.php?page=download&op=editCategory&id=$sid' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=download&op=deleteCategory&id=$sid' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></div>";
	}
	
		echo "<br>";
		
		//Files
		$n = 0;
		echo "<table width='100%' align='center' cellspacing='1' cellpadding='0' class='std_nicetable'>";
		echo "<thead>\n";
		echo "<tr><td>"._FILENAME_."</td><td width='25%'>"._TAGS_."</td><td width='10%'>"._AUTHOR_."</td><td width='1%'>&nbsp;</td></tr>\n";
		echo "</thead>\n";
		echo "<tbody>\n";
		if ($result_sf = $dblink->get_list("SELECT *,DATE_FORMAT(data, '".$siteConfig['timestamp']."') as data2 FROM memht_download WHERE cat='$cid' ORDER BY id DESC LIMIT $ofsbgn,$ofsppg")) {
		foreach ($result_sf as $row_sf) {
			$id = intval($row_sf['id']);
			$cat = intval($row_sf['cat']);
			$nome = outCode($row_sf['nome']);
			$autore = outCode($row_sf['autore']);
			$data = $row_sf['data2'];
			
			$class = (($n++%2)!=0) ? "hlight" : "clean" ;
			
			echo "<tr><td class='$class'><img src='images/bullet_paper.gif' alt='Bullet' border='0'> <a href='index.php?page=download&op=getFile&id=$id&title=".mem_urlencode($nome)."' title='$nome'><b>$nome</b></a></td><td class='$class' id='info'>";
			if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=4 AND cid=$id ORDER BY tag")) {
				foreach ($result as $row) {
					echo "<span style='padding: 0 4px;'><a href=\"index.php?page=tags&op=list&tag=".mem_urlencode(outCode($row['tag']))."\" title=\"".outCode($row['tag'])."\" target='_blank'>".outCode($row['tag'])."</a></span>";
				}
			}
			echo "</td><td class='$class' nowrap>$autore</td><td class='$class' align='right' nowrap><a href='admin.php?page=download&op=editFile&id=$id' title='"._MODIFY_."'><img src='images/edit.gif' alt='Edit' border='0'></a> <a href='admin.php?page=download&op=deleteFile&id=$id' title='"._DELETE_."'><img src='images/delete.gif' alt='Delete' border='0'></a></td></tr>\n";
		}
		} else {
			echo "<tr><td align='center' id='errorText' colspan='4' class='clean'><b>"._EMPTY_."</b></td></tr>";
		}
		echo "</tbody>\n";
		echo "</table>";
		
		//Pages
		include_once("inc/class/paginationSystem.class.php");
		$ps = new paginationSystem();
		$ps->items = $ofsppg;
		$ps->actpg = $pg;
		$ps->query = "SELECT id FROM memht_download WHERE cat=$cid";
		$ps->url = "admin.php?page=download&op=category&id=$cid&pg={{N}}";
		$ps->show();
}

function addFile($cat_post,$nome_post,$url_post,$descrizione_post,$dimensione_post,$demolink_post,$version_post,$permission,$group,$usecomments,$tags,$enabled,$ok=false) {
	global $dblink,$userInfo,$tzNOW,$level,$spcs;
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_file' method='post' action='admin.php?page=download&op=addFile&ok=true' enctype='multipart/form-data'>";
			echo "<tr><td><b>"._FILENAME_."</b></td><td><input type='text' name='nome_post' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._URL_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._FILE_URL_."]\"></td><td><input type='text' name='url_post' size='60' maxlength='255'></td></tr>\n";
			echo "<tr><td></td><td>"._OR_."</td></tr>\n";
            echo "<tr><td><b>"._UPLOAD_." ".strtolower(_FILE_)."</b></td><td><input type='file' name='nome_file' size='40'></td></tr>\n";
			echo "<tr><td><b>"._CATEGORY_."</b></td><td>\n";
			
			echo "<select name='cat_post'>\n";
				if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=0 ORDER BY nome")) {
					foreach ($result as $row) {
						$id = intval($row['id']);
						$nome = outCode($row['nome']);
						
						echo "<option value='$id'>$nome</div>";
						godeep($id,$level,$spcs);
					}
				}
			echo "</select>\n";
			
			echo "</td></tr>\n";
			echo "<tr><td><b>"._DIMENSION_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._FILLEDAUTOWHENUPLOADFILE_."]\"></td><td><input type='text' name='dimensione_post' size='20' maxlength='255'> byte\n";
			echo "<tr><td><b>"._DEMO_."</b></td><td><input type='text' name='demolink_post' size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._VERSION_."</b></td><td><input type='text' name='version_post' size='20' maxlength='255'></td></tr>\n";
			
			echo "<tr><td><b>"._PERMISSION_."</b></td><td>\n";
			echo "<select name='permission'>\n";
				echo "<option value='0' selected>"._PUBLIC_."</option>\n";
				echo "<option value='1'>"._REGISTERED_." ("._USER_.")</option>\n";
				echo "<option value='2'>"._GROUP_."</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._GROUP_."</b></td><td>\n";
			echo "<select name='group'>\n";
				echo "<option value='0' selected>-</option>\n";
				$result = $dblink->get_list("SELECT * FROM memht_groups ORDER BY name");
				foreach ($result as $row) {
					$gid = intval($row['id']);
					$gname = outCode($row['name']);
						
					echo "<option value='$gid'>$gname</option>\n";
				}
			echo "</select></td></tr>\n";
			
			echo "<tr><td valign='top'><b>"._DESCRIPTION_."</b></td><td>\n";

				textarea("descrizione_post","100%","200px",1,"fulladmin");
						
			echo "</td></tr>\n";
			echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
			echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
			echo "<select name='usecomments'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='enabled'>\n";
				echo "<option value='1' selected>"._YES_."</option>\n";
				echo "<option value='0'>"._NO_."</option>\n";
			echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'></td></tr>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome_post=="") { $save = false; $msg = _FILENAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($permission<2) { $group = 0; }
		if ($permission==2 AND $group==0) { $save = false; $msg = _GROUP_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
	
		if ($save) {
			require_once("inc/class/uploadFile.class.php");
			$upload = new uploadFile();
			$upload->field = "nome_file";
			$upload->show_errors = false;
			$upload->path = "uploads/file/";
			$upload->max_size = 5242880;
			
			if (!$nome_file = $upload->upload()) {
				if (!$upload->selected) {
					if ($url_post!="") {
						$nome_file = $url_post;
					} else {
						$save = false;
						$msg =  "<b>"._URL_."</b> "._OR_." <b>"._FILE_."</b> ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_);
					}
				} else {
					$save = false;
					$msg = $upload->error;
				}
			} else {
				$nome_file = $upload->path.$nome_file;
				$dimensione_post = filesize($nome_file);
			}
		}
		if ($save) {
			if (memRunHooks('AddFile',array($cat_post,$nome_post,$nome_file,$descrizione_post,$userInfo['user'],$demolink_post,$version_post,$dimensione_post,$permission,$group))) {
				$dblink->query("INSERT INTO memht_download (id,cat,nome,url,descrizione,autore,demolink,version,data,dimensione,flag,pgroup,usecomments,enabled)
								VALUES (null,'$cat_post','$nome_post','$nome_file','$descrizione_post','".$userInfo['user']."','$demolink_post','$version_post',$tzNOW,'$dimensione_post','$permission','$group','$usecomments','$enabled')");
				if ($tags!="") {
					$row = $dblink->get_row("SELECT id FROM memht_download ORDER BY id DESC LIMIT 1");
					$lastid = intval($row['id']);
					
					$tags = explode(",",$tags);
					foreach ($tags as $tag) {
						$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."','$lastid','4')");
					}
				}
				
				memRunHooks('AddFileEnd',array($cat_post,$nome_post,$nome_file,$descrizione_post,$userInfo['user'],$demolink_post,$version_post,$dimensione_post,$permission,$group));
			}
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function editFile($id,$cat_post,$nome_post,$url_post,$descrizione_post,$dimensione_post,$demolink_post,$version_post,$permission,$group,$usecomments,$tags,$enabled,$ok=false) {
	global $dblink,$level,$spcs;
	
	$id = intval($id);
	
	if (!$ok) {
		$row_sfe = $dblink->get_row("SELECT * FROM memht_download WHERE id=$id");
		$cat = intval($row_sfe['cat']);
		$nome = outCode($row_sfe['nome']);
		$url = outCode($row_sfe['url']);
		$demolink = outCode($row_sfe['demolink']);
		$version = outCode($row_sfe['version']);
		$descrizione = outCode($row_sfe['descrizione']);
		$dimensione = intval($row_sfe['dimensione']);
		$permission = intval($row_sfe['flag']);
		$group = intval($row_sfe['pgroup']);
		$usecomments = intval($row_sfe['usecomments']);
		$enabled = intval($row_sfe['enabled']);
	
		if ($result = $dblink->get_list("SELECT tag FROM memht_tags WHERE whr=4 AND cid=$id")) {
			$ntags = array();
			foreach ($result as $row) {
				$ntags[] = outCode($row['tag']);
			}
			$ntags = implode(",",$ntags);
		} else {
			$ntags = "";
		}
		
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_file' method='post' action='admin.php?page=download&op=editFile&id=$id&ok=true' enctype='multipart/form-data'>";
			echo "<tr><td><b>"._FILENAME_."</b><td><input type='text' name='nome_post' size='40' maxlength='255' value=\"$nome\">\n";
			echo "<tr><td><b>"._URL_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._FILE_URL_."]\"></td><td><input type='text' name='url_post' size='60' maxlength='255' value=\"$url\"></td></tr>\n";
			echo "<tr><td></td><td>"._OR_."</td></tr>\n";
            echo "<tr><td valign='top'><b>"._UPLOAD_." ".strtolower(_FILE_)."</b></td><td><input type='file' name='nome_file' size='40'></td></tr>\n";
			echo "<tr><td><b>"._CATEGORY_."</b><td>\n";
			
			echo "<select name='cat_post'>\n";
				if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=0 ORDER BY nome")) {
					foreach ($result as $row) {
						$cid = intval($row['id']);
						$cnome = outCode($row['nome']);
						
						$selected = ($cat==$cid) ? " selected" : "" ;
						echo "<option value='$cid'{$selected}>$cnome</div>";
						godeep($cid,$level,$spcs,$cat);
					}
				}
			echo "</select>\n";
			
			echo "<tr><td><b>"._DIMENSION_."</b> <img src='images/info.gif' alt='Info' title=\"header=["._INFORMATIONS_."] body=["._FILLEDAUTOWHENUPLOADFILE_."]\"><td><input type='text' name='dimensione_post' size='20' maxlength='255' value=\"$dimensione\"> byte\n";
			echo "<tr><td><b>"._DEMO_."</b></td><td><input type='text' name='demolink_post' value=\"$demolink\" size='40' maxlength='255'></td></tr>\n";
			echo "<tr><td><b>"._VERSION_."</b></td><td><input type='text' name='version_post' value=\"$version\" size='20' maxlength='255'></td></tr>\n";
			
			echo "<tr><td><b>"._PERMISSION_."</b></td><td>\n";
			echo "<select name='permission'>\n";
				$selected = ($permission==0) ? "selected" : "" ;
				echo "<option value='0'$selected>"._PUBLIC_."</option>\n";
				$selected = ($permission==1) ? "selected" : "" ;
				echo "<option value='1'$selected>"._REGISTERED_." ("._USER_.")</option>\n";
				$selected = ($permission==2) ? "selected" : "" ;
				echo "<option value='2'$selected>"._GROUP_."</option>\n";
			echo "</select></td></tr>\n";
			
			echo "<tr><td><b>"._GROUP_."</b></td><td>\n";
			echo "<select name='group'>\n";
				$selected = ($group==0) ? "selected" : "" ;
				echo "<option value='0' selected>-</option>\n";
				$result = $dblink->get_list("SELECT * FROM memht_groups ORDER BY name");
				foreach ($result as $row) {
					$gid = intval($row['id']);
					$gname = outCode($row['name']);
						
					$selected = ($group==$gid) ? "selected" : "" ;
					echo "<option value='$gid'$selected>$gname</option>\n";
				}
			echo "</select></td></tr>\n";
			
			echo "<tr><td valign='top'><b>"._DESCRIPTION_."</b></td><td>\n";
			
				textarea("descrizione_post","100%","200px",1,"fulladmin",$descrizione);	
			
			echo "</td></tr>\n";
			echo "<tr><td><b>"._TAGS_."</b></td><td><input type='text' name='tags' value=\"$ntags\" size='40' maxlength='255'> <span id='info'>("._SEPARATEDBYCOMMAS_.")</span></td></tr>\n";
			echo "<tr><td><b>"._COMMENTSENABLED_."</b></td><td>";
			echo "<select name='usecomments'>\n";
				if ($usecomments==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
			echo "</select></td></tr>\n";
			echo "<tr><td><b>"._ENABLED_."</b></td><td>";
			echo "<select name='enabled'>\n";
				if ($enabled==1) {
					echo "<option value='1' selected>"._YES_."</option>\n";
					echo "<option value='0'>"._NO_."</option>\n";
				} else {
					echo "<option value='1'>"._YES_."</option>\n";
					echo "<option value='0' selected>"._NO_."</option>\n";
				}
				echo "</select></td></tr>\n";
			echo "<tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome_post=="") { $save = false; $msg = _FILENAME_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		if ($permission<2) { $group = 0; }
		if ($permission==2 AND $group==0) { $save = false; $msg = _GROUP_." ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_); }
		
		if ($save) {
			require_once("inc/class/uploadFile.class.php");
			$upload = new uploadFile();
			$upload->field = "nome_file";
			$upload->show_errors = false;
			$upload->path = "uploads/file/";
			$upload->max_size = 5242880;
			
			if (!$nome_file = $upload->upload()) {
				if (!$upload->selected) {
					if ($url_post!="") {
						$nome_file = $url_post;
					} else {
						$save = false;
						$msg =  "<b>"._URL_."</b> "._OR_." <b>"._FILE_."</b> ".strtolower(_FIELD_)." ".strtolower(_REQUIRED_);
					}
				} else {
					$save = false;
					$msg = $upload->error;
				}
			} else {
				$nome_file = $upload->path.$nome_file;
				$dimensione_post = filesize($nome_file);
			}
		}
		
		if ($save) {
			$dblink->query("UPDATE memht_download SET cat='$cat_post',nome='$nome_post',url='$nome_file',descrizione='$descrizione_post',dimensione='$dimensione_post',demolink='$demolink_post',version='$version_post',flag='$permission',pgroup='$group',usecomments='$usecomments',enabled='$enabled' WHERE id=$id");
			
			$dblink->query("DELETE FROM memht_tags WHERE whr=4 AND cid=$id");
			if ($tags!="") {	
				$tags = explode(",",$tags);
				foreach ($tags as $tag) {
					$dblink->query("INSERT INTO memht_tags (tag,cid,whr) VALUES ('".inCode(trim($tag))."',$id,'4')");
				}
			}
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";
		} else {
			echo "<div align='center' id='errorText'><b>$msg</b></div>";
		}
	}
}

function deleteFile($id,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if ($ok) {
		$dblink->query("DELETE FROM memht_download WHERE id=$id");
		$dblink->query("DELETE FROM memht_comments WHERE whr=4 AND wid=$id");
		$dblink->query("DELETE FROM memht_ratings WHERE whr=4 AND wid=$id");
		$dblink->query("DELETE FROM memht_tags WHERE whr=4 AND cid=$id");
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETEFILE_."</b><br><a href='admin.php?page=download&op=deleteFile&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=download' title='"._NO_."'>"._NO_."</a></div>";
	}
}

function addCat($cat_post,$nome_post,$descrizione_post,$ok=false) {
	global $dblink,$level,$spcs;
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_cat' method='post' action='admin.php?page=download&op=addCategory&ok=true'>";
			echo "<tr><td>"._CATEGORYNAME_."<td><input type='text' name='nome_post' size='40' maxlength='255'>\n";
			echo "<tr><td>"._CATEGORY_."<td>\n";
			
			echo "<select name='cat_post'>\n";
				echo "<option value='0'>- "._PRINCIPALCATEGORY_." -</option>\n";
				if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=0 ORDER BY nome")) {
					foreach ($result as $row) {
						$id = intval($row['id']);
						$nome = outCode($row['nome']);
						
						echo "<option value='$id'>$nome</div>";
						godeep($id,$level,$spcs);
					}
				}
			echo "</select>\n";

			echo "<tr><td valign='top'>"._DESCRIPTION_."</td><td>\n";
			
				textarea("descrizione_post","100%","200px",1,"fulladmin");
				
			echo "</td></tr><tr><td colspan='2'><input type='submit' name='Submit' value='"._ADD_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome_post=="") { $save = false; }
	
		if ($save) {
			$dblink->query("INSERT INTO memht_download_categorie VALUES (null,'$cat_post','$nome_post','$descrizione_post')");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function editCat($id,$cat_post,$nome_post,$descrizione_post,$ok=false) {
	global $dblink,$level,$spcs;
	
	$id = intval($id);

	$row_dwc = $dblink->get_row("SELECT * FROM memht_download_categorie WHERE id=$id");
	$parentdb = intval($row_dwc['parent']);
	$nomedb = outCode($row_dwc['nome']);
	$descrizionedb = outCode($row_dwc['descrizione']);
	
	if (!$ok) {
		echo "<table width='100%' align='center' cellspacing='0' cellpadding='1'>";
		echo "<form name='form_cat' method='post' action='admin.php?page=download&op=editCategory&id=$id&ok=true'>";
			echo "<tr><td>"._CATEGORYNAME_."<td><input type='text' name='nome_post' size='40' maxlength='255' value=\"$nomedb\">\n";
			echo "<tr><td>"._CATEGORY_."<td>\n";
			
			echo "<select name='cat_post'>\n";
				$pselected = ($parentdb==0) ? "selected" : "" ;
				echo "<option value='0'$pselected>- "._PRINCIPALCATEGORY_." -</option>\n";
				if ($result = $dblink->get_list("SELECT id,nome FROM memht_download_categorie WHERE parent=0 ORDER BY nome")) {
					foreach ($result as $row) {
						$cid = intval($row['id']);
						$cnome = outCode($row['nome']);
						
						$selected = ($parentdb==$cid) ? " selected" : "" ;
						echo "<option value='$cid'{$selected}>$cnome</div>";
						godeep($cid,$level,$spcs,$parentdb);
					}
				}
			echo "</select>\n";

			echo "<tr><td valign='top'>"._DESCRIPTION_."</td><td>\n";
			
				textarea("descrizione_post","100%","200px",1,"fulladmin",$descrizionedb);
				
			echo "</td></tr><tr><td colspan='2'><input type='submit' name='Submit' value='"._MODIFY_."'>\n";
		echo "</form>\n";
		echo "</table>";
	} else {
		$save = true;
		if ($nome_post=="") { $save = false; }
	
		if ($save) {
			$dblink->query("UPDATE memht_download_categorie SET parent='$cat_post',nome='$nome_post',descrizione='$descrizione_post' WHERE id=$id");
			echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";
		} else {
			echo "<div align='center' id='errorText'><b>"._FIELDINVALID_."</b></div>";
		}
	}
}

function deleteCat($id,$ok=false) {
	global $dblink;
	
	$id = intval($id);
	if ($ok) {
		$row = $dblink->get_row("SELECT parent FROM memht_download_categorie WHERE id=$id");
		$parent = intval($row['parent']);
		$dblink->query("UPDATE memht_download_categorie SET parent=$parent WHERE parent=$id");
		
		$dblink->query("DELETE FROM memht_download_categorie WHERE id=$id");
		$result = $dblink->get_list("SELECT id FROM memht_download WHERE cat=$id");
		foreach ($result as $row) {
			$fid = intval($row['id']);
			$dblink->query("DELETE FROM memht_download WHERE id=$fid");
			$dblink->query("DELETE FROM memht_tags WHERE whr=4 AND cid=$fid");
		}
		
		echo "<meta http-equiv='refresh' content='0;URL=admin.php?page=download'>";		
	} else {
		echo "<div align='center'><b>"._SUREDELETECATEGORY_."</b><br>"._FILESINCATWILLBEDELETED_."<br><a href='admin.php?page=download&op=deleteCategory&id=$id&ok=true' title='"._YES_."'>"._YES_."</a> - <a href='admin.php?page=download' title='"._NO_."'>"._NO_."</a></div>";
	}
}

require_once("admin/inc/inc_header.php");
	admin_page_title($page);
				
	openTable();
		echo "<div align='center' class='box'>";
			echo "<a href='admin.php?page=download' title='"._MAIN_."'><img src='admin/icons/download.png' border='0' title='"._MAIN_."' align='top'> "._MAIN_."</a>";
			echo " - <a href='admin.php?page=download&op=addFile' title='"._ADD_FILE_."'><img src='admin/icons/add.png' border='0' title='"._ADD_FILE_."' align='top'> "._ADD_FILE_."</a>";
			echo " - <a href='admin.php?page=download&op=addCategory' title='"._ADD_CATEGORY_."'><img src='admin/icons/addcategory.png' border='0' title='"._ADD_CATEGORY_."' align='top'> "._ADD_CATEGORY_."</a>";
		echo "</div>";
	closeTable();
	
	openTable();

		switch($op) {
			case "category":
				openCategory($id,$ofsbgn,$ofsppg,$pg);
			break;
			case "addFile":
				addFile($cat_post,$nome_post,$url_post,$descrizione_post,$dimensione_post,$demolink_post,$version_post,$permission,$group,$usecomments,$tags,$enabled,$ok);
			break;
			
			case "editFile":
				editFile($id,$cat_post,$nome_post,$url_post,$descrizione_post,$dimensione_post,$demolink_post,$version_post,$permission,$group,$usecomments,$tags,$enabled,$ok);
			break;
			
			case "deleteFile":
				deleteFile($id,$ok);
			break;
			
			case "addCategory":
				addCat($cat_post,$nome_post,$descrizione_post,$ok);
			break;
		
			case "editCategory":
				editCat($id,$cat_post,$nome_post,$descrizione_post,$ok);
			break;
			
			case "deleteCategory":
				deleteCat($id,$ok);
			break;
		
			default:
				mainDownload();
			break;
		}
	
	closeTable();
require_once("admin/inc/inc_footer.php");

?>