| 1 31 package org.blojsom.plugin.moderation.admin; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.blojsom.blog.Blog; 36 import org.blojsom.blog.Entry; 37 import org.blojsom.fetcher.Fetcher; 38 import org.blojsom.fetcher.FetcherException; 39 import org.blojsom.plugin.PluginException; 40 import org.blojsom.plugin.admin.WebAdminPlugin; 41 import org.blojsom.util.BlojsomConstants; 42 import org.blojsom.util.BlojsomUtils; 43 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import java.io.BufferedReader ; 47 import java.io.IOException ; 48 import java.io.StringReader ; 49 import java.util.ArrayList ; 50 import java.util.List ; 51 import java.util.Map ; 52 53 60 public class SpamPhraseModerationAdminPlugin extends WebAdminPlugin { 61 62 private Log _logger = LogFactory.getLog(SpamPhraseModerationAdminPlugin.class); 63 64 private static final String SPAM_PHRASE_BLACKLIST_IP = "spam-phrase-blacklist"; 65 66 private static final String FAILED_SPAM_PHRASE_PERMISSION_KEY = "failed.spam.phrase.permission.text"; 68 private static final String ADDED_SPAM_PHRASE_KEY = "added.spam.phrase.text"; 69 private static final String SPAM_PHRASE_ALREADY_ADDED_KEY = "spam.phrase.already.added.text"; 70 private static final String DELETED_SPAM_PHRASE_KEY = "deleted.spam.phrase.text"; 71 private static final String NO_SPAM_PHRASES_TO_DELETE_KEY = "no.spam.phrases.to.delete.text"; 72 73 private static final String BLOJSOM_PLUGIN_SPAM_PHRASES = "BLOJSOM_PLUGIN_SPAM_PHRASES"; 75 76 private static final String EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-spam-phrase-moderation-settings"; 78 79 private static final String SPAM_PHRASE = "spam-phrase"; 81 82 private static final String ADD_SPAM_PHRASE_ACTION = "add-spam-phrase"; 84 private static final String DELETE_SPAM_PHRASE_ACTION = "delete-spam-phrase"; 85 86 private static final String SPAM_PHRASE_MODERATION_PERMISSION = "spam_phrase_moderation"; 88 89 private Fetcher _fetcher; 90 91 94 public SpamPhraseModerationAdminPlugin() { 95 } 96 97 102 public String getDisplayName() { 103 return "Spam Phrase Moderation plugin"; 104 } 105 106 111 public String getInitialPage() { 112 return EDIT_SPAM_PHRASE_MODERATION_SETTINGS_PAGE; 113 } 114 115 120 public void setFetcher(Fetcher fetcher) { 121 _fetcher = fetcher; 122 } 123 124 130 public void init() throws PluginException { 131 super.init(); 132 } 133 134 145 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 146 entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries); 147 148 String page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest); 149 150 String username = getUsernameFromSession(httpServletRequest, blog); 151 if (!checkPermission(blog, null, username, SPAM_PHRASE_MODERATION_PERMISSION)) { 152 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 153 addOperationResultMessage(context, getAdminResource(FAILED_SPAM_PHRASE_PERMISSION_KEY, FAILED_SPAM_PHRASE_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 154 155 return entries; 156 } 157 158 if (ADMIN_LOGIN_PAGE.equals(page)) { 159 return entries; 160 } else { 161 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 162 List spamPhrases = loadSpamPhrases(blog); 163 String spamPhrase = BlojsomUtils.getRequestValue(SPAM_PHRASE, httpServletRequest); 164 165 if (ADD_SPAM_PHRASE_ACTION.equals(action)) { 166 if (!spamPhrases.contains(spamPhrase)) { 167 spamPhrases.add(spamPhrase); 168 blog.setProperty(SPAM_PHRASE_BLACKLIST_IP, BlojsomUtils.listToString(spamPhrases, "\n")); 169 170 try { 171 _fetcher.saveBlog(blog); 172 } catch (FetcherException e) { 173 if (_logger.isErrorEnabled()) { 174 _logger.error(e); 175 } 176 } 177 178 addOperationResultMessage(context, formatAdminResource(ADDED_SPAM_PHRASE_KEY, ADDED_SPAM_PHRASE_KEY, blog.getBlogAdministrationLocale(), new Object [] {spamPhrase})); 179 } else { 180 addOperationResultMessage(context, formatAdminResource(SPAM_PHRASE_ALREADY_ADDED_KEY, SPAM_PHRASE_ALREADY_ADDED_KEY, blog.getBlogAdministrationLocale(), new Object [] {spamPhrase})); 181 } 182 } else if (DELETE_SPAM_PHRASE_ACTION.equals(action)) { 183 String [] spamPhrasesToDelete = httpServletRequest.getParameterValues(SPAM_PHRASE); 184 if (spamPhrasesToDelete != null && spamPhrasesToDelete.length > 0) { 185 for (int i = 0; i < spamPhrasesToDelete.length; i++) { 186 spamPhrases.set(Integer.parseInt(spamPhrasesToDelete[i]), null); 187 } 188 189 spamPhrases = BlojsomUtils.removeNullValues(spamPhrases); 190 blog.setProperty(SPAM_PHRASE_BLACKLIST_IP, BlojsomUtils.listToString(spamPhrases, "\n")); 191 192 try { 193 _fetcher.saveBlog(blog); 194 } catch (FetcherException e) { 195 if (_logger.isErrorEnabled()) { 196 _logger.error(e); 197 } 198 } 199 200 addOperationResultMessage(context, formatAdminResource(DELETED_SPAM_PHRASE_KEY, DELETED_SPAM_PHRASE_KEY, blog.getBlogAdministrationLocale(), new Object [] {new Integer (spamPhrasesToDelete.length)})); 201 } else { 202 addOperationResultMessage(context, getAdminResource(NO_SPAM_PHRASES_TO_DELETE_KEY, NO_SPAM_PHRASES_TO_DELETE_KEY, blog.getBlogAdministrationLocale())); 203 } 204 } 205 206 context.put(BLOJSOM_PLUGIN_SPAM_PHRASES, spamPhrases); 207 } 208 209 return entries; 210 } 211 212 218 protected List loadSpamPhrases(Blog blog) { 219 ArrayList spamPhrases = new ArrayList (25); 220 221 String spamPhrasesValues = blog.getProperty(SPAM_PHRASE_BLACKLIST_IP); 222 if (!BlojsomUtils.checkNullOrBlank(spamPhrasesValues)) { 223 try { 224 StringReader stringReader = new StringReader (spamPhrasesValues); 225 BufferedReader br = new BufferedReader (stringReader); 226 String phrase; 227 228 while ((phrase = br.readLine()) != null) { 229 spamPhrases.add(phrase); 230 } 231 232 br.close(); 233 } catch (IOException e) { 234 if (_logger.isErrorEnabled()) { 235 _logger.error(e); 236 } 237 } 238 } 239 240 return spamPhrases; 241 } 242 } | Popular Tags |