<?
require_once "./pcp_upload_library.php";

$mimeTypes = array("jpg" => "image/jpeg", 
		"gif" => "image/gif",
		"GIF" => "image/gif",
		"JPG" => "image/jpeg",
		"thumb" => "image/jpeg",
		"html" => "text/html",
		"htm" => "text/html",
		"txt" => "text/plain",
		"avi" => "video/x-msvideo",
		"emw" => "application/x-daum-mytv",
		"default" => "application/octet-stream"
		);

$fhandle = $_GET['fhandle'];
$fileName = $_GET['filename'];
$viewOnly = $_GET['viewonly'];
$thisFileName = $_SERVER["SCRIPT_FILENAME"];

if ( !strstr( $fhandle, 'planet.daum.net' ) ) $fhandle = base64_decode($fhandle);

if($fhandle == "" || $fileName == "" || strstr ($fhandle, "..") ){
	error(WRONG_PARAMETER,"Parameter fhandle, filename missing", $thisFileName);
}

$filePath = filePath($fhandle);
$type = substr($filePath, strrpos($filePath, ".") + 1 );
$contentType = "";
if($mimeTypes[$type] == "") {
   $contentType = $mimeTypes["default"];
} else {
   $contentType = $mimeTypes[$type];
}
if ( file_exists($filePath) ) {
	$filemtime = date("D, j M Y G:i:s T",filemtime($filePath));
	$filesize = filesize($filePath);
	header("Cache-Control: max-age=86400");
	header("Expires: ".date("D, j M Y G:i:s T", time() + 86400));
	header("Last-Modified: $filemtime");
	header("Accept-Ranges: bytes");
	header("Content-type: ".$contentType);
	if( $contentType == "default" || $viewOnly != "Y") {
		header("Content-Disposition: attachment;filename=".$fileName);
	}
	header("Content-Length: $filesize");
#header("Pragma: no-cache");
#header("Expires: ");
	print file_get_contents($filePath);
} else {
	header("HTTP/1.1 404 Not Found");
}
?>
