2020-01-09 05:09:35 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2020-01-10 10:09:51 +00:00
|
|
|
* The database service class file.
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
|
|
|
* @version 3.9.0
|
|
|
|
* @package WooCommerce/Integrations
|
|
|
|
*/
|
|
|
|
|
|
|
|
defined( 'ABSPATH' ) || exit;
|
|
|
|
|
|
|
|
/**
|
2020-01-10 10:09:51 +00:00
|
|
|
* The service class responsible for interacting with MaxMind databases.
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
2020-01-10 07:46:25 +00:00
|
|
|
* @since 3.9.0
|
2020-01-09 05:09:35 +00:00
|
|
|
*/
|
2020-01-10 10:09:51 +00:00
|
|
|
class WC_Integration_MaxMind_Database_Service {
|
2020-01-13 22:41:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The name of the MaxMind database to utilize.
|
|
|
|
*/
|
|
|
|
const DATABASE = 'GeoLite2-Country';
|
|
|
|
|
2020-01-09 05:48:05 +00:00
|
|
|
/**
|
|
|
|
* The extension for the MaxMind database.
|
2020-01-09 05:09:35 +00:00
|
|
|
*/
|
2020-01-09 05:48:05 +00:00
|
|
|
const DATABASE_EXTENSION = '.mmdb';
|
2020-01-09 05:09:35 +00:00
|
|
|
|
2020-01-13 16:16:44 +00:00
|
|
|
/**
|
|
|
|
* A prefix for the MaxMind database filename.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
private $database_prefix;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WC_Integration_MaxMind_Database_Service constructor.
|
|
|
|
*
|
|
|
|
* @param string|null $database_prefix A prefix for the MaxMind database filename.
|
|
|
|
*/
|
2020-01-13 22:41:24 +00:00
|
|
|
public function __construct( $database_prefix ) {
|
2020-01-13 16:16:44 +00:00
|
|
|
$this->database_prefix = $database_prefix;
|
|
|
|
}
|
|
|
|
|
2020-01-09 05:09:35 +00:00
|
|
|
/**
|
2020-01-09 05:48:05 +00:00
|
|
|
* Fetches the path that the database should be stored.
|
2020-01-09 05:09:35 +00:00
|
|
|
*
|
2020-01-09 05:48:05 +00:00
|
|
|
* @return string The local database path.
|
2020-01-09 05:09:35 +00:00
|
|
|
*/
|
2020-01-10 10:09:51 +00:00
|
|
|
public function get_database_path() {
|
2020-01-13 19:00:23 +00:00
|
|
|
$uploads_dir = wp_upload_dir();
|
|
|
|
|
|
|
|
$database_path = trailingslashit( $uploads_dir['basedir'] ) . 'woocommerce_uploads/';
|
2020-01-13 16:16:44 +00:00
|
|
|
if ( ! empty( $this->database_prefix ) ) {
|
|
|
|
$database_path .= $this->database_prefix . '-';
|
|
|
|
}
|
2020-01-13 22:41:24 +00:00
|
|
|
$database_path .= self::DATABASE . self::DATABASE_EXTENSION;
|
2020-01-09 05:48:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the geolocation database storage path.
|
|
|
|
*
|
|
|
|
* @param string $database_path The path to the database.
|
|
|
|
* @param int $version Deprecated since 3.4.0.
|
|
|
|
* @deprecated 3.9.0
|
|
|
|
*/
|
|
|
|
$database_path = apply_filters_deprecated(
|
|
|
|
'woocommerce_geolocation_local_database_path',
|
|
|
|
array( $database_path, 2 ),
|
|
|
|
'3.9.0',
|
|
|
|
'woocommerce_maxmind_geolocation_database_path'
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the geolocation database storage path.
|
|
|
|
*
|
|
|
|
* @since 3.9.0
|
|
|
|
* @param string $database_path The path to the database.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'woocommerce_maxmind_geolocation_database_path', $database_path );
|
2020-01-09 05:09:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the database from the MaxMind service.
|
|
|
|
*
|
2020-01-09 05:48:05 +00:00
|
|
|
* @param string $license_key The license key to be used when downloading the database.
|
2020-01-09 05:09:35 +00:00
|
|
|
* @return string|WP_Error The path to the database file or an error if invalid.
|
|
|
|
*/
|
2020-01-10 10:09:51 +00:00
|
|
|
public function download_database( $license_key ) {
|
2020-01-10 00:18:20 +00:00
|
|
|
$download_uri = add_query_arg(
|
2020-01-09 05:09:35 +00:00
|
|
|
array(
|
2020-01-13 22:41:24 +00:00
|
|
|
'edition_id' => self::DATABASE,
|
2020-01-10 04:44:13 +00:00
|
|
|
'license_key' => wc_clean( $license_key ),
|
2020-01-09 05:09:35 +00:00
|
|
|
'suffix' => 'tar.gz',
|
2020-01-10 00:18:20 +00:00
|
|
|
),
|
|
|
|
'https://download.maxmind.com/app/geoip_download'
|
2020-01-09 05:09:35 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Needed for the download_url call right below.
|
|
|
|
require_once ABSPATH . 'wp-admin/includes/file.php';
|
|
|
|
|
2020-02-16 21:55:49 +00:00
|
|
|
$tmp_archive_path = download_url( esc_url_raw( $download_uri ) );
|
2020-01-09 05:09:35 +00:00
|
|
|
if ( is_wp_error( $tmp_archive_path ) ) {
|
|
|
|
// Transform the error into something more informative.
|
|
|
|
$error_data = $tmp_archive_path->get_error_data();
|
|
|
|
if ( isset( $error_data['code'] ) ) {
|
|
|
|
switch ( $error_data['code'] ) {
|
|
|
|
case 401:
|
|
|
|
return new WP_Error(
|
|
|
|
'woocommerce_maxmind_geolocation_database_license_key',
|
2020-01-20 15:57:44 +00:00
|
|
|
__( 'The MaxMind license key is invalid. If you have recently created this key, you may need to wait for it to become active.', 'woocommerce' )
|
2020-01-09 05:09:35 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_Error( 'woocommerce_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'woocommerce' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract the database from the archive.
|
|
|
|
try {
|
|
|
|
$file = new PharData( $tmp_archive_path );
|
|
|
|
|
2020-01-13 22:41:24 +00:00
|
|
|
$tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION;
|
2020-01-09 05:09:35 +00:00
|
|
|
|
|
|
|
$file->extractTo(
|
2020-01-09 06:56:24 +00:00
|
|
|
dirname( $tmp_archive_path ),
|
2020-01-13 22:41:24 +00:00
|
|
|
trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION,
|
2020-01-09 05:09:35 +00:00
|
|
|
true
|
|
|
|
);
|
|
|
|
} catch ( Exception $exception ) {
|
|
|
|
return new WP_Error( 'woocommerce_maxmind_geolocation_database_archive', $exception->getMessage() );
|
|
|
|
} finally {
|
|
|
|
// Remove the archive since we only care about a single file in it.
|
|
|
|
unlink( $tmp_archive_path );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tmp_database_path;
|
|
|
|
}
|
2020-01-10 07:46:25 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Fetches the ISO country code associated with an IP address.
|
|
|
|
*
|
|
|
|
* @param string $ip_address The IP address to find the country code for.
|
2020-01-21 05:20:32 +00:00
|
|
|
* @return string The country code for the IP address, or empty if not found.
|
2020-01-10 07:46:25 +00:00
|
|
|
*/
|
2020-01-10 10:09:51 +00:00
|
|
|
public function get_iso_country_code_for_ip( $ip_address ) {
|
2020-01-21 05:20:32 +00:00
|
|
|
$country_code = '';
|
2020-01-10 07:46:25 +00:00
|
|
|
|
2020-01-10 15:16:53 +00:00
|
|
|
if ( ! class_exists( 'MaxMind\Db\Reader' ) ) {
|
|
|
|
wc_get_logger()->notice( __( 'Missing MaxMind Reader library!', 'woocommerce' ), array( 'source' => 'maxmind-geolocation' ) );
|
|
|
|
return $country_code;
|
|
|
|
}
|
|
|
|
|
2020-01-21 05:20:32 +00:00
|
|
|
$database_path = $this->get_database_path();
|
|
|
|
if ( ! file_exists( $database_path ) ) {
|
|
|
|
return $country_code;
|
|
|
|
}
|
|
|
|
|
2020-01-10 07:46:25 +00:00
|
|
|
try {
|
2020-01-21 05:20:32 +00:00
|
|
|
$reader = new MaxMind\Db\Reader( $database_path );
|
2020-01-10 07:46:25 +00:00
|
|
|
$data = $reader->get( $ip_address );
|
|
|
|
|
|
|
|
if ( isset( $data['country']['iso_code'] ) ) {
|
|
|
|
$country_code = $data['country']['iso_code'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$reader->close();
|
|
|
|
} catch ( Exception $e ) {
|
|
|
|
wc_get_logger()->notice( $e->getMessage(), array( 'source' => 'maxmind-geolocation' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $country_code;
|
|
|
|
}
|
2020-01-09 05:09:35 +00:00
|
|
|
}
|