<!-- #Include File="Include/AMH_onlogin.asp" -->
<%
'=======================================================
' 欢迎使用《阿赛大型门户网站系统V2.0》，简称“AsaiMHV2”
' 技术支持、商业版咨询、官方唯一指定联系QQ“4941172”
' 官方网站：http://77ya.com      程序负责人：阿赛
'=======================================================

tit="上传文件管理"
Dim FileType,TopDir,ucid,rttype
Dim Fso,FsoFile,AllFileSize
Dim ThisDir,DirFiles,DirFolder
Dim ComeUrl
ComeUrl=Cstr(Request.ServerVariables("HTTP_REFERER"))
ThisDir=Trim(Request("ThisDir"))
ucid=Trim(Request("CID"))
rttype=Trim(Request("ReturnType"))
if ucid<>"" and isnumeric(ucid) then
   Call Cls.Channel_Initialize(ucid)
   ThisDir = Cls.ChannelDir
end if
FileType=LCase(Trim(Request("FileType")))
TopDir=Replace(Cls.Web_Dir&"/"&Cls.Web_Upload&"/","//","/")
Select Case FileType
Case "upload"
	tit_fir="上传文件"
Case "select"
	tit_fir="选择已上传的文件"
End Select
if ThisDir<>"" then
	ThisDir=Replace(ThisDir & "/","//","/")
end if
Call AMH_Header(0)
Set Fso=Server.CreateObject("Scripting.FileSystemObject")
Select Case LCase(Action)
Case "del"
	Call DelAll()
Case "rname"
	Call Rname()
Case Else
	Call Main()
End Select
'if Err then Response.write "错误：" & Err.Description
Set Fso = nothing
Call AMH_Ender(tit)

Sub Main()
	On Error Resume Next
	Set FsoFile = Fso.GetFolder(Server.MapPath(TopDir))
		if Err or TopDir="/" then
			Set FsoFile = nothing
			Response.write "找不到目录，可能参数配置错误！"
			Response.end
		end if
		AllFileSize = FsoFile.size
	Set FsoFile = nothing
	Set FsoFile = Fso.GetFolder(Server.MapPath(TopDir & ThisDir))
	Dim FolderNuns,FileNums
	FolderNuns=FsoFile.SubFolders.count
	FileNums=FsoFile.Files.count
	dim obj_page
	Dim j,k,n
	j=0:k=0:n=0
  set obj_page=New Cls_PageCute
  obj_page.nummer=nummer
  obj_page.pageurl="?action=Main&FileType="&FileType&"&ThisDir="&ThisDir&"&"
  obj_page.page_tit="文件"
  obj_page.page_unit="个"
  obj_page.rssum=FolderNuns+FileNums
  call obj_page.p_info()
%>
<script language=javascript>
function Checked()
{
	var j = 0
	for(i=0;i < document.form.elements.length;i++){
		if(document.form.elements[i].name == "FileId" || document.form.elements[i].name == "FolderId"){
			if(document.form.elements[i].checked){
				j++;
			}
		}
	}
	return j;
}
function CheckAll1()
{
	for(i=0;i<document.form.elements.length;i++)
	{
		if(document.form.elements[i].checked){
			document.form.elements[i].checked=false;
			document.form.CheckAll.checked=false;
		}
		else{
			document.form.elements[i].checked = true;
			document.form.CheckAll.checked = true;
		}
	}
}
function DelAll()
{
	if(Checked()  <= 0){
		alert("您必须选择其中的一个文件或文件夹");
	}	
	else{
		if(confirm("确定要删除选择的文件或文件夹么？\n此操作不可以恢复！"))
		{
			form.action="?action=Del&FileType=<%=FileType%>";
			form.submit();
		}
	}
}
function Rname()
{
	if(Checked() == 0){
		alert("您必须选择一个文件或文件夹");
	}
	else{
		if(Checked() != 1){
			alert("只能选择一个文件或一个文件夹");
		}
		else{
			for(i=0;i < document.form.elements.length;i++){
				if(document.form.elements[i].name == "FolderId" && document.form.elements[i].checked){
					var j = prompt("请输入新文件夹名",document.form.elements[i].value)
					break;
				}
				else if(document.form.elements[i].name == "FileId" && document.form.elements[i].checked){
					var j = prompt("请输入新文件名",document.form.elements[i].value.split(".")[0])
					break;
				}
			}
			if(j != "" && j != null){
				if(IsStr(j) == j.length){
					form.action="?action=Rname&FileType=<%=FileType%>&NewName=" + j;
                                                      form.target="_self";
					form.submit();
				}
				else{
					alert("新名称不符合标准，只能是字母、数字和下划线的组合,\n不能含有汉字、空格、点和其他符号");
				}
			}
		}
	}
}
function IsStr(w)
{
	var str = "abcdefghijklmnopqrstuvwxyz_1234567890"
	 w = w.toLowerCase();
	var j = 0;
	for(i=0;i < w.length;i++){
		if(str.indexOf(w.substr(i,1)) != -1){
			j++;
		}
	}
	return j;
}
</script>
<tr><td align=center colspan=2 height=30>
<table border=0 width='100%'>
 <tr>
  <td align=left>主目录地址：<u><font class=blue><%=TopDir%></font></u>，占用空间：<font class=red><%=Cls.GetSize(AllFileSize,"b")%></font></td>
  <td align=right><a href='?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>' title=返回到主目录><font class=red>返回主目录</font></a></td>
 </tr>
 <tr>
  <td align=left>当前目录地址：<u><font class=blue><%=TopDir%><%=ThisDir%></font></u>，占用空间：<font class=red><%=Cls.GetSize(FsoFile.size,"b")%></font>，文件夹：<font class=blue><%=FolderNuns%></font> 个，文件：<font class=blue><%=FileNums%></font> 个 </td>
  <td align=right><a href='?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>&ThisDir=<%=GetUpDir%>'><font class=red>↑上一目录</font></a></td>
 </tr>
</table>
  </tr>
  </table>
</td></tr>
<%
Call AMH_Table(1)
%>
	<form name="form" method="post" >
	 <tr align=center>
	  <td class=td width='50%'>文件/文件夹名</td>
	  <td class=td width='20%'>最后修改时间</td>
	  <td class=td width='15%'>文件大小</td>
	  <td class=td width='10%'>相关属性</td>
	  <%if FileType="upload" then%>
	  <td class=td width='5%'><input type=checkbox name=CheckAll onClick="CheckAll1()" title=全部选择></td>
	  <%end if%>
	 </tr>
	 <%if ThisDir<>"" then%>
	 <tr align=center>
	  <td align=left colspan=5><img src="Images/folder-up.gif" border=0 width="16" height="16" align="absmiddle">&nbsp;<a href="?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>&ThisDir=<%=GetUpDir%>">返回上一级目录</a></td>
	 </tr>
	 <%end if%>
	 <%
	For Each DirFolder in FsoFile.SubFolders
	n=n+1
	if j>=obj_page.nummer then
		exit for
	elseif n>obj_page.nummer*(obj_page.viewpage-1) then		
	%>
	 <tr align=center>
	  <td align=left><a href='?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>&ThisDir=<%=ThisDir & DirFolder.name%>'><img src="<%=Cls.Web_Dir%>Images/FileType/folder.gif" border=0 width="16" height="16" align="absmiddle"></a>&nbsp;<a href="?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>&ThisDir=<%=ThisDir & DirFolder.name%>"><%=DirFolder.name%></a></td>
	  <td class=tims><%=Cls.time_type(DirFolder.DateLastModified,1)%></td>
	  <td><%=Cls.GetSize(DirFolder.size,"b")%></td>
	  <td><a href='?action=Main&FileType=<%=FileType%>&ReturnType=<%=rttype%>&ThisDir=<%=ThisDir & DirFolder.name%>'>打开</a></td>
	  <%if FileType="upload" then%>
	  <td><input type=checkbox name=FolderId value="<%=DirFolder.name%>"></td>
	  <%end if%>
	 </tr>
	 <%
	j=j+1
	end if
	Next
	For Each DirFiles in FsoFile.Files
	k=k+1
	if j>=obj_page.nummer then
		exit for
	elseif k>obj_page.nummer*(obj_page.viewpage-1) then	
	%>
	 <tr align=center>
	  <td align=left>
	  <%if FileType="select" then%>
	  <a href="#" onClick="window.returnValue='<%=GetFileUrl(TopDir & ThisDir & DirFiles.name)%>|<%=int(DirFiles.size/1000)%>';window.close();" <%=ViewPictrue(TopDir & ThisDir & DirFiles.name)%> >
	  <%else%>
	  <a href='<%=TopDir & ThisDir & DirFiles.name%>' target='_blank' <%=ViewPictrue(TopDir & ThisDir & DirFiles.name)%> >
	  <%end if%>
	  <img src="<%=Cls.Web_Dir%>Images/FileType/<%=GetFileIcon(DirFiles.name)%>" border=0 width=16 height=16 align="absmiddle" title="<%=DirFiles.type%>">&nbsp;<%=DirFiles.name%></a></td>
	  <td class=tims><%=Cls.time_type(DirFiles.DateLastModified,1)%></td>
	  <td><%=Cls.GetSize(DirFiles.size,"b")%></td>
	  <td>
	  <%if FileType="select" then%>
	  <a href="#" onClick="window.returnValue='<%=GetFileUrl(TopDir & ThisDir & DirFiles.name)%>|<%=int(DirFiles.size/1000)%>';window.close();">选择</a>
	  <%else%>
	  <a href="<%=TopDir & ThisDir & DirFiles.name%>" target="_blank">浏览</a> | <a href="?action=Del&FileType=<%=FileType%>&ThisDir=<%=ThisDir%>&FileId=<%=DirFiles.name%>" onClick="return confirm('确定要删除选择的文件么？\n此操作不可以恢复！')">删除</a>
	  <%end if%>
	  </td>
	  <%if FileType="upload" then%>
	  <td><input type=checkbox name=FileId value="<%=DirFiles.name%>"</td>
	  <%end if%>
	 </tr>
	 <%
	j=j+1
	end if
	Next
	%>	
	 <tr class=tr1> 	
	  <%if FileType<>"select" then%>	
	  <td colspan=2>
	  <%else%>	  	
	  <td colspan=5>
	  <%end if%>	  	
	  <table border=0 cellspacing=0 cellpadding=2>
    <tr class=tr1>
    <td><%Response.Write obj_page.page_var%></td>
    <td width=3></td>
    <td>分页：</td>
    <td><%Response.Write obj_page.page_cute%></td>
    </tr>
    </table>	
	  </td>
	  <%if FileType<>"select" then%>	
	   <td colspan=3 align=center>
	   <input type="button" name="Submit" value="重命名" onClick="Rname()" class=btn>
	   <input type="button" name="Submit" value="删 除" onClick="DelAll()" class=btn>
	   <input type="hidden" name="ThisDir" value="<%=ThisDir%>">
	   </td>
	  <%end if%>	  	
	 </tr>
	</form>
   <%
  set obj_page=nothing
Set FsoFile = nothing
End Sub

Function GetUpDir()
	Dim strDir,strDir2,i
	strDir=""
	If ThisDir = "" then Exit Function
	strDir2=ThisDir
	strDir2=Split(strDir2,"/")
	for i=0 to Ubound(strDir2)-1
		if i<Ubound(strDir2)-1 then strDir=strDir & strDir2(i) & "/"
	next
	GetUpDir=strDir
End Function

Sub DelAll()
	Dim FolderId,FileId,FileNum,FolderNum,FilePath,FolderPath
	Dim FsoFolder,sSize
	FolderId = Split(Request.Form("FolderId"),",")
	FileId = trim(Request("FileId"))
	FileNum=0
	FolderNum=0
	If instr(FileId,",")>0 then
		FileId = Split(FileId,",")
		If Ubound(FileId) > -1 then
			For i = 0 to Ubound(FileId)
				FilePath = Server.MapPath(TopDir & ThisDir & trim(FileId(i)))
				If Fso.FileExists(FilePath) then
					Fso.DeleteFile FilePath,true
					FileNum = FileNum + 1
				End If
			Next
		End If
	else
		FilePath = Server.MapPath(TopDir & ThisDir & FileId)
		If Fso.FileExists(FilePath) then
			Fso.DeleteFile FilePath,true
			FileNum = FileNum + 1
		End If
	end if
	If Ubound(FolderId) > -1 then
		For i = 0 to Ubound(FolderId)
			FolderPath = Server.MapPath(TopDir & ThisDir & trim(FolderId(i)))
			If Fso.FolderExists(FolderPath) then
				Set FsoFolder = Fso.GetFolder(FolderPath)
				'if FsoFolder.size <=0 then
				Fso.DeleteFolder FolderPath,true
				FolderNum = FolderNum + 1
				'end if
			End If
		Next
	End If
  Call AMH_Event("成功删除 "& FileNum &" 个文件,成功删除 "& FolderNum &" 个文件夹")
	Call Cls.Out_Msg("\n成功删除 "& FileNum &" 个文件\n成功删除 "& FolderNum &" 个文件夹",ComeUrl)
End Sub

Sub Rname()
	Dim FolderName,FileName,NewName,OldName,strNewName
	FolderName = Trim(Request("FolderId"))
	FileName = Trim(Request("FileId"))
	NewName = Trim(Request("NewName"))
	if NewName="" then Call Cls.Out_Msg("请输入新文件名！",ComeUrl)
	If len(FolderName) <> 0 then
		strNewName = Server.MapPath(TopDir & ThisDir & NewName)
		OldName = Server.MapPath(TopDir & ThisDir & FolderName)
		If not Fso.FolderExists(strNewName) then
			Fso.MoveFolder OldName,strNewName			
			Call Cls.Out_Msg("文件夹“"& FolderName &"”已经成功改名为“"& NewName &"”",ComeUrl)
		Else
			Call Cls.Out_Msg("有同名文件夹，请换个文件夹名试试！",ComeUrl)
		End If
	End If
	If len(FileName) <> 0 then
		Dim FileExt,NewFileExt
		Response.write FileName
		FileExt=Split(FileName,".")
		NewFileExt=Trim(FileExt(Ubound(FileExt)))
		if Instr(NewName,".")>0 then
			Call Cls.Out_Msg("文件名中不能带“.”，换个文件名！",ComeUrl)
			Response.end
		end if
		NewName=NewName & "." & NewFileExt
		strNewName = Server.MapPath(TopDir & ThisDir & NewName)
		OldName = Server.MapPath(TopDir & ThisDir & FileName)
		If not Fso.FileExists(strNewName) then
			Fso.MoveFile OldName,strNewName			
      Call AMH_Event("文件“"& FileName &"”已经成功改名为“"& NewName &"”")
			Call Cls.Out_Msg("文件“"& FileName &"”已经成功改名为“"& NewName &"”",ComeUrl)
		Else
			Call Cls.Out_Msg("有同名文件，请换个文件名试试！",ComeUrl)
		End If
	End If
End Sub

Function GetFileUrl(strUrl)
	dim strSaveUrl,UpID,F_ID
	strSaveUrl=Trim(strUrl)
	If int(Format_Mid_Num(45))=0 Then
		If rttype = "1" then
	   		If Lcase(left(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)))=Lcase(Cls.Web_Dir & Cls.Web_Upload) then
		  		strSaveUrl=Cls.Web_RealURL &Cls.Web_Dir & Cls.Web_Upload & mid(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)+1)
			End If	
		Else
			If Lcase(left(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)))=Lcase(Cls.Web_Dir & Cls.Web_Upload) then
				strSaveUrl="{$download_dir}" & mid(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)+1)
			ElseIf Lcase(left(strSaveUrl,Len(Cls.Web_Upload)))=Lcase(Cls.Web_Upload) then
				strSaveUrl="{$upload_dir}" & mid(strSaveUrl,Len(Cls.Web_Upload)+1)
	   		ElseIf left(strSaveUrl,1)="/" then
				strSaveUrl="{$web_dir}" & mid(strSaveUrl,2)
			End If
		End If
	Else
		strSaveUrl=Replace(strSaveUrl,Cls.Web_Dir & Cls.Web_Upload,"{$download_dir}")
		Sql="Select id from AsaiMHUpload where UpFileUrl='"&strSaveUrl&"'"
		Set rs=Cls.exec(sql,1)
		If rs.eof then
			If rttype = "1" then
	   			If Lcase(left(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)))=Lcase(Cls.Web_Dir & Cls.Web_Upload) then
		  			strSaveUrl=Cls.Web_RealURL &Cls.Web_Dir & Cls.Web_Upload & mid(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)+1)
				End If	
			Else
				If Lcase(left(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)))=Lcase(Cls.Web_Dir & Cls.Web_Upload) then
					strSaveUrl="{$download_dir}" & mid(strSaveUrl,Len(Cls.Web_Dir & Cls.Web_Upload)+1)
				ElseIf Lcase(left(strSaveUrl,Len(Cls.Web_Upload)))=Lcase(Cls.Web_Upload) then
					strSaveUrl="{$upload_dir}" & mid(strSaveUrl,Len(Cls.Web_Upload)+1)
	   			ElseIf left(strSaveUrl,1)="/" then
					strSaveUrl="{$web_dir}" & mid(strSaveUrl,2)
				End If
			End If
		Else
			UpID=rs(0)
			F_ID=Encrypt(UpID)		'防盗链ID生成
			strSaveUrl="{$web_dir}Common/ResourcesUp.Asp?id="&F_ID&""
		End If
		rs.close 
	End If
	GetFileUrl=strSaveUrl
End Function

Function GetFileIcon(strFilename)
	strFilename=Split(LCase(strFilename),".")
	Select Case strFilename(Ubound(strFilename))
	Case "asp"
		GetFileIcon="asp.gif"
	Case "aspx"
		GetFileIcon="asp.gif"
	Case "jsp"
		GetFileIcon="asp.gif"
	Case "html"
		GetFileIcon="html.gif"
	Case "shtml"
		GetFileIcon="html.gif"
	Case "htm"
		GetFileIcon="html.gif"
	Case "shtm"
		GetFileIcon="html.gif"
	Case "xml"
		GetFileIcon="xml.gif"
	Case "bmp"
		GetFileIcon="bmp.gif"
	Case "doc"
		GetFileIcon="doc.gif"
	Case "gif"
		GetFileIcon="gif.gif"
	Case "ico"
		GetFileIcon="ico.gif"
	Case "jpg"
		GetFileIcon="jpg.gif"
	Case "js"
		GetFileIcon="js.gif"
	Case "mdb"
		GetFileIcon="mdb.gif"
	Case "mp3"
		GetFileIcon="media.gif"
	Case "wma"
		GetFileIcon="media.gif"
	Case "mid"
		GetFileIcon="media.gif"
	Case "png"
		GetFileIcon="png.gif"
	Case "rar"
		GetFileIcon="rar.gif"
	Case "swf"
		GetFileIcon="swf.gif"
	Case "txt"
		GetFileIcon="txt.gif"
	Case "xls"
		GetFileIcon="xls.gif"
	Case "xlt"
		GetFileIcon="xlt.gif"
	Case "zip"
		GetFileIcon="zip.gif"
	Case "exe"
		GetFileIcon="exe.gif"
	Case "dll"
		GetFileIcon="dll.gif"
	Case Else
		GetFileIcon="other.gif"
	End Select
End Function

Function ViewPictrue(vPicUrl)
      Dim vPicUrls,vPicUrlType
      vPicUrls=vPicUrl
      vPicUrlType=LCase(Right(vPicUrls,4))
      If vPicUrlType=".jpg" or vPicUrlType=".gif" or vPicUrlType=".bmp" or vPicUrlType=".png" or vPicUrlType="jpeg" then
      ViewPictrue=" Title='<img src="&vPicUrls&" border=0 height=120>'"
      Else
      ViewPictrue=""
      End If
End Function

'上传文件防盗链ID生成  
Function Encrypt(theNumber) 
	On Error Resume Next 
	Dim n, szEnc, t, HiN, LoN, i 
	n = CDbl((theNumber + 1570) ^ 2 - 7 * (theNumber + 1570) - 450) 
	If n < 0 Then szEnc = "R" Else szEnc = "J" 
	n = CStr(abs(n)) 
	For i = 1 To Len(n) step 2 
	t = Mid(n, i, 2) 
	If Len(t) = 1 Then 
		szEnc = szEnc & t 
		Exit For 
	End If 
	HiN = (CInt(t) And 240) / 16 
	LoN = CInt(t) And 15 
	szEnc = szEnc & Chr(Asc("M") + HiN) & Chr(Asc("C") + LoN) 
	Next 
	Encrypt = szEnc 
End Function
%>
