<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: viewfile.php 12 2011-06-14 01:54:33Z along $
#
#	Copyright (C) 2008-2011 PHPDisk Team. All Rights Reserved.
#
*/

include "includes/commons.inc.php";

$in_front = true;

lang('viewfile');

$file_id = (int)gpc('file_id','GP',0);
$code = trim(gpc('code','G',''));
$file_time = (int)@$db->result_first("select file_time from {$tpf}files where file_id='$file_id'");
function cur_file($file_id){
	global $db,$tpf,$settings,$file_time,$code;
	$file = $db->fetch_one_array("select * from {$tpf}files where file_id='$file_id'");
	if(!$file){
		$file['is_del'] = 1;
		$file['file_name'] = $L['visited_tips'];
	}else{
		$file['is_del'] = 0;
		$file['in_extract'] = ($code==md5($file['file_key'])) ? 1 : 0;
		$file['username'] = @$db->result_first("select username from {$tpf}users where userid='{$file['userid']}' limit 1");
		$tmp_username = is_utf8() ? convert_str('utf-8','gbk',$file['username']) : $file['username'];
		$file_key = trim($file['file_key']);
		$tmp_ext = $file['file_extension'] ? '.'.$file['file_extension'] : "";
		$file_extension = $file['file_extension'];
		$file_ext = get_real_ext($file_extension);
		if(display_plugin('multi_server','open_multi_server_plugin',$settings['open_multi_server'],0) && $file['server_oid'] >1){
			$rs2 = $db->fetch_one_array("select * from {$tpf}servers where server_oid='{$file['server_oid']}' limit 1");
			if($rs2){
				$file['file_thumb'] = $rs2['server_host'].$rs2['server_store_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].'_thumb.'.$file_extension;
			}
			unset($rs2);
		}else{			
			$file['file_thumb'] = $settings['file_path'].'/'.$tmp_username.$file['file_store_path'].$file['file_real_name'].'_thumb.'.$file['file_extension'];
		}
		$file_description = $file['file_description'];
		$file['file_description'] = nl2br($file['file_description']);
		$file['a_space'] = urr("space","username=".rawurlencode($file['username']));
		$file['file_name'] = $file['file_name'].$tmp_ext;
		$file['file_size'] = get_size($file['file_size']);
		$file['file_time'] = custom_time("Y-m-d",$file['file_time']);
		$file['credit_down'] = $file['file_credit'] ? (int)$file['file_credit'] : (int)$settings['credit_down'];
		$file['username'] = $file['username'] ? '<a href="'.$file['a_space'].'">'.$file['username'].'</a>' : $L['visitor'];

		$file['tags'] = super_cache('file','get_file_tags|'.$file_id,1,0,'file|'.$file_time,$settings['cache_time']);

		if($settings['open_file_url']){
			$file['file_view_url'] = $settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}");
			$file['file_html_url'] = '<a href='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").' target=_blank>'.$file['file_name'].'</a>';
			$file['file_ubb_url'] = '[url='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").']'.$file['file_name'].'[/url]';
		}

		if($file['server_oid']>1){
			$rs = $db->fetch_one_array("select server_host,server_store_path from {$tpf}servers where server_oid='{$file['server_oid']}'");
			if($rs){
				if(display_plugin('multi_server','open_multi_server_plugin',($settings['open_multi_server'] && $settings['upload_server_type']=='sub_server'),0)){
					$file['a_downfile'] = $rs['server_host']."downfile.php?file_id={$file['file_id']}&file_key={$file['file_key']}&uid=$pd_uid";
					$file['a_viewfile'] = $rs['server_host']."downfile.php?action=view&file_id={$file['file_id']}&file_key={$file['file_key']}&uid=$pd_uid";
				}else{
					$file['a_downfile'] = urr("downfile","file_id={$file['file_id']}&file_key={$file['file_key']}");
				}
				$file['url'] = $rs['server_host']."downfile.php?action=by_tools&file_id={$file['file_id']}&file_key={$file['file_key']}&uid=$pd_uid";
			}
			unset($rs);
		}else{
			$file['a_downfile'] = urr("downfile","file_id={$file['file_id']}&file_key={$file['file_key']}");
			$file['url'] = $settings['phpdisk_url'].urr("downfile","action=by_tools&file_id={$file['file_id']}&file_key={$file['file_key']}");
		}
		$file['flashget_url'] = flashget_encode($file['url'],$settings['flashget_uid']);
		$file['thunder_url'] = thunder_encode($file['url']);

		if($settings['open_vote']){
			$all_count = $file['good_count']+$file['bad_count'];
			$file['good_rate'] = $file['good_count'] ? round($file['good_count']/$all_count,3)*100 : 0;
			$file['bad_rate'] = $file['bad_count'] ? round($file['bad_count']/$all_count,3)*100 : 0;
		}

		if(display_plugin('multi_server','open_multi_server_plugin',$settings['open_multi_server'],0) && $file['server_oid'] >1){
			$rs2 = $db->fetch_one_array("select * from {$tpf}servers where server_oid='{$file['server_oid']}' limit 1");
			if($rs2){
				if(can_true_link($file_extension)){
					$str = $rs2['server_host'].$rs2['server_store_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'];
					$file_url = $str.$file_ext;
					$file['a_viewfile'] = $file['a_downfile'] = $file['file_view_url'] = $file_url;
					$file['file_html_url'] = '<a href='.$file_url.' target=_blank>'.$file['file_name'].'</a>';
					$file['file_ubb_url'] = '[url='.$file_url.']'.$file['file_name'].'[/url]';
				}else{
					$file['a_viewfile'] = $rs2['server_host'].urr("downfile","action=view&file_id={$file['file_id']}&file_key={$file['file_key']}");
					$file['file_view_url'] = $settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}");
					$file['file_html_url'] = '<a href='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").' target=_blank>'.$file['file_name'].'</a>';
					$file['file_ubb_url'] = '[url='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").']'.$file['file_name'].'[/url]';
				}
				$file['preview_link'] = $rs2['server_host'].$rs2['server_store_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].$file_ext;
			}
			unset($rs2);
		}else{
			if(can_true_link($file_extension)){
				$str = $settings['phpdisk_url'].$settings['file_path'].'/'.$tmp_username.$file['file_store_path'].$file['file_real_name'];
				$file_url = $str.$file_ext;
				$file['a_viewfile'] = $file['a_downfile'] = $file['file_view_url'] = $file_url;
				$file['file_html_url'] = '<a href='.$file_url.' target=_blank>'.$file['file_name'].'</a>';
				$file['file_ubb_url'] = '[url='.$file_url.']'.$file['file_name'].'[/url]';
			}else{
				$file['a_viewfile'] = urr("downfile","action=view&file_id={$file['file_id']}&file_key={$file['file_key']}");
				$file['file_view_url'] = $settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}");
				$file['file_html_url'] = '<a href='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").' target=_blank>'.$file['file_name'].'</a>';
				$file['file_ubb_url'] = '[url='.$settings['phpdisk_url'].urr("viewfile","file_id={$file['file_id']}&file_key={$file['file_key']}").']'.$file['file_name'].'[/url]';
			}			
			$file['preview_link'] = $settings['phpdisk_url'].$settings['file_path'].'/'.$tmp_username.$file['file_store_path'].$file['file_real_name'].$file_ext;
		}
		return $file;
	}
}
$file = super_cache('file','cur_file|'.$file_id,1,0,'file|'.$file_time,$settings['cache_time']);

if($settings['open_report']){
	$rs = $db->fetch_one_array("select count(*) as total from {$tpf}reports where userid='$pd_uid' and file_id='$file_id'");
	$has_report = $rs['total'] ? 1 : 0;
	$a_report_file = urr("viewfile","action=report&file_id=$file_id&file_key=$file_key");
	if(!$pd_uid){
		$login_txt = $L['please_login'];
		$disabled = 'disabled';
	}
}
if($settings['show_relative_file']){
	function relate_file($file_id){
		global $db,$tpf,$file;
		$q = $db->query("select file_id,file_key,file_name,file_extension,file_size,file_time from {$tpf}files where ((is_public=1 and is_checked=1) or in_share=1) and in_recycle=0 and file_extension='{$file['file_extension']}' and file_id<>'$file_id' order by file_id desc limit 20");
		$relate_file = array();
		while($rs = $db->fetch_array($q)){
			$tmp_ext = $rs['file_extension'] ? '.'.$rs['file_extension'] : "";
			$rs['file_name_all'] = $rs['file_name'].$tmp_ext;
			$rs['file_name'] = cutstr($rs['file_name'].$tmp_ext,18);
			$rs['file_size'] = get_size($rs['file_size']);
			$rs['file_time'] = custom_time("Y-m-d",$rs['file_time']);
			$rs['a_viewfile'] = urr("viewfile","file_id={$rs['file_id']}");
			$relate_file[] = $rs;
		}
		$db->free($q);
		unset($rs);
		return $relate_file;
	}
	$relate_file = super_cache('file','relate_file|'.$file_id,1,0,'file|'.$file_time,$settings['cache_time']);
}

if($settings['open_comment']){
	function last_comment($file_id){
		global $db,$tpf;
		$q = $db->query("select c.*,u.username from {$tpf}comments c,{$tpf}users u where file_id='$file_id' and is_checked=1 and c.userid=u.userid order by cmt_id desc limit 5");
		$cmts = array();
		while($rs = $db->fetch_array($q)){
			$rs['content'] = str_replace("\r\n","<br>",$rs['content']);
			$rs['in_time'] = custom_time("Y-m-d H:i:s",$rs['in_time']);
			$rs['a_space'] = urr("space","username=".rawurlencode($rs['username']));
			$cmts[] = $rs;
		}
		$db->free($q);
		unset($rs);
		return $cmts;
	}
	$cmts = super_cache('file','last_comment|'.$file_id,1,0,'file|'.$file_time,$settings['cache_time']);
	$a_comment = urr("comment","file_id=$file_id");
}

$title = $file['file_name'].' - '.$settings['site_title'];

if($settings['open_seo'] && $settings['open_tag']){
	function file2tag($file_id){
		global $db,$tpf;
		$q = $db->query("select * from {$tpf}file2tag where file_id='$file_id'");
		while($rs = $db->fetch_array($q)){
			$file_tags .= $rs['tag_name'].',';
		}
		$db->free($q);
		unset($rs);
		return $file_tags;
	}
	$file_tags = super_cache('file','file2tag|'.$file_id,1,0,'file|'.$file_time,$settings['cache_time']);
	$file_keywords = trim(substr($file_tags,0,-1)).$file['file_name'].',';
	$file_description = $file['file_name'].','.str_replace("\r\n",'',preg_replace("/<.+?>/i","",$file_description));
}

$loading_secs = 0;
if($pd_uid && $pd_gid){
	if($settings['secs_for_user']){
		$group_set = $group_settings[$pd_gid];
		$loading_secs = min((int)$group_set['secs_loading'],(int)$settings['secs_loading']);
	}else{
		$loading_secs = 0;
	}
}else{
	$loading_secs = (int)$settings['secs_loading'];
}

include PHPDISK_ROOT."./includes/header.inc.php";

switch($action){
	case 'report':
		form_auth(gpc('formhash','P',''),formhash());

		$content = trim(gpc('content','P',''));
		$file_id = (int)gpc('file_id','P',0);
		$file_key = gpc('file_key','P',0);

		if(checklength($content,2,250)){
			$error = true;
			$sysmsg[] = $L['report_content_error'];
		}
		$rs = $db->fetch_one_array("select count(*) as total from {$tpf}reports where file_id='".$file_id."' and userid='$pd_uid'");
		if($rs['total']){
			$error = true;
			$sysmsg[] = $L['report_already_exists'];
		}
		unset($rs);
		if(!$error){
			$ins = array(
			'userid' => $pd_uid,
			'file_id' => $file_id,
			'file_key' => $db->escape($file_key),
			'content' => $db->escape(replace_js($content)),
			'in_time' => $timestamp,
			'ip' => $onlineip,
			'is_new' => 1,
			);
			$db->query("insert into {$tpf}reports set ".$db->sql_array($ins).";");
			$db->query_unbuffered("update {$tpf}files set report_status=1 where file_id='$file_id'");
			$sysmsg[] = $L['report_success'];
			redirect(urr("viewfile","file_id=$file_id&file_key=$file_key"),$sysmsg,5000);
		}else{
			redirect('back',$sysmsg);
		}

		break;

	case 'comment':
		form_auth(gpc('formhash','P',''),formhash());

		$content = trim(gpc('content','P',''));
		$file_id = (int)gpc('file_id','P',0);
		$file_key = gpc('file_key','P',0);

		if(checklength($content,2,600)){
			$error = true;
			$sysmsg[] = $L['cmt_content_error'];
		}
		if(!$error){
			$ins = array(
			'userid' => $pd_uid,
			'file_id' => $file_id,
			'file_key' => $db->escape($file_key),
			'content' => $db->escape(replace_js($content)),
			'in_time' => $timestamp,
			'ip' => $onlineip,
			'is_checked' => $settings['check_comment'] ? 0 : 1,
			);
			$db->query("insert into {$tpf}comments set ".$db->sql_array($ins).";");
			$sysmsg[] = $L['cmt_success'];
			redirect(urr("viewfile","file_id=$file_id&file_key=$file_key"),$sysmsg,5000);
		}else{
			redirect('back',$sysmsg);
		}
		break;
}

require_once template_echo('pd_viewfile',$user_tpl_dir);

$db->query_unbuffered("update {$tpf}files set file_views=file_views+1 where file_id='$file_id'");

include PHPDISK_ROOT."./includes/footer.inc.php";

function get_file_tags($file_id){
	global $db,$tpf,$pd_uid,$settings;
	$tags = '';
	if($settings['open_tag']){
		$q = $db->query("select * from {$tpf}file2tag where file_id='$file_id'");
		while($rs = $db->fetch_array($q)){
			$tags .= " <a href='".urr("tag","tag=".rawurlencode($rs['tag_name'])."")."' target='_blank'>".$rs['tag_name']."</a> ,";
		}
		$db->free($q);
		unset($rs);
	}
	return substr($tags,-1)==',' ? trim(substr($tags,0,-1)) : trim($tags);
}

?>