<!--#include file="include/inc.asp"-->
<%
Dim Action
Action = Request.QueryString("action")
Const SHOWVERSION = "1.1"
Select Case Action
	Case "upgrade"
		Call Upgrade()
	Case Else
		Call Main()
End Select

'========================================================
'执行升级操作
'========================================================
Sub Upgrade()
	Dim MyDB, MyTable

	Application.Contents.RemoveAll()

	RQ.Execute("CREATE TABLE "& TablePre &"attachments(aid int IDENTITY(1,1) NOT NULL PRIMARY KEY,tid int NOT NULL DEFAULT 0,pid int NOT NULL DEFAULT 0,uid int NOT NULL,filename nvarchar(255) NOT NULL,filetype varchar(50) NOT NULL,filesize int NOT NULL,savepath varchar(100) NOT NULL,downloads int NOT NULL DEFAULT 0,ifimage tinyint NOT NULL DEFAULT 0,description nvarchar(100) NOT NULL DEFAULT """",posttime datetime NOT NULL DEFAULT Now())")

	RQ.Execute("CREATE INDEX IX_"& TablePre &"attachments ON "& TablePre &"attachments (tid ASC)")

	RQ.Execute("CREATE INDEX IX_"& TablePre &"attachments_1 ON "& TablePre &"attachments (uid ASC)")

	RQ.Execute("CREATE INDEX IX_"& TablePre &"attachments_2 ON "& TablePre &"attachments (pid ASC,posttime ASC)")

	Set MyDB = Server.CreateObject("ADOX.Catalog")
	Set MyTable = Server.CreateObject("ADOX.Table")

	MyDB.ActiveConnection = Conn
	Set MyTable = MyDB.Tables(TablePre &"attachments")
	MyTable.Columns("description").Properties("Jet OLEDB:Allow Zero Length") = True
	Set MyTable = Nothing
	Set MyDB = Nothing

	RQ.Execute("ALTER TABLE "& TablePre &"posts ADD ifattachment tinyint NULL DEFAULT 0")

	RQ.Execute("UPDATE "& TablePre &"posts SET ifattachment = 0")

	RQ.Execute("ALTER TABLE "& TablePre &"posts ALTER COLUMN ifattachment tinyint NOT NULL")

	RQ.Execute("ALTER TABLE "& TablePre &"topics ADD ifattachment tinyint NULL DEFAULT 0")

	RQ.Execute("UPDATE "& TablePre &"topics SET ifattachment = 0")

	RQ.Execute("ALTER TABLE "& TablePre &"topics ALTER COLUMN ifattachment tinyint NOT NULL")

	RQ.Execute("ALTER TABLE "& TablePre &"chatmessages ALTER COLUMN uid int DEFAULT 0")

	RQ.Execute("DROP TABLE "& TablePre &"online")

	RQ.Execute("CREATE TABLE "& TablePre &"online(sid char(10) NOT NULL PRIMARY KEY,uid int NOT NULL,username nvarchar(20) NOT NULL,userip char(15) NOT NULL,usergroupid smallint NOT NULL,lastupdate datetime NOT NULL DEFAULT Now())")

	RQ.Execute("CREATE INDEX IX_"& TablePre &"online ON "& TablePre &"online (uid ASC)")


	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 1, maxattachsize = 0, attachextensions = '' WHERE gid = 1")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 1, maxattachsize = 4000, attachextensions = '' WHERE gid = 2")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 1, maxattachsize = 2048, attachextensions = '' WHERE gid = 3")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 1, maxattachsize = 512, attachextensions = 'jpg,jpeg,gif,png,rar,zip' WHERE gid = 4")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 0, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 5")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 0, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 6")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 7")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 8")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 1, allowpostattach = 0, maxattachsize = 0, attachextensions = '', allowinvate = 0, invateprice = 0, invatemaxnum = 0, invateexpiryday = 0 WHERE gid = 9")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 0, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 10")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 0, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 11")

	RQ.Execute("UPDATE "& TablePre &"usergroups SET allowgetattach = 0, allowpostattach = 0, maxattachsize = 0, attachextensions = '' WHERE gid = 12")

	RQ.Execute("SELECT * INTO "& TablePre &"pms_tmp FROM "& TablePre &"pms")
	RQ.Execute("DROP TABLE "& TablePre &"pms")
	RQ.Execute("CREATE TABLE "& TablePre &"pms(pmid int NOT NULL PRIMARY KEY,uid int NOT NULL,message ntext NOT NULL,posttime datetime NOT NULL)")
	RQ.Execute("CREATE INDEX IX_"& TablePre &"pms ON "& TablePre &"pms (uid ASC,posttime DESC)")
	RQ.Execute("INSERT INTO "& TablePre &"pms (pmid, uid, message, posttime) SELECT pmid, uid, message, posttime FROM "& TablePre &"pms_tmp")
	RQ.Execute("DROP TABLE "& TablePre &"pms_tmp")

	Call closeDatabase()

	Dim strCommon
	strCommon = LoadFile("./include/common.inc.asp")
	strCommon = Preg_Replace(strCommon, "Const GBABOOKBBSVERSION = ""(.*?)""", "")
	strCommon = Replace(strCommon, "Const INGBABOOK = True"& vbCrLf, "Const INGBABOOK = True"& vbCrLf &"Const SHOWVERSION = ""1.1""")
	strCommon = Replace(strCommon, "Rs.Close", "")
	Call MakeFile(strCommon, "./include/common.inc.asp")

	On Error Resume Next
	Dim Fso, tFile
	Set Fso = Server.CreateObject("Scripting.FileSystemObject")
	Set tFile = Fso.CreateTextFile(Server.MapPath("./cache/index.html"), True)
	Set tFile = Nothing

	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./htmls/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./images/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./images/attachicons/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./images/common/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./images/manage/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./include/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./include/items/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./js/index.html"))
	Call Fso.CopyFile(Server.MapPath("./cache/index.html"), Server.MapPath("./js/swfupload/index.html"))

	Call Fso.DeleteFile(Server.MapPath("./cache/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./htmls/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./images/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./images/common/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./images/manage/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./include/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./include/items/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./js/index.asp"))
	Call Fso.DeleteFile(Server.MapPath("./upgrade_to_11_access.asp"))

	Set Fso = Nothing

	Call RQ.showTips("升级完毕。<span class=""red"">请登陆FTP，如果发现升级文件还存在，请务必手动删除。</span><br /><a href=""index.asp"" class=""bluelink"">点击这里进入论坛</a>", "", "HALTED")
End Sub

'========================================================
'显示升级界面
'========================================================
Sub Main()
	RQ.Header()
%>
<br />
<br />
<table width="600" border="0" cellpadding="0" cellspacing="0" class="tblborder" align="center" style="margin: 0px auto;">
  <tr class="header">
    <td>提示信息</td>
  </tr>
  <tr>
    <td>强烈建议您先关闭网站，再进行升级。如果您在升级过程中遇到任何问题，请到<a href="http://www.gbabook.net/" target="_blank" class="underline">官方论坛</a>求助。</td>
  </tr>
</table>
<br />
<form method="post" id="install" action="?action=upgrade" onsubmit="$('btnsubmit').value='正在提交,请稍后...';$('btnsubmit').disabled=true;">
  <table width="600" border="0" cellpadding="0" cellspacing="0" class="tblborder" align="center" style="margin: 0px auto;">
    <tr class="header">
      <td height="25" colspan="2"><strong>GBABOOK BBS V1.01升级V1.1(Access版)</strong></td>
    </tr>
	<tr height="25">
      <td>&nbsp;</td>
      <td><input type="submit" id="btnsubmit" value="点击运行升级程序" class="button" /></td>
    </tr>
  </table>
</form>
<%
	RQ.Footer()
End Sub
%>