<!-- #include file=../../inc/BBSsetup.asp -->
<!-- #include file=../../inc/Board_Popfun.asp -->
<!-- #include file=../inc/bbsmanage_Fun.asp -->
<%
DEF_BBS_HomeUrl = "../../"
initDatabase
GBL_CHK_TempStr = ""
checkSupervisorPass

Dim Action
Action = Left(Request("Action"),14)
If Action <> "Join" and Action <> "Modify" and Action <> "Delete" Then
	Action = "Manage"
End If

Dim LMT_TempletFlag,Form_TempletName,Form_TempletString(4),Form_MaxTempletID
LMT_TempletFlag = 0

Dim LMT_TempletFlagData,LMT_TempletFlagDataNum
LMT_TempletFlagData = Array("首页版面简单列表","首页版面列表信息完全显示","子版面简单列表","子版面详细信息显示")
LMT_TempletFlagDataNum = Ubound(LMT_TempletFlagData,1)

Dim LMT_ID

SiteHead(DEF_SiteNameString & " - 管理员")

UserTopicTopInfo
DisplayUserNavigate("论坛模板管理")%>
<p><a href=TempletManage.asp>管理论坛模板</a>
<a href=TempletManage.asp?action=Join>添加论坛模板</a>
<br><br><%If GBL_CHK_Flag=1 Then
	Select Case Action:
		Case "Join": Join
		Case "Modify": Join
		Case "Delete": Delete
		Case "Manage": Manage
	End Select
Else%>
	<table width=96%>
	<tr>
	<td>
	<%
	If Request("submitflag")="" Then
		Response.Write "<br><b>请先登录</b>"
	Else
		Response.Write "<br><p align=left><font color=ff0000 class=RedFont><b>" & GBL_CHK_TempStr & "</b></font>"
	End If
	DisplayLoginForm
	Response.Write "</p>"%>
	</td>
	</tr>
	</table>
<%End If
UserTopicBottomInfo
closeDataBase
SiteBottom
If GBL_ShowBottomSure = 1 Then Response.Write GBL_SiteBottomString

Function Join

	If Action = "Modify" Then
		LMT_ID = Left(Trim(Request("ID")),14)
		If isNumeric(LMT_ID) = 0 Then LMT_ID = -1
		LMT_ID = Fix(cCur(LMT_ID))
		If LMT_ID < 0 or CheckTempletIDExist(LMT_ID) = 0 Then
			Response.Write "<br><b>编辑的模板不存在!<br>" & VbCrLf
			Exit Function
		End If
	End If
	%>
	<b><%
	If Action = "Modify" Then
		Response.Write "编辑"
	Else
		Response.Write "添加"
	End If%>论坛模板</b>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
	<td>
		<table width="100%" border="0" cellspacing="0" cellpadding="1" bgcolor="<%=DEF_BBS_LightColor%>" class=TBBG1 align=right>
		<tr>
			<td>
				<table width="100%" border="0" cellspacing="1" cellpadding="15">
				<tr bgcolor="<%=DEF_BBS_LightestColor%>" class=TBBG9>
					<td><%
						GBL_CHK_TempStr = ""
						If Request.Form("submitflag")="LKOkxk2" Then
							If CheckFormData=0 Then
								Response.Write "<font color=ff0000 class=RedFont><b>错误信息：" & GBL_CHK_TempStr & "</b></font><br>" & VbCrLf
								DisplayJoinForm
			        		Else
								If UpdateTemplet = 0 Then
									Response.Write "<font color=ff0000 class=RedFont><b>插入出错：" & GBL_CHK_TempStr & "</b></font><br>" & VbCrLf
									DisplayJoinForm
								Else
									Response.Write "<font color=008800 class=GreenFont><b>成功操作!</b></font><br>" & VbCrLf
								End If
							End If
						Else
							DisplayJoinForm
						End If%>
					</td>
				</tr>
				</table>
			</td>
		</tr>
		</table>
	</td>
</tr>
</table>
<%

End Function

Function DisplayJoinForm

	Dim TempN
	If Action = "Modify" Then
		DisplayModifyForm
		Exit Function
	End If%>
	<form action=TempletManage.asp method=post name=form1 id=form1>
	<table>
	<tr>
		<td>模板名称:</td><td><input name=Form_TempletName maxlength=50 value="<%=htmlencode(Form_TempletName)%>" class=fminpt></td>
	</tr>
	<tr valign=top>
		<td width=80>
			模板启用：<br>不勾选则表示不启用</td>
		<td align=left><%
			for TempN = 0 to LMT_TempletFlagDataNum%>	 
			<input type="checkbox" class=fmchkbox name="Limit<%=TempN+1%>" value="1"<%If GetBinarybit(LMT_TempletFlag,TempN+1) = 1 Then
				Response.Write " checked>"
			Else
				Response.Write ">"
			End If%><%=LMT_TempletFlagData(tempN)%><br>
			<%Next%>
			</td>
	</tr><%
			for TempN = 0 to LMT_TempletFlagDataNum%>
	<tr>
		<td><%=LMT_TempletFlagData(tempN)%>:<br>使用JS语法</td><td><textarea name=Form_TempletString<%=tempN%> rows=6 cols=51 class=fmtxtra><%If Form_TempletString(tempN) <> "" Then Response.Write VbCrLf & Server.htmlEncode(Form_TempletString(tempN))%></textarea></td>
	</tr><%Next%>
	<tr>
		<td colspan=2>
			<input name=LMT_GoodNum type=hidden value="0">
			<input name=action type=hidden value="Join">
			<input name=submitflag type=hidden value="LKOkxk2">
			<input type=submit value="提交" class=fmbtn> <input type=reset value="取消" class=fmbtn>
		</td>
	</tr>
	</table></form>

<%End Function

Function DisplayModifyForm

	Dim TempN
	%>
	<form action=TempletManage.asp method=post name=form1 id=form1>
	<table>
	<tr>
		<td>模板名称:</td><td><input name=Form_TempletName maxlength=50 value="<%=htmlencode(Form_TempletName)%>" class=fminpt></td>
	</tr>
	<tr valign=top>
		<td width=80>
			模板启用：<br>不勾选则表示不启用</td>
		<td align=left><%
			for TempN = 0 to LMT_TempletFlagDataNum%>	 
			<input type="checkbox" class=fmchkbox name="Limit<%=TempN+1%>" value="1"<%If GetBinarybit(LMT_TempletFlag,TempN+1) = 1 Then
				Response.Write " checked>"
			Else
				Response.Write ">"
			End If%><%=LMT_TempletFlagData(tempN)%><br>
			<%Next%>
			</td>
	</tr><%
			for TempN = 0 to LMT_TempletFlagDataNum%>
	<tr>
		<td><%=LMT_TempletFlagData(tempN)%>:<br>使用JS语法</td><td><textarea name=Form_TempletString<%=tempN%> rows=6 cols=51 class=fmtxtra><%If Form_TempletString(tempN) <> "" Then Response.Write VbCrLf & Server.htmlEncode(Form_TempletString(tempN))%></textarea></td>
	</tr><%Next%>
	<tr>
		<td colspan=2>
			<input name=action type=hidden value="Modify">
			<input name=submitflag type=hidden value="LKOkxk2">
			<input name=ID type=hidden value="<%=LMT_ID%>">
			<input type=submit value="提交" class=fmbtn> <input type=reset value="取消" class=fmbtn>
		</td>
	</tr>
	</table></form>

<%End Function

Function CheckFormData

	Dim Temp,Rs
	Set Rs = Con.ExeCute("Select Max(ID) from LeadBBS_Templet")
	If Rs.Eof Then
		Form_MaxTempletID = -1
	Else
		Form_MaxTempletID = Rs(0)
		If isNull(Form_MaxTempletID) Then
			Form_MaxTempletID = -1
		Else
			Form_MaxTempletID = cCur(Rs(0))
		End If
	End If

	Dim Temp1,Temp2,TempN
	LMT_TempletFlag = 0
	Temp2 = 1
	For TempN = 0 to LMT_TempletFlagDataNum
		Temp1 = Request("Limit" & TempN+1)
		If Temp1 <> "1" Then Temp1 = "0"
		If Temp1 = "1" Then LMT_TempletFlag = LMT_TempletFlag + cCur(Temp2)
		Temp2 = Temp2*2
	Next

	Form_TempletName = Left(Trim(Request.Form("Form_TempletName")),50)
	For TempN = 0 to LMT_TempletFlagDataNum
		Form_TempletString(TempN) = Trim(Request.Form("Form_TempletString" & TempN))
	Next

	If Form_TempletName = "" or Len(Form_TempletName) > 50 Then
		GBL_CHK_TempStr = GBL_CHK_TempStr & "Error: 模板名称必须填写<br>" & VbCrLf
		CheckFormData = 0
		Exit Function
	End If

	If inStr(LCase(Form_TempletName),"/") or inStr(LCase(Form_TempletName),"\") or inStr(LCase(Form_TempletName),"""") or inStr(LCase(Form_TempletName),"<script") or inStr(LCase(Form_TempletName),"<\script") or inStr(LCase(Form_TempletName),"</script") Then
		GBL_CHK_TempStr = GBL_CHK_TempStr & "Error: 模板名称不能包含有/\""及JS等字符<br>" & VbCrLf
		CheckFormData = 0
		Exit Function
	End If

	For TempN = 0 to LMT_TempletFlagDataNum
		If inStr(LCase(Form_TempletString(TempN)),"<%") > 0 or inStr(LCase(Form_TempletString(TempN)),"include") > 0 or inStr(LCase(Form_TempletString(TempN)),"<script") > 0 or inStr(LCase(Form_TempletString(TempN)),"<\script") > 0 or inStr(LCase(Form_TempletString(TempN)),"</script") > 0 Then
			GBL_CHK_TempStr = GBL_CHK_TempStr & "Error: " & LMT_TempletFlagData(tempN) & "不允许插入js等其它编码！<br>" & VbCrLf
			CheckFormData = 0
			Exit Function
		End If
	Next

	CheckFormData = 1

End Function

Function UpdateTemplet

	Dim SQL,TempN
	If Action = "Join" Then
		SQL = "inSert into LeadBBS_Templet(ID,TempletName,TempletFlag"
		For TempN = 0 to LMT_TempletFlagDataNum
			SQL = SQL & ",TempletString" & TempN
		Next
		SQL = SQL & ") Values("
		SQL = SQL & Form_MaxTempletID + 1 & ",'" & Replace(Form_TempletName,"'","''") & "'," & LMT_TempletFlag

		For TempN = 0 to LMT_TempletFlagDataNum
			SQL = SQL & ",'" & Replace(Form_TempletString(TempN),"'","''") & "'"
		Next
		SQL = SQL & ")"
		Con.ExeCute(SQL)
		LMT_ID = Form_MaxTempletID + 1
	Else
		SQL = "Update LeadBBS_Templet Set TempletFlag=" & LMT_TempletFlag & _
			",TempletName='" & Replace(Form_TempletName,"'","''") & "'"
		For TempN = 0 to LMT_TempletFlagDataNum
			SQL = SQL & ",TempletString" & TempN & "='" & Replace(Form_TempletString(TempN),"'","''") & "'"
		Next
		SQL = SQL & " where ID=" & LMT_ID
		Con.ExeCute(SQL)
	End If
	For TempN = 0 to LMT_TempletFlagDataNum
		If Form_TempletString(TempN) <> "" Then
			WriteTempletFile LMT_ID,TempN,Form_TempletString(TempN)
		Else
			DeleteFiles(Server.Mappath("../../inc/Templet/" & LMT_ID & "_" & TempN & ".JS"))
		End If
	Next
	ReloadTempletStyle(LMT_ID)
	UpdateTemplet = 1

End Function

Sub WriteTempletFile(TempletID,TempN,TempStr)

	ADODB_SaveToFile TempStr,"../../inc/Templet/" & TempletID & "_" & TempN & ".JS"
	If GBL_CHK_TempStr = "" Then
		Response.Write "<br><font color=Green class=GreenFont>2.成功完成设置！</font>"
	Else
		%><%=GBL_CHK_TempStr%><br>服务器不支持在线写入文件功能，请使用FTP等功能，将<font color=Red Class=RedFont><%="inc/Templet/" & TempletID & "_" & TempN & ".JS"%></font>文件替换成框中内容(注意备份)<p>
		<textarea name="fileContent" cols="80" rows="30" class=fmtxtra><%=Server.htmlencode(TempStr)%></textarea><%
		GBL_CHK_TempStr = ""
	End If

End Sub

Sub ReloadTempletStyle(TempletID)

	Dim Rs,GetData,N
	Set Rs = Con.ExeCute("Select StyleID From LeadBBS_Skin where TempletID=" & TempletID)
	If Not Rs.Eof Then
		GetData = Rs.GetRows(-1)
		Rs.Close
		Set Rs = Nothing
		For N = 0 to Ubound(GetData,2)
			ReloadBoardStyleInfo(cCur(GetData(0,N)))
		Next
	Else
		Rs.Close
		Set Rs = Nothing
	End If

End Sub

Rem 检测模板编号ID是否存在
Function CheckTempletIDExist(ID)

	Dim Rs,TempN
	If ID < 0 Then
		CheckTempletIDExist = 1
		Exit Function
	End If
	Set Rs = Con.ExeCute("Select top 1 * from LeadBBS_Templet where ID=" & ID)
	If Rs.Eof Then
		CheckTempletIDExist = 0
	Else
		LMT_TempletFlag = cCur(Rs("TempletFlag"))
		Form_TempletName = Rs("TempletName")
		For TempN = 0 to LMT_TempletFlagDataNum
			Form_TempletString(TempN) = Rs("TempletString" & TempN)
		Next
		CheckTempletIDExist = 1
	End if
	Rs.Close
	Set Rs = Nothing

End Function

Function Manage

	%>
	<script language=javascript>
	function s(ID,TempletName,TempletFlag)
	{
		if(ID=="")return;
		document.write("<tr class=TBBG9 bgcolor=<%=DEF_BBS_LightestColor%> valign=top><td>第" + ID + "模板</td>");
		document.write("<td><a href=TempletManage.asp?action=Modify&ID=" + ID + ">" + TempletName + "</a></td>");
		document.write("<td><a href=TempletManage.asp?action=Modify&ID=" + ID + ">" + TempletFlag + "</a></td>");
		document.write("<td><a href=TempletManage.asp?action=Delete&ID=" + ID + ">删除</a></td></tr>");
	}
	</script>
	<table border="0" cellspacing="0" cellpadding="0" align=center width=100%>
	<tr>
		<td><br>
			<p align=center><b>论坛模板管理</b><br><br></p>
			<table width=100% border="0" cellspacing="1" cellpadding="4" bgcolor="<%=DEF_BBS_LightColor%>" class=TBBG1>
			<tbody>
			<tr height="19" class=TBfour>
				<td width=30><img src=../images/null.gif width=2 height=1><br>编号</td>
				<td><img src=../images/null.gif width=2 height=1><br>名称(修改)</td>
				<td><img src=../images/null.gif width=2 height=1><br>状态</td>
				<td><img src=../images/null.gif width=2 height=1><br>删除</td>
			</tr>
				<%
	Dim Rs,SQL
	SQL = "select ID,TempletName,TempletFlag from LeadBBS_Templet"

	Set Rs = Con.ExeCute(SQL)
	GBL_DBNum = GBL_DBNum + 1
	Dim Num
	Dim GetData
	If Not rs.Eof Then
		Response.Write "<script language=javascript>" & VbCrLf & "s("""
		Response.Write Rs.GetString(,,""",""",""");" & VbCrLf & "s(""","")
		%>","","","");
		</script>
		<%
	Else
		Num = -1
	End If
	Rs.close
	Set Rs = Nothing%>
			</table>
		</td>
	</tr>
	</table>
	<%

End Function

Function Delete

	Dim ID
	ID = Left(Request("ID"),14)
	If isNumeric(ID) = 0 Then ID = 0
	ID = Fix(cCur(ID))
	If Request.Form("DeleteSuer")="E72ksiOkw2" Then
		If DeleteTemplet(ID) > 0 Then
			Response.Write "<p><font color=008800 class=GreenFont><b>已经成功删除编号为" & ID & "的论坛模板！</b></font></p>"
		Else
			Response.Write "<p><font color=ff0000 class=RedFont><b>" & GBL_CHK_TempStr & "</b></font></p>"
		End If
	Else
		%>
		<p><form action=TempletManage.asp method=post>
		注意：删除论坛模板<br>
		<br><b><font color=ff0000 class=RedFont>确认信息： 真的要删除此论坛模板吗？<br><br>
		
		<input type=hidden name=Action value="Delete">
		<input type=hidden name=ID value="<%=urlencode(ID)%>">
		<input type=hidden name=DeleteSuer value="E72ksiOkw2">
			
		<input type=button value=不能删啊 onclick="javascript:history.go(-1);" class=fmbtn>
		<input type=submit value=当然删除 class=fmbtn>
		</form>
	<%End If

End Function

Function DeleteTemplet(ID)

	GBL_CHK_TempStr = ""
	Dim Rs,BoardID
	Set Rs = Con.ExeCute("select top 1 ID from LeadBBS_Templet where ID=" & ID)
	If Rs.Eof Then
		GBL_CHK_TempStr = "错误，不存在此专题区．"
		DeleteTemplet = 0
		Rs.Close
		Set Rs = Nothing
		Exit Function
	End If
	Rs.Close
	Set Rs = Nothing
	Con.ExeCute("Delete from LeadBBS_Templet where ID=" & ID)
	DeleteTemplet = 1

End Function

Function DeleteFiles(path)

	If DEF_FSOString = "" Then Exit Function
	'on error resume next
	Dim fs
	Set fs = Server.CreateObject(DEF_FSOString)
	If err <> 0 Then
		Err.Clear
		Response.Write "<br>服务器不支持FSO，硬盘文件未删除．"
		Exit Function
	End If
	If fs.FileExists(path) Then
		fs.DeleteFile path,True
		DeleteFiles = 1
	Else
		DeleteFiles = 0
	End If
	Set fs = Nothing
	Response.Write "<br>删除文件成功．"

End Function%>