| @@ -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 | * |
| @@ -149,16 +152,23 @@ | ||
| 149 | 152 | * @return void |
| 150 | 153 | * @defender_route |
| 151 | 154 | */ |
| 152 | 155 | public function export_as_csv(): void { |
| 153 | - $date_from = (int) HTTP::get( 'date_from', strtotime( '-7 days midnight' ) ); | |
| 154 | - $date_to = (int) HTTP::get( 'date_to', strtotime( 'tomorrow' ) ); | |
| 155 | - $ip = sanitize_text_field( (string) HTTP::get( 'ip', '' ) ); | |
| 156 | - $user_agent = sanitize_text_field( (string) HTTP::get( 'user_agent', '' ) ); | |
| 157 | - $type = sanitize_text_field( (string) HTTP::get( 'type', '' ) ); | |
| 158 | - $ban_status = sanitize_text_field( (string) HTTP::get( 'ban_status', '' ) ); | |
| 159 | - $sort = sanitize_text_field( (string) HTTP::get( 'sort', Table_Lockout::SORT_DESC ) ); | |
| 160 | - $sort_params = wd_di()->get( Table_Lockout::class )->resolve_sort( $sort ); | |
| 156 | + $timezone = wp_timezone(); | |
| 157 | + $date_from_str = sanitize_text_field( (string) HTTP::get( 'date_from', '' ) ); | |
| 158 | + $date_to_str = sanitize_text_field( (string) HTTP::get( 'date_to', '' ) ); | |
| 159 | + $date_from = $date_from_str | |
| 160 | + ? $this->date_string_to_timestamp( $date_from_str ) | |
| 161 | + : ( new DateTime( '-30 days', $timezone ) )->setTime( 0, 0, 0 )->getTimestamp(); | |
| 162 | + $date_to = $date_to_str | |
| 163 | + ? $this->date_string_to_timestamp( $date_to_str, true ) | |
| 164 | + : ( new DateTime( 'now', $timezone ) )->setTime( 23, 59, 59 )->getTimestamp(); | |
| 165 | + $ip = sanitize_text_field( (string) HTTP::get( 'ip', '' ) ); | |
| 166 | + $user_agent = sanitize_text_field( (string) HTTP::get( 'user_agent', '' ) ); | |
| 167 | + $type = sanitize_text_field( (string) HTTP::get( 'type', '' ) ); | |
| 168 | + $ban_status = sanitize_text_field( (string) HTTP::get( 'ban_status', '' ) ); | |
| 169 | + $sort = sanitize_text_field( (string) HTTP::get( 'sort', Table_Lockout::SORT_DESC ) ); | |
| 170 | + $sort_params = wd_di()->get( Table_Lockout::class )->resolve_sort( $sort ); | |
| 161 | 171 | |
| 162 | 172 | $filters = array( |
| 163 | 173 | 'from' => $date_from, |
| 164 | 174 | 'to' => $date_to, |
| @@ -167,26 +177,8 @@ | ||
| 167 | 177 | 'type' => 'all' === $type ? '' : $type, |
| 168 | 178 | 'ban_status' => 'all' === $ban_status ? '' : $ban_status, |
| 169 | 179 | ); |
| 170 | 180 | |
| 171 | - $logs = Lockout_Log::query_logs( $filters, 1, $sort_params['order_by'], $sort_params['order'], -1 ); | |
| 172 | - | |
| 173 | - $tl_component = new Table_Lockout(); | |
| 174 | - | |
| 175 | - $ua_component = wd_di()->get( User_Agent::class ); | |
| 176 | - | |
| 177 | - $filename = 'wdf-lockout-logs-export-' . wp_date( 'ymdHis' ) . '.csv'; | |
| 178 | - | |
| 179 | - header( 'Expires: 0' ); | |
| 180 | - header( 'Cache-Control: must-revalidate, post-check=0, pre-check=0' ); | |
| 181 | - header( 'Cache-Control: private', false ); | |
| 182 | - header( 'Content-Type: application/octet-stream' ); | |
| 183 | - header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); | |
| 184 | - header( 'Content-Transfer-Encoding: binary' ); | |
| 185 | - | |
| 186 | - extension_loaded( 'zlib' ) ? ob_start( 'ob_gzhandler' ) : ob_start(); | |
| 187 | - | |
| 188 | - $fp = fopen( 'php://output', 'w' ); | |
| 189 | 181 | $headers = array( |
| 190 | 182 | esc_html__( 'Log', 'defender-security' ), |
| 191 | 183 | esc_html__( 'Date / Time', 'defender-security' ), |
| 192 | 184 | esc_html__( 'Type', 'defender-security' ), |
| @@ -194,31 +186,54 @@ | ||
| 194 | 186 | esc_html__( 'IP Status', 'defender-security' ), |
| 195 | 187 | esc_html__( 'User Agent Name', 'defender-security' ), |
| 196 | 188 | esc_html__( 'User Agent Status', 'defender-security' ), |
| 197 | 189 | ); |
| 198 | - fputcsv( $fp, $headers, ',', '"', '\\' ); | |
| 199 | 190 | |
| 200 | - $flush_limit = Lockout_Log::INFINITE_SCROLL_SIZE; | |
| 201 | - foreach ( $logs as $key => $log ) { | |
| 202 | - $item = array( | |
| 203 | - $log->log, | |
| 204 | - $this->format_date_time( $log->date ), | |
| 205 | - $tl_component->get_type( $log->type ), | |
| 206 | - $log->ip, | |
| 207 | - $tl_component->get_ip_status_text( $log->ip ), | |
| 208 | - $log->user_agent, | |
| 209 | - $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 | |
| 210 | 219 | ); |
| 211 | - fputcsv( $fp, $item, ',', '"', '\\' ); | |
| 212 | 220 | |
| 213 | - if ( 0 === $key % $flush_limit ) { | |
| 214 | - ob_flush(); | |
| 215 | - 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 | + ); | |
| 216 | 231 | } |
| 217 | - } | |
| 218 | - // WP_Filesystem is not suitable here because it abstracts to reading/writing files on disk, not to output streams. | |
| 219 | - fclose( $fp ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose | |
| 220 | - exit(); | |
| 232 | + | |
| 233 | + $has_more = count( $logs ) === $per_page; | |
| 234 | + ++$page; | |
| 235 | + } while ( $has_more ); | |
| 221 | 236 | } |
| 222 | 237 | |
| 223 | 238 | /** |
| 224 | 239 | * Toggles an IP address to or from a specified list. |
| @@ -314,13 +329,13 @@ | ||
| 314 | 329 | public function query_logs( Request $request ): Response { |
| 315 | 330 | $data = $request->get_data( |
| 316 | 331 | array( |
| 317 | 332 | 'date_from' => array( |
| 318 | - 'type' => 'int', | |
| 333 | + 'type' => 'string', | |
| 319 | 334 | 'sanitize' => 'sanitize_text_field', |
| 320 | 335 | ), |
| 321 | 336 | 'date_to' => array( |
| 322 | - 'type' => 'int', | |
| 337 | + 'type' => 'string', | |
| 323 | 338 | 'sanitize' => 'sanitize_text_field', |
| 324 | 339 | ), |
| 325 | 340 | 'ip' => array( |
| 326 | 341 | 'type' => 'string', |
| @@ -354,11 +369,15 @@ | ||
| 354 | 369 | ); |
| 355 | 370 | // Validate. |
| 356 | 371 | $v = new Validator( $data, array() ); |
| 357 | 372 | $v->rule( 'required', array( 'date_from', 'date_to' ) ); |
| 358 | - $v->rule( 'integer', array( 'date_from', 'date_to' ) ); | |
| 359 | - $v->rule( 'min', array( 'date_from', 'date_to' ), 0 ); | |
| 360 | 373 | if ( ! $v->validate() ) { |
| 374 | + return new Response( false, array( 'message' => esc_html__( 'Start and end date are required.', 'defender-security' ) ) ); | |
| 375 | + } | |
| 376 | + | |
| 377 | + $date_from = $this->date_string_to_timestamp( $data['date_from'] ); | |
| 378 | + $date_to = $this->date_string_to_timestamp( $data['date_to'], true ); | |
| 379 | + if ( $date_from <= 0 || $date_to <= 0 ) { | |
| 361 | 380 | return new Response( false, array( 'message' => esc_html__( 'Wrong start and end date.', 'defender-security' ) ) ); |
| 362 | 381 | } |
| 363 | 382 | |
| 364 | 383 | $sort = $data['sort'] ?? Table_Lockout::SORT_DESC; |
| @@ -365,10 +384,10 @@ | ||
| 365 | 384 | $sort_params = wd_di()->get( Table_Lockout::class )->resolve_sort( $sort ); |
| 366 | 385 | |
| 367 | 386 | $result = $this->retrieve_logs( |
| 368 | 387 | array( |
| 369 | - 'from' => $data['date_from'], | |
| 370 | - 'to' => $data['date_to'], | |
| 388 | + 'from' => $date_from, | |
| 389 | + 'to' => $date_to, | |
| 371 | 390 | 'ip' => $data['ip'], |
| 372 | 391 | 'user_agent' => $data['user_agent'] ?? '', |
| 373 | 392 | // If this is all, then we set to null to exclude it from the filter. |
| 374 | 393 | 'type' => 'all' === $data['type'] ? '' : $data['type'], |
| @@ -401,19 +420,22 @@ | ||
| 401 | 420 | public function data_frontend(): array { |
| 402 | 421 | $type = defender_get_data_from_request( 'type', 'g' ); |
| 403 | 422 | $ip = defender_get_data_from_request( 'ip', 'g' ); |
| 404 | 423 | $user_agent = defender_get_data_from_request( 'user_agent', 'g' ); |
| 424 | + $timezone = wp_timezone(); | |
| 405 | 425 | |
| 406 | - $init_filters = array( | |
| 407 | - 'from' => strtotime( '-30 days' ), | |
| 408 | - 'to' => time(), | |
| 426 | + $init_filters = array( | |
| 427 | + 'from' => ( new DateTime( '-30 days', $timezone ) )->setTime( 0, 0, 0 )->getTimestamp(), | |
| 428 | + 'to' => ( new DateTime( 'now', $timezone ) )->setTime( 23, 59, 59 )->getTimestamp(), | |
| 409 | 429 | 'type' => $type, |
| 410 | 430 | 'ip' => $ip, |
| 411 | 431 | 'user_agent' => $user_agent, |
| 412 | 432 | 'ban_status' => '', |
| 413 | 433 | ); |
| 414 | - $def_filters = array( | |
| 415 | - '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, | |
| 416 | 438 | 'default_filter' => $init_filters, |
| 417 | 439 | 'per_page' => self::DEFAULT_PER_PAGE, |
| 418 | 440 | ); |
| 419 | 441 | |