001/* 002 * Copyright 2010 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.ametys.cms.repository.comment; 018 019import java.util.ArrayList; 020import java.util.List; 021import java.util.Map; 022import java.util.regex.Pattern; 023 024import org.apache.avalon.framework.logger.AbstractLogEnabled; 025import org.apache.cocoon.environment.ObjectModelHelper; 026import org.apache.cocoon.environment.Request; 027import org.apache.commons.lang.StringUtils; 028 029import org.ametys.cms.repository.Content; 030import org.ametys.cms.repository.comment.actions.AddCommentAction; 031import org.ametys.core.captcha.CaptchaHelper; 032import org.ametys.core.util.mail.SendMailHelper; 033import org.ametys.runtime.config.Config; 034import org.ametys.runtime.i18n.I18nizableText; 035 036/** 037 * Handle all the listeners for the add comment action 038 */ 039public class DefaultCommentManagerExtensionPoint extends AbstractLogEnabled implements CommentManagerExtensionPoint 040{ 041 /** The pattern to check url */ 042 public static final Pattern URL_VALIDATOR = Pattern.compile("^(https?:\\/\\/.+)?$"); 043 044 @Override 045 public boolean isValidatedByDefault(Content content) 046 { 047 boolean postValidation = Config.getInstance().getValue("cms.contents.comments.postvalidation"); 048 return postValidation; 049 } 050 051 @Override 052 public boolean isCaptchaRequired(Content content, Map objectModel) 053 { 054 return false; 055 } 056 057 /** 058 * Call the validityCheck method on all listeners and add the results 059 * @param content The content to comment 060 * @param objectModel The cocoon object model 061 * @return An list of error messages (empty if no errors) 062 */ 063 @Override 064 public List<I18nizableText> getErrors(CommentableContent content, Map objectModel) 065 { 066 Request request = ObjectModelHelper.getRequest(objectModel); 067 068 List<I18nizableText> errors = new ArrayList<>(); 069 070 if (StringUtils.isBlank(request.getParameter(AddCommentAction.PARAMETER_AUTHOR_NAME))) 071 { 072 errors.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_ADD_ERROR_NAME")); 073 } 074 075 if (!SendMailHelper.EMAIL_VALIDATION.matcher(StringUtils.trimToEmpty(request.getParameter(AddCommentAction.PARAMETER_AUTHOR_EMAIL))).matches()) 076 { 077 errors.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_ADD_ERROR_EMAIL")); 078 } 079 080 if (!URL_VALIDATOR.matcher(StringUtils.trimToEmpty(request.getParameter(AddCommentAction.PARAMETER_AUTHOR_URL))).matches()) 081 { 082 errors.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_ADD_ERROR_URL")); 083 } 084 085 if (StringUtils.isBlank(request.getParameter(AddCommentAction.PARAMETER_CONTENTTEXT))) 086 { 087 errors.add(new I18nizableText("plugin.cms", "PLUGINS_CMS_CONTENT_COMMENTS_ADD_ERROR_CONTENT")); 088 } 089 090 if (isCaptchaRequired(content, objectModel)) 091 { 092 String captchaKey = request.getParameter(AddCommentAction.PARAMETER_CAPTCHA_KEY); 093 String captchaValue = request.getParameter(AddCommentAction.PARAMETER_CAPTCHA_VALUE); 094 if (!CaptchaHelper.checkAndInvalidate(captchaKey, captchaValue)) 095 { 096 errors.add(new I18nizableText("plugin.cms", "plugin.cms:PLUGINS_CMS_CONTENT_COMMENTS_ADD_ERROR_CAPTCHA")); 097 } 098 } 099 100 return errors; 101 } 102}