ai_crawlers = new WPBan_AI_Crawlers(); $this->seo_crawlers = new WPBan_SEO_Crawlers(); } public function check_ban() { $ip = ban_anything_get_ip(); $options = $this->get_options(); $is_banned = false; foreach ($options['banned_ips'] as $banned_ip) { if (preg_match_wildcard($banned_ip, $ip)) { $is_banned = true; break; } } if ($is_banned && !in_array($ip, $options['banned_exclude_ips'])) { $this->update_stats($ip, 'ip_ban'); $this->print_banned_message(); } } public function print_banned_message() { $options = $this->get_options(); $message = str_replace( ['%SITE_NAME%', '%SITE_URL%', '%USER_IP%', '%USER_HOSTNAME%'], [get_option('blogname'), get_option('siteurl'), ban_anything_get_ip(), @gethostbyaddr(ban_anything_get_ip())], stripslashes($options['banned_message']) ); wp_die($message, 'Access Denied', ['response' => 403]); } public function restrict_login_access() { $options = $this->get_options(); if (strpos($_SERVER['REQUEST_URI'], 'wp-login.php') !== false) { $user_ip = ban_anything_get_ip(); $allowed_ips = $options['login_restrictions']['allowed_ips'] ?? []; if (!empty($allowed_ips) && !ban_anything_ip_in_range($user_ip, $allowed_ips)) { $this->update_stats($user_ip, 'login_restriction'); wp_redirect(home_url()); exit; } } } public function block_wechat_qq_browsers() { $options = $this->get_options(); $ua = $_SERVER['HTTP_USER_AGENT'] ?? ''; if (strpos($ua, 'MQQBrowser') !== false || strpos($ua, 'MicroMessenger') !== false) { $settings = $options['wechat_qq_settings']; if ($settings['enabled']) { $this->update_stats(ban_anything_get_ip(), 'wechat_qq_block'); ?>

get_option('banned_ips', []), 'banned_ips_range' => get_option('banned_ips_range', []), 'banned_hosts' => get_option('banned_hosts', []), 'banned_referers' => get_option('banned_referers', []), 'banned_user_agents' => get_option('banned_user_agents', []), 'banned_exclude_ips' => get_option('banned_exclude_ips', []), 'banned_message' => get_option('banned_message', "

You are banned.

"), 'banned_options' => get_option('banned_options', ['reverse_proxy' => 0]), 'login_restrictions' => get_option('login_restrictions', ['allowed_ips' => []]), 'wechat_qq_settings' => get_option('wechat_qq_settings', [ 'enabled' => false, 'message' => __('Please use your system browser to open this site.', 'wpban-anything'), 'copy_text' => __('Copy Link', 'wpban-anything') ]), 'banned_stats' => get_option('banned_stats', [ 'users' => [], 'total_count' => 0, 'last_ban_time' => null, 'types' => ['ip_ban' => 0, 'login_restriction' => 0, 'wechat_qq_block' => 0, 'ai_crawler_block' => 0, 'seo_crawler_block' => 0] ]), 'ai_crawler_settings' => $this->ai_crawlers->get_settings(), 'seo_crawler_settings' => $this->seo_crawlers->get_settings() ]; } private function save_options($data) { $allowed_tags = wp_kses_allowed_html('post'); $allowed_tags['html'] = $allowed_tags['head'] = $allowed_tags['body'] = true; $allowed_tags['meta'] = ['charset' => true]; $options = [ 'banned_options' => ['reverse_proxy' => isset($data['reverse_proxy']) ? 1 : 0], 'banned_ips' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_ips'] ?? '')))), 'banned_ips_range' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_ips_range'] ?? '')))), 'banned_hosts' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_hosts'] ?? '')))), 'banned_referers' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_referers'] ?? '')))), 'banned_user_agents' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_user_agents'] ?? '')))), 'banned_exclude_ips' => array_map('esc_html', array_filter(explode("\n", trim($data['banned_exclude_ips'] ?? '')))), 'banned_message' => wp_kses(trim($data['banned_message'] ?? ''), $allowed_tags), 'login_restrictions' => [ 'allowed_ips' => array_map('esc_html', array_filter(explode("\n", trim($data['login_allowed_ips'] ?? '')))) ], 'wechat_qq_settings' => [ 'enabled' => isset($data['wechat_qq_enabled']) ? 1 : 0, 'message' => esc_html($data['wechat_qq_message'] ?? ''), 'copy_text' => esc_html($data['wechat_qq_copy_text'] ?? '') ], 'ai_crawler_settings' => $this->ai_crawlers->save_settings($data), 'seo_crawler_settings' => $this->seo_crawlers->save_settings($data) ]; foreach ($options as $key => $value) { if ($key !== 'ai_crawler_settings' && $key !== 'seo_crawler_settings') { update_option($key, $value); } } return $options; } private function update_stats($ip, $type) { $stats = get_option('banned_stats', [ 'users' => [], 'total_count' => 0, 'last_ban_time' => null, 'types' => ['ip_ban' => 0, 'login_restriction' => 0, 'wechat_qq_block' => 0, 'ai_crawler_block' => 0, 'seo_crawler_block' => 0] ]); $stats['users'][$ip] = [ 'count' => ($stats['users'][$ip]['count'] ?? 0) + 1, 'last_time' => current_time('mysql'), 'type' => $type ]; $stats['total_count']++; $stats['last_ban_time'] = current_time('mysql'); $stats['types'][$type]++; update_option('banned_stats', $stats); } public function render_admin_page() { if (!current_user_can('manage_options')) { wp_die(__('You do not have permission to access this page.', 'wpban-anything')); } $options = $this->get_options(); ?>

['title' => __('General Bans', 'wpban-anything'), 'desc' => __('Basic ban settings for IPs.')], 'advanced_bans' => ['title' => __('Advanced Bans', 'wpban-anything'), 'desc' => __('Ban settings for hosts and agents.')], 'login_restrictions' => ['title' => __('Login Restrictions', 'wpban-anything'), 'desc' => __('Restrict login page access.')], 'browser_bans' => ['title' => __('Browser Bans', 'wpban-anything'), 'desc' => __('Ban specific browsers.')], 'ai_bans' => ['title' => __('AI Bans', 'wpban-anything'), 'desc' => __('Block AI crawlers.')], 'seo_bans' => ['title' => __('SEO Bans', 'wpban-anything'), 'desc' => __('Block SEO crawlers.')], 'ban_message' => ['title' => __('Ban Message', 'wpban-anything'), 'desc' => __('Customize the ban message.')], 'options' => ['title' => __('Options', 'wpban-anything'), 'desc' => __('Additional ban options.')], ]; foreach ($sections as $key => $data): ?>

$data) { echo esc_html("$ip ({$data['count']} times, last: {$data['last_time']}, type: {$data['type']})
"); } } ?>
save_options($settings); wp_send_json_success(['message' => __('Settings saved.', 'wpban-anything')]); } public function preview_banned_message() { check_ajax_referer('wpban_anything_nonce'); $this->print_banned_message(); } }