PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.0.1
Defender Security – Malware Scanner, Login Security & Firewall v6.0.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 / controller / class-global-ip.php

class-global-ip.php in Defender Security – Malware Scanner, Login Security & Firewall 6.0.1, at src/controller/class-global-ip.php

337 lines 8.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * It handles functionalities such as saving settings, fetching and syncing global IP lists from the HUB.
4 *
5 * @package WP_Defender\Controller
6 */
7
8 namespace WP_Defender\Controller;
9
10 use WP_Defender\Controller;
11 use Calotes\Component\Request;
12 use Calotes\Component\Response;
13 use WP_Defender\Traits\Setting;
14 use WP_Defender\Traits\Formats;
15 use WP_Defender\Behavior\WPMUDEV;
16 use WP_Defender\Component\Network_Cron_Manager;
17 use WP_Defender\Model\Setting\Global_Ip_Lockout;
18 use WP_Defender\Component\Config\Config_Hub_Helper;
19 use WP_Defender\Component\IP\Global_IP as Global_IP_Component;
20
21 /**
22 * Handles global IP lockout settings.
23 */
24 class Global_Ip extends Controller {
25
26 use Setting;
27 use Formats;
28
29 /**
30 * Module slug for identifying Custom IP sync auto-enable after Hub connection.
31 *
32 * @const string
33 */
34 public const MODULE_SLUG = 'custom-ip-sync';
35
36 /**
37 * The slug identifier for this controller.
38 *
39 * @var string
40 */
41 protected $slug = 'wdf-ip-lockout';
42
43 /**
44 * The model for handling the data.
45 *
46 * @var Global_Ip_Lockout
47 */
48 protected $model;
49
50 /**
51 * Service for handling logic.
52 *
53 * @var Global_IP_Component
54 */
55 protected $service;
56
57 /**
58 * The WPMUDEV instance used for interacting with WPMUDEV services.
59 *
60 * @var WPMUDEV
61 */
62 private $wpmudev;
63
64 /**
65 * Initializes the model and service, registers routes, and sets up scheduled events if the model is active.
66 */
67 public function __construct() {
68 $this->register_routes();
69 add_action( 'defender_enqueue_assets', array( $this, 'enqueue_assets' ) );
70 $this->model = wd_di()->get( Global_Ip_Lockout::class );
71 $this->service = wd_di()->get( Global_IP_Component::class );
72 $this->wpmudev = wd_di()->get( WPMUDEV::class );
73
74 /**
75 * Network Cron Manager
76 *
77 * @var Network_Cron_Manager $network_cron_manager
78 */
79 $network_cron_manager = wd_di()->get( Network_Cron_Manager::class );
80 $network_cron_manager->register_callback(
81 'wpdef_fetch_global_ip_list',
82 array( $this, 'fetch_global_ip_list' ),
83 HOUR_IN_SECONDS
84 );
85
86 if ( $this->service->can_blocklist_autosync() ) {
87 // No need to run Rate mechanism for IP lockouts because we do it in Blacklist class.
88 add_action( 'wd_blacklist_this_ip', array( $this, 'blacklist_an_ip' ) );
89 }
90 add_action( 'init', array( $this->service, 'handle_expired_membership' ) );
91
92 // admin_init priority 20 runs after Hub_Connector sets the transient (priority 10);
93 // the other two hooks cover async-sync cases.
94 add_action( 'admin_init', array( $this, 'maybe_hcm_connection_attempt' ), 20 );
95 add_action( 'wpdef_hub_connector_synced', array( $this, 'maybe_hcm_connection_attempt' ) );
96 add_action( 'wpmudev_hub_connector_first_sync_completed', array( $this, 'maybe_hcm_connection_attempt' ) );
97 }
98
99 /**
100 * Auto-enable Custom IP list sync after Hub connection if user triggered from Custom Rules page.
101 *
102 * @return void
103 */
104 public function maybe_hcm_connection_attempt(): void {
105 $data = get_site_transient( Hub_Connector::TRANSIENT_KEY );
106 $module_slug = $data['module_slug'] ?? '';
107
108 if ( self::MODULE_SLUG !== $module_slug || ! self::get_hcm_status() ) {
109 return;
110 }
111
112 delete_site_transient( Hub_Connector::TRANSIENT_KEY );
113
114 $this->model->blocklist_autosync = true;
115 $this->model->save();
116 }
117
118 /**
119 * Save settings.
120 *
121 * @param Request $request The request object containing new settings data.
122 *
123 * @return Response
124 * @defender_route
125 */
126 public function save_settings( Request $request ) {
127 $data = $request->get_data(
128 array(
129 'enabled' => array( 'type' => 'bool' ),
130 'allow_self_unlock' => array( 'type' => 'bool' ),
131 'blocklist_autosync' => array( 'type' => 'bool' ),
132 // Temporary property.
133 'module_title' => array(
134 'type' => 'string',
135 'sanitize' => 'sanitize_text_field',
136 ),
137 // End.
138 )
139 );
140
141 $message_type = 'central_ip';
142 // Split messages.
143 if ( isset( $data['module_title'] ) && 'antibot' === $data['module_title'] ) {
144 $message_type = 'antibot';
145 unset( $data['module_title'] );
146 }
147
148 $old_enabled = $this->model->enabled;
149 $old_self_unlock = $this->model->allow_self_unlock;
150
151 $this->model->import( $data );
152 if ( $this->model->validate() ) {
153 $this->model->enabled = $this->model->allow_self_unlock || $this->model->blocklist_autosync;
154 $this->model->save();
155 Config_Hub_Helper::set_clear_active_flag();
156 if ( 'central_ip' === $message_type ) {
157 $message = $this->get_update_message( $data, $old_enabled, Global_Ip_Lockout::get_module_name() );
158 } else {
159 $message = '';
160 if ( $data['allow_self_unlock'] ?? false ) {
161 $message = esc_html__( 'Temporary self unlock CAPTCHA challenge is enabled successfully.', 'defender-security' );
162 } elseif ( true === $old_self_unlock && ! ( $data['allow_self_unlock'] ?? false ) ) {
163 $message = esc_html__( 'Temporary self unlock CAPTCHA challenge is disabled successfully.', 'defender-security' );
164 }
165 }
166
167 return new Response(
168 true,
169 array_merge(
170 array(
171 'message' => $message,
172 'auto_close' => true,
173 ),
174 $this->data_frontend()
175 )
176 );
177 }
178
179 return new Response(
180 false,
181 array( 'message' => $this->model->get_formatted_errors() )
182 );
183 }
184
185 /**
186 * Enqueues scripts and styles for this page.
187 * Only enqueues assets if the page is active.
188 */
189 public function enqueue_assets() {
190 if ( ! $this->is_page_active() ) {
191 return;
192 }
193 }
194
195 /**
196 * Provides data for the frontend.
197 *
198 * @return array An array of data for the frontend.
199 */
200 public function data_frontend(): array {
201 return array_merge(
202 array(
203 'model' => $this->model->export(),
204 'misc' => array(
205 'show_global_ips_disable' => $this->wpmudev->is_disabled_hub_option(),
206 'is_wpmu_dev_admin' => $this->wpmudev->is_wpmu_dev_admin(),
207 'module_name' => Global_Ip_Lockout::get_module_name(),
208 'text_to_connect' => sprintf(
209 /* translators: %s: Module name. */
210 esc_html__( 'Connect to a WPMU DEV account to activate %s.', 'defender-security' ),
211 Global_Ip_Lockout::get_module_name()
212 ),
213 'is_show_dashboard_notice' => $this->service->is_show_dashboard_notice(),
214 'is_expired_membership' => $this->service->is_expired_membership_type(),
215 ),
216 'hub' => array(
217 'global_ip_list' => $this->service->get_formated_global_ip_list(),
218 'global_ip_setting_url' => $this->wpmudev->get_api_base_url() . 'hub2/ip-banning',
219 ),
220 ),
221 $this->dump_routes_and_nonces()
222 );
223 }
224
225 /**
226 * Fetch Global IP list from HUB.
227 *
228 * @return void
229 * @since 3.4.0
230 */
231 public function fetch_global_ip_list(): void {
232 if ( true === $this->model->enabled ) {
233 $this->service->fetch_global_ip_list();
234 }
235 }
236
237 /**
238 * Refresh Global IP list.
239 *
240 * @return Response
241 * @defender_route
242 * @since 3.4.0
243 */
244 public function refresh_global_ip_list(): Response {
245 $data = $this->service->fetch_global_ip_list();
246
247 if ( ! is_wp_error( $data ) ) {
248 return new Response(
249 true,
250 array(
251 'message' => esc_html__(
252 'The Custom IP List has been updated successfully.',
253 'defender-security'
254 ),
255 'global_ip_list' => $this->service->get_formated_global_ip_list(),
256 )
257 );
258 } else {
259 return new Response(
260 false,
261 array(
262 'message' => esc_html__(
263 'An error occurred while synchronizing the Custom IP List.',
264 'defender-security'
265 ),
266 )
267 );
268 }
269 }
270
271 /**
272 * Add an IP to blacklist.
273 *
274 * @param string $ip The IP to blacklist.
275 *
276 * @return void
277 */
278 public function blacklist_an_ip( string $ip ): void {
279 $data = array(
280 'block_list' => array( $ip ),
281 );
282 $this->service->add_to_global_ip_list( $data );
283 }
284
285 /**
286 * Converts the current object state to an array.
287 *
288 * @return array The array representation of the object.
289 */
290 public function to_array(): array {
291 return array();
292 }
293
294 /**
295 * Imports data into the model.
296 *
297 * @param array $data Data to be imported into the model.
298 */
299 public function import_data( array $data ) {
300 $model = $this->model;
301 if ( isset( $data['global_ip_list'] ) ) {
302 $model->enabled = (bool) $data['global_ip_list'];
303 if ( isset( $data['global_ip_list_blocklist_autosync'] ) ) {
304 $model->blocklist_autosync = (bool) $data['global_ip_list_blocklist_autosync'];
305 }
306 } else {
307 $model->enabled = false;
308 $model->blocklist_autosync = false;
309 }
310 $model->save();
311 }
312
313 /**
314 * Removes settings for all submodules.
315 */
316 public function remove_settings() {
317 }
318
319 /**
320 * Delete all the data & the cache.
321 */
322 public function remove_data() {
323 delete_site_transient( Global_IP_Component::LIST_KEY );
324 delete_site_option( Global_IP_Component::LIST_LAST_SYNCED_KEY );
325 $this->service->delete_dashboard_notice_reminder();
326 }
327
328 /**
329 * Exports strings.
330 *
331 * @return array An array of strings.
332 */
333 public function export_strings() {
334 return array();
335 }
336 }
337