Plugin Directory

Changeset 1266863


Ignore:
Timestamp:
10/15/2015 08:12:23 PM (10 years ago)
Author:
achbed
Message:
  • Plugin_BNBlocker::netmatch no longer throws warnings when error level is high
  • Reworked Plugin_BNBlocker::netmatch to reduce computation a bit
  • Fixes error when using Block on Page Load
Location:
botnet-blocker/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • botnet-blocker/trunk/botnet-blocker.php

    r1258674 r1266863  
    55Description: Free botnet IP blocker according to public DNSBL bases. Based on public DNSBL class.
    66Author: Dennis Wallace
    7 Version: 1.2.2
     7Version: 1.2.3
    88License: GPLv2 or later
    99*/
     
    7070         * Things that run during the WP plugins loaded action
    7171         */
    72         public function handle_plugins_loaded() {
     72        public function handle_pre_get_posts( $query ) {
    7373            if ( $this->config->onload != 'on' ) {
    7474                // We only do this if the option is set
    7575                return;
    7676            }
     77           
     78            if ( ! $query->is_main_query() ) {
     79                return;
     80            }
    7781           
    7882            if ( $this->is_botnet() ) {
    79                 $this->block();
     83                $this->block( 404, $query );
    8084            }
    8185        }
     
    98102           
    99103            add_action( 'init', array( &$this, 'handle_init' ) );
    100             add_action( 'plugins_loaded', array( &$this, 'handle_plugins_loaded' ) );
     104            add_action( 'pre_get_posts', array( &$this, 'handle_pre_get_posts' ), 0 );
    101105        }
    102106       
     
    225229        /**
    226230         * This is called when a spammish IP is detected.
    227          */
    228         public function block( $status = 404 ) {
     231         *
     232         * @param integer $status The status code to return.  Default is 404.
     233         * @param WP_Query $query The query object to set as a 404 result.  Null uses the main global $wp_query object if possible.  Default is null.
     234         */
     235        public function block( $status = 404, $query = null ) {
    229236            // We've got a blacklisted IP.  404 it.
    230             global $wp_query;
    231             $wp_query->set_404();
     237            if( is_null( $query ) ) {
     238                if( isset( $GLOBALS['wp_query'] ) ) {
     239                    $query = $GLOBALS['wp_query'];
     240                }
     241            }
     242            if ( method_exists( $query, 'set_404' ) ) {
     243                $query->set_404();
     244            }
    232245            status_header( $status );
    233246            nocache_headers();
     
    243256         */
    244257        public function netmatch( $ip, $cidr ) {
    245             if ( empty( $cidr ) ) {
     258            if ( empty( $cidr ) || empty( $ip ) ) {
    246259                return false;
    247260            }
    248261           
    249262            $cidr .= '/32'; // Put the default mask in case one wasn't given.  We ignore it if it's extra.
    250             list ( $net, $mask, $extra ) = explode ( '/', $cidr );
    251             $mask = intval( $mask );
    252             $net = ip2long ($net) & ~((1 << (32 - $mask)) - 1); // Enforce the mask on the network too (just in case)
    253             return ( ( ip2long ($ip) & ~((1 << (32 - $mask)) - 1) ) == $net );
     263            $parts = explode( '/', $cidr );
     264            $net = ip2long( $parts[0] );
     265            $mask = intval( $parts[1] );
     266            $mask = ( 1 << ( 32 - $mask ) ) - 1;
     267            $net = $net & ~( $mask ); // Enforce the mask on the network too (just in case)
     268            return ( ( ip2long( $ip ) & ~( $mask ) ) == $net );
    254269        }
    255270       
  • botnet-blocker/trunk/readme.txt

    r1258674 r1266863  
    3939== Upgrade Notice ==
    4040
     41= 1.2.3 =
     42No longer throws extra warnings; fixes error when using Block on Page Load
     43
    4144= 1.2.2 =
    4245Added commenting in lists, now respects the Block on Page Load setting
     
    5659
    5760== Changelog ==
     61
     62= 1.2.3 =
     63* Plugin_BNBlocker::netmatch no longer throws warnings when error level is high
     64* Reworked Plugin_BNBlocker::netmatch to reduce computation a bit
     65* Fixes error when using Block on Page Load
    5866
    5967= 1.2.2 =
Note: See TracChangeset for help on using the changeset viewer.