<?php

$id = intval($_REQUEST["id"]);
$tid = $pid = intval($_REQUEST["tid"]);

if (!$id||!$tid) exit("Unsufficient data provided");
$www_dir = realpath(dirname(__FILE__));
include ("$www_dir/functions.php");
include ("$www_dir/conf.php");

if (!$board_config->en_guest_viewing && !$_SESSION["SBBauth"]) exit("Permission denied"); 

$upload_dir = $www_data_dir;


//check name and availibility
$sql = "select * from `" . $board_config->prefix . "attachments` where id = '$id' and (pid = '$pid' or tid = '$tid');";
$res = dbquery($sql);
if (!mysql_num_rows($res)) {
  exit("404 - Document not found");
} else {
  $row = mysql_fetch_object($res);
  $doc = $row->file_name;
}
mysql_free_result($res);
$file = $upload_dir . "/attachments/$id/$doc";
if (!file_exists($file)) {
  mail($conf_admin_mail,"SBB - doc not found", "ID: $id\nFILE: $file\nTopicID: $tid\nIP: " . $_SERVER["REMOTE_ADDR"]);
  exit("404 - Document not found");
}
$len = filesize($file);
$filename = basename($file);
$file_extension = strtolower(substr(strrchr($filename,"."),1));

// Content-Type
switch( $file_extension ) {
   case "exe":  $ctype="application/octet-stream"; break;
   case "zip":  $ctype="application/zip"; break;
   case "mp3":  $ctype="audio/mpeg"; break;
   case "mpg":  $ctype="video/mpeg"; break;
   case "avi":  $ctype="video/x-msvideo"; break;
   case "gz":   $ctype="application/gzip"; break;
   case "xls":  $ctype="application/msexcel"; break;
   case "xla":  $ctype="application/msexcel"; break;
   case "hlp":  $ctype="application/mshelp"; break;
   case "chm":  $ctype="application/mshelp"; break;
   case "ppt":  $ctype="application/mspowerpoint"; break;
   case "pps":  $ctype="application/mspowerpoint"; break;
   case "doc":  $ctype="application/msword"; break;
   case "dot":  $ctype="application/msword"; break;
   case "dot":  $ctype="application/msword"; break;
   case "pdf":  $ctype="application/pdf"; break;
   case "ps":   $ctype="application/postscript"; break;
   case "rtf":  $ctype="application/rtf"; break;
   case "xml":  $ctype="application/xml"; break;
   case "swf":  $ctype="application/x-shockwave-flash"; break;
   case "wav":  $ctype="application/x-wav"; break;
   case "gif":  $ctype="application/gif"; break;
   case "jpeg": $ctype="application/jpeg"; break;
   case "jpg":  $ctype="application/jpeg"; break;
   case "png":  $ctype="application/png"; break;
   case "tiff": $ctype="application/tiff"; break;
   case "tif":  $ctype="application/tiff"; break;
   case "csv":  $ctype="text/comma-separated-values"; break;
   case "txt":  $ctype="text/plain"; break;
   default: $ctype="application/force-download";
}
   //Begin writing headers
   header("Pragma: public");
   header("Expires: 0");
   header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
   header("Cache-Control: public");
   header("Content-Description: File Transfer");
  
   //Use the switch-generated Content-Type
   header("Content-Type: $ctype");

   //Force the download
   $header="Content-Disposition: attachment; filename=".$filename.";";
   header($header );
   header("Content-Transfer-Encoding: binary");
   header("Content-Length: ".$len);
   @readfile($file);
   exit;
?>
