skinutils.java
来自「Jive是基于JSP/JAVA技术构架的一个大型BBS论坛系统,这是Jive论坛」· Java 代码 · 共 1,157 行 · 第 1/3 页
JAVA
1,157 行
* @param request the servlet request object * @return True if the email was send successfully, false otherwise * @throws IllegalArgumentException if either username or request is null */ public static boolean emailResetPasswordInstructions(String username, HttpServletRequest request) throws IllegalArgumentException { if (username == null || request == null) { throw new IllegalArgumentException("Username and request must not be null"); } String token = null; DbForumFactory dbf = null; UserManager userManager = null; User user = null; long now = System.currentTimeMillis(); try { dbf = DbForumFactory.getInstance(); userManager = dbf.getUserManager(); user = userManager.getUser(username); token = user.getProperty(JIVE_PASSWORDRESET_TOKEN); // don't email the default administrator if (user.getID() == 1) { return false; } } catch (UserNotFoundException e) { return false; } // first, check to see if we already have a token if (token != null) { String tStamp = user.getProperty(JIVE_PASSWORDRESET_TIMESTAMP); String lSent = user.getProperty(JIVE_PASSWORDRESET_LAST_SENT); long timeStamp = Long.valueOf(tStamp).longValue(); long lastSent = Long.valueOf(lSent).longValue(); // we've set a token prior to the last 72 hours, reset it if (now-timeStamp > 72*60*60*1000) { token = StringUtils.randomString(8); try { user.setProperty(JIVE_PASSWORDRESET_TOKEN, token); user.setProperty(JIVE_PASSWORDRESET_TIMESTAMP, String.valueOf(now)); } catch (UnauthorizedException e) { System.err.println("Unable to update user password reset properties"); return false; } } // an email was sent in the last 24 hours, don't send another else if (now-lastSent < 24*60*60*1000) { return false; } } else { token = StringUtils.randomString(8); try { user.setProperty(JIVE_PASSWORDRESET_TOKEN, token); user.setProperty(JIVE_PASSWORDRESET_TIMESTAMP, String.valueOf(now)); } catch (UnauthorizedException e) { System.err.println("Unable to update user password reset properties"); return false; } } String toName = user.getName(); String toEmail = user.getEmail(); String fromEmail = JiveGlobals.getJiveProperty("passwordReset.email.fromEmail"); String fromName = JiveGlobals.getJiveProperty("passwordReset.email.fromName"); String subject = JiveGlobals.getJiveProperty("passwordReset.email.subject"); String body = JiveGlobals.getJiveProperty("passwordReset.email.body"); // Check for null values if (fromEmail == null || fromName == null || subject == null || body == null) { System.err.println("WARNING: Not all password reset values set in " + "jive_config.xml, password resetting may fail."); } Map args = new HashMap(); args.put("token", token); args.put("request", request); args.put("host", JiveGlobals.getJiveProperty("passwordReset.email.host")); args.put("port", JiveGlobals.getJiveProperty("passwordReset.email.port")); args.put("webAppDir", JiveGlobals.getJiveProperty("passwordReset.email.webAppDir")); subject = replaceTokens(subject, args, user); body = replaceTokens(body, args, user); EmailTask emailTask = new EmailTask(); emailTask.addMessage(toName, toEmail, fromName, fromEmail, subject, body); TaskEngine.addTask(emailTask); // update the last sent timestamp in the db try { user.setProperty(JIVE_PASSWORDRESET_LAST_SENT, String.valueOf(now)); } catch (UnauthorizedException e) { System.err.println("Unable to update user password reset properties"); return false; } return true; } private static String replaceTokens(String string, Map args, User user) { if (string == null || user == null) { return ""; } HttpServletRequest request = (HttpServletRequest) args.get("request"); String name = (user.getName() == null) ? user.getUsername() : user.getName(); String host = (String) args.get("host"); String port = (String) args.get("port"); String webAppDir = (String) args.get("webAppDir"); String requestIP = request.getRemoteHost(); String token = (String) args.get("token"); if (host == null) { host = request.getServerName(); } if (port == null) { port = String.valueOf(request.getServerPort()); } if (webAppDir == null) { webAppDir = request.getContextPath(); } // {userID}, {username}, {name}, {email}, {token}, // {requestIP}, {host}, {port} {webAppDir} string = StringUtils.replace(string, "{userID}", Long.toString(user.getID())); string = StringUtils.replace(string, "{username}", user.getUsername()); string = StringUtils.replace(string, "{name}", name); string = StringUtils.replace(string, "{email}", user.getEmail()); string = StringUtils.replace(string, "{token}", token); string = StringUtils.replace(string, "{requestIP}", requestIP); string = StringUtils.replace(string, "{host}", host); string = StringUtils.replace(string, "{port}", port); string = StringUtils.replace(string, "{webAppDir}", webAppDir); return string; } /** * Returns true is the supplied token is valid for the userId given. This * method checks the consistency of the token (case sensitive) as well as * whether the token is valid timewise (The token is only good for 72 hours). * If the token is not valid timewise, the user property storing the token * will be deleted. * * @param userId the id of the user for whom we are validating the password * reset token. * @param token the password reset token associated with the above user. * @return true is the token is valid, false otherwise. */ public static boolean isValidPasswordToken(long userId, String token) { String dbToken = null; String dbTimeStamp = null; DbForumFactory dbf = null; UserManager userManager = null; User user = null; long now = System.currentTimeMillis(); long timeStamp = -1; try { dbf = DbForumFactory.getInstance(); userManager = dbf.getUserManager(); user = userManager.getUser(userId); dbToken = user.getProperty(JIVE_PASSWORDRESET_TOKEN); dbTimeStamp = user.getProperty(JIVE_PASSWORDRESET_TIMESTAMP); // Check validity of token if (dbToken == null || dbTimeStamp == null) { return false; } timeStamp = Long.valueOf(dbTimeStamp).longValue(); } catch (UserNotFoundException e) { return false; } // token in db was too old - delete it if (now-timeStamp > 72*60*60*1000) { try { user.deleteProperty(JIVE_PASSWORDRESET_TOKEN); user.deleteProperty(JIVE_PASSWORDRESET_TIMESTAMP); user.deleteProperty(JIVE_PASSWORDRESET_LAST_SENT); } catch (UnauthorizedException e) { System.err.println("Unable to delete user password reset properties"); } return false; } else if (!dbToken.equals(token)) { return false; } else if (user.getID() == 1) { return false; } else { return true; } } /** * This method is used in conjunction with the emailResetPasswordInstructions * method to reset a user's password. The system generated token will be * provided in the instructional email. Returns true if the password was * reset, false otherwise. Possible reasons for failure:<p> * <ul> * <li>The token given is not valid for the userId specified. * <li>The user specified has no current password reset token. * <li>The token specified for the user has expired (Tokens have a 72 hour * lifespan). * </ul> * * @param userId the id of the user for whom the password should be changed. * @param token the password reset token associated with the above user. * @param newPassword the password to use for the specified user. * @return true is the password was changed, false otherwise. */ public static boolean resetUserPassword(long userId, String token, String newPassword) { // first, validate the password token if (!isValidPasswordToken(userId, token)) { return false; } DbForumFactory dbf = null; UserManager userManager = null; User user = null; try { dbf = DbForumFactory.getInstance(); userManager = dbf.getUserManager(); user = userManager.getUser(userId); user.setPassword(newPassword); user.deleteProperty(JIVE_PASSWORDRESET_TOKEN); user.deleteProperty(JIVE_PASSWORDRESET_TIMESTAMP); user.deleteProperty(JIVE_PASSWORDRESET_LAST_SENT); } catch (UserNotFoundException e) { System.err.println("User not found resetting user password"); return false; } catch (UnauthorizedException e) { System.err.println("Unable to reset user password, uae thrown"); return false; } return true; } /** * Returns an English sentence which describes the <code>Date</code> * parameter relative to the current time. For instance, if the passed * in date was 39 seconds ago, this method returns:<p> * "Less than 1 min ago"<p> * Similiarly, a date 1 ago would be:<p> * "Yesterday at 3:53 PM"<p> * * The method obeys user preferences for time zone if they exist. * * @param request the servlet request object. * @param user the User that the date is being formatted for, or null * if an anonymmous user. * @param date the Date object we're comparing the current time with. */ public static String dateToText(HttpServletRequest request, User user, Date date) { if (date == null) { return ""; } // Time difference between now and the parameter object's time. Using // the cache timer means the resolution is no better than a seconds, // for for our purposes, we don't need better. long delta = CacheFactory.currentTime - date.getTime(); // Within the last hour if ((delta / JiveGlobals.HOUR) < 1) { long minutes = (delta/JiveGlobals.MINUTE); if (minutes == 0) { return "Less than 1 min ago"; } else if (minutes == 1) { return "1 minute ago"; } else { return (minutes + " minutes ago"); } } // Sometime today if ((delta / JiveGlobals.DAY) < 1) { long hours = (delta/JiveGlobals.HOUR); if(hours <= 1) { return "1 hour ago"; } else { return (hours + " hours ago"); } } int hour = -1; int minute = -1; int am_pm = -1; int day_of_month = -1; int month = -1; int day_of_the_week = -1; synchronized (globalCal) { globalCal.setTime(date); globalCal.setTimeZone(getTimeZone(request, user)); hour = globalCal.get(Calendar.HOUR); minute = globalCal.get(Calendar.MINUTE); am_pm = globalCal.get(Calendar.AM_PM); day_of_the_week = globalCal.get(Calendar.DAY_OF_WEEK); day_of_month = globalCal.get(Calendar.DAY_OF_MONTH); month = globalCal.get(Calendar.MONTH); } // Within the last week if ((delta / JiveGlobals.WEEK) < 1) { long days = (delta/JiveGlobals.DAY); if (days <= 1) { StringBuffer buf = new StringBuffer("Yesterday, "); if (am_pm == 1 && hour == 0) { buf.append(12).append(":"); } else { buf.append(hour).append(":"); } if (minute < 10) { buf.append("0").append(minute); } else { buf.append(minute); } buf.append(" "); if (am_pm == 0) { buf.append("AM"); } else { buf.append("PM"); } return buf.toString(); } else { StringBuffer buf = new StringBuffer(); buf.append(DAYS_OF_WEEK[day_of_the_week-1]); buf.append(", ").append(MONTHS_OF_YEAR[month]); buf.append(" ").append(day_of_month).append(" "); if (am_pm == 1 && hour == 0) { buf.append(12).append(":"); } else { buf.append(hour).append(":"); } if (minute < 10) { buf.append("0").append(minute); } else { buf.append(minute); } buf.append(" "); if (am_pm == 0) { buf.append("AM"); } else { buf.append("PM"); } return buf.toString(); } } // More than a week ago. else { return formatDate(request, user, date); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?