PluginProbe
Defender Security – Malware Scanner, Login Security & Firewall / 3.0.1
Defender Security – Malware Scanner, Login Security & Firewall v3.0.1
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.0.1, at src/model/setting/blacklist-lockout.php

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