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

📄 postwebhandler.java

📁 java servlet著名论坛源代码
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
/*
 * $Header: /cvsroot/mvnforum/mvnforum/src/com/mvnforum/user/PostWebHandler.java,v 1.25 2004/05/19 19:11:59 minhnn Exp $
 * $Author: minhnn $
 * $Revision: 1.25 $
 * $Date: 2004/05/19 19:11:59 $
 *
 * ====================================================================
 *
 * Copyright (C) 2002-2004 by MyVietnam.net
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * All copyright notices regarding mvnForum MUST remain intact
 * in the scripts and in the outputted HTML.
 * The "powered by" text/logo with a link back to
 * http://www.mvnForum.com and http://www.MyVietnam.net in the
 * footer of the pages MUST remain visible when the pages
 * are viewed on the internet or intranet.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Support can be obtained from support forums at:
 * http://www.mvnForum.com/mvnforum/index
 *
 * Correspondence and Marketing Questions can be sent to:
 * info@MyVietnam.net
 *
 * @author: Minh Nguyen  minhnn@MyVietnam.net
 * @author: Mai  Nguyen  mai.nh@MyVietnam.net
 */
package com.mvnforum.user;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.*;

import javax.servlet.http.HttpServletRequest;

import com.mvnforum.*;
import com.mvnforum.auth.*;
import com.mvnforum.common.StatisticsUtil;
import com.mvnforum.db.*;
import com.mvnforum.search.*;
import net.myvietnam.mvncore.exception.*;
import net.myvietnam.mvncore.filter.DisableHtmlTagFilter;
import net.myvietnam.mvncore.interceptor.InterceptorService;
import net.myvietnam.mvncore.security.Encoder;
import net.myvietnam.mvncore.security.FloodControl;
import net.myvietnam.mvncore.util.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

class PostWebHandler {

    private static Log log = LogFactory.getLog(PostWebHandler.class);

    private OnlineUserManager onlineUserManager = OnlineUserManager.getInstance();

    PostWebHandler() {
    }

    /**
     * This method is for addpost page
     */
    void prepareAdd(HttpServletRequest request)
        throws ObjectNotFoundException, DatabaseException, BadInputException, AuthenticationException, AssertionException {

        if (MVNForumConfig.getEnableNewPost() == false) {
            throw new AssertionException("Cannot create new post because NEW_POST feature is disabled by administrator.");
        }
        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();

        if (MVNForumConfig.isGuestUserInDatabase() == false) {
            permission.ensureIsAuthenticated();
        }

        // we set this action attribute first because the return below can make method return prematurely
        request.setAttribute("action", "addnew");

        int parentPostID    = 0;
        try {
            // neu co parent thi` khong co forum !!!
            parentPostID = ParamUtil.getParameterInt(request, "parent");
        } catch (Exception ex) {
            // do nothing
            // NOTE: we cannot return here since user can have a parameter parent = 0
        }

        if (parentPostID == 0) {// new thread
            int forumID = ParamUtil.getParameterInt(request, "forum");

            ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
            forumBean.ensureNotDisabledForum();
            forumBean.ensureNotClosedForum();
            forumBean.ensureNotLockedForum();

            permission.ensureCanAddThread(forumID);
        } else {// reply to a post
            // this is a parent post
            PostBean postBean = DAOFactory.getPostDAO().getPost(parentPostID);// can throw DatabaseException

            // check permission
            int forumID  = postBean.getForumID();

            ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
            forumBean.ensureNotDisabledForum();
            forumBean.ensureNotClosedForum();
            forumBean.ensureNotLockedForum();

            permission.ensureCanAddPost(forumID);

            // now we prepare to list lastest post in the thread
            int threadID = postBean.getThreadID();

            // now check if thread is closed or locked, if it is, then cannot reply to a post
            ThreadBean threadBean = DAOFactory.getThreadDAO().getBean(threadID);

            threadBean.ensureStatusCanReply();

            Collection postBeans = DAOFactory.getPostDAO().getLastEnablePosts_inThread_limit(threadID, MVNForumConfig.ROWS_IN_LAST_REPLIES);
            request.setAttribute("ParentPostBean", postBean);
            request.setAttribute("PostBeans", postBeans);
        }

        boolean isPreviewing = ParamUtil.getParameterBoolean(request, "preview");
        if (isPreviewing) {
            // Check if user enter some text or not
            ParamUtil.getParameter(request, "PostTopic", true);
            ParamUtil.getParameter(request, "message", true);// use message instead of MessageBody

            MemberBean memberBean = DAOFactory.getMemberDAO().getMember_forPublic(onlineUser.getMemberID());
            request.setAttribute("MemberBean", memberBean);
        }
    }

    void processAdd(HttpServletRequest request)
        throws ObjectNotFoundException, AssertionException, DatabaseException, CreateException,
        BadInputException, ForeignKeyNotFoundException, AuthenticationException, FloodException, InterceptorException {

        if (MVNForumConfig.getEnableNewPost() == false) {
            throw new AssertionException("Cannot create new post because NEW_POST feature is disabled by administrator.");
        }

        String currentIP = request.getRemoteAddr();
        FloodControl.ensureNotReachMaximum(MVNForumGlobal.FLOOD_ID_NEW_POST, currentIP);

        OnlineUser onlineUser = onlineUserManager.getOnlineUser(request);
        MVNForumPermission permission = onlineUser.getPermission();

        int memberID        = onlineUser.getMemberID();
        String memberName   = onlineUser.getMemberName();

        Timestamp now       = DateUtil.getCurrentGMTTimestamp();

        int parentPostID    = ParamUtil.getParameterInt(request, "parent");
        boolean attachMore  = ParamUtil.getParameterBoolean(request, "AttachMore");
        boolean addFavoriteThread = ParamUtil.getParameterBoolean(request, "AddFavoriteParentThread");
        boolean addWatchThread    = ParamUtil.getParameterBoolean(request, "AddWatchParentThread");

        String postTopic = ParamUtil.getParameter(request, "PostTopic", true);
        postTopic = DisableHtmlTagFilter.filter(postTopic);// always disable HTML
        InterceptorService.getInstance().validateContent(postTopic);

        String postBody  = ParamUtil.getParameter(request, "message", true);// use message instead of MessageBody
        postBody = DisableHtmlTagFilter.filter(postBody);// always disable HTML
        InterceptorService.getInstance().validateContent(postBody);

        String postIcon = ParamUtil.getParameter(request, "PostIcon");
        postIcon = DisableHtmlTagFilter.filter(postIcon);// always disable HTML

        int forumID = 0;
        int threadID= 0;
        boolean isPendingThread = false;
        if (parentPostID == 0) {// new thread
            forumID = ParamUtil.getParameterInt(request, "forum");

            ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
            forumBean.ensureNotDisabledForum();
            forumBean.ensureNotClosedForum();
            forumBean.ensureNotLockedForum();

            // check permission
            permission.ensureCanAddThread(forumID);

            String lastPostMemberName = memberName;
            int threadType            = 0;//@todo review and support it later
            int threadOption          = 0;//@todo review and support it later
            int threadStatus          = ThreadBean.THREAD_STATUS_DEFAULT;
            if (forumBean.shouldModerateThread()) {
                threadStatus          = ThreadBean.THREAD_STATUS_DISABLED;
                isPendingThread = true;
            }
            int threadHasPoll           = 0;//@todo review and support it later
            int threadDuration          = 0;//@todo review and support it later
            threadID = DAOFactory.getThreadDAO().createThread(forumID, memberName, lastPostMemberName,
                                   postTopic, postBody, 0/*threadVoteCount*/,
                                   0/*threadVoteTotalStars*/, now/*threadCreationDate*/, now/*threadLastPostDate*/,
                                   threadType, threadOption, threadStatus,
                                   threadHasPoll, 0/*threadViewCount*/, 0/*threadReplyCount*/,
                                   postIcon, threadDuration);
        } else {// reply to a post
            PostBean parentPostBean = DAOFactory.getPostDAO().getPost(parentPostID);
            forumID = parentPostBean.getForumID();
            threadID = parentPostBean.getThreadID();

            ForumBean forumBean = ForumCache.getInstance().getBean(forumID);
            forumBean.ensureNotDisabledForum();
            forumBean.ensureNotClosedForum();
            forumBean.ensureNotLockedForum();

            // check permission
            permission.ensureCanAddPost(forumID);

            // now check if thread is closed or locked, if it is, then cannot reply to a post
            ThreadBean threadBean = DAOFactory.getThreadDAO().getBean(threadID);
            threadBean.ensureStatusCanReply();
        }

        //Timestamp postLastEditDate = now;
        String postCreationIP       = currentIP;
        String postLastEditIP       = "";// should we init it to postCreationIP ???
        int postFormatOption        = 0;
        int postOption              = 0;
        int postStatus              = 0;
        if (ForumCache.getInstance().getBean(forumID).shouldModeratePost()) {
            // we will not disble post that is a thread (parentPostID == 0)
            if (parentPostID != 0) {// replied post
                postStatus              = PostBean.POST_STATUS_DISABLED;
            }
        }
        int postAttachCount         = 0;

        int postID = DAOFactory.getPostDAO().createPost(parentPostID, forumID, threadID,
                             memberID, memberName, ""/*lastEditMemberName*/,
                             postTopic, postBody, now/*postCreationDate*/,
                             now/*postLastEditDate*/, postCreationIP, postLastEditIP,
                             0/*postEditCount*/, postFormatOption, postOption,
                             postStatus, postIcon, postAttachCount);

        StatisticsUtil.updateMemberStatistics(memberID);

        StatisticsUtil.updateForumStatistics(forumID);
        StatisticsUtil.updateThreadStatistics(threadID);

        /** @todo Update PostEditLog table here */

        //add favorite thread if user checked it
        if (addFavoriteThread) {
            permission.ensureIsAuthenticated();
            //@todo: add checking of MVNForumConfig.getEnableFavoriteThread()
            // check to make sure that this user doesnt exceed his favorite max
            int currentFavoriteCount = DAOFactory.getFavoriteThreadDAO().getNumberOfBeans_inMember(memberID);
            int maxFavorites = MVNForumConfig.getMaxFavoriteThread();
            if (currentFavoriteCount < maxFavorites) {
                Timestamp favoriteCreationDate = now;
                int favoriteType = 0; //@todo implement it later
                int favoriteOption = 0; //@todo implement it later
                int favoriteStatus = 0; //@todo implement it later

                // now check permission the this user have the readPost permission
                permission.ensureCanReadPost(forumID);

⌨️ 快捷键说明

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