PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 5.4.0
Defender Security – Malware Scanner, Login Security & Firewall v5.4.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 / model / setting / class-notfound-lockout.php

class-notfound-lockout.php in Defender Security – Malware Scanner, Login Security & Firewall 5.4.0, at src/model/setting/class-notfound-lockout.php

219 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles not found lockout settings.
4 *
5 * @package WP_Defender\Model\Setting
6 */
7
8 namespace WP_Defender\Model\Setting;
9
10 use Calotes\Model\Setting;
11
12 /**
13 * Model for not found lockout settings.
14 */
15 class Notfound_Lockout extends Setting {
16
17 /**
18 * Option name.
19 *
20 * @var string
21 */
22 protected $table = 'wd_notfound_lockout_settings';
23
24 /**
25 * Activate this module.
26 *
27 * @var bool
28 * @defender_property
29 */
30 public $enabled = false;
31
32 /**
33 * How many 404 error happen before we lock out the IP.
34 *
35 * @var int
36 * @defender_property
37 * @rule required|integer
38 */
39 public $attempt = 20;
40
41 /**
42 * The time window we use for counting.
43 *
44 * @var int
45 * @defender_property
46 * @rule required|integer
47 */
48 public $timeframe = 300;
49
50 /**
51 * How long we block them.
52 *
53 * @var int
54 * @defender_property
55 * @rule required|integer
56 */
57 public $duration = 300;
58
59 /**
60 * Duration unit.
61 *
62 * @var string
63 * @defender_property
64 * @rule in[seconds,minutes,hours]
65 */
66 public $duration_unit = 'seconds';
67
68 /**
69 * How the lock going to be, if we chose permanent, then their IP will be blacklisted.
70 *
71 * @var string
72 * @defender_property
73 * @rule in[timeframe,permanent]
74 */
75 public $lockout_type = 'timeframe';
76
77 /**
78 * Data allowed:
79 * - URL, as relative form /this-should-be-block, with or without dash would be fine
80 * - filetype extension, with dot before .sql|.exe
81 * - regex pattern, like \/.+\.html
82 * This will lockout any IP if an attempt is triggered when visit the URL that in the list.
83 *
84 * @var string
85 * @defender_property
86 */
87 public $blacklist = '';
88
89 /**
90 * Refer to $blacklist, but we will ignore instead of blocking.
91 *
92 * @var string
93 * @defender_property
94 */
95 public $whitelist = '';
96
97 /**
98 * A message to display on frontend when an IP is locked out.
99 *
100 * @var string
101 * @defender_property
102 * @sanitize sanitize_textarea_field
103 */
104 public $lockout_message = '';
105
106 /**
107 * Set to true for enabling the 404 tracking on logged-in user.
108 *
109 * @var bool
110 * @defender_property
111 */
112 public $detect_logged = false;
113
114 /**
115 * Validation rules.
116 *
117 * @var array
118 */
119 protected $rules = array(
120 array( array( 'enable', 'detect_logged' ), 'boolean' ),
121 array( array( 'attempt', 'timeframe', 'duration' ), 'integer' ),
122 array( array( 'lockout_type' ), 'in', array( 'timeframe', 'permanent' ) ),
123 array( array( 'duration_unit' ), 'in', array( 'seconds', 'minutes', 'hours' ) ),
124 );
125
126 /**
127 * Retrieves the default values for the function.
128 *
129 * @return array The default values.
130 */
131 public function get_default_values(): array {
132 return array(
133 'message' => esc_html__(
134 'You have been locked out due to too many attempts to access a file that doesn`t exist.',
135 'defender-security'
136 ),
137 'whitelist' => ".css\n.js\n.map",
138 );
139 }
140
141 /**
142 * Initializes the object before loading by setting the lockout message and whitelist properties
143 * to their default values.
144 *
145 * @return void
146 */
147 protected function before_load(): void {
148 $default_values = $this->get_default_values();
149 $this->lockout_message = $default_values['message'];
150 $this->whitelist = $default_values['whitelist'];
151 }
152
153 /**
154 * Get list of blocklisted or allowlisted data.
155 *
156 * @param string $type blocklist|allowlist.
157 *
158 * @return array
159 */
160 public function get_lockout_list( $type = 'blocklist' ): array {
161 $data = ( 'blocklist' === $type ) ? $this->blacklist : $this->whitelist;
162 $arr = is_array( $data ) ? $data : array_filter( explode( PHP_EOL, $data ) );
163 $arr = array_map( 'trim', $arr );
164 $arr = array_map( 'strtolower', $arr );
165
166 return $arr;
167 }
168
169 /**
170 * Define settings labels.
171 *
172 * @return array
173 */
174 public function labels(): array {
175 return array(
176 // New key 'enabled'.
177 'detect_404' => self::get_module_name(),
178 // New key 'attempt'.
179 'detect_404_threshold' => esc_html__( '404 Detection - Threshold', 'defender-security' ),
180 // New key 'timeframe'.
181 'detect_404_timeframe' => esc_html__( '404 Protection - Timeframe', 'defender-security' ),
182 // New key 'lockout_type'.
183 'detect_404_lockout_ban' => esc_html__( '404 Protection - Duration Type', 'defender-security' ),
184 // New key 'duration'.
185 'detect_404_lockout_duration' => esc_html__( '404 Detection - Duration', 'defender-security' ),
186 // New key 'duration_unit'.
187 'detect_404_lockout_duration_unit' => esc_html__( '404 Protection - Duration units', 'defender-security' ),
188 // New key 'lockout_message'.
189 'detect_404_lockout_message' => esc_html__( '404 Detection - Lockout Message', 'defender-security' ),
190 // New key 'blacklist'.
191 'detect_404_blacklist' => esc_html__( '404 Detection - Files and Folders Blocklist', 'defender-security' ),
192 // New key 'whitelist'.
193 'detect_404_whitelist' => esc_html__( '404 Detection - Files and Folders Allowlist', 'defender-security' ),
194 // New key 'detect_logged'.
195 'detect_404_logged' => esc_html__( '404 Detection - Monitor logged in users', 'defender-security' ),
196 );
197 }
198
199 /**
200 * Returns the module name for 404 Detection.
201 *
202 * @return string The module name.
203 */
204 public static function get_module_name(): string {
205 return esc_html__( '404 Detection', 'defender-security' );
206 }
207
208 /**
209 * Returns the module state based on the given flag.
210 *
211 * @param bool $flag The flag indicating the module state.
212 *
213 * @return string The module state, either 'active' or 'inactive'.
214 */
215 public static function get_module_state( $flag ): string {
216 return $flag ? esc_html__( 'active', 'defender-security' ) : esc_html__( 'inactive', 'defender-security' );
217 }
218 }
219