⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 topicdao.java

📁 EasyJForum 是一个基于 Java 技术的免费社区论坛软件系统
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
package com.hongshee.ejforum.data;

/**
 * <p>Title: TopicDAO.java</p>
 * <p>Description: Forum topic data access object</p>
 * <p>Copyright: Hongshee Software (c) 2007</p>
 * @author jackie du
 * @version 1.0
 */

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.fileupload.FileUploadBase.SizeLimitExceededException;

import com.hongshee.ejforum.util.PageUtils;
import com.hongshee.ejforum.util.AppUtils;
import com.hongshee.ejforum.util.MyFileUpload;
import com.hongshee.ejforum.util.MyFileUpload.UploadVO;
import com.hongshee.ejforum.data.UserDAO.UserInfo;
import com.hongshee.ejforum.data.SectionDAO.SectionVO;
import com.hongshee.ejforum.data.AttachDAO.AttachVO;
import com.hongshee.ejforum.data.BoardDAO.BoardVO;
import com.hongshee.ejforum.data.GroupDAO.GroupVO;
import com.hongshee.ejforum.data.ReplyDAO.PostVO;
import com.hongshee.ejforum.common.ForumSetting;
import com.hongshee.ejforum.common.CacheManager;
import com.hongshee.ejforum.common.IConstants;

public class TopicDAO extends EntityDAO
{
    private static TopicDAO _dao = null;

    private Pattern script_pattern = 
        Pattern.compile("<script[\\u0000-\\uffff]+?</script>", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    
    private Pattern iframe_pattern = 
        Pattern.compile("<i?frame[^>]+>", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    
    private Pattern object_pattern = 
        Pattern.compile("<object[\\u0000-\\uffff]+?</object>", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    
    private Pattern media_pattern = 
        Pattern.compile("\\[media=\"([^\"]+?)\"\\]([\\u0000-\\uffff]*?)\\[/media\\]", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    private Pattern image_pattern = 
        Pattern.compile("\\[img=\"([^\"]+?)\"\\]([\\u0000-\\uffff]*?)\\[/img\\]", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    private Pattern href_pattern = 
        Pattern.compile("href=[\"']?([^\"'\\s>]+?)[\"'\\s>]", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    
    private Pattern media2bb_pattern = 
        Pattern.compile("<script[^>]*>\\s*showMedia\\('([^']*)'," +
                        "'([^']*)','([^']*)'\\);\\s*</script>", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);

    private Pattern script2defer_pattern = 
        Pattern.compile("<script\\s([^>]*)>", 
                Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
    
    protected TopicDAO()
    {}

    public static TopicDAO getInstance()
    {
        if (_dao == null)
        {
            _dao = new TopicDAO();
        }
        return _dao;
    } 

    public String createTopic(HttpServletRequest request, UserInfo userinfo,
                              SectionVO aSection, BoardVO aBoard,
                              GroupVO aGroup) throws Exception
    {
        String result = null;
        ArrayList<UploadVO> attaches = null;
        try
        {
            attaches = MyFileUpload.getInstance().upload(request);
        }
        catch(SizeLimitExceededException e)
        {
            return "上传附件时出错:文件大小超过限制,允许的最大值为:" 
                    + ((SizeLimitExceededException)e).getPermittedSize() + " 字节。";
        }
        catch(Exception e)
        {
            return "上传附件时出错:" + e.getMessage();
        }
        
        String subject = PageUtils.decodeParam((String)request.getAttribute("subject"));
        subject = subject.replace("<", "&lt;");
        subject = subject.replace(">", "&gt;");

        StringBuilder badwords = new StringBuilder();
        subject = this.censorWords(subject, badwords);
        
        int attachCount = 0;
        if (attaches != null)
            attachCount = attaches.size();
        if (attachCount > 0 && 
                !PageUtils.isPermitted(aBoard,aGroup,IConstants.PERMIT_UPLOAD))
        {
            return "您没有上传附件的权限";
        }
        
        String content = validateContent(request, aGroup, aSection, aBoard,
                                         attaches, badwords); 
        
        Connection conn = dbManager.getConnection();
        try
        {
            conn.setAutoCommit(false);

            String attachIcon = null;
            if (attaches != null)
                attachIcon = this.getAttachIcon(attaches);
            if (attachIcon == null)
                attachIcon = "";
            
            if (attachIcon.indexOf('I') < 0)
            {            
                Object picAttr = request.getAttribute("pic");
                if (picAttr != null && picAttr.toString().equals("1"))
                    attachIcon = attachIcon + 'I';
            }
            if (attachIcon.indexOf('F') < 0)
            {            
                Object flashAttr = request.getAttribute("flv");
                if (flashAttr != null && flashAttr.toString().equals("1"))
                    attachIcon = attachIcon + 'F';
            }
            
            String userID = (userinfo == null ? "" : userinfo.userID);
            
            int topicID = 0;
            if (this.adapterName.indexOf("HsqldbAdapter") > 0
                    || this.adapterName.indexOf("OracleAdapter") > 0)
            {
                synchronized(this)
                {
                    topicID = addTopic(request, userinfo, subject, content, attachCount, attachIcon, conn, false);
                }
            }
            else
                topicID = addTopic(request, userinfo, subject, content, attachCount, attachIcon, conn, true);
            
            // Increase posts
            ArrayList<Object> paramValues = new ArrayList<Object>();
            paramValues.add(userID);
            this.execUpdateSql(adapter.User_IncPosts, paramValues, conn);

            if (userinfo != null)
                userinfo.posts = userinfo.posts + 1;
            
            HttpSession sess = request.getSession();
            Object sessionPosts = (Object)sess.getAttribute("posts");
            if (sessionPosts != null)
                sess.setAttribute("posts", Integer.parseInt(sessionPosts.toString()) + 1);
            else
                sess.setAttribute("posts", 1);
            
            // Increase credits
            int totalCredits = ForumSetting.getInstance().getInt(ForumSetting.CREDITS, "newTopic");
            
            if (attachCount > 0 && topicID > 0)
            {
                AttachDAO.getInstance()
                         .addAttaches(attaches, userID, String.valueOf(topicID), "0", conn);

                int credits = ForumSetting.getInstance().getInt(ForumSetting.CREDITS, "upload");
                if (credits > 0)
                    totalCredits = totalCredits + credits * attachCount;
            }
            
            if (totalCredits > 0 && userID.length() > 0)
            {
                paramValues.clear();
                paramValues.add(totalCredits);
                paramValues.add(userID);
                this.execUpdateSql(adapter.User_IncCredits, paramValues, conn);

                if (userinfo != null)
                    userinfo.credits = userinfo.credits + totalCredits;
            }
            
            if (badwords.length() > 1)
            {
                ActionLogDAO.getInstance().addCensorLog(
                        userID, aBoard.boardID, aBoard.boardName, String.valueOf(topicID), 
                        subject, "0", badwords.toString(), conn);
            }
            
            conn.commit();
            result = "OK";
        }
        catch(Exception e)
        {
            conn.rollback();
            throw e;
        }
        finally
        {
            dbManager.closeConnection(conn);
        }
        return result;
    }
    
    private String getAttachIcon(ArrayList<UploadVO> attaches)
    {
        boolean hasImage = false;
        boolean hasFlash = false;
        boolean hasAttach = false;

        for (int i=0; i<attaches.size(); i++)
        {
            if (attaches.get(i).state == 'I')
                hasImage = true;
            else if (attaches.get(i).state == 'F')
                hasFlash = true;
            else if (attaches.get(i).state == 'N')
                hasAttach = true;
        }
        String result = "";
        if (hasAttach)
            result = result + "A";
        else if (hasImage)
            result = result + "I";
        else if (hasFlash)
            result = result + "F";
        return result;
    }
    
    /**
     * Add a forum topic
     * @param 
     *      request - HttpServletRequest
     *      userID - User ID
     *      attachCount - Attachment count
     *      conn - DB Connection
     * @return Topic ID
     * @throws SQLException
     * @since 1.0
     */
    private int addTopic(HttpServletRequest request, UserInfo userinfo, 
                         String subject, String content,
                         int attachCount, String attachIcon, Connection conn, 
                         boolean hasGeneratedKey) throws SQLException
    {
        int result = 0;
        PreparedStatement pstmtInsert = null;
        Statement stmtQuery = null;
        ResultSet rs = null;
        try
        {
            int iReward = 0;
            String reward = PageUtils.decodeParam((String)request.getAttribute("reward"));
            if (reward.length() > 0)
            {
                try
                {
                    iReward = Integer.parseInt(reward);
                }
                catch(Exception e){ /* Ignored */ }
            }

            String isHidePost = (String)request.getAttribute("isHidePost");
            if (isHidePost == null || isHidePost.length() == 0)
                isHidePost = "F";

            String isTopPost = (String)request.getAttribute("isTopPost");
            if (isTopPost == null || isTopPost.length() == 0)
                isTopPost = "F";

            String isReplyNotice = (String)request.getAttribute("isReplyNotice");
            if (isReplyNotice == null || isReplyNotice.length() == 0)
                isReplyNotice = "F";
            
            String sectionID = PageUtils.getParam(request, "sid");
            String boardID = PageUtils.getParam(request, "fid");

            String userID = (userinfo == null ? "" : userinfo.userID);
            String nickname = (userinfo == null ? "" : userinfo.nickname);
            
            if (hasGeneratedKey)
                pstmtInsert = conn.prepareStatement(adapter.Topic_Insert, Statement.RETURN_GENERATED_KEYS);
            else
                pstmtInsert = conn.prepareStatement(adapter.Topic_Insert);
                
            pstmtInsert.setString(1, sectionID);
            pstmtInsert.setString(2, boardID);
            pstmtInsert.setString(3, userID);
            pstmtInsert.setString(4, nickname);
            pstmtInsert.setString(5, request.getRemoteAddr());
            pstmtInsert.setString(6, subject);
            pstmtInsert.setString(7, content);
            pstmtInsert.setInt(8, iReward);
            pstmtInsert.setString(9, isReplyNotice);
            pstmtInsert.setString(10, isHidePost);
            pstmtInsert.setInt(11, attachCount);
            pstmtInsert.setString(12, attachIcon);

            if (isHidePost.charAt(0) == 'T')
                pstmtInsert.setString(13, "");
            else
                pstmtInsert.setString(13, userID);

            if (isTopPost.charAt(0) == 'T')
                pstmtInsert.setString(14, "3");
            else
                pstmtInsert.setString(14, "N");
            
            pstmtInsert.executeUpdate();
            
            if (hasGeneratedKey)
            {
                rs = pstmtInsert.getGeneratedKeys();
            }
            else if (this.adapterName.indexOf("OracleAdapter") > 0)
            {
                stmtQuery = conn.createStatement();
                rs = stmtQuery.executeQuery("select ejf_topic_seq.CURRVAL from dual");
            }
            else // Hsqldb
            {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -