private string ExecuteValidateRequest(CommentCheckContext context, string action) {
            var uri = String.Format(AkismetApiPattern, _apiKey, _endpoint, action);

            WebRequest request = WebRequest.Create(uri);
            request.Method = "POST";
            request.Timeout = 5000; //milliseconds
            request.ContentType = "application/x-www-form-urlencoded";

            var postData = "blog=" + HttpUtility.UrlEncode(context.Url)
                           + "&user_ip=" + HttpUtility.UrlEncode(context.UserIp)
                           + "&user_agent=" + HttpUtility.UrlEncode(context.UserAgent)
                           + "&referrer=" + HttpUtility.UrlEncode(context.Referrer)
                           + "&permalink=" + HttpUtility.UrlEncode(context.Permalink)
                           + "&comment_type=" + HttpUtility.UrlEncode(context.CommentType)
                           + "&comment_author=" + HttpUtility.UrlEncode(context.CommentAuthor)
                           + "&comment_author_email=" + HttpUtility.UrlEncode(context.CommentAuthorEmail)
                           + "&comment_author_url=" + HttpUtility.UrlEncode(context.CommentAuthorUrl)
                           + "&comment_content=" + HttpUtility.UrlEncode(context.CommentContent);

            byte[] content = Encoding.UTF8.GetBytes(postData);
            using (Stream stream = request.GetRequestStream()) {
                stream.Write(content, 0, content.Length);
            }
            using (WebResponse webResponse = request.GetResponse()) {
                using (var reader = new StreamReader(webResponse.GetResponseStream())) {
                    return reader.ReadToEnd();
                }
            }
        }
 public void ReportHam(CommentCheckContext context) {
     try {
         var result = ExecuteValidateRequest(context, "submit-ham");
     }
     catch (Exception e) {
         Logger.Error(e, "An error occured while reporting ham");
     }
 }
        public SpamStatus CheckForSpam(CommentCheckContext context) {
            try {
                var result = ExecuteValidateRequest(context, "comment-check");

                if (HandleValidateResponse(_context, result)) {
                    return SpamStatus.Spam;
                }

                return SpamStatus.Ham;
            }
            catch(Exception e) {
                Logger.Error(e, "An error occured while checking for spam");
                return SpamStatus.Spam;
            }
        }
Beispiel #4
0
        public SpamStatus CheckForSpam(CommentCheckContext context, SpamFilterAction action, IContent content) {

            if (string.IsNullOrWhiteSpace(context.CommentContent)) {
                return SpamStatus.Ham;
            }

            var spamFilters = GetSpamFilters().ToList();

            var result = SpamStatus.Ham;

            switch (action) {
                case SpamFilterAction.AllOrNothing:
                    if (spamFilters.All(x => x.CheckForSpam(context) == SpamStatus.Spam)) {
                        result = SpamStatus.Spam;
                    }

                    break;
                case SpamFilterAction.One:
                    if (spamFilters.Any(x => x.CheckForSpam(context) == SpamStatus.Spam)) {
                        result =  SpamStatus.Spam;
                    }

                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            // trigger events and rules
            switch (result) {
                case SpamStatus.Spam:
                    _spamEventHandler.SpamReported(content);
                    _rulesManager.TriggerEvent("AntiSpam", "Spam", () => new Dictionary<string, object> { { "Content", content } });
                    break;
                case SpamStatus.Ham:
                    _spamEventHandler.HamReported(content);
                    _rulesManager.TriggerEvent("AntiSpam", "Ham", () => new Dictionary<string, object> { { "Content", content } });
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return result;
        }
Beispiel #5
0
        public void ReportSpam(CommentCheckContext context) {
            var spamFilters = GetSpamFilters().ToList();

            foreach(var filter in spamFilters) {
                filter.ReportSpam(context);
            }
        }
Beispiel #6
0
        private CommentCheckContext CreateCommentCheckContext(SpamFilterPart part, WorkContext workContext) {
            var settings = part.TypePartDefinition.Settings.GetModel<SpamFilterPartSettings>();

            var data = new Dictionary<string, object> {{"Content", part.ContentItem}};

            var context = new CommentCheckContext {
                Url = _tokenizer.Replace(settings.UrlPattern, data),
                Permalink = _tokenizer.Replace(settings.PermalinkPattern, data),
                CommentAuthor = _tokenizer.Replace(settings.CommentAuthorPattern, data),
                CommentAuthorEmail = _tokenizer.Replace(settings.CommentAuthorEmailPattern, data),
                CommentAuthorUrl = _tokenizer.Replace(settings.CommentAuthorUrlPattern, data),
                CommentContent = _tokenizer.Replace(settings.CommentContentPattern, data),
            };

            if(workContext.HttpContext != null) {
                context.UserIp = workContext.HttpContext.Request.ServerVariables["REMOTE_ADDR"];
                context.UserAgent = workContext.HttpContext.Request.UserAgent;
                context.Referrer = Convert.ToString(workContext.HttpContext.Request.UrlReferrer);
            }

            return context;
        }