PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.3.0
Defender Security – Malware Scanner, Login Security & Firewall v6.3.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
← All changes | src/controller/class-ua-lockout.php +55 -36 5.8.1 → 6.3.0 View file →
@@ -7,13 +7,16 @@
7 7
8 8 namespace WP_Defender\Controller;
9 9
10 10 use Exception;
11 +use Generator;
11 12 use WP_Defender\Event;
12 13 use Calotes\Component\Request;
13 14 use Calotes\Component\Response;
14 15 use WP_Defender\Traits\Setting;
16 +use WP_Defender\Model\Lockout_Log;
15 17 use WP_Defender\Model\Setting\User_Agent_Lockout;
18 +use WP_Defender\Component\Export\Csv;
16 19 use WP_Defender\Component\Config\Config_Hub_Helper;
17 20 use WP_Defender\Component\User_Agent as User_Agent_Service;
18 21
19 22 /**
@@ -76,9 +79,8 @@
76 79 public function enqueue_assets() {
77 80 if ( ! $this->is_page_active() ) {
78 81 return;
79 82 }
80 - wp_localize_script( 'def-iplockout', 'ua_lockout', $this->data_frontend() );
81 83 }
82 84
83 85 /**
84 86 * Save settings.
@@ -90,11 +92,11 @@
90 92 * @throws Exception If the table is not defined.
91 93 */
92 94 public function save_settings( Request $request ) {
93 95 $data = $request->get_data_by_model( $this->model );
94 - $old_enabled = (bool) $this->model->enabled;
95 - $old_malicious_bot_enabled = (bool) $this->model->malicious_bot_enabled;
96 - $old_fake_bots_enabled = (bool) $this->model->fake_bots_enabled;
96 + $old_enabled = $this->model->enabled;
97 + $old_malicious_bot_enabled = $this->model->malicious_bot_enabled;
98 + $old_fake_bots_enabled = $this->model->fake_bots_enabled;
97 99 $prev_data = $this->model->export();
98 100
99 101 $this->model->import( $data );
100 102 if ( $this->model->validate() ) {
@@ -135,9 +137,9 @@
135 137 }
136 138
137 139 $this->model->save();
138 140 Config_Hub_Helper::set_clear_active_flag();
139 -
141 + // Attention: next actions after saving model changes.
140 142 if (
141 143 ( ! $this->model->enabled && $old_enabled ) ||
142 144 ( ! $this->model->malicious_bot_enabled && $old_malicious_bot_enabled )
143 145 ) {
@@ -157,9 +159,13 @@
157 159 }
158 160
159 161 // Maybe track.
160 162 if ( ! defender_is_wp_cli() ) {
161 - if ( $this->is_feature_state_changed( $prev_data, $data ) ) {
163 + // Track if UA Module is activated/deactivated OR there is a change in State on Add/Remove Custom User Agents (Allowlist/Blocklist).
164 + if ( ( $old_enabled !== $data['enabled'] )
165 + || ( $this->is_feature_state_changed( $prev_data['blacklist'], $data['blacklist'] ) )
166 + || ( $this->is_feature_state_changed( $prev_data['whitelist'], $data['whitelist'] ) )
167 + ) {
162 168 $track_data = array(
163 169 'Action' => $data['enabled'] ? 'Enabled' : 'Disabled',
164 170 'No of Bots in the Whitelist' => count( $this->model->get_lockout_list( 'allowlist', false ) ),
165 171 'No of Bots in the Blocklist' => count( $this->model->get_lockout_list( 'blocklist', false ) ),
@@ -267,8 +273,9 @@
267 273 'module_name' => User_Agent_Lockout::get_module_name(),
268 274 'blocklist_presets' => User_Agent_Service::get_blocklist_presets(),
269 275 'script_presets' => User_Agent_Service::get_script_presets(),
270 276 'has_empty_disallow_line' => $has_empty_disallow,
277 + 'lockouts_last_24_hours' => Lockout_Log::count_ua_lockouts_in_24_hours(),
271 278 );
272 279
273 280 return array_merge(
274 281 array(
@@ -340,34 +347,34 @@
340 347 * @defender_route
341 348 * @since 2.6.0
342 349 */
343 350 public function export_ua(): void {
344 - $data = array();
351 + try {
352 + Csv::to_browser( 'ua', $this->get_export_rows() );
353 + } catch ( \RuntimeException $e ) {
354 + $this->log( $e->getMessage(), 'ua-lockout' );
355 + }
356 + }
345 357
346 - foreach ( $this->model->get_lockout_list( 'blocklist', false ) as $ua ) {
347 - $data[] = array(
358 + /**
359 + * Generates User Agent export rows.
360 + *
361 + * @return Generator
362 + */
363 + private function get_export_rows(): Generator {
364 + foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) {
365 + yield array(
348 366 'ua' => $ua,
349 - 'type' => 'blocklist',
367 + 'type' => 'allowlist',
350 368 );
351 369 }
352 - foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) {
353 - $data[] = array(
370 +
371 + foreach ( $this->model->get_all_selected_blocklist_ua( false ) as $ua ) {
372 + yield array(
354 373 'ua' => $ua,
355 - 'type' => 'allowlist',
374 + 'type' => 'blocklist',
356 375 );
357 376 }
358 - // WP_Filesystem class doesn’t directly provide a function for opening a stream to php://memory with the 'w' mode.
359 - $fp = fopen( 'php://memory', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen
360 - foreach ( $data as $fields ) {
361 - fputcsv( $fp, $fields, ',', '"', '\\' );
362 - }
363 - $filename = 'wdf-ua-export-' . wp_date( 'ymdHis' ) . '.csv';
364 - fseek( $fp, 0 );
365 - header( 'Content-Type: text/csv' );
366 - header( 'Content-Disposition: attachment; filename="' . $filename . '";' );
367 - // Make php send the generated csv lines to the browser.
368 - fpassthru( $fp );
369 - exit();
370 377 }
371 378
372 379 /**
373 380 * Importing UAs from exporter.
@@ -385,18 +392,27 @@
385 392 ),
386 393 )
387 394 );
388 395
389 - $attached_id = $data['id'];
390 - if ( ! is_object( get_post( $attached_id ) ) ) {
391 - return new Response(
392 - false,
393 - array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) )
394 - );
396 + $file = '';
397 + $attached_id = $data['id'] ?? 0;
398 + if ( 0 < $attached_id ) {
399 + if ( ! is_object( get_post( $attached_id ) ) ) {
400 + return new Response(
401 + false,
402 + array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) )
403 + );
404 + }
405 +
406 + $file = get_attached_file( $attached_id );
407 + } else {
408 + $file_data = defender_get_data_from_request( 'file', 'f' );
409 + if ( is_array( $file_data ) && isset( $file_data['tmp_name'] ) && is_uploaded_file( $file_data['tmp_name'] ) ) {
410 + $file = $file_data['tmp_name'];
411 + }
395 412 }
396 413
397 - $file = get_attached_file( $attached_id );
398 - if ( ! is_file( $file ) ) {
414 + if ( ! is_string( $file ) || '' === $file || ! is_file( $file ) ) {
399 415 return new Response(
400 416 false,
401 417 array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) )
402 418 );
@@ -418,11 +434,14 @@
418 434 }
419 435
420 436 return new Response(
421 437 true,
422 - array(
423 - 'message' => esc_html__( 'Your blocklist and allowlist have been successfully imported.', 'defender-security' ),
424 - 'interval' => 1,
438 + array_merge(
439 + array(
440 + 'message' => esc_html__( 'Your blocklist and allowlist have been successfully imported.', 'defender-security' ),
441 + 'auto_close' => true,
442 + ),
443 + $this->data_frontend()
425 444 )
426 445 );
427 446 }
428 447