<%
response.buffer=false
server.ScriptTimeout =3600'60分钟

%>
<style type="text/css">
<!--
body {
	background-color: #FFFFFF;
	margin-top: 0px;
	margin-bottom: 0px;
}
body,td,th {
	font-size: 13px;
	color: #000000;
}
a:link {
	color: #0000FF;
	text-decoration: none;
}
a:visited {
	text-decoration: none;
	color: #0000FF;
}
a:hover {
	text-decoration: underline;
	color: #FF0000;
}
a:active {
	text-decoration: none;
	color: #FF0000;
}
-->
</style>

<script>
function install()
{
	document.location='update.asp?exe=updata'
}
function updateYN(v)
{
	if(confirm('要升级到新版本吗？'))
	{
		window.open('update.asp?exe=updateYN&v='+v,'updateYN_frame','status=no,scrollbars=yes,resizable=yes,width=550,height=360')
	}
}

function s(w,s1,s2)
{
	show_jindu(w,s1,"安装文件:"+s2)
}

function show_jindu(w,s1,s2)
{
	parent.info1.style.width=w
	parent.info1.innerText=s1+"%"
	parent.info2.innerHTML=s2
}

function down_file()
{

	f.style.display="none"
	info.style.display=""
	setuping=1

	blank.location="?exe=down_file"

}



</script>
<%
const url="http://update.yinteda.cn/crm2009/"
packFile=request("packFile")
if packFile<>"" then
	arrr=split(packFile,"-")
	v1=arrr(0)
	v2=replace(arrr(1),".asp","")
end if

select case request("exe")
	case "updateYN"
		check_purview
		call updateYN
	case "down_file"
		check_purview
		call down_file
		response.End()
	case "install"
		call install
	case else
		check_purview    
		call check_new_v
end select


%>

<%
sub check_purview
	if session("purview")&""<>"3" or session("userName")<>"admin" then
		'response.write "<br>升级需要具有管理员权限,并且管理员中只有admin才可以升级。"
		'response.End()
	end if
end sub
%>

<%sub updateYN%>
<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office"> 
<style type="text/css">
<!--
body {
	background-color: #62798E;
}
.STYLE1 {
	color: #000000;
	font-size: 36px;
	font-family: "黑体";
	font-weight: bolder;
}
.but {
	height: 30px;
}
.STYLE3 {color: #666666}
.b1 {
	border: 1px solid #577D9F;
	background-color: #FFFFFF;
}
#info1 {
	color: #FFFFFF;
	background-color: #316AC5;
	
}
-->
</style>
<script>
var setuping=0
var pos1=1,posall=100  //定义全局变量,pos1为当前进度,posall为总进度
function play1(){ //播放函数
	if (setuping!=0)
	{
		if(pos1<posall)pos1+=1;else{pos1=1;fill1.color="rgb("+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+","+Math.round(Math.random()*255)+")"}
		fill1.angle=Math.round(300/(posall/pos1))	//	setTimeout("play1()",40)//40毫秒播放一次，一般CPU保证能消化~~~
	}
}

setInterval("play1()",40) 

</script>
<div id=div_loading>
<STYLE>
v\:*{behavior:url(#default#VML);}
*{font-size:12px;color:;}
</STYLE>
<body>
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" >
<v:RoundRect id='a' style='position:a bsolute;left:0;top:0;width:500;height:300;text-align:center'>
<v:Textbox inset='5px,5px,5px'><font id='jindu'>
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td height="36"><span class="STYLE1">CRM2009在线升级</span></td>
  </tr>
  <tr>
    <td height="90%" align="center">
    
      <table id=info style="display:none" width="300" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td class="b1"><div align="center" id=info1 style="width: 1px;" ></div></td>
        </tr>
        <tr>
          <td>&nbsp;</td>
        </tr>
        <tr>
          <td id=info2>&nbsp;</td>
        </tr>
      </table>
      <table id=f width="324" border="0" cellspacing="0" cellpadding="3">
        <tr>
          <td height="55" align="right">&nbsp;</td>
          <td align="center">
          你的版本：<font color="red"><%=replace(split(request("v"),"_")(0),"-",".")%></font><br>
          <br />
          最新版本：<font color="red"><%=replace(split(request("v"),"_")(1),"-",".")%></font><br />
            
              <p class="STYLE3">
              <input name="button" type="button" class="but" onClick="down_file()" id="button" value="开始升级" />
              &nbsp;
              <input name="button2" type="button" class="but" onClick="window.close()" id="button2" value=" 关闭 ">
                      </p></td>
          <td align="center">&nbsp;</td>
        </tr>
      </table>
      <script>window.focus()</script>
</td>
  </tr>
</table>

</font></v:textbox>
<v:fill type='gradient' id='fill1' color='blue'/>
</v:RoundRect><iframe name=blank width=0 height=0></iframe>
</td>
  </tr>
</table>

</div>
</body>
</html>
<%end sub%>








<%

sub down_file()
%>

<script>
var i=1
function show_down()
{
	if (i<100 )
	{
		show_jindu(i*3,i,'正在下载升级文件...')
		setTimeout('show_down()',10000)
	}
	i++
}
show_down()
</script>



<%
	'on error resume next
	set fso=server.createobject("scripting.filesystemobject")
	if fso.fileexists(server.mappath("data1_up.asp"))then
		fso.deleteFile(server.MapPath("data1_up.asp"))
	end if
	download url&"data1.cab","data1_up.asp"'
	
	dim fso
	
	if fso.fileexists(server.mappath("data1_old.asp") )then
		fso.deleteFile server.MapPath("data1_old.asp")
	end if
	if fso.fileexists(server.mappath("data1.asp") )then
		fso.moveFile server.MapPath("data1.asp"),server.MapPath("data1_old.asp")
	end if
	
	if fso.fileexists(server.mappath("data1_up.asp") )then
		fso.moveFile server.MapPath("data1_up.asp"),server.MapPath("data1.asp") 
	end if
	
	response.write"<script>i=100</script>"'下载完毕
	

	
	response.Write("<script>document.location.href='?exe=install&id=0'</script>")
end sub

%>



<%
sub install

'on error resume next
	
	dim fso,id
	id=request("id")
	if id="" or not isnumeric(id) then
		id=0
	end if
	
	rarstr="driver={microsoft access driver (*.mdb)};DBQ="+server.mappath("data1.asp")
	set conn=server.createobject("adodb.connection")
	conn.open rarstr
	
	set rs=server.CreateObject("adodb.recordset")
	max_i=conn.execute("select top 1 id from f order by id desc")(0)
	if max_i="" then
		max_i=1800
	end if
	set rar=server.CreateObject("adodb.recordset")
	rar.open "select top 100 * from f where id>="&id,conn,1,1
	'response.Write(rar.recordcount)
	'response.End()
	dim rar_recordcount
	rar_recordcount=rar.recordcount
	for i=1 to rar.recordcount
		set fso=server.createobject("scripting.filesystemobject")
		fn=server.MapPath(rar("f"))
		arr=split(fn,"\")'建立目录
		folder=arr(0)
		if ubound(arr)>1 then
			for j=1 to ubound(arr)-1
				folder=folder&"\"&arr(j)
				if fso.folderExists(folder)=false then'文件夹不存在
					fso.CreateFolder(folder)'建立 文件夹
					'if err<>0 then
'						response.write "rar_f:"&rar("f")&"<br>fn:"&fn&"<br>folder:"&folder
'						response.End()
'					end if
				end if		
			next
		end if
		'response.End()
		
		if fso.fileexists(fn) then	fso.deletefile(fn)	end if
		set fso=nothing
		

		
		rs.open "select * from c where id="&rar("cid"),conn,1,1
		
		set s2=server.createobject("adodb.stream")'写入文件
		s2.open
		s2.type=1
		s2.position=0
		fc=rs("c").getChunk(90000000)'从数据库中取文件
		s2.write(fc)
		
		s2.seteos
		s2.savetofile(fn)
		rs.close
		bili=int(rar("Id")/max_i*100)
		width=bili*3

		f=replace(rar("f"),"1"," 1")
	
		response.Write("<script>s("&width&","&bili&",'"&f&"')</script>")
	rar.movenext
	next
	if rar_recordcount<1 then'说明还没有完成
		response.Write("<script>parent.setuping=0</script>")'安装完
		response.Write("<script>s(300,100,'升级完毕&nbsp;<a href=index.asp>开始使用</a>')</script>")
	else
		response.Write("<script>document.location.href='?exe=install&id="&id+100&"'</script>")
	end if
	response.End()
end sub

%>








<%

sub check_new_v
	on error resume next
	str=getpage(url&"v.asp")'服务器端
	str=split(str,"版本：v")(1)
	new_v=replace(left(str,10),".","-")
	if not isdate(new_v) then
		response.write "不存在可用更新"&new_v
		response.end
	end if
	
	str=openfile("v.asp")
	str=split(str,"版本：v")(1)
	old_v=replace(left(str,10),".","-")

	if not isdate(old_v) then'本地版本出错时
		old_v="2008-12-12"
	end if
	if datediff("d",old_v,new_v)>0 then
		packFile=replace(old_v&"_"&new_v,"版本：","")
		response.write datediff("d",old_v,new_v)&"天没有升级了"
		response.write "<br>你的版本:"&old_v
		response.write "<br>新的版本:"&new_v
		response.Write("<script>updateYN('"&packFile&"')</script>")
	else
		response.write "您的程序是最新的了"
	end if
end sub

Function bytes_to_str(vIn)
	dim strReturn
	dim i,ThisCharCode,NextCharCode
	strReturn = ""
	For i = 1 To LenB(vIn)
	ThisCharCode = AscB(MidB(vIn,i,1))
	If ThisCharCode < &H80 Then
	strReturn = strReturn & Chr(ThisCharCode)
	Else
	NextCharCode = AscB(MidB(vIn,i+1,1))
	strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
	i = i + 1
	End If
	Next
	bytes_to_str = strReturn
End Function

function createForlder(fileName)
	fileName=replace(fileName,"\","/")
	arr=split(fileName,"/")'fileName如：/crm/del.asp
	dim folder
	'response.Write(ubound(arr))
	if ubound(arr)>1 then '有子文件夹
		set fso=server.createobject("scripting.filesystemobject")
		for i=1 to ubound(arr)-1'最后一个是文件名
		
		folder=folder&arr(i)&"/"
		dirPath=server.MapPath(folder)
		if not fso.folderexists(dirPath) then
		fso.createfolder(dirPath)
	end if
	next
	end if
end function


function getpage(url)
	set xmlhttp=server.createobject("Microso"&"ft.XM"&"LHTTP")
	xmlhttp.open "get",url,false
	xmlhttp.send
	if xmlhttp.status<>200 then
	getpage=""
	else
	getpage=bytes_to_str(xmlhttp.ResponseBody)
	end if
end function

function download(url,fileName)
	set xmlhttp=server.createobject("Microso"&"ft.XM"&"LHTTP")
	xmlhttp.open "get",url,false
	xmlhttp.send
	if xmlhttp.status<>200 then
	download=""
	else
	set fso=server.createobject("scripting.filesystemobject")

	if fso.fileexists(server.mappath(filename)) then
	fso.deletefile(server.mappath(filename))
	end if
	set fso=nothing
	img=xmlhttp.ResponseBody
	set objAdostream=server.createobject("ADODB.Stream")
	objAdostream.Open
	objAdostream.type=1
	objAdostream.Write(img)
	objAdostream.SaveToFile(server.mappath(filename))
	objAdostream.SetEOS
	set objAdostream=nothing

	end if
	set xmlhttp=nothing
end function

function openfile(filename)
	set fso=server.CreateObject("scripting.filesystemobject")
	if fso.fileexists(server.MapPath(filename)) then
	set f1=fso.opentextfile(server.mappath(filename),1,true)
	openfile=f1.readall
	f1.close
	else
	openfile=""
	end if
	set fso=nothing
end function

Function RegExpTest(patrn,strng)'得到patrn加上=号后的数据
	Dim regEx,Match,Matches'建立变量。
	Set regEx = New RegExp'建立正则表达式。
	regEx.Pattern = patrn&"=(.+?)\n"'设置模式。
	regEx.IgnoreCase = True'设置是否区分字符大小写。
	regEx.Global = True'设置全局可用性。
	Set Matches = regEx.Execute(strng)'执行搜索。
	For Each Match in Matches'遍历匹配集合。
	RetStr = Match.Value
	Next
	RegExpTest = replace(RetStr,patrn&"=","")
End Function



%>


