PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.5.1
Defender Security – Malware Scanner, Login Security & Firewall v5.5.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.5.1, at src/integrations/class-antibot-global-firewall-client.php

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