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

158 lines 3.9 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 // Check length.
112 if ( 40 !== strlen( $apikey ) ) {
113 return new WP_Error( 'invalid_api_key', 'Invalid API key length' );
114 }
115
116 // Check characters.
117 preg_match( '/[a-f0-9]{40}/', $apikey, $matches );
118 if ( ! isset( $matches[0] ) ) {
119 return new WP_Error( 'invalid_api_key', 'Invalid API key format' );
120 }
121
122 $base_url = $this->get_base_url();
123 // Combine Url.
124 $url = $base_url . $endpoint;
125 $args = array(
126 'method' => $method,
127 'headers' => array(
128 'x-blocklist-auth' => $apikey,
129 ),
130 );
131
132 if ( 'POST' === $method ) {
133 $args['headers']['Content-Type'] = 'application/json';
134 $args['body'] = wp_json_encode( $data );
135 } elseif ( 'GET' === $method ) {
136 $url = add_query_arg( $data, $url );
137 }
138
139 $response = wp_remote_request( $url, $args );
140
141 if ( is_wp_error( $response ) ) {
142 return $response;
143 }
144
145 $status_code = wp_remote_retrieve_response_code( $response );
146 $body = json_decode( wp_remote_retrieve_body( $response ), true );
147
148 if ( $status_code >= 400 ) {
149 $error_message = isset( $body['message'] ) ? $body['message'] : wp_remote_retrieve_response_message( $response );
150 $error_code = isset( $body['status'] ) ? $body['status'] : 'http_error_' . $status_code;
151
152 return new WP_Error( $error_code, $error_message, array( 'status_code' => $status_code ) );
153 }
154
155 return $body;
156 }
157 }
158