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

📄 reg.java

📁 一个jsp写的bbs
💻 JAVA
字号:
package com.laoer.bbscs.web.action;

import java.util.*;
import javax.servlet.http.*;

import org.apache.commons.lang.*;
import org.apache.commons.mail.*;
import org.apache.struts.action.*;
import org.apache.struts.util.*;
import com.laoer.bbscs.bean.*;
import com.laoer.bbscs.comm.*;
import com.laoer.bbscs.exception.*;
import com.laoer.bbscs.service.*;
import com.laoer.bbscs.service.config.*;
import com.laoer.bbscs.web.form.*;
import com.laoer.comm.util.*;
import freemarker.template.*;
import java.io.StringWriter;
import java.io.File;
import java.io.*;

public class Reg
    extends Action {

  private UserService userService;

  private SysConfig sysConfig;

  private IPSeeker ipSeeker;

  private SysStatService sysStatService;

  private HtmlEmail htmlEmail;

  private Configuration tempConfiguration;

  public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                               HttpServletResponse response) {
    RegForm regForm = (RegForm) form;

    ActionMessages errors = new ActionMessages();

    if (this.getSysConfig().getUsePass() == 1) {
      return new ActionForward(this.getSysConfig().getPassRegUrl(), true);
    }

    if (this.getSysConfig().getOpenUserReg() == 0) { //关闭注册
      errors.add("error.reg.notallowreg", new ActionMessage("error.reg.notallowreg"));
      this.saveErrors(request, errors);
      return mapping.findForward("error");
    }

    if (regForm.getAction().equalsIgnoreCase("new")) { //进入注册页面
      regForm.setAction("add");
      return mapping.findForward("reg");
    }

    /*
         if (regForm.getAction().equalsIgnoreCase("check")) {
      if (StringUtils.isBlank(regForm.getUserName())) {
        request.setAttribute("message", "请填写用户名!");
      }

      UserInfo userInfo = this.getUserService().findUserInfoByUserName(regForm.getUserName());
      if (userInfo != null) {
        request.setAttribute("message", "用户名已存在,请选择其他用户名!");
      }
      else {
        request.setAttribute("message", "该用户名不存在,可以注册!");
      }
      return mapping.findForward("ajaxHtml");
         }*/

    if (regForm.getAction().equalsIgnoreCase("add")) {

      if (this.getSysConfig().isCanNotRegUserName(regForm.getUserName())) { //不能注册的用户名
        errors.add("error.reg.badusername",
                   new ActionMessage("error.reg.badusername", regForm.getUserName()));
        this.saveErrors(request, errors);
        return mapping.getInputForward();
      }

      if (this.getSysConfig().getUseForbid() == 1) {
        if (this.getSysConfig().isForbidIP(request.getRemoteAddr())) {
          errors.add("error.reg.ipforbid",
                     new ActionMessage("error.reg.ipforbid", request.getRemoteAddr()));
          this.saveErrors(request, errors);
          return mapping.getInputForward();
        }

        if (this.getSysConfig().isForbidEmail(regForm.getEmail())) {
          errors.add("error.reg.emailforbid",
                     new ActionMessage("error.reg.emailforbid", regForm.getEmail()));
          this.saveErrors(request, errors);
          return mapping.getInputForward();
        }
      }

      UserInfo ui = this.getUserService().findUserInfoByUserName(regForm.getUserName());

      if (ui != null) { //检查用户名
        errors.add("error.reg.name1", new ActionMessage("error.reg.name1"));
        this.saveErrors(request, errors);
        return mapping.getInputForward();
      }

      ui = this.getUserService().findUserInfoByEmail(regForm.getEmail());
      if (ui != null) { //检查Email
        errors.add("error.reg.emailerror", new ActionMessage("error.reg.emailerror"));
        this.saveErrors(request, errors);
        return mapping.getInputForward();
      }

      MessageResources mr = this.getResources(request);
      Locale locale = this.getLocale(request);

      ui = new UserInfo();
      ui.setAcceptFriend(1);
      ui.setAnswer(regForm.getAnswer());
      ui.setArticleEliteNum(0);
      ui.setArticleNum(0);
      ui.setBirthDay(1);
      ui.setBirthMonth(1);
      ui.setBirthYear(1980);
      ui.setEmail(regForm.getEmail());
      ui.setExperience(0);
      ui.setForumPerNum(0);
      ui.setForumViewMode(0);
      ui.setHavePic(0);
      ui.setLastLoginIP("0.0.0.0");
      ui.setLastLoginTime(new Date());
      ui.setLifeForce(0);
      ui.setLiterary(0);
      ui.setLoginIP("0.0.0.0");
      ui.setLoginTime(new Date());
      ui.setLoginTimes(0);
      ui.setNickName(this.getSysConfig().bestrowScreenNickName(regForm.getNickName())); //屏蔽敏感字
      ui.setPasswd(regForm.getPasswd());
      ui.setPicFileName("");
      ui.setPostPerNum(0);
      ui.setQuestion(regForm.getQuestion());
      ui.setReceiveNote(1);
      ui.setRegTime(new Date());
      ui.setRePasswd(Util.hash(regForm.getPasswd()));
      ui.setSignDetail0(mr.getMessage(locale, "bbscs.userdefaultsign"));
      ui.setSignDetail1(mr.getMessage(locale, "bbscs.userdefaultsign"));
      ui.setSignDetail2(mr.getMessage(locale, "bbscs.userdefaultsign"));
      ui.setSignName0("A");
      ui.setSignName1("B");
      ui.setSignName2("C");
      ui.setStayTime(0);
      ui.setTimeZone("GMT+08:00");
      ui.setUserFrom(this.getIpSeeker().getCountry(request.getRemoteAddr()));
      ui.setUserKnow(0);
      ui.setUserName(regForm.getUserName());
      ui.setUserTitle(0);
      if (this.getSysConfig().isCheckRegUser()) {
        ui.setValidated(0);
        ui.setGroupID(Constant.USER_GROUP_UNVUSER);
      }
      else {
        ui.setValidated(1);
        ui.setGroupID(Constant.USER_GROUP_REGUSER);
      }
      ui.setEditType( -1);
      ui.setUserLocale(request.getLocale().toString());
      ui.setValidateCode(RandomStringUtils.randomAlphanumeric(10));
      ui.setCoin(100);

      UserDetail ud = new UserDetail();
      ud.setBrief("");
      ud.setDreamJob("");
      ud.setDreamLover("");
      ud.setFavourArt("");
      ud.setFavourBook("");
      ud.setFavourChat("");
      ud.setFavourMovie("");
      ud.setFavourMusic("");
      ud.setFavourPeople("");
      ud.setFavourTeam("");
      ud.setGraduate("");
      ud.setHeight("");
      ud.setHomePage("");
      ud.setIcqNo("");
      ud.setInterest("");
      ud.setMsn("");
      ud.setOicqNo("");
      ud.setSex( (short) 0);
      ud.setWeight("");
      ud.setYahoo("");

      ui.setUserDetail(ud);
      ud.setUserInfo(ui);

      try {
        //this.getUserService().createUserInfo(ui, ud);
        ui = this.getUserService().saveUserInfo(ui);
        this.getUserService().writeUserFile(ui);
        this.getSysStatService().saveAllUserNum(this.getUserService().getAllUserNum(), regForm.getUserName());
        if (this.getSysConfig().isCheckRegUser()) {
          if (this.getSysConfig().getSmtpAuth() == 1) {
            DefaultAuthenticator defaultAuthenticator = new DefaultAuthenticator(this.getSysConfig().
                getSmtpUser(), this.getSysConfig().getSmtpPasswd());
            this.getHtmlEmail().setAuthenticator(defaultAuthenticator);
          }
          try {
            this.getHtmlEmail().setHostName(this.getSysConfig().getSmtpServer());
            this.getHtmlEmail().setSmtpPort(this.getSysConfig().getSmtpPort());
            this.getHtmlEmail().setFrom(this.getSysConfig().getSenderEmail());
            this.getHtmlEmail().setSubject(mr.getMessage(locale, "reg.validate.email.title",
                this.getSysConfig().getForumName()));
            this.getTempConfiguration().setDirectoryForTemplateLoading(new File(Constant.ROOTPATH +
                Constant.FTL_PATH));
            this.getTempConfiguration().setDefaultEncoding(Constant.CHARSET);
            this.getTempConfiguration().setLocale(request.getLocale());
            this.getTempConfiguration().setNumberFormat("0.##########");

            String url = BBSCSUtil.absoluteActionURL(request,
                "/reg?action=validate&userName=" + ui.getUserName() + "&validateCode=" + ui.getValidateCode()).
                toString();

            Map root = new HashMap();
            root.put("website", this.getSysConfig().getForumName());

            root.put("validateUrl", "<a href=\"" + url + "\" target=\"_blank\">" + url + "</a>");

            Template temp = this.getTempConfiguration().getTemplate("regValidate.ftl");
            StringWriter sw = new StringWriter();
            temp.process(root, sw);
            this.getHtmlEmail().setHtmlMsg(sw.toString());
            this.getHtmlEmail().setTextMsg("Your email client does not support HTML messages");
            this.getHtmlEmail().addTo(ui.getEmail());
            this.getHtmlEmail().setCharset(Constant.CHARSET);
            this.getHtmlEmail().send();
            sw.flush();
          }
          catch (Exception ex1) {

          }
        }
        return mapping.findForward("regSucceed");
      }
      catch (BbscsException ex) {
        errors.add("error.reg.createrror", new ActionMessage("error.reg.createrror"));
        this.saveErrors(request, errors);
        return mapping.findForward("error");
      }

    }

    if (regForm.getAction().equalsIgnoreCase("validate")) {
      UserInfo ui = this.getUserService().findUserInfoByUserName(regForm.getUserName());
      if (ui == null) {
        errors.add("error.user.noexist", new ActionMessage("error.user.noexist"));
        this.saveErrors(request, errors);
        return mapping.findForward("error");
      }
      if (!ui.getValidateCode().equals(regForm.getValidateCode())) {
        errors.add("error.reg.validatecode", new ActionMessage("error.reg.validatecode"));
        this.saveErrors(request, errors);
        return mapping.findForward("error");
      }
      return mapping.findForward("validateSucceed");
    }

    return mapping.findForward("reg");
  }

  public UserService getUserService() {
    return userService;
  }

  public SysConfig getSysConfig() {
    return sysConfig;
  }

  public IPSeeker getIpSeeker() {
    return ipSeeker;
  }

  public SysStatService getSysStatService() {
    return sysStatService;
  }

  public HtmlEmail getHtmlEmail() {
    return htmlEmail;
  }

  public Configuration getTempConfiguration() {
    return tempConfiguration;
  }

  public void setUserService(UserService userService) {
    this.userService = userService;
  }

  public void setSysConfig(SysConfig sysConfig) {
    this.sysConfig = sysConfig;
  }

  public void setIpSeeker(IPSeeker ipSeeker) {
    this.ipSeeker = ipSeeker;
  }

  public void setSysStatService(SysStatService sysStatService) {
    this.sysStatService = sysStatService;
  }

  public void setHtmlEmail(HtmlEmail htmlEmail) {
    this.htmlEmail = htmlEmail;
  }

  public void setTempConfiguration(Configuration tempConfiguration) {
    this.tempConfiguration = tempConfiguration;
  }
}

⌨️ 快捷键说明

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