| 1 |
<?php |
| 2 |
/** |
| 3 |
* Handles the display and management of lockout-related data in tables. |
| 4 |
* |
| 5 |
* @package WP_Defender\Component |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace WP_Defender\Component; |
| 9 |
|
| 10 |
use WP_Defender\Component; |
| 11 |
use WP_Defender\Traits\Formats; |
| 12 |
use WP_Defender\Model\Lockout_Ip; |
| 13 |
use WP_Defender\Model\Lockout_Log; |
| 14 |
|
| 15 |
/** |
| 16 |
* Handles the display and management of lockout-related data in tables. |
| 17 |
*/ |
| 18 |
class Table_Lockout extends Component { |
| 19 |
|
| 20 |
use Formats; |
| 21 |
|
| 22 |
public const STATUS_BAN = 'ban', STATUS_NOT_BAN = 'not_ban', STATUS_ALLOWLIST = 'allowlist'; |
| 23 |
public const SORT_DESC = 'latest', SORT_ASC = 'oldest', SORT_BY_IP = 'ip', SORT_BY_UA = 'user_agent'; |
| 24 |
public const LIMIT_20 = '20', LIMIT_50 = '50', LIMIT_100 = '100', LIMIT_ALL = '-1'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Retrieves the status text for a given IP address. |
| 28 |
* |
| 29 |
* @param string $ip The IP address to check. |
| 30 |
* |
| 31 |
* @return string The status text related to the IP address. |
| 32 |
*/ |
| 33 |
public function get_ip_status_text( $ip ): string { |
| 34 |
$bl_component = new Blacklist_Lockout(); |
| 35 |
if ( $bl_component->is_ip_whitelisted( $ip ) ) { |
| 36 |
return esc_html__( 'Is allowlisted', 'defender-security' ); |
| 37 |
} |
| 38 |
if ( $bl_component->is_blacklist( $ip ) ) { |
| 39 |
return esc_html__( 'Is blocklisted', 'defender-security' ); |
| 40 |
} |
| 41 |
|
| 42 |
$model = Lockout_Ip::get( $ip ); |
| 43 |
if ( ! is_object( $model ) ) { |
| 44 |
return esc_html__( 'Not banned', 'defender-security' ); |
| 45 |
} |
| 46 |
|
| 47 |
if ( Lockout_Ip::STATUS_BLOCKED === $model->status ) { |
| 48 |
return esc_html__( 'Banned', 'defender-security' ); |
| 49 |
} elseif ( Lockout_Ip::STATUS_NORMAL === $model->status ) { |
| 50 |
return esc_html__( 'Not banned', 'defender-security' ); |
| 51 |
} |
| 52 |
|
| 53 |
return ''; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Get types. |
| 58 |
* |
| 59 |
* @return array |
| 60 |
*/ |
| 61 |
private function get_types(): array { |
| 62 |
return array( |
| 63 |
'all' => esc_html__( 'All', 'defender-security' ), |
| 64 |
Lockout_Log::AUTH_FAIL => esc_html__( 'Failed login attempts', 'defender-security' ), |
| 65 |
Lockout_Log::AUTH_LOCK => esc_html__( 'Login lockout', 'defender-security' ), |
| 66 |
Lockout_Log::ERROR_404 => esc_html__( '404 error', 'defender-security' ), |
| 67 |
Lockout_Log::LOCKOUT_404 => esc_html__( '404 lockout', 'defender-security' ), |
| 68 |
Lockout_Log::LOCKOUT_UA => esc_html__( 'User Agent Lockout', 'defender-security' ), |
| 69 |
Lockout_Log::LOCKOUT_BOT_TRAP => esc_html__( 'Bot Trap Lockout', 'defender-security' ), |
| 70 |
// New IP Lockout type since 5.3.0. |
| 71 |
Lockout_Log::LOCKOUT_IP_CUSTOM => esc_html__( 'Custom IP Lockout', 'defender-security' ), |
| 72 |
Lockout_Log::IP_UNLOCK => esc_html__( 'IP Unlock', 'defender-security' ), |
| 73 |
); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Get ban statuses. |
| 78 |
* |
| 79 |
* @return array |
| 80 |
*/ |
| 81 |
private function ban_status(): array { |
| 82 |
return array( |
| 83 |
'all' => esc_html__( 'All', 'defender-security' ), |
| 84 |
self::STATUS_NOT_BAN => esc_html__( 'Not Banned', 'defender-security' ), |
| 85 |
self::STATUS_BAN => esc_html__( 'Banned', 'defender-security' ), |
| 86 |
self::STATUS_ALLOWLIST => esc_html__( 'Allowlisted', 'defender-security' ), |
| 87 |
); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Retrieves the type description based on the type key. |
| 92 |
* |
| 93 |
* @param string $type The type key. |
| 94 |
* |
| 95 |
* @return string The description of the type. |
| 96 |
*/ |
| 97 |
public function get_type( $type ): string { |
| 98 |
$types = $this->get_types(); |
| 99 |
$types[ Lockout_Log::ERROR_404_IGNORE ] = esc_html__( '404 error', 'defender-security' ); |
| 100 |
return $types[ $type ] ?? ''; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Retrieves sorting values. |
| 105 |
* |
| 106 |
* @return array An associative array of sorting options. |
| 107 |
*/ |
| 108 |
private function sort_values(): array { |
| 109 |
return array( |
| 110 |
self::SORT_DESC => esc_html__( 'Latest', 'defender-security' ), |
| 111 |
self::SORT_ASC => esc_html__( 'Oldest', 'defender-security' ), |
| 112 |
self::SORT_BY_IP => esc_html__( 'IP Address', 'defender-security' ), |
| 113 |
self::SORT_BY_UA => esc_html__( 'User agent', 'defender-security' ), |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Retrieves pagination limits. |
| 119 |
* |
| 120 |
* @return array An associative array of pagination limits. |
| 121 |
*/ |
| 122 |
private function limit_per_page(): array { |
| 123 |
return array( |
| 124 |
self::LIMIT_20 => '20', |
| 125 |
self::LIMIT_50 => '50', |
| 126 |
self::LIMIT_100 => '100', |
| 127 |
self::LIMIT_ALL => esc_html__( 'All', 'defender-security' ), |
| 128 |
); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Retrieves all filters used in the lockout table. |
| 133 |
* |
| 134 |
* @return array An associative array of all filters. |
| 135 |
*/ |
| 136 |
public function get_filters(): array { |
| 137 |
return array( |
| 138 |
'lockout_types' => $this->get_types(), |
| 139 |
'ban_status' => $this->ban_status(), |
| 140 |
'sort_values' => $this->sort_values(), |
| 141 |
'limit_logs' => $this->limit_per_page(), |
| 142 |
); |
| 143 |
} |
| 144 |
} |
| 145 |
|