| @@ -369,10 +369,10 @@ | ||
| 369 | 369 | * |
| 370 | 370 | * @return array |
| 371 | 371 | */ |
| 372 | 372 | public function get_access_status( $ua ): array { |
| 373 | - $blocklist = str_replace( '#', '\#', $this->get_all_selected_blocklist_ua() ); | |
| 374 | - $allowlist = str_replace( '#', '\#', $this->get_lockout_list( 'allowlist' ) ); | |
| 373 | + $blocklist = $this->get_all_selected_blocklist_ua(); | |
| 374 | + $allowlist = $this->get_lockout_list( 'allowlist' ); | |
| 375 | 375 | |
| 376 | 376 | // Escape regex special characters in each item before building the pattern. |
| 377 | 377 | $blocklist_escaped = array_map( 'preg_quote', $blocklist, array_fill( 0, count( $blocklist ), '#' ) ); |
| 378 | 378 | $allowlist_escaped = array_map( 'preg_quote', $allowlist, array_fill( 0, count( $allowlist ), '#' ) ); |
| @@ -379,12 +379,12 @@ | ||
| 379 | 379 | |
| 380 | 380 | $blocklist_regex_pattern = '#' . implode( '|', $blocklist_escaped ) . '#i'; |
| 381 | 381 | $allowlist_regex_pattern = '#' . implode( '|', $allowlist_escaped ) . '#i'; |
| 382 | 382 | |
| 383 | - $blocklist_match = preg_match( $blocklist_regex_pattern, $ua ); | |
| 384 | - $allowlist_match = preg_match( $allowlist_regex_pattern, $ua ); | |
| 383 | + $blocklist_match = count( $blocklist ) > 0 && 1 === preg_match( $blocklist_regex_pattern, $ua ); | |
| 384 | + $allowlist_match = count( $allowlist ) > 0 && 1 === preg_match( $allowlist_regex_pattern, $ua ); | |
| 385 | 385 | |
| 386 | - if ( 1 !== $blocklist_match && 1 !== $allowlist_match ) { | |
| 386 | + if ( ! $blocklist_match && ! $allowlist_match ) { | |
| 387 | 387 | return array( 'na' ); |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | $result = array(); |
| @@ -389,14 +389,14 @@ | ||
| 389 | 389 | |
| 390 | 390 | $result = array(); |
| 391 | 391 | |
| 392 | 392 | // Check blocklist first - if it matches, add 'banned'. |
| 393 | - if ( 1 === $blocklist_match ) { | |
| 393 | + if ( $blocklist_match ) { | |
| 394 | 394 | $result[] = 'banned'; |
| 395 | 395 | } |
| 396 | 396 | |
| 397 | 397 | // Check allowlist - if it matches, add 'allowlist'. |
| 398 | - if ( 1 === $allowlist_match ) { | |
| 398 | + if ( $allowlist_match ) { | |
| 399 | 399 | $result[] = 'allowlist'; |
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | return $result; |
| @@ -402,100 +402,8 @@ | ||
| 402 | 402 | return $result; |
| 403 | 403 | } |
| 404 | 404 | |
| 405 | 405 | /** |
| 406 | - * Checks if a user agent is present in a given list. | |
| 407 | - * | |
| 408 | - * @param string $ua The user agent to check. | |
| 409 | - * @param string $collection The type of list to check against ('blocklist' or 'allowlist'). | |
| 410 | - * | |
| 411 | - * @return bool Returns true if the user agent is in the list, false otherwise. | |
| 412 | - */ | |
| 413 | - public function is_ua_in_list( $ua, $collection ): bool { | |
| 414 | - if ( 'allowlist' === $collection ) { | |
| 415 | - $arr = str_replace( '#', '\#', $this->get_lockout_list( $collection ) ); | |
| 416 | - } else { | |
| 417 | - $arr = str_replace( '#', '\#', $this->get_all_selected_blocklist_ua() ); | |
| 418 | - } | |
| 419 | - | |
| 420 | - // Escape regex special characters in each item before building the pattern. | |
| 421 | - $arr_escaped = array_map( 'preg_quote', $arr, array_fill( 0, count( $arr ), '#' ) ); | |
| 422 | - $list_regex_pattern = '#' . implode( '|', $arr_escaped ) . '#i'; | |
| 423 | - | |
| 424 | - return 1 === preg_match( $list_regex_pattern, $ua ); | |
| 425 | - } | |
| 426 | - | |
| 427 | - /** | |
| 428 | - * Remove User Agent from a list. | |
| 429 | - * | |
| 430 | - * @param string $ua The user agent to remove. | |
| 431 | - * @param string $collection blocklist|allowlist. | |
| 432 | - * | |
| 433 | - * @return void | |
| 434 | - */ | |
| 435 | - public function remove_from_list( $ua, $collection ) { | |
| 436 | - if ( 'allowlist' === $collection ) { | |
| 437 | - $arr = $this->get_lockout_list( $collection ); | |
| 438 | - // Array can contain uppercase. | |
| 439 | - $orig_arr = str_replace( '#', '\#', $this->get_lockout_list( $collection, false ) ); | |
| 440 | - } else { | |
| 441 | - $arr = $this->get_all_selected_blocklist_ua(); | |
| 442 | - // Array can contain uppercase. | |
| 443 | - $orig_arr = str_replace( '#', '\#', $this->get_all_selected_blocklist_ua( false ) ); | |
| 444 | - } | |
| 445 | - | |
| 446 | - $list_regex_pattern = '#' . implode( '|', $arr ) . '#i'; | |
| 447 | - $list_match = preg_match( $list_regex_pattern, $ua ); | |
| 448 | - if ( false !== $list_match ) { | |
| 449 | - if ( 'blocklist' === $collection ) { | |
| 450 | - // Check in 'Blocklist Presets'. | |
| 451 | - if ( $this->blocklist_presets ) { | |
| 452 | - $key = array_search( $ua, $this->blocklist_preset_values, true ); | |
| 453 | - if ( false !== $key ) { | |
| 454 | - unset( $this->blocklist_preset_values[ $key ] ); | |
| 455 | - $this->blocklist_preset_values = array_values( $this->blocklist_preset_values ); | |
| 456 | - } | |
| 457 | - } | |
| 458 | - // Check in 'Script Presets' using Regex. | |
| 459 | - if ( $this->script_presets ) { | |
| 460 | - if ( false !== strpos( $ua, User_Agent_Service::GO_HTTP_CLIENT_KEY . '/' ) ) { | |
| 461 | - $key_script_preset = User_Agent_Service::GO_HTTP_CLIENT_KEY; | |
| 462 | - } elseif ( false !== strpos( $ua, User_Agent_Service::PYTHON_REQUESTS_KEY . '/' ) ) { | |
| 463 | - $key_script_preset = User_Agent_Service::PYTHON_REQUESTS_KEY; | |
| 464 | - } else { | |
| 465 | - $key_script_preset = ''; | |
| 466 | - } | |
| 467 | - | |
| 468 | - $key = array_search( $key_script_preset, $this->script_preset_values, true ); | |
| 469 | - if ( false !== $key ) { | |
| 470 | - unset( $this->script_preset_values[ $key ] ); | |
| 471 | - $this->script_preset_values = array_values( $this->script_preset_values ); | |
| 472 | - } | |
| 473 | - } | |
| 474 | - // Check 'Custom User Agents' case. | |
| 475 | - $arr_blocklist = $this->get_lockout_list( 'blocklist', false ); | |
| 476 | - if ( array() !== $arr_blocklist ) { | |
| 477 | - $key = array_search( $ua, $arr_blocklist, true ); | |
| 478 | - if ( false !== $key && isset( $arr_blocklist[ $key ] ) ) { | |
| 479 | - unset( $arr_blocklist[ $key ] ); | |
| 480 | - // Convert back to string. | |
| 481 | - $this->blacklist = implode( PHP_EOL, $arr_blocklist ); | |
| 482 | - } | |
| 483 | - } | |
| 484 | - } elseif ( 'allowlist' === $collection ) { | |
| 485 | - $key = array_search( $ua, $arr, true ); | |
| 486 | - if ( false !== $key && isset( $arr[ $key ] ) ) { | |
| 487 | - unset( $arr[ $key ] ); | |
| 488 | - // Convert back to string. | |
| 489 | - $this->whitelist = implode( PHP_EOL, $arr ); | |
| 490 | - } | |
| 491 | - } | |
| 492 | - | |
| 493 | - $this->save(); | |
| 494 | - } | |
| 495 | - } | |
| 496 | - | |
| 497 | - /** | |
| 498 | 406 | * Add an UA to the list. |
| 499 | 407 | * |
| 500 | 408 | * @param string $ua User agent name. |
| 501 | 409 | * @param string $collection blocklist|allowlist. |
| @@ -539,19 +447,17 @@ | ||
| 539 | 447 | /** |
| 540 | 448 | * Filter the UAs, as we use a textarea to submit, so it can contain some un-valid UAs. |
| 541 | 449 | */ |
| 542 | 450 | protected function after_validate(): void { |
| 543 | - $lists = array( | |
| 544 | - 'blacklist' => $this->get_lockout_list( 'blocklist' ), | |
| 545 | - 'whitelist' => $this->get_lockout_list( 'allowlist' ), | |
| 451 | + $this->blacklist = implode( | |
| 452 | + PHP_EOL, | |
| 453 | + array_filter( $this->get_lockout_list( 'blocklist' ), 'strlen' ) | |
| 546 | 454 | ); |
| 547 | 455 | |
| 548 | - foreach ( $lists as $key => &$collection ) { | |
| 549 | - // If UA collection is not valid, we should display an error message. We'll improve it by UA standard/pattern. | |
| 550 | - if ( property_exists( $this, $key ) ) { | |
| 551 | - $this->$key = implode( PHP_EOL, array_filter( $collection, 'strlen' ) ); | |
| 552 | - } | |
| 553 | - } | |
| 456 | + $this->whitelist = implode( | |
| 457 | + PHP_EOL, | |
| 458 | + array_filter( $this->get_lockout_list( 'allowlist' ), 'strlen' ) | |
| 459 | + ); | |
| 554 | 460 | } |
| 555 | 461 | |
| 556 | 462 | /** |
| 557 | 463 | * Get the module name. |