<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!--#include file="Conn.asp"-->
<!--#include file="Ex_Cls/Ex_CommonCls.asp"-->
<!--#include file="Ex_Cls/Ex_XmlCls.asp"-->
<!--#include file="Ex_Cls/Ex_FsoCls.asp"-->
<!--#include file="Ex_Cls/Ex_ZipCls.asp"-->
<%response.buffer=true%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>在线服务</title>
</head>
<link href="Admin_STYLE.CSS" rel="stylesheet" type="text/css"> 
<body>
<%
Dim ExComm,ExFso,ExXml,ExZip
Set ExComm=new Ex_CommonCls
Set ExFso=new Ex_FsoCls
Set ExXml=new Ex_XmlCls
Set ExZip=new Ex_ZipCls
ExComm.IsAdLogin 
ExComm.IsIpAllow
Dim Action
Action=Request.QueryString("action")
Select Case Action
	Case "showskin"
		ShowSkin
	Case "update"
		BlogUpdate
	Case "blogbak"
		BakStoreForm
		BlogBak
	Case "blogrestore"
		BakStoreForm
		BlogRestore
	Case "blogbakrestore"
		BakStoreForm
	Case "blogbakdown"
		downloadFile "Temp/BlogBak.ExZip"
End Select
Sub ShowSkin
	Dim Html
	Html="<table width=""98%"" border=""0"" align=""center"" cellpadding=""2"" cellspacing=""1"" bgcolor=""#FFFFFF"" class=""border"">"
	Html=Html&" <tr><th class=""topbg"" colspan=""5"">风格在线安装</th></tr>"
	Html=Html&"<tr><td class=""tdbg"">"
	Html=Html&GetServerData("http://localhost:30/ReturnData.asp")
	Html=Html&"</td></tr></table>"
	Response.Write Html
End Sub 
Function GetVersion()
	If Trim(Application(Sn&"version"))="" Then 
		ExComm.GetConfigCache("version")
	End If 
	GetVersion=Split(Application(Sn&"version"),"Bulid")(0)
End Function 
Function GetNewVersion(DataUrl)
	Dim Temp,TempV
	DataUrl=LCase(DataUrl)
	Temp=Split(DataUrl,"/")
	TempV=Replace(Temp(UBound(Temp)),".xml","")
	TempV=Replace(TempV,"exblogv","ExblogV")
	TempV=Replace(TempV,"bulid","Bulid")
	GetNewVersion=TempV
End Function 
Sub BlogUpdate()
	Dim Temp,I,DataUrl
	Response.Write("正在获取易心博客最新版本信息...<br>")
	Response.Flush()
	Temp=ExComm.GetServerData("http://www.ex123.net/BlogOnlineServer/Update/Info/"&GetVersion&".Xml")
	ExFso.CreateFile "Temp/TempUpdateList.ExZip",Temp
	ExXml.letFilePath="Temp/TempUpdateList.ExZip"
	DataUrl=ExXml.getNodeValue(Replace(LCase(Application(Sn&"Version")),LCase(GetVersion),""))
	If Trim(DataUrl)="" Then 
		Response.Write("您的博客已经是最新版本,无需升级.")
		Exit Sub 
	Else 
		Response.Write("正在下载升级文件,请稍后...<br>地址为:"&DataUrl&"<br>")
		Response.Flush()
		Temp=ExComm.GetServerData(DataUrl)
		ExFso.CreateFile "Temp/TempUpdateData.ExZip",Temp
		Response.Write("正在进行更新文件的备份,以便升级出问题时恢复...<br>")
		Response.Flush()
		ExXml.letFilePath="Temp/TempUpdateData.ExZip"
		Temp=ExXml.getNodesAttValue("file","path")
		ExZip.ZipStr Temp,"Temp/TempUpdateBak.ExZip"
		Response.Write("正在从"&Application(Sn&"Version")&"升级到"&GetNewVersion(DataUrl)&",请稍后...<br>")
		Response.Flush()
		On Error Resume Next 
		ExZip.UZip "Temp/TempUpdateData.ExZip"
		If Err.number=0 Then 
			Conn.Execute "Update Ex_Config set Version='"&GetNewVersion(DataUrl)&"'"
			Application.Lock
			Application(Sn&"Version")=GetNewVersion(DataUrl)
			Application.UnLock
			response.write("升级完成<br>")
			ExXml.letFilePath="Temp/TempUpdateData.ExZip"
			Response.Write(ExXml.getNodeValue("info")&"<br><br>")
			BlogUpdate
		Else
			Response.Write("升级失败,失败原因:"&Err.description&"<br>正在用备份文件恢复...<br>")
			Response.Flush()
			ExZip.UZip "Temp/TempUpdateBak.ExZip"
			Response.Write("恢复完成,请到论坛提交错误信息,以便我们帮你解决..")
		End If 
	End If 
End Sub
Sub  downloadFile(strFile) 
Dim StrFileName,S,Fso,F,intFilelength
strFilename = server.MapPath(strFile) 
Response.Buffer = True 
Response.Clear 
Set s = Server.CreateObject("ADODB.Stream") 
s.Open 
s.Type = 1 
'on error resume next
Set fso = Server.CreateObject("Scripting.FileSystemObject") 
if not fso.FileExists(strFilename) then 
Response.Write("<h1>Error:</h1>"&strFilename&" does not exists!<p>") 
Response.End 
end if 
Set f = fso.GetFile(strFilename) 
intFilelength = f.size 
s.LoadFromFile(strFilename) 
if err then 
Response.Write("<h1>Error: </h1>Unknown Error!<p>") 
Response.End 
end if 
Response.AddHeader "Content-Disposition","attachment; filename="&f.name 
Response.AddHeader "Content-Length",intFilelength 
Response.CharSet = "gb2312" 
Response.ContentType = "application/octet-stream" 
Response.BinaryWrite s.Read 
Response.Flush 
s.Close 
Set s = Nothing 

End Sub  

Sub BlogBak()
	Dim Str,I,Temp,Data
	Str=Request.Form("bak")
	Temp=Split(Str,",")
	ExFso.DelFile "Temp/BlogBak.ExZip"
	For I=0 To UBound(Temp)
		If InStr(Temp(I),".")<>0 Then 
			Response.Write("<script>document.getElementById('msg').innerHTML='正在压缩文件"&Temp(I)&"<br>';</script>")
			Response.Flush()
			ExZip.ZipStr Temp(I)&"|","Temp/BlogBak.ExZip"
		Else 
			Response.Write("<script>document.getElementById('msg').innerHTML='正在压缩文件夹"&Temp(I)&"<br>';</script>")
			Response.Flush()
			ExZip.Zip Temp(I),"Temp/BlogBak.ExZip"
		End If 
	Next 
	If Request.Form("data")<>"" Then 
		Response.Write("<script>document.getElementById('msg').innerHTML='正在压缩数据库"&Request.Form("data")&"<br>';</script>")
		Response.Flush()
		Data=LCase(Request.Form("data"))
		Data=Replace(Data,".asp",".asp.mdb")
		ExFso.CopyOneFile Request.Form("data"),Data
		ExZip.ZipStr "conn.asp|"&Data&"|","Temp/BlogBak.ExZip"
		ExFso.DelFile Data
	End If 
	Response.Write("<script>document.getElementById('msg').innerHTML='压缩完成<br>';</script>")
	Response.Write("<script>document.getElementById('msg').innerHTML='<a href=?action=blogbakdown>点此下载备份文件Temp/BlogBak.ExZip</a>';</script>")
	Response.Write("<script>if(confirm('是否要下载备份?建议用迅雷下载.'))location='?action=blogbakdown';</script>")
	Response.Flush()
End Sub 
Sub RestoreDataBase
	Dim Temp,I
	Temp=ExFso.GetSubFiles("Data")
	Temp=Split(Temp,"|")
	For I=0 To UBound(Temp)
		If InStr(LCase(Temp(I)),".asp.mdb")<>0 Then 
			ExFso.CopyOneFile "Data/"&Temp(I),"Data/"&Replace(Temp(I),".asp.mdb",".asp")
			ExFso.DelFile "Data/"&Temp(I)
		End If 
	Next 
End Sub 
Sub BlogRestore
	ExZip.UZip Request.Form("bakfile")
	RestoreDataBase
	Response.Write("<script>document.getElementById('rmsg').innerHTML='恢复成功';</script>")
	Response.Write("<script>alert('恢复成功');</script>")
	Response.Flush()
End Sub 
function GetDataBase
	dim Fso,str
	set Fso=server.CreateObject("scripting.filesystemobject")
	dim myfolder,item
	set myfolder=Fso.GetFolder(server.MapPath("Data"))
	for each item in myfolder.files
		str=str&"<option value=""data/"&item.name&""">"&item.name&"</option>"
	next
	GetDataBase=str
end function 
function GetBakFile
	dim Fso,str
	set Fso=server.CreateObject("scripting.filesystemobject")
	dim myfolder,item
	set myfolder=Fso.GetFolder(server.MapPath("Temp"))
	for each item in myfolder.files
		str=str&"<option value=""Temp/"&item.name&""">"&item.name&"</option>"
	next
	GetBakFile=str
end function 
'备份表单
Sub BakStoreForm
%>
<form action="?action=blogbak" method="post">
<table width="98%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF" class="border">
  <tr>
    <th colspan="3" class="topbg">在线备份</th>
  </tr>
  <tr>
    <td width="7%" class="tdbg">&nbsp;</td>
    <td width="80%" class="tdbg"><label>&nbsp;
        <select name="data">
  <option value="">请选择要备份的数据库文件</option>
  <%=GetDataBase%>
        </select> 
&nbsp; 
<input name="bak" type="checkbox" id="bak" value="photo">
相册 &nbsp; 
<input name="bak" type="checkbox" id="bak" value="upfile">
附件 &nbsp; 
<input name="bak" type="checkbox" id="bak" value="skin,exskin">
风格&nbsp;&nbsp; 
<input name="bak" type="checkbox" id="bak" value="inc/sn.asp"> 
序列号文件 &nbsp;
<input type="submit" name="Submit" value="开始备份">
    </label></td>
    <td width="13%" class="tdbg">&nbsp;</td>
  </tr>
  <tr>
    <td class="tdbg">&nbsp;</td>
    <td class="tdbg">&nbsp;
      <div align="center" id="msg" style="text-align:center;color:red;"></div>
    </td>
    <td class="tdbg">&nbsp;</td>
  </tr>
</table>
</form>
<br>
<form action="?action=blogrestore" method="post">
<table width="98%" border="0" cellpadding="0" cellspacing="1" bgcolor="#FFFFFF" class="border">
  <tr>
    <th colspan="3" class="topbg">在线恢复</th>
  </tr>
  <tr>
    <td width="15%" bgcolor="#FFFFFF" class="tdbg">&nbsp;</td>
    <td width="64%" bgcolor="#FFFFFF" class="tdbg"><label>&nbsp;请选择你用来恢复博客的备份文件
        <select name="bakfile">
  <option value="">请选择备份文件</option>
  <%=GetBakFile%>
        </select> 
&nbsp;&nbsp;
<input type="submit" name="Submit" value="开始恢复">
    </label></td>
    <td width="21%" bgcolor="#FFFFFF" class="tdbg">&nbsp;</td>
  </tr>
  <tr>
    <td bgcolor="#FFFFFF" class="tdbg">&nbsp;</td>
    <td bgcolor="#FFFFFF" class="tdbg"><div id="rmsg" align="center" style="text-align:center;color:red;">&nbsp;</div></td>
    <td bgcolor="#FFFFFF" class="tdbg">&nbsp;</td>
  </tr>
</table>
</form>
<%
Response.Flush()
End Sub 
%>
</body>
</html>
