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

	rem ------获得asp文件参数------
	call getInput()

	rem -----检查版面ID的合法性------	
	dim boardRs
	set boardRs=conn.execute("select lockboard from bbs_board where boardid="&cstr(boardid))
	if boardRs.bof and boardRs.eof then
		Errmsg=ErrMsg+"<Br>"+"<li>你要加贴的版面不存在！"
		FoundError=true
	else
		lockboard=boardRs("lockboard")

	end if
	boardRs.close
	set boardRs=nothing
	if err.number<>0 then err.clear
	
	foundError=false
	ErrMsg=""

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

	if foundError=true then
  	       	call Error()
	else
		call checkUser()
		if foundError then
  	       		call Error()
		else
			call saveAnnounce()
		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 huiyuan,password,lockuser,userclass,article,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>您的密码不正确(可能该名字被他人占用了，请尝试用<a href=reg.asp>别的名字</a>)"
	       	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")+3 
        	rs.update

		if lockboard=1 then
			if rs("userclass")<>user_level9 then
				Errmsg=ErrMsg+"<Br>"+"<li>您没有权限在本版面发布贴子！"
				FoundError=true
			end if
		elseif 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 saveAnnounce()
        DateTimeStr=CSTR(NOW()+TIMEADJUST/24)
        dim cmdTemp
	dim InsertCursor
	Set InsertCursor = Server.CreateObject("ADODB.Recordset")
        cmdTemp="SELECT * FROM bbs_content WHERE (UserName IS NULL)"
       	InsertCursor.Open cmdTemp, conn, 1, 3
	     	InsertCursor.AddNew
		InsertCursor("BoardID")=boardID
	     	InsertCursor("ParentID")=0
         	InsertCursor("Child")=0
	     	InsertCursor("UserName")=UserName
	     	InsertCursor("Topic")=Topic
	     	InsertCursor("Body")=Body
	     	InsertCursor("DateAndTime")=DateTimeStr
	     	InsertCursor("hits")=0
	     	InsertCursor("length")=strlength(body)
	     	InsertCursor("rootID")=0
	     	InsertCursor("layer")=1
	     	InsertCursor("orders")=0
         	InsertCursor("ip")=ip
         	InsertCursor("Expression")=Expression
         	InsertCursor("locktopic")=0
         	InsertCursor("istop")=0
         	InsertCursor("signflag")=signflag
         	InsertCursor("emailflag")=mailflag
	     	InsertCursor.Update
		InsertCursor.MoveLast
         	announceid=InsertCursor("AnnounceID")
         	InsertCursor("RootID")= announceid
	     	InsertCursor("Times")= announceid
	     	InsertCursor.Update
 	    	InsertCursor.close
		set InsertCursor=nothing

        sql="SELECT lastpostuser,lastposttime,lastbbsnum,lasttopicnum FROM bbs_board WHERE boardid="&cstr(boardID)
     	rs.Open sql,conn,1,3
         	rs("lastpostuser")=username
         	rs("lastposttime")=DateTimeStr
         	rs("lastbbsnum")=rs("lastbbsnum")+1
         	rs("lasttopicnum")=rs("lasttopicnum")+1
	     	rs.Update
 	rs.close
  		if err.number<>0 then
	       		err.clear
			ErrMsg=ErrMsg+"<Br>"+"<li>数据库操作失败，请以后再试"&err.Description 
  	       		call Error()
		else
	    		'response.redirect("list.asp?boardid="&boardid&"")
			response.redirect("dispbbs.asp?boardid="&boardid&"&rootid="&announceid&"&id="&announceid)
    		end if

	end sub

	sub activeuser()
	dim rsactiveusers,activeuser
	dim membername
	dim memberword
	dim memberclass
	membername=request.cookies("lgcookie")("username")
	memberword=request.cookies("lgcookie")("password")
	memberclass=request.cookies("lgcookie")("userclass")
	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")=Request.ServerVariables("SCRIPT_NAME")
		rsactiveusers.update
	else
		rsactiveusers("lastimebk")=Now()
		rsactiveusers("lastime")=DateToStr(Now())
		rsactiveusers("stats")=Request.ServerVariables("SCRIPT_NAME")
		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
	IP=Request.ServerVariables("REMOTE_ADDR") 
	Expression=Request.Form("Expression")&".gif"
   	Topic=trim(request("subject"))
   	Body=trim(request("body"))
   	UserName=trim(request("username"))
   	boardtype=trim(request("boardtype"))
	signflag=trim(request("signflag"))
	mailflag=trim(request("emailflag"))
   	UserPassWord=trim(request("passwd"))
	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 Topic="" then
   		FoundError=True
   		if Len(ErrMsg)=0 then
      			ErrMsg=ErrMsg+"<Br>"+"<li>主题不应为空"
   		else
      			ErrMsg=ErrMsg+"<Br>"+"<li>主题不应为空"
   		end if
	elseif 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 strLength(body)>AnnounceMaxBytes then
   		ErrMsg=ErrMsg+"<Br>"+"<li>发言内容不得大于" & CSTR(AnnounceMaxBytes) & "bytes"
   		foundError=true
	end if
	if err.number<>0 then err.clear
	end function
%>