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0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / includes / class-wc-geolocation.php
woocommerce / includes Last commit date
abstracts 9 years ago admin 9 years ago api 9 years ago cli 9 years ago data-stores 9 years ago emails 9 years ago gateways 9 years ago interfaces 9 years ago legacy 9 years ago libraries 9 years ago log-handlers 9 years ago payment-tokens 9 years ago shipping 9 years ago shortcodes 9 years ago theme-support 9 years ago vendor 9 years ago walkers 9 years ago widgets 9 years ago class-wc-ajax.php 9 years ago class-wc-api.php 9 years ago class-wc-auth.php 9 years ago class-wc-autoloader.php 9 years ago class-wc-background-emailer.php 9 years ago class-wc-background-updater.php 9 years ago class-wc-breadcrumb.php 9 years ago class-wc-cache-helper.php 9 years ago class-wc-cart.php 9 years ago class-wc-checkout.php 9 years ago class-wc-cli.php 9 years ago class-wc-comments.php 9 years ago class-wc-countries.php 9 years ago class-wc-coupon.php 9 years ago class-wc-customer-download.php 9 years ago class-wc-customer.php 9 years ago class-wc-data-exception.php 9 years ago class-wc-data-store.php 9 years ago class-wc-datetime.php 9 years ago class-wc-deprecated-action-hooks.php 9 years ago class-wc-deprecated-filter-hooks.php 9 years ago class-wc-download-handler.php 9 years ago class-wc-emails.php 9 years ago class-wc-embed.php 9 years ago class-wc-form-handler.php 9 years ago class-wc-frontend-scripts.php 9 years ago class-wc-geo-ip.php 9 years ago class-wc-geolocation.php 9 years ago class-wc-https.php 9 years ago class-wc-install.php 9 years ago class-wc-integrations.php 9 years ago class-wc-legacy-api.php 9 years ago class-wc-log-levels.php 9 years ago class-wc-logger.php 9 years ago class-wc-order-factory.php 9 years ago class-wc-order-item-coupon.php 9 years ago class-wc-order-item-fee.php 9 years ago class-wc-order-item-meta.php 9 years ago class-wc-order-item-product.php 9 years ago class-wc-order-item-shipping.php 9 years ago class-wc-order-item-tax.php 9 years ago class-wc-order-item.php 9 years ago class-wc-order-refund.php 9 years ago class-wc-order.php 9 years ago class-wc-payment-gateways.php 9 years ago class-wc-payment-tokens.php 9 years ago class-wc-post-data.php 9 years ago class-wc-post-types.php 9 years ago class-wc-product-attribute.php 9 years ago class-wc-product-download.php 9 years ago class-wc-product-external.php 9 years ago class-wc-product-factory.php 9 years ago class-wc-product-grouped.php 9 years ago class-wc-product-simple.php 9 years ago class-wc-product-variable.php 9 years ago class-wc-product-variation.php 9 years ago class-wc-query.php 9 years ago class-wc-register-wp-admin-settings.php 9 years ago class-wc-session-handler.php 9 years ago class-wc-shipping-rate.php 9 years ago class-wc-shipping-zone.php 9 years ago class-wc-shipping-zones.php 9 years ago class-wc-shipping.php 9 years ago class-wc-shortcodes.php 9 years ago class-wc-structured-data.php 9 years ago class-wc-tax.php 9 years ago class-wc-template-loader.php 9 years ago class-wc-tracker.php 9 years ago class-wc-validation.php 9 years ago class-wc-webhook.php 9 years ago wc-account-functions.php 9 years ago wc-attribute-functions.php 9 years ago wc-cart-functions.php 9 years ago wc-conditional-functions.php 9 years ago wc-core-functions.php 9 years ago wc-coupon-functions.php 9 years ago wc-deprecated-functions.php 9 years ago wc-formatting-functions.php 9 years ago wc-notice-functions.php 9 years ago wc-order-functions.php 9 years ago wc-order-item-functions.php 9 years ago wc-page-functions.php 9 years ago wc-product-functions.php 9 years ago wc-rest-functions.php 9 years ago wc-stock-functions.php 9 years ago wc-template-functions.php 9 years ago wc-template-hooks.php 9 years ago wc-term-functions.php 9 years ago wc-update-functions.php 9 years ago wc-user-functions.php 9 years ago wc-webhook-functions.php 9 years ago wc-widget-functions.php 9 years ago
class-wc-geolocation.php
314 lines
1 <?php
2 /**
3 * Geolocation class
4 *
5 * Handles geolocation and updating the geolocation database.
6 *
7 * This product includes GeoLite data created by MaxMind, available from http://www.maxmind.com.
8 *
9 * @author WooThemes
10 * @category Admin
11 * @package WooCommerce/Classes
12 * @version 2.4.0
13 */
14
15 if ( ! defined( 'ABSPATH' ) ) {
16 exit;
17 }
18
19 /**
20 * WC_Geolocation Class.
21 */
22 class WC_Geolocation {
23
24 /** URL to the geolocation database we're using */
25 const GEOLITE_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz';
26 const GEOLITE_IPV6_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz';
27
28 /** @var array API endpoints for looking up user IP address */
29 private static $ip_lookup_apis = array(
30 'icanhazip' => 'http://icanhazip.com',
31 'ipify' => 'http://api.ipify.org/',
32 'ipecho' => 'http://ipecho.net/plain',
33 'ident' => 'http://ident.me',
34 'whatismyipaddress' => 'http://bot.whatismyipaddress.com',
35 'ip.appspot' => 'http://ip.appspot.com',
36 );
37
38 /** @var array API endpoints for geolocating an IP address */
39 private static $geoip_apis = array(
40 'freegeoip' => 'https://freegeoip.net/json/%s',
41 'ipinfo.io' => 'https://ipinfo.io/%s/json',
42 'ip-api.com' => 'http://ip-api.com/json/%s',
43 );
44
45 /**
46 * Hook in tabs.
47 */
48 public static function init() {
49 // Only download the database from MaxMind if the geolocation function is enabled, or a plugin specifically requests it
50 if ( 'geolocation' === get_option( 'woocommerce_default_customer_address' ) || apply_filters( 'woocommerce_geolocation_update_database_periodically', false ) ) {
51 add_action( 'woocommerce_geoip_updater', array( __CLASS__, 'update_database' ) );
52 }
53 add_filter( 'pre_update_option_woocommerce_default_customer_address', array( __CLASS__, 'maybe_update_database' ), 10, 2 );
54 }
55
56 /**
57 * Maybe trigger a DB update for the first time.
58 * @param string $new_value
59 * @param string $old_value
60 * @return string
61 */
62 public static function maybe_update_database( $new_value, $old_value ) {
63 if ( $new_value !== $old_value && 'geolocation' === $new_value ) {
64 self::update_database();
65 }
66 return $new_value;
67 }
68
69 /**
70 * Get current user IP Address.
71 * @return string
72 */
73 public static function get_ip_address() {
74 if ( isset( $_SERVER['X-Real-IP'] ) ) {
75 return $_SERVER['X-Real-IP'];
76 } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) {
77 // Proxy servers can send through this header like this: X-Forwarded-For: client1, proxy1, proxy2
78 // Make sure we always only send through the first IP in the list which should always be the client IP.
79 return trim( current( explode( ',', $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) );
80 } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) {
81 return $_SERVER['REMOTE_ADDR'];
82 }
83 return '';
84 }
85
86 /**
87 * Get user IP Address using an external service.
88 * This is used mainly as a fallback for users on localhost where
89 * get_ip_address() will be a local IP and non-geolocatable.
90 * @return string
91 */
92 public static function get_external_ip_address() {
93 $transient_name = 'external_ip_address_' . self::get_ip_address();
94 $external_ip_address = get_transient( $transient_name );
95
96 if ( false === $external_ip_address ) {
97 $external_ip_address = '0.0.0.0';
98 $ip_lookup_services = apply_filters( 'woocommerce_geolocation_ip_lookup_apis', self::$ip_lookup_apis );
99 $ip_lookup_services_keys = array_keys( $ip_lookup_services );
100 shuffle( $ip_lookup_services_keys );
101
102 foreach ( $ip_lookup_services_keys as $service_name ) {
103 $service_endpoint = $ip_lookup_services[ $service_name ];
104 $response = wp_safe_remote_get( $service_endpoint, array( 'timeout' => 2 ) );
105
106 if ( ! is_wp_error( $response ) && $response['body'] ) {
107 $external_ip_address = apply_filters( 'woocommerce_geolocation_ip_lookup_api_response', wc_clean( $response['body'] ), $service_name );
108 break;
109 }
110 }
111
112 set_transient( $transient_name, $external_ip_address, WEEK_IN_SECONDS );
113 }
114
115 return $external_ip_address;
116 }
117
118 /**
119 * Geolocate an IP address.
120 * @param string $ip_address
121 * @param bool $fallback If true, fallbacks to alternative IP detection (can be slower).
122 * @param bool $api_fallback If true, uses geolocation APIs if the database file doesn't exist (can be slower).
123 * @return array
124 */
125 public static function geolocate_ip( $ip_address = '', $fallback = true, $api_fallback = true ) {
126 // Filter to allow custom geolocation of the IP address.
127 $country_code = apply_filters( 'woocommerce_geolocate_ip', false, $ip_address, $fallback, $api_fallback );
128
129 if ( false === $country_code ) {
130 // If GEOIP is enabled in CloudFlare, we can use that (Settings -> CloudFlare Settings -> Settings Overview)
131 if ( ! empty( $_SERVER['HTTP_CF_IPCOUNTRY'] ) ) {
132 $country_code = sanitize_text_field( strtoupper( $_SERVER['HTTP_CF_IPCOUNTRY'] ) );
133 // WP.com VIP has a variable available.
134 } elseif ( ! empty( $_SERVER['GEOIP_COUNTRY_CODE'] ) ) {
135 $country_code = sanitize_text_field( strtoupper( $_SERVER['GEOIP_COUNTRY_CODE'] ) );
136 // VIP Go has a variable available also.
137 } elseif ( ! empty( $_SERVER['HTTP_X_COUNTRY_CODE'] ) ) {
138 $country_code = sanitize_text_field( strtoupper( $_SERVER['HTTP_X_COUNTRY_CODE'] ) );
139 } else {
140 $ip_address = $ip_address ? $ip_address : self::get_ip_address();
141
142 if ( self::is_IPv6( $ip_address ) ) {
143 $database = self::get_local_database_path( 'v6' );
144 } else {
145 $database = self::get_local_database_path();
146 }
147
148 if ( file_exists( $database ) ) {
149 $country_code = self::geolocate_via_db( $ip_address );
150 } elseif ( $api_fallback ) {
151 $country_code = self::geolocate_via_api( $ip_address );
152 } else {
153 $country_code = '';
154 }
155
156 if ( ! $country_code && $fallback ) {
157 // May be a local environment - find external IP
158 return self::geolocate_ip( self::get_external_ip_address(), false, $api_fallback );
159 }
160 }
161 }
162
163 return array(
164 'country' => $country_code,
165 'state' => '',
166 );
167 }
168
169 /**
170 * Path to our local db.
171 * @param string $version
172 * @return string
173 */
174 public static function get_local_database_path( $version = 'v4' ) {
175 $version = ( 'v4' == $version ) ? '' : 'v6';
176 $upload_dir = wp_upload_dir();
177
178 return apply_filters( 'woocommerce_geolocation_local_database_path', $upload_dir['basedir'] . '/GeoIP' . $version . '.dat', $version );
179 }
180
181 /**
182 * Update geoip database. Adapted from https://wordpress.org/plugins/geoip-detect/.
183 */
184 public static function update_database() {
185 $logger = wc_get_logger();
186
187 if ( ! is_callable( 'gzopen' ) ) {
188 $logger->notice( 'Server does not support gzopen', array( 'source' => 'geolocation' ) );
189 return;
190 }
191
192 require_once( ABSPATH . 'wp-admin/includes/file.php' );
193
194 $tmp_databases = array(
195 'v4' => download_url( self::GEOLITE_DB ),
196 'v6' => download_url( self::GEOLITE_IPV6_DB ),
197 );
198
199 foreach ( $tmp_databases as $tmp_database_version => $tmp_database_path ) {
200 if ( ! is_wp_error( $tmp_database_path ) ) {
201 $gzhandle = @gzopen( $tmp_database_path, 'r' );
202 $handle = @fopen( self::get_local_database_path( $tmp_database_version ), 'w' );
203
204 if ( $gzhandle && $handle ) {
205 while ( $string = gzread( $gzhandle, 4096 ) ) {
206 fwrite( $handle, $string, strlen( $string ) );
207 }
208 gzclose( $gzhandle );
209 fclose( $handle );
210 } else {
211 $logger->notice( 'Unable to open database file', array( 'source' => 'geolocation' ) );
212 }
213 @unlink( $tmp_database_path );
214 } else {
215 $logger->notice(
216 'Unable to download GeoIP Database: ' . $tmp_database_path->get_error_message(),
217 array( 'source' => 'geolocation' )
218 );
219 }
220 }
221 }
222
223 /**
224 * Use MAXMIND GeoLite database to geolocation the user.
225 * @param string $ip_address
226 * @return string
227 */
228 private static function geolocate_via_db( $ip_address ) {
229 if ( ! class_exists( 'WC_Geo_IP', false ) ) {
230 include_once( WC_ABSPATH . 'includes/class-wc-geo-ip.php' );
231 }
232
233 $gi = new WC_Geo_IP();
234
235 if ( self::is_IPv6( $ip_address ) ) {
236 $database = self::get_local_database_path( 'v6' );
237 $gi->geoip_open( $database, 0 );
238 $country_code = $gi->geoip_country_code_by_addr_v6( $ip_address );
239 } else {
240 $database = self::get_local_database_path();
241 $gi->geoip_open( $database, 0 );
242 $country_code = $gi->geoip_country_code_by_addr( $ip_address );
243 }
244
245 $gi->geoip_close();
246
247 return sanitize_text_field( strtoupper( $country_code ) );
248 }
249
250 /**
251 * Use APIs to Geolocate the user.
252 * @param string $ip_address
253 * @return string|bool
254 */
255 private static function geolocate_via_api( $ip_address ) {
256 $country_code = get_transient( 'geoip_' . $ip_address );
257
258 if ( false === $country_code ) {
259 $geoip_services = apply_filters( 'woocommerce_geolocation_geoip_apis', self::$geoip_apis );
260 $geoip_services_keys = array_keys( $geoip_services );
261 shuffle( $geoip_services_keys );
262
263 foreach ( $geoip_services_keys as $service_name ) {
264 $service_endpoint = $geoip_services[ $service_name ];
265 $response = wp_safe_remote_get( sprintf( $service_endpoint, $ip_address ), array( 'timeout' => 2 ) );
266
267 if ( ! is_wp_error( $response ) && $response['body'] ) {
268 switch ( $service_name ) {
269 case 'ipinfo.io' :
270 $data = json_decode( $response['body'] );
271 $country_code = isset( $data->country ) ? $data->country : '';
272 break;
273 case 'ip-api.com' :
274 $data = json_decode( $response['body'] );
275 $country_code = isset( $data->countryCode ) ? $data->countryCode : '';
276 break;
277 case 'freegeoip' :
278 $data = json_decode( $response['body'] );
279 $country_code = isset( $data->country_code ) ? $data->country_code : '';
280 break;
281 default :
282 $country_code = apply_filters( 'woocommerce_geolocation_geoip_response_' . $service_name, '', $response['body'] );
283 break;
284 }
285
286 $country_code = sanitize_text_field( strtoupper( $country_code ) );
287
288 if ( $country_code ) {
289 break;
290 }
291 }
292 }
293
294 set_transient( 'geoip_' . $ip_address, $country_code, WEEK_IN_SECONDS );
295 }
296
297 return $country_code;
298 }
299
300 /**
301 * Test if is IPv6.
302 *
303 * @since 2.4.0
304 *
305 * @param string $ip_address
306 * @return bool
307 */
308 private static function is_IPv6( $ip_address ) {
309 return false !== filter_var( $ip_address, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6 );
310 }
311 }
312
313 WC_Geolocation::init();
314