| @@ -6,9 +6,12 @@ | ||
| 6 | 6 | */ |
| 7 | 7 | |
| 8 | 8 | namespace WP_Defender\Traits; |
| 9 | 9 | |
| 10 | +use RuntimeException; | |
| 10 | 11 | use WP_Defender\Extra\GeoIp; |
| 12 | +use WP_DEFENDER_VENDOR\MaxMind\Db\Reader\InvalidDatabaseException; | |
| 13 | +use WP_Defender\Model\Setting\Blacklist_Lockout; | |
| 11 | 14 | |
| 12 | 15 | trait Country { |
| 13 | 16 | |
| 14 | 17 | /** |
| @@ -13,17 +16,16 @@ | ||
| 13 | 16 | |
| 14 | 17 | /** |
| 15 | 18 | * Get country of the current user. |
| 16 | 19 | * |
| 17 | - * @param string $ip IPV4 or IPV6 address. | |
| 20 | + * @param string $ip IPV4 or IPV6 address. | |
| 18 | 21 | * |
| 19 | 22 | * @return array|bool |
| 20 | - * @throws \DI\DependencyException Container exception. | |
| 21 | - * @throws \DI\NotFoundException Thrown when DI injected class not found. | |
| 22 | - * @throws \MaxMind\Db\Reader\InvalidDatabaseException Thrown for unexpected data is found in DB. | |
| 23 | + * @throws RuntimeException Thrown when a dependency cannot be resolved. | |
| 24 | + * @throws InvalidDatabaseException Thrown for unexpected data is found in DB. | |
| 23 | 25 | */ |
| 24 | 26 | public function get_current_country( $ip ) { |
| 25 | - if ( 'cli' === php_sapi_name() ) { | |
| 27 | + if ( defender_is_wp_cli() ) { | |
| 26 | 28 | // Never catch if from cli. |
| 27 | 29 | return false; |
| 28 | 30 | } |
| 29 | 31 | $service = wd_di()->get( \WP_Defender\Component\Blacklist_Lockout::class ); |
| @@ -32,14 +34,15 @@ | ||
| 32 | 34 | } |
| 33 | 35 | if ( ! filter_var( $ip, FILTER_VALIDATE_IP ) ) { |
| 34 | 36 | return false; |
| 35 | 37 | } |
| 36 | - $model = wd_di()->get( \WP_Defender\Model\Setting\Blacklist_Lockout::class ); | |
| 38 | + $model = wd_di()->get( Blacklist_Lockout::class ); | |
| 37 | 39 | // Additional check if MaxMind dir is deleted. |
| 38 | - if ( is_null( $model->geodb_path ) || ! is_file( $model->geodb_path ) ) { | |
| 40 | + if ( ! is_string( $model->geodb_path ) || ! is_file( $model->geodb_path ) ) { | |
| 39 | 41 | return false; |
| 40 | 42 | } |
| 41 | 43 | $helper = new GeoIp( $model->geodb_path ); |
| 44 | + | |
| 42 | 45 | // Todo: add check for unhandled exceptions. |
| 43 | 46 | return $helper->ip_to_country( $ip ); |
| 44 | 47 | } |
| 45 | 48 | |
| @@ -48,9 +51,9 @@ | ||
| 48 | 51 | * |
| 49 | 52 | * @return array |
| 50 | 53 | * @since 2.8.0 Add hook. |
| 51 | 54 | */ |
| 52 | - public function countries_list() { | |
| 55 | + public function countries_list(): array { | |
| 53 | 56 | return apply_filters( |
| 54 | 57 | 'wd_countries', |
| 55 | 58 | array( |
| 56 | 59 | 'AD' => 'Andorra', |
| @@ -311,74 +314,19 @@ | ||
| 311 | 314 | ); |
| 312 | 315 | } |
| 313 | 316 | |
| 314 | 317 | /** |
| 315 | - * Fetch country from ip using external api service. | |
| 316 | - * | |
| 317 | - * @param string $ip IPV4 or IPV6 address. | |
| 318 | - * | |
| 319 | - * @return false|array false on failure to fetch and array on success. | |
| 320 | - */ | |
| 321 | - public function ip_to_country_api( $ip ) { | |
| 322 | - $url = 'https://api.redirect.li/v1/ip/' . $ip; | |
| 323 | - | |
| 324 | - $ssl = wp_http_supports( array( 'ssl' ) ); | |
| 325 | - | |
| 326 | - if ( $ssl ) { | |
| 327 | - | |
| 328 | - $url = set_url_scheme( $url, 'https' ); | |
| 329 | - | |
| 330 | - } | |
| 331 | - | |
| 332 | - $options = array( | |
| 333 | - | |
| 334 | - 'timeout' => wp_doing_cron() ? 30 : 3, | |
| 335 | - | |
| 336 | - ); | |
| 337 | - | |
| 338 | - $response = wp_remote_get( $url, $options ); | |
| 339 | - | |
| 340 | - if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { | |
| 341 | - | |
| 342 | - $log_message = 'HTTP Status Code: ' . wp_remote_retrieve_response_code( $response ) . PHP_EOL; | |
| 343 | - $log_message .= 'HTTP Response Message: ' . wp_remote_retrieve_response_message( $response ) . PHP_EOL; | |
| 344 | - $log_message .= 'API URL: ' . $url . PHP_EOL; | |
| 345 | - | |
| 346 | - $this->log( $log_message, 'firewall.log' ); | |
| 347 | - | |
| 348 | - return false; | |
| 349 | - | |
| 350 | - } | |
| 351 | - | |
| 352 | - $body = trim( wp_remote_retrieve_body( $response ) ); | |
| 353 | - | |
| 354 | - $body = json_decode( $body, true ); | |
| 355 | - | |
| 356 | - if ( ! is_array( $body ) || ! isset( $body['countryName'] ) ) { | |
| 357 | - | |
| 358 | - return false; | |
| 359 | - | |
| 360 | - } | |
| 361 | - | |
| 362 | - return array( | |
| 363 | - 'iso' => $body['countryCode'], | |
| 364 | - 'name' => $body['countryName'], | |
| 365 | - ); | |
| 366 | - | |
| 367 | - } | |
| 368 | - | |
| 369 | - /** | |
| 370 | 318 | * Generic method to return country detail of an IP. |
| 371 | 319 | * |
| 372 | - * @param string $ip IPV4 or IPV6 address. | |
| 320 | + * @param string $ip IPV4 or IPV6 address. | |
| 373 | 321 | * |
| 374 | - * @return false|array false on failure to fetch and array on success. | |
| 322 | + * @return bool|array false on failure to fetch and array on success. | |
| 375 | 323 | */ |
| 376 | 324 | public function ip_to_country( $ip ) { |
| 377 | 325 | $country = $this->get_current_country( $ip ); |
| 378 | - | |
| 379 | 326 | if ( ! $country ) { |
| 380 | - $country = $this->ip_to_country_api( $ip ); | |
| 327 | + // since 3.8.0. | |
| 328 | + $country = apply_filters( 'wd_ip_to_country_api', $country, $ip ); | |
| 381 | 329 | } |
| 382 | 330 | |
| 383 | 331 | return $country; |
| 384 | 332 | } |