<?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['idforummessage']) || !isset($_REQUEST['idfile'])) {
	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');
}
if ( GetSetting('portal','useRights', 0) == 1 && !IsUserLogin()){
	header("Location: ".getCurPageURLWO()."/login.php?returl=".getCurPageWithEncodeParams());
	ob_end_flush();
	exit;
}

$cn = db_connect();
$idforummessage = $_REQUEST['idforummessage'];
$idfile = $_REQUEST['idfile'];
$fes_arr = db_getForumMessagesFile($cn, $idforummessage, $idfile);
if (sizeof($fes_arr) > 0 ) {
	$filename = $fes_arr[0]['file_path'];
	$nametoclient = $fes_arr[0]['file_name'];
	if ( is_image($filename) ){
    	image_stream($filename, $nametoclient);
	} else {
    	file_download($filename, $nametoclient);
    }
}

db_disconnect($cn);
ob_end_flush();
?>