<?php
define('THIS_SCRIPT', 'viewfile');
require_once 'global.php';
require_once INC_PATH.'FileHandler.class.php';
$lang = load_lang($lang, 'viewfile' );//print viewfile.lang.php;
global $title_num,$action,$stats,$title_num;
$stats = $fsetting['forumname'].' - '.$lang['hometitle'];
$bbs_stat=$fsetting['forumname'];
$title_num = array(
				0=>array('linktitle'=>$lang['Error.str1'],'linkurl'=>'login.php')
				,1=>array('linktitle'=>$lang['Error.str2']));
PageLoad();
function PageLoad(){
	global $dv,$db,$lang,$id,$file,$f_type,$filename,$content_disp,$content,$part,$ext,$fp,$data,$boardid,$src;
	if('space' != $src AND intval($_GET['boardid'])<=0 or !is_numeric($_GET['id'])){
		head();
		showmsg($lang['error.str3']);
	exit;
	}
	if ('space' === $src) {
		space_pageload();
	} else {
		forum_pageload();
	}
	exit;
}
function space_pageload()
{
	global $dv,$db,$lang,$id,$file,$f_type,$filename,$content_disp,$content,$part,$ext,$fp,$data,$boardid,$sr,$fsetting;
	//更新下载次数
		$db->query("UPDATE {$dv}space_upfile SET downnum=downnum+1 WHERE id=".intval($_GET['id'])."");
	//取得文件名
		$file = strtolower($db->scalar("SELECT filename FROM {$dv}space_upfile WHERE id='".intval($id)."'"));
	$fileext = get_ext_name($file);
	if (strpos($file, 'dv_uploadfile') !== false){
		head();
		showmsg($lang['Error.nofile']);
		exit;
	}
	if (strpos($file, '..')!==false){
		head();
		showmsg($lang['Error.info']);
		exit;
	}
	$filename = basename($file);
	$ext = get_ext_name($filename);
	if(substr($filename, -1, 1) == '_'){
		$filename = substr($filename, 0, -1);
	}
	$mime_type=(USR_BROWSER_AGENT=='IE'||USR_BROWSER_AGENT=='OPERA')?'application/octetstream':'application/octet-stream';
	header ('Content-Type: unknow/unknow');
	$content_disp =(USR_BROWSER_AGENT =='IE')?'inline':'attachment';
	header ('Content-Disposition:'.$content_disp.';filename="'.$filename.'"');
	header ('Pragma: no-cache');
	header ('Expires: 0');
	require_once CACHE_PATH.'space/space_config.php';
	$spaceConfig_System =& $_DCACHE['spaceConfig_system'];
	$spaceSystemSetting = explode(',',$spaceConfig_System['s_setting']);
	$spaceSystemUpSetting = explode('|',$spaceSystemSetting[12]);
	$spaceuploadbasedir = basename($fsetting['forumuploadname']).'/'.basename($spaceSystemUpSetting[19]).'/';
	print read_from_file($spaceuploadbasedir.'/'.$file);
}//end function space_pageload
function forum_pageload()
{
	global $dv,$db,$lang,$id,$file,$f_type,$filename,$content_disp,$content,$part,$ext,$fp,$data,$boardid,$src,$fsetting,$groupsetting,$userid,$_DCACHE;
	if($userid<=0 and intval($groupsetting[61])==0){
		head();
		showmsg($lang['error.str4']);
		footer();		
	}

	
	//检查用户下载限制
	$sql= "select usergroupid,userpost,userep,userwealth,usercp,userpower,userisbest,userdel,joindate from {$dv}user where  userid={$userid}";
	$ret= '';
	if($result = $db->query($sql)){
		if( $row =& $db->fetch_array($result) ){
			if($row['usergroupid'] > 4){
				include_once CACHE_PATH.'board/board_all.php';
				if($row['userpost'] < $_DCACHE['forum_settings'][1]['setting'][84])
					$ret= $lang['error.str5'].$_DCACHE['forum_settings'][1]['setting'][84].$lang['error.str14'];
				else if($row['userep'] < $_DCACHE['forum_settings'][1]['setting'][85])
					$ret= $lang['error.str6'].$_DCACHE['forum_settings'][1]['setting'][85].$lang['error.str14'];
				else if($row['userwealth'] < $_DCACHE['forum_settings'][1]['setting'][86])
					$ret= $lang['error.str7'].$_DCACHE['forum_settings'][1]['setting'][86].$lang['error.str14'];
				else if($row['usercp'] < $_DCACHE['forum_settings'][1]['setting'][87])
					$ret= $lang['error.str8'].$_DCACHE['forum_settings'][1]['setting'][87].$lang['error.str14'];
				else if($row['userpower'] < $_DCACHE['forum_settings'][1]['setting'][88])
					$ret= $lang['error.str9'].$_DCACHE['forum_settings'][1]['setting'][88].$lang['error.str14'];
				else if($row['userisbest'] < $_DCACHE['forum_settings'][1]['setting'][89])
					$ret= $lang['error.str10'].$_DCACHE['forum_settings'][1]['setting'][89].$lang['error.str14'];
				else if(($row['userdel'] > $_DCACHE['forum_settings'][1]['setting'][90]) && ($_DCACHE['forum_settings'][1]['setting'][90] != 0))
					$ret= $lang['error.str11'].$_DCACHE['forum_settings'][1]['setting'][90].$lang['error.str14'];
				else if((time()-$row['joindate']) < (60*$_DCACHE['forum_settings'][1]['setting'][91]))
					$ret= $lang['error.str12'].$_DCACHE['forum_settings'][1]['setting'][91].$lang['error.str15'];
				else{
					$upnum=intval($db->scalar("select count(1) from {$dv}upfile where f_userid={$userid}"));
					if($upnum < $_DCACHE['forum_settings'][1]['setting'][92])
						$ret= $lang['error.str13'].$_DCACHE['forum_settings'][1]['setting'][92].$lang['error.str16'];
				}
			}
		}
		else
			$ret= $lang['error.str3'];
	}
	else
		$ret= $lang['error.str3'];
	
	if($ret != ''){
		head();
		showmsg($ret);
		footer();
	}

	click_down();
	get_filetype();
	$file=strtolower($f_type['f_filename']);
	get_ext_name($file);
	if (strpos($file,"dv_uploadfile")!==false){
		head();
		showmsg($lang['Error.nofile']);
	}
	if (strpos($file,"..")!==false){
		head();
		showmsg($lang['Error.info']);
	}
	$filename = basename($file);
	$ext= get_ext_name($filename);
	if(substr($filename, -1, 1) == '_'){
		$filename = substr($filename, 0, -1);
	}
	$mime_type=(USR_BROWSER_AGENT=='IE'||USR_BROWSER_AGENT=='OPERA')?'application/octetstream':'application/octet-stream';
	header ('Content-Type: unknow/unknow');
	$content_disp =(USR_BROWSER_AGENT =='IE')?'inline':'attachment';
	header ('Content-Disposition:'.$content_disp.';filename="'.$filename.'"');
	header ('Pragma: no-cache');
	header ('Expires: 0');
    if (defined('UPLOAD_BASEDIR')) {
        print read_from_file($fsetting['forumuploadname'].$file);
    } else {
        $uploadbasedir = basename($fsetting['forumuploadname']);
        print read_from_file($uploadbasedir.'/'.$file);
    }
}//end function forum_pageload
function  click_down(){
	global $dv,$db,$lang,$id;
	$sql="update {$dv}upfile set f_downnum=(f_downnum+1) where f_id=".intval($_GET['id'])."";
	$result=$db->query($sql);
}
$db->free_result($result);
function get_ext_name($file){
	global $file,$part,$ext;
	$part= explode('.',$file );
	$ext = $part[count($part) - 1];	
	return ($ext);
}

function read_from_file ($file){
	$file=strtolower($file);
	if (!FileHandler::file_exists($file)){
		return ( FALSE );
	}
	return FileHandler::file_get_contents($file);
}
function get_filetype(){
	global $dv,$db,$lang,$f_type,$id;
	$sql="select f_filename from {$dv}upfile where f_id='".intval($id)."'";
	$result=$db->query($sql);
	$f_type=$db->fetch_array($result);
}
$db->free_result($result);
?>