/**
  * Decorate the Member object to ghost the user if suspected of being a spammer.
  */
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     // we only want to run this if any of the fields to check have been modified.
     $shouldRun = false;
     foreach ($this->owner->config()->fields_to_check as $field) {
         if ($this->owner->isChanged($field)) {
             $shouldRun = true;
             break;
         }
     }
     if (!$shouldRun) {
         return;
     }
     $spam_needles = SuspendSpammerKeyword::get();
     if ($spam_needles) {
         $spam_needles = $spam_needles->map()->toArray();
     } else {
         $spam_needles = array();
     }
     //if anything matches do something about it to stop the spam registration.
     if (0 < count(array_intersect($this->spamHaystack(), $spam_needles))) {
         //Ghost a spammer.
         $this->owner->ForumStatus = 'Ghost';
         //Email the admin
         if (Config::inst()->get('SuspendSpammerEmail', 'enable_email') && !$this->owner->ID) {
             $body = "<h1>Suspected Spammer Registration</h1>\n\t\t\t\t<ul>\n\t\t\t\t<li>Email: " . $this->owner->Email . "</li>";
             foreach ($this->owner->config()->fields_to_check as $field) {
                 $body .= "<li>" . $field . ": " . $this->owner->{$field} . "</li>";
             }
             $body .= "</ul>";
             SuspendSpammerEmail::create($body)->send();
         }
     }
 }
 public function beforePostMessage($data, $member = null)
 {
     if (!$member) {
         $member = Member::currentUser();
     }
     //If member has CMS access they don't need to be checked out.
     if (Permission::check('CMS_ACCESS_CMSMain', 'any', $member)) {
         return;
     }
     //Is this the Members first message and they are not already a ghost or banned.
     if ($member->ForumStatus == 'Normal' && $member->NumPosts() == 0) {
         $content = $data['Title'] . ' ' . $data['Content'];
         //Check title and content for spam keywords.
         $spam_words = SuspendSpammerKeyword::get();
         //ensure it returns as an array.
         if ($spam_words->Exists()) {
             $spam_words = $spam_words->map()->toArray();
             $matches = array();
             //Check words for known spammer content
             $matchFound = preg_match_all("/\\b(" . implode($spam_words, "|") . ")\\b/i", $content, $matches);
             //@TODO do phone number pattern recognittion in the future as this is a
             //common indication of the human spammers this module is design to counter.
             if ($matchFound) {
                 //If true, ghost the member.
                 $member->ForumStatus = 'Ghost';
                 $member->write();
                 //Email the admin
                 if (Config::inst()->get('SuspendSpammerEmail', 'enable_email')) {
                     $body = "<h1>Suspected Spammer Post</h1>\n\t\t\t\t\t\t\t<p>Email: " . $member->Email . "</p>\n\t\t\t\t\t\t\t<p>" . $content . "</p>";
                     SuspendSpammerEmail::create($body)->send();
                 }
             }
         }
     }
 }
 public function requireDefaultRecords()
 {
     parent::requireDefaultRecords();
     //Ensure at least 1 spam keyword exists.
     if (!SuspendSpammerKeyword::get()->Exists()) {
         $keyword = SuspendSpammerKeyword::create();
         $keyword->Title = 'astrologer';
         $keyword->write();
     }
 }