001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on 09/08/2007 09:31:17
040: *
041: * The JForum Project
042: * http://www.jforum.net
043: */
044: package net.jforum.view.forum;
045:
046: import java.util.ArrayList;
047: import java.util.Date;
048: import java.util.List;
049:
050: import net.jforum.Command;
051: import net.jforum.SessionFacade;
052: import net.jforum.dao.DataAccessDriver;
053: import net.jforum.dao.PostDAO;
054: import net.jforum.entities.ModerationLog;
055: import net.jforum.entities.Post;
056: import net.jforum.entities.User;
057: import net.jforum.repository.PostRepository;
058: import net.jforum.repository.SecurityRepository;
059: import net.jforum.search.LuceneManager;
060: import net.jforum.search.SearchFacade;
061: import net.jforum.security.SecurityConstants;
062: import net.jforum.util.SafeHtml;
063: import net.jforum.util.mail.Spammer;
064: import net.jforum.util.preferences.ConfigKeys;
065: import net.jforum.util.preferences.SystemGlobals;
066: import net.jforum.util.preferences.TemplateKeys;
067: import net.jforum.view.forum.common.PostCommon;
068:
069: import org.apache.commons.lang.StringEscapeUtils;
070: import org.apache.log4j.Logger;
071: import org.apache.lucene.document.Document;
072:
073: import freemarker.template.SimpleHash;
074:
075: /**
076: * @author Rafael Steil
077: * @version $Id: AjaxAction.java,v 1.7 2007/09/21 15:54:31 rafaelsteil Exp $
078: */
079: public class AjaxAction extends Command {
080: private static Logger logger = Logger.getLogger(AjaxAction.class);
081:
082: /**
083: * Sends a test message
084: * @param sender The sender's email address
085: * @param host the smtp host
086: * @param auth if need authorization or not
087: * @param username the smtp server username, if auth is needed
088: * @param password the smtp server password, if auth is needed
089: * @param to the recipient
090: * @return The status message
091: */
092: public void sendTestMail() {
093: String sender = this .request.getParameter("sender");
094: String host = this .request.getParameter("host");
095: String port = this .request.getParameter("port");
096: String auth = this .request.getParameter("auth");
097: String ssl = this .request.getParameter("ssl");
098: String username = this .request.getParameter("username");
099: String password = this .request.getParameter("password");
100: String to = this .request.getParameter("to");
101:
102: // Save the current values
103: String originalHost = SystemGlobals
104: .getValue(ConfigKeys.MAIL_SMTP_HOST);
105: String originalAuth = SystemGlobals
106: .getValue(ConfigKeys.MAIL_SMTP_AUTH);
107: String originalUsername = SystemGlobals
108: .getValue(ConfigKeys.MAIL_SMTP_USERNAME);
109: String originalPassword = SystemGlobals
110: .getValue(ConfigKeys.MAIL_SMTP_PASSWORD);
111: String originalSender = SystemGlobals
112: .getValue(ConfigKeys.MAIL_SENDER);
113: String originalSSL = SystemGlobals
114: .getValue(ConfigKeys.MAIL_SMTP_SSL);
115: String originalPort = SystemGlobals
116: .getValue(ConfigKeys.MAIL_SMTP_PORT);
117:
118: // Now put the new ones
119: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST, host);
120: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH, auth);
121: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME, username);
122: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD, password);
123: SystemGlobals.setValue(ConfigKeys.MAIL_SENDER, sender);
124: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL, ssl);
125: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT, port);
126:
127: String status = "OK";
128:
129: // Send the test mail
130: class TestSpammer extends Spammer {
131: public TestSpammer(String to) {
132: List l = new ArrayList();
133:
134: User user = new User();
135: user.setEmail(to);
136:
137: l.add(user);
138:
139: this .setUsers(l);
140:
141: this .setTemplateParams(new SimpleHash());
142: this .prepareMessage("JForum Test Mail", null);
143: }
144:
145: protected String processTemplate() throws Exception {
146: return ("Test mail from JForum Admin Panel. Sent at " + new Date());
147: }
148:
149: protected void createTemplate(String messageFile)
150: throws Exception {
151: }
152: }
153:
154: Spammer s = new TestSpammer(to);
155:
156: try {
157: s.dispatchMessages();
158: } catch (Exception e) {
159: status = StringEscapeUtils.escapeJavaScript(e.toString());
160: logger.error(e.toString(), e);
161: } finally {
162: // Restore the original values
163: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_HOST,
164: originalHost);
165: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_AUTH,
166: originalAuth);
167: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_USERNAME,
168: originalUsername);
169: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PASSWORD,
170: originalPassword);
171: SystemGlobals.setValue(ConfigKeys.MAIL_SENDER,
172: originalSender);
173: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_SSL,
174: originalSSL);
175: SystemGlobals.setValue(ConfigKeys.MAIL_SMTP_PORT,
176: originalPort);
177: }
178:
179: this .setTemplateName(TemplateKeys.AJAX_TEST_MAIL);
180: this .context.put("status", status);
181: }
182:
183: public void isPostIndexed() {
184: int postId = this .request.getIntParameter("post_id");
185:
186: this .setTemplateName(TemplateKeys.AJAX_IS_POST_INDEXED);
187:
188: LuceneManager manager = (LuceneManager) SearchFacade.manager();
189: Document doc = manager.luceneSearch().findDocumentByPostId(
190: postId);
191:
192: this .context.put("doc", doc);
193: }
194:
195: public void loadPostContents() {
196: int postId = this .request.getIntParameter("id");
197: PostDAO dao = DataAccessDriver.getInstance().newPostDAO();
198: Post post = dao.selectById(postId);
199: this .setTemplateName(TemplateKeys.AJAX_LOAD_POST);
200: this .context.put("post", post);
201: }
202:
203: public void savePost() {
204: PostDAO postDao = DataAccessDriver.getInstance().newPostDAO();
205: Post post = postDao.selectById(this .request
206: .getIntParameter("id"));
207:
208: String originalMessage = post.getText();
209:
210: if (!PostCommon.canEditPost(post)) {
211: post = PostCommon.preparePostForDisplay(post);
212: } else {
213: post.setText(this .request.getParameter("value"));
214: postDao.update(post);
215: SearchFacade.update(post);
216: post = PostCommon.preparePostForDisplay(post);
217: }
218:
219: boolean isModerator = SecurityRepository
220: .canAccess(SecurityConstants.PERM_MODERATION_POST_EDIT);
221:
222: if (SystemGlobals
223: .getBoolValue(ConfigKeys.MODERATION_LOGGING_ENABLED)
224: && isModerator
225: && post.getUserId() != SessionFacade.getUserSession()
226: .getUserId()) {
227: ModerationHelper helper = new ModerationHelper();
228:
229: this .request.addParameter("log_original_message",
230: originalMessage);
231: this .request.addParameter("post_id", String.valueOf(post
232: .getId()));
233: this .request.addParameter("topic_id", String.valueOf(post
234: .getTopicId()));
235:
236: ModerationLog log = helper.buildModerationLogFromRequest();
237: log.getPosterUser().setId(post.getUserId());
238:
239: helper.saveModerationLog(log);
240: }
241:
242: if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
243: PostRepository.update(post.getTopicId(), PostCommon
244: .preparePostForDisplay(post));
245: }
246:
247: this .setTemplateName(TemplateKeys.AJAX_LOAD_POST);
248: this .context.put("post", post);
249: }
250:
251: public void previewPost() {
252: Post post = new Post();
253:
254: post.setText(this .request.getParameter("text"));
255: post.setSubject(this .request.getParameter("subject"));
256: post.setHtmlEnabled("true".equals(this .request
257: .getParameter("html")));
258: post.setBbCodeEnabled("true".equals(this .request
259: .getParameter("bbcode")));
260: post.setSmiliesEnabled("true".equals(this .request
261: .getParameter("smilies")));
262:
263: if (post.isHtmlEnabled()) {
264: post.setText(new SafeHtml().makeSafe(post.getText()));
265: }
266:
267: post = PostCommon.preparePostForDisplay(post);
268: post.setSubject(StringEscapeUtils.escapeJavaScript(post
269: .getSubject()));
270: post
271: .setText(StringEscapeUtils.escapeJavaScript(post
272: .getText()));
273:
274: this .setTemplateName(TemplateKeys.AJAX_PREVIEW_POST);
275: this .context.put("post", post);
276: }
277:
278: /**
279: * @see net.jforum.Command#list()
280: */
281: public void list() {
282: this.ignoreAction();
283: }
284: }
|