| @@ -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. |
| @@ -271,8 +273,9 @@ | ||
| 271 | 273 | 'module_name' => User_Agent_Lockout::get_module_name(), |
| 272 | 274 | 'blocklist_presets' => User_Agent_Service::get_blocklist_presets(), |
| 273 | 275 | 'script_presets' => User_Agent_Service::get_script_presets(), |
| 274 | 276 | 'has_empty_disallow_line' => $has_empty_disallow, |
| 277 | + 'lockouts_last_24_hours' => Lockout_Log::count_ua_lockouts_in_24_hours(), | |
| 275 | 278 | ); |
| 276 | 279 | |
| 277 | 280 | return array_merge( |
| 278 | 281 | array( |
| @@ -344,34 +347,34 @@ | ||
| 344 | 347 | * @defender_route |
| 345 | 348 | * @since 2.6.0 |
| 346 | 349 | */ |
| 347 | 350 | public function export_ua(): void { |
| 348 | - $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 | + } | |
| 349 | 357 | |
| 350 | - foreach ( $this->model->get_lockout_list( 'blocklist', false ) as $ua ) { | |
| 351 | - $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( | |
| 352 | 366 | 'ua' => $ua, |
| 353 | - 'type' => 'blocklist', | |
| 367 | + 'type' => 'allowlist', | |
| 354 | 368 | ); |
| 355 | 369 | } |
| 356 | - foreach ( $this->model->get_lockout_list( 'allowlist', false ) as $ua ) { | |
| 357 | - $data[] = array( | |
| 370 | + | |
| 371 | + foreach ( $this->model->get_all_selected_blocklist_ua( false ) as $ua ) { | |
| 372 | + yield array( | |
| 358 | 373 | 'ua' => $ua, |
| 359 | - 'type' => 'allowlist', | |
| 374 | + 'type' => 'blocklist', | |
| 360 | 375 | ); |
| 361 | 376 | } |
| 362 | - // WP_Filesystem class doesn’t directly provide a function for opening a stream to php://memory with the 'w' mode. | |
| 363 | - $fp = fopen( 'php://memory', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen | |
| 364 | - foreach ( $data as $fields ) { | |
| 365 | - fputcsv( $fp, $fields, ',', '"', '\\' ); | |
| 366 | - } | |
| 367 | - $filename = 'wdf-ua-export-' . wp_date( 'ymdHis' ) . '.csv'; | |
| 368 | - fseek( $fp, 0 ); | |
| 369 | - header( 'Content-Type: text/csv' ); | |
| 370 | - header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); | |
| 371 | - // Make php send the generated csv lines to the browser. | |
| 372 | - fpassthru( $fp ); | |
| 373 | - exit(); | |
| 374 | 377 | } |
| 375 | 378 | |
| 376 | 379 | /** |
| 377 | 380 | * Importing UAs from exporter. |
| @@ -389,18 +392,27 @@ | ||
| 389 | 392 | ), |
| 390 | 393 | ) |
| 391 | 394 | ); |
| 392 | 395 | |
| 393 | - $attached_id = $data['id']; | |
| 394 | - if ( ! is_object( get_post( $attached_id ) ) ) { | |
| 395 | - return new Response( | |
| 396 | - false, | |
| 397 | - array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) ) | |
| 398 | - ); | |
| 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 | + } | |
| 399 | 412 | } |
| 400 | 413 | |
| 401 | - $file = get_attached_file( $attached_id ); | |
| 402 | - if ( ! is_file( $file ) ) { | |
| 414 | + if ( ! is_string( $file ) || '' === $file || ! is_file( $file ) ) { | |
| 403 | 415 | return new Response( |
| 404 | 416 | false, |
| 405 | 417 | array( 'message' => esc_html__( 'Your file is invalid!', 'defender-security' ) ) |
| 406 | 418 | ); |
| @@ -422,11 +434,14 @@ | ||
| 422 | 434 | } |
| 423 | 435 | |
| 424 | 436 | return new Response( |
| 425 | 437 | true, |
| 426 | - array( | |
| 427 | - 'message' => esc_html__( 'Your blocklist and allowlist have been successfully imported.', 'defender-security' ), | |
| 428 | - '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() | |
| 429 | 444 | ) |
| 430 | 445 | ); |
| 431 | 446 | } |
| 432 | 447 | |