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-blacklist-lockout.php

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

288 lines 7.4 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 $whitelist = array_filter( $whitelist );
107 $this->ip_whitelist = implode( PHP_EOL, $whitelist );
108 $this->ip_lockout_message = $default_values['message'];
109 }
110
111 /**
112 * Adds an IP address to a list of IP addresses.
113 *
114 * @param string $ip The IP address to add.
115 * @param string $collection The type of list to add the IP address to. Must be either "blocklist" or "allowlist".
116 *
117 * @return void
118 */
119 public function add_to_list( $ip, $collection ) {
120 $arr = $this->get_list( $collection );
121 if ( $this->validate_ip( $ip ) ) {
122 $arr[] = trim( $ip );
123 $arr = array_unique( $arr );
124 if ( 'blocklist' === $collection ) {
125 $this->ip_blacklist = implode( PHP_EOL, $arr );
126 } elseif ( 'allowlist' === $collection ) {
127 $this->ip_whitelist = implode( PHP_EOL, $arr );
128 }
129
130 $this->save();
131 }
132 }
133
134 /**
135 * Checks if an IP address is in a given list.
136 *
137 * @param string $ip The IP address to check.
138 * @param string $collection The type of list to check against. Must be either "blocklist" or "allowlist".
139 *
140 * @return bool Returns true if the IP address is in the list, false otherwise.
141 */
142 public function is_ip_in_list( $ip, $collection ): bool {
143 $arr = $this->get_list( $collection );
144 if ( $this->validate_ip( $ip ) && in_array( $ip, $arr, true ) ) {
145 return true;
146 }
147
148 return false;
149 }
150
151 /**
152 * Remove IP from a list.
153 *
154 * @param string $ip The IP address to check.
155 * @param string $collection The type of list to check against. Must be either "blocklist" or "allowlist".
156 *
157 * @return void
158 */
159 public function remove_from_list( $ip, $collection ) {
160 $arr = $this->get_list( $collection );
161 $key = array_search( $ip, $arr, true );
162 if ( false !== $key ) {
163 unset( $arr[ $key ] );
164 if ( 'blocklist' === $collection ) {
165 $this->ip_blacklist = implode( PHP_EOL, $arr );
166 } elseif ( 'allowlist' === $collection ) {
167 $this->ip_whitelist = implode( PHP_EOL, $arr );
168 }
169
170 $this->save();
171 }
172 }
173
174 /**
175 * We're going to use this for filter the IPs, as we use textarea to submit, so it can contain some un-valid IPs.
176 */
177 protected function after_validate(): void {
178 $lists = array(
179 'ip_blacklist' => $this->get_list( 'blocklist' ),
180 'ip_whitelist' => $this->get_list( 'allowlist' ),
181 );
182 $errors = array();
183
184 foreach ( $lists as $key => &$collection ) {
185 foreach ( $collection as $i => $v ) {
186 $messages = $this->display_validation_message( $v );
187 if ( ! empty( $messages ) ) {
188 unset( $collection[ $i ] );
189 $errors = array_merge( $errors, $messages );
190 }
191 }
192 $this->$key = implode( PHP_EOL, array_filter( $collection ) );
193 }
194
195 if ( ! empty( $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 * Get list of blocklisted or allowlisted IPs.
203 *
204 * @param string $type blocklist|allowlist.
205 *
206 * @return array
207 */
208 public function get_list( $type = 'blocklist' ): array {
209 // The list should be always strings.
210 $collection = ( 'blocklist' === $type ) ? $this->ip_blacklist : $this->ip_whitelist;
211 $arr = preg_split( '/\r\n|\r|\n/', $collection );
212 if ( ! is_array( $arr ) ) {
213 return array();
214 }
215
216 $arr = array_map(
217 function ( $value ) {
218 return strtolower( trim( $value ) );
219 },
220 $arr
221 );
222
223 return array_filter( $arr );
224 }
225
226 /**
227 * Get list of blacklisted countries.
228 *
229 * @return array
230 */
231 public function get_country_blacklist(): array {
232 return $this->country_blacklist;
233 }
234
235 /**
236 * Get list of whitelisted countries.
237 *
238 * @return array
239 */
240 public function get_country_whitelist(): array {
241 return $this->country_whitelist;
242 }
243
244 /**
245 * Define settings labels.
246 *
247 * @return array
248 */
249 public function labels(): array {
250 return array(
251 'ip_blacklist' => esc_html__( 'IP Banning - IP Addresses Blocklist', 'defender-security' ),
252 'ip_whitelist' => esc_html__( 'IP Banning - IP Addresses Allowlist', 'defender-security' ),
253 'country_blacklist' => esc_html__( 'IP Banning - Country Allowlist', 'defender-security' ),
254 'country_whitelist' => esc_html__( 'IP Banning - Country Blocklist', 'defender-security' ),
255 'ip_lockout_message' => esc_html__( 'IP Banning - Lockout Message', 'defender-security' ),
256 'maxmind_license_key' => esc_html__( 'MaxMind license key', 'defender-security' ),
257 );
258 }
259
260 /**
261 * Executes after loading the object.
262 *
263 * @return void
264 */
265 protected function after_load(): void {
266 if (
267 ! empty( $this->geodb_path ) &&
268 is_string( $this->geodb_path ) &&
269 strlen( $this->geodb_path ) > 0
270 ) {
271 $service_geo = wd_di()->get( MaxMind_Geolocation::class );
272
273 preg_match( '#.*[\\\/](.*[\\\/].*)$#', $this->geodb_path, $matches );
274
275 $this->geodb_path = $service_geo->get_db_base_path() . DIRECTORY_SEPARATOR . $matches[1];
276 }
277 }
278
279 /**
280 * Returns the module name.
281 *
282 * @return string The module name.
283 */
284 public static function get_module_name(): string {
285 return esc_html__( 'Local Blocklist', 'defender-security' );
286 }
287 }
288