PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.0
Defender Security – Malware Scanner, Login Security & Firewall v6.2.0
6.3.0 6.2.3 6.2.4 6.2.0 6.2.1 6.2.2 6.1.0 5.3.1 5.4.0 5.4.1 5.5.0 5.5.1 5.6.0 5.6.1 5.6.2 5.7.0 5.7.1 5.7.2 5.8.0 5.8.1 5.9.0 6.0.0 6.0.1 3.0.1 3.1.0 All 141 releases
defender-security / src / component / class-table-lockout.php

class-table-lockout.php in Defender Security – Malware Scanner, Login Security & Firewall 6.2.0, at src/component/class-table-lockout.php

187 lines 6.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_10 = '10', 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__( 'Any', '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_MALICIOUS_BOT => esc_html__( 'Malicious Bot Lockout', 'defender-security' ),
70 Lockout_Log::LOCKOUT_FAKE_BOT => esc_html__( 'Fake Bot Lockout', 'defender-security' ),
71 // New types since 5.3.0.
72 Lockout_Log::LOCKOUT_IP_CUSTOM => esc_html__( 'Custom IP Lockout', 'defender-security' ),
73 Lockout_Log::IP_UNLOCK => esc_html__( 'IP Unlock', 'defender-security' ),
74 // New types since 5.7.0.
75 Lockout_Log::ERROR_404_XSS => esc_html__( 'XSS attempt', 'defender-security' ),
76 Lockout_Log::LOCKOUT_404_XSS => esc_html__( 'XSS lockout', 'defender-security' ),
77 Lockout_Log::ERROR_404_NON_EXISTENT_THEME => esc_html__( 'Attempt to access non-installed theme', 'defender-security' ),
78 Lockout_Log::LOCKOUT_404_NON_EXISTENT_THEME => esc_html__( 'Non-installed theme lockout', 'defender-security' ),
79 Lockout_Log::ERROR_404_NON_EXISTENT_PLUGIN => esc_html__( 'Attempt to access non-installed plugin', 'defender-security' ),
80 Lockout_Log::LOCKOUT_404_NON_EXISTENT_PLUGIN => esc_html__( 'Non-installed plugin lockout', 'defender-security' ),
81 );
82 }
83
84 /**
85 * Get ban statuses.
86 *
87 * @return array
88 */
89 private function ban_status(): array {
90 return array(
91 'all' => esc_html__( 'Any', 'defender-security' ),
92 self::STATUS_NOT_BAN => esc_html__( 'Not Banned', 'defender-security' ),
93 self::STATUS_BAN => esc_html__( 'Banned', 'defender-security' ),
94 self::STATUS_ALLOWLIST => esc_html__( 'Allowlisted', 'defender-security' ),
95 );
96 }
97
98 /**
99 * Retrieves the type description based on the type key.
100 *
101 * @param string $type The type key.
102 *
103 * @return string The description of the type.
104 */
105 public function get_type( $type ): string {
106 $types = $this->get_types();
107 $types[ Lockout_Log::ERROR_404_IGNORE ] = esc_html__( '404 error', 'defender-security' );
108 return $types[ $type ] ?? '';
109 }
110
111 /**
112 * Retrieves sorting values.
113 *
114 * @return array An associative array of sorting options.
115 */
116 private function sort_values(): array {
117 return array(
118 self::SORT_DESC => esc_html__( 'Latest', 'defender-security' ),
119 self::SORT_ASC => esc_html__( 'Oldest', 'defender-security' ),
120 self::SORT_BY_IP => esc_html__( 'IP Address', 'defender-security' ),
121 self::SORT_BY_UA => esc_html__( 'User agent', 'defender-security' ),
122 );
123 }
124
125 /**
126 * Retrieves pagination limits.
127 *
128 * @return array An associative array of pagination limits.
129 */
130 private function limit_per_page(): array {
131 return array(
132 self::LIMIT_10 => '10',
133 self::LIMIT_20 => '20',
134 self::LIMIT_50 => '50',
135 self::LIMIT_100 => '100',
136 self::LIMIT_ALL => esc_html__( 'All', 'defender-security' ),
137 );
138 }
139
140 /**
141 * Retrieves all filters used in the lockout table.
142 *
143 * @return array An associative array of all filters.
144 */
145 public function get_filters(): array {
146 return array(
147 'lockout_types' => $this->get_types(),
148 'ban_status' => $this->ban_status(),
149 'sort_values' => $this->sort_values(),
150 'limit_logs' => $this->limit_per_page(),
151 );
152 }
153
154 /**
155 * Resolves sorting parameters based on the provided sort key.
156 *
157 * @param string $sort The sort key.
158 *
159 * @return array An associative array containing 'order' and 'order_by' keys.
160 */
161 public function resolve_sort( string $sort ): array {
162 switch ( $sort ) {
163 case self::SORT_BY_IP:
164 return array(
165 'order' => 'desc',
166 'order_by' => 'ip',
167 );
168 case self::SORT_ASC:
169 return array(
170 'order' => 'asc',
171 'order_by' => 'id',
172 );
173 case self::SORT_BY_UA:
174 return array(
175 'order' => 'asc',
176 'order_by' => 'user_agent',
177 );
178 case self::SORT_DESC:
179 default:
180 return array(
181 'order' => 'desc',
182 'order_by' => 'id',
183 );
184 }
185 }
186 }
187