001: /**
002: * Copyright (c) 2003-2007, David A. Czarnecki
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms, with or without
006: * modification, are permitted provided that the following conditions are met:
007: *
008: * Redistributions of source code must retain the above copyright notice, this list of conditions and the
009: * following disclaimer.
010: * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
011: * following disclaimer in the documentation and/or other materials provided with the distribution.
012: * Neither the name of "David A. Czarnecki" and "blojsom" nor the names of its contributors may be used to
013: * endorse or promote products derived from this software without specific prior written permission.
014: * Products derived from this software may not be called "blojsom", nor may "blojsom" appear in their name,
015: * without prior written permission of David A. Czarnecki.
016: *
017: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
018: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
019: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
020: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
021: * EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
022: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
023: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
025: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
026: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
027: * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
028: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
029: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
030: */package org.blojsom.plugin.moderation.admin;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034: import org.blojsom.blog.Blog;
035: import org.blojsom.blog.Entry;
036: import org.blojsom.fetcher.Fetcher;
037: import org.blojsom.fetcher.FetcherException;
038: import org.blojsom.plugin.PluginException;
039: import org.blojsom.plugin.admin.WebAdminPlugin;
040: import org.blojsom.util.BlojsomConstants;
041: import org.blojsom.util.BlojsomUtils;
042:
043: import javax.servlet.http.HttpServletRequest;
044: import javax.servlet.http.HttpServletResponse;
045: import java.io.BufferedReader;
046: import java.io.IOException;
047: import java.io.StringReader;
048: import java.util.ArrayList;
049: import java.util.List;
050: import java.util.Map;
051:
052: /**
053: * Spam phrase moderation administration plugin
054: *
055: * @author David Czarnecki
056: * @version $Id: SpamPhraseModerationAdminPlugin.java,v 1.3 2007/01/17 02:35:12 czarneckid Exp $
057: * @since blojsom 3.0
058: */
059: public class SpamPhraseModerationAdminPlugin extends WebAdminPlugin {
060:
061: private Log _logger = LogFactory
062: .getLog(SpamPhraseModerationAdminPlugin.class);
063:
064: private static final String SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist";
065:
066: // Localization constants
067: private static final String FAILED_SPAM_PHRASE_PERMISSION_KEY = "failed.spam.phrase.permission.text";
068: private static final String ADDED_SPAM_PHRASE_KEY = "added.spam.phrase.text";
069: private static final String SPAM_PHRASE_ALREADY_ADDED_KEY = "spam.phrase.already.added.text";
070: private static final String DELETED_SPAM_PHRASE_KEY = "deleted.spam.phrase.text";
071: private static final String NO_SPAM_PHRASES_TO_DELETE_KEY = "no.spam.phrases.to.delete.text";
072:
073: // Context
074: private static final String BLOJSOM_PLUGIN_SPAM_PHRASES = "BLOJSOM_PLUGIN_SPAM_PHRASES";
075:
076: // Pages
077: private static final String EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-spam-phrase-moderation-settings";
078:
079: // Form itmes
080: private static final String SPAM_PHRASE = "spam-phrase";
081:
082: // Actions
083: private static final String ADD_SPAM_PHRASE_ACTION = "add-spam-phrase";
084: private static final String DELETE_SPAM_PHRASE_ACTION = "delete-spam-phrase";
085:
086: // Permissions
087: private static final String SPAM_PHRASE_MODERATION_PERMISSION = "spam_phrase_moderation";
088:
089: private Fetcher _fetcher;
090:
091: /**
092: * Create a new instance of the spam phrase moderation administration plugin
093: */
094: public SpamPhraseModerationAdminPlugin() {
095: }
096:
097: /**
098: * Return the display name for the plugin
099: *
100: * @return Display name for the plugin
101: */
102: public String getDisplayName() {
103: return "Spam Phrase Moderation plugin";
104: }
105:
106: /**
107: * Return the name of the initial editing page for the plugin
108: *
109: * @return Name of the initial editing page for the plugin
110: */
111: public String getInitialPage() {
112: return EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE;
113: }
114:
115: /**
116: * Set the {@link Fetcher}
117: *
118: * @param fetcher {@link Fetcher}
119: */
120: public void setFetcher(Fetcher fetcher) {
121: _fetcher = fetcher;
122: }
123:
124: /**
125: * Initialize this plugin. This method only called when the plugin is instantiated.
126: *
127: * @throws org.blojsom.plugin.PluginException
128: * If there is an error initializing the plugin
129: */
130: public void init() throws PluginException {
131: super .init();
132: }
133:
134: /**
135: * Process the blog entries
136: *
137: * @param httpServletRequest Request
138: * @param httpServletResponse Response
139: * @param blog {@link Blog} instance
140: * @param context Context
141: * @param entries Blog entries retrieved for the particular request
142: * @return Modified set of blog entries
143: * @throws PluginException If there is an error processing the blog entries
144: */
145: public Entry[] process(HttpServletRequest httpServletRequest,
146: HttpServletResponse httpServletResponse, Blog blog,
147: Map context, Entry[] entries) throws PluginException {
148: entries = super .process(httpServletRequest,
149: httpServletResponse, blog, context, entries);
150:
151: String page = BlojsomUtils.getRequestValue(
152: BlojsomConstants.PAGE_PARAM, httpServletRequest);
153:
154: String username = getUsernameFromSession(httpServletRequest,
155: blog);
156: if (!checkPermission(blog, null, username,
157: SPAM_PHRASE_MODERATION_PERMISSION)) {
158: httpServletRequest.setAttribute(
159: BlojsomConstants.PAGE_PARAM,
160: ADMIN_ADMINISTRATION_PAGE);
161: addOperationResultMessage(context, getAdminResource(
162: FAILED_SPAM_PHRASE_PERMISSION_KEY,
163: FAILED_SPAM_PHRASE_PERMISSION_KEY, blog
164: .getBlogAdministrationLocale()));
165:
166: return entries;
167: }
168:
169: if (ADMIN_LOGIN_PAGE.equals(page)) {
170: return entries;
171: } else {
172: String action = BlojsomUtils.getRequestValue(ACTION_PARAM,
173: httpServletRequest);
174: List spamPhrases = loadSpamPhrases(blog);
175: String spamPhrase = BlojsomUtils.getRequestValue(
176: SPAM_PHRASE, httpServletRequest);
177:
178: if (ADD_SPAM_PHRASE_ACTION.equals(action)) {
179: if (!spamPhrases.contains(spamPhrase)) {
180: spamPhrases.add(spamPhrase);
181: blog.setProperty(SPAM_PHRASE_BLACKLIST_IP,
182: BlojsomUtils
183: .listToString(spamPhrases, "\n"));
184:
185: try {
186: _fetcher.saveBlog(blog);
187: } catch (FetcherException e) {
188: if (_logger.isErrorEnabled()) {
189: _logger.error(e);
190: }
191: }
192:
193: addOperationResultMessage(context,
194: formatAdminResource(ADDED_SPAM_PHRASE_KEY,
195: ADDED_SPAM_PHRASE_KEY,
196: blog.getBlogAdministrationLocale(),
197: new Object[] { spamPhrase }));
198: } else {
199: addOperationResultMessage(context,
200: formatAdminResource(
201: SPAM_PHRASE_ALREADY_ADDED_KEY,
202: SPAM_PHRASE_ALREADY_ADDED_KEY,
203: blog.getBlogAdministrationLocale(),
204: new Object[] { spamPhrase }));
205: }
206: } else if (DELETE_SPAM_PHRASE_ACTION.equals(action)) {
207: String[] spamPhrasesToDelete = httpServletRequest
208: .getParameterValues(SPAM_PHRASE);
209: if (spamPhrasesToDelete != null
210: && spamPhrasesToDelete.length > 0) {
211: for (int i = 0; i < spamPhrasesToDelete.length; i++) {
212: spamPhrases
213: .set(
214: Integer
215: .parseInt(spamPhrasesToDelete[i]),
216: null);
217: }
218:
219: spamPhrases = BlojsomUtils
220: .removeNullValues(spamPhrases);
221: blog.setProperty(SPAM_PHRASE_BLACKLIST_IP,
222: BlojsomUtils
223: .listToString(spamPhrases, "\n"));
224:
225: try {
226: _fetcher.saveBlog(blog);
227: } catch (FetcherException e) {
228: if (_logger.isErrorEnabled()) {
229: _logger.error(e);
230: }
231: }
232:
233: addOperationResultMessage(
234: context,
235: formatAdminResource(
236: DELETED_SPAM_PHRASE_KEY,
237: DELETED_SPAM_PHRASE_KEY,
238: blog.getBlogAdministrationLocale(),
239: new Object[] { new Integer(
240: spamPhrasesToDelete.length) }));
241: } else {
242: addOperationResultMessage(context,
243: getAdminResource(
244: NO_SPAM_PHRASES_TO_DELETE_KEY,
245: NO_SPAM_PHRASES_TO_DELETE_KEY,
246: blog.getBlogAdministrationLocale()));
247: }
248: }
249:
250: context.put(BLOJSOM_PLUGIN_SPAM_PHRASES, spamPhrases);
251: }
252:
253: return entries;
254: }
255:
256: /**
257: * Load the list of spam phrases from the blog
258: *
259: * @param blog {@link blog}
260: * @return List of spam phrases
261: */
262: protected List loadSpamPhrases(Blog blog) {
263: ArrayList spamPhrases = new ArrayList(25);
264:
265: String spamPhrasesValues = blog
266: .getProperty(SPAM_PHRASE_BLACKLIST_IP);
267: if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) {
268: try {
269: StringReader stringReader = new StringReader(
270: spamPhrasesValues);
271: BufferedReader br = new BufferedReader(stringReader);
272: String phrase;
273:
274: while ((phrase = br.readLine()) != null) {
275: spamPhrases.add(phrase);
276: }
277:
278: br.close();
279: } catch (IOException e) {
280: if (_logger.isErrorEnabled()) {
281: _logger.error(e);
282: }
283: }
284: }
285:
286: return spamPhrases;
287: }
288: }
|