woocommerce/includes/class-wc-geolocation.php

225 lines
6.7 KiB
PHP
Raw Normal View History

2014-12-23 18:49:37 +00:00
<?php
/**
* Geolocation class.
*
* Handles geolocation and updating the geolocation database.
*
* This product uses GeoLite2 data created by MaxMind, available from http://www.maxmind.com
*
* @author WooThemes
* @category Admin
* @package WooCommerce/Classes
* @version 2.3.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* WC_Geolocation Class
*/
class WC_Geolocation {
/** URL to the geolocation database we're using */
const GEOLITE_DB = 'http://geolite.maxmind.com/download/geoip/database/GeoLiteCountry/GeoIP.dat.gz';
2014-12-30 11:11:17 +00:00
/** @var array API endpoints for looking up user IP address */
private static $ip_lookup_apis = array(
'icanhazip' => 'http://ipv4.icanhazip.com',
'ipify' => 'http://api.ipify.org/',
'ipecho' => 'http://ipecho.net/plain',
'ident' => 'http://v4.ident.me',
'whatismyipaddress' => 'http://bot.whatismyipaddress.com',
'ip.appspot' => 'http://ip.appspot.com'
);
/** @var array API endpoints for geolocating an IP address */
private static $geoip_apis = array(
'freegeoip' => 'https://freegeoip.net/json/%s',
'telize' => 'http://www.telize.com/geoip/%s',
'ip-api' => 'http://ip-api.com/json/%s',
'geoip-api.meteor' => 'http://geoip-api.meteor.com/lookup/%s'
);
2014-12-23 18:49:37 +00:00
/**
* Hook in tabs.
*/
public static function init() {
add_action( 'woocommerce_geoip_updater', array( __CLASS__, 'update_database' ) );
}
2014-12-23 22:03:10 +00:00
/**
* Get current user IP Address
* @return string
*/
public static function get_ip_address() {
return isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
}
/**
* Get user IP Address using a service
* @return string
*/
public static function get_external_ip_address() {
$transient_name = 'external_ip_address_' . self::get_ip_address();
$external_ip_address = get_transient( $transient_name );
if ( false === $external_ip_address ) {
2014-12-30 11:11:17 +00:00
$external_ip_address = '0.0.0.0';
$ip_lookup_services = apply_filters( 'woocommerce_geolocation_ip_lookup_apis', self::$ip_lookup_apis );
$ip_lookup_services_keys = array_keys( $ip_lookup_services );
shuffle( $ip_lookup_services_keys );
2014-12-23 22:03:10 +00:00
2014-12-30 11:11:17 +00:00
foreach ( $ip_lookup_services_keys as $service_name ) {
$service_endpoint = $ip_lookup_services[ $service_name ];
$response = wp_remote_get( $service_endpoint, array( 'timeout' => 2 ) );
2014-12-23 22:03:10 +00:00
if ( ! is_wp_error( $response ) && $response['body'] ) {
2014-12-30 11:11:17 +00:00
$external_ip_address = apply_filters( 'woocommerce_geolocation_ip_lookup_api_response', $response['body'], $service_name );
2014-12-23 22:03:10 +00:00
break;
}
}
set_transient( $transient_name, $external_ip_address, WEEK_IN_SECONDS );
}
return $external_ip_address;
}
/**
* Geolocate an IP address
* @param string $ip_address
* @return array
*/
2014-12-30 11:11:17 +00:00
public static function geolocate_ip( $ip_address = '', $fallback = true ) {
2014-12-23 22:03:10 +00:00
$ip_address = $ip_address ? $ip_address : self::get_ip_address();
$database = self::get_local_database_path();
if ( file_exists( $database ) ) {
2014-12-30 11:11:17 +00:00
$country_code = self::geolocate_via_db( $ip_address );
2014-12-23 22:03:10 +00:00
} else {
2014-12-30 11:11:17 +00:00
$country_code = self::geolocate_via_api( $ip_address );
2014-12-23 22:03:10 +00:00
}
2014-12-30 11:11:17 +00:00
if ( ! $country_code && $fallback ) {
2014-12-23 22:03:10 +00:00
// May be a local environment - find external IP
2014-12-30 11:11:17 +00:00
return self::geolocate_ip( self::get_external_ip_address(), false );
2014-12-23 22:03:10 +00:00
}
return array(
'country' => $country_code,
'state' => ''
);
}
/**
* Path to our local db
* @return string
*/
private static function get_local_database_path() {
$upload_dir = wp_upload_dir();
return $upload_dir['basedir'] . '/GeoIP.dat';
}
2014-12-23 18:49:37 +00:00
/**
* Update geoip database. Adapted from https://wordpress.org/plugins/geoip-detect/.
*/
public static function update_database() {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
$tmp_database = download_url( self::GEOLITE_DB );
$logger = new WC_Logger();
if ( ! is_wp_error( $tmp_database ) ) {
$gzhandle = @gzopen( $tmp_database, 'r' );
2014-12-23 22:03:10 +00:00
$handle = @fopen( self::get_local_database_path(), 'w' );
2014-12-23 18:49:37 +00:00
if ( $gzhandle && $handle ) {
while ( ( $string = gzread( $gzhandle, 4096 ) ) != false ) {
fwrite( $handle, $string, strlen( $string ) );
}
gzclose( $gzhandle );
fclose( $handle );
} else {
$logger->add( 'geolocation', 'Unable to open database file' );
}
@unlink( $tmp_database );
} else {
$logger->add( 'geolocation', 'Unable to download GeoIP Database: ' . $tmp_database->get_message() );
}
}
/**
* Use MAXMIND GeoLite database to geolocation the user.
* @param string $ip_address
* @return string
*/
2014-12-30 11:11:17 +00:00
private static function geolocate_via_db( $ip_address ) {
2015-01-01 12:41:04 +00:00
if ( ! class_exists( 'GeoIP' ) && ! class_exists( 'geoiprecord' ) ) {
2014-12-23 22:03:10 +00:00
include_once( 'libraries/geoip.php' );
2014-12-23 18:49:37 +00:00
}
2014-12-23 22:03:10 +00:00
$database = self::get_local_database_path();
$gi = geoip_open( $database, GEOIP_STANDARD );
$country_code = geoip_country_code_by_addr( $gi, $ip_address );
geoip_close( $gi );
2014-12-23 18:49:37 +00:00
2014-12-30 11:11:17 +00:00
return sanitize_text_field( strtoupper( $country_code ) );
2014-12-23 18:49:37 +00:00
}
/**
2014-12-30 11:11:17 +00:00
* Use APIs to Geolocate the user.
2014-12-23 18:49:37 +00:00
* @param string $ip_address
2014-12-30 11:11:17 +00:00
* @return string|bool
2014-12-23 18:49:37 +00:00
*/
2014-12-30 11:11:17 +00:00
private static function geolocate_via_api( $ip_address ) {
2014-12-23 18:49:37 +00:00
$country_code = get_transient( 'geoip_' . $ip_address );
if ( false === $country_code ) {
2014-12-30 11:11:17 +00:00
$geoip_services = apply_filters( 'woocommerce_geolocation_geoip_apis', self::$geoip_apis );
$geoip_services_keys = array_keys( $geoip_services );
shuffle( $geoip_services_keys );
2014-12-23 18:49:37 +00:00
2014-12-30 11:11:17 +00:00
foreach ( $geoip_services_keys as $service_name ) {
$service_endpoint = $geoip_services[ $service_name ];
$response = wp_remote_get( sprintf( $service_endpoint, $ip_address ), array( 'timeout' => 2 ) );
if ( ! is_wp_error( $response ) && $response['body'] ) {
$country_code = '';
switch ( $service_name ) {
case 'ip-api' :
$data = json_decode( $response['body'] );
$country_code = isset( $data->countryCode ) ? $data->countryCode : '';
break;
case 'geoip-api.meteor' :
$data = json_decode( $response['body'] );
$country_code = isset( $data->country ) ? $data->country : '';
break;
case 'freegeoip' :
case 'telize' :
$data = json_decode( $response['body'] );
$country_code = isset( $data->country_code ) ? $data->country_code : '';
break;
default :
$country_code = apply_filters( 'woocommerce_geolocation_geoip_response_' . $service_name, '', $response['body'] );
break;
}
$country_code = sanitize_text_field( strtoupper( $country_code ) );
if ( $country_code ) {
break;
}
}
2014-12-23 18:49:37 +00:00
}
2014-12-30 11:11:17 +00:00
set_transient( 'geoip_' . $ip_address, $country_code, WEEK_IN_SECONDS );
2014-12-23 18:49:37 +00:00
}
return $country_code;
}
}
WC_Geolocation::init();