<?php
$filepath = __FILE__;
$basepath = dirname(__FILE__);
$downpath = $basepath . '/files/';
$statpath = $basepath . '/stats/';

$filename = $_REQUEST['f'];
$filename = ereg_replace("[^A-Za-z0-9\_\.\-]", "", $filename);

$statname = $statpath . $filename . ".txt";
$stats = date("Y/m/d-H:i:s") . ' - '
        . $_SERVER['REMOTE_ADDR'] . ' - '
        . $_SERVER['HTTP_USER_AGENT'] . ' - '
        . $_SERVER['HTTP_REFERER'] . "\n";


if (is_readable( $downpath . $filename ) ) {

    if(is_writable( $statpath )) {
        $handle = fopen($statname, "a");
        fwrite($handle, $stats);
        fclose($handle);
    }

    header('Content-Type: application/x-gzip');
    header("Content-Description: File Transfer");
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header("Content-Transfer-Encoding: binary");
    header('Content-Length: '. filesize( $downpath . $filename ));
    readfile( $downpath . $filename);

    exit(0);

} else {
    header("HTTP/1.0 404 Not Found");
    exit;
}

?>
