Advertisement
UHLHosting

wp_abuseipdb

Jan 13th, 2023
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.24 KB | None | 0 0
  1. <?php
  2.  
  3. // Create a custom admin menu for blacklist
  4. add_action('admin_menu', 'blacklist_menu');
  5.  
  6. function blacklist_menu() {
  7.     add_menu_page('Blacklist', 'Blacklist', 'manage_options', 'blacklist', 'blacklist_menu_page', 'dashicons-shield', 6);
  8. }
  9.  
  10. // Create a page to show all blacklisted IPs
  11. function blacklist_menu_page() {
  12.     echo '<h1>Blacklisted IPs</h1>';
  13.     echo '<p>Here are all the IPs that have been blacklisted:</p>';
  14.     // Get all blacklisted IPs from database
  15.     global $wpdb;
  16.     $blacklist_ips = $wpdb->get_results("SELECT * FROM {$wpdb->prefix}blacklist_ips");
  17.     // Display all blacklisted IPs
  18.     if ($blacklist_ips) {
  19.         echo '<ul>';
  20.         foreach ($blacklist_ips as $ip) {
  21.             echo '<li>' . $ip->ip_address . '</li>';
  22.         }
  23.         echo '</ul>';
  24.     } else {
  25.         echo '<p>No IPs have been blacklisted yet.</p>';
  26.     }
  27. }
  28.  
  29. // Check if user's IP is blacklisted
  30. add_action('init', 'check_ip_blacklist');
  31.  
  32. function check_ip_blacklist() {
  33.     // Get user's IP
  34.     $user_ip = $_SERVER['REMOTE_ADDR'];
  35.     // Sanitize user's IP
  36.     $user_ip = sanitize_text_field($user_ip);
  37.     // Check if user's IP is blacklisted
  38.     global $wpdb;
  39.     $blacklist_ip = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}blacklist_ips WHERE ip_address = '$user_ip'");
  40.     // If user's IP is blacklisted, ban the user
  41.     if ($blacklist_ip) {
  42.         wp_die('You have been banned from this website.');
  43.     }
  44. }
  45.  
  46. // Check if user's IP is blacklisted on AbuseIPDB
  47. add_action('init', 'check_ip_abuseipdb');
  48.  
  49. function check_ip_abuseipdb() {
  50.     // Get user's IP
  51.     $user_ip = $_SERVER['REMOTE_ADDR'];
  52.     // Sanitize user's IP
  53.     $user_ip = sanitize_text_field($user_ip);
  54.     // Get AbuseIPDB API key
  55.     $api_key = get_option('abuseipdb_api_key');
  56.     // Check if user's IP is blacklisted on AbuseIPDB
  57.     $url = 'https://api.abuseipdb.com/api/v2/check?ipAddress=' . $user_ip . '&maxAgeInDays=90&verbose';
  58.     $args = array(
  59.         'headers' => array(
  60.             'Key' => $api_key,
  61.             'Accept' => 'application/json'
  62.         )
  63.     );
  64.     $response = wp_remote_get($url, $args);
  65.     $data = json_decode($response['body'], true);
  66.     // If user's IP is blacklisted, add it to the blacklist
  67.     if ($data
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement