<?php
if(count(get_included_files()) ==1) exit("Direct access not permitted.");
if (!isset($_SESSION)) session_start();
date_default_timezone_set('Asia/Jakarta');

if(isset($_GET['file'])){
	require_once('include/global.php');
	$db=pDatabase::getInstance();
	$id=$_GET['file'];
	
	$query = "SELECT t1.id,t2.nama_baru,t2.nama_awal FROM tb_download as t1
		inner join tb_dokumen as t2 on t1.iddokumen=t2.id
		WHERE t1.status=1 and t1.iddokumen=?";
	$rs=$db->loadAssoc($query,array($id));
	if(isset($rs['id'])){
		$file='download/'.$rs['nama_baru'];
		$name=$rs['nama_awal'];
		
	 	
		$known_mime_types=array(
		 	"pdf" => "application/pdf",
		 	"txt" => "text/plain",
		 	"html" => "text/html",
		 	"htm" => "text/html",
			"exe" => "application/octet-stream",
			"zip" => "application/zip",
			"doc" => "application/msword",
			"xls" => "application/vnd.ms-excel",
			"ppt" => "application/vnd.ms-powerpoint",
			"gif" => "image/gif",
			"png" => "image/png",
			"jpeg"=> "image/jpg",
			"jpg" =>  "image/jpg",
			"php" => "text/plain",
			"xlsx" => "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
			"docx" => "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
			"pptx" => "application/vnd.openxmlformats-officedocument.presentationml.presentation"
		 );
		 $file_extension = strtolower(substr(strrchr($file,"."),1));
		 if(array_key_exists($file_extension, $known_mime_types)){
			$mime_type=$known_mime_types[$file_extension];
		 } else {
			$mime_type="application/force-download";
		 };
		 
		 header("Cache-Control: public");
		 header("Content-Description: File Transfer");
		 header("Content-Disposition: attachment; filename=".$name);
		 header("Content-Type: ".$mime_type);
		 header("Content-Transfer-Encoding: binary");
		
		 readfile($file);
	}
}
?>