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

📄 msgmgr.java~73~

📁 云网论坛CWBBS 源码,内容丰富,学习,参考,教学的好资料,具体见内说明,
💻 JAVA~73~
📖 第 1 页 / 共 4 页
字号:
package com.redmoon.forum;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2002
 * Company:
 * @author
 * @version 1.0
 */
import java.io.IOException;
import java.util.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import cn.js.fan.util.*;
import com.redmoon.forum.plugin.*;
import com.redmoon.forum.plugin.base.*;
import com.redmoon.kit.util.FileUpload;
import org.apache.log4j.Logger;
import com.redmoon.forum.plugin2.Plugin2Mgr;
import com.redmoon.forum.plugin2.Plugin2Unit;
import cn.js.fan.web.SkinUtil;
import com.redmoon.kit.util.FileInfo;
import cn.js.fan.web.Global;
import java.io.File;
import com.redmoon.forum.message.MessageDb;
import com.redmoon.forum.person.UserPrivDb;

public class MsgMgr {
    // public: connection parameters
    boolean debug = true;
    Privilege privilege;
    Logger logger = Logger.getLogger(MsgMgr.class.getName());
    FileUpload fileUpload = null;

    public MsgMgr() {
        privilege = new Privilege();
    }

    public FileUpload doUpload(ServletContext application, HttpServletRequest request) throws
            ErrMsgException {
        Config cfg = new Config();
        int maxAttachmentSize = cfg.getIntProperty("forum.maxAttachmentSize");
        int maxAllFileSize = cfg.getIntProperty("forum.maxAllFileSize");

        MultiFileUpload fu = new MultiFileUpload();

        String userName = privilege.getUser(request);
        // 取得用户贴子最大所有上传文件尺寸
        UserPrivDb upd = new UserPrivDb();
        upd = upd.getUserPrivDb(userName);
        int maxAllSize = upd.getInt("attach_size");

        fu.setMaxAllFileSize(maxAllSize); // 最大1500K
        fu.setMaxFileSize(maxAttachmentSize);

        // String[] ext = {"jpg", "gif", "zip", "rar", "doc", "rm", "avi",
        //            "bmp",
        //            "swf", "png", "mp3", "wmv", "rmvb", "wma", "xls", "txt", "wps", "css", "htm", "html"};
        // String[] ext = cfg.getProperty("forum.ext").split(",");

        String[] ext = StrUtil.split(cfg.getProperty("forum.ext"), ",");

        if (ext!=null)
            fu.setValidExtname(ext);

        int ret = 0;
        try {
            ret = fu.doUpload(application, request);
            fileUpload = fu;
            if (ret!=fu.RET_SUCCESS) {
                throw new ErrMsgException(fu.getErrMessage(request));
            }
        } catch (IOException e) {
            logger.error("doUpload:" + e.getMessage());
            throw new ErrMsgException(e.getMessage());
        }

        String boardcode = StrUtil.getNullString(fu.getFieldValue("boardcode"));
        if (boardcode.equals(""))
            throw new ErrMsgException(LoadString(request, "err_need_board"));
        // 判断能否上传
        if (!privilege.canUserUpload(userName, boardcode)) {
            throw new ErrMsgException(SkinUtil.LoadString(request,
                    "res.forum.MsgMgr", "err_upload_count_exceed") +
                                      upd.getAttachTodayUploadCount());
        }
        // 水印处理
        boolean waterMarkImg = cfg.getBooleanProperty("forum.waterMarkImg");
        if (waterMarkImg) {
            Vector files = fu.getFiles();
            com.redmoon.forum.ImageUtil iu = new com.redmoon.forum.ImageUtil();
            iu.setRealPath(application.getRealPath("/"));
            if (files.size() > 0) {
                Iterator ir = files.iterator();
                while (ir.hasNext()) {
                    FileInfo fi = (FileInfo) ir.next();
                    if (fi.getExt().equalsIgnoreCase("jpg") ||
                        fi.getExt().equalsIgnoreCase("jpeg")) {
                        iu.WaterMark(fi);
                    }
                }
            }
        }
        return fu;
    }

    public String[] uploadImg(ServletContext application, HttpServletRequest request) throws
            ErrMsgException {
        if (!privilege.isUserLogin(request))
            throw new ErrMsgException(SkinUtil.LoadString(request, SkinUtil.ERR_NOT_LOGIN));

        UserPrivDb upd = new UserPrivDb();
        upd = upd.getUserPrivDb(privilege.getUser(request));
        if (upd.getBoolean("attach_upload") && upd.getAttachTodayUploadCount()<upd.getInt("attach_day_count"))
            ;
        else {
            throw new ErrMsgException(SkinUtil.LoadString(request, "res.forum.MsgMgr", "err_upload_count_exceed") + upd.getAttachTodayUploadCount());
        }

        Config cfg = new Config();
        int maxAttachmentSize = cfg.getIntProperty("forum.maxAttachmentSize");
        int maxAllFileSize = cfg.getIntProperty("forum.maxAllFileSize");

        // 取得用户贴子最大所有上传文件尺寸
        int maxAllSize = upd.getInt("attach_size");

        MultiFileUpload fu = new MultiFileUpload();
        fu.setMaxAllFileSize(maxAllSize); // 最大1500K
        fu.setMaxFileSize(maxAttachmentSize);

        String[] ext = new String[] {"jpg", "gif", "png", "bmp"};
        if (ext!=null)
            fu.setValidExtname(ext);

        int ret = 0;
        try {
            ret = fu.doUpload(application, request);
            fileUpload = fu;
            if (ret!=fu.RET_SUCCESS) {
                throw new ErrMsgException(fu.getErrMessage(request));
            }
            if (!cn.js.fan.security.Form.isTokenValid(request, fu))
                throw new ErrMsgException(SkinUtil.LoadString(request, "res.forum.MsgMgr", "err_back_refresh"));
            if (fu.getFiles().size()==0)
                throw new ErrMsgException(SkinUtil.LoadString(request, "res.forum.MsgMgr", "err_upload_none"));
        } catch (IOException e) {
            logger.error("doUpload:" + e.getMessage());
            throw new ErrMsgException(e.getMessage());
        }

        // 水印处理
        boolean waterMarkImg = cfg.getBooleanProperty("forum.waterMarkImg");
        if (waterMarkImg) {
            Vector files = fu.getFiles();
            com.redmoon.forum.ImageUtil iu = new com.redmoon.forum.ImageUtil();
            iu.setRealPath(application.getRealPath("/"));
            if (files.size() > 0) {
                Iterator ir = files.iterator();
                if (ir.hasNext()) {
                    FileInfo fi = (FileInfo) ir.next();
                    if (fi.getExt().equalsIgnoreCase("jpg") ||
                        fi.getExt().equalsIgnoreCase("jpeg")) {
                        iu.WaterMark(fi);
                    }
                }
            }
        }

        Calendar cal = Calendar.getInstance();
        String year = "" + (cal.get(cal.YEAR));
        String month = "" + (cal.get(cal.MONTH) + 1);
        String filepath = Global.getRealPath() + "forum/upfile/" +
                          year + "/" + month +
                          "/";
        File f = new File(filepath);
        if (!f.isDirectory()) {
            f.mkdirs();
        }

        String virtualpath = "upfile/" + year +
                             "/" +
                             month;
        fu.setSavePath(filepath); // 设置保存的目录
        // logger.info(filepath);
        String[] re = null;
        Iterator ir = fu.getFiles().iterator();
        int orders = 0;

        if (ir.hasNext()) {
            FileInfo fi = (FileInfo) ir.next();
            // 保存至磁盘相应路径
            String fname = FileUpload.getRandName() + "." +
                             fi.getExt();
            fi.write(fu.getSavePath(), fname);
            // 记录于数据库
            Attachment att = new Attachment();
            att.setDiskName(fi.getDiskName());
            String fpath = fu.getSavePath() + fi.getDiskName();
            // logger.info(fpath);
            att.setFullPath(fpath);
            att.setMsgId(att.TEMP_MSG_ID);
            att.setName(fi.getName());
            att.setDiskName(fi.getDiskName());
            att.setOrders(orders);
            att.setVisualPath(virtualpath);
            att.setUploadDate(new java.util.Date());
            att.setSize(fi.getSize());
            att.setUserName(privilege.getUser(request));
            if (att.create()) {
                re = new String[2];
                re[0] = "" + att.getId();
                re[1] = att.getVisualPath() + "/" + att.getDiskName();

                upd.addAttachTodayUploadCount(1);
            }
        }

        return re;
    }

    /**
     * 以webedit的方式添加贴子
     * @param application ServletContext
     * @param request HttpServletRequest
     * @return boolean
     * @throws ErrMsgException
     */
    public boolean AddNewWE(ServletContext application,
                            HttpServletRequest request) throws
            ErrMsgException {
        MultiFileUpload fu = (MultiFileUpload)doUpload(application, request);

        String boardcode;
        boardcode = fu.getFieldValue("boardcode");
        if (boardcode == null || boardcode.trim().equals(""))
            throw new ErrMsgException(LoadString(request, "err_need_board"));
        if (!privilege.canAddNew(request, boardcode, fu))
            throw new ErrMsgException(SkinUtil.LoadString(request, SkinUtil.PVG_INVALID));

        // 检查版块是否存在
        Leaf lf = new Leaf();
        lf = lf.getLeaf(boardcode);
        if (lf==null || !lf.isLoaded())
            throw new ErrMsgException(LoadString(request, "err_board_lost")); // "版块 " + boardcode + " 不存在!");

        // 插件的权限检查
        String pluginCode = StrUtil.getNullString(fu.getFieldValue("pluginCode"));
        boolean isPluginValid = false;
        PluginMgr pm = new PluginMgr();
        Vector vplugin = pm.getAllPluginUnitOfBoard(boardcode);
        if (vplugin.size() > 0) {
            Iterator irplugin = vplugin.iterator();
            while (irplugin.hasNext()) {
                PluginUnit pu = (PluginUnit) irplugin.next();
                IPluginPrivilege ipp = pu.getPrivilege();
                // logger.info("plugin name:" + pu.getName(request));
                if (!ipp.canAddNew(request, boardcode)) {
                    String str = LoadString(request, "err_pvg_plugin");
                    str = str.replaceFirst("\\$p", pu.getName(request));
                    throw new ErrMsgException(str);
                }
                // 检查指定的pluginCode是否被允许
                if (!pluginCode.equals(""))
                    if (pu.getCode().equals(pluginCode))
                        isPluginValid = true;
            }
        }

        // 如果指定的pluginCode不被允许,则报错
        if (!pluginCode.equals("") && !isPluginValid) {
            throw new ErrMsgException(LoadString(request, "err_plugin_invalid"));
        }

        String name;
        name = privilege.getUser(request); // cookiebean.getCookieValue(request, "name");
        MsgDb md = new MsgDb();
        boolean re = false;
        try {
            re = md.AddNewWE(application, request, name, fu);
        } catch (ErrMsgException e) {
            throw e;
        }

        if (re) {
            // 如果有plugin2Code传递过来
            String plugin2Code = StrUtil.getNullString(fu.getFieldValue("plugin2Code")).trim();
            if (!plugin2Code.equals("")) {
                Plugin2Mgr p2m = new Plugin2Mgr();
                Plugin2Unit p2u = p2m.getPlugin2Unit(plugin2Code);
                if (p2u!=null) {
                    try {
                        p2u.getMsgAction().AddNew(application, request, md, fu);
                    }
                    catch (ErrMsgException e) {
                        // 删除新发的贴子,但是贴子作者的相关经验值则不受影响
                        try {
                            md.delTopic(md.getId(), true);
                        }
                        catch (ResKeyException e1) {
                            logger.error("AddNewWE:" + e1.getMessage(request));
                        }
                        throw e;
                    }
                }
            }
        }

        if (re) {
            // 插件对应加入新贴的action
            if (vplugin.size() > 0) {
                Iterator irplugin = vplugin.iterator();
                while (irplugin.hasNext()) {
                    PluginUnit pu = (PluginUnit) irplugin.next();
                    boolean isPlugin = false;

⌨️ 快捷键说明

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