| @@ -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; |
| @@ -18,12 +19,12 @@ | ||
| 18 | 19 | use WP_Defender\Traits\Country; |
| 19 | 20 | use WP_Defender\Behavior\WPMUDEV; |
| 20 | 21 | use WP_Defender\Model\Lockout_Log; |
| 21 | 22 | use WP_Defender\Component\User_Agent; |
| 22 | -use WP_Defender\Component\IP\Global_IP; | |
| 23 | 23 | use WP_Defender\Component\Table_Lockout; |
| 24 | 24 | use WP_Defender\Component\Network_Cron_Manager; |
| 25 | 25 | use WP_Defender\Integrations\Antibot_Global_Firewall_Client; |
| 26 | +use WP_Defender\Component\Export\Csv; | |
| 26 | 27 | use WP_Defender\Model\Setting\Blacklist_Lockout; |
| 27 | 28 | use WP_Defender\Model\Setting\User_Agent_Lockout; |
| 28 | 29 | use WP_Defender\Component\Firewall_Logs as Firewall_Logs_Component; |
| 29 | 30 | |
| @@ -176,26 +177,8 @@ | ||
| 176 | 177 | 'type' => 'all' === $type ? '' : $type, |
| 177 | 178 | 'ban_status' => 'all' === $ban_status ? '' : $ban_status, |
| 178 | 179 | ); |
| 179 | 180 | |
| 180 | - $logs = Lockout_Log::query_logs( $filters, 1, $sort_params['order_by'], $sort_params['order'], -1 ); | |
| 181 | - | |
| 182 | - $tl_component = new Table_Lockout(); | |
| 183 | - | |
| 184 | - $ua_component = wd_di()->get( User_Agent::class ); | |
| 185 | - | |
| 186 | - $filename = 'wdf-lockout-logs-export-' . wp_date( 'ymdHis' ) . '.csv'; | |
| 187 | - | |
| 188 | - header( 'Expires: 0' ); | |
| 189 | - header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); | |
| 190 | - header( 'Cache-Control: private', false ); | |
| 191 | - header( 'Content-Type: application/octet-stream' ); | |
| 192 | - header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); | |
| 193 | - header( 'Content-Transfer-Encoding: binary' ); | |
| 194 | - | |
| 195 | - extension_loaded( 'zlib' ) ? ob_start( 'ob_gzhandler' ) : ob_start(); | |
| 196 | - | |
| 197 | - $fp = fopen( 'php://output', 'w' ); | |
| 198 | 181 | $headers = array( |
| 199 | 182 | esc_html__( 'Log', 'defender-security' ), |
| 200 | 183 | esc_html__( 'Date / Time', 'defender-security' ), |
| 201 | 184 | esc_html__( 'Type', 'defender-security' ), |
| @@ -203,31 +186,54 @@ | ||
| 203 | 186 | esc_html__( 'IP Status', 'defender-security' ), |
| 204 | 187 | esc_html__( 'User Agent Name', 'defender-security' ), |
| 205 | 188 | esc_html__( 'User Agent Status', 'defender-security' ), |
| 206 | 189 | ); |
| 207 | - fputcsv( $fp, $headers, ',', '"', '\\' ); | |
| 208 | 190 | |
| 209 | - $flush_limit = Lockout_Log::INFINITE_SCROLL_SIZE; | |
| 210 | - foreach ( $logs as $key => $log ) { | |
| 211 | - $item = array( | |
| 212 | - $log->log, | |
| 213 | - $this->format_date_time( $log->date ), | |
| 214 | - $tl_component->get_type( $log->type ), | |
| 215 | - $log->ip, | |
| 216 | - $tl_component->get_ip_status_text( $log->ip ), | |
| 217 | - $log->user_agent, | |
| 218 | - $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 | |
| 219 | 219 | ); |
| 220 | - fputcsv( $fp, $item, ',', '"', '\\' ); | |
| 221 | 220 | |
| 222 | - if ( 0 === $key % $flush_limit ) { | |
| 223 | - ob_flush(); | |
| 224 | - 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 | + ); | |
| 225 | 231 | } |
| 226 | - } | |
| 227 | - // WP_Filesystem is not suitable here because it abstracts to reading/writing files on disk, not to output streams. | |
| 228 | - fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 229 | - exit(); | |
| 232 | + | |
| 233 | + $has_more = count( $logs ) === $per_page; | |
| 234 | + ++$page; | |
| 235 | + } while ( $has_more ); | |
| 230 | 236 | } |
| 231 | 237 | |
| 232 | 238 | /** |
| 233 | 239 | * Toggles an IP address to or from a specified list. |