PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.1.0
Defender Security – Malware Scanner, Login Security & Firewall v3.1.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 / blacklist-lockout.php

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

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