HEX
Server: Apache/2.2.22
System: Linux server1.blueharbor.com 3.10.0-1160.90.1.vz7.200.7 #1 SMP Wed Jul 12 12:00:44 MSK 2023 x86_64
User: locglobe (1004)
PHP: 5.6.37
Disabled: NONE
Upload Files
File: //proc/self/cwd/wp-content/plugins/bot-nemesis_6551354a614844b2a78837b5afc250af/inc/BotNemesis.php
<?php

namespace BotNemesis;

use BotNemesis\Entities\IPAddressEntity;
use BotNemesis\Entities\UserAgentKeywordEntity;
use BotNemesis\Entities\CountryIPAddressEntity;
use IPTools\Range;
use IPTools\IP;

class BotNemesis
{
    public static function init()
    {
        $priority = 10;
        add_action( 'set_current_user', array( get_called_class(), 'doBlock' ), $priority );
        add_filter('query_vars', array(get_called_class(), 'custom_query_vars'));
        add_action('parse_request', array(get_called_class(), 'custom_responses'));
    }

    public static function detectVisitorType($remote_addr, $http_user_agent)
    {
        return self::compareIPAddress($remote_addr) || self::compareUserAgent($http_user_agent)  ? 'Spider' : 'Visitor';
    }

    protected static function compareUserAgent($user_agent)
    {
        return UserAgentKeywordEntity::isInUserAgent($user_agent);
    }

    protected static function compareIPAddress($ip)
    {
        try {
            $ip_addresses = IPAddressEntity::getEntitiesByProperty(array('ip_address_removed' => '0'), 'ARRAY_A');
            foreach($ip_addresses as $ip_address) {
                $ip_value = explode('#', $ip_address['ip_address_value']);
                $ip_value = rtrim($ip_value[0]);
                if(Range::parse($ip_value)->contains(new IP($ip))) {
                    return TRUE;
                }
            }
        }
        catch(\Exception $e) {
            return TRUE;
        }

        return FALSE;
    }

    protected static function compareCountryIPAddress($ip)
    {
        try {
            $country_ip_addresses = CountryIPAddressEntity::getBlockedCountryIPAddresses();

            foreach($country_ip_addresses as $country_ip_address) {
                $ip_value = explode('#', $country_ip_address->country_ip_address_value);
                $ip_value = rtrim($ip_value[0]);
                if(Range::parse($ip_value)->contains(new IP($ip))) {
                    return TRUE;
                }
            }
        }
        catch(\Exception $e) {
            return TRUE;
        }

        return FALSE;
    }

    public static function doBlock()
    {
        if( ! is_admin() &&
            (
                self::compareIPAddress($_SERVER['REMOTE_ADDR']) ||
                self::compareUserAgent($_SERVER['HTTP_USER_AGENT']) ||
                self::compareCountryIPAddress($_SERVER['REMOTE_ADDR'])
            ) &&
            strpos($_SERVER['HTTP_USER_AGENT'], 'botnemesis.com') === FALSE &&
            strpos($_SERVER['HTTP_USER_AGENT'], get_option('siteurl')) === FALSE)
        {
            header('HTTP/1.1 ' . get_option('bn_blocked_return_status', '408 Request Timeout'));
            exit;
        }
    }

    public static function custom_query_vars($query_vars)
    {
        $query_vars[] = 'receive_updates';
        $query_vars[] = 'echo_domain';

        return $query_vars;
    }

    public static function custom_responses($wp)
    {
        if(isset($wp->query_vars['receive_updates'])) {
            Server::receiveUpdates();
        }
        elseif(isset($wp->query_vars['echo_domain'])) {
            Server::echoDomain();
        }
    }
}