<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__)).'/include/includesroutines.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/includesroutines.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/includesroutines.inc');
}

// если не передано ничего про файл
if (!isset($_REQUEST['objectversionhash']) && !isset($_REQUEST['rguid']) && !isset($_REQUEST['oguid']) && !isset($_REQUEST['name'])) {
	echoNotRigths();
	ob_end_flush();
	exit;
}

$cn = db_connect();
if (isset($_REQUEST['lang']))
{
	$lng = 'lang='.$_REQUEST['lang'];
	$contlngid = db_getLanguageID($cn, $_REQUEST['lang']);
	$contlng = $lng;
	$contlngid = $contlngid;
} else {
	$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
	$contlngid = $_SESSION['user_idlanguagecontent'];
}

$uoguid = (isset($_REQUEST['oguid']) ? urldecode($_REQUEST['oguid']) : '') ;
$objectversionhash = (isset($_REQUEST['objectversionhash']) ? urldecode($_REQUEST['objectversionhash']) : '') ;
$urguid = (isset($_REQUEST['rguid']) ? urldecode($_REQUEST['rguid']) : '') ;
$name = (isset($_REQUEST['name']) ? urldecode($_REQUEST['name']) : '') ;
$folderext = (isset($_REQUEST['folderext']) ? $_REQUEST['folderext'] : '') ;
$flroot = getSetting('storage','arcroot',realpath(dirname(__FILE__)).'/data/');
if (db_isLanguageValidByID($cn, $contlngid)){
	$lang_short = db_getLanguageShortByID($cn, $contlngid);
} else {
	$lang_short = 'falseLang';
}
$filename = $flroot.'/'.$objectversionhash.'/'.($lang_short == null ? '' : '/'.$lang_short.'/').$uoguid.'_'.$urguid.$folderext.'/'.$name;
if (!file_exists($filename)){
	$filename = $flroot.'/'.$objectversionhash.'/'.$uoguid.'_'.$urguid.$folderext.'/'.$name;
}	
if (!file_exists($filename)){
	http_response_code(404);
	header('Temporary-Header: True', true, 404);
	db_disconnect($cn);
	ob_end_flush();
	exit;
}

if( !IsUserLogin() ){
	http_response_code(404);
	header('Temporary-Header: True', true, 404);
	db_disconnect($cn);
	ob_end_flush();
	exit;
} 
$Cnttype = "Content-type: image/";
switch (mb_strtoupper(getFileExtension($name), "utf-8"))
{
	case "GIF":	 	$Cnttype = $Cnttype."gif"; break;
	case "JPG":
	case "JPEG": 	$Cnttype = $Cnttype."jpeg"; break;
	case "PJPG":
	case "PJPEG": 	$Cnttype = $Cnttype."pjpeg"; break;
	case "PNG": 	$Cnttype = $Cnttype."png"; break;
	case "TIFF": 	$Cnttype = $Cnttype."tiff"; break;
	case "SVG": 	$Cnttype = $Cnttype."svg+xml"; break;
	case "ICO": 	$Cnttype = $Cnttype."vnd.microsoft.icon"; break;
	case "WBMP": 	$Cnttype = $Cnttype."vnd.wap.wbmp"; break;
	default:		$Cnttype = $Cnttype."jpeg";
}
header($Cnttype);
header("Content-Length: " . filesize($filename));
$fp = fopen($filename, 'rb');
fpassthru($fp);
exit;