No Soup

Block users from a specific IP or range(s) of IPs and redirect them to another site.
Use with care, it’s version 0.3 :)

Download

<?php
/*
Plugin Name: No Soup
Plugin URI: http://jonasnordstrom.se/plugins/no-soup/
Description: Blocks user from IP-ranges that you specify and sends them to another page
Version: 0.3
Author: Jonas Nordstrom
Author URI: http://jonasnordstrom.se/ 
*/
 
/**
 * Copyright (c) 2012 Jonas Nordstrom. All rights reserved.
 *
 * Released under the GPL license
 * http://www.opensource.org/licenses/gpl-license.php
 *
 * This is an add-on for WordPress
 * http://wordpress.org/
 *
 * **********************************************************************
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * **********************************************************************
 */
 
if (!class_exists("NoSoup")) {
    class NoSoup {
 
        protected $bad_ip_ranges;
        protected $eat_that;
 
        public function NoSoup() {
            $this->bad_ip_ranges = get_option('_no_soup_bad_ip_ranges', '');
            $this->eat_that = get_option('_no_soup_eat_that', '');          
        }
        public function init_admin() {
            add_options_page(__('No Soup', "nosoup"), __('No Soup', "nosoup"), 'manage_options', basename(__FILE__), array(&$this, 'no_soup_admin_page') );
        }
 
        public function no_soup_admin_page() {
            // Handle updates
            if( !empty($_POST) && $_POST[ 'action' ] == 'no-soup-save' ) {
                check_admin_referer('no-soup-save', 'no-soup-nonce');
                $this->bad_ip_ranges = esc_html($_POST[ 'no_soup_bad_ip_ranges' ]);
                update_option('_no_soup_bad_ip_ranges', $this->bad_ip_ranges );
                $this->eat_that = esc_html($_POST[ 'no_soup_eat_that' ]);
                update_option('_no_soup_eat_that', $this->eat_that );
                ?>
                <div class="updated"><p><strong><?php _e('Settings saved.', 'nosoup' ); ?></strong></p></div>
                <?php
            }
 
            ?>
            <div class="wrap">
            <h2><?php _e("No Soup", "nosoup"); ?></h2>
            <p>Block users from a specific IP or range(s) of IPs and redirect them to another site.</p>
 
            <form name="no-soup-admin-form" method="post" action="">
                <input type="hidden" name="action" value="no-soup-save" />
                <?php wp_nonce_field('no-soup-save', 'no-soup-nonce'); ?>
                <table class="nosoup-form-table">
                    <tr>
                        <td colspan="2">
                            &nbsp;
                        </td>
                    </tr>
                    <tr>
                        <td valign="top"><?php _e("Block these ip ranges<br/>Separate with new-line<br/> CIDR format (192.168.0.1/20)<br/> or IP range (fromip - toip)): ", "nosoup"); ?></td>
                        <td><textarea rows="25" cols="50" id="no_soup_bad_ip_ranges" name="no_soup_bad_ip_ranges"><?php echo $this->bad_ip_ranges; ?></textarea></td>
                    </tr>
                    <tr>
                        <td><?php _e("Send them here: ", "nosoup"); ?></td>
                        <td><input type="text" style="width: 100%" id="no_soup_eat_that" name="no_soup_eat_that" value="<?php echo $this->eat_that; ?>" /></td>
                    </tr>
                    <tr>
                        <td colspan="2">
                            <p class="submit">
                                <input type="submit" name="Submit" value="<?php _e('Update settings', 'nosoup' ) ?>" />
                            </p>
                        </td>
                    </tr>
 
                </table>
            </form>
            </div>
            <?php
        }
 
        function check_ip() {
            $current_url = $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
 
            // Test if the client is bad (exception, the redirect-to-page)
            if ( $current_url != $this->eat_that && $this->banned_ip() ) :
                wp_redirect( $this->eat_that );
                die;
            endif;
        }
 
        private function banned_ip() {
            $ip = $_SERVER['REMOTE_ADDR'];
            $networks = preg_split("/[\r\n,]+/", $this->bad_ip_ranges); 
 
            foreach ( $networks as $range ) :
                if ( false === strpos($range, '-')) :
                    if ( 3 <= substr_count( $range, '.' ) && $this->in_cidr( $range, $ip ) ) :
                        return true;
                    endif;
                else :
                    if ( $this->in_ip_range($range, $ip)) :
                        return true;
                    endif;
                endif;
            endforeach;
            return false;
        }
 
        function in_cidr($network, $ip) {
            $ip_arr = explode('/', $network);
 
            $network_long = ip2long($ip_arr[0]);
 
            $x = ip2long($ip_arr[1]);
            $mask =  long2ip($x) == $ip_arr[1] ? $x : 0xffffffff << (32 - $ip_arr[1]);
            $ip_long = ip2long($ip);
 
            return ($ip_long & $mask) == ($network_long & $mask);
        }
 
        function in_ip_range($range, $ip){
            $range = preg_replace("'\s+'", '', $range);
            if ( false === strpos( $range, '-') ) :
                return ( $ip_start == $ip );
            else :
                list($ip_start, $ip_end) = preg_split('/-/', $range);
                $iplong = ip2long( $ip );
                return ( ip2long( $ip_start ) <= $iplong && ip2long( $ip_end ) >= $iplong );
            endif;
        }
    }
}
if ( ! function_exists( '_log' ) ) {
    function _log() {
        if( WP_DEBUG === true ) {
            $args = func_get_args();
            error_log( print_r( $args, true ) );
        }
    }
}
// Init class
if (class_exists("NoSoup")) {
    $nosoup = new NoSoup();
}
 
add_action( 'admin_menu', array(&$nosoup, 'init_admin') );
add_action( 'init', array(&$nosoup, 'check_ip') );

If you want to block for instance the IP-addresses of swedish authorities, you can use the ranges below and instead show them the way to the Wikipedia article about ACTA. I’m not saying you should, I’m just saying you could, ok?

193.44.94.0 - 193.44.94.255  
193.15.242.192 - 193.15.242.223  
195.67.14.48 - 195.67.14.63  
194.71.64.0 - 194.71.82.255  
192.137.161.0 - 192.137.164.255  
192.165.145.0 - 192.165.145.255  
80.252.170.244 - 80.252.170.247  
194.52.83.0 - 194.52.83.255  
195.67.14.64 - 195.67.14.127  
130.242.25.0 - 130.242.25.255  
195.198.201.208 - 195.198.201.215  
195.67.88.208 - 195.67.88.223  
81.216.70.0 - 81.216.70.255  
212.181.33.112 - 212.181.33.115  
212.181.33.12 - 212.181.33.15  
212.28.207.32 - 212.28.207.63  
217.77.240.177 - 217.77.240.177  
217.77.247.160 - 217.77.247.163  
192.71.128.0 - 192.71.128.255  
194.16.218.60 - 194.16.218.63  
194.18.111.24 - 194.18.111.27  
62.65.85.16 - 62.65.85.31  
192.165.31.0 - 192.165.31.255  
192.165.173.0 - 192.165.173.255  
195.198.122.144 - 195.198.122.159  
192.36.96.0 - 192.36.96.255  
193.182.184.0 - 193.182.184.255  
193.182.190.0 - 193.182.190.255  
195.5.185.0 - 195.5.185.255  
194.103.14.0 - 194.103.15.255  
85.119.130.192 - 85.119.130.207  
85.119.133.128 - 85.119.133.191  
212.181.83.0 - 212.181.83.3  
192.121.27.0 - 192.121.27.255  
192.121.211.0 - 192.121.218.255  
213.50.75.16 - 213.50.75.31  
193.234.1.0 - 193.234.141.255  
193.235.152.0 - 193.235.152.255  
193.241.212.0 - 193.241.212.255  
194.18.73.0 - 194.18.73.255  
194.18.169.0 - 194.18.171.255  
192.121.237.0 - 192.121.237.255  
193.180.162.0 - 193.180.163.255  
194.52.96.0 - 194.52.127.255  
194.16.84.160 - 194.16.84.167  
195.84.103.128 - 195.84.103.191  
195.84.15.32 - 195.84.15.47  
195.84.76.0 - 195.84.76.255  
81.216.66.0 - 81.216.66.31  
194.198.144.0 - 194.198.144.255  
80.80.64.0 - 80.80.65.255  
192.121.238.0 - 192.121.238.255  
62.20.16.216 - 62.20.16.223  
195.58.108.96 - 195.58.108.127  
195.58.108.128 - 195.58.108.159  
195.58.110.248 - 195.58.110.255  
195.67.68.48 - 195.67.68.55  
195.67.81.48 - 195.67.81.63  
195.84.193.80 - 195.84.193.95  
193.181.64.0 - 193.181.127.255  
193.241.252.0 - 193.241.252.255  
193.241.0.0 - 193.241.211.255  
193.241.213.0 - 193.241.251.255  
193.241.253.0 - 193.241.255.255  
192.121.30.0 - 192.121.30.255  
143.118.0.0 - 143.118.255.255  
192.121.44.0 - 192.121.44.255  
62.119.64.128 - 62.119.64.135  
212.247.172.64 - 212.247.172.95  
145.235.0.0 - 145.235.255.255  
192.121.50.0 - 192.121.52.255  
192.165.140.0 - 192.165.141.255  
159.72.0.0 - 159.72.255.255  
62.119.150.16 - 62.119.150.31  
194.14.184.0 - 194.14.186.255  
192.121.82.0 - 192.121.82.255  
192.121.200.0 - 192.121.202.255  
159.190.0.0 - 159.190.255.255  
192.36.5.0 - 192.36.5.255  
212.247.200.64 - 212.247.200.95  
192.121.90.0 - 192.121.90.255  
130.242.58.0 - 130.242.58.255  
138.215.0.0 - 138.215.255.255  
212.214.131.192 - 212.214.131.223  
212.247.50.144 - 212.247.50.159  
192.121.219.0 - 192.121.219.255  
62.95.69.0 - 62.95.69.255  
62.95.13.160 - 62.95.13.175  
91.199.170.0 - 91.199.170.255  
62.119.21.32 - 62.119.21.63  
213.131.146.64 - 213.131.146.79  
62.95.35.0 - 62.95.35.31  
195.67.83.80 - 195.67.83.95  
213.242.135.0 - 213.242.135.7  
62.119.134.8 - 62.119.134.15  
62.119.24.8 - 62.119.24.15  
150.227.0.0 - 150.227.255.255  
192.36.132.0 - 192.36.132.255  
192.36.14.0 - 192.36.14.255  
192.36.220.0 - 192.36.220.255  
192.36.221.0 - 192.36.221.255  
193.180.254.0 - 193.180.254.255  
147.186.0.0 - 147.186.255.255  
195.67.26.48 - 195.67.26.51  
195.67.45.136 - 195.67.45.143  
195.198.83.24 - 195.198.83.31  
212.162.184.104 - 212.162.184.111  
137.61.0.0 - 137.61.255.255  
137.60.0.0 - 137.60.255.255  
137.59.0.0 - 137.59.255.255  
192.121.131.0 - 192.121.131.255  
193.15.216.208 - 193.15.216.223  
193.15.200.48 - 193.15.200.63  
195.198.184.208 - 195.198.184.223  
156.49.0.0 - 156.49.255.255  
62.20.16.216 - 62.20.16.223  
62.20.57.192 - 62.20.57.223  
212.181.19.0 - 212.181.19.255  
62.127.100.40 - 62.127.100.47  
192.36.25.0 - 192.36.25.255  
192.165.69.0 - 192.165.69.255  
193.181.190.0 - 193.181.191.255  
193.235.83.0 - 193.235.84.255  
194.132.96.0 - 194.132.103.255  
193.10.58.0 - 193.10.59.255  
193.182.167.0 - 193.182.167.255  
62.13.78.0 - 62.13.79.255  
213.115.124.8 - 213.115.124.11  
213.242.135.144 - 213.242.135.159  
82.136.153.64 - 82.136.153.95  
192.36.213.0 - 192.36.213.255  
192.165.64.0 - 192.165.64.255  
193.12.105.0 - 193.12.105.255  
195.42.219.208 - 195.42.219.215  
192.121.89.0 - 192.121.89.255  
130.242.12.0 - 130.242.12.255  
130.242.60.24 - 130.242.60.31  
195.17.245.40 - 195.17.245.47  
213.88.173.0 - 213.88.173.15  
193.15.197.32 - 193.15.197.47  
195.67.16.32 - 195.67.16.63  
194.50.107.0 - 194.50.107.255  
62.181.240.120 - 62.181.240.135  
83.241.192.88 - 83.241.192.91  
130.242.22.0 - 130.242.22.255  
195.67.76.224 - 195.67.76.255  
213.80.95.40 - 213.80.95.47  
80.252.162.192 - 80.252.162.207  
194.18.111.96 - 194.18.111.103  
192.165.243.0 - 192.165.243.255  
192.176.242.0 - 192.176.242.255  
194.198.105.0 - 194.198.105.255  
194.198.255.0 - 194.198.255.255  
193.121.206.128 - 193.121.206.159  
212.181.96.224 - 212.181.96.231  
62.20.85.128 - 62.20.85.143  
213.115.92.240 - 213.115.92.247  
88.131.68.108 - 88.131.68.111  
88.131.16.180 - 88.131.16.183  
83.241.222.192 - 83.241.222.223  
91.217.186.0 - 91.217.186.255  
213.199.105.128 - 213.199.105.135  
193.15.189.192 - 193.15.189.255  
82.182.190.0 - 82.182.190.255  
194.198.96.0 - 194.198.96.255  
213.115.31.0 - 213.115.31.15