PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.8.1
Defender Security – Malware Scanner, Login Security & Firewall v5.8.1
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / integrations / class-antibot-global-firewall-client.php

class-antibot-global-firewall-client.php in Defender Security – Malware Scanner, Login Security & Firewall 5.8.1, at src/integrations/class-antibot-global-firewall-client.php

137 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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;
31
32 /**
33 * Class construct.
34 *
35 * @param string|null $base_url Optional. If not provided, fallback to default or constant.
36 */
37 public function __construct( ?string $base_url = null ) {
38 if ( $base_url ) {
39 $this->base_url = $base_url;
40 } elseif ( defined( 'ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER' ) && ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER ) {
41 $this->base_url = ANTIBOT_GLOBAL_FIREWALL_CUSTOM_API_SERVER;
42 } else {
43 $this->base_url = 'https://api.blocklist-service.com';
44 }
45 }
46
47 /**
48 * Get the base URL of the AntiBot Global Firewall API service.
49 *
50 * @return string
51 */
52 private function get_base_url(): string {
53 return $this->base_url . '/api';
54 }
55
56 /**
57 * Send firewall logs to AntiBot Global Firewall API.
58 *
59 * @param array $data The firewall logs.
60 *
61 * @return array|WP_Error
62 */
63 public function send_reports( $data ) {
64 return $this->make_request( 'POST', '/report', $data );
65 }
66
67 /**
68 * Get the blocklist download URL and hashes.
69 *
70 * @param string $mode The mode for fetching the blocklist.
71 *
72 * @since 4.8.0
73 * @return array|WP_Error
74 */
75 public function get_blocklist_download( $mode ) {
76 $path = '/download/' . rawurlencode( $mode );
77 return $this->make_request( 'GET', $path );
78 }
79
80 /**
81 * Get Blocklist Statistics.
82 *
83 * @return array|\WP_Error
84 */
85 public function get_blocklist_stats() {
86 $response = $this->make_request( 'GET', '/stats' );
87
88 if ( is_wp_error( $response ) ) {
89 return $response;
90 }
91
92 return isset( $response['data'] ) && is_array( $response['data'] ) ? $response['data'] : array();
93 }
94
95 /**
96 * Make a request to the AntiBot Global Firewall API service.
97 *
98 * @param string $method The HTTP method to use.
99 * @param string $endpoint The API endpoint to request.
100 * @param array $data The data to send with the request or query variables.
101 *
102 * @return array|WP_Error
103 */
104 private function make_request( $method, $endpoint, $data = array() ) {
105 $apikey = $this->get_api_key();
106
107 if ( '' === $apikey ) {
108 return new WP_Error( 'no_api_key', 'No API key provided' );
109 }
110
111 $base_url = $this->get_base_url();
112 // Combine Url.
113 $url = $base_url . $endpoint;
114 $args = array(
115 'method' => $method,
116 'headers' => array(
117 'x-blocklist-auth' => $apikey,
118 ),
119 );
120
121 if ( 'POST' === $method ) {
122 $args['headers']['Content-Type'] = 'application/json';
123 $args['body'] = wp_json_encode( $data );
124 } elseif ( 'GET' === $method ) {
125 $url = add_query_arg( $data, $url );
126 }
127
128 $response = wp_remote_request( $url, $args );
129
130 if ( is_wp_error( $response ) ) {
131 return $response;
132 }
133
134 return json_decode( wp_remote_retrieve_body( $response ), true );
135 }
136 }
137