<%@ LANGUAGE="VBSCRIPT" %>
<!--#include file="../conn/zkconn.asp"-->
<!-- #include file="bbsinc/char.asp" -->
<!-- #include file="bbsinc/const.asp" -->
<!--#include file="bbsinc/grade.asp"-->
<!--#include file="bbsinc/email.asp"-->
<% 
	dim UserName
	dim userPassword
	dim useremail
	dim article
	dim Topic
	dim body
	dim FoundError
	dim ErrMsg
	dim dateTimeStr
	dim ParentID
	dim UserID
	dim newUser
	dim RootID
	dim iLayer
	dim iOrders
	dim ip
	dim announceid
	dim Expression
	dim FoundUser
	dim boardID,top
	dim boardtype
	dim signflag
	dim mailflag
	dim TIME_ADJUST
   	dim rs
   	dim sql
	dim lockboard
	dim skin
	dim Email,mailbody

	rem ------获取参数------
	call getInput()

	rem -----检查版面ID的合法性------	
	dim boardRs
	set boardRs=conn.execute("select lockboard from bbs_board where boardid=" & boardid)
	if boardRs.bof and boardRs.eof then
		Errmsg=ErrMsg+"<Br>"+"<li>你要加贴的版面不存在！"
		FoundError=true
	else
		lockboard=boardRs("lockboard")
		if lockboard=1 then
			Errmsg=ErrMsg+"<Br>"+"<li>本版面不允许回复！"
			FoundError=true
		end if
	end if
	boardRs.close
	set boardRs=nothing	
	if err.number<>0 then err.clear

	rem -----检查user输入数据的合法性------	
	call chkData()

	if foundError=true then
		call Error()
	else
		call checkUser()
		if foundError then
			call Error()
		else
			call saveReAnnounce()
		end if
	end if	

	set rs=nothing
	conn.close
	set conn=nothing
	rem ----------------------
	rem ------主程序结束------
	rem ----------------------

	rem 检测用户输入数据
	sub checkUser()
   	set rs=server.createobject("adodb.recordset")
	sql="select locktopic from bbs_content where announceid="&cstr(rootid)
	rs.open sql,conn,1,1
	if not rs.eof and not rs.bof then
		if rs("locktopic")=1 then
		Errmsg=ErrMsg+"<Br>"+"<li>本主题已经锁定，不能发表回复。"
		founderror=true
		exit sub
		end if
	end if
	rs.close
   	sql ="select article,huiyuan,password,userclass,lockuser,jifen from [huiyuan] where huiyuan='"&replace(trim(username),"'","''")&"'"
   	rs.open sql,conn,1,3
 
   	if not rs.EOF then
        	FoundUser=True
   	end if
	if not FoundUser then
		Errmsg=ErrMsg+"<Br>"+"<li>本论坛只有<a href=reg.asp>注册用户</a>才能发言！"
		founderror=true
        elseif UserPassword<>rs("password") then
          	ErrMsg=ErrMsg+"<Br>"+"<li>您的密码不正确(可能该名字被他人占用了，请尝试用别的名字)"
	       	foundError=true
	elseif rs("lockuser")=1 then
           	ErrMsg=ErrMsg+"<Br>"+"<li>该用户账号已被锁定，请和管理员联系。"
	       	foundError=true
      	else
         		rs("article")=rs("article")+1
			article=rs("article")
			if rs("userclass")<>user_level7 and rs("userclass")<>user_level8 and rs("userclass")<>user_level9 then
				if rs("article")>=level2_point and rs("article")<level3_point then
					if rs("userclass")<>user_level2 then
						rs("userclass")=user_level2
						rs.update
					end if
				elseif rs("article")>=level3_point and rs("article")<level4_point then
					if rs("userclass")<>user_level3 then
						rs("userclass")=user_level3
						rs.update
					end if
				elseif rs("article")>=level4_point and rs("article")<level5_point then
					if rs("userclass")<>user_level4 then
						rs("userclass")=user_level4
						rs.update
					end if
				elseif rs("article")>=level5_point and rs("article")<level6_point then
					if rs("userclass")<>user_level5 then
						rs("userclass")=user_level5
						rs.update
					end if
				elseif rs("article")>=level6_point then
					if rs("userclass")<>user_level6 then
						rs("userclass")=user_level6
						rs.update
					end if
				end if
			end if
			Response.Cookies("lgcookie")("username") = UserName
			Response.Cookies("lgcookie")("password") = UserPassWord
			Response.Cookies("lgcookie")("userclass") = rs("userclass")
			Response.Cookies("lgcookie").Expires = Now()+365
			session("huiyuan")=Username
			call activeuser()
         		rs("jifen")=rs("jifen")+1
			rs.update
		if lockboard=2 then
			if rs("userclass")<>user_level8 and rs("userclass")<>user_level9 and rs("userclass")<>user_level7 then
				Errmsg=ErrMsg+"<Br>"+"<li>您没有权限在本版面发布贴子！"
				FoundError=true
			end if
		end if
      	end if
    	rs.close
	end sub

	rem 保存贴子信息
	sub saveReAnnounce()
     	dim rsLayer
     	set rsLayer=conn.execute("select layer,orders from bbs_content where announceid="&cstr(parentid)) 

      	if not(rsLayer.eof and rsLayer.bof) then
         	if isnull(rsLayer(0)) then
            		iLayer=0
         	else
            		iLayer=rslayer(0)
         	end if
         	if isNUll(rslayer(1)) then
            		iOrders=0
         	else
            		iOrders=rsLayer(1) 
         	end if
      	else
         	iLayer=0
         	iOrders=0
      	end if
      	rsLayer.close
      	if rootid<>0 then 
         	iLayer=ilayer+1
         	conn.execute "update bbs_content set orders=orders+1 where rootid="&cstr(RootID)&" and orders>"&cstr(iOrders)

         	iOrders=iOrders+1
     	end if      


      	DateTimeStr=CSTR(NOW()+TIMEADJUST/24)
      	'ON ERROR RESUME NEXT 
      	dim cmdTemp
      	dim InsertCursor
      	Set InsertCursor = Server.CreateObject("ADODB.Recordset")
      	cmdTemp = "SELECT *, UserName FROM bbs_content WHERE (UserName IS NULL)"
 	InsertCursor.Open cmdTemp, conn, 1, 3
      		InsertCursor.AddNew
		InsertCursor("BoardID") = boardID
      		InsertCursor("Child") = 0
      		InsertCursor("ParentID") = ParentID 
	  	InsertCursor("UserName") = UserName
	  	InsertCursor("Topic") =Topic
	  	InsertCursor("Body") =Body
	  	InsertCursor("DateAndTime") =DateTimeStr
	  	InsertCursor("hits") =0
	  	InsertCursor("length")=strlength(body)
      		InsertCurSor("RootID")=RootID
      		InsertCurSor("layer")=ilayer
      		InsertCurSor("orders")=iorders
      		InsertCurSor("ip")=ip
      		InsertCurSor("Expression")=Expression
         	InsertCursor("locktopic")=0
         	InsertCursor("istop")=0
         	InsertCursor("signflag")=signflag
         	InsertCursor("emailflag")=mailflag
	  	InsertCursor.Update
      		announceid=InsertCursor("AnnounceID")
	  if err.number<>0 then
          	err.clear
	       	ErrMsg=ErrMsg+"<Br>"+"<li>数据库操作失败，请以后再试:"&err.Description 
  	       	call Error()
	  else
	      	conn.execute "update bbs_content set child=child+1,times="&cstr(announceid)&" where rootID="&cstr(rootID)

		conn.execute "update bbs_board set lastpostuser='"&username&"',lastposttime='"&DateTimeStr&"',lastbbsnum=lastbbsnum+1 where boardid="&cstr(boardID)
		'all replyemail()
	    	'response.redirect("list.asp?boardid="&boardid&"")
		response.redirect("dispbbs.asp?boardid="&boardid&"&rootid="&rootid&"&id="&parentid)
	  end if
		InsertCursor.close
		set InsertCursor=nothing
	end sub

	sub replyemail()
	if EmailFlag<>0 then
	on error resume next
	sql="select bbs_content.EmailFlag,bbs_content.username,[huiyuan].mail from bbs_content,[huiyuan] where bbs_content.username=[huiyuan].huiyuan and bbs_content.announceid="&cstr(ParentID)
	rs.open sql,conn,1,1
	if not rs.eof and not rs.bof then
		if rs("EmailFlag")=1 then
			topic="您在"&ForumName&"发表的文章有人回复了"
			email=rs("mail")
			mailbody=mailbody & ""&rs("username")&"，您好：<br>"
			mailbody=mailbody & "您在"&ForumName&"发表的文章有人回复了<br>"
			mailbody=mailbody & "请到以下地址浏览该贴子内容。<br>"
			mailbody=mailbody & "<a href="&Forumurl&"showannounce.asp?boardid="&boardid&"&rootid="&rootid&"&id="&announceid&" target=_blank>查看贴子内容</a>"
			if EmailFlag=0 then

			elseif EmailFlag=1 then
				call jmail(email)
			elseif EmailFlag=2 then
				call Cdonts(email)
			elseif EmailFlag=3 then
				call aspemail(email)
			end if
			if SendMail<>"OK" then
				Errmsg=Errmsg+"<br>"+"贴子已经成功保存，回复主题作者的Email发送失败。"
				call Error()
			end if
		end if
	end if
	rs.close
	end if
	end sub

	sub activeuser()
	dim rsactiveusers,activeuser
	dim membername
	dim memberword
	dim memberclass
	dim stats
	membername=request.cookies("lgcookie")("username")
	memberword=request.cookies("lgcookie")("password")
	memberclass=request.cookies("lgcookie")("userclass")
	stats=request.cookies("lgcookie")("stats")
	if session.sessionid<>"" then
	activeuser="delete from bbs_online where id="&cstr(session.sessionid)
	Conn.Execute activeuser
	end if
	set rsactiveusers=server.createobject("adodb.recordset")
	activeuser="select * from bbs_online where username='"&membername&"'"
	rsactiveusers.open activeuser,conn,1,3
	if rsactiveusers.eof and rsactiveusers.bof then
		rsactiveusers.addnew
		rsactiveusers("id")=Session.SessionID
		rsactiveusers("username")=membername
		rsactiveusers("userclass")=memberclass
		rsactiveusers("ip")=Request.ServerVariables("REMOTE_HOST")
		rsactiveusers("startime")=Now()
		rsactiveusers("lastimebk")=Now()
		rsactiveusers("lastime")=DateToStr(Now())
		rsactiveusers("browser")=Request.ServerVariables("HTTP_USER_AGENT")
		rsactiveusers("stats")=stats
		rsactiveusers.update
		'session("userid")=""
	else
		rsactiveusers("lastimebk")=Now()
		rsactiveusers("lastime")=DateToStr(Now())
		rsactiveusers("stats")=stats
		rsactiveusers.update
	end if
	rsactiveusers.close
	set rsactiveusers=nothing
	end sub

	rem ------获得asp文件参数------
	sub getInput()
	if request("boardid")="" then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>请指定论坛版面。"
	elseif not isInteger(request("boardid")) then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>非法的版面参数。"
	else
		boardID=request("boardID")
	end if
	if request("followup")="" then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>非法的贴子参数。"
	elseif not isInteger(request("followup")) then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>非法的贴子参数。"
	else
		announceid=request("followup")
		ParentID=request("followup")
	end if
	if request("RootID")="" then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>非法的贴子参数。"
	elseif not isInteger(request("RootID")) then
		foundError=true
		Errmsg=Errmsg+"<br>"+"<li>非法的贴子参数。"
	else
		rootID=request("RootID")
	end if
   	UserName=nohack(request("username"))
   	UserPassWord=nohack(request("passwd"))
	IP=Request.ServerVariables("REMOTE_ADDR") 
	Expression=Request.Form("Expression")&".gif"
   	Topic=trim(request("subject"))
   	Body=trim(request("body"))
	signflag=trim(request("signflag"))
	mailflag=trim(request("emailflag"))
   	boardtype=trim(request("boardtype"))
	end sub

	rem -----检查user输入数据的合法性------	
	function chkData()
	if signflag="yes" then
		signflag=1
	else
		signflag=0
	end if
	if mailflag="yes" then
		mailflag=1
	else
		mailflag=0
	end if

	if UserName="" or strLength(UserName)>20 then
   		ErrMsg=ErrMsg+"<Br>"+"<li>请输入姓名(长度不能大于20)"
   		foundError=True
	elseif Trim(UserPassWord)="" or strLength(UserPassWord)>20 then
   		ErrMsg=ErrMsg+"<Br>"+"<li>请输入密码(长度不能大于20)"
   		foundError=True
	end if
	if strLength(topic)>100 then
   		FoundError=True
   		if strLength(ErrMsg)=0 then
      			ErrMsg=ErrMsg+"<Br>"+"<li>主题长度不能超过100"
   		else
      			ErrMsg=ErrMsg+"<Br>"+"<li>主题长度不能超过100"
   		end if
	end if
	if request("method")="Topic" then
		if topic="" then
			if body="" then
   				ErrMsg=ErrMsg+"<Br>"+"<li>主题和内容必须填写其一。"
   				foundError=True
			end if
		end if
	end if
	if request("method")="fastreply" then
		if body="" then
   		ErrMsg=ErrMsg+"<Br>"+"<li>快速回复请填写发言内容。"
   		foundError=True
		end if
	end if
	if strLength(body)>AnnounceMaxBytes then
   		ErrMsg=ErrMsg+"<Br>"+"<li>发言内容不得大于" & CSTR(AnnounceMaxBytes) & "bytes"
   		foundError=true
	end if
	end function 
%>