| @@ -8,8 +8,9 @@ | ||
| 8 | 8 | namespace WP_Defender\Controller; |
| 9 | 9 | |
| 10 | 10 | use DateTime; |
| 11 | 11 | use Exception; |
| 12 | +use Generator; | |
| 12 | 13 | use Valitron\Validator; |
| 13 | 14 | use Calotes\Helper\HTTP; |
| 14 | 15 | use WP_Defender\Controller; |
| 15 | 16 | use Calotes\Component\Request; |
| @@ -14,15 +15,16 @@ | ||
| 14 | 15 | use WP_Defender\Controller; |
| 15 | 16 | use Calotes\Component\Request; |
| 16 | 17 | use Calotes\Component\Response; |
| 17 | 18 | use WP_Defender\Traits\Formats; |
| 19 | +use WP_Defender\Traits\Country; | |
| 18 | 20 | use WP_Defender\Behavior\WPMUDEV; |
| 19 | 21 | use WP_Defender\Model\Lockout_Log; |
| 20 | 22 | use WP_Defender\Component\User_Agent; |
| 21 | -use WP_Defender\Component\IP\Global_IP; | |
| 22 | 23 | use WP_Defender\Component\Table_Lockout; |
| 23 | 24 | use WP_Defender\Component\Network_Cron_Manager; |
| 24 | 25 | use WP_Defender\Integrations\Antibot_Global_Firewall_Client; |
| 26 | +use WP_Defender\Component\Export\Csv; | |
| 25 | 27 | use WP_Defender\Model\Setting\Blacklist_Lockout; |
| 26 | 28 | use WP_Defender\Model\Setting\User_Agent_Lockout; |
| 27 | 29 | use WP_Defender\Component\Firewall_Logs as Firewall_Logs_Component; |
| 28 | 30 | |
| @@ -32,8 +34,9 @@ | ||
| 32 | 34 | */ |
| 33 | 35 | class Firewall_Logs extends Controller { |
| 34 | 36 | |
| 35 | 37 | use Formats; |
| 38 | + use Country; | |
| 36 | 39 | |
| 37 | 40 | /** |
| 38 | 41 | * The slug identifier for this controller. |
| 39 | 42 | * |
| @@ -174,26 +177,8 @@ | ||
| 174 | 177 | 'type' => 'all' === $type ? '' : $type, |
| 175 | 178 | 'ban_status' => 'all' === $ban_status ? '' : $ban_status, |
| 176 | 179 | ); |
| 177 | 180 | |
| 178 | - $logs = Lockout_Log::query_logs( $filters, 1, $sort_params['order_by'], $sort_params['order'], -1 ); | |
| 179 | - | |
| 180 | - $tl_component = new Table_Lockout(); | |
| 181 | - | |
| 182 | - $ua_component = wd_di()->get( User_Agent::class ); | |
| 183 | - | |
| 184 | - $filename = 'wdf-lockout-logs-export-' . wp_date( 'ymdHis' ) . '.csv'; | |
| 185 | - | |
| 186 | - header( 'Expires: 0' ); | |
| 187 | - header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); | |
| 188 | - header( 'Cache-Control: private', false ); | |
| 189 | - header( 'Content-Type: application/octet-stream' ); | |
| 190 | - header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); | |
| 191 | - header( 'Content-Transfer-Encoding: binary' ); | |
| 192 | - | |
| 193 | - extension_loaded( 'zlib' ) ? ob_start( 'ob_gzhandler' ) : ob_start(); | |
| 194 | - | |
| 195 | - $fp = fopen( 'php://output', 'w' ); | |
| 196 | 181 | $headers = array( |
| 197 | 182 | esc_html__( 'Log', 'defender-security' ), |
| 198 | 183 | esc_html__( 'Date / Time', 'defender-security' ), |
| 199 | 184 | esc_html__( 'Type', 'defender-security' ), |
| @@ -201,31 +186,54 @@ | ||
| 201 | 186 | esc_html__( 'IP Status', 'defender-security' ), |
| 202 | 187 | esc_html__( 'User Agent Name', 'defender-security' ), |
| 203 | 188 | esc_html__( 'User Agent Status', 'defender-security' ), |
| 204 | 189 | ); |
| 205 | - fputcsv( $fp, $headers, ',', '"', '\\' ); | |
| 206 | 190 | |
| 207 | - $flush_limit = Lockout_Log::INFINITE_SCROLL_SIZE; | |
| 208 | - foreach ( $logs as $key => $log ) { | |
| 209 | - $item = array( | |
| 210 | - $log->log, | |
| 211 | - $this->format_date_time( $log->date ), | |
| 212 | - $tl_component->get_type( $log->type ), | |
| 213 | - $log->ip, | |
| 214 | - $tl_component->get_ip_status_text( $log->ip ), | |
| 215 | - $log->user_agent, | |
| 216 | - $ua_component->get_status_text( $log->type, $log->tried ), | |
| 191 | + try { | |
| 192 | + Csv::to_browser( 'lockout-logs', $this->get_export_rows( $filters, $sort_params ), $headers ); | |
| 193 | + } catch ( \RuntimeException $e ) { | |
| 194 | + $this->log( $e->getMessage(), 'firewall-logs' ); | |
| 195 | + } | |
| 196 | + } | |
| 197 | + | |
| 198 | + /** | |
| 199 | + * Generates firewall log export rows in database-backed pages. | |
| 200 | + * | |
| 201 | + * @param array $filters Log filters. | |
| 202 | + * @param array $sort_params Sort parameters. | |
| 203 | + * | |
| 204 | + * @return Generator | |
| 205 | + */ | |
| 206 | + private function get_export_rows( array $filters, array $sort_params ): Generator { | |
| 207 | + $page = 1; | |
| 208 | + $per_page = 1000; | |
| 209 | + $tl_component = new Table_Lockout(); | |
| 210 | + $ua_component = wd_di()->get( User_Agent::class ); | |
| 211 | + | |
| 212 | + do { | |
| 213 | + $logs = Lockout_Log::query_logs( | |
| 214 | + $filters, | |
| 215 | + $page, | |
| 216 | + $sort_params['order_by'], | |
| 217 | + $sort_params['order'], | |
| 218 | + $per_page | |
| 217 | 219 | ); |
| 218 | - fputcsv( $fp, $item, ',', '"', '\\' ); | |
| 219 | 220 | |
| 220 | - if ( 0 === $key % $flush_limit ) { | |
| 221 | - ob_flush(); | |
| 222 | - flush(); | |
| 221 | + foreach ( $logs as $log ) { | |
| 222 | + yield array( | |
| 223 | + $log->log, | |
| 224 | + $this->format_date_time( $log->date ), | |
| 225 | + $tl_component->get_type( $log->type ), | |
| 226 | + $log->ip, | |
| 227 | + $tl_component->get_ip_status_text( $log->ip ), | |
| 228 | + $log->user_agent, | |
| 229 | + $ua_component->get_status_text( $log->type, $log->tried ), | |
| 230 | + ); | |
| 223 | 231 | } |
| 224 | - } | |
| 225 | - // WP_Filesystem is not suitable here because it abstracts to reading/writing files on disk, not to output streams. | |
| 226 | - fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 227 | - exit(); | |
| 232 | + | |
| 233 | + $has_more = count( $logs ) === $per_page; | |
| 234 | + ++$page; | |
| 235 | + } while ( $has_more ); | |
| 228 | 236 | } |
| 229 | 237 | |
| 230 | 238 | /** |
| 231 | 239 | * Toggles an IP address to or from a specified list. |
| @@ -414,9 +422,9 @@ | ||
| 414 | 422 | $ip = defender_get_data_from_request( 'ip', 'g' ); |
| 415 | 423 | $user_agent = defender_get_data_from_request( 'user_agent', 'g' ); |
| 416 | 424 | $timezone = wp_timezone(); |
| 417 | 425 | |
| 418 | - $init_filters = array( | |
| 426 | + $init_filters = array( | |
| 419 | 427 | 'from' => ( new DateTime( '-30 days', $timezone ) )->setTime( 0, 0, 0 )->getTimestamp(), |
| 420 | 428 | 'to' => ( new DateTime( 'now', $timezone ) )->setTime( 23, 59, 59 )->getTimestamp(), |
| 421 | 429 | 'type' => $type, |
| 422 | 430 | 'ip' => $ip, |
| @@ -422,10 +430,12 @@ | ||
| 422 | 430 | 'ip' => $ip, |
| 423 | 431 | 'user_agent' => $user_agent, |
| 424 | 432 | 'ban_status' => '', |
| 425 | 433 | ); |
| 426 | - $def_filters = array( | |
| 427 | - 'misc' => wd_di()->get( Table_Lockout::class )->get_filters(), | |
| 434 | + $misc = wd_di()->get( Table_Lockout::class )->get_filters(); | |
| 435 | + $misc['countries'] = $this->countries_list(); | |
| 436 | + $def_filters = array( | |
| 437 | + 'misc' => $misc, | |
| 428 | 438 | 'default_filter' => $init_filters, |
| 429 | 439 | 'per_page' => self::DEFAULT_PER_PAGE, |
| 430 | 440 | ); |
| 431 | 441 | |