<?php
/*
 ########################################################################
#                                                                        #
#           Version 4       /                        /   /               #
#          -----------__---/__---__------__----__---/---/-               #
#           | /| /  /___) /   ) (_ `   /   ) /___) /   /                 #
#          _|/_|/__(___ _(___/_(__)___/___/_(___ _/___/___               #
#                       Free Content / Management System                 #
#                                   /                                    #
#                                                                        #
#                                                                        #
#   Copyright 2005-2006 by webspell.org                                  #
#                                                                        #
#   visit webSPELL.org, webspell.info to get webSPELL for free           #
#   - Script runs under the GNU GENERAL PUBLIC LICENSE                   #
#   - It's NOT allowed to remove this copyright-tag                      #
#   -- http://www.fsf.org/licensing/licenses/gpl.html                    #
#                                                                        #
#   Code based on WebSPELL Clanpackage (Michael Gruber - webspell.at),   #
#   Far Development by Development Team - webspell.org                   #
#                                                                        #
#   visit webspell.org                                                   #
#                                                                        #
 ########################################################################
*/

function unit_to_size($num, $unit) {
	
	switch($unit) {
		case 'b': $size=$num; break;
		case 'kb': $size=$num*1024; break;		
		case 'mb': $size=$num*1024*1024; break;			
		case 'gb': $size=$num*1024*1024*1024; break;				
	}
	return $size;
}

eval ("\$title_files = \"".gettemplate("title_files")."\";");
echo $title_files;

$action = $_GET['action'];
if($action=="save") {
  if(!isfileadmin($userID)) die(redirect("index.php?site=files", "no access!", "3"));

  $upfile = $_FILES[upfile];
	$poster = $_POST['poster'];
  $filecat = $_POST['filecat'];
  $filename = $_POST['filename'];
  $fileurl = $_POST['fileurl'];
  $filesize = unit_to_size($_POST['filesize'],$_POST['unit']);
  $info = $_POST['info'];
  $accesslevel = $_POST['accesslevel'];
	$mirror1 = $_POST['mirror2'];
	$mirror2 = $_POST['mirror3'];

	// MIRRORS
	if(eregi("http://", $mirror1) OR eregi("ftp://", $mirror1)) {
		if(eregi("http://", $mirror2) OR eregi("ftp://", $mirror2)) {
			$mirrors = $mirror1.'||'.$mirror2;
		} else $mirrors = $mirror1;
	}
	elseif(eregi("http://", $mirror2) OR eregi("ftp://", $mirror2)) {
		$mirrors = $mirror2;
	}
	else $mirrors = '';

	if($upfile OR $fileurl) {
		$filepath = "./downloads/";
		if($upfile[name] != "") {
			$des_file = $filepath.$upfile[name];
			if(!file_exists($des_file)) {
				if(move_uploaded_file($upfile[tmp_name], $des_file)) {
					$file=$upfile[name];
					$filesize = $upfile[size];
					@chmod($des_file, 0644);
				}
			}	
			else die("file already exists.");  
		}
		else {
			if(eregi("http://", $fileurl) OR eregi("ftp://", $fileurl)) $file=$fileurl;	
		}	
		$date = time();
		$qry="INSERT INTO ".PREFIX."files ( filecatID, poster, date, filename, filesize, info, file, mirrors, downloads, accesslevel ) values ( '$filecat', '$poster', '$date', '$filename', '$filesize', '$info', '$file', '$mirrors', '0', '$accesslevel')";
		if(safe_query($qry)) {
			redirect("index.php?site=files&file=".mysql_insert_id()."", "File created.", "3");
		} else redirect("index.php?site=files", "File could not be created.", "3");
	} else redirect("index.php?site=files", "No valid file.", "3");
}
elseif($action=="saveedit") {
  if(!isfileadmin($userID)) die(redirect("index.php?site=files", "no access!", "3"));

  $fileID = $_POST['fileID'];
  $upfile = $_FILES[upfile];
  $filecat = $_POST['filecat'];
  $filename = $_POST['filename'];
  $fileurl = $_POST['fileurl'];
  $filesize = unit_to_size($_POST['filesize'],$_POST['unit']);
  $info = $_POST['info'];
  $accesslevel = $_POST['accesslevel'];
	$mirror1 = $_POST['mirror1'];
	$mirror2 = $_POST['mirror2'];	
  unset($file);
	
	// MIRRORS
	if(eregi("http://", $mirror1) OR eregi("ftp://", $mirror1)) {
		if(eregi("http://", $mirror2) OR eregi("ftp://", $mirror2)) {
			$mirrors = $mirror1.'||'.$mirror2;
		} else $mirrors = $mirror1;
	}
	elseif(eregi("http://", $mirror2) OR eregi("ftp://", $mirror2)) {
		$mirrors = $mirror2;
	}
	else $mirrors = '';
	
	$filepath = "./downloads/";
	if($upfile[name] != "") {
		$des_file = $filepath.$upfile[name];
		if(file_exists($des_file)) {
			unlink($des_file);
		}
		if(move_uploaded_file($upfile[tmp_name], $des_file)) {
			$file=$upfile[name];
			$filesize = $upfile[size];
			chmod($des_file, 0644);
		}
	}
	else {
		if(eregi("http://", $fileurl) OR eregi("ftp://", $fileurl)) {
			$file=$fileurl;	
		}
	}
	safe_query("UPDATE ".PREFIX."files SET filecatID='$filecat', mirrors='$mirrors', filename='$filename', filesize='$filesize', info='$info', accesslevel='$accesslevel' WHERE fileID='$fileID'") or die(redirect("index.php?site=files", "Failed to save file-information", "3"));
	if($file) {
		if(!safe_query("UPDATE ".PREFIX."files SET file='$file' WHERE fileID='$fileID'")) die(redirect("index.php?site=files", "Failed to edit file!", "3"));
	}
	redirect("index.php?site=files", "Successful.");
}
elseif($action=="delete") {
  if(!isfileadmin($userID)) die(redirect("index.php?site=files", "no access!", "3"));

	$cat = $_GET['cat']; if($cat) $ref='&cat='.$cat;
  $file = $_GET['file'];
	
	if($file) {
		$ergebnis=safe_query("SELECT * FROM ".PREFIX."files WHERE fileID='".$file."'");
		$ds=mysql_fetch_array($ergebnis);
		if(!eregi('http://', $ds[file])) @unlink('./downloads/'.$ds[file]);
		
		if(safe_query("DELETE FROM ".PREFIX."files WHERE fileID='$file'")) {
			redirect("index.php?site=files".$ref,"File deleted.","3");
		} else {
			redirect("index.php?site=files", "File could not be deleted.", "3");
		}
	} else redirect("index.php","Can't delete files without fileID","3");
}
elseif($action=="newfile") {
	// ADMINACTIONS
	$adminactions='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left">';
	$adminactions.= '<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=files\');return document.MM_returnValue" value="files"> ';
	if(isfileadmin($userID)) {
		$adminactions.='</td><td align="right"><input type="button" class="button" onClick="MM_openBrWindow(\'admin/admincenter.php?site=filecategorys\',\'News\')" value="new category"></td></tr></table>';
		$rubrics=safe_query("SELECT * FROM ".PREFIX."files_categorys ORDER BY name");
		while($dr=mysql_fetch_array($rubrics)) {
			$filecats.='<option value="'.$dr[filecatID].'">'.$dr[name].'</option>';
		}
		
    $access='<option value="0">All</option><option value="1">Registered</option><option value="2">Clanmember</option>';

		$bg1=BG_1;

		if(!isset($filecats)) redirect('index.php?site=files', 'You have to create a file-category first!', '3');
		else {
			echo $adminactions;
			eval ("\$files_new = \"".gettemplate("files_new")."\";");
			echo $files_new;
	  }
	}
	else redirect("index.php?site=files", "no access!", "3");
}
elseif($action=="edit") {
  $fileID = $_GET['fileID'];
	if($fileID) {
		if(isfileadmin($userID)) {
			// ADMINACTIONS
			$adminactions ='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left"><td align="right"><input type="button" class="button" onClick="MM_openBrWindow(\'admin/admincenter.php?site=filecategorys\',\'News\')" value="new category"></td></tr></table><br>';

			$rubrics=safe_query("SELECT * FROM ".PREFIX."files_categorys ORDER BY name");
			while($cat=mysql_fetch_array($rubrics)) {
				$filecats.='<option value="'.$cat[filecatID].'">'.$cat[name].'</option>';
			}
		
			$file=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files WHERE fileID='$fileID'"));
			$filecats=str_replace('value="'.$file[filecatID].'"', 'value="'.$file[filecatID].'" selected', $filecats);
			$accessmenu='<option value="0">All</option><option value="1">Registered</option><option value="2">Clanmember</option>';
			$access=str_replace('value="'.$file[accesslevel].'"', 'value="'.$file[accesslevel].'" selected', $accessmenu);
			
			$sizeinfo = strtolower(detectfilesize($file[filesize]));
			$sizeinfo = explode(" ",$sizeinfo);
			
			$filesize=$sizeinfo[0];

			$unit = '<option value="b">Byte</option><option value="kb">KByte</option><option value="mb">MByte</option><option value="gb">GByte</option>';			
			switch($sizeinfo[1]) {
				case 'byte': $unit=str_replace('value="b"','value="b" selected',$unit); break;
				case 'kb': $unit=str_replace('value="kb"','value="kb" selected',$unit); break;			
				case 'mb': $unit=str_replace('value="mb"','value="mb" selected',$unit); break;			
				case 'gb': $unit=str_replace('value="gb"','value="gb" selected',$unit); break;			
			}
										
			if(eregi('http://', $file[file])) $extern=$file[file];
			// FILE-MIRRORS (remember: the primary mirror is still the uploaded or external file!)
			$mirrors = $file['mirrors'];
			if($mirrors) {
				if(!eregi("||", $mirrors)) {
					$secondarymirror = explode("||", $mirrors);
					$mirror2=$secondarymirror[0];
					$mirror3=$secondarymirror[1];
				} else $mirror2=$mirrors;		
			}
			
			// display
			$bg1=BG_1;
			$bg2=BG_2;
			$border=BORDER;
			$pagebg=PAGEBG;
			eval ("\$files_edit = \"".gettemplate("files_edit")."\";");
			echo $files_edit;
		}	else redirect("index.php?site=files", "no access!", "3");
	} else redirect("index.php", "Can't edit files without fileID.", "3");
}
elseif($_GET['cat']) {
	$accesslevel = 1;
	if(isclanmember($userID)) $accesslevel = 2;
	// ADMINACTIONS
	if(isfileadmin($userID) AND $publicadmin) {
		$adminactions='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left">';
		$adminactions.='</td><td align="right"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=files&action=newfile\');return document.MM_returnValue" value="new file"> ';
		$adminactions.='<input type="button" class="button" onClick="MM_openBrWindow(\'admin/admincenter.php?site=filecategorys\',\'News\')" value="new category">';
		$adminactions.='</td></tr></table>';
	}
	
	// CATEGORY
	$catID=$_GET['cat'];
	$cat=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files_categorys WHERE filecatID='".$catID."'"));
	$category=$cat['name'];
	$level=1;
	if(isclanmember($userID)) $level=2;
	$files=safe_query("SELECT * FROM ".PREFIX."files WHERE filecatID='".$cat['filecatID']."' AND accesslevel<=$level ORDER BY filename");
	if(mysql_num_rows($files)) {
	
		$bg1 = BG_1;
		$bg2 = BG_2;
		$border = BORDER;
		$pagebg = PAGEBG;
		$bgcat = BGCAT;
		
		echo $adminactions.'<br>';
		unset($adminactions);
		
		eval ("\$files_category_list = \"".gettemplate("files_category_list_head")."\";");
		echo $files_category_list;
		
		$n=0;
		
		while($file = mysql_fetch_array($files)) {
			
			$n++;
			if($n%2) {
				$bg1 = BG_1;
				$bg2 = BG_2;
			} else {
				$bg1 = BG_4;
				$bg2 = BG_3;
			}
			
			$fileid = $file['fileID'];
			$filename = '<a href="index.php?site=files&file='.$fileid.'"><b>'.$file['filename'].'</b></a>';
			$fileinfo = cleartext($file['info']);
			$filesize = $file['filesize'];
			$fileload = $file['downloads'];
			$filevotes = $file['votes'];
			$filevotes ? $filevotes = ', '.$filevotes.' votes' : $filevotes = ', unrated';
			$filedate = date("d.M.Y - H:i", $file['date']);
			$traffic = $filesize * $fileload;
			$rating = $file['rating'];
			
			// adminactions
			if(isfileadmin($userID) AND $publicadmin) {
				$adminactions = '<tr><td bgcolor='.$bg1.' colspan="5" align="right">';
				$adminactions .= '<input type="button" class="button" onClick="MM_goToURL(\'parent\', \'index.php?site=files&action=edit&fileID='.$fileid.'\');return document.MM_returnValue" value="edit file"> ';
				$adminactions .= '<input type="button" class="button" onClick="MM_confirm(\'really delete this file?\', \'index.php?site=files&action=delete&cat='.$file['filecatID'].'&file='.$fileid.'\')" value="delete file"> ';
				$adminactions .= '</td></tr>';
			}
			
			// RATING
			$rating = $file['rating'];
			$rating ? $rating = $rating.' / 10' : $rating='0 / 10';
			$ratings=array(0,0,0,0,0,0,0,0,0,0);
			for($i=0; $i<$file['rating']; $i++) {
				$ratings[$i]=1;
			}
			$ratingpic='<img src="images/icons/rating_'.$ratings[0].'_start.gif" width="1" height="5">';
			foreach($ratings as $pic) {
				$ratingpic.='<img src="images/icons/rating_'.$pic.'.gif" width="4" height="5">';
			}	
			
			if(!$userID AND $file[accesslevel]>=1) $link='(R)';
			else $link = '<a href="download.php?fileID='.$fileid.'"><img src="images/icons/download.gif" border="0"></a>';
			
			eval ("\$files_category_list = \"".gettemplate("files_category_list")."\";");
			echo $files_category_list;
		}
		eval ("\$files_category_list = \"".gettemplate("files_category_list_foot")."\";");
		echo $files_category_list;

	} else redirect("index.php?site=files", "Can't display category without catID", "3");
}
elseif($_GET['file']) {
	// ADMINACTIONS
	if(isfileadmin($userID) AND $publicadmin) {	
		$adminactions='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="left">';
		$adminactions.='</td><td align="right"><input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=files&action=newfile\');return document.MM_returnValue" value="new file"> ';
		$adminactions.='<input type="button" class="button" onClick="MM_openBrWindow(\'admin/admincenter.php?site=filecategorys\',\'News\')" value="new category">';
		$adminactions.='</td></tr></table><br>';
	}
	
	// FILE-INFORMATION
	$file=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files WHERE fileID='".$_GET['file']."'"));
	if($file[accesslevel]==2 AND !isclanmember($userID)) die('No access');

	$fileID=$file[fileID];
	$filename = $file['filename'];
	$fileinfo = cleartext($file['info']);
	$filesize = $file['filesize']; if(!$filesize) $filesize=0;
	$downloads = $file['downloads']; if(!$downloads) $downloads=0;
	$filevotes = $file['votes'];
	$filevotes ? $filevotes = ', '.$filevotes.' votes' : $filevotes = ', unrated';
	$traffic = detectfilesize($filesize * $downloads);
	$filesize = detectfilesize($file['filesize']);
	$reportlink = '<a href="index.php?site=files&action=report&link='.$file['fileID'].'"><b>report dead link</b></a>';
	$date = date("d.m.Y", $file['date']);
	
	// FILE-AUTHOR
	$uploader = cleartext('[flag]'.getcountry($file['poster']).'[/flag]').' <a href="index.php?site=profile&id='.$file['poster'].'">'.getnickname($file['poster']).'</a>';

	// FILE-CATEGORY
	$cat=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files_categorys WHERE filecatID='".$file['filecatID']."'"));
	$category = '<a href="index.php?site=files&cat='.$cat['filecatID'].'"><strong>'.$cat['name'].'</strong></a>';

	// FILE-MIRRORS (remember: the primary mirror is still the uploaded or external file!)
	$mirrors = $file['mirrors'];
	if($mirrors) {
		if(eregi("||", $mirrors)) {
			$secondarymirror = explode("||", $mirrors);
			$mirrorlist = '&#8226; <a href="'.$secondarymirror[0].'" target="_blank">download via mirror #2</a><br>&#8226; <a href="'.$secondarymirror[1].'" target="_blank">download via mirror #3</a>';
		} else $mirrorlist = '&#8226; <a href="'.$mirrors.'" target="_blank">download via mirror #2</a>';		
	} else $mirrorlist = 'no mirrors';
	
	if($file[accesslevel] AND !$userID) $mirrorlist = 'please login';

	// RATING
	$rating = $file['rating'];
	$rating ? $rating = $rating.' / 10' : $rating='0 / 10';
	$ratings=array(0,0,0,0,0,0,0,0,0,0);
	for($i=0; $i<$file['rating']; $i++) {
		$ratings[$i]=1;
	}
	$ratingpic='<img src="images/icons/rating_'.$ratings[0].'_start.gif" width="1" height="5">';
	foreach($ratings as $pic) {
		$ratingpic.='<img src="images/icons/rating_'.$pic.'.gif" width="4" height="5">';
	}	
	if($loggedin) {
		$getfiles=safe_query("SELECT files FROM ".PREFIX."user WHERE userID='$userID'");
		$found=false;
		if(mysql_num_rows($getfiles)) {
			$ga=mysql_fetch_array($getfiles);
			if($ga[files]!="") {
				$string=$ga[files];
				$array=explode(":", $string);
				$anzarray=count($array);
				for($i=0; $i<$anzarray; $i++) {
					if($array[$i]==$file[fileID]) $found=true;
				}
			}
		}
		if($found) $rateform="<i>You have already rated.</i>";
		else {
			$rateform='<form method="post" name="rating_file'.$file[fileID].'" action="rating.php">Rate now!
				<select name="rating">
					<option>0 - poor</option>
					<option>1</option>
					<option>2</option>
					<option>3</option>
					<option>4</option>
					<option>5</option>
					<option>6</option>
					<option>7</option>
					<option>8</option>
					<option>9</option>
					<option>10 - perfect</option>
				</select>
				<input type="hidden" name="userID" value="'.$userID.'">
				<input type="hidden" name="type" value="fi">
				<input type="hidden" name="id" value="'.$file[fileID].'">
				<input type="submit" name="Submit" value="rate"></form>';
		}
	} else $rateform = '<i>Rate: You have to register or login!</i>';

	// DISPLAY
	$bg1=BG_1;
	$bg2=BG_2;
	$border=BORDER;
	$pagebg=PAGEBG;
	
	// ADMINTOOLS
	if(isfileadmin($userID)) {
		$admintools = '<tr><td bgcolor="'.$pagebg.'" colspan="2"></td></tr><tr><td bgcolor='.$bg2.' colspan="2" align="right">';
		$admintools .= '<input type="button" class="button" onClick="MM_goToURL(\'parent\', \'index.php?site=files&action=edit&fileID='.$file['fileID'].'\');return document.MM_returnValue" value="edit file"> ';
		$admintools .= '<input type="button" class="button" onClick="MM_confirm(\'really delete this file?\', \'index.php?site=files&action=delete&file='.$file['fileID'].'\')" value="delete file"> ';
		$admintools .= '</td></tr>';
	}
	
	$accesslevel = 0;
	if($userID) $accesslevel = 1;
	if(isclanmember($userID)) $accesslevel = 2;
	
	if($file[accesslevel] <= $accesslevel) $link = '<a href="download.php?fileID='.$fileID.'"><img src="images/icons/download.gif" border="0"> <b><font color="#00AA00">Download '.$filename.' now</font></b> <img src="images/icons/download.gif" border="0"></a>';
	else $link='Download for registered users only!<br><a href="index.php?site=login">Login</a> | <a href="index.php?site=register">Register</a>';
	
	eval ("\$files_display = \"".gettemplate("files_display")."\";");
	echo $files_display;
}
elseif($action=="statistic") {
	// EXTENDED STATS
}
elseif($action=="report") {
	// DEAD-LINK TICKET SYSTEM
	$mode = 'deadlink';
	$type = 'files';
	$id = $_GET['link'];
	$referer=$_SERVER['HTTP_REFERER'];
	
	// display
	$bg1 = BG_1;
	$bg2 = BG_2;
	$bghead = BGHEAD;
	$border = BORDER;
	$pagebg = PAGEBG;
	
	if($id) {
		$type = 'files';
		if(!$userID) {
			$CAPCLASS = new Captcha;
			$captcha = $CAPCLASS->create_captcha();
			$hash = $CAPCLASS->get_hash();
			$CAPCLASS->clear_oldcaptcha();
			$captcha_form = $captcha.' <input type="text" name="captcha" size="5" class="form_off" onFocus="this.className=\'form_on\';" onBlur="this.className=\'form_off\'" maxlength="5"><input name="captcha_hash" type="hidden" value="'.$hash.'"><br>';
		}

		eval("\$report_deadlink = \"".gettemplate("report_deadlink")."\";");
		echo $report_deadlink;
	} else redirect("index.php?site=files", "Can't report dead link without fileID", "3");
}
else {
	$accesslevel = 1;
	if(isclanmember($userID)) $accesslevel = 2;
	if(isfileadmin($userID) AND $publicadmin) {
		$adminactions='<table width="100%" border="0" cellpadding="0" cellspacing="0"><tr><td align="right">';
		$adminactions.='<input type="button" class="button" onClick="MM_goToURL(\'parent\',\'index.php?site=files&action=newfile\');return document.MM_returnValue" value="new file"> ';
		$adminactions.='<input type="button" class="button" onClick="MM_openBrWindow(\'admin/admincenter.php?site=filecategorys\',\'News\')" value="new category">';
		$adminactions.='</td></tr></table><br>';
	}
	
	// STATS
	
	// categories in database
	$catQry=safe_query("SELECT * FROM ".PREFIX."files_categorys ORDER BY name");
	$totalcats=mysql_num_rows($catQry);
	if($totalcats) {
	
		// files in database
		$fileQry=safe_query("SELECT * FROM ".PREFIX."files");
		$totalfiles=mysql_num_rows($fileQry);
		if($totalfiles) {
		
			// total traffic caused by downloads
			while($file=mysql_fetch_array($fileQry)) {
				$filesize = $file['filesize'];
				$fileload = $file['downloads'];
				$hddspace += $filesize;
				$traffic += $filesize * $fileload;
				$rating = $file['rating'];
			}			
			$traffic = detectfilesize($traffic);
			$hddspace = detectfilesize($hddspace);
			
			// last uploaded file
			$filedata=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files WHERE accesslevel<=$accesslevel ORDER BY date DESC LIMIT 0,1"));
			$filename=$filedata['filename'];
			if(strlen($filename) > 12) {
				$filename =substr($filename, 0, 12);
				$filename.='...';
			}
			$lastfile='<a href="index.php?site=files&file='.$filedata['fileID'].'" title="'.$filedata['filename'].' - rating: '.$filedata['rating'].' of 10">'.$filename.'</a>';
		} else {
			$traffic='n/a';
			$hddspace='n/a';
			$lastfile='n/a';
		}
		
		// FILE-TOP 5
		$top5qry=safe_query("SELECT * FROM ".PREFIX."files WHERE accesslevel<=$accesslevel ORDER BY downloads DESC LIMIT 0,5");
		$top5='
			<table width="100%" cellspacing="1" cellpadding="2" bgcolor="'.BORDER.'" border="0" align="right">
				<tr> 
					<td colspan="3" class="title">&nbsp; &#8226; TOP 5 Downloads</td>
				</tr>
				<tr>
					<td bgcolor="'.PAGEBG.'" colspan="3"></td></tr>';
		$n=1;
		while($file=mysql_fetch_array($top5qry)) {
			$n%2 ? $bg=BG_1 : $bg=BG_2;
			
			$filename=$file['filename'];
			if(strlen($filename) > 12) {
				$filename =substr($filename, 0, 12);
				$filename.='...';
			}
			$filename='<a href="index.php?site=files&file='.$file[fileID].'"><b>'.$filename.'</b></a>';
			if($file['downloads'] != '0') {
				$top5.='
					<tr>
						<td bgcolor="'.$bg.'" width="20" align="center"><b>'.$n.'.</b></td>
						<td bgcolor="'.$bg.'">'.$filename.'</td>
						<td bgcolor="'.$bg.'" width="70" align="right">'.$file[downloads].'</td>
					</tr>';
			}
			$n++;
		}
		$top5.='</table>';
		
		// FILE-OVERVIEW
		$pagebg=PAGEBG;
		$border=BORDER;
		$bg1=BG_1;
		$bg2=BG_2;
		eval ("\$files_stats = \"".gettemplate("files_stats")."\";");
		eval ("\$files_overview = \"".gettemplate("files_overview_head")."\";");
		echo $files_overview;
		
		unset($traffic);
		unset($size);
		
		// FILE-CATEGORIES
		if($totalcats) {
			$i=0;
			while($cat = mysql_fetch_array($catQry)) {
        if($i%2) {
				  $bg1=BG_1;
          $bg2=BG_2;
				}
        else {
          $bg1=BG_3;
          $bg2=BG_4;
        }
				
				// cat-information
				$catID = $cat['filecatID'];
        $catname = '<a href="index.php?site=files&cat='.$catID.'"><b>'.$cat['name'].'</b></a>';
				$caticon = $cat['icon'];
				$iconfolder = './images/download-rubrics/';
				if(is_file($iconfolder.$caticon)) $caticon='<img src="'.$iconfolder.$caticon.'" border="0">';
				else $caticon='<img src="'.$iconfolder.'/missing.gif" border="0">';
				
				// get all files associated to the catID
				$catFileQry = safe_query("SELECT * FROM ".PREFIX."files WHERE filecatID='".$catID."' AND accesslevel<=$accesslevel ORDER BY fileID DESC");
				$catFileTotal = mysql_num_rows($catFileQry);
				if($catFileTotal) {
					$i++;
					while($file = mysql_fetch_array($catFileQry)) {
						$filename = $file['name'];
						$filesize = $file['filesize'];
						$fileload = $file['downloads'];
						$traffic += $filesize * $fileload;
						$downloads += $fileload;
						$size += $file['filesize']; 
					}
					$size = detectfilesize($size);
					$traffic = detectfilesize($traffic);
					
					// last uploaded file in category
					$filedata=mysql_fetch_array(safe_query("SELECT * FROM ".PREFIX."files WHERE filecatID='".$cat['filecatID']."' ORDER BY date DESC LIMIT 0,1"));
					$filename=$filedata['filename'];
					if(strlen($filename) > 12) {
						$filename =substr($filename, 0, 12);
						$filename.='...';
					}
					$lastfile_cat='<a href="index.php?site=files&file='.$filedata['fileID'].'" title="'.$filedata['filename'].' - rating: '.$filedata['rating'].' of 10">'.$filename.'</a>';
					
					// output
					eval ("\$files_category = \"".gettemplate("files_category")."\";");
					echo $files_category;
					
					unset($traffic); unset($downloads);
				}
			}	
		}
		eval ("\$files_overview = \"".gettemplate("files_overview_foot")."\";");
		echo $files_overview;

	} else echo 'no categories and files.';
}
?>