<?php
/*
 * Vytvořeno pro vynucení stahování
 */ 
$filename = $_GET['download_this_file'];
//$filename = str_replace("/","\\",$filename);
//var_dump($filename);
$file = $_SERVER['DOCUMENT_ROOT']."/download/".$filename;
//echo $file;
if(!file_exists($file)) {
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <title>Edituj - control access</title>
  </head>
  <body>
	<p>Požadovaný soubor není v tuto chvíli dostupný.</p>
  </body>
</html>
<?php
	die();
}


$info = pathinfo($file);
$extension = strtolower($info['extension']);

if ($extension == 'php') {
	echo("Requested file does not exist");
	die();	           	
}
           
// Determine correct MIME type
switch($extension){
	case "rar":    $type = "encoding/x-compress";          break;
	case "zip":    $type = "application/x-zip-compressed"; break;
  	case "001":    $type = "application/x-zip-compressed"; break;
    	case "002":    $type = "application/x-zip-compressed"; break;
      	case "003":    $type = "application/x-zip-compressed"; break;
        	case "004":    $type = "application/x-zip-compressed"; break;
          	case "005":    $type = "application/x-zip-compressed"; break;
	case "pdf":    $type = "application/pdf";              break;
	case "doc":    $type = "application/ms-word";          break;
	case "xls":    $type = "application/ms-excel";         break;
	case "pdf":    $type = "application/adobe-acrobat";    break;
	default:       $type = "application/force-download";   break;
}

$header_file = (strstr($_SERVER['HTTP_USER_AGENT'], 'MSIE')) ? preg_replace('/\./', '%2e', $filename, substr_count($filename, '.') - 1) : $filename;

$fl = str_replace( '\\' , '|', $filename );


$tokens = explode ('|', $fl);
$tokensCount = count ($tokens);
 
if($tokensCount>1)
  $header_file =  $tokens[$tokensCount-1];
else
  $header_file =  $filename;

//echo  $header_file;
//die();


header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public", false);
header("Content-Description: File Transfer");
header("Content-Type: " . $type);
header("Accept-Ranges: bytes");
header("Content-Disposition: attachment; filename=\"" . $header_file . "\";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . filesize($file));
// Send file for download
flush();
readfile($file);
die();
?>