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 IPAddressModerationAdminPlugin extends WebAdminPlugin { 61 62 private Log _logger = LogFactory.getLog(IPAddressModerationAdminPlugin.class); 63 64 private static final String BLACKLIST = "blacklist"; 65 private static final String IP_BLACKLIST_IP = "ip-blacklist"; 66 private static final String IP_WHITELIST_IP = "ip-whitelist"; 67 68 private static final String FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY = "failed.ip.address.moderation.permission.text"; 70 private static final String ADDED_IP_TO_BLACKLIST_KEY = "added.ip.to.blacklist.text"; 71 private static final String IP_ALREADY_ADDED_TO_BLACKLIST_KEY = "ip.already.added.to.blacklist.text"; 72 private static final String ADDED_IP_TO_WHITELIST_KEY = "added.ip.to.whitelist.text"; 73 private static final String IP_ALREADY_ADDED_TO_WHITELIST_KEY = "ip.already.added.to.whitelist.text"; 74 private static final String DELETED_IP_ADDRESSES_BLACKLIST_KEY = "deleted.ip.addresses.blacklist.text"; 75 private static final String DELETED_IP_ADDRESSES_WHITELIST_KEY = "deleted.ip.addresses.whitelist.text"; 76 private static final String NO_IP_ADDRESSES_SELECTED_KEY = "no.ip.addresses.selected.text"; 77 78 private static final String BLOJSOM_PLUGIN_IP_WHITELIST = "BLOJSOM_PLUGIN_IP_WHITELIST"; 80 private static final String BLOJSOM_PLUGIN_IP_BLACKLIST = "BLOJSOM_PLUGIN_IP_BLACKLIST"; 81 82 private static final String EDIT_IP_MODERATION_SETTINGS_PAGE = "/org/blojsom/plugin/moderation/admin/templates/admin-edit-ip-moderation-settings"; 84 85 private static final String IP_ADDRESS = "ip-address"; 87 private static final String LIST_TYPE = "list-type"; 88 89 private static final String ADD_IP_ADDRESS_ACTION = "add-ip-address"; 91 private static final String DELETE_IP_ADDRESS_ACTION = "delete-ip-address"; 92 93 private static final String IP_MODERATION_PERMISSION = "ip_moderation"; 95 96 private Fetcher _fetcher; 97 98 101 public IPAddressModerationAdminPlugin() { 102 } 103 104 109 public String getDisplayName() { 110 return "IP Address Moderation plugin"; 111 } 112 113 118 public String getInitialPage() { 119 return EDIT_IP_MODERATION_SETTINGS_PAGE; 120 } 121 122 127 public void setFetcher(Fetcher fetcher) { 128 _fetcher = fetcher; 129 } 130 131 137 public void init() throws PluginException { 138 super.init(); 139 } 140 141 152 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 153 entries = super.process(httpServletRequest, httpServletResponse, blog, context, entries); 154 155 String page = BlojsomUtils.getRequestValue(BlojsomConstants.PAGE_PARAM, httpServletRequest); 156 157 String username = getUsernameFromSession(httpServletRequest, blog); 158 if (!checkPermission(blog, null, username, IP_MODERATION_PERMISSION)) { 159 httpServletRequest.setAttribute(BlojsomConstants.PAGE_PARAM, ADMIN_ADMINISTRATION_PAGE); 160 addOperationResultMessage(context, getAdminResource(FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY, FAILED_IP_ADDRESS_MODERATION_PERMISSION_KEY, blog.getBlogAdministrationLocale())); 161 162 return entries; 163 } 164 165 if (ADMIN_LOGIN_PAGE.equals(page)) { 166 return entries; 167 } else { 168 String action = BlojsomUtils.getRequestValue(ACTION_PARAM, httpServletRequest); 169 List ipAddressesFromBlacklist = loadIPList(blog, IP_BLACKLIST_IP); 170 List ipAddressesFromWhitelist = loadIPList(blog, IP_WHITELIST_IP); 171 String listType = BlojsomUtils.getRequestValue(LIST_TYPE, httpServletRequest); 172 173 if (ADD_IP_ADDRESS_ACTION.equals(action)) { 174 String ipAddress = BlojsomUtils.getRequestValue(IP_ADDRESS, httpServletRequest); 175 176 if (BLACKLIST.equals(listType)) { 177 if (!ipAddressesFromBlacklist.contains(ipAddress)) { 178 ipAddressesFromBlacklist.add(ipAddress); 179 blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils.listToString(ipAddressesFromBlacklist, "\n")); 180 try { 181 _fetcher.saveBlog(blog); 182 } catch (FetcherException e) { 183 if (_logger.isErrorEnabled()) { 184 _logger.error(e); 185 } 186 } 187 addOperationResultMessage(context, formatAdminResource(ADDED_IP_TO_BLACKLIST_KEY, ADDED_IP_TO_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {ipAddress})); 188 } else { 189 addOperationResultMessage(context, formatAdminResource(IP_ALREADY_ADDED_TO_BLACKLIST_KEY, IP_ALREADY_ADDED_TO_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {ipAddress})); 190 } 191 } else { 192 if (!ipAddressesFromWhitelist.contains(ipAddress)) { 193 ipAddressesFromWhitelist.add(ipAddress); 194 blog.setProperty(IP_WHITELIST_IP, BlojsomUtils.listToString(ipAddressesFromWhitelist, "\n")); 195 try { 196 _fetcher.saveBlog(blog); 197 } catch (FetcherException e) { 198 if (_logger.isErrorEnabled()) { 199 _logger.error(e); 200 } 201 } 202 addOperationResultMessage(context, formatAdminResource(ADDED_IP_TO_WHITELIST_KEY, ADDED_IP_TO_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {ipAddress})); 203 } else { 204 addOperationResultMessage(context, formatAdminResource(IP_ALREADY_ADDED_TO_WHITELIST_KEY, IP_ALREADY_ADDED_TO_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {ipAddress})); 205 } 206 } 207 } else if (DELETE_IP_ADDRESS_ACTION.equals(action)) { 208 String [] ipAddressesToDelete = httpServletRequest.getParameterValues(IP_ADDRESS); 209 210 if (ipAddressesToDelete != null && ipAddressesToDelete.length > 0) { 211 if (BLACKLIST.equals(listType)) { 212 for (int i = 0; i < ipAddressesToDelete.length; i++) { 213 ipAddressesFromBlacklist.set(Integer.parseInt(ipAddressesToDelete[i]), null); 214 } 215 216 ipAddressesFromBlacklist = BlojsomUtils.removeNullValues(ipAddressesFromBlacklist); 217 blog.setProperty(IP_BLACKLIST_IP, BlojsomUtils.listToString(ipAddressesFromBlacklist, "\n")); 218 try { 219 _fetcher.saveBlog(blog); 220 } catch (FetcherException e) { 221 if (_logger.isErrorEnabled()) { 222 _logger.error(e); 223 } 224 } 225 addOperationResultMessage(context, formatAdminResource(DELETED_IP_ADDRESSES_BLACKLIST_KEY, DELETED_IP_ADDRESSES_BLACKLIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {new Integer (ipAddressesToDelete.length)})); 226 } else { 227 for (int i = 0; i < ipAddressesToDelete.length; i++) { 228 ipAddressesFromWhitelist.set(Integer.parseInt(ipAddressesToDelete[i]), null); 229 } 230 231 ipAddressesFromWhitelist = BlojsomUtils.removeNullValues(ipAddressesFromWhitelist); 232 blog.setProperty(IP_WHITELIST_IP, BlojsomUtils.listToString(ipAddressesFromWhitelist, "\n")); 233 try { 234 _fetcher.saveBlog(blog); 235 } catch (FetcherException e) { 236 if (_logger.isErrorEnabled()) { 237 _logger.error(e); 238 } 239 } 240 addOperationResultMessage(context, formatAdminResource(DELETED_IP_ADDRESSES_WHITELIST_KEY, DELETED_IP_ADDRESSES_WHITELIST_KEY, blog.getBlogAdministrationLocale(), new Object [] {new Integer (ipAddressesToDelete.length)})); 241 } 242 } else { 243 addOperationResultMessage(context, getAdminResource(NO_IP_ADDRESSES_SELECTED_KEY, NO_IP_ADDRESSES_SELECTED_KEY, blog.getBlogAdministrationLocale())); 244 } 245 } 246 247 context.put(BLOJSOM_PLUGIN_IP_BLACKLIST, ipAddressesFromBlacklist); 248 context.put(BLOJSOM_PLUGIN_IP_WHITELIST, ipAddressesFromWhitelist); 249 } 250 251 return entries; 252 } 253 254 261 protected List loadIPList(Blog blog, String property) { 262 ArrayList ipAddresses = new ArrayList (); 263 String ipAddressValues = blog.getProperty(property); 264 265 if (!BlojsomUtils.checkNullOrBlank(ipAddressValues)) { 266 try { 267 BufferedReader br = new BufferedReader (new StringReader (ipAddressValues)); 268 String ipAddress; 269 270 while ((ipAddress = br.readLine()) != null) { 271 ipAddresses.add(ipAddress); 272 } 273 274 br.close(); 275 } catch (IOException e) { 276 if (_logger.isErrorEnabled()) { 277 _logger.error(e); 278 } 279 } 280 } 281 282 return ipAddresses; 283 } 284 } | Popular Tags |