<?php

$Result = isset($_GET["file"]);
if ($Result)
{
	$file = $_GET["file"];
	$Result = $file != "";
}

if ($Result) // Parameter existiert
{
	if (file_exists($file)) // Datei ist lokale Datei
	{
		// Exclude Dateiendungen, die nicht geladen werden dürfen:
		$ExcludeExt[] = "t3001";
		$ExcludeExt[] = "lpi";
	
		if (ini_get('zlib.output_compression')) // Required for some browsers
			ini_set('zlib.output_compression', 'Off');
		$path_parts = pathinfo($file);
		
		if (in_array(strtolower($path_parts['extension']), $ExcludeExt))
		{
			die("<b>Sorry, Datei darf nicht geladen werden: \"".$file."\"</b>");
		}
		
		$dispo = "attachment"; // oder "inline"
		switch (strtolower($path_parts['extension'])) {	// Determine Content Type
		  case "pdf": $ctype="application/pdf"; $dispo="inline"; break;
		  case "exe": $ctype="application/octet-stream"; 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"; $dispo="inline"; break;
		  case "png": $ctype="image/png"; $dispo="inline"; break;
		  case "jpeg":
		  case "jpg": $ctype="image/jpg"; $dispo="inline"; break;
		  default: $ctype="application/force-download";
		} 
		header("Pragma: public");
		header("Expires: 0");
		header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
		header("Content-Type: ".$ctype);
		if ($dispo != "inline")
		{
			header("Content-Type: application/octet-stream");
			header("Content-Type: application/download");
		}
		header("Content-Disposition: ".$dispo."; filename=".$path_parts["basename"]);
		header("Content-Transfer-Encoding: binary");
		header("Content-Length: ".filesize($file));
		readfile($file); 
		die(); // stop execution
	} else { // Test Web-Link
		$s = strtolower(substr($file, 0, 4));
		$Result = ($s == "www.") || ($s == "http") || ($s == "ftp:");
		if ($Result) // Ist Weblink
		{
			if ($s == "www.")
				$file = "http://".$file;
			header("refresh:0;url=".$file); // Weiterleitung zu URL
			echo 'Automatische Weiterleitung zu <a href="'.$file.'">'.$file.'</a>.';
		}
	}
}

if (!$Result)
	die("<b>Datei nicht gefunden: \"".$file."\"</b>");
?>