<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*,com.jspsmart.upload.*" errorPage="" %>
<%@ page import="java.sql.*"%> 
<%@ page import="java.util.Date"%>
<%@ page import="java.text.*"%>
<%//@ page import="com.jspsmart.upload.*" %>
<%@ include file="checkbbsuser.jsp"%>
<%@ include file="../inc/conn.jsp"%>
<%@ include file="../inc/ftbbstohtml.jsp"%>

<%//!int picsize;%>
<%!String remenu,sql,strsql,strDest="";%>
<%
    String content,bbs_img;
    ResultSet rs1,rs2,rs3,rs5;
	String IP=request.getRemoteAddr();
    Date currTime = new Date();
    SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss",Locale.US);
    String ftime=new String(formatter.format(currTime).getBytes("iso-8859-1"));
	
    SimpleDateFormat formatter2 = new SimpleDateFormat("yyyyMMddhhmmss",Locale.US);
    String ptime=new String(formatter2.format(currTime).getBytes("iso-8859-1"));
	
    sql="select * from config where kind='3' order by id asc";
    Statement stmt5=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs5=stmt5.executeQuery(sql);
	rs5.next();
	//if(rs5.next())
	//{
	int picsize = Integer.parseInt(rs5.getString("content"));
	//}
	//picsize=204800;

SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();

com.jspsmart.upload.File file = su.getFiles().getFile(0);
if (!file.isMissing()){
String myFileName=file.getFileName();//取得上载的文件的文件名
String  suffix=myFileName.substring(0,myFileName.lastIndexOf('.'));//取得不带后缀的文件名

int fileSize=file.getSize();//取得文件的大小

String ext= su.getFiles().getFile(0).getFileExt(); //取得后缀名
//String picname = ptime + "."+ext;
//picname = ptime + "."+ext;
if(fileSize>picsize)
{
   out.print("<script language='javascript'>");
   out.print("alert('你上传文件太大了！');");
   out.print("history.go(-1);");
   out.print("</script>");
   out.close();
   return;
}
if (ext.equals("gif")||ext.equals("GIF")||ext.equals("jpg")||ext.equals("JPG"))
{
}
else
{
   out.print("<script language='javascript'>");
   out.print("alert('只能上传gif,jpg文件类型！');");
   out.print("history.go(-1);");
   out.print("</script>");
   out.close();
   return;
}

	bbs_img = ptime+"."+ext;//file.getFileName();
	
file.saveAs("/upfile/"+ptime+"."+ext);//将文件保存在服务器端
}
else{
bbs_img = "";
}
    int pid = Integer.parseInt(su.getRequest().getParameter("id"));
    String BoardID_1 = su.getRequest().getParameter("BoardID_1");
    String BoardID_2 = su.getRequest().getParameter("BoardID_2");
	remenu = su.getRequest().getParameter("remenu");
	//String bbs_img = ptime+"."+ext;//file.getFileName();
	String bbs_img_text = su.getRequest().getParameter("bbs_img_text");
    String Sql_arrest="select * from BBS_arrest where (kind='Keyword' or kind='IP') order by id asc";
    Statement stmt3=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs3=stmt3.executeQuery(Sql_arrest);
	if(rs3.next()){
	   content=rs3.getString("content");
	   if(remenu.indexOf(content)!=-1||IP.indexOf(content)!=-1)
	   {
           out.print("<script language='javascript'>");
           out.print("alert('对不起，回复内容中不能含有"+rs3.getString("content")+"关键字或你的IP被禁止！');");
           out.print("history.go(-1);");
           out.print("</script>");
           out.close();
	   }
	 }	
	//加入引用内容
	String str2="[/quote]";
	if(remenu.indexOf(str2)!=-1){ 
	   int b=remenu.lastIndexOf("[/quote]");
	   int len=remenu.length();
	   String reid=remenu.substring(7,b);
	   String sqlid="select top 1 * from bbsmenu where id="+reid;
       Statement stmt2=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
       rs2=stmt2.executeQuery(sqlid);
	   if(rs2.next()){
	   String keyxx="<font color=#568AC2>回复: "+rs2.getString("user_id")+"</font>";
	   String keyLY="<table width=99% border=0 align=center cellpadding=4 cellspacing=1 bgcolor=#999999><tr><td bgcolor=#ffffee>"+ keyxx +"<br>"+ rs2.getString("remenu") +"</td></tr></table>";
	   String keyab="[quote]"+reid+"[/quote]";
	   int intFromLen = keyab.length(); 
	   int intPos; 
	   while((intPos=remenu.indexOf(keyab))!=-1){ 
	      strDest = remenu.substring(0,intPos); 
	      strDest = strDest + keyLY; 
	      remenu = remenu.substring(intPos+intFromLen); 
	      } 
	      strDest = strDest + remenu; 
		  remenu=strDest;
	   }   
     }
	//remenu = htmln(remenu);
    String str="select id,title,name,BoardID_1,BoardID_2,rep,htmfilename,P_display,postnum from bbstitle where id="+pid;
    Statement stmt1=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
    rs1=stmt1.executeQuery(str);
	if(rs1.next())
	{
	    if (rs1.getString("P_display").equals("0"))
	       {
           out.print("<script language='javascript'>");
           out.print("alert('该主题贴被禁止回贴！');");
           out.print("history.go(-1);");
           out.print("</script>");
           out.close();
		   }
	   }
    if(bbs_img==null||bbs_img.equals("")){
    sql="insert into bbsmenu(pid,BoardID_1,BoardID_2,user_id,rename,remenu,reip,date,kind,bbs_img_display,bbs_img_text,img_url_display,deleted)";
    sql=sql+" values('"+pid+"','"+BoardID_1+"','"+BoardID_2+"','"+(String)session.getAttribute("bbsuser")+"','"+(String)session.getAttribute("name")+"','"+remenu+"','"+IP+"','"+ftime+"','0','1','"+bbs_img_text+"','0','0')";
    }
	else{
	sql="insert into bbsmenu(pid,BoardID_1,BoardID_2,user_id,rename,remenu,bbs_img,reip,date,kind,bbs_img_display,bbs_img_text,img_url_display,deleted)";
    sql=sql+" values('"+pid+"','"+BoardID_1+"','"+BoardID_2+"','"+(String)session.getAttribute("bbsuser")+"','"+(String)session.getAttribute("name")+"','"+remenu+"','"+bbs_img+"','"+IP+"','"+ftime+"','0','1','"+bbs_img_text+"','0','0')";
	}
	stmt.executeUpdate(sql);
    strsql="update bbstitle set rep=rep+1,postnum=postnum+1,redate=getdate() where id="+pid;
    stmt.executeUpdate(strsql);
    strsql="update BBS_Cate set BBS_Cate_Reply=BBS_Cate_Reply+1 where BBS_Cate_ID='"+BoardID_1+"'";
    stmt.executeUpdate(strsql);
    if(BoardID_2!=null)
	{
	strsql="update BBS_Cate set BBS_Cate_Reply=BBS_Cate_Reply+1 where BBS_Cate_ID='"+BoardID_2+"'";
	stmt.executeUpdate(strsql);
     }
    if((String)session.getAttribute("bz")!="1"){
    strsql="update clubuser set jingyuan=jingyuan+5,tili=tili-10 where clubuser_name='"+(String)session.getAttribute("name")+"'";
    stmt.executeUpdate(strsql);
     }
	response.sendRedirect("checkasp.jsp?id="+pid);
%>