<?php
/* super force download and proper headers script
 * by Charles Phillips (charles@doublerebel.com)
 * compiled from various intertubes sources
 * I can't even remember who to credit
 * so I'm releasing this with no restrictions or warranties under the WTFPL:
 * http://sam.zoy.org/wtfpl/?profanity
 *
 * Enjoy! -- Charles
 *
 * Official disclaimer (many lolz):
 * This program is free software. It comes without any warranty, to
 * the extent permitted by applicable law. You can redistribute it
 * and/or modify it under the terms of the Do What The Fuck You Want
 * To Public License, Version 2, as published by Sam Hocevar. See
 * http://sam.zoy.org/wtfpl/COPYING for more details.
 */

$fail_url 'index.php'
$file = empty($_REQUEST['d']) ? 'notfound' addslashes($_REQUEST['d']);
$force = empty($_REQUEST['force']) ? false addslashes($_REQUEST['force']);

if(
ini_get('zlib.output_compression'))
    
ini_set('zlib.output_compression''Off');

$file_extension strtolower(substr(strrchr($file,"."),1));

if( 
$file === 'notfound' ) {
    
header('Refresh: 3; '.$fail_url);
    
?>
    <html>
        <head>
            <title>File not found.</title>
        </head>
        <body>
        <b>File not found.</b>
        </body>
    </html>
    <?php
    
exit;
} elseif (! 
file_exists$file ) ) {
    
header('Refresh: 3; '.$fail_url);
    
?>
    <html>
    <head>
    <title>The file <?=$file;?> was not found.</title>
    </head>
    <body>
    The file <?=$file;?> was not found.
    </body>
    </html>
    <?php
    
exit;
}
if (
$force$ctype "application/force-download";
else switch( 
$file_extension ) {
    case 
"pdf"$ctype="application/pdf"; break;
    case 
"zip"$ctype="application/zip"; break;
    case 
"doc"$ctype="application/msword"; break;
    case 
"xls"$ctype="application/vnd.ms-excel"; break;
    case 
"ppt"$ctype="application/vnd.ms-powerpoint"; break;
    case 
"gif"$ctype="image/gif"; break;
    case 
"png"$ctype="image/png"; break;
    case 
"jpeg":
    case 
"jpg"$ctype="image/jpg"; break;
    case 
"wav":
    case 
"mp3"$ctype="application/mp3"; break;
    default: 
$ctype="";
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false);
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".basename($file)."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
readfile("$file");
exit;
?>