AjaxAction.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 9k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * 
  5.  * Redistribution and use in source and binary forms, 
  6.  * with or without modification, are permitted provided 
  7.  * that the following conditions are met:
  8.  * 
  9.  * 1) Redistributions of source code must retain the above 
  10.  * copyright notice, this list of conditions and the 
  11.  * following  disclaimer.
  12.  * 2)  Redistributions in binary form must reproduce the 
  13.  * above copyright notice, this list of conditions and 
  14.  * the following disclaimer in the documentation and/or 
  15.  * other materials provided with the distribution.
  16.  * 3) Neither the name of "Rafael Steil" nor 
  17.  * the names of its contributors may be used to endorse 
  18.  * or promote products derived from this software without 
  19.  * specific prior written permission.
  20.  * 
  21.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  22.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  23.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  24.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  25.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  26.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  27.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  28.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  29.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  30.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  31.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  32.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  33.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  34.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  35.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  36.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  37.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  38.  * 
  39.  * Created on 09/08/2007 09:31:17
  40.  * 
  41.  * The JForum Project
  42.  * http://www.jforum.net
  43.  */
  44. package net.jforum.view.forum;
  45. import java.util.ArrayList;
  46. import java.util.Date;
  47. import java.util.List;
  48. import net.jforum.Command;
  49. import net.jforum.SessionFacade;
  50. import net.jforum.dao.DataAccessDriver;
  51. import net.jforum.dao.PostDAO;
  52. import net.jforum.entities.ModerationLog;
  53. import net.jforum.entities.Post;
  54. import net.jforum.entities.User;
  55. import net.jforum.repository.PostRepository;
  56. import net.jforum.repository.SecurityRepository;
  57. import net.jforum.search.LuceneManager;
  58. import net.jforum.search.SearchFacade;
  59. import net.jforum.security.SecurityConstants;
  60. import net.jforum.util.SafeHtml;
  61. import net.jforum.util.mail.Spammer;
  62. import net.jforum.util.preferences.ConfigKeys;
  63. import net.jforum.util.preferences.SystemGlobals;
  64. import net.jforum.util.preferences.TemplateKeys;
  65. import net.jforum.view.forum.common.PostCommon;
  66. import org.apache.commons.lang.StringEscapeUtils;
  67. import org.apache.log4j.Logger;
  68. import org.apache.lucene.document.Document;
  69. import freemarker.template.SimpleHash;
  70. /**
  71.  * @author Rafael Steil
  72.  * @version $Id: AjaxAction.java,v 1.7 2007/09/21 15:54:31 rafaelsteil Exp $
  73.  */
  74. public class AjaxAction extends Command
  75. {
  76. private static Logger logger = Logger.getLogger(AjaxAction.class);
  77. /**
  78.  * Sends a test message
  79.  * @param sender The sender's email address
  80.  * @param host the smtp host
  81.  * @param auth if need authorization or not
  82.  * @param username the smtp server username, if auth is needed
  83.  * @param password the smtp server password, if auth is needed
  84.  * @param to the recipient
  85.  * @return The status message
  86.  */
  87. public void sendTestMail()
  88. {
  89. String sender = this.request.getParameter("sender");
  90. String host = this.request.getParameter("host");
  91. String port = this.request.getParameter("port");
  92. String auth = this.request.getParameter("auth");
  93. String ssl = this.request.getParameter("ssl");
  94. String username = this.request.getParameter("username");
  95. String password = this.request.getParameter("password");
  96. String to = this.request.getParameter("to");
  97. // Save the current values
  98. String originalHost = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_HOST);
  99. String originalAuth = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_AUTH);
  100. String originalUsername = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_USERNAME);
  101. String originalPassword = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PASSWORD);
  102. String originalSender = SystemGlobals.getValue(ConfigKeys.MAIL_SENDER);
  103. String originalSSL = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_SSL);
  104. String originalPort = SystemGlobals.getValue(ConfigKeys.MAIL_SMTP_PORT);
  105. // Now put the new ones
  106. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, host);
  107. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, auth);
  108. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, username);
  109. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, password);
  110. SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, sender);
  111. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL, ssl);
  112. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT, port);
  113. String status = "OK";
  114. // Send the test mail
  115. class TestSpammer extends Spammer {
  116. public TestSpammer(String to) {
  117. List l = new ArrayList();
  118. User user = new User();
  119. user.setEmail(to);
  120. l.add(user);
  121. this.setUsers(l);
  122. this.setTemplateParams(new SimpleHash());
  123. this.prepareMessage("JForum Test Mail", null);
  124. }
  125. protected String processTemplate() throws Exception {
  126. return ("Test mail from JForum Admin Panel. Sent at " + new Date());
  127. }
  128. protected void createTemplate(String messageFile) throws Exception {}
  129. }
  130. Spammer s = new TestSpammer(to);
  131. try {
  132. s.dispatchMessages();
  133. }
  134. catch (Exception e) {
  135. status = StringEscapeUtils.escapeJavaScript(e.toString());
  136. logger.error(e.toString(), e);
  137. }
  138. finally {
  139. // Restore the original values
  140. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, originalHost);
  141. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, originalAuth);
  142. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, originalUsername);
  143. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, originalPassword);
  144. SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, originalSender);
  145. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL, originalSSL);
  146. SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT, originalPort);
  147. }
  148. this.setTemplateName(TemplateKeys.AJAX_TEST_MAIL);
  149. this.context.put("status", status);
  150. }
  151. public void isPostIndexed()
  152. {
  153. int postId = this.request.getIntParameter("post_id");
  154. this.setTemplateName(TemplateKeys.AJAX_IS_POST_INDEXED);
  155. LuceneManager manager = (LuceneManager)SearchFacade.manager();
  156. Document doc = manager.luceneSearch().findDocumentByPostId(postId);
  157. this.context.put("doc", doc);
  158. }
  159. public void loadPostContents()
  160. {
  161. int postId = this.request.getIntParameter("id");
  162. PostDAO dao = DataAccessDriver.getInstance().newPostDAO();
  163. Post post = dao.selectById(postId);
  164. this.setTemplateName(TemplateKeys.AJAX_LOAD_POST);
  165. this.context.put("post", post);
  166. }
  167. public void savePost()
  168. {
  169. PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
  170. Post post = postDao.selectById(this.request.getIntParameter("id"));
  171. String originalMessage = post.getText();
  172. if (!PostCommon.canEditPost(post)) {
  173. post = PostCommon.preparePostForDisplay(post);
  174. }
  175. else {
  176. post.setText(this.request.getParameter("value"));
  177. postDao.update(post);
  178. SearchFacade.update(post);
  179. post = PostCommon.preparePostForDisplay(post);
  180. }
  181. boolean isModerator = SecurityRepository.canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT);
  182. if (SystemGlobals.getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED)
  183. && isModerator && post.getUserId() != SessionFacade.getUserSession().getUserId()) {
  184. ModerationHelper helper = new ModerationHelper();
  185. this.request.addParameter("log_original_message", originalMessage);
  186. this.request.addParameter("post_id", String.valueOf(post.getId()));
  187. this.request.addParameter("topic_id", String.valueOf(post.getTopicId()));
  188. ModerationLog log = helper.buildModerationLogFromRequest();
  189. log.getPosterUser().setId(post.getUserId());
  190. helper.saveModerationLog(log);
  191. }
  192. if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
  193. PostRepository.update(post.getTopicId(), PostCommon.preparePostForDisplay(post));
  194. }
  195. this.setTemplateName(TemplateKeys.AJAX_LOAD_POST);
  196. this.context.put("post", post);
  197. }
  198. public void previewPost()
  199. {
  200. Post post = new Post();
  201. post.setText(this.request.getParameter("text"));
  202. post.setSubject(this.request.getParameter("subject"));
  203. post.setHtmlEnabled("true".equals(this.request.getParameter("html")));
  204. post.setBbCodeEnabled("true".equals(this.request.getParameter("bbcode")));
  205. post.setSmiliesEnabled("true".equals(this.request.getParameter("smilies")));
  206. if (post.isHtmlEnabled()) {
  207. post.setText(new SafeHtml().makeSafe(post.getText()));
  208. }
  209. post = PostCommon.preparePostForDisplay(post);
  210. post.setSubject(StringEscapeUtils.escapeJavaScript(post.getSubject()));
  211. post.setText(StringEscapeUtils.escapeJavaScript(post.getText()));
  212. this.setTemplateName(TemplateKeys.AJAX_PREVIEW_POST);
  213. this.context.put("post", post);
  214. }
  215. /**
  216.  * @see net.jforum.Command#list()
  217.  */
  218. public void list()
  219. {
  220. this.ignoreAction();
  221. }
  222. }