woocommerce
/
includes
/
integrations
/
maxmind-geolocation
/
class-wc-integration-maxmind-database-service.php
views
6 years ago
class-wc-integration-maxmind-database-service.php
5 years ago
class-wc-integration-maxmind-geolocation.php
1 month ago
class-wc-integration-maxmind-database-service.php
173 lines
| 1 | <?php |
| 2 | /** |
| 3 | * The database service class file. |
| 4 | * |
| 5 | * @version 3.9.0 |
| 6 | * @package WooCommerce\Integrations |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * The service class responsible for interacting with MaxMind databases. |
| 13 | * |
| 14 | * @since 3.9.0 |
| 15 | */ |
| 16 | class WC_Integration_MaxMind_Database_Service { |
| 17 | |
| 18 | /** |
| 19 | * The name of the MaxMind database to utilize. |
| 20 | */ |
| 21 | const DATABASE = 'GeoLite2-Country'; |
| 22 | |
| 23 | /** |
| 24 | * The extension for the MaxMind database. |
| 25 | */ |
| 26 | const DATABASE_EXTENSION = '.mmdb'; |
| 27 | |
| 28 | /** |
| 29 | * A prefix for the MaxMind database filename. |
| 30 | * |
| 31 | * @var string |
| 32 | */ |
| 33 | private $database_prefix; |
| 34 | |
| 35 | /** |
| 36 | * WC_Integration_MaxMind_Database_Service constructor. |
| 37 | * |
| 38 | * @param string|null $database_prefix A prefix for the MaxMind database filename. |
| 39 | */ |
| 40 | public function __construct( $database_prefix ) { |
| 41 | $this->database_prefix = $database_prefix; |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Fetches the path that the database should be stored. |
| 46 | * |
| 47 | * @return string The local database path. |
| 48 | */ |
| 49 | public function get_database_path() { |
| 50 | $uploads_dir = wp_upload_dir(); |
| 51 | |
| 52 | $database_path = trailingslashit( $uploads_dir['basedir'] ) . 'woocommerce_uploads/'; |
| 53 | if ( ! empty( $this->database_prefix ) ) { |
| 54 | $database_path .= $this->database_prefix . '-'; |
| 55 | } |
| 56 | $database_path .= self::DATABASE . self::DATABASE_EXTENSION; |
| 57 | |
| 58 | /** |
| 59 | * Filter the geolocation database storage path. |
| 60 | * |
| 61 | * @param string $database_path The path to the database. |
| 62 | * @param int $version Deprecated since 3.4.0. |
| 63 | * @deprecated 3.9.0 |
| 64 | */ |
| 65 | $database_path = apply_filters_deprecated( |
| 66 | 'woocommerce_geolocation_local_database_path', |
| 67 | array( $database_path, 2 ), |
| 68 | '3.9.0', |
| 69 | 'woocommerce_maxmind_geolocation_database_path' |
| 70 | ); |
| 71 | |
| 72 | /** |
| 73 | * Filter the geolocation database storage path. |
| 74 | * |
| 75 | * @since 3.9.0 |
| 76 | * @param string $database_path The path to the database. |
| 77 | */ |
| 78 | return apply_filters( 'woocommerce_maxmind_geolocation_database_path', $database_path ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Fetches the database from the MaxMind service. |
| 83 | * |
| 84 | * @param string $license_key The license key to be used when downloading the database. |
| 85 | * @return string|WP_Error The path to the database file or an error if invalid. |
| 86 | */ |
| 87 | public function download_database( $license_key ) { |
| 88 | $download_uri = add_query_arg( |
| 89 | array( |
| 90 | 'edition_id' => self::DATABASE, |
| 91 | 'license_key' => urlencode( wc_clean( $license_key ) ), |
| 92 | 'suffix' => 'tar.gz', |
| 93 | ), |
| 94 | 'https://download.maxmind.com/app/geoip_download' |
| 95 | ); |
| 96 | |
| 97 | // Needed for the download_url call right below. |
| 98 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 99 | |
| 100 | $tmp_archive_path = download_url( esc_url_raw( $download_uri ) ); |
| 101 | if ( is_wp_error( $tmp_archive_path ) ) { |
| 102 | // Transform the error into something more informative. |
| 103 | $error_data = $tmp_archive_path->get_error_data(); |
| 104 | if ( isset( $error_data['code'] ) ) { |
| 105 | switch ( $error_data['code'] ) { |
| 106 | case 401: |
| 107 | return new WP_Error( |
| 108 | 'woocommerce_maxmind_geolocation_database_license_key', |
| 109 | __( 'The MaxMind license key is invalid. If you have recently created this key, you may need to wait for it to become active.', 'woocommerce' ) |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | return new WP_Error( 'woocommerce_maxmind_geolocation_database_download', __( 'Failed to download the MaxMind database.', 'woocommerce' ) ); |
| 115 | } |
| 116 | |
| 117 | // Extract the database from the archive. |
| 118 | try { |
| 119 | $file = new PharData( $tmp_archive_path ); |
| 120 | |
| 121 | $tmp_database_path = trailingslashit( dirname( $tmp_archive_path ) ) . trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION; |
| 122 | |
| 123 | $file->extractTo( |
| 124 | dirname( $tmp_archive_path ), |
| 125 | trailingslashit( $file->current()->getFilename() ) . self::DATABASE . self::DATABASE_EXTENSION, |
| 126 | true |
| 127 | ); |
| 128 | } catch ( Exception $exception ) { |
| 129 | return new WP_Error( 'woocommerce_maxmind_geolocation_database_archive', $exception->getMessage() ); |
| 130 | } finally { |
| 131 | // Remove the archive since we only care about a single file in it. |
| 132 | unlink( $tmp_archive_path ); |
| 133 | } |
| 134 | |
| 135 | return $tmp_database_path; |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Fetches the ISO country code associated with an IP address. |
| 140 | * |
| 141 | * @param string $ip_address The IP address to find the country code for. |
| 142 | * @return string The country code for the IP address, or empty if not found. |
| 143 | */ |
| 144 | public function get_iso_country_code_for_ip( $ip_address ) { |
| 145 | $country_code = ''; |
| 146 | |
| 147 | if ( ! class_exists( 'MaxMind\Db\Reader' ) ) { |
| 148 | wc_get_logger()->notice( __( 'Missing MaxMind Reader library!', 'woocommerce' ), array( 'source' => 'maxmind-geolocation' ) ); |
| 149 | return $country_code; |
| 150 | } |
| 151 | |
| 152 | $database_path = $this->get_database_path(); |
| 153 | if ( ! file_exists( $database_path ) ) { |
| 154 | return $country_code; |
| 155 | } |
| 156 | |
| 157 | try { |
| 158 | $reader = new MaxMind\Db\Reader( $database_path ); |
| 159 | $data = $reader->get( $ip_address ); |
| 160 | |
| 161 | if ( isset( $data['country']['iso_code'] ) ) { |
| 162 | $country_code = $data['country']['iso_code']; |
| 163 | } |
| 164 | |
| 165 | $reader->close(); |
| 166 | } catch ( Exception $e ) { |
| 167 | wc_get_logger()->notice( $e->getMessage(), array( 'source' => 'maxmind-geolocation' ) ); |
| 168 | } |
| 169 | |
| 170 | return $country_code; |
| 171 | } |
| 172 | } |
| 173 |