/**
  * Processes incoming requests
  *
  * @return A positive PipelineResult object is the comment is not spam or a negative
  * one if it is.
  */
 function filter()
 {
     $config =& Config::getConfig();
     if (!$config->getValue("bayesian_filter_enabled")) {
         return new PipelineResult(true);
     }
     // get some info
     $blogInfo = $this->_pipelineRequest->getBlogInfo();
     $request = $this->_pipelineRequest->getHttpRequest();
     // we only have to filter the contents if the user is posting a comment
     // so there's no point in doing anything else if that's not the case
     if ($request->getValue("op") != "AddComment") {
         $result = new PipelineResult();
         return $result;
     }
     // text and topic of the comment
     $commentText = $request->getValue("commentText");
     $commentTopic = $request->getValue("commentTopic");
     $userName = $request->getValue("userName");
     $userEmail = $request->getValue("userEmail");
     $userUrl = $request->getValue("userUrl");
     $articleId = $request->getValue("articleId");
     $parentId = $request->getValue("parentId");
     if ($parentId == "") {
         $parentId = 0;
     }
     $spamicity = $this->getSpamProbability($blogInfo->getId(), $commentTopic, $commentText, $userName, $userEmail, $userUrl);
     if ($spamicity >= $config->getValue("bayesian_filter_spam_probability_treshold")) {
         $result = new PipelineResult(false, HIGH_SPAM_PROBABILITY, "You cannot post this message. Anti-spam filter has blocked it.");
         // now we need to check what we have to do with this comment... either throw it away
         // or keep it in the database
         // this piece of code shouldn't really go here, but it's easier than letting
         // the AddComment action that there was actually a comment and that it should
         // still be added but marked as spam and so on... sometimes breaking a few
         // rules makes things easier :)
         if ($config->getValue("bayesian_filter_spam_comments_action") == BAYESIAN_FILTER_KEEP_COMMENT_ACTION) {
             $comments = new ArticleComments();
             $clientIp = Client::getIp();
             $comment = new UserComment($articleId, $parentId, $commentTopic, $commentText, null, $userName, $userEmail, $userUrl, $clientIp, 0, COMMENT_STATUS_SPAM);
             $comments->addComment($comment);
         } else {
             // nothing to do here, simply throw the comment away
         }
         $spam = true;
     } else {
         $result = new PipelineResult(true);
         $spam = false;
     }
     // train the filter with the message, be it spam or not...
     BayesianFilterCore::train($blogInfo->getId(), $commentTopic, $commentText, $userName, $userEmail, $userUrl, $spam);
     //print "<h1>" . number_format($spamicity * 100, 0) . "% of spamicity</h1>";
     return $result;
 }
 function filter()
 {
     // get all the hosts that have been blacklisted
     // by this blog
     $blogInfo = $this->_pipelineRequest->getBlogInfo();
     $request = $this->_pipelineRequest->getHttpRequest();
     // check if this section has been enabled or disabled
     $blogSettings = $blogInfo->getSettings();
     $pluginEnabled = $blogSettings->getValue("plugin_hostblock_enabled");
     if (!$pluginEnabled) {
         // if not, nothing to do here...
         //_debug("ip address filter not enabled! quitting...<br/>");
         return new PipelineResult();
     }
     // get the list of blocked hosts for this blog
     $blockedHosts = new BlockedHosts();
     $hostsAccessBlocked = $blockedHosts->getBlogBlacklist($blogInfo->getId(), BLOCK_ACCESS, true);
     $hostsPostCommentBlocked = $blockedHosts->getBlogBlacklist($blogInfo->getId(), BLOCK_COMMENT_POSTING, true);
     // and now check one by one, comparing with the ip we just got
     $clientIp = Client::getIp();
     //
     // check the hosts that are not even allowed to access
     //
     $ipMatchValidator = new IpMatchValidator();
     foreach ($hostsAccessBlocked as $hostAccessBlocked) {
         if ($ipMatchValidator->validate($clientIp, $hostAccessBlocked->getCidrAddress())) {
             return new PipelineResult(false, HOST_BLACKLIST_BLOCKED_HOST_FOUND, $hostAccessBlocked->getReason());
         }
     }
     //
     // and now if we're posting a comment, check the ips
     //
     if ($request->getValue("op") == "AddComment") {
         foreach ($hostsPostCommentBlocked as $hostPostCommendBlocked) {
             if ($ipMatchValidator->validate($clientIp, $hostPostCommendBlocked->getCidrAddress())) {
                 return new PipelineResult(false, HOST_BLACKLIST_COMMENT_BLOCKED_HOST_FOUND, $hostPostCommendBlocked->getReason());
             }
         }
     }
     $result = new PipelineResult();
     return $result;
 }
 /**
  * Carries out the action
  */
 function perform()
 {
     // need to check the ip of the client
     $clientIp = Client::getIp();
     // fetch the same article again so that we can have all the comments from
     // the database, plus this last one
     $articles = new Articles();
     $article = $articles->getBlogArticle($this->_articleId, $this->_blogInfo->getId());
     // check if the user wanted to receive comments for this article
     // or not...
     if ($article->getCommentsEnabled() == false) {
         $this->_view = new ErrorView($this->_blogInfo);
         $this->_view->setValue("message", "Comments have been disabled for this article.");
         $this->setCommonData();
         return false;
     }
     $this->notifyEvent(EVENT_POST_LOADED, array("article" => &$article));
     // we have already checked all the data, so we are sure that everything's in place
     $comments = new ArticleComments();
     $comment = new UserComment($this->_articleId, $this->_parentId, $this->_commentTopic, $this->_commentText, null, $this->_userName, $this->_userEmail, $this->_userUrl, $clientIp);
     // check if there is already a comment with the same text, topic and made from the same
     // IP already in the database because if so, then we will not add the comment that
     // the user is trying to add (a reload button mistake, perhaps?)
     if (!$comments->getIdenticalComment($this->_commentTopic, $this->_commentText, $this->_articleId, $this->_parentId, $this->_userName, $this->_userEmail, $this->_userUrl, $clientIp)) {
         // fire an event
         $this->notifyEvent(EVENT_PRE_COMMENT_ADD, array("comment" => &$comment));
         if (!$comments->addComment($comment)) {
             // show an error message if problems
             $this->_view = new ErrorView($this->_blogInfo);
             $this->_view->setValue("message", "error_adding_comment");
             $this->setCommonData();
             return false;
         }
     }
     // finally, check if there was any user who wanted to be notified of new comments
     // to this post...
     $notifier = new ArticleNotifications();
     $notifier->notifyUsers($article->getId(), $this->_blogInfo);
     // fire the post event...
     $this->notifyEvent(EVENT_POST_COMMENT_ADD, array("comment" => &$comment));
     //
     // clear caches. This should be done in a more granular way, because right now
     // we're either removing *all* of them or none. I guess we should only remove the
     // cache whose identifier corresponds with the blog and article that we just removed,
     // but let's leave it as it is for the time being...
     //
     CacheControl::resetBlogCache($this->_blogInfo->getId());
     // clean up the request, there's a parameter called 'userName' also used by
     // ViewArticleAction but that doesn't have the same meaning so we better remove it
     // before it's too late! We also need to add a new request commentUserName to replace
     // original userName, in case developer need it in filter or event plugin.
     $request = HttpVars::getRequest();
     $request["commentUserName"] = $request["userName"];
     $request["userName"] = "";
     HttpVars::setRequest($request);
     // forward the action to ViewArticleAction
     return BlogController::setForwardAction("ViewArticle");
 }