<?php 
/**
#	Project: PHPDISK File Storage Solution
#	This is NOT a freeware, use is subject to license terms.
#
#	Site: http://www.phpdisk.com
#
#	$Id: tag.php 25 2011-03-04 07:36:51Z along $
#
#	Copyright (C) 2008-2011 PHPDisk Team. All Rights Reserved.
#
*/

include "includes/commons.inc.php";

$in_front = true;
lang('tag');

$tag = $db->escape(trim(gpc('tag','G','')));
if($tag){
	$title = $L['tag'].': '.$tag;
}else{
	$title = $L['tag_view'];
}
include PHPDISK_ROOT."./includes/header.inc.php";

if(!$tag){
	$q = $db->query("select * from {$tpf}tags where is_hidden=0 and tag_count>0 order by tag_count desc,tag_id desc limit 30");
	$hot_tags = array();
	while($rs = $db->fetch_array($q)){
		$rs['a_view_tag'] = urr("tag","tag=".rawurlencode($rs['tag_name'])."");
		$rs['tag_count'] = $rs['tag_count'] ? "({$rs['tag_count']})" : '';
		$hot_tags[] = $rs;
	}
	$db->free($q);
	unset($rs);
	
	$q = $db->query("select * from {$tpf}tags where is_hidden=0 and tag_count>0 order by tag_id desc limit 30");
	$last_tags = array();
	while($rs = $db->fetch_array($q)){
		$rs['a_view_tag'] = urr("tag","tag=".rawurlencode($rs['tag_name'])."");
		$rs['tag_count'] = $rs['tag_count'] ? "({$rs['tag_count']})" : '';
		$last_tags[] = $rs;
	}
	$db->free($q);
	unset($rs);

}else{
	$q = $db->query("select file_id from {$tpf}file2tag where tag_name='{$tag}'");
	while($rs = $db->fetch_array($q)){
		$file_ids .= $rs['file_id'].',';
	}
	$db->free($q);
	unset($rs);

	$file_ids = (substr($file_ids,-1) ==',') ? substr($file_ids,0,-1) : $file_ids;
	if(!$file_ids){
		header("Location: ".urr("tag",""));
	}
	
	$sql_do = "{$tpf}files where file_id in ($file_ids)";
	
	$rs = $db->fetch_one_array("select count(*) as total_num from {$sql_do}");
	$total_num = $rs['total_num'];
	$start_num = ($pg-1) * $perpage;
	
	$q = $db->query("select file_id,file_key,file_name,file_extension,file_size,file_time,server_oid,file_store_path,file_real_name,is_image,store_old from {$sql_do} order by file_id desc limit $start_num,$perpage");
	$files_array = array();
	while($rs = $db->fetch_array($q)){
		$tmp_ext = $rs['file_extension'] ? '.'.$rs['file_extension'] : "";
		$rs['file_thumb'] = get_file_thumb($rs);
		$rs['file_name_all'] = $rs['file_name'].$tmp_ext;
		$rs['file_name'] = cutstr($rs['file_name'].$tmp_ext,35);
		$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']}");
		$files_array[] = $rs;
	}
	$db->free($q);
	unset($rs);
	
	$page_nav = multi($total_num, $perpage, $pg, urr("tag","tag=".rawurlencode($tag)));
}

$q = $db->query("select file_id,file_key,file_name,file_views,file_extension,file_size,file_time from {$tpf}files where in_share=1 order by file_views desc limit 10");
$hot_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,35);
	$rs['file_size'] = get_size($rs['file_size']);
	$rs['file_time'] = custom_time("Y-m-d",$rs['file_time']);
	$rs['file_views'] = $rs['file_views'] ? "({$rs['file_views']})" : "";
	$rs['a_viewfile'] = urr("viewfile","file_id={$rs['file_id']}");
	$hot_file[] = $rs;
}
$db->free($q);
unset($rs);

require_once template_echo('pd_tag',$user_tpl_dir);

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


?>