public function validatePost(PostWillBeSaved $event) { $post = $event->post; if ($post->exists || $post->user->groups()->count()) { return; } $akismet = new Akismet($this->settings->get('akismet.api_key'), Core::url()); $isSpam = $akismet->isSpam($post->content, $post->user->username, $post->user->email, null, 'comment'); if ($isSpam) { $post->hide(); $this->savingPost = $post; CommentPost::saved(function (CommentPost $post) { if ($post !== $this->savingPost) { return; } $report = new Report(); $report->post_id = $post->id; $report->reporter = 'Akismet'; $report->reason = 'spam'; $report->time = time(); $report->save(); $this->savingPost = null; }); } }
public function validatePost(PostWillBeSaved $event) { $post = $event->post; if ($post->exists || $post->user->groups()->count()) { return; } $akismet = new Akismet($this->settings->get('akismet.api_key'), Core::url()); $isSpam = $akismet->isSpam($post->content, $post->user->username, $post->user->email, null, 'comment'); if ($isSpam) { $post->is_approved = false; // TODO: // $post->is_spam = true; $post->afterSave(function ($post) { $flag = new Flag(); $flag->post_id = $post->id; $flag->type = 'akismet'; $flag->time = time(); $flag->save(); }); } }
/** * General method to check if something is spam * * @param string $content The content that was submitted. * @param string $permaLink The permanent location of the entry the comment was submitted to. * @param string $author Commenter's name. * @param string $email Commenter's email address. * @param string $URL Commenter's URL. * @param string $type May be blank, comment, trackback, pingback, or a made up value like "registration". * @return bool|string Will return a boolean, except when we can't decide the status * (unknown will be returned in that case) */ public static function isSpam($content, $permaLink, $author = null, $email = null, $URL = null, $type = 'comment') { // get some settings $akismetKey = self::get('fork.settings')->get('Core', 'akismet_key'); // invalid key, so we can't detect spam if ($akismetKey === '') { return false; } // create new instance $akismet = new Akismet($akismetKey, SITE_URL); // set properties $akismet->setTimeOut(10); $akismet->setUserAgent('Fork CMS/' . FORK_VERSION); // try it, to decide if the item is spam try { // check with Akismet if the item is spam return $akismet->isSpam($content, $author, $email, $URL, $permaLink, $type); } catch (\Exception $e) { // in debug mode we want to see exceptions, otherwise the fallback will be triggered if (self::getContainer()->getParameter('kernel.debug')) { throw $e; } // return unknown status return 'unknown'; } }