<!--#include file="conn.asp"-->
<!--#include file="inc/function.asp"-->
<!--#include file="inc/class_upload.asp"-->
<!--#include file="inc/md5.asp"-->
<!--#include file="inc/Language.asp"-->
<%
Dim AskD
Set AskD = Server.CreateObject("KwAsk.FunAsk")
	opendb
	set AskD.myConn =conn
	AskD.myPredb =predb
	AskD.Getsetting()
	AskD.GetUserRole()
	
	siteinfo=AskD.siteinfo
	setting=AskD.setting
	UserRole=AskD.UserRole
dim act
act=Trim(Request.QueryString("act"))
if act="ansupfileF" then
   call saveans()
end if
set AskD.myConn =Nothing
closedb
Set AskD = Nothing 

sub saveans()
Dim UpFile,sSavePath,UstrFtyp,pSavePath
Dim oFSO,oFile,Fso
	sSavePath ="UploadFile/"&Year(date())&Right("0"&Month(date()),2)&"/"
    pSavePath=replace(sSavePath,"UploadFile/","")
	oFile=server.MapPath(sSavePath)
	Set oFSO = server.CreateObject("Scripting.FileSystemObject")
	if oFSO.FolderExists(oFile)=false then
	   oFSO.CreateFolder(oFile)
	end if
	Set oFSO = Nothing
	set UpFile = New UpLoadClass
	UpFile.AutoSave = 2
	UpFile.MaxSize =  Int(setting(32) * 1024) '# (1024*1024=1M) SESSION里的单位为KB
	UpFile.FileType = setting(33)
	UpFile.SavePath = sSavePath
	UpFile.Open()
	If UpFile.Save("upfile",0) Then
	    UstrFtyp=Ucase(UpFile.strFtyp)
		IF UstrFtyp="TEXT/PLAIN" Then
		Response.Write "<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""注意，请上传正确文件！"");</script>"
		Response.End()
		End IF
	Else
		Select Case UpFile.Form("upfile_Err")
		Case -1 : Response.Write ""  '允许不上传文件
		Case 1 : Response.Write "<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""文件大小超出限制，请返回重新上传"");</script>":Response.End()
		Case 2 : Response.Write "<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""不允许上传的文件类型，请返回重新上传"");</script>":Response.End()
		Case 3 : Response.Write "<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""文件大小超出限制并且是不允许上传的文件类型，请返回重新上传"");</script>":Response.End()
		Case Else : Response.Write "<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""未知错误，请返回重新上传"");</script>":Response.End()
		End Select
	End If

Dim ansContent,askid,upfiles,addurl,userid,userpass,niming,askUser,ansUser,role,fck,nid,nscore,nltime
ansContent=UpFile.Form("ansContent")
ansContent=AskD.chkAllow(ansContent,3)
askid=UpFile.Form("askid")
upfiles=UpFile.Form("upfile")
addurl=UpFile.Form("addurl")
userid=UpFile.Form("userid")
userpass=UpFile.Form("userpass")
niming=UpFile.Form("niming")
fck=UpFile.Form("fck")
ansContent=AskD.ScriptHtml(ansContent,"iframe",2)
ansContent=AskD.ScriptHtml(ansContent,"script",2)
IF fck<>"1" Then ansContent=AskD.HTMLcode(ansContent)
Set UpFile = Nothing

'判断登陆	
IF Request.Cookies("Muser")="" then
   set rs = Server.CreateObject("adodb.recordset")
   sql="select * from "&predb&"user where userid='"&userid&"' and userpass='"&md5(userpass,16)&"' and isLock=0"
   rs.open sql,conn,1,3
   IF rs.bof and rs.eof then
      Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您没有发布的权限，请重新登录！"");</script>")
      IF upfiles<>"" Then 
      Set FSO = server.CreateObject("Scripting.FileSystemObject")
	    IF FSO.FileExists(Server.mappath(sSavePath&upfiles)) Then
           FSO.DeleteFile server.MapPath(sSavePath&upfiles),True
	    End IF
      Set FSO = Nothing
	  End IF
	  Response.End()
   Else
	  
	  nid=rs("id")
	  nscore=rs("score")
	  nltime=rs("lasttime")
	  
	dim cip
	CIP=Request.ServerVariables("HTTP_X_FORWARDED_FOR")
	If CIP="" then
	   CIP=Request.ServerVariables("REMOTE_ADDR")
	End If
      rs("PreviousIP")=rs("Lastip")
	  rs("PreviousTime")=rs("Lasttime")
	  rs("Lastip")=cip
	  rs("Lasttime")=now()
	  rs.update
	  
	  IF nscore=0 Then
	     Call AskD.Score(nid,setting(11),"首次登陆") '第一次登陆加20分
	  End If
      If DateDiff("h",nltime,now())>Hour(now()) Then
	     Call AskD.Score(id,setting(12),"每天第一次登陆")  '每天第一次登陆加5分
	  End If
	  
	  
	  Response.Cookies("Muser")=Rs("ID")
	  Response.Cookies("Musername")=Rs("userID")
	  Response.Cookies("Grade")=Rs("grade")
	  Response.Cookies("islock")=Rs("islock")
	End IF
	rs.close
    Set rs=nothing
Else
    IF Request.Cookies("islock")<>"0" Then 
       Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您还未通过审核，无提问权限！"");</script>")
	   Response.End()
	End IF
End IF

'权限判断
Role=UserRole(Request.Cookies("Grade"))
IF Instr(Role,",1,")=0 and upfiles<>"" Then 
   Set FSO = server.CreateObject("Scripting.FileSystemObject")
	 IF FSO.FileExists(Server.mappath(sSavePath&upfiles)) Then
	    FSO.DeleteFile server.MapPath(sSavePath&upfiles),True
	 End IF
   Set FSO = Nothing
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您没有上传文件权限，请返回清除！"");</script>")
   Response.End()
End IF
IF Instr(Role,",2,")=0 and addurl<>"" Then 
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您没有参考资料权限，请返回清除！"");</script>")
   Response.End()
End IF

IF Instr(Role,",3,")=0 and fck="1" Then 
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您没有图文问答权限，请返回选择标准问答！"");</scr"&"ipt>")
   Response.End()
End IF

IF niming="1" Then
   set rs = Server.CreateObject("adodb.recordset")
   sql="select Score from "&predb&"user where ID="&Request.Cookies("Muser")
   rs.open sql,conn,1,1
   if rs("Score")<clng(setting(23)) then
      Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""你目前的积分<font color=red><b>"&rs("Score")&"</b></font>小于您的匿名扣分<font color=red><b>"&setting(23)&"</b></font>，请取消匿名！"");</script>")
      IF upfiles<>"" Then 
      Set FSO = server.CreateObject("Scripting.FileSystemObject")
	    IF FSO.FileExists(Server.mappath(sSavePath&upfiles)) Then
           FSO.DeleteFile server.MapPath(sSavePath&upfiles),True
	    End IF
      Set FSO = Nothing
	  End IF
	  Response.End()
   rs.close
   Set rs=nothing
   end if
   Call AskD.Score(Request.Cookies("Muser"),-clng(setting(23)),"匿名回答")
Else
  niming=0
End IF

IF not isnumeric(askid) then  Response.Write("<script>history.go(-1);</scr"&"ipt>")
Set rs=conn.execute("select * from "&predb&"ask where askid="&askid)
   askUser=rs("askuser")
   ansUser=rs("ansUser")&","
Set rs=nothing

IF Request.Cookies("Muser")=Cstr(askUser) Then
      IF upfiles<>"" Then 
      Set FSO = server.CreateObject("Scripting.FileSystemObject")
	    IF FSO.FileExists(Server.mappath(sSavePath&upfiles)) Then
           FSO.DeleteFile server.MapPath(sSavePath&upfiles),True
	    End IF
      Set FSO = Nothing
	  End IF
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""提问者不能回答自己的问题！"");</script>")
   Response.End()
End IF
IF instr(ansuser,","&Request.Cookies("Muser"))&",">0 Then
      IF upfiles<>"" Then 
      Set FSO = server.CreateObject("Scripting.FileSystemObject")
	    IF FSO.FileExists(Server.mappath(sSavePath&upfiles)) Then
           FSO.DeleteFile server.MapPath(sSavePath&upfiles),True
	    End IF
      Set FSO = Nothing
	  End IF
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""您已经回答过该问题！"");</script>")
   Response.End()
End IF

'入库
IF ansContent<>"" Then
    Dim rs,sql
	Set rs=Server.CreateObject("Adodb.recordset")
	sql="select top 1 * from "&predb&"Ans order by Ansid desc"		
	rs.open sql,Conn,1,3
		rs.AddNew
		rs("ansContent")=ansContent
		rs("AskID")=askid
		rs("AnsUser")=Request.Cookies("Muser")
		rs("IsPass")=1
		rs("AnsTime")=Now()
		if upfiles<>"" then
		rs("upfile")=pSavePath&upfiles
		end if
		rs("addurl")=addurl
		rs("niming")=niming
		rs.Update
	rs.Close
	Set rs=Nothing
	conn.execute("update "&predb&"ask set ansNum=ansNum+1,ansUser=ansUser+',"&Request.Cookies("Muser")&"' where askid="&askid)
	conn.execute("update "&predb&"user set ansNum=ansNum+1 where id="&Request.Cookies("Muser"))
	Call AskD.Score(Request.Cookies("Muser"),setting(14),"提交回答")
   
   IF setting(43)=0 Then
	   Dim From,FromName,Topic,MailBody,ToMail,SmtpName,SmtpPass,MailSmtp,GoStr
	   Set rs=conn.execute("select Usermail from "&predb&"user a inner join "&predb&"ask b on a.id=b.AskUser where a.isActivated=1 and b.askid="&askid)
	   IF Not(rs.bof and rs.eof) Then
	      IF Instr(rs("Usermail"),"@")>0 Then
				Topic=siteInfo(4)&" - "&Lan3
				MailBody=Lan4&" <a target='_blank' href='"&siteInfo(0)&siteInfo(1)&"ask.asp?id="&askid&"'>"&siteInfo(0)&siteInfo(1)&"ask.asp?id="&askid&"</a>"
				ToMail=rs("Usermail")
				MailSmtp=setting(39)
				SmtpName=setting(40)
				SmtpPass=setting(41)
				From=setting(42)
				FromName=siteInfo(4)
		   '发送邮件
		    Call AskD.SendMail(From,FromName,Topic,MailBody,ToMail,SmtpName,SmtpPass,MailSmtp)
			IF Isgo then 
			   GoStr="邮件发至提问者邮箱"
			Else
			   GoStr="给提问者的邮件失败"
			End IF
		  End IF
		End IF
		SET rs=Nothing
   End IF
   
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""恭喜你回答成功了！"&GoStr&""");setTimeout(""window.parent.location.reload()"",2000);</script>")
Else
   Response.Write("<script>var a = new parent.xWin(""1"",240,100,"""&siteInfo(4)&""",""请正确填写相应内容！"");</script>")
End If
end sub
%>