<?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 81 2011-04-23 06:21:09Z 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['is_del'] = 0;
$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{
	$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");
	$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);
	$store_old = $file['store_old'];
	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($store_old){
				$file['file_thumb'] = $rs2['server_host'].$rs2['server_store_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].'_thumb';

			}else{
				$file['file_thumb'] = $rs2['server_host'].$rs2['server_store_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].'_thumb.'.$file_extension;
			}
		}
		unset($rs2);
	}else{
		if($store_old){
			$file['file_thumb'] = $settings['file_path'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].'_thumb';
		}else{
			$file['file_thumb'] = $settings['file_path'].'/'.$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'] = get_file_tags($file_id);

	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";
			}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 = $file['store_old'] ? $str : $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'] = $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'].'/'.$file['file_store_path'].'/'.$file['file_real_name'];
			$file_url = $file['store_old'] ? $str : $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'].'/'.$file['file_store_path'].'/'.$file['file_real_name'].$file_ext;
	}
}
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']){
	$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,25);
		$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);
}

if($settings['open_comment']){
	$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);
	$a_comment = urr("comment","file_id=$file_id");
}

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

if($settings['open_seo'] && $settings['open_tag']){
	$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);

	$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);
}

?>