<?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['rguid']) && !isset($_REQUEST['oguid']) && !isset($_REQUEST['ext'])) {
	echoNotRigths();
	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');
}
$oguid = (isset($_REQUEST['oguid']) ? $_REQUEST['oguid'] : '') ;
$rguid = (isset($_REQUEST['rguid']) ? $_REQUEST['rguid'] : '') ;
$ext = (isset($_REQUEST['ext']) ? $_REQUEST['ext'] : '') ;
$cn = db_connect();
if (isset($_REQUEST['idbranch'])) {
	$idbranch = $_REQUEST['idbranch'];
	$skbranch = db_getSKBranch($cn,$idbranch);	
} else if (isset($_REQUEST['skbranch'])) {
	$skbranch = $_REQUEST['skbranch'];
	$idbranch = db_getIDBranch($cn, $skbranch );	
} else {
	$isMainBranch = true;
	$skbranch = db_getMainSKBranch($cn);
	$idbranch = db_getMainIDBranch($cn);	
}
if (isset($_REQUEST['lang']) && getSetting('portal','useRights',0) == 1)
{
	$lng = 'lang='.$_REQUEST['lang'];
	$lngid = db_getLanguageID($cn, $_REQUEST['lang']);
	$contlng = $lng;
	$contlngid = $lngid;
} else {
	if (getSetting('portal','useRights',0) == 1){
		$contlng = 'lang='.db_getLanguageShortByID($cn, $_SESSION['user_idlanguagecontent']);
		$contlngid = $_SESSION['user_idlanguagecontent'];
	} else {
		if (!isset($_SESSION['user_idlanguagecontent'])){
			$_SESSION['user_idlanguagecontent'] = $lngid;
		}
	} 
}

if ( GetSetting('portal','useRights',0) == 1 ){
	if( !IsUserLogin() ){
		header("Location: ".getCurPageURLWO()."/login.php?returl=".getCurPageWithEncodeParams());
		db_disconnect($cn);
		ob_end_flush();
		exit;
	} else {
		$userids = GetUserIDs();
		if (!isset($_REQUEST['rguid'])){
	  		$trSt = db_getReportsToObject($cn, $oguid , $userids, $langid, $contlngid);
			if (sizeof($trSt) > 0 ) {
				$rguid = $trSt[0]['r_guid'];
			}
		}
		if (db_getUserRightsToObject($cn, GetUserIDs(), $oguid) === null || db_getUserRightsToReport($cn, $userids, $rguid, $oguid, $contlngid) === null ) {
			echoNotRigths();
			db_disconnect($cn);
			ob_end_flush();
			exit;
		}
	}
}

if (db_isObjectsMultianguage($cn, $oguid, $rguid, $skbranch)){
	$lang_short = null;
} else {
	if (db_isLanguageValidByID($cn, $contlngid)){
		$lang_short = db_getLanguageShortByID($cn, $contlngid);
	} else {
		$lang_short = 'falseLang';
	}
}
//60312
$flroot = getSetting('storage','fileroot',realpath(dirname(__FILE__)).'/data/');
$filenamebase = ((getSetting('portal','useRights', 0)==1) ? $flroot.'/'.$idbranch.'/'.($lang_short == null ? '' : $lang_short.'/') : $flroot.'/');
$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;

if (mb_strtoupper($ext, "utf-8") == 'DOCX' && !file_exists($filename)) {
	$ext = 'docm';
	$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
}

if (mb_strtoupper($ext, "utf-8") == 'DOCM' && !file_exists($filename)) {
	$ext = 'xlsx';
	$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
}

if (mb_strtoupper($ext, "utf-8") == 'XLSX' && !file_exists($filename)) {
	$ext = 'xlsm';
	$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
}


if (!is_readable($filename)) {
	$lang_short = db_getDefaultLangShortName($cn);
	$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
	
	if (mb_strtoupper($ext, "utf-8") == 'DOCX' && !file_exists($filename)) {
		$ext = 'docm';
		$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
	}
	
	if (mb_strtoupper($ext, "utf-8") == 'DOCM' && !file_exists($filename)) {
		$ext = 'xlsx';
		$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
	}
	
	if (mb_strtoupper($ext, "utf-8") == 'XLSX' && !file_exists($filename)) {
		$ext = 'xlsm';
		$filename = $filenamebase.$oguid.'_'.$rguid.'.'.$ext;
	}
}

$langid = $_SESSION['user_idlanguagecontent'];
$nametoclient = db_getObjectName($cn, $oguid, $langid, $skbranch).' ('.db_getReportName($cn, $rguid, $langid, $skbranch).')';
$nametoclient .= '.'.$ext;
file_download($filename, str_replace('"', "''", html_entity_decode($nametoclient))); //Task 62097

if ($cn)
	db_disconnect($cn);
ob_end_flush();

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>';
}
?>