PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 6.2.3
Defender Security – Malware Scanner, Login Security & Firewall v6.2.3
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 3.1.1 All 140 releases
defender-security / src / model / setting / class-blacklist-lockout.php

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

310 lines 7.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles blacklist and lockout settings.
4 *
5 * @package WP_Defender\Model\Setting
6 */
7
8 namespace WP_Defender\Model\Setting;
9
10 use Calotes\Model\Setting;
11 use WP_Defender\Traits\IP;
12 use WP_Defender\Integrations\MaxMind_Geolocation;
13
14 /**
15 * Model for blacklist and lockout settings.
16 */
17 class Blacklist_Lockout extends Setting {
18
19 use IP;
20
21 /**
22 * Option name.
23 *
24 * @var string
25 */
26 protected $table = 'wd_blacklist_lockout_settings';
27 /**
28 * Store a list of IPs blocked from the site, the priority of this list is lower than whitelist.
29 *
30 * @var string
31 * @defender_property
32 */
33 public $ip_blacklist = '';
34 /**
35 * Top priority, if an IP in this list, mean we never check any on them.
36 *
37 * @var string
38 * @defender_property
39 */
40 public $ip_whitelist = '';
41 /**
42 * The message to show on frontend when a blocklisted IP access the site, recommend to use something generic,
43 * so we don't expose our intention.
44 *
45 * @var string
46 * @defender_property
47 * @sanitize sanitize_textarea_field
48 */
49 public $ip_lockout_message = '';
50
51 /**
52 * This should be use if you don't want an IP from some country to access your site, the error message will refer to
53 * $ip_lockout_message.
54 *
55 * @var array
56 * @defender_property
57 */
58 public $country_blacklist = array();
59
60 /**
61 * This uses when you want to block all and allow some countries, it will have less priority than the IP
62 * white/black above.
63 *
64 * @var array
65 * @defender_property
66 */
67 public $country_whitelist = array();
68
69 /**
70 * Path to downloaded GeoDB.
71 * Important: This var doesn't support Union Types. So just 'string'.
72 *
73 * @var string
74 * @defender_property
75 */
76 public $geodb_path = null;
77
78 /**
79 * MaxMind license key.
80 *
81 * @var string
82 * @defender_property
83 * @sanitize sanitize_text_field
84 */
85 public $maxmind_license_key = '';
86
87 /**
88 * Retrieves the default values for the function.
89 *
90 * @return array An associative array containing the default values.
91 */
92 public function get_default_values(): array {
93 return array(
94 'message' => esc_html__( 'The administrator has blocked your IP from accessing this website.', 'defender-security' ),
95 );
96 }
97
98 /**
99 * Initializes the object before loading.
100 *
101 * @return void
102 */
103 protected function before_load(): void {
104 $default_values = $this->get_default_values();
105 $whitelist = $this->get_list( 'allowlist' );
106 $this->ip_whitelist = implode( PHP_EOL, $whitelist );
107 $this->ip_lockout_message = $default_values['message'];
108 }
109
110 /**
111 * Adds an IP address to a list of IP addresses.
112 *
113 * @param string $ip The IP address to add.
114 * @param string $collection The type of list to add the IP address to. Must be either "blocklist" or "allowlist".
115 *
116 * @return void
117 */
118 public function add_to_list( $ip, $collection ) {
119 $arr = $this->get_list( $collection );
120 if ( $this->validate_ip( $ip ) ) {
121 $arr[] = trim( $ip );
122 $arr = array_unique( $arr );
123 if ( 'blocklist' === $collection ) {
124 $this->ip_blacklist = implode( PHP_EOL, $arr );
125 } elseif ( 'allowlist' === $collection ) {
126 $this->ip_whitelist = implode( PHP_EOL, $arr );
127 }
128
129 $this->save();
130 }
131 }
132
133 /**
134 * Checks if an IP address is in a given list.
135 *
136 * @param string $ip The IP address to check.
137 * @param string $collection The type of list to check against. Must be either "blocklist" or "allowlist".
138 *
139 * @return bool Returns true if the IP address is in the list, false otherwise.
140 */
141 public function is_ip_in_list( $ip, $collection ): bool {
142 $arr = $this->get_list( $collection );
143 if ( $this->validate_ip( $ip ) && in_array( $ip, $arr, true ) ) {
144 return true;
145 }
146
147 return false;
148 }
149
150 /**
151 * Remove IP from a list.
152 *
153 * @param string $ip The IP address to check.
154 * @param string $collection The type of list to check against. Must be either "blocklist" or "allowlist".
155 *
156 * @return void
157 */
158 public function remove_from_list( $ip, $collection ) {
159 $arr = $this->get_list( $collection );
160 $key = array_search( $ip, $arr, true );
161 if ( false !== $key ) {
162 unset( $arr[ $key ] );
163 if ( 'blocklist' === $collection ) {
164 $this->ip_blacklist = implode( PHP_EOL, $arr );
165 } elseif ( 'allowlist' === $collection ) {
166 $this->ip_whitelist = implode( PHP_EOL, $arr );
167 }
168
169 $this->save();
170 }
171 }
172
173 /**
174 * We're going to use this for filter the IPs, as we use textarea to submit, so it can contain some un-valid IPs.
175 */
176 protected function after_validate(): void {
177 $lists = array(
178 'ip_blacklist' => $this->get_list( 'blocklist' ),
179 'ip_whitelist' => $this->get_list( 'allowlist' ),
180 );
181 $errors = array();
182
183 foreach ( $lists as $key => &$collection ) {
184 foreach ( $collection as $i => $v ) {
185 $messages = $this->display_validation_message( $v );
186 if ( array() !== $messages ) {
187 unset( $collection[ $i ] );
188 $errors = array_merge( $errors, $messages );
189 }
190 }
191
192 $this->set_list_property( $key, implode( PHP_EOL, array_filter( $collection, 'strlen' ) ) );
193 }
194
195 if ( array() !== $errors ) {
196 $this->errors[] = esc_html__( 'Invalid IP addresses detected. Please fix the following errors:', 'defender-security' );
197 $this->errors = array_merge( $this->errors, $errors );
198 }
199 }
200
201 /**
202 * Set a list property.
203 *
204 * @param string $property Property name.
205 * @param string $value Property value.
206 *
207 * @throws \InvalidArgumentException Invalid property name.
208 */
209 private function set_list_property( string $property, string $value ): void {
210 switch ( $property ) {
211 case 'ip_blacklist':
212 $this->ip_blacklist = $value;
213 break;
214
215 case 'ip_whitelist':
216 $this->ip_whitelist = $value;
217 break;
218
219 default:
220 throw new \InvalidArgumentException( 'Invalid property name.' );
221 }
222 }
223
224 /**
225 * Get list of blocklisted or allowlisted IPs.
226 *
227 * @param string $type blocklist|allowlist.
228 *
229 * @return array
230 */
231 public function get_list( $type = 'blocklist' ): array {
232 // The list should be always strings.
233 $collection = ( 'blocklist' === $type ) ? $this->ip_blacklist : $this->ip_whitelist;
234 $arr = preg_split( '/\r\n|\r|\n/', $collection );
235 if ( ! is_array( $arr ) ) {
236 return array();
237 }
238
239 $arr = array_map(
240 function ( $value ) {
241 return strtolower( trim( $value ) );
242 },
243 $arr
244 );
245
246 return array_filter( $arr, 'strlen' );
247 }
248
249 /**
250 * Get list of blacklisted countries.
251 *
252 * @return array
253 */
254 public function get_country_blacklist(): array {
255 return $this->country_blacklist;
256 }
257
258 /**
259 * Get list of whitelisted countries.
260 *
261 * @return array
262 */
263 public function get_country_whitelist(): array {
264 return $this->country_whitelist;
265 }
266
267 /**
268 * Define settings labels.
269 *
270 * @return array
271 */
272 public function labels(): array {
273 return array(
274 'ip_blacklist' => esc_html__( 'IP Banning - IP Addresses Blocklist', 'defender-security' ),
275 'ip_whitelist' => esc_html__( 'IP Banning - IP Addresses Allowlist', 'defender-security' ),
276 'country_blacklist' => esc_html__( 'IP Banning - Country Allowlist', 'defender-security' ),
277 'country_whitelist' => esc_html__( 'IP Banning - Country Blocklist', 'defender-security' ),
278 'ip_lockout_message' => esc_html__( 'IP Banning - Lockout Message', 'defender-security' ),
279 'maxmind_license_key' => esc_html__( 'MaxMind license key', 'defender-security' ),
280 );
281 }
282
283 /**
284 * Executes after loading the object.
285 *
286 * @return void
287 */
288 protected function after_load(): void {
289 if (
290 is_string( $this->geodb_path ) &&
291 strlen( $this->geodb_path ) > 0
292 ) {
293 $service_geo = wd_di()->get( MaxMind_Geolocation::class );
294
295 preg_match( '#.*[\\\/](.*[\\\/].*)$#', $this->geodb_path, $matches );
296
297 $this->geodb_path = $service_geo->get_db_base_path() . DIRECTORY_SEPARATOR . $matches[1];
298 }
299 }
300
301 /**
302 * Returns the module name.
303 *
304 * @return string The module name.
305 */
306 public static function get_module_name(): string {
307 return esc_html__( 'Local Blocklist', 'defender-security' );
308 }
309 }
310