<!--#include file="inc/MainClass.asp"-->
<!--#include file="inc/pinyin.asp"-->
<%
'******************************************************************************************
'******************************************************************************************
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>数据库检测</title>
<style>
	body{text-align:center}#installbox{background-color:white;border:1px solid #1B76B7;margin:0 auto;width:600px;text-align:left;margin-top:2em}
	.msgtitle{padding:3px 3px;color:white;font-weight:700;line-height:21px;height:25px;font-size:12px;border-bottom:1px solid #1B76B7; text-indent:3px; background-color:#1B76B7}
	#msgbody{font-size:12px;line-height:25px;height:auto;padding-top:2em;}#msgbottom{text-align:center;height:20px;line-height:20px;font-size:12px;background-color:#1b76b7;color:#FFFFFF;overflow:auto;}
	label{display:block;padding-left:50px;height:30px;line-height:30px}label:hover{background-color:#f5f5f5}h3{font-size:18px;font-family:"黑体";font-weight:300;border-bottom:1px solid #C4E1FF;padding:2px 0 2px 30px;color:#333}
	input{border-width:0;}.inpt{border:1px solid;border-color:#CCCCCC;height:23;height:23px;}
	textarea, input{padding:2px;border:1px solid;border-color:#666 #ccc #ccc #666;background:#F9F9F9;color:#333}
	.btn{margin:3px 0;padding:2px 5px;*padding:4px 5px 1px;border-color:#ddd #666 #666 #ddd;background:#DDD;color:#000;cursor:pointer;vertical-align:middle}
</style>
</head>
<body>
<%
dim action,back ,gConn,gCols(1):action = getForm("action", "get"):back=getForm("back", "get")
dim currentDir,timeStr:timeStr=ifthen(databaseType=0,"now()","getdate()")
Select case action
	case "chk" : chk
	case "image" : upImage
	case "letter" : setPinyin
	case "field":CreateField
	case "other": chkother
	case "install" : chkinstall
	case else : chk
End Select
%>
</body>
</html>
<%
sub chk
goAdmin 1
	'if not CheckIndexs() OR not isExistTable("m_review") then
		'response.Redirect("?action=install")
	'elseif back="install" OR isExistField("type","m_type")=false or isExistField("data","m_recycle")=false or isExistField("temp","m_lang")=false then
		'response.Redirect("?action=image&page=1&back="&back)
	'else
		'goAdmin 1
	'end if
end sub

Sub goAdmin(goType)
	if goType=0 then 
		alertMsg "恭喜，Version:4.0已经升级成功,请手动删除根目录的chk_database.asp文件，现在进入后台登陆界面","admin/index.asp" 
	else
		alertMsg "恭喜，Version:4.0数据库升级成功,请手动删除根目录的chk_database.asp文件!\n现在返回安装","install.asp?action=stepTwo" 
	end if
End Sub

Sub CreateField
	if isExistField("data","m_keyword")=false then conn.db "alter table {pre}data add m_keyword text","execute":conn.db "update {pre}data set m_keyword=''","execute":echo "<label>表m_data新增字段m_keyword成功</label>"
	if isExistField("data","m_enname")=false then conn.db "alter table {pre}data add m_enname varchar(120)","execute":conn.db "update {pre}data set m_enname=''","execute":echo "<label>表m_data新增字段m_enname成功</label>"
	if isExistField("data","m_director")=false then conn.db "alter table {pre}data add m_director varchar(50)","execute":conn.db "update {pre}data set m_director=''","execute":echo "<label>表m_data新增字段m_director成功</label>"
	if isExistField("data","m_lang")=false then conn.db "alter table {pre}data add m_lang varchar(30)","execute":conn.db "update {pre}data set m_lang=''","execute":echo "<label>表m_data新增字段m_lang成功</label>"
	if isExistField("data","m_tread")=false then conn.db "alter table {pre}data add m_tread int default 0","execute":conn.db "update {pre}data set m_tread=0","execute":echo "<label>表m_data新增字段m_tread成功</label>"
	if isExistField("data","m_dayhit")=false then conn.db "alter table {pre}data add m_dayhit int default 0","execute":conn.db "update {pre}data set m_dayhit=0","execute":echo "<label>表m_data新增字段m_dayhit成功</label>"
	if isExistField("data","m_weekhit")=false then conn.db "alter table {pre}data add m_weekhit int default 0","execute":conn.db "update {pre}data set m_weekhit=0","execute":echo "<label>表m_data新增字段m_weekhit成功</label>"
	if isExistField("data","m_monthhit")=false then conn.db "alter table {pre}data add m_monthhit int default 0","execute":conn.db "update {pre}data set m_monthhit=0","execute":echo "<label>表m_data新增字段m_monthhit成功</label>"
	if isExistField("data","m_datetime")=false then conn.db "alter table {pre}data add m_datetime datetime default "&timeStr&"","execute":conn.db "update {pre}data set m_datetime=m_addtime","execute":echo "<label>表m_data新增字段m_datetime成功</label>"
	if isExistField("data","m_recycle")=false then conn.db "alter table {pre}data add m_recycle tinyint default 0","execute":conn.db "update {pre}data set m_recycle=0","execute":echo "<label>表m_data新增字段m_recycle成功</label>"
	if isExistField("data","m_score")=false then conn.db "alter table {pre}data add m_score int default 0","execute":conn.db "update {pre}data set m_score=0","execute":echo "<label>表m_data新增字段m_score成功</label>"

	if isExistField("temp","m_director")=false then conn.db "alter table {pre}temp add m_director varchar(50)","execute":conn.db "update {pre}temp set m_director=''","execute":echo "<label>表m_temp新增字段m_director成功</label>"
	if isExistField("temp","m_lang")=false then conn.db "alter table {pre}temp add m_lang varchar(30)","execute":conn.db "update {pre}temp set m_lang=''","execute":echo "<label>表m_temp新增字段m_lang成功</label>"

	if isExistField("type","m_type")=false then conn.db "alter table {pre}type add m_type tinyint default 0","execute":conn.db "update {pre}type set m_type=0","execute":echo "<label>表m_type新增字段m_type成功</label>"
	if isExistField("type","m_keyword")=false then conn.db "alter table {pre}type add m_keyword text","execute":echo "<label>表m_type新增字段m_keyword成功</label>"
	if isExistField("type","m_description")=false then conn.db "alter table {pre}type add m_description text","execute":echo "<label>表m_type新增字段m_description成功</label>"
	if isExistField("type","m_subtemplate")=false then conn.db "alter table {pre}type add m_subtemplate varchar(150)","execute":conn.db "update {pre}type set m_subtemplate='content.html' WHERE m_type=0","execute":conn.db "update {pre}type set m_subtemplate='news.html' WHERE m_type=1","execute":echo "<label>表m_type新增字段m_subtemplate成功</label>"

	if isExistField("info","m_hit")=false then conn.db "alter table {pre}info add m_hit int default 0","execute":conn.db "update {pre}info set m_hit =0 where m_type='news'","execute":echo "<label>表m_info新增字段m_hit成功</label>"
End Sub

sub chkother
	CreateField
	if back="install" then
		goAdmin 1
	else
		goAdmin 0
	end if
end sub

Function upImage
	echo "图片路径检测升级<br/>"
	dim rsObj,vpage,i
	vpage = getForm("page", "get"): if isnul(vpage) then vpage=1
	 set rsObj = conn.db("SELECT  * from m_data where left(m_pic,7) <>'http://' and left(m_pic,7)='images/'","records3")
		if rsObj.recordcount=0  then 
		echo "图片替换全部完成!开始检测数据首字母<br/>":session("max2.5_update_count1")=""
		echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=letter&back="&back&"&page=1';}</script>"
		exit function
	end if 
	rsObj.pagesize=500
	if isNul(session("max2.5_update_count1")) then session("max2.5_update_count1")=rsObj.pagecount
	rsObj.absolutepage= 1 
	echo "共<font color=red>"&session("max2.5_update_count")&"</font>页,当前为第<font color=red>"&vpage&"</font>页</br>"
	for i=0 to rsObj.pagesize
		if rsobj.eof then 
			echo "图片替换全部完成!开始检测数据首字母<br/>":session("max2.5_update_count1")=""
			echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=letter&back="&back&"&page=1';}</script>"
			exit function
		end if 
		rsObj("m_pic")  =  "pic/"&getSubStrByFromAndEnd(rsObj("m_pic"),"images/","","start")
		rsObj.update
		echo "数据<font color=red>"&rsObj("m_name")&"</font>图片地址更新成功<br/>"
		rsObj.movenext
	next
	rsObj.close:set rsObj = nothing
	echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=image&back="&back&"&page="&vpage+1&"';}</script>"
End Function 

Function setPinyin
	echo "正在进行：首字母检测升级<br/>"
	dim rsObj,vpage,i,t
	vpage = getForm("page", "get"): if isnul(vpage) then vpage=1
	if isExistField("data","m_letter")=false then conn.db "alter table {pre}data add m_letter varchar(1)","execute"
	if isExistField("data","m_enname")=false then conn.db "alter table {pre}data add m_enname varchar(120)","execute" 
	set rsObj = conn.db("select * from {pre}data ","records3")
	if rsObj.recordcount=0 then 
		echo "首字母替换完成!<br/>":session("max2.5_update_count2")=""
		echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=other&back="&back&"&page=1';}</script>"
		exit function
	end if 
	rsObj.pagesize=500
	rsObj.absolutepage= vpage
	if isNul(session("max2.5_update_count2")) then session("max2.5_update_count2")=rsObj.pagecount
	echo "共<font color=red>"&session("max2.5_update_count2")&"</font>页,当前为第<font color=red>"&vpage&"</font>页<br/>"
	for i=0 to rsObj.pagesize
		if rsObj.eof then
			echo "首字母替换完成!<br/>":session("max2.5_update_count2")=""
			echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=other&back="&back&"&page=1';}</script>"
		exit function
		end if
		t=MoviePinYin(rsObj("m_name"))
		rsObj("m_enname") = t:rsObj("m_letter") = Left(t,1)
		echo "数据<font color=red>"&rsObj("m_name")&"</font>拼音和首字母更新成功<br/>"
		rsObj.update:rsObj.movenext
	next
	rsObj.close:set rsObj = nothing
	echo "暂停2秒后继续生成<br/><script language=""javascript"">setTimeout(""makeNextPage()"",2000);function makeNextPage(){location.href='?action=letter&back="&back&"&page="&vpage+1&"';}</script>"
End Function

Function GetAllColumnName(ByVal table)
	if gCols(0)<>table then
		dim rsObj,i,ret:ret=" ,":set rsObj = conn.db("select top 1 * from {pre}"&table&" where 1=1","records1")
		for i=0 to rsObj.Fields.count-1:ret=ret&rsObj.Fields(i).name&",":next
		rsObj.close:set rsObj = nothing:gCols(1)=ret:gCols(0)=table
	end if
	GetAllColumnName=gCols(1)
End Function

Function isExistField(ByVal stable,ByVal sCol)
	isExistField=InStr(GetAllColumnName(stable),","&sCol&",")>0
End Function

Function CheckIndexs()
	CheckIndexs=isExistIndex("m_data","m_type") AND isExistIndex("m_data","m_topic") AND isExistIndex("m_data","m_commend") AND isExistIndex("m_info","m_videoid")
End Function

Sub chkinstall
on error resume next
%>
<div id='installbox'><div class='msgtitle'>Maxcms4.0安装向导 >> 数据库设置</div>
<table style="width:598px;text-align:center;font-size:10pt;"><tr><td align="center"><span onClick="javascript:location.href='install.asp?action=main'" style="display:block;float:left;cursor:pointer;background-color:#62C0FF;width:146px;color:#ffffff;border:solid 1px #ffffff;font-size:12px;"><font size="4"><b>1.</b></font>基本设置</span><span style="display:block;float:left;cursor:pointer;background-color:#62C0FF;width:146px;color:#ffffff;width:146px;border:solid 1px #ffffff;font-size:12px;"><font size="4"><b>2.</b></font>数据库设置</span><span style="display:block;float:left;background-color:#eeeeee;width:146px;border:solid 1px #ffffff;font-size:12px;"><font size="4"><b>3.</b></font>管理员设置</span><span style="display:block;float:left;background-color:#eeeeee;width:146px;border:solid 1px #ffffff;font-size:12px;"><font size="4"><b>4.</b></font>运行模式设置</span></td></tr></table>
<div id='msgbody'>
<h3>数据库检查结果：</h3>
<%
	if not isExistTable("m_manager") then
	conn.db "create table m_manager(m_id  int identity(1,1) not null  primary key,m_username varchar(20),m_pwd varchar(50),m_state tinyint default 1,m_logintime datetime ,m_loginip varchar(15),m_level tinyint  default 0,m_random varchar(40))","execute"
	echo "<label>m_manager数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_data") then
	conn.db "create table m_data(m_id  int identity(1,1) not null primary key,m_name varchar(120),m_type int default 0,m_state int default 0,m_pic varchar(200),m_hit int default 0,m_digg int default 0,m_actor varchar(255),m_note text,m_des text,m_topic int default 0,m_color varchar(10),m_commend tinyint default 0,m_wrong tinyint default 0,m_addtime datetime default "&timeStr&",m_publishyear int default 0,m_publisharea varchar(20),m_playdata text,m_downdata text,m_isunion tinyint default 0,m_letter varchar(1),m_keyword text,m_tread int default 0,m_director varchar(50),m_lang varchar(30),m_dayhit int default 0,m_weekhit int default 0,m_monthhit int default 0,m_enname varchar(120),m_datetime datetime default "&timeStr&",m_recycle tinyint default 0,m_score int default 0)","execute"
	echo "<label>m_data数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_news") then
	conn.db "create table m_news(m_id int identity(1,1) not null primary key,m_type int default 0,m_vid int default 0,m_title varchar(255),m_author varchar(80),m_color varchar(20),m_pic varchar(200),m_from varchar(50),m_keyword text,m_outline text,m_content text,m_commend tinyint default 0,m_hit int default 0,m_dayhit int default 0,m_weekhit int default 0,m_monthhit int default 0,m_note tinyint default 0,m_topic int default 0,m_digg int default 0,m_tread int default 0,m_entitle varchar(120),m_letter varchar(1),m_addtime datetime default "&timeStr&",m_datetime datetime default "&timeStr&",m_recycle tinyint default 0,m_score int default 0)","execute"
	echo "<label>m_news数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_type") then
	conn.db "create table m_type(m_id int identity(1,1) not null primary key,m_name varchar(60),m_enname varchar(40),m_sort int default 0,m_upid int default 0,m_hide tinyint default 0,m_template varchar(150),m_unionid varchar(255),m_keyword text,m_description text,m_type tinyint default 0,m_subtemplate varchar(150))","execute"
	echo "<label>m_type数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_link") then
	conn.db "create table m_link(m_id int identity(1,1) not null primary key,m_type varchar(10),m_name varchar(100),m_pic varchar(200),m_url varchar(200),m_des varchar(255),m_sort int  default 0)","execute"
	echo "<label>m_link数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_selflabel") then
	conn.db "create table m_selflabel(m_id int identity(1,1) not null primary key,m_name varchar(40),m_content text,m_des varchar(255),m_addtime datetime default "&timeStr&")","execute"
	conn.db "INSERT INTO m_selflabel (m_name,m_content,m_des)VALUES('areasearch','','地区搜索')","execute"
	conn.db "INSERT INTO m_selflabel (m_name,m_content,m_des)VALUES('yearsearch','','按发行年份查看电影')","execute"
	conn.db "INSERT INTO m_selflabel (m_name,m_content,m_des)VALUES('actorsearch','','演员名字')","execute"
	conn.db "INSERT INTO m_selflabel (m_name,m_content,m_des)VALUES('nav_bottom_banner','','导航栏下方通栏广告')","execute"
	echo "<label>m_selflabel数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_topic") then
	conn.db "create table m_topic(m_id int identity(1,1) not null primary key,m_name varchar(100),m_sort int default 0,m_template varchar(255),m_enname varchar(60),m_pic varchar(255),m_des text)","execute"
	echo "<label>m_topic数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_ads") then
	conn.db "create table m_ads(m_id int identity(1,1) not null primary key,m_name varchar(100),m_enname varchar(60),m_des varchar(255),m_content text,m_addtime datetime default "&timeStr&")","execute"
	echo "<label>m_ads数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_info") then
	conn.db "create table m_info(m_id int identity(1,1) not null primary key,m_author varchar(80),m_title varchar(255),m_type varchar(20),m_videoid int default 0,m_content text,m_ip varchar(20),m_addtime datetime default "&timeStr&",m_color varchar(20),m_hit int default 0)","execute"
	echo "<label>m_info数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_review") then
	conn.db "create table m_review(m_id int identity(1,1) not null primary key,m_author varchar(80),m_type tinyint default 0,m_videoid int default 0,m_content text,m_ip varchar(20),m_addtime datetime default "&timeStr&",m_reply int default 0,m_agree int default 0,m_anti int default 0,m_pic text,m_vote int default 0,m_check tinyint default 0)","execute"
	echo "<label>m_review数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_leaveword") then
	conn.db "create table m_leaveword(m_id int identity(1,1) not null primary key,m_replyid int default 0,m_author varchar(80),m_qq varchar(30),m_mail varchar(50),m_content text,m_ip varchar(20),m_addtime datetime default "&timeStr&")","execute"
	echo "<label>m_leaveword数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("m_temp") then
	conn.db "create table m_temp(m_id int identity(1,1) not null primary key,m_name varchar(120),m_type int default 0,m_state int default 0,m_pic varchar(200),m_actor varchar(255),m_des text,m_addtime datetime default "&timeStr&",m_playdata text,m_publishyear int default 0,m_publisharea varchar(50),m_director varchar(50),m_lang varchar(30))","execute"
	echo "<label>m_temp数据表不存在，已经创建成功。</label>"
	end if

	if not isExistTable("nodownload") then
		conn.db "create table nodownload(notdown image)","execute"
		conn.db "insert into nodownload (notdown) values ('┼氠潯┼')","execute"
		echo "<label>nodownload数据表不存在，已经创建成功。</label>"
		echo "<label>防下载处理成功</label>"
	end if
	CreateField:CreateIndexs
	echo "</div>"
	echo "<div id='msgbottom'>Powered By "&siteName&"</div></div>"
	terminateAllObjects
%>
</form>
</body>
</html>
<%
	if conn.db("SELECT Count(*) FROM {pre}data WHERE m_enname=''","execute")(0)>0 then
		alertMsg "准备对数据库进行升级....","?action=chk&back=install"
	else
		alertMsg "数据库检查完毕\n返回安装","install.asp?action=stepTwo"
	end if
End Sub

Sub CreateIndexs
	if Not isExistIndex("m_data","m_type") then conn.db "CREATE INDEX m_type ON m_data (m_type)","execute"
	if Not isExistIndex("m_data","m_topic") then conn.db "CREATE INDEX m_topic ON m_data (m_topic)","execute"
	if Not isExistIndex("m_data","m_commend") then conn.db "CREATE INDEX m_commend ON m_data (m_commend)","execute"
	if Not isExistIndex("m_data","m_wrong") then conn.db "CREATE INDEX m_wrong ON m_data (m_wrong)","execute"
	if Not isExistIndex("m_data","m_letter") then conn.db "CREATE INDEX m_letter ON m_data (m_letter)","execute"
	if Not isExistIndex("m_data","m_recycle") then conn.db "CREATE INDEX m_recycle ON m_data (m_recycle)","execute"

	if Not isExistIndex("m_news","m_type") then conn.db "CREATE INDEX m_type ON m_news (m_type)","execute"
	if Not isExistIndex("m_news","m_vid") then conn.db "CREATE INDEX m_vid ON m_news (m_vid)","execute"
	if Not isExistIndex("m_news","m_topic") then conn.db "CREATE INDEX m_topic ON m_news (m_topic)","execute"
	if Not isExistIndex("m_news","m_commend") then conn.db "CREATE INDEX m_commend ON m_news (m_commend)","execute"
	if Not isExistIndex("m_news","m_letter") then conn.db "CREATE INDEX m_letter ON m_news (m_letter)","execute"
	if Not isExistIndex("m_news","m_recycle") then conn.db "CREATE INDEX m_recycle ON m_news (m_recycle)","execute"

	if Not isExistIndex("m_review","m_type") then conn.db "CREATE INDEX m_type ON m_review (m_type)","execute"
	if Not isExistIndex("m_review","m_videoid") then conn.db "CREATE INDEX m_videoid ON m_review (m_videoid)","execute"
	if Not isExistIndex("m_review","m_reply") then conn.db "CREATE INDEX m_reply ON m_review (m_reply)","execute"

	if Not isExistIndex("m_info","m_videoid") then conn.db "CREATE INDEX m_videoid ON m_info (m_videoid)","execute"
	echo "<label>重建索引，已经创建成功。</label>"
End Sub

Function getConn()
	if Not isObject(gConn) then
		Dim connTest,connStr
		if databaseType=1 then
			connStr="Provider=Sqloledb;Data Source="&databaseServer&";Initial Catalog="&databaseName&";User ID="&databaseUser&";Password="&databasePwd&";"
		elseif databaseType=0 then
			connStr="Provider=Microsoft.Jet.OLEdb.4.0;Data Source="&server.mappath(accessFilePath)
		end if
		set gConn=server.CreateObject(CONN_OBJ_NAME)
		gConn.open connStr
	end if
	SET getConn=gConn
End Function

Function isExistTable(tName)
	dim tables:set tables = getConn().OpenSchema(20,array(empty,empty,tName,"table")):isExistTable=tables.eof = false:set tables = nothing
End Function

Function isExistIndex(tName,sIndexName)
	Dim Indexs:SET Indexs=getConn().OpenSchema(12,array(empty,empty,sIndexName,empty,tName)):isExistIndex=Indexs.eof = false:SET Indexs=Nothing
End Function
%>