1 31 package org.blojsom.plugin.comment; 32 33 import org.blojsom.blog.Blog; 34 import org.blojsom.blog.Entry; 35 import org.blojsom.plugin.PluginException; 36 import org.blojsom.plugin.comment.event.CommentResponseSubmissionEvent; 37 import org.blojsom.util.BlojsomUtils; 38 import org.blojsom.event.Listener; 39 import org.blojsom.event.Event; 40 import org.blojsom.event.EventBroadcaster; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 44 import javax.servlet.http.HttpServletRequest ; 45 import javax.servlet.http.HttpServletResponse ; 46 import javax.servlet.http.HttpSession ; 47 import java.util.HashMap ; 48 import java.util.Map ; 49 import java.util.Iterator ; 50 51 58 public class MathCommentAuthenticationPlugin extends CommentModerationPlugin implements Listener { 59 60 private Log _logger = LogFactory.getLog(MathCommentAuthenticationPlugin.class); 61 62 private static final String MATH_COMMENT_MODERATION_ENABLED = "math-comment-moderation-enabled"; 63 private static final String MATH_COMMENT_AUTHENTICATION_OPERATIONS_IP = "math-comment-authentication-operations"; 64 private static final String MATH_COMMENT_AUTHENTICATION_BOUND_IP = "math-comment-authentication-bound"; 65 66 private static final int AVAILABLE_OPERATIONS = 3; 67 private static final int BOUND_DEFAULT = 10; 68 69 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER"; 70 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1 = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1"; 71 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2 = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2"; 72 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION"; 73 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER_CHECK_PARAM = "mathAnswerCheck"; 74 public static final String BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE = "BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE"; 75 76 private EventBroadcaster _eventBroadcaster; 77 78 81 public MathCommentAuthenticationPlugin() { 82 } 83 84 89 public void setEventBroadcaster(EventBroadcaster eventBroadcaster) { 90 _eventBroadcaster = eventBroadcaster; 91 } 92 93 98 public void init() throws PluginException { 99 super.init(); 100 101 _eventBroadcaster.addListener(this); 102 } 103 104 115 public Entry[] process(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 116 HttpSession httpSession = httpServletRequest.getSession(); 117 118 int bound = BOUND_DEFAULT; 119 int availableOperations = AVAILABLE_OPERATIONS; 120 String boundProperty = blog.getProperty(MATH_COMMENT_AUTHENTICATION_BOUND_IP); 121 String availableOperationsProperty = blog.getProperty(MATH_COMMENT_AUTHENTICATION_OPERATIONS_IP); 122 123 if (!BlojsomUtils.checkNullOrBlank(boundProperty)) { 124 try { 125 bound = Integer.parseInt(boundProperty); 126 } catch (NumberFormatException e) { 127 } 128 } 129 130 if (!BlojsomUtils.checkNullOrBlank(availableOperationsProperty)) { 131 try { 132 availableOperations = Integer.parseInt(availableOperationsProperty); 133 if (availableOperations < 1 || availableOperations > AVAILABLE_OPERATIONS) { 134 availableOperations = AVAILABLE_OPERATIONS; 135 } else { 136 availableOperations -= 1; 137 } 138 } catch (NumberFormatException e) { 139 } 140 } 141 142 int operation = (int) (Math.random() * (availableOperations + 1)); 143 int value1 = (int) (Math.random() * bound); 144 int value2 = (int) (Math.random() * bound); 145 int answer; 146 147 answer = getAnswerForOperation(value1, value2, operation); 148 149 httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE1, new Integer (value1)); 150 httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_VALUE2, new Integer (value2)); 151 httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER, new Integer (answer)); 152 httpSession.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_OPERATION, getOperatorForOperation(operation)); 153 154 return entries; 155 } 156 157 168 protected void moderateComment(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Blog blog, Map context, Entry[] entries) throws PluginException { 169 if ("true".equalsIgnoreCase(blog.getProperty(COMMENT_MODERATION_ENABLED)) && 170 "true".equalsIgnoreCase(blog.getProperty(MATH_COMMENT_MODERATION_ENABLED))) { 171 HttpSession httpSession = httpServletRequest.getSession(); 172 173 if ("y".equalsIgnoreCase(httpServletRequest.getParameter(CommentPlugin.COMMENT_PARAM)) && blog.getBlogCommentsEnabled().booleanValue()) { 174 String mathAnswerCheck = BlojsomUtils.getRequestValue(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER_CHECK_PARAM, httpServletRequest); 175 176 boolean passedCheck = false; 177 if (httpSession.getAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER) != null) { 178 Integer mathAnswer = (Integer ) httpSession.getAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_ANSWER); 179 180 try { 181 int mathAnswerCheckValue = Integer.parseInt(mathAnswerCheck); 182 int originalMathAnswerValue = mathAnswer.intValue(); 183 184 if (mathAnswerCheckValue == originalMathAnswerValue) { 185 passedCheck = true; 186 } 187 } catch (NumberFormatException e) { 188 } 189 } 190 191 Map commentMetaData; 192 if (context.containsKey(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA)) { 193 commentMetaData = (Map ) context.get(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA); 194 } else { 195 commentMetaData = new HashMap (); 196 } 197 198 if (!passedCheck) { 199 commentMetaData.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA_DESTROY, Boolean.TRUE); 200 httpServletRequest.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE, "Failed math comment authentication check."); 201 } else { 202 commentMetaData.put(CommentModerationPlugin.BLOJSOM_COMMENT_MODERATION_PLUGIN_APPROVED, Boolean.TRUE.toString()); 203 httpServletRequest.setAttribute(BLOJSOM_MATH_AUTHENTICATOR_PLUGIN_STATUS_MESSAGE, "Passed math comment authentication check."); 204 } 205 206 context.put(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA, commentMetaData); 207 } 208 } 209 } 210 211 219 protected int getAnswerForOperation(int value1, int value2, int operation) { 220 int answer; 221 222 switch (operation) { 223 case 0: 224 { 225 answer = value1 + value2; 226 break; 227 } 228 case 1: 229 { 230 answer = value1 - value2; 231 break; 232 } 233 case 2: 234 { 235 answer = value1 * value2; 236 break; 237 } 238 default: 239 { 240 answer = value1 + value2; 241 } 242 } 243 244 return answer; 245 } 246 247 253 protected String getOperatorForOperation(int operation) { 254 switch (operation) { 255 case 0: 256 { 257 return "+"; 258 } 259 case 1: 260 { 261 return "-"; 262 } 263 case 2: 264 { 265 return "*"; 266 } 267 default: 268 { 269 return "+"; 270 } 271 } 272 } 273 274 279 public void handleEvent(Event event) { 280 } 281 282 287 public void processEvent(Event event) { 288 if (event instanceof CommentResponseSubmissionEvent) { 289 CommentResponseSubmissionEvent commentResponseSubmissionEvent = (CommentResponseSubmissionEvent) event; 290 291 try { 292 HashMap context = new HashMap (); 293 moderateComment(commentResponseSubmissionEvent.getHttpServletRequest(), 294 commentResponseSubmissionEvent.getHttpServletResponse(), commentResponseSubmissionEvent.getBlog(), 295 context, 296 new Entry[] {commentResponseSubmissionEvent.getEntry()}); 297 298 Map operationMetadata = (Map ) context.get(CommentPlugin.BLOJSOM_PLUGIN_COMMENT_METADATA); 300 Map commentMetadata = commentResponseSubmissionEvent.getMetaData(); 301 302 Iterator keys = operationMetadata.keySet().iterator(); 303 while (keys.hasNext()) { 304 Object key = keys.next(); 305 commentMetadata.put(key.toString(), operationMetadata.get(key).toString()); 306 } 307 } catch (PluginException e) { 308 if (_logger.isErrorEnabled()) { 309 _logger.error(e); 310 } 311 } 312 } 313 } 314 } | Popular Tags |