-
-
Save michaeltyson/08b924f5ea4ea0c9cc14 to your computer and use it in GitHub Desktop.
"Regex Spam Catcher" Vanilla plugin, take 1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if (!defined('APPLICATION')) exit(); | |
/** | |
* @copyright Copyright 2013 Michael Tyson | |
* @license GPL | |
*/ | |
// Define the plugin: | |
$PluginInfo['RegexSpamCatcher'] = array( | |
'Name' => 'Regex Spam Catcher', | |
'Description' => 'Regular expression-based spam protection for Vanilla.', | |
'Version' => '1.0', | |
'RequiredApplications' => array('Vanilla' => '2.0.18a1'), | |
'SettingsUrl' => '/dashboard/settings/regexspamcatcher', | |
'SettingsPermission' => 'Garden.Settings.Manage', | |
'Author' => 'Michael Tyson', | |
'AuthorEmail' => 'michael@atastypixel.com', | |
'AuthorUrl' => 'http://atastypixel.com/blog' | |
); | |
define('REGEX_SPAM_CATCHER_DEFAULT_REGEX', '(visit |review |here is |surf |surf to |stop by |check out |look at |\n|^)my (site|blog|website|web-site|web site|page|webpage|web page|web-page|web blog|weblog|homepage)'); | |
class RegexSpamCatcherPlugin extends Gdn_Plugin { | |
public function Setup() { | |
// Get a user for operations. | |
$UserID = Gdn::SQL()->GetWhere('User', array('Name' => 'RegexSpamCatcher', 'Admin' => 2))->Value('UserID'); | |
if (!$UserID) { | |
$UserID = Gdn::SQL()->Insert('User', array( | |
'Name' => 'RegexSpamCatcher', | |
'Password' => RandomString('20'), | |
'HashMethod' => 'Random', | |
'Email' => 'RegexSpamCatcher@domain.com', | |
'DateInserted' => Gdn_Format::ToDateTime(), | |
'Admin' => '2' | |
)); | |
} | |
SaveToConfig('Plugins.RegexSpamCatcher.UserID', $UserID); | |
} | |
public function UserID() { | |
return C('Plugins.RegexSpamCatcher.UserID', NULL); | |
} | |
public function Base_CheckSpam_Handler($Sender, $Args) { | |
if ( $Args['IsSpam'] ) { | |
return; // don't double check | |
} | |
switch ( $Args['RecordType'] ) { | |
case 'Comment': | |
case 'Discussion': | |
case 'Activity': | |
case 'ActivityComment': | |
$Regex = '/('.str_replace('/', '\/', join(')|(', explode("\n", C('Plugins.RegexSpamCatcher.Regex', REGEX_SPAM_CATCHER_DEFAULT_REGEX)))).')/i'; | |
$Data =& $Args['Data']; | |
$Body = ConcatSep("\n\n", GetValue('Name', $Data), GetValue('Body', $Data), GetValue('Story', $Data)); | |
if ( preg_match($Regex, $Body) ) { | |
$Sender->EventArguments['IsSpam'] = TRUE; | |
$Data['Log_InsertUserID'] = $this->UserID(); | |
$Data['RecordIPAddress'] = Gdn::Request()->IpAddress(); | |
} | |
break; | |
default: | |
break; | |
} | |
} | |
public function SettingsController_RegexSpamCatcher_Create($Sender, $Args = array()) { | |
$Sender->Permission('Garden.Settings.Manage'); | |
$Sender->SetData('Title', T('Regex Spam Catcher Settings')); | |
$Cf = new ConfigurationModule($Sender); | |
$Cf->Initialize(array( | |
'Plugins.RegexSpamCatcher.Regex' => array( | |
'Description' => 'Enter a regular expression to use for filtering content', | |
'Control' => 'TextBox', | |
'Options' => array('style' => 'width: 100%'), | |
'Default' => REGEX_SPAM_CATCHER_DEFAULT_REGEX), | |
)); | |
$Sender->AddSideMenu('dashboard/settings/plugins'); | |
$Cf->RenderAll(); | |
} | |
public function Base_GetAppSettingsMenuItems_Handler($Sender) { | |
$Menu = $Sender->EventArguments['SideMenu']; | |
$Menu->AddLink('Moderation', 'Regex Spam Settings', 'dashboard/settings/regexspamcatcher', 'Plugins.Settings.Manage'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment