<?php
//刪除文件
function DelFile($fileid,$userid,$username){
	global $empire,$class_r,$dbtbpre;
	$fileid=(int)$fileid;
	if(!$fileid)
	{printerror("NotFileid","history.go(-1)");}
	//操作權限
	CheckLevel($userid,$username,$classid,"file");
	$modtype=(int)$_GET['modtype'];
	$fstb=(int)$_GET['fstb'];
	$r=$empire->fetch1("select filename,path,classid,fpath from ".eReturnFileTable($modtype,$fstb)." where fileid='$fileid'".do_dblimit_one());
	$sql=$empire->query("delete from ".eReturnFileTable($modtype,$fstb)." where fileid='$fileid'");
	DoDelFile($r);
	if($sql)
	{
		//操作日誌
		insert_dolog("fileid=".$fileid."<br>filename=".$r['filename']);
		printerror("DelFileSuccess",EcmsGetReturnUrl());
    }
	else
	{
		printerror("DbError","history.go(-1)");
    }
}

//批量刪除文件
function DelFile_all($fileid,$userid,$username){
	global $empire,$dbtbpre,$class_r;
	//操作權限
	if($_POST['enews']=='TDelFile_all')
	{
		$userid=(int)$userid;
		$ur=$empire->fetch1("select groupid,adminclass,filelevel from {$dbtbpre}enewsuser where userid='$userid'".do_dblimit_one());
		if($ur['filelevel'])
		{
			$gr=$empire->fetch1("select dofile from {$dbtbpre}enewsgroup where groupid='".$ur['groupid']."'");
			if(!$gr['dofile'])
			{
				$classid=(int)$_POST['classid'];
				$searchclassid=(int)$_POST['searchclassid'];
				$classid=$searchclassid?$searchclassid:$classid;
				if(!$class_r[$classid]['classid'])
				{
					printerror("NotLevel","history.go(-1)");
				}
				if(!strstr($ur['adminclass'],'|'.$classid.'|'))
				{
					printerror("NotLevel","history.go(-1)");
				}
			}
		}
		else
		{
			CheckLevel($userid,$username,$classid,"file");
		}
	}
	else
	{
		CheckLevel($userid,$username,$classid,"file");
	}
	$fileid=eCheckEmptyArray($fileid);
	$count=count($fileid);
	if(!$count)
	{printerror("NotFileid","history.go(-1)");}
	$modtype=(int)$_POST['modtype'];
	$fstb=(int)$_POST['fstb'];
	for($i=0;$i<count($fileid);$i++)
	{
		$fileid[$i]=(int)$fileid[$i];
		$r=$empire->fetch1("select filename,path,classid,fpath from ".eReturnFileTable($modtype,$fstb)." where fileid='".$fileid[$i]."'".do_dblimit_one());
		$sql=$empire->query("delete from ".eReturnFileTable($modtype,$fstb)." where fileid='".$fileid[$i]."'");
		DoDelFile($r);
    }
	if($sql)
	{
		//操作日誌
		insert_dolog("");
		printerror("DelFileAllSuccess",EcmsGetReturnUrl());
    }
	else
	{
		printerror("DbError","history.go(-1)");
    }
}

//刪除多餘附件
function DelFreeFile($userid,$username){
	global $empire,$dbtbpre;
	//操作權限
	CheckLevel($userid,$username,$classid,"file");
	//清理信息附件
	DelFileAllTable("cjid<>0 and (id=0 or cjid=id)");
	//會員附件
	DelFileOtherTable("cjid<>0 and (id=0 or cjid=id)","member");
	//其他附件
	DelFileOtherTable("cjid<>0 and (id=0 or cjid=id)","other");
	//操作日誌
	insert_dolog("");
	printerror("DelFreeFileSuccess",EcmsGetReturnUrl());
}

//刪除目錄文件
function DelPathFile($filename,$userid,$username){
	global $empire,$dbtbpre,$public_r,$efileftp_dr;
	//操作權限
	CheckLevel($userid,$username,$classid,"file");
	$filename=eCheckEmptyArray($filename);
	$count=count($filename);
	if(empty($count))
	{
		printerror("NotFileid","history.go(-1)");
	}
	//基目錄
	$basepath=eReturnEcmsMainPortPath()."d/file";//moreport
	for($i=0;$i<$count;$i++)
	{
		if(strstr($filename[$i],".."))
		{
			continue;
	    }
		if(!file_exists($basepath."/".$filename[$i]))
		{
			continue;
		}
		DelFiletext($basepath."/".$filename[$i]);
		$dfile=ReturnPathFile($filename[$i]);
		$dfile=hRepPostStr($dfile,1);
		$dfnum=$empire->gettotal("select count(*) as total from {$dbtbpre}enewsfile_1 where filename='$dfile'");
		if($dfnum)
		{
			$empire->query("delete from {$dbtbpre}enewsfile_1 where filename='$dfile'");
			//FileServer
			if($public_r['openfileserver'])
			{
				$efileftp_dr[]=$basepath."/".$filename[$i];
			}
		}
    }
	//操作日誌
	insert_dolog("");
	printerror("DelFileSuccess",EcmsGetReturnUrl());
}

//批量修改文件別名
function EditFile_all($add,$userid,$username){
	global $empire,$dbtbpre,$class_r;
	//操作權限
	if($_POST['enews']=='TEditFile_all')
	{
		$userid=(int)$userid;
		$ur=$empire->fetch1("select groupid,adminclass,filelevel from {$dbtbpre}enewsuser where userid='$userid'".do_dblimit_one());
		if($ur['filelevel'])
		{
			$gr=$empire->fetch1("select dofile from {$dbtbpre}enewsgroup where groupid='".$ur['groupid']."'");
			if(!$gr['dofile'])
			{
				$classid=(int)$_POST['classid'];
				$searchclassid=(int)$_POST['searchclassid'];
				$classid=$searchclassid?$searchclassid:$classid;
				if(!$class_r[$classid]['classid'])
				{
					printerror("NotLevel","history.go(-1)");
				}
				if(!strstr($ur['adminclass'],'|'.$classid.'|'))
				{
					printerror("NotLevel","history.go(-1)");
				}
			}
		}
		else
		{
			CheckLevel($userid,$username,$classid,"file");
		}
	}
	else
	{
		CheckLevel($userid,$username,$classid,"file");
	}
	$dofileid=eCheckEmptyArray($add['dofileid']);
	$dofileno=$add['dofileno'];
	$count=count($dofileid);
	if(!$count)
	{
		printerror("ErrorUrl","history.go(-1)");
	}
	$modtype=(int)$_POST['modtype'];
	$fstb=(int)$_POST['fstb'];
	for($i=0;$i<count($dofileid);$i++)
	{
		$dofileid[$i]=(int)$dofileid[$i];
		if(!$dofileid[$i])
		{
			continue;
		}
		$dofileno[$i]=addslashes(dgdbe_rpstr($dofileno[$i]));
		if(!$dofileno[$i])
		{
			continue;
		}
		$sql=$empire->query("update ".eReturnFileTable($modtype,$fstb)." set no='".$dofileno[$i]."' where fileid='".$dofileid[$i]."'");
    }
	if($sql)
	{
		//操作日誌
		insert_dolog("");
		printerror("EditFileAllSuccess",EcmsGetReturnUrl());
    }
	else
	{
		printerror("DbError","history.go(-1)");
    }
}

//批量加水印/縮略圖
function DoMarkSmallPic($add,$userid,$username){
	global $empire,$class_r,$dbtbpre,$public_r,$efileftp_fr;
	//導入gd處理文件
	if($add['getsmall']||$add['getmark'])
	{
		@include(ECMS_PATH."e/class/gd.php");
	}
	else
	{
		printerror("EmptyDopicFileid","history.go(-1)");
	}
	$fileid=eCheckEmptyArray($add['fileid']);
	$count=count($fileid);
	if($count==0)
	{
		printerror("EmptyDopicFileid","history.go(-1)");
	}
	$add['classid']=(int)$add['classid'];
	$modtype=(int)$add['modtype'];
	$fstb=(int)$add['fstb'];
	$fstb=eReturnFileStb($fstb);
	for($i=0;$i<$count;$i++)
	{
		$fileid[$i]=intval($fileid[$i]);
		$r=$empire->fetch1("select classid,filename,path,no,fpath,cid,cid2 from ".eReturnFileTable($modtype,$fstb)." where fileid='".$fileid[$i]."'");
		$rpath=$r['path']?$r['path'].'/':$r['path'];
		$fspath=ReturnFileSavePath($r['classid'],$r['fpath']);
		$path=eReturnEcmsMainPortPath().$fspath['filepath'].$rpath;//moreport
		$yname=$path.$r['filename'];
		//縮略圖
		if($add['getsmall'])
		{
			$filetype=GetFiletype($r['filename']);
			$insertfile=substr($r['filename'],0,strlen($r['filename'])-strlen($filetype)).time();
			$name=$path."small".$insertfile;
			GetMySmallImg($add['classid'],$r['no'],$insertfile,$r['path'],$yname,$add['width'],$add['height'],$name,$add['filepass'],$add['filepass'],$userid,$username,$modtype,$fstb,$r['cid'],$r['cid2']);
		}
		//水印
		if($add['getmark'])
		{
			GetMyMarkImg($yname);
			//FileServer
			if($public_r['openfileserver'])
			{
				$efileftp_fr[]=$yname;
			}
		}
	}
	printerror("DoMarkSmallPicSuccess",EcmsGetReturnUrl());
}

//修改附件文件
function EditFileOne($add,$userid,$username){
	global $empire,$dbtbpre,$class_r;
	$modtype=(int)$add['modtype'];
	$fstb=(int)$add['fstb'];
	$fileid=(int)$add['fileid'];
	$no=addslashes(dgdbe_rpstr($add['fileno']));
	$cid=(int)$add['cid'];
	$cid2=(int)$add['cid2'];
	if(!$fileid)
	{
		printerror("ErrorUrl","history.go(-1)");
	}
	$r=$empire->fetch1("select * from ".eReturnFileTable($modtype,$fstb)." where fileid='$fileid'");
	if(!$r['fileid'])
	{
		printerror("ErrorUrl","history.go(-1)");
	}
	//操作權限
	if($add['enews']=='TEditFileOne')
	{
		$userid=(int)$userid;
		$ur=$empire->fetch1("select groupid,adminclass,filelevel from {$dbtbpre}enewsuser where userid='$userid'".do_dblimit_one());
		if($ur['filelevel'])
		{
			$gr=$empire->fetch1("select dofile from {$dbtbpre}enewsgroup where groupid='".$ur['groupid']."'");
			if(!$gr['dofile'])
			{
				$classid=(int)$r['classid'];
				if(!$class_r[$classid]['classid'])
				{
					printerror("NotLevel","history.go(-1)");
				}
				if(!strstr($ur['adminclass'],'|'.$classid.'|'))
				{
					printerror("NotLevel","history.go(-1)");
				}
			}
		}
		else
		{
			CheckLevel($userid,$username,$classid,"file");
		}
	}
	else
	{
		CheckLevel($userid,$username,$classid,"file");
	}
	//修改
	$upstr='';
	if($no)
	{
		$upstr=",no='".$no."'";
	}
	$sql=$empire->query("update ".eReturnFileTable($modtype,$fstb)." set cid='$cid',cid2='$cid2'".$upstr." where fileid='".$fileid."'");
	if($sql)
	{
		//操作日誌
		insert_dolog("modtype=$modtype&fstb=$fstb&fileid=$fileid");
		printerror("EditFileOneSuccess",EcmsGetReturnUrl());
    }
	else
	{
		printerror("DbError","history.go(-1)");
    }
}

//上傳多附件
function TranMoreFile($file,$file_name,$file_type,$file_size,$no,$type,$userid,$username){
	global $empire,$public_r,$dbtbpre;
	$file_name=eCheckEmptyArray($file_name);
	$count=count($file_name);
	if(empty($count))
	{
		printerror("MustChangeTranOneFile","history.go(-1)");
    }
	//操作權限
	CheckLevel($userid,$username,$classid,"file");
	$type=(int)$type;
	$cid=(int)$_POST['cid'];
	$cid2=(int)$_POST['cid2'];
	for($i=0;$i<$count;$i++)
	{
		if(empty($file_name[$i]))
		{
			continue;
		}
		//取得文件類型
		$filetype=GetFiletype($file_name[$i]);
		//如果是.php文件
		if(CheckSaveTranFiletype($filetype))
		{continue;}
	    $type_r=explode("|".$filetype."|",$public_r['filetype']);
	    if(count($type_r)<2)
		{continue;}
		if($file_size[$i]>$public_r['filesize']*1024)
		{continue;}
		//上傳
		$r=DoTranFile($file[$i],$file_name[$i],$file_type[$i],$file_size[$i],$classid);
		//寫入數據庫
		$r['filesize']=(int)$r['filesize'];
		$classid=(int)$classid;
		if(empty($no[$i]))
		{$no[$i]=$file_name[$i];}

		eInsertFileTable($r['filename'],$r['filesize'],$r['filepath'],$username,$classid,$no[$i],$type,0,0,$public_r['fpath'],0,5,0,$cid,$cid2);
	}
	insert_dolog("");//操作日誌
	printerror("TranMoreFileSuccess","file/TranMoreFile.php".hReturnEcmsHashStrHref2(1));
}

//************************************ 附件分表管理 ************************************

//增加附件分表
function AddFileDataTable($add,$userid,$username){
	echo'This is the Free Version of EmpireCMS.';
	exit();
}

//默認附件存放表
function DefFileDataTable($add,$userid,$username){
	echo'This is the Free Version of EmpireCMS.';
	exit();
}

//刪除附件分表
function DelFileDataTable($add,$userid,$username){
	echo'This is the Free Version of EmpireCMS.';
	exit();
}
?>