<%Option Explicit%>
<!--#include file="conn.asp"-->
<%DoWhat="保存留言"
Dim action
IsErr=false
action=request.querystring("action")
if action="" then
		IsErr=true
		WhatErr="<LI>错误的操作参数，请从合法链接进入！</LI>"
else
		Dim name,email,sex,homeurl,face,submain,ly
		name=Replace(Request.Form("name"),"'","&#39;")
		email=Replace(Request.Form("email"),"'","")
		sex=Request.Form("sex")
		homeurl=Replace(Request.Form("homeurl"),"'","")
		face=Request.Form("face")
		submain=Replace(Request.Form("submain"),"'","&#39;")
		ly=Replace(Request.Form("ly"),"'","&#39;")
		email=trim(email)
		homeurl=trim(homeurl)
end if
		if name="" or submain="" or ly="" then
			IsErr=true
			WhatErr=WhatErr & "<LI>姓名、主题、内容均不能为空！</LI>"
		end if
		if len(name)>20 or len(submain)>100 or len(ly)>16240 then
			IsErr=true
			WhatErr=WhatErr & "<LI>姓名、主题、内容均不能超过限定的长度！</LI>"
		end if
		if len(email)>180 or len(homeurl)>180 then
			IsErr=true
			WhatErr=WhatErr & "<LI>邮箱、主页地址均不能超过180个字符！</LI>"
		end if
if IsErr then 
			DoWhat=DoWhat & "失败"
else
			name=server.HTMLEncode(name)
			email=server.HTMLEncode(email)
			homeurl=server.HTMLEncode(homeurl)
			submain=server.HTMLEncode(submain)
			ly=server.HTMLEncode(ly)
			if sex=0 then sex=False else sex=True
			if face="" or (not isnumeric(face)) then face=0
			if homeurl<>"" and left(homeurl,7)<>"http://" then
				homeurl="http://" & homeurl
			elseif homeurl="http://" then
				homeurl=""
			end if
			if action="addnew" then
		  	call addnew()
			else
				Dim submainid
				submainid=request.querystring("id")
				if submainid="" or (not isnumeric(submainid)) then
					IsErr=true
					WhatErr=WhatErr & "<LI>请不要通过非法途径提交！</LI>"
				else
					if action="addreport" then 
						call addreport()
					elseif action="saveedit" then
						If session("admin") then
							call SaveEdit()
						else
							IsErr=true
							WhatErr="<LI>您不是管理员或还没有登陆，请先<a href=""admin.asp"">登陆！</a></LI>"
						end if
					else
						IsErr=true
						WhatErr="<LI>错误的操作参数，请从合法链接进入！</LI>"
			  	end if
			  end if
			end if
			if IsErr then
				DoWhat=DoWhat & "失败"
			else
				set Rs=nothing
				conn.close
				set conn=nothing
				DoWhat=DoWhat & "成功"				
				session("FileUrl")=""
				session("postname")=name
				session("postemail")=email
				session("posthomeurl")=homeurl
				session("sex")=sex
			end if
end if

sub backurl()	'判断返回页面地址
	if session("BackUrl")>"" then
		ToUrl=session("BackUrl")
		session("BackUrl")=""
	end if
end sub

sub addnew()	'新增
	ToUrl="default.asp"
	Conn.Execute("insert into Data(name,sex,email,homeurl,face,ip,SubMain,ly,lastname) values ('" & name & "'," & sex & ",'" & email & "','" & homeurl & "'," & face & ",'" & Request.Servervariables("REMOTE_ADDR") & "','" & submain & "','" & ly & "','-----')")
end sub

sub addreport()	'回复
	call backurl()
	Conn.Execute("update Data set report=report+1,lastname='" & name & "',lasttime='" & Now() & "' where id=" & submainid)
	Conn.Execute("insert into Data(SubMainNumber,name,sex,email,homeurl,face,ip,SubMain,ly) values (" & submainid & ",'" & name & "'," & sex & ",'" & email & "','" & homeurl & "'," & face & ",'" & Request.Servervariables("REMOTE_ADDR") & "','" & submain & "','" & ly & "')")
end sub

sub SaveEdit()	'编辑
	call backurl()
	ly=ly & " [align=right][color=#666666][u]曾被管理员编辑于" & Now() & "[/u][/color][/align]"
	Conn.Execute("update Data set name='" & name & "',sex=" & sex & ",email='" & email & "',homeurl='" & homeurl & "',face=" & face & ",SubMain='" & submain & "',ly='" & ly & "' Where id =" & submainid)
end sub

call HeadHtml()		'网页头过程
call FounderErr() '信息过程
call FootHtml()		'网页尾过程
%>
