<?php
define('THIS_SCRIPT', 'uploadlist');
include_once 'global.php';
include_once CACHE_PATH.'board/board_all.php';
include_once CACHE_PATH.'forum_setting.php';
$lang = load_lang($lang, 'uploadlist' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'35,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$q_search,$action,$doaction,$bordertype,$page_size,$page,$page_count,$offset,$total,$adjunct_list,$pa,$fsetting,$_DCACHE,$uploaddir;
	$action=$_GET['action'];
	$uploaddir=basename($_DCACHE['settings']['forumuploadname']);
	if($action=="quick_search"){
		quick_search();
	}
	if($action=="highgrade_search"){
		highgrade_search();
	}
	if($action=="norecord"){
		no_record_file();
	}
	SWITCH($doaction){
		CASE "del_selfile":
			del_selectfile();
		BREAK;
		CASE "clr_currec":
			clear_currecord();
		BREAK;
		CASE "clr_allrec":
			clear_allrecord();
		BREAK;
	}
include printout('uploadlist');
}
function quick_search(){
	global $q_search,$lang,$dv,$db,$page_size,$page,$page_count,$offset,$total,$adjunct_list,$pa,$uploaddir;
	$q_search=intval($_POST['q_search']);
	$adjunct_list=array();
	$page_size=30;//分页部分;
	$sql="select u.f_id,u.f_announceid,u.f_boardid,u.f_filename,u.f_username,u.f_filetype,u.f_type,u.f_filesize,u.f_downnum,u.f_viewnum,u.f_addtime ,b.boardtype from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0";
	switch ($q_search){
		case 1:
			$sql.=" order by f_id desc";
			$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0");
		break;
		case 2:
			$time_start=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'));
			$time_end=$time_start+(24*3600-1);
			$sql.=" and (f_addtime between ".$time_start." and ".$time_end.")";
			$sql.=" order by f_id desc";
			$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0  and (f_addtime between ".$time_start." and ".$time_end.")");
		break;
		case 3:
			$endaddtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(30*24*3600);
			$sql.=" and f_addtime<'".$endaddtime."'";
			$sql.=" order by f_id desc";
			$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0  and f_addtime<'".$endaddtime."'");
		break;
		case 4:
			$endaddtime=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-(90*24*3600);
			$sql.=" and f_addtime<'".$endaddtime."'";
			$sql.=" order by f_id desc";
			$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0  and f_addtime<'".$endaddtime."'");
		break;
		case 5:
			$sql.=" order by f_downnum desc";
			$page_size = 100;
			$total=$db->scalar("select count(f_downnum) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0 order by f_downnum desc");
		break;
		case 6:
			$sql.=" order by f_viewnum desc";
			$page_size = 100;
			$total=$db->scalar("select count(f_viewnum) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0 order by f_viewnum desc");
		break;
		default:
			$sql.=" order by f_id desc";
			$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0");
		break;
	}
	if ($page=="" or $page<1)
		$page=1;
	 $page_count=ceil($total/$page_size);
	if ($page>$page_count)
		$page=$page_count;
	$offset=$page;
	if( $offset < 1 ) $offset = 1;
	$result=$db->query($sql,array('absolutePage'=>$offset,'pageSize'=>$page_size));
	while ($arr=$db->fetch_array($result)){
		$adjunct_list[]=$arr;
	}
	if($total<=0){
		show_msg($lang['sys.info'],$lang['sys.info1'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function highgrade_search(){
	global $lang,$dv,$db,$bordertype,$filedown_num,$filedown_num_type,$adjunctview_num,$adjunctview_num_type,$update_num,$update_num_type,$adjunct_write,$adjunct_write_type,$adjunct_explain,$adjunct_explain_type,$adjunct_size,$adjunct_size_type,$adjunct_sort,$adjunct_type,$page_size,$page,$page_count,$offset,$total,$adjunct_list,$pa,$uploaddir;
	$adjunct_list=array();
	$search_str="";
	$bordertype=intval($_POST['bordertype']);//所属板块
	$filedown_num=intval($_POST['filedown_num']);//下载次数
	$filedown_num_type=intval($_POST['filedown_num_type']);
	$adjunctview_num=intval($_POST['adjunctview_num']);//浏览次数
	$adjunctview_num_type=intval($_POST['adjunctview_num_type']);
	$update_num=intval($_POST['update_num']);//上传天数
	$update_num_type=intval($_POST['update_num_type']);
	$adjunct_write=str_replace(" ","",$_POST['adjunct_write']);//作者
	$adjunct_write_type=$_POST['adjunct_write_type'];
	$adjunct_explain=str_replace(" ","",$_POST['adjunct_explain']);//说明
	$adjunct_explain_type=$_POST['adjunct_explain_type'];
	$adjunct_size=intval($_POST['adjunct_size']);//大小
	$adjunct_size_type=intval($_POST['adjunct_size_type']);
	$adjunct_sort=$_POST['adjunct_sort'];//分类
	$adjunct_type=$_POST['adjunct_type'];//类型
	if($bordertype!="" and $bordertype>0){//所属版块条件
		$search_str.=" f_boardid=".$bordertype." and ";
	}
	if($filedown_num!="" and $filedown_num>0){//下载次数条件
		if($filedown_num_type==1){
			$search_str.="f_downnum>=".$filedown_num." and ";
		}
		else{
			$search_str.="f_downnum<=".$filedown_num." and ";
		}
	}
	if($adjunctview_num!="" and $adjunctview_num>0){//浏览次数条件
		if($adjunctview_num_type==1){
			$search_str.="f_viewnum>=".$adjunctview_num." and ";
		}
		else{
			$search_str.="f_viewnum<=".$adjunctview_num." and ";
		}
	}
	if($update_num!="" and $update_num>0){//上传天数条件
		$endupdate_num=dv_gmtimestamp(gmdate('Y-m-d 00:00:00'))-($update_num*24*3600);
		if($update_num_type==1){
			$search_str.="f_addtime<'".$endupdate_num."' and ";
		}
		else{
			$search_str.="f_addtime>'".$endupdate_num."' and ";
		}
	}
	if($adjunct_write!=""){//附件作者
		if($adjunct_write_type=="Y"){
			$search_str="f_username='".$adjunct_write."' and ";
		}
		else{
			$search_str="f_username like '%$adjunct_write%' and ";
		}
	}
	if($adjunct_explain!=""){//附件说明
		if($adjunct_explain_type=="Y"){
			$search_str="f_readme='".$adjunct_explain."' and ";
		}
		else{
			$search_str="f_readme='%$adjunct_explain%' and ";
		}
	}
	if($adjunct_size!="" and $adjunct_size>0){//附件大小条件
		if($adjunct_size_type==1){
			$search_str.="f_filesize>=".$adjunct_size." and ";
		}
		else{
			$search_str.="f_filesize<=".$adjunct_size." and ";
		}
	}
	if($adjunct_type==""){//附件类型条件
		$search_str.=$search_str;
	}
	else{
		$search_str.="f_filetype='".$adjunct_type."' and ";
	}
	if($adjunct_sort!=""){//附件分类条件
		if($adjunct_sort=="all"){
			$search_str.=$search_str;
		}
		else{
			$adjunct_sort=intval($_POST['adjunct_sort']);//分类
			$search_str.="f_type=".$adjunct_sort." and ";
		}
	}
	else{
		$search_str=substr($search_str,0,strlen($search_str)-5);
	}
	$search_str=substr($search_str,0,strlen($search_str)-5);
	$order=" order by f_id desc";
	if($search_str==""){
		$sql="select u.f_id,u.f_announceid,u.f_boardid,u.f_filename,u.f_username,u.f_filetype,u.f_type,u.f_filesize,u.f_downnum,u.f_viewnum,u.f_addtime ,b.boardtype from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0";
		$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0");
	}
	else{
		$sql="select u.f_id,u.f_announceid,u.f_boardid,u.f_filename,u.f_username,u.f_filetype,u.f_type,u.f_filesize,u.f_downnum,u.f_viewnum,u.f_addtime ,b.boardtype from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0 and ".$search_str."";
		$total=$db->scalar("select count(1) from {$dv}upfile u,{$dv}board b where b.boardid=u.f_boardid and f_flag=0 and ".$search_str."");
	}
	if($total<=0){
		show_msg($lang['sys.info'],$lang['sys.info1'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$page_size=30;//分页部分;
		if ($page=="" or $page<1)
			$page=1;
		 $page_count=ceil($total/$page_size);
		if ($page>$page_count)
			$page=$page_count;
		$offset=$page;
		if( $offset < 1 ) $offset = 1;
		$result=$db->query($sql.$order,array('absolutePage'=>$offset,'pageSize'=>$page_size));
		while($arr=$db->fetch_array($result)){
			$adjunct_list[]=$arr;
		}
	}
$db->free_result($result);
}
function del_selectfile(){//执行删除所选文件,同时删除文件和帖子;
	global $lang,$dv,$db,$delid,$uploaddir;
	$delid=$_POST['delid'];
	$count=count($delid);
	IF(count($delid)=="" or count($delid)===0){
		show_msg($lang['sys.info'],$lang['sys.info13'],0,'','','uploadlist.php',$lang['cmdback']);
	exit;
	}
	for($i=0;$i<$count;$i++){
		$sql=$db->query("select f_id,f_filename from {$dv}upfile where f_id=".$delid[$i]."");
		while($arr=$db->fetch_array($sql)){
			if($file_exist=strpos($arr['f_filename'],":")===false or $file_exist=strpos($arr['f_filename'],"//")===false){
				$f_path=$arr['f_filename'];
				$outfile_num+=1;
			}
			else{
				$f_path=$lang['dir_path'].$arr['f_filename'];
				$infile_num+=1;
			}
			unlink($f_path);
			$stmt=$db->query("delete from {$dv}upfile where f_id=".$arr['f_id']."");
		}
	}
	$del_allfile=$outfile_num+$infile_num;
	if($stmt){
		show_msg($lang['sys.info'],$lang['sys.info2'].$del_allfile.$lang['sys.info3'].$lang['sys.info4'].$outfile_num.$lang['sys.info5'].$infile_num,1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['sys.info6'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
$db->free_result($sql);
}
function clear_currecord(){//清理当前列表记录
	global $lang,$dv,$db,$clearfile_type,$delid,$uploaddir;
	$delid=$_POST['delid'];
	$clearfile_type=intval($_POST['clearfile_type']);
	$count=count($delid);
	IF(count($delid)=="" or count($delid)===0){
		show_msg($lang['sys.info'],$lang['sys.info13'],0,'','','uploadlist.php',$lang['cmdback']);
	exit;
	}
	for($i=0;$i<$count;$i++){
		$sql=$db->query("select f_id,f_filename from {$dv}upfile where f_id=".$delid[$i]."");
		while($arr=$db->fetch_array($sql)){
			if($file_exist=strpos($arr['f_filename'],":")===false or $file_exist=strpos($arr['f_filename'],"//")===false){
				$f_path=$arr['f_filename'];
				$outfile_num+=1;
			}
			else{
				$f_path=$lang['dir_path'].$arr['f_filename'];
				$infile_num+=1;
			}
			if($clearfile_type){
				unlink($f_path);
				$stmt=$db->query("delete from {$dv}upfile where f_id=".$arr['f_id']."");
			}
			else{
				$stmt=$db->query("delete from {$dv}upfile where f_id=".$arr['f_id']."");
			}
		}
	}
	$del_allfile=$outfile_num+$infile_num;
	if($stmt){
		show_msg($lang['sys.info'],$lang['sys.info2'].$del_allfile.$lang['sys.info3'].$lang['sys.info4'].$outfile_num.$lang['sys.info5'].$infile_num,1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['sys.info6'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
$db->free_result($sql);
}
function clear_allrecord(){//清理所有上传记录;
	global $lang,$dv,$db,$file_list,$uploaddir;
	$file_list=array();
	$upfile_dir="/";
	$uploaddir='../'.$uploaddir.'/forum/';
	$sql=$db->query("select f_id,f_filename from {$dv}upfile");
	WHILE ($arr=$db->fetch_array($sql)){
		IF($file_exist=strpos($arr['f_filename'],":")===false or $file_exist=strpos($arr['f_filename'],"//")===false){
			$tmp_arr=explode("/",$arr['f_filename']);
			IF(is_dir($uploaddir.$tmp_arr[1].$upfile_dir)){//uploadfile/;
				IF ($dir_hand = opendir($uploaddir.$tmp_arr[1].$upfile_dir)) {
					WHILE(($filename=readdir($dir_hand))!==FALSE){
						IF($filename=="." or $filename==".."){
							continue;
						}
						ELSE{
							IF($arr['f_filename']!=$tmp_arr[0].$upfile_dir.$tmp_arr[1].$upfile_dir.$filename){
								$stmt=$db->query("delete from {$dv}upfile where f_filename='".$arr['f_filename']."'");
								$file_num+=1;
							}
						}
					}
				}
			}
		}
	}
	IF($file_num>0){
		show_msg($lang['sys.info'],$lang['sys.info11'].$file_num.$lang['sys.info9'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	ELSE{
		show_msg($lang['sys.info'],$lang['sys.info12'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
function no_record_file(){//清除未记录文件
	global $lang,$dv,$db,$upfile_dir,$file_list,$uploaddir;
	$file_list=array();
	$upfile_dir=$_POST['upfile_dir'];
	$upfile_dir=str_replace(" ","",$upfile_dir);
	$upfile_dir.="/";
	$uploaddir='../'.$uploaddir.'/forum/';
	if (is_dir($uploaddir.$upfile_dir)) {
		if ($dir_hand = opendir($uploaddir.$upfile_dir)) {
			while (($file = readdir($dir_hand)) !== false) {
				if($file=="." or $file==".."){
					continue;
				}
				else{
					$file_list[]=$file;
					$total+=1;
				}
			}
			closedir($dir_hand);
		}
	}
	else{
		show_msg($lang['sys.info'],$lang['sys.info7'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	for($i=0;$i<$total;$i++){
		$file_dir=$uploaddir.$upfile_dir.$file_list[$i];
		$fileis_exist=$db->scalar("select count(1) from {$dv}upfile where f_filename='".$file_dir."'");
		if($fileis_exist<=0){
			unlink($file_dir);
			$delfile_num+=1;
		}
	}
	if($delfile_num>0){
		show_msg($lang['sys.info'],$lang['sys.info8'].$delfile_num.$lang['sys.info9'],1,'','','uploadlist.php',$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['sys.info10'],0,'','','uploadlist.php',$lang['cmdback']);
	exit;
	}
}
?>