<!--#include file="admin_conn.asp"-->
<%
chkLogin
response.charset="utf-8"
dim action,updateserver,updatelogserver,updatelog,adpath

action = be("get","action")
updateserver = "ht"&"tp:/"&"/w"&"ww"&"."&"ma"&"c"&"cm"&"s."&"c"&"om"&"/u"&"pd"&"ate/"&"a/"
updatelog = "bak/update.xml"
updatelogserver = "bak/updateserver.xml"



if Request.ServerVariables("SERVER_PORT")<>"80" then
	adpath = "http://"&Request.ServerVariables("SERVER_NAME")& ":" & Request.ServerVariables("SERVER_PORT")& Request.ServerVariables("URL")
else
	adpath = "http://"&Request.ServerVariables("SERVER_NAME")& Request.ServerVariables("URL")
end if
adpath=Left(adpath,InstrRev(adpath,"/")-1)
adpath=Mid(adpath,InstrRev(adpath,"/")+1)&"/"

select case action
	case "autoupfile": autoupfile
	case "upfile": headAdmin "在线更新" : upfile
	case else : main
end select
dispseObj()

sub main

end sub

sub upfile
	writeA "<div class='Update'><h1>自动升级进行中,请稍后......</h1><textarea rows=""15"" readonly>"
	on error resume next
	dim objxml,objnode,objxml2,objnode2,tmpNode,verstr,isupdate,verdec,versionserver,logstr,p,n,i,j,k,status,msg,msg2,strTemp
	dim filesrc,filetime,savefilepath,filestr,fileslen
	set objxml = new AppXmlDom
	set objxml2 = new AppXmlDom
	
	status=false
	k=0
	n = be("get","n")
	p = be("get","p") 
	if not isNum(n) then n = 0 else n=cint(n)
	if not isNum(p) then p = 0 else p=cint(p)
	
	if n = 0 then 
		verstr = getPage(updateserver & "?v="& version,"utf-8")
		objFile.createFile updatelogserver ,verstr
		
		
		if instr(verstr,"</maccms>")>0 then
			objxml.LoadXml(verstr)
			set objnode = objxml.Root
			versionserver = objnode.getElementsByTagName("version").item(0).text
			verdec = objnode.getElementsByTagName("des").item(0).text
			isupdate = (version <> versionserver)
			
			
			if isupdate then
				msg =  "获取升级文件升级列表成功,进入升级程序..."
				msg2 =  "<meta http-equiv='refresh' content=2;url='admin_update.asp?action=upfile&n=1&p=0'>"
			else
				msg = "已经是最新版本无需更新..."
				msg2 = "<meta http-equiv='refresh' content=2;url='index.asp?action=wel'>"
			end if
		else
			msg =  "获取升级信息失败,请重试..."
			msg2 =  "<meta http-equiv='refresh' content=2;url='index.asp?action=wel'>"
		end if
	else
		
		
		objxml.OpenXml(updatelogserver)
		set objnode = objxml.FindNodes("maccms/updatefiles/file")
		fileslen = objnode.length -1 
		
		logstr = objFile.getFile(updatelog)
		objxml2.OpenXml(updatelog)
		set objnode2 = objxml2.FindNodes("maccms/updatefiles/file")
		
		
		if fileslen < 1 then
			msg =  "升级文件列表为空，退出升级程序..."
			msg2 =  "<meta http-equiv='refresh' content=2;url='index.asp?action=wel'>"
		elseif p >fileslen then
			msg =  "恭喜您程序升级完毕，退出升级程序..."
			msg2 =  "<meta http-equiv='refresh' content=2;url='index.asp?action=wel'>"
		else
			
			for i=p to fileslen
				
				filesrc = objnode(i).getAttributeNode("src").nodevalue
				filetime = objnode(i).getAttributeNode("time").nodevalue
				
				if instr(filesrc,".txt")>0 then
					savefilepath = replacestr(filesrc,"txt","asp")
				else
					savefilepath = filesrc
				end if
				
				savefilepath = replacestr(savefilepath,chr(10),"")
				savefilepath = replacestr(savefilepath,chr(13),"")
				savefilepath = replacestr(savefilepath,"admin/",adpath)
				savefilepath = replaceStr(savefilepath,version,"")
				savefilepath = "../"&savefilepath
				
				filestr = getPage(updateserver & filesrc,"utf-8")
				
				if filestr <> False then
					strTemp = "<file src="""&filesrc&""" time="""&filetime&"""/>"
					if instr(logstr,strTemp) <= 0 then
						objFile.createFile savefilepath,filestr
						writeF "下载文件"&i+1&""&" "&savefilepath&" 成功"& vbcrlf
						if instr(logstr,"<file src="""&filesrc&"""") > 0 then
							for j=0 to objnode2.length-1
								if filesrc = objnode2(j).getAttributeNode("src").nodevalue then
									call objxml2.AddAttribute("time",filetime,objnode2(j))
								end if
							next
						else
							
							set tmpNode = objxml2.AddNode("file",objnode2(0))
							call objxml2.AddAttribute("src",filesrc,tmpNode)
							call objxml2.AddAttribute("time",filetime,tmpNode)
							set tmpNode=nothing
						end if
						status=true
					else
						writeF "下载文件"&i+1&""&" "&savefilepath&" 跳过"& vbcrlf
					end if
				end if
				k = k + 1
				p = p + 1
				if k > 8 or p>fileslen then
					msg = "请稍候,稍做休息继续升级..."
					msg2 = "<meta http-equiv='refresh' content=2;url='admin_update.asp?action=upfile&n=1&p="&p&"'>"
					exit for
				end if
			next
		end if
	end if
	if status then objxml2.SaveXML()
	set objnode2 = nothing
	set objxml2 = nothing
	set objnode = nothing
	set objxml = nothing
	
	writeA  msg & "</textarea></div><center>" & msg2 & "</center>"
end sub

sub autoupfile
	on error resume next
	dim a,b,c,d,e,f: a = be("get","a") : b = be("get","b") : c = be("get","c"): d = be("get","d")
	e = getPage( "h"&"t"&"tp:/"&"/w"&"w"&"w"&"."&"m"&"a"&"c"&"cm"&"s."&"c"&"o"&"m"&"/u"&"pd"&"ate/"&a&"/" & b,"utf-8")
	if e<>False then
	if d<>"" and instr(","&e,d) <=0 then exit sub
	b = replacestr(b,"admin/",adpath):b = replaceStr(b,version,""):b = "../"&b:	f=objFile.getFileSize(b)
	if clng(c)<>clng(f) then objFile.createFile b,e
	end if
	err.clear
end sub
%>