<?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['oguid']) || !isset($_REQUEST['oguid'])) {
	ob_end_flush();
	exit;
}
if(!(is_readable(realpath(dirname(__FILE__)).'/include/downloads.inc'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__)).'/include/downloads.inc');
} else {
	include_once(realpath(dirname(__FILE__)).'/include/downloads.inc');
}

$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'];
}
$oguid = urldecode($_REQUEST['oguid']);
$file = rawurldecode($_REQUEST['file']);  //8607, 69394

$flroot = getSetting('storage','fileroot',realpath(dirname(__FILE__)).'/data/');
if (isset($_REQUEST['idbranch'])) {
	$idbranch = $_REQUEST['idbranch'];
	$skbranch = db_getSKBranch($cn,$idbranch);	
} else {
	$skbranch = db_getMainSKBranch($cn);
	$idbranch = db_getMainIDBranch($cn);	
}

if ( GetSetting('portal','useRights',0) == 1 ){
	if( !IsUserLogin() ){
		header("Location: ".getCurPageURLWO()."/login.php?returl=".getCurPageWithEncodeParams());
		db_disconnect($cn);
		ob_end_flush();
		exit;
	} else {
		if (db_getUserRightsToObject($cn, GetUserIDs(), $oguid, $skbranch) === null) {
			echoNotRigths();
			db_disconnect($cn);
			ob_end_flush();
			exit;
		}
	}
}

$lang_short = (isset($_REQUEST['langshort']) ? $_REQUEST['langshort'] : 'falseLang');
if (getSetting('portal','useRights', 0)==1) {
	$filename = $flroot.'/'.$idbranch.'/'.($lang_short == null ? '' : '/'.$lang_short.'/').$oguid.'/'.($file);
} else {
	// 70446
	$filename = $flroot.'/'.$oguid.'/'.($file);
}
$filename = str_replace('\\', '/', $filename);
file_download($filename, $file);

if ($cn)
	db_disconnect($cn);

function echoNotRigths(){
	header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
	header('Status: 404 Not Found');
	echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
	echo '<html>';
	echo '<head>';
	echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>';
	echo '<link rel="shortcut icon" href="favicon.ico" />';
	echo '</head>';
	echo '<body>';
	getResource("message.haveNotRightsToLoadFile");
	echo '</body>';
	echo '</html>';
}
ob_end_flush();
?>