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

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

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


$filename = realpath(dirname(__FILE__)).'/data/'.$oguid.'/'.$file;
file_download($filename, $file);
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 '</head>';
	echo '<body>';
	getResource("message.haveNotRightsToLoadFile");
	echo '</body>';
	echo '</html>';
}
?>