<!--#include file="admin_start.asp" -->
<%
set rs=nothing
conn.close
set conn=nothing

Function CheckT(str)
	
		if str="" then
		CheckT=false
		exit function
		end if
	
		dim c
		c=0
		'c=c+Instr(str,":")
		c=c+Instr(str,"?")
		c=c+Instr(str,"<")
		c=c+Instr(str,"*")
		c=c+Instr(str,">")
		c=c+Instr(str,"|")
		c=c+Instr(str,"""")
		'c=c+Instr(str,"/")		
                'c=c+Instr(str,"\")
		if c=0 then
		   CheckT=true
		else
		   CheckT=false
		end if
End Function

Action=trim(request("Action"))
mdb="../inc/config_database_name.asp"
Bkmdb="../inc/config_databackup_name.asp"

select case Action
case "editname"  '数剧库更名
	    call editname()
case "backup"  '备份数剧库
		call backup()
case "compress"  '数据库压缩
		call Compress()
case "restore"   '数据库恢复
		call Restore()
end select 
'数剧库更名//////////////////////////////////////////////////////////////////////////////////////////////
sub editname()   '###数剧库更名
	dim dataname_new
	dim fs
	dim TS1
	
	dataname_new=trim(request.Form("dataname"))
	
	if not CheckT(dataname_new) then
	MySys.error.info="数剧库名称出现非法字符!"
	MySys.error.location="back"
	MySys.error.err
	exit sub
	end if
	
	Set fs=Server.CreateObject("Scripting.FileSystemObject")
	fs.CopyFile Server.MapPath("../database/"&dataname),Server.MapPath("../database/"&dataname_new)
	Set TS1 = fs.CreateTextFile(Server.MapPath(""&mdb&""), True) 
	TS1.write "<"&chr(37)&"Dataname="&chr(34)&dataname_new&chr(34)&chr(37)&">"
	Set TS1 = Nothing

	MySys.error.info="已经成功将数据库文件名&nbsp;<font color=red>"&Dataname&"</font>&nbsp;改为&nbsp;<font color=red>"&Dataname_New&"</font>!"
	on error resume next
	if dataname<>dataname_new then fs.DeleteFile Server.MapPath("../database/"&dataname),True
	if err.number<>0 then
		MySys.error.info=MySys.error.info&"<br>原数据库没有删除成功，您可以手动在服务器端删除!"
	end if
	Set fs=nothing
	
	MySys.error.location="back"
	MySys.error.suc

end sub

'数剧库备份//////////////////////////////////////////////////////////////////////////////////////////////
sub backup()   '###备份数剧库

	dim fso
	dim databackup_path
	dim data_source
	dim data_location
	
	databackup_path=Server.MapPath(trim("../"&request.form("databackup_path")))

	Set Fso=server.createobject("scripting.filesystemobject")
	t=split(databackup_path,"\")
	databackup_floder=left(databackup_path,len(databackup_path)-len(t(ubound(t))))
	if not CheckDir(databackup_floder) then 
             if not CheckT(databackup_floder) then
		MySys.error.info="备份数剧库路径出现非法字符!hhh"&databackup_floder
		MySys.error.location="back"
		MySys.error.err
		exit sub
	     end if
             MakeNewsDir(databackup_floder)
        end if
	
	data_source=Server.MapPath("../database/"&dataname)
	data_location=databackup_path
	
	fso.copyfile data_source,data_location
	
	Set TS1 = fso.CreateTextFile(Server.MapPath(""&Bkmdb&""), True) 
	TS1.write "<"&chr(37)&"backupName="&chr(34)&request.form("databackup_path")&chr(34)&chr(37)&">"
	Set TS1 = Nothing
	
	set fso=nothing

	MySys.error.info="备份数据库成功，您备份的数据库路径为<nobr>" &databackup_path&"</nobr>"
	MySys.error.location="back"
	MySys.error.suc

end sub	

'数剧库恢复//////////////////////////////////////////////////////////////////////////////////////////////
sub Restore()   '###数据库恢复
	dim databackup
	dim datatorestore
	dim fso
	
	databackup=request.form("databackup")
	datatorestore=request.form("datatorestore")
	
	if not CheckT(databackup) or not CheckT(datatorestore) then
		MySys.error.info="请输入正确的备份数据库路径和当前数据库路径！"
		MySys.error.location="back"
		MySys.error.err
		exit sub
	end if

	databackup=Server.MapPath("../"&request.form("databackup"))
	datatorestore=Server.MapPath("../database/"&request.form("datatorestore"))

	Set Fso=server.createobject("scripting.filesystemobject")
	if fso.fileexists(databackup) then  					
		fso.copyfile databackup,datatorestore
		MySys.error.info="恢复数据库成功，恢复后的数据库路径为" &datatorestore
		MySys.error.location="back"
		MySys.error.suc
	else
		MySys.error.info="恢复数据库失败，没有找到备份的数据库!"
		MySys.error.location="back"
		MySys.error.suc
	end if
	
end sub

'数剧库压缩//////////////////////////////////////////////////////////////////////////////////////////////
sub Compress()   '###数据库压缩
	dim data_tocompress
	dim boolIs97
	dim Engine
	dim strDBPath
	dim fso
	
	data_tocompress = trim(request.form("data_tocompress"))
	data_ver_97 = trim(request.form("data_ver_97"))
	Set fso = CreateObject("Scripting.FileSystemObject")
	
	if not CheckT(data_tocompress) then
		MySys.error.info="请输入正确的待压缩数据库对象路径！"
		MySys.error.location="back"
		MySys.error.err
		exit sub
	end if

	data_tocompress = server.mappath("../"&trim(request.form("data_tocompress")))
	
	If fso.FileExists(data_tocompress) Then
	
		strDBPath = left(data_tocompress,instrrev(data_tocompress,"\"))
		Set Engine = CreateObject("JRO.JetEngine")
	
		If data_ver_97 = 1 Then
			Engine.Compactdatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & data_tocompress, _
			"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb;" _
			& "Jet OLEDB:Engine Type=" & JET_3X
		Else
			Engine.Compactdatabase "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & data_tocompress, _
			"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & strDBPath & "temp.mdb"
		End If
	
		fso.CopyFile strDBPath & "temp.mdb",data_tocompress
		fso.DeleteFile(strDBPath & "temp.mdb")
		Set fso = nothing
		Set Engine = nothing
		
		MySys.error.info="数据库压缩成功，压缩的数据库路径为：" &data_tocompress
		MySys.error.location="back"
		MySys.error.suc
			
	Else
	
		MySys.error.info="数据库压缩失败，没有找到待压缩的数据库!"
		MySys.error.location="back"
		MySys.error.suc
	
	End If

end sub
	
'------------------检查某一目录是否存在-------------------
Function CheckDir(FolderPath)
	'folderpath=Server.MapPath(".")&"\"&folderpath
    Set fso1 = CreateObject("Scripting.FileSystemObject")
    If fso1.FolderExists(FolderPath) then
       '存在
       CheckDir = True
    Else
       '不存在
       CheckDir = False
    End if
    Set fso1 = nothing
End Function
'-------------根据指定名称生成目录-----------------------
Function MakeNewsDir(foldername)
	dim f
	dim t
	dim i
	
    Set fso1 = CreateObject("Scripting.FileSystemObject")
	t=split(foldername,"\")
	for i=0 to ubound(t)
	if i>0 then t(i)=t(i-1)+"\"+t(i)
        if not fso1.FolderExists(t(i)) then
			Set f = fso1.CreateFolder(t(i))
			set f=nothing
		end if
	next
    MakeNewsDir = True
    Set fso1 = nothing
End Function

set MySys=nothing
%>
