| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles interactions with AntiBot Global Firewall API. |
| 4 |
* |
| 5 |
* @package WP_Defender\Integrations |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Integrations; |
| 9 |
|
| 10 |
if ( ! defined( 'ABSPATH' ) ) { |
| 11 |
die; |
| 12 |
} |
| 13 |
|
| 14 |
use WP_Error; |
| 15 |
use WP_Defender\Traits\Defender_Dashboard_Client; |
| 16 |
|
| 17 |
/** |
| 18 |
* AntiBot Global Firewall API client. |
| 19 |
* |
| 20 |
* @since 4.7.1 |
| 21 |
*/ |
| 22 |
class Antibot_Global_Firewall_Client { |
| 23 |
use Defender_Dashboard_Client; |
| 24 |
|
| 25 |
/** |
| 26 |
* The base URL of the AntiBot Global Firewall API service. |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $base_url = 'https://api.blocklist-service.com'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Get the base URL of the AntiBot Global Firewall API service. |
| 34 |
* |
| 35 |
* @return string |
| 36 |
*/ |
| 37 |
private function get_base_url(): string { |
| 38 |
$base_url = defined( 'ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER' ) && ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER |
| 39 |
? ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER |
| 40 |
: $this->base_url; |
| 41 |
|
| 42 |
return $base_url . '/api'; |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* Send firewall logs to AntiBot Global Firewall API. |
| 47 |
* |
| 48 |
* @param array $data The firewall logs. |
| 49 |
* |
| 50 |
* @return array|WP_Error |
| 51 |
*/ |
| 52 |
public function send_reports( $data ) { |
| 53 |
return $this->make_request( 'POST', '/report', $data ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get the blocklist download URL and hashes. |
| 58 |
* |
| 59 |
* @param string $mode The mode for fetching the blocklist. |
| 60 |
* |
| 61 |
* @since 4.8.0 |
| 62 |
* @return array|WP_Error |
| 63 |
*/ |
| 64 |
public function get_blocklist_download( $mode ) { |
| 65 |
$path = '/download/' . rawurlencode( $mode ); |
| 66 |
return $this->make_request( 'GET', $path ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Get Blocklist Statistics. |
| 71 |
* |
| 72 |
* @return array|\WP_Error |
| 73 |
*/ |
| 74 |
public function get_blocklist_stats() { |
| 75 |
$response = $this->make_request( 'GET', '/stats' ); |
| 76 |
|
| 77 |
if ( is_wp_error( $response ) ) { |
| 78 |
return $response; |
| 79 |
} |
| 80 |
|
| 81 |
return isset( $response['data'] ) && is_array( $response['data'] ) ? $response['data'] : array(); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Make a request to the AntiBot Global Firewall API service. |
| 86 |
* |
| 87 |
* @param string $method The HTTP method to use. |
| 88 |
* @param string $endpoint The API endpoint to request. |
| 89 |
* @param array $data The data to send with the request or query variables. |
| 90 |
* |
| 91 |
* @return array|WP_Error |
| 92 |
*/ |
| 93 |
private function make_request( $method, $endpoint, $data = array() ) { |
| 94 |
$apikey = $this->get_api_key(); |
| 95 |
|
| 96 |
if ( '' === $apikey ) { |
| 97 |
return new WP_Error( 'no_api_key', 'No API key provided' ); |
| 98 |
} |
| 99 |
|
| 100 |
$base_url = $this->get_base_url(); |
| 101 |
// Combine Url. |
| 102 |
$url = $base_url . $endpoint; |
| 103 |
$args = array( |
| 104 |
'method' => $method, |
| 105 |
'headers' => array( |
| 106 |
'x-blocklist-auth' => $apikey, |
| 107 |
), |
| 108 |
); |
| 109 |
|
| 110 |
if ( 'POST' === $method ) { |
| 111 |
$args['headers']['Content-Type'] = 'application/json'; |
| 112 |
$args['body'] = wp_json_encode( $data ); |
| 113 |
} elseif ( 'GET' === $method ) { |
| 114 |
$url = add_query_arg( $data, $url ); |
| 115 |
} |
| 116 |
|
| 117 |
$response = wp_remote_request( $url, $args ); |
| 118 |
|
| 119 |
if ( is_wp_error( $response ) ) { |
| 120 |
return $response; |
| 121 |
} |
| 122 |
|
| 123 |
return json_decode( wp_remote_retrieve_body( $response ), true ); |
| 124 |
} |
| 125 |
} |
| 126 |
|