| @@ -67,14 +67,14 @@ | ||
| 67 | 67 | public $country_whitelist = array(); |
| 68 | 68 | |
| 69 | 69 | /** |
| 70 | 70 | * Path to downloaded GeoDB. |
| 71 | - * Important: This var doesn't support Union Types. So just 'string'. | |
| 71 | + * An empty string indicates that no database path is saved. | |
| 72 | 72 | * |
| 73 | 73 | * @var string |
| 74 | 74 | * @defender_property |
| 75 | 75 | */ |
| 76 | - public $geodb_path = null; | |
| 76 | + public $geodb_path = ''; | |
| 77 | 77 | |
| 78 | 78 | /** |
| 79 | 79 | * MaxMind license key. |
| 80 | 80 | * |
| @@ -102,9 +102,8 @@ | ||
| 102 | 102 | */ |
| 103 | 103 | protected function before_load(): void { |
| 104 | 104 | $default_values = $this->get_default_values(); |
| 105 | 105 | $whitelist = $this->get_list( 'allowlist' ); |
| 106 | - $whitelist = array_filter( $whitelist ); | |
| 107 | 106 | $this->ip_whitelist = implode( PHP_EOL, $whitelist ); |
| 108 | 107 | $this->ip_lockout_message = $default_values['message']; |
| 109 | 108 | } |
| 110 | 109 | |
| @@ -183,17 +182,18 @@ | ||
| 183 | 182 | |
| 184 | 183 | foreach ( $lists as $key => &$collection ) { |
| 185 | 184 | foreach ( $collection as $i => $v ) { |
| 186 | 185 | $messages = $this->display_validation_message( $v ); |
| 187 | - if ( ! empty( $messages ) ) { | |
| 186 | + if ( array() !== $messages ) { | |
| 188 | 187 | unset( $collection[ $i ] ); |
| 189 | 188 | $errors = array_merge( $errors, $messages ); |
| 190 | 189 | } |
| 191 | 190 | } |
| 192 | - $this->$key = implode( PHP_EOL, array_filter( $collection ) ); | |
| 191 | + | |
| 192 | + $this->set_list_property( $key, implode( PHP_EOL, array_filter( $collection, 'strlen' ) ) ); | |
| 193 | 193 | } |
| 194 | 194 | |
| 195 | - if ( ! empty( $errors ) ) { | |
| 195 | + if ( array() !== $errors ) { | |
| 196 | 196 | $this->errors[] = esc_html__( 'Invalid IP addresses detected. Please fix the following errors:', 'defender-security' ); |
| 197 | 197 | $this->errors = array_merge( $this->errors, $errors ); |
| 198 | 198 | } |
| 199 | 199 | } |
| @@ -198,8 +198,31 @@ | ||
| 198 | 198 | } |
| 199 | 199 | } |
| 200 | 200 | |
| 201 | 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 | + /** | |
| 202 | 225 | * Get list of blocklisted or allowlisted IPs. |
| 203 | 226 | * |
| 204 | 227 | * @param string $type blocklist|allowlist. |
| 205 | 228 | * |
| @@ -219,9 +242,9 @@ | ||
| 219 | 242 | }, |
| 220 | 243 | $arr |
| 221 | 244 | ); |
| 222 | 245 | |
| 223 | - return array_filter( $arr ); | |
| 246 | + return array_filter( $arr, 'strlen' ); | |
| 224 | 247 | } |
| 225 | 248 | |
| 226 | 249 | /** |
| 227 | 250 | * Get list of blacklisted countries. |
| @@ -262,12 +285,16 @@ | ||
| 262 | 285 | * |
| 263 | 286 | * @return void |
| 264 | 287 | */ |
| 265 | 288 | protected function after_load(): void { |
| 289 | + // Normalize legacy or imported settings before using this value in filesystem functions. | |
| 290 | + if ( ! is_string( $this->geodb_path ) ) { | |
| 291 | + $this->geodb_path = ''; | |
| 292 | + } | |
| 293 | + | |
| 266 | 294 | if ( |
| 267 | - ! empty( $this->geodb_path ) && | |
| 268 | - is_string( $this->geodb_path ) && | |
| 269 | - strlen( $this->geodb_path ) > 0 | |
| 295 | + strlen( $this->geodb_path ) > 0 && | |
| 296 | + ! is_file( $this->geodb_path ) | |
| 270 | 297 | ) { |
| 271 | 298 | $service_geo = wd_di()->get( MaxMind_Geolocation::class ); |
| 272 | 299 | |
| 273 | 300 | preg_match( '#.*[\\\/](.*[\\\/].*)$#', $this->geodb_path, $matches ); |