[pLog-svn] r3252 - in plugins/trunk: . 0ipabuse 0ipabuse/class 0ipabuse/class/action 0ipabuse/class/security 0ipabuse/class/view 0ipabuse/locale 0ipabuse/templates

pwestbro at devel.lifetype.net pwestbro at devel.lifetype.net
Sun Apr 16 04:07:41 GMT 2006


Author: pwestbro
Date: 2006-04-16 04:07:39 +0000 (Sun, 16 Apr 2006)
New Revision: 3252

Added:
   plugins/trunk/0ipabuse/
   plugins/trunk/0ipabuse/class/
   plugins/trunk/0ipabuse/class/action/
   plugins/trunk/0ipabuse/class/action/pluginipabuseconfigaction.class.php
   plugins/trunk/0ipabuse/class/action/pluginipabuseupdateconfigaction.class.php
   plugins/trunk/0ipabuse/class/security/
   plugins/trunk/0ipabuse/class/security/ipabusefilter.class.php
   plugins/trunk/0ipabuse/class/view/
   plugins/trunk/0ipabuse/class/view/pluginipabuseconfigview.class.php
   plugins/trunk/0ipabuse/locale/
   plugins/trunk/0ipabuse/locale/locale_en_UK.php
   plugins/trunk/0ipabuse/plugin0ipabuse.class.php
   plugins/trunk/0ipabuse/templates/
   plugins/trunk/0ipabuse/templates/ipabuse.template
Log:
 Added the first version of the ip abuse plugin.  This plugin keeps a count
 of the number of attempted comment posts for each ip address for a day.  If
 a comment comes in from an ip address that has more than the max number
 (currently hardcode to 10), it will be rejected. 

This plugin uses a db4 database, so it requires this support in php.  I used
db4 to not add additional load on the mysql database.

I wanted this plugin because during the comment spam flood my web server was
getting overwhelmed.  I wanted a plugin that could prevent some of this.

This plugin was named with a 0, to make sure that it runs first.  (It would
be great to have a different mechanism to specify the order of the pipeline
filters.


There are some things that I think need to be done:
1) Write readme
2) Allow the max number of comments for a single ip address to be configured
3) change this filter to handle trackbacks
4) Add a exception list in the admin interface where the ip addresses listed
would be exempt from this filter


Added: plugins/trunk/0ipabuse/class/action/pluginipabuseconfigaction.class.php
===================================================================
--- plugins/trunk/0ipabuse/class/action/pluginipabuseconfigaction.class.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/class/action/pluginipabuseconfigaction.class.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,26 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
+	include_once( PLOG_CLASS_PATH."plugins/0ipabuse/class/view/pluginipabuseconfigview.class.php" );
+
+	/**
+	 * shows a form with the current configuration
+	 */
+	class PluginIPAbuseConfigAction extends AdminAction
+	{
+		
+		function PluginIPAbuseConfigAction( $actionInfo, $request )
+		{
+			$this->AdminAction( $actionInfo, $request );
+		}
+		
+		function perform()
+		{
+            $this->_view = new PluginIPAbuseConfigView( $this->_blogInfo );
+			
+			$this->setCommonData();
+			
+			return true;
+		}
+	}
+?>
\ No newline at end of file

Added: plugins/trunk/0ipabuse/class/action/pluginipabuseupdateconfigaction.class.php
===================================================================
--- plugins/trunk/0ipabuse/class/action/pluginipabuseupdateconfigaction.class.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/class/action/pluginipabuseupdateconfigaction.class.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,58 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/action/admin/adminaction.class.php" );
+	include_once( PLOG_CLASS_PATH."plugins/0ipabuse/class/view/pluginipabuseconfigview.class.php" );
+		
+	/**
+	 * updates the plugin configuration
+	 */
+	class PluginIPAbuseUpdateConfigAction extends AdminAction
+	{
+		var $_pluginEnabled;
+		
+		function PluginIPAbuseUpdateConfigAction( $actionInfo, $request )
+		{
+			$this->AdminAction( $actionInfo, $request );
+		}
+		
+		function validate()
+		{
+            $this->_pluginEnabled = $this->_request->getValue( "pluginEnabled" );
+            $this->_pluginEnabled = ($this->_pluginEnabled != "" );			
+			
+			return true;
+		}
+		        
+		function perform()
+		{
+            // update the plugin configurations to blog setting
+			$blogSettings = $this->_blogInfo->getSettings();
+            $blogSettings->setValue( "plugin_ipabuse_enabled", $this->_pluginEnabled );
+            $this->_blogInfo->setSettings( $blogSettings ); 
+		
+			// save the blogs settings
+			$blogs = new Blogs();
+            if( !$blogs->updateBlog( $this->_blogInfo )) {
+                $this->_view = new PluginIPAbuseConfigView( $this->_blogInfo );
+                $this->_view->setErrorMessage( $this->_locale->tr("error_updating_settings"));
+                $this->setCommonData();
+
+                return false;                       
+            }
+			
+			// if everything went ok...
+            $this->_blogInfo->setSettings( $blogSettings );
+            $this->_session->setValue( "blogInfo", $this->_blogInfo );
+            $this->saveSession();
+			
+			$this->_view = new PluginIPAbuseConfigView( $this->_blogInfo );
+			$this->_view->setSuccessMessage( $this->_locale->tr("ipabuse_settings_saved_ok"));			
+			$this->setCommonData();
+			
+			// clear the cache
+			CacheControl::resetBlogCache( $this->_blogInfo->getId());					
+            
+            return true;		
+		}
+	}
+?>
\ No newline at end of file

Added: plugins/trunk/0ipabuse/class/security/ipabusefilter.class.php
===================================================================
--- plugins/trunk/0ipabuse/class/security/ipabusefilter.class.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/class/security/ipabusefilter.class.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,94 @@
+<?php
+
+	include_once( PLOG_CLASS_PATH."class/security/pipelinefilter.class.php" );
+    include_once( PLOG_CLASS_PATH."class/net/client.class.php" );	
+
+    // custom error code that will be returned to the pipeline whenever an
+    // error is found... Be careful so as to not to have two different modules
+    // use the same code!!
+    define( "IP_ABUSE_MATCH_FOUND", 700 );
+
+	class IPAbuseFilter extends PipelineFilter 
+	{
+
+    	function IPAbuseFilter( $pipelineRequest )
+        {
+        	$this->PipelineFilter( $pipelineRequest );
+        }
+
+        function filter()
+        {
+        	// get some info
+            $blogInfo = $this->_pipelineRequest->getBlogInfo();
+            $request  = $this->_pipelineRequest->getHttpRequest();
+
+        	// check if this section has been enabled or disabled
+            $blogSettings = $blogInfo->getSettings();
+		    $pluginEnabled = $blogSettings->getValue( "plugin_ipabuse_enabled" );
+            if( !$pluginEnabled) {
+            	// if not, nothing to do here...
+                //_debug("ip address filter not enabled! quitting...<br/>");
+            	return new PipelineResult();
+            }
+
+            // 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;
+            }
+
+			// if this is already rejected, there is no reason to do anything here
+			// In the future this could submit the up address, and the urls to 
+			// the dnsbl sites.
+			if ( $this->_pipelineRequest->getRejectedState() )
+            	return new PipelineResult();
+            	
+            $config =& Config::getConfig();
+            $cacheFolder = $config->getValue('temp_folder');
+            $cacheFolder = $cacheFolder.'/ipabuse/'.$blogInfo->getId();
+            if( !File::exists( $cacheFolder )) {
+                File::createDir( $cacheFolder, 0755 );
+            }
+
+            $today = date("Ymd");
+            $dbFile = $cacheFolder."/".$today."ipabuse.db";
+             	
+            $db = dba_open($dbFile, "cl", "db4");
+
+            if (!$db) {
+                // The database couldn't be opened, just return 
+                // a PipelineResult
+            	return new PipelineResult();
+            }
+  
+            $clientIp = Client::getIp();
+            $result = new PipelineResult();
+
+            if (dba_exists( $clientIp, $db ) ) {
+                // Check to see if this machine has not accessed more than
+                // the specified amount of times for this day
+                // Right now 10
+                $numAccesses = intval( dba_fetch($clientIp, $db) );
+                if ( $numAccesses > 10 ) {
+                    // Return the result
+                    $result = new PipelineResult( false, IP_ABUSE_MATCH_FOUND, $locale->tr("error_ipabuse_ip_address_banned") );
+                }
+                $newValueInt = $numAccesses + 1;
+                dba_replace($clientIp, "$newValueInt", $db);
+                error_log( "$newValueInt" );
+            }
+            else {
+                // Add an entry for this ip address
+               dba_replace($clientIp,  "1", $db);
+            }
+            
+            dba_close($db);
+
+
+            return $result;
+        }
+
+        
+    }
+?>

Added: plugins/trunk/0ipabuse/class/view/pluginipabuseconfigview.class.php
===================================================================
--- plugins/trunk/0ipabuse/class/view/pluginipabuseconfigview.class.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/class/view/pluginipabuseconfigview.class.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,28 @@
+<?php
+	
+	include_once( PLOG_CLASS_PATH."class/view/admin/adminplugintemplatedview.class.php" );
+
+	/**
+	 * implements the main view of the feed reader plugin
+	 */
+	class PluginIPAbuseConfigView extends AdminPluginTemplatedView
+	{
+
+		function PluginIPAbuseConfigView( $blogInfo )
+		{
+			$this->AdminPluginTemplatedView( $blogInfo, "0ipabuse", "ipabuse" );
+		}
+		
+		function render()
+		{
+			// load some configuration settings
+			$blogSettings = $this->_blogInfo->getSettings();
+			$pluginEnabled = $blogSettings->getValue( "plugin_ipabuse_enabled" );
+			
+			// create a view and export the settings to the template
+			$this->setValue( "pluginEnabled", $pluginEnabled );		
+			
+			parent::render();
+		}
+	}
+?>
\ No newline at end of file

Added: plugins/trunk/0ipabuse/locale/locale_en_UK.php
===================================================================
--- plugins/trunk/0ipabuse/locale/locale_en_UK.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/locale/locale_en_UK.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,12 @@
+<?php
+$messages["manageAntiSpamPlugins"] = "Anti Spam Management";
+$messages["IPAbuse"] = "IP Abuse";
+
+$messages["ipabuse_plugin_enabled"] = "Enable this plugin";
+$messages["ipabuse_plugin"] = "IP Abuse Plugin";
+
+$messages["ipabuse_settings_saved_ok"] = "IP Abuse settings saved successfully!";
+
+$messages["label_configuration"] = "Configuration";
+$messages["label_enable"] = "Enable";
+?>
\ No newline at end of file

Added: plugins/trunk/0ipabuse/plugin0ipabuse.class.php
===================================================================
--- plugins/trunk/0ipabuse/plugin0ipabuse.class.php	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/plugin0ipabuse.class.php	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,58 @@
+<?php
+	include_once( PLOG_CLASS_PATH."class/plugin/pluginbase.class.php" );
+	include_once( PLOG_CLASS_PATH."class/plugin/pluginmanager.class.php" );
+    include_once( PLOG_CLASS_PATH."plugins/0ipabuse/class/security/ipabusefilter.class.php" );
+
+	class Plugin0IPAbuse extends PluginBase
+	{
+		var $pluginEnabled;
+        var $cacheFolder;
+    
+		
+		function Plugin0IPAbuse()
+		{
+			$this->PluginBase();
+
+			$this->id      = "0ipabuse";
+			$this->author  = 'Paul Westbrook';
+			$this->desc    = "Check if a comment poster has made a lot of posts recently.";
+
+			$this->locales = Array( "en_UK" );
+
+			$this->init();
+		}
+
+		function init()
+		{
+            $this->registerFilter( "IPAbuseFilter" );
+
+            $this->registerAdminAction( "ipabuse", "PluginIPAbuseConfigAction" );
+			$this->registerAdminAction( "updateIPAbuseConfig", "PluginIPAbuseUpdateConfigAction" );
+			
+			include_once( PLOG_CLASS_PATH."class/template/menu/menu.class.php" );
+			
+			$menu =& Menu::getMenu();
+			if( !$menu->entryExists( "/menu/controlCenter/manageAntiSpamPlugins" ))						
+				$this->addMenuEntry( "/menu/controlCenter", "manageAntiSpamPlugins", "", "", true, false );			
+            $this->addMenuEntry( "/menu/controlCenter/manageAntiSpamPlugins", "PluginIPAbuse", "?op=ipabuse", "" );            
+		}
+
+		function register()
+		{
+            $config =& Config::getConfig();
+            $this->cacheFolder = $config->getValue('temp_folder');
+            $this->cacheFolder = $this->cacheFolder.'/ipabuse/'.$this->blogInfo->getId();
+            if( !File::exists( $this->cacheFolder )) {
+                File::createDir( $this->cacheFolder, 0755 );
+            }
+
+		    $blogSettings = $this->blogInfo->getSettings();
+			$this->pluginEnabled = $blogSettings->getValue( "plugin_ipabuse_enabled" );
+		}
+
+	    function isEnabled()
+	    {
+	        return $this->pluginEnabled;
+	    }
+	}
+?>
\ No newline at end of file

Added: plugins/trunk/0ipabuse/templates/ipabuse.template
===================================================================
--- plugins/trunk/0ipabuse/templates/ipabuse.template	2006-04-16 03:22:26 UTC (rev 3251)
+++ plugins/trunk/0ipabuse/templates/ipabuse.template	2006-04-16 04:07:39 UTC (rev 3252)
@@ -0,0 +1,24 @@
+{include file="$admintemplatepath/header.template"}
+{include file="$admintemplatepath/navigation.template" showOpt=IPAbuse title=$locale->tr("ipabuse_plugin")}
+<form name="nofollowPluginConfig" method="post">
+ <fieldset class="inputField">
+ <legend>{$locale->tr("label_configuration")}</legend>  
+  {include file="$admintemplatepath/successmessage.template"}
+  {include file="$admintemplatepath/errormessage.template"}   
+  <div class="field">
+   <label for="pluginEnabled">{$locale->tr("label_enable")}</label>
+   <div class="formHelp">   
+    <input class="checkbox" type="checkbox" name="pluginEnabled" id="pluginEnabled" {if $pluginEnabled} checked="checked" {/if} value="1" />{$locale->tr("ipabuse_plugin_enabled")}
+   </div>
+  </div>
+  
+ </fieldset>  
+
+ <div class="buttons">
+  <input type="hidden" name="op" value="updateIPAbuseConfig" />
+  <input type="reset" name="{$locale->tr("reset")}" />    
+  <input type="submit" name="{$locale->tr("update_settings")}" value="{$locale->tr("update")}" />
+ </div>
+</form>
+{include file="$admintemplatepath/footernavigation.template"}
+{include file="$admintemplatepath/footer.template"}
\ No newline at end of file



More information about the pLog-svn mailing list