| @@ -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 | * |
| @@ -188,11 +188,9 @@ | ||
| 188 | 188 | $errors = array_merge( $errors, $messages ); |
| 189 | 189 | } |
| 190 | 190 | } |
| 191 | 191 | |
| 192 | - if ( property_exists( $this, $key ) ) { | |
| 193 | - $this->$key = implode( PHP_EOL, array_filter( $collection, 'strlen' ) ); | |
| 194 | - } | |
| 192 | + $this->set_list_property( $key, implode( PHP_EOL, array_filter( $collection, 'strlen' ) ) ); | |
| 195 | 193 | } |
| 196 | 194 | |
| 197 | 195 | if ( array() !== $errors ) { |
| 198 | 196 | $this->errors[] = esc_html__( 'Invalid IP addresses detected. Please fix the following errors:', 'defender-security' ); |
| @@ -200,8 +198,31 @@ | ||
| 200 | 198 | } |
| 201 | 199 | } |
| 202 | 200 | |
| 203 | 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 | + /** | |
| 204 | 225 | * Get list of blocklisted or allowlisted IPs. |
| 205 | 226 | * |
| 206 | 227 | * @param string $type blocklist|allowlist. |
| 207 | 228 | * |
| @@ -264,11 +285,16 @@ | ||
| 264 | 285 | * |
| 265 | 286 | * @return void |
| 266 | 287 | */ |
| 267 | 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 | + | |
| 268 | 294 | if ( |
| 269 | - is_string( $this->geodb_path ) && | |
| 270 | - strlen( $this->geodb_path ) > 0 | |
| 295 | + strlen( $this->geodb_path ) > 0 && | |
| 296 | + ! is_file( $this->geodb_path ) | |
| 271 | 297 | ) { |
| 272 | 298 | $service_geo = wd_di()->get( MaxMind_Geolocation::class ); |
| 273 | 299 | |
| 274 | 300 | preg_match( '#.*[\\\/](.*[\\\/].*)$#', $this->geodb_path, $matches ); |