<?php

function is_image_byidforum($cn, $idforummessage, $idfile)
{
	$fes_arr = db_getForumMessagesFile($cn, $idforummessage, $idfile);
	if (sizeof($fes_arr) > 0 ) {
		$filename = $fes_arr[0]['file_path'];
	    return is_image($filename);
	}
	return false;
}

function is_image($path)
{
    $a = getimagesize($path);
	if (!$a) {
		return false;	
	}
    $image_type = $a[2];
    if(in_array($image_type , array(IMAGETYPE_GIF , IMAGETYPE_JPEG ,IMAGETYPE_PNG , IMAGETYPE_BMP)))
    {
        return true;
    }
    return false;
}

function file_download($file, $name) {
	if(!file_exists($file)) {
		header("Location: ".getCurPageURLWO()."/filenotfound.php?".$lng."&url=".getCurPageWithEncodeParams());
		ob_end_flush();
		exit;
	}

	$size = filesize($file);

	if (preg_match('Opera(/| )([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']))
		$UserBrowser = "Opera";
	elseif (preg_match('MSIE ([0-9].[0-9]{1,2})', $_SERVER['HTTP_USER_AGENT']) || preg_match("/(Trident\/(\d{2,}|7|8|9)(.*)rv:(\d{2,}))|(MSIE\ (\d{2,}|8|9)(.*)Tablet\ PC)|(Trident\/(\d{2,}|7|8|9))/", $_SERVER["HTTP_USER_AGENT"], $match) != 0)
		$UserBrowser = "IE";
	else
		$UserBrowser = '';
	mb_internal_encoding('UTF-8');


	$mime_type = ($UserBrowser == 'IE' || $UserBrowser == 'Opera') ?  'application/octetstream' : 'application/octet-stream';
	$name_encode = ($UserBrowser == 'IE' ?  str_replace('+++++',' ',urlencode(str_replace(' ','     ',$name))) : $name);
	@ob_end_clean(); /// decrease cpu usage extreme
	header('Content-Type: ' . $mime_type . '; charset=utf-8' );
	header('Content-Transfer-Encoding: Binary');
	header('Accept-Charset:utf-8;q=0.7,*;q=0.3');
	header("Content-Type: application/download; charset=utf-8");
	header('Content-Disposition: attachment; filename="'.$name_encode.'"');
	header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
	header('Accept-Ranges: bytes');
	header("Cache-control: private");
	header('Pragma: private');

	/////  multipart-download and resume-download

	if(isset($_SERVER['HTTP_RANGE']))
	{
		list($a, $range) = explode("=",$_SERVER['HTTP_RANGE']);
		str_replace($range, "-", $range);
		$size2 = $size-1;
		$new_length = $size-$range;
		header("HTTP/1.1 206 Partial Content");
		header("Content-Length: $new_length");
		header("Content-Range: bytes $range$size2/$size");
	} else {
		$size2=$size-1;
		header("Content-Length: ".$size);
	}

	$chunksize = 1*(1024*1024);
	$bytes_send = 0;
	if ($file = fopen($file, 'r'))
	{
		if(isset($_SERVER['HTTP_RANGE']))
		fseek($file, $range);
		while(!feof($file) and (connection_status()==0))
		{
			$buffer = fread($file, $chunksize);
			print($buffer);//echo($buffer); // is also possible
			flush();
			$bytes_send += strlen($buffer);
			//sleep(1);//// decrease download speed
		}
		fclose($file);
	}

	else
		die('error can not open file');

	if(isset($new_length))
		$size = $new_length;
	die();
}

function image_stream($file, $name){
 if(!file_exists($file)) {
	header("Location: ".getCurPageURLWO()."/filenotfound.php?".$lng."&url=".getCurPageWithEncodeParams());
	ob_end_flush();
	exit;
 }
$type = 'image/jpeg';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}

?>