You are here

function smart_ip_get_location in Smart IP 6.2

Same name and namespace in other branches
  1. 6 smart_ip.module \smart_ip_get_location()
  2. 7.2 smart_ip.module \smart_ip_get_location()
  3. 7 smart_ip.module \smart_ip_get_location()

Get the geo location from the IP address

Parameters

string $ip_address: IP address to query for geolocation. It will use current user's IP address if empty.

Return value

array Geolocation details of queried IP address

3 calls to smart_ip_get_location()
smart_ip_get_current_visitor_location_data in ./smart_ip.module
Get current visitor's location information
smart_ip_update_user_location in ./smart_ip.module
Update user's location only if the IP address stored in session is not the same as the IP address detected by the server or debug mode IP.
_smart_ip_lookup_submit in includes/smart_ip.admin.inc
Submit handler to lookup an IP address in the database.

File

./smart_ip.module, line 285
Determines country, geo location (longitude/latitude), region, city and postal code of the user, based on IP address

Code

function smart_ip_get_location($ip_address = NULL) {
  $ip_address = trim($ip_address);

  // Options used in drupal_http_request().
  $timeout = variable_get('smart_ip_get_location_timeout', 2);
  if (empty($ip_address)) {
    $ip_address = ip_address();
  }

  // Use a static cache if this function is called more often
  // for the same ip on the same page.
  static $results;
  if (isset($results[$ip_address])) {
    return $results[$ip_address];
  }
  $result = array();
  $smart_ip_source = variable_get('smart_ip_source', 'ipinfodb_service');
  if ($smart_ip_source == 'local_db') {

    // Local database
    $ip = explode('.', $ip_address);
    if (count($ip) == 4) {
      smart_ip_check_fix_local_db();
      $ipl = (($ip[0] * 256 + $ip[1]) * 256 + $ip[2]) * 256 + $ip[3];
      $result = db_fetch_array(db_query('SELECT * FROM {smart_ip} WHERE ip_ref <= %d ORDER BY ip_ref DESC LIMIT 1', array(
        '%d' => $ipl,
      )));
      if (!empty($result)) {
        module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
        $countries = country_get_predefined_list();
        $result['country'] = $countries[$result['country_code']];
        $region = array();
        $result['region_code'] = $result['region'];
        $region = smart_ip_get_region_static($result['country_code'], $result['region_code']);
        $result['region'] = $region[$result['country_code']][$result['region_code']];
      }
    }
  }
  elseif ($smart_ip_source == 'mod_geoip') {
    if (function_exists('apache_note')) {
      $result['country'] = apache_note('GEOIP_COUNTRY_NAME');
      $result['country_code'] = apache_note('GEOIP_COUNTRY_CODE');
      $result['country_code'] = strtoupper($result['country_code']);
      $result['region'] = apache_note('GEOIP_REGION_NAME');
      $result['region_code'] = apache_note('GEOIP_REGION');
      $result['city'] = apache_note('GEOIP_CITY');
      $result['zip'] = apache_note('GEOIP_POSTAL_CODE');
      $result['latitude'] = apache_note('GEOIP_LATITUDE');
      $result['longitude'] = apache_note('GEOIP_LONGITUDE');
      $result['time_zone'] = '';
    }
    else {
      $result['country'] = isset($_SERVER['GEOIP_COUNTRY_NAME']) ? $_SERVER['GEOIP_COUNTRY_NAME'] : '';
      $result['country_code'] = isset($_SERVER['GEOIP_COUNTRY_CODE']) ? $_SERVER['GEOIP_COUNTRY_CODE'] : '';
      $result['region'] = isset($_SERVER['GEOIP_REGION_NAME']) ? $_SERVER['GEOIP_REGION_NAME'] : '';
      $result['region_code'] = isset($_SERVER['GEOIP_REGION']) ? strtoupper($_SERVER['GEOIP_REGION']) : '';
      $result['city'] = isset($_SERVER['GEOIP_CITY']) ? $_SERVER['GEOIP_CITY'] : '';
      $result['zip'] = isset($_SERVER['GEOIP_POSTAL_CODE']) ? $_SERVER['GEOIP_POSTAL_CODE'] : '';
      $result['latitude'] = isset($_SERVER['GEOIP_LATITUDE']) ? $_SERVER['GEOIP_LATITUDE'] : '';
      $result['longitude'] = isset($_SERVER['GEOIP_LONGITUDE']) ? $_SERVER['GEOIP_LONGITUDE'] : '';
      $result['time_zone'] = '';
    }
  }
  elseif ($smart_ip_source == 'maxmindgeoip_service') {

    // MaxMind GeoIP Legacy Web Services
    module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
    $countries = country_get_predefined_list();
    $request = drupal_http_request(smart_ip_get_maxmindgeoip_url($ip_address), array(), 'GET', NULL, 3, $timeout);
    if (!empty($request->code) && $request->code == 200 && !empty($request->data)) {
      switch (variable_get('smart_ip_maxmind_service', 'country')) {
        case 'country':

          // For MaxMind GeoIP Legacy Country Web Service
          // In case of errors the country code "(null),IP_NOT_FOUND" is returned,
          // so we need to check if the code is in our countries list.
          $result['country_code'] = isset($request->data) && isset($countries[$request->data]) ? strtoupper($request->data) : '';
          $result['country'] = empty($result['country_code']) ? '' : $countries[$request->data];
          $result['region'] = '';
          $result['region_code'] = '';
          $result['city'] = '';
          $result['zip'] = '';
          $result['latitude'] = '';
          $result['longitude'] = '';
          $result['time_zone'] = '';
          break;
        case 'city':

          // For MaxMind GeoIP Legacy City Web Service
          // In case of errors the country code "(null),IP_NOT_FOUND" is returned,
          // so we need to check if the code is in our countries list.
          $response = str_getcsv($request->data);
          $result['country_code'] = isset($request->data) && isset($countries[$response[0]]) ? strtoupper($response[0]) : '';
          $result['country'] = empty($result['country_code']) ? '' : $countries[$response[0]];
          $result['region'] = $response[1];
          $result['region_code'] = $response[1];
          $result['city'] = $response[2];
          $result['zip'] = '';
          $result['latitude'] = $response[3];
          $result['longitude'] = $response[4];
          $result['time_zone'] = '';
          break;
        case 'city_isp_org':

          // For MaxMind GeoIP Legacy City/ISP/Organization Web Service
          // In case of errors the country code "(null),IP_NOT_FOUND" is returned,
          // so we need to check if the code is in our countries list.
          $response = str_getcsv($request->data);
          $result['country_code'] = isset($request->data) && isset($countries[$response[0]]) ? strtoupper($response[0]) : '';
          $result['country'] = empty($result['country_code']) ? '' : $countries[$response[0]];
          $result['region'] = $response[1];
          $result['region_code'] = $response[1];
          $result['city'] = $response[2];
          $result['zip'] = $response[3];
          $result['latitude'] = $response[4];
          $result['longitude'] = $response[5];
          $result['time_zone'] = '';
          $result['metro_code'] = $response[6];
          $result['area_code'] = $response[7];
          $result['isp_name'] = $response[8];
          $result['organization_name'] = $response[9];
          break;
        case 'omni':

          // For MaxMind GeoIP Legacy Insights (formerly Omni) Web Service
          // In case of errors the country code "(null),IP_NOT_FOUND" is returned,
          // so we need to check if the code is in our countries list.
          $response = str_getcsv($request->data);
          $result['country_code'] = $response[0];
          $result['country'] = $response[1];
          $result['region'] = $response[3];
          $result['region_code'] = $response[2];
          $result['city'] = $response[4];
          $result['zip'] = $response[11];
          $result['latitude'] = $response[5];
          $result['longitude'] = $response[6];
          $result['time_zone'] = $response[9];
          $result['metro_code'] = $response[7];
          $result['area_code'] = $response[8];
          $result['isp_name'] = $response[12];
          $result['domain'] = $response[14];
          $result['autonomous_system_number'] = $response[15];
          $result['netspeed'] = $response[16];
          $result['user_type'] = $response[17];
          $result['country_confidence_factor'] = $response[19];
          $result['city_confidence_factor'] = $response[20];
          $result['region_confidence_factor'] = $response[21];
          $result['zip_confidence_factor'] = $response[22];
          $result['accuracy_radius'] = $response[18];
          $result['continent_code'] = $response[10];
          $result['organization_name'] = $response[13];
          break;
      }
    }
    else {
      watchdog('smart_ip', 'The MaxMind GeoIP Legacy Web Service request from %site seems to be broken, due to "%error".', array(
        '%site' => $smart_ip_source,
        '%error' => "{$request->code} {$request->error}",
      ), WATCHDOG_WARNING);
    }
  }
  elseif ($smart_ip_source == 'maxmindgeoip2_service') {

    // MaxMind GeoIP2 Precision Web Services
    global $language;
    $lang = $language->language;
    $request = drupal_http_request(smart_ip_get_maxmindgeoip2_url($ip_address), array(), 'GET', NULL, 3, $timeout);
    if (!empty($request->code) && $request->code == 200 && !empty($request->data)) {
      $response = json_decode($request->data, TRUE);
      $result['country_code'] = isset($response['country']['iso_code']) ? $response['country']['iso_code'] : '';
      if (isset($response['country']['names']['en'])) {
        $result['country'] = isset($response['country']['names'][$lang]) ? $response['country']['names'][$lang] : $response['country']['names']['en'];
      }
      else {
        $result['country'] = '';
      }
      if (isset($response['subdivisions'][0]['names']['en'])) {
        $result['region'] = isset($response['subdivisions'][0]['names'][$lang]) ? $response['subdivisions'][0]['names'][$lang] : $response['subdivisions'][0]['names']['en'];
      }
      else {
        $result['region'] = '';
      }
      $result['region_code'] = isset($response['subdivisions'][0]['iso_code']) ? $response['subdivisions'][0]['iso_code'] : '';
      if (isset($response['city']['names']['en'])) {
        $result['city'] = isset($response['city']['names'][$lang]) ? $response['city']['names'][$lang] : $response['city']['names']['en'];
      }
      else {
        $result['city'] = '';
      }
      $result['zip'] = isset($response['postal']['code']) ? $response['postal']['code'] : '';
      $result['latitude'] = isset($response['location']['latitude']) ? $response['location']['latitude'] : '';
      $result['longitude'] = isset($response['location']['longitude']) ? $response['location']['longitude'] : '';
      $result['time_zone'] = isset($response['location']['time_zone']) ? $response['location']['time_zone'] : '';
      $result['is_eu_country'] = isset($response['country']['is_in_european_union']) ? $response['country']['is_in_european_union'] : '';
      if (isset($response['location']['metro_code'])) {
        $result['metro_code'] = $response['location']['metro_code'];
      }
      if (isset($response['location']['accuracy_radius'])) {
        $result['accuracy_radius'] = $response['location']['accuracy_radius'];
      }
      if (isset($response['location']['average_income'])) {
        $result['average_income'] = $response['location']['average_income'];
      }
      if (isset($response['location']['population_density'])) {
        $result['population_density'] = $response['location']['population_density'];
      }
      if (isset($response['continent']['names'][$lang])) {
        $result['continent'] = $response['continent']['names'][$lang];
      }
      elseif (isset($response['continent']['names']['en'])) {
        $result['continent'] = $response['continent']['names']['en'];
      }
      if (isset($response['continent']['code'])) {
        $result['continent'] = $response['continent']['code'];
      }
      if (isset($response['traits']['isp'])) {
        $result['isp_name'] = $response['traits']['isp'];
      }
      if (isset($response['traits']['domain'])) {
        $result['domain'] = $response['traits']['domain'];
      }
      if (isset($response['traits']['netspeed'])) {
        $result['netspeed'] = $response['traits']['netspeed'];
      }
      if (isset($response['traits']['user_type'])) {
        $result['user_type'] = $response['traits']['user_type'];
      }
      if (isset($response['traits']['organization'])) {
        $result['organization_name'] = $response['traits']['organization'];
      }
      if (isset($response['traits']['autonomous_system_number'])) {
        $result['autonomous_system_number'] = $response['traits']['autonomous_system_number'];
      }
      if (isset($response['traits']['autonomous_system_organization'])) {
        $result['autonomous_system_organization'] = $response['traits']['autonomous_system_organization'];
      }
      if (isset($response['traits']['is_anonymous_proxy'])) {
        $result['is_anonymous_proxy'] = $response['traits']['is_anonymous_proxy'];
      }
      if (isset($response['traits']['is_satellite_provider'])) {
        $result['is_satellite_provider'] = $response['traits']['is_satellite_provider'];
      }
      if (isset($response['country']['confidence'])) {
        $result['country_confidence_factor'] = $response['country']['confidence'];
      }
      if (isset($response['city']['confidence'])) {
        $result['city_confidence_factor'] = $response['city']['confidence'];
      }
      if (isset($response['postal']['confidence'])) {
        $result['zip_confidence_factor'] = $response['postal']['confidence'];
      }
      if (isset($response['subdivisions'][0]['confidence'])) {
        $result['region_confidence_factor'] = $response['subdivisions'][0]['confidence'];
      }
    }
    else {
      watchdog('smart_ip', 'The Maxmind GeoIP2 Precision Web Service request from %site seems to be broken, due to "%error".', array(
        '%site' => $smart_ip_source,
        '%error' => "{$request->code} {$request->error}",
      ), WATCHDOG_WARNING);
    }
  }
  elseif ($smart_ip_source == 'maxmind_bin') {

    // MaxMind GeoIP Legacy binary database
    module_load_include('inc', 'smart_ip', 'includes/geoipcity');
    $db_path = variable_get('smart_ip_maxmind_bin_custom_path', '');
    if (empty($db_path) || variable_get('smart_ip_maxmind_bin_db_auto_update', TRUE)) {
      $db_path = file_directory_path() . '/smart_ip';
    }
    $version = variable_get('smart_ip_maxmind_bin_version', SMART_IP_MAXMIND_BIN_LICENSED_VERSION);
    $edition = variable_get('smart_ip_maxmind_bin_edition', SMART_IP_MAXMIND_BIN_EDITION_CITY);
    $filename = smart_ip_get_bin_source_filename($version, $edition);
    $db_path = "{$db_path}/{$filename}.dat";
    if (file_exists($db_path)) {
      require 'includes/geoipregionvars.php';
      $gi = geoip_open($db_path, GEOIP_STANDARD);
      switch ($edition) {
        case SMART_IP_MAXMIND_BIN_EDITION_CITY:
          $rec = geoip_record_by_addr($gi, $ip_address);
          if ($rec) {
            $result['country'] = $rec->country_name;
            $result['country_code'] = strtoupper($rec->country_code);
            $result['city'] = $rec->city;
            $result['zip'] = $rec->postal_code;
            $result['region_code'] = $rec->region;
            $result['region'] = isset($GEOIP_REGION_NAME[$rec->country_code][$rec->region]) ? $GEOIP_REGION_NAME[$rec->country_code][$rec->region] : '';
            $result['latitude'] = $rec->latitude;
            $result['longitude'] = $rec->longitude;
            $result['time_zone'] = '';
          }
          break;
        case SMART_IP_MAXMIND_BIN_EDITION_COUNTRY:
          $rec = geoip_country_code_by_addr($gi, $ip_address);
          if ($rec) {
            module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
            $countries = country_get_predefined_list();
            $result['country_code'] = strtoupper($rec);
            $result['country'] = $countries[$result['country_code']];
            $result['region'] = '';
            $result['region_code'] = '';
            $result['city'] = '';
            $result['zip'] = '';
            $result['latitude'] = '';
            $result['longitude'] = '';
            $result['time_zone'] = '';
          }
          break;
      }
      geoip_close($gi);
    }
    else {
      watchdog('smart_ip', 'Maxmind bin database has not been downloaded yet', array(), WATCHDOG_NOTICE);
      variable_set('smart_ip_maxmind_bin_db_update_error', t('Maxmind bin database does not exist (you can download the database now to get rid of this message).'));
    }
  }
  elseif ($smart_ip_source == 'maxmindgeoip2_bin') {

    // MaxMind GeoIP2 binary database
    module_load_include('php', 'smart_ip', 'includes/vendor/autoload');
    $version = variable_get('smart_ip_maxmind_geoip2_bin_version', SMART_IP_MAXMIND_BIN_LICENSED_VERSION);
    $edition = variable_get('smart_ip_maxmind_geoip2_bin_edition', SMART_IP_MAXMIND_GEOIP2_BIN_EDITION_CITY);
    $filename = smart_ip_get_geoip2_bin_source_filename($version, $edition);
    $db_path = variable_get('smart_ip_maxmind_geoip2_bin_custom_path', '');
    if (empty($db_path)) {
      $db_path = file_directory_path() . '/smart_ip';
    }
    $db_path = "{$db_path}/{$filename}";
    if (file_exists($db_path)) {
      if (class_exists('MaxMind\\Db\\Reader')) {
        $reader = new MaxMind\Db\Reader($db_path);
        $raw = $reader
          ->get($ip_address);
      }
      else {
        $reader = new GeoIp2\Database\Reader($db_path);
        if ($edition == SMART_IP_MAXMIND_GEOIP2_BIN_EDITION_CITY) {
          $record = $reader
            ->city($ip_address);
        }
        elseif ($edition == SMART_IP_MAXMIND_GEOIP2_BIN_EDITION_COUNTRY) {
          $record = $reader
            ->country($ip_address);
        }
        if (!empty($record)) {
          $raw = $record
            ->jsonSerialize();
        }
      }
      if (!empty($raw)) {
        global $language;
        $lang = $language->language;
        if (!isset($raw['country']['names'][$lang])) {

          // The current language is not yet supported by MaxMind, use English as
          // default language.
          $lang = 'en';
        }
        $result['original_data'] = $raw;
        $result['country'] = isset($raw['country']['names'][$lang]) ? $raw['country']['names'][$lang] : '';
        $result['country_code'] = isset($raw['country']['iso_code']) ? strtoupper($raw['country']['iso_code']) : '';
        $result['city'] = isset($raw['city']['names'][$lang]) ? $raw['city']['names'][$lang] : '';
        $result['zip'] = isset($raw['postal']['code']) ? $raw['postal']['code'] : '';
        $result['region_code'] = isset($raw['subdivisions'][0]['iso_code']) ? $raw['subdivisions'][0]['iso_code'] : '';
        $result['region'] = isset($raw['subdivisions'][0]['names'][$lang]) ? $raw['subdivisions'][0]['names'][$lang] : '';
        $result['latitude'] = isset($raw['location']['latitude']) ? $raw['location']['latitude'] : '';
        $result['longitude'] = isset($raw['location']['longitude']) ? $raw['location']['longitude'] : '';
        $result['time_zone'] = isset($raw['location']['time_zone']) ? $raw['location']['time_zone'] : '';
        $result['is_eu_country'] = isset($raw['country']['is_in_european_union']) ? $raw['country']['is_in_european_union'] : '';
      }
    }
  }
  elseif ($smart_ip_source == 'ip2location_bin') {

    // IP2Location binary database
    module_load_include('php', 'smart_ip', 'includes/vendor/autoload');
    $ip_ver = smart_ip_get_ip_address_version($ip_address);
    if ($ip_ver == 4) {
      $custom_path = variable_get('smart_ip_ip2location_bin_path', '');
    }
    elseif ($ip_ver == 6) {
      $custom_path = variable_get('smart_ip_ip2location_ipv6_bin_path', '');
    }
    if (!empty($custom_path)) {
      switch (variable_get('smart_ip_ip2location_bin_cache', 'no_cache')) {
        case 'shared_memory':
          $location_data = new IP2Location\Database($custom_path, IP2Location\Database::SHARED_MEMORY);
          break;
        case 'memory_cache':
          $location_data = new IP2Location\Database($custom_path, IP2Location\Database::MEMORY_CACHE);
          break;
        case 'no_cache':
        default:
          $location_data = new IP2Location\Database($custom_path, IP2Location\Database::FILE_IO);
          break;
      }
      $record = $location_data
        ->lookup($ip_address, IP2Location\Database::ALL);
      foreach ($record as &$item) {
        if (strpos($item, 'Please upgrade') !== FALSE || strpos($item, 'Invalid IP address') !== FALSE || $item == '-') {

          // Make the value "This parameter is unavailable in selected .BIN
          // data file. Please upgrade." or "Invalid IP address" or "-" as
          // NULL.
          $item = NULL;
        }
      }
      $result['original_data'] = $record;
      $result['country_code'] = isset($record['countryCode']) ? strtoupper($record['countryCode']) : '';
      $result['country'] = isset($record['countryName']) ? $record['countryName'] : '';
      $result['region'] = isset($record['regionName']) ? $record['regionName'] : '';
      $result['region_code'] = '';
      $result['city'] = isset($record['cityName']) ? $record['cityName'] : '';
      $result['zip'] = isset($record['zipCode']) ? $record['zipCode'] : '';
      $result['latitude'] = isset($record['latitude']) ? $record['latitude'] : '';
      $result['longitude'] = isset($record['longitude']) ? $record['longitude'] : '';
      $result['time_zone'] = isset($record['timeZone']) ? $record['timeZone'] : '';
    }
  }
  elseif ($smart_ip_source == 'ipinfodb_service') {

    // IPInfoDB.com service
    $ipinfodb_key = variable_get('smart_ip_ipinfodb_key', 0);
    $version = variable_get('smart_ip_use_ipinfodb_api_version', 3);
    $request = drupal_http_request(smart_ip_get_ipinfodb_url($ipinfodb_key, $ip_address, $version), array(), 'GET', NULL, 3, $timeout);
    $request_data = isset($request->data) ? (array) json_decode($request->data) : '';
    if (!empty($request_data)) {
      foreach ($request_data as &$datum) {
        if (strpos($datum, 'Please upgrade') !== FALSE || strpos($datum, 'Invalid IP address') !== FALSE || $datum == '-') {

          // Make the value "This parameter is unavailable in selected .BIN
          // data file. Please upgrade." or "Invalid IP address" or "-" as
          // NULL.
          $datum = NULL;
        }
      }
      if ($version == 3) {

        // IPInfoDB version 3
        $result['country'] = isset($request_data['countryName']) ? ucwords(strtolower($request_data['countryName'])) : '';
        $result['country_code'] = isset($request_data['countryCode']) ? strtoupper($request_data['countryCode']) : '';
        $result['region'] = isset($request_data['regionName']) ? ucwords(strtolower($request_data['regionName'])) : '';
        $result['region_code'] = '';
        $result['city'] = isset($request_data['cityName']) ? ucwords(strtolower($request_data['cityName'])) : '';
        $result['zip'] = isset($request_data['zipCode']) ? $request_data['zipCode'] : '';
        $result['latitude'] = isset($request_data['latitude']) ? $request_data['latitude'] : '';
        $result['longitude'] = isset($request_data['longitude']) ? $request_data['longitude'] : '';
        $result['time_zone'] = isset($request_data['timeZone']) ? $request_data['timeZone'] : '';
      }
      else {

        // IPInfoDB version 2
        $region = '';
        if (isset($request_data['RegionCode']) && isset($request_data['CountryCode'])) {
          $region_result = array();
          $region_result = smart_ip_get_region_static($request_data['CountryCode'], $request_data['RegionCode']);
          $region = $region_result[$request_data['CountryCode']][$request_data['RegionCode']];
        }
        elseif (isset($request_data['RegionName'])) {
          $region = $request_data['RegionName'];
        }
        $result['country'] = isset($request_data['CountryName']) ? $request_data['CountryName'] : '';
        $result['country_code'] = isset($request_data['CountryCode']) ? strtoupper($request_data['CountryCode']) : '';
        $result['region'] = $region;
        $result['region_code'] = isset($request_data['RegionCode']) ? $request_data['RegionCode'] : '';
        $result['city'] = isset($request_data['City']) ? $request_data['City'] : '';
        $result['zip'] = isset($request_data['ZipPostalCode']) ? $request_data['ZipPostalCode'] : '';
        $result['latitude'] = isset($request_data['Latitude']) ? $request_data['Latitude'] : '';
        $result['longitude'] = isset($request_data['Longitude']) ? $request_data['Longitude'] : '';
        $result['time_zone'] = '';
      }
    }
  }
  elseif ($smart_ip_source == 'abstract_service') {

    // Abstract IP Geolocation service
    $abstract_key = variable_get('smart_ip_abstract_key', 0);
    $version = variable_get('smart_ip_abstract_version', 1);
    $request = drupal_http_request(smart_ip_get_abstract_url($abstract_key, $ip_address, $version), array(), 'GET', NULL, 3, $timeout);
    $request_data = isset($request->data) ? (array) json_decode($request->data) : '';
    if (!empty($request_data) && !isset($request_data['error'])) {
      if ($version == 1) {

        // Abstract IP Geolocation service version 1
        $result['country'] = isset($request_data['country']) ? $request_data['country'] : '';
        $result['country_code'] = isset($request_data['country_code']) ? strtoupper($request_data['country_code']) : '';
        $result['region'] = isset($request_data['region']) ? $request_data['region'] : '';
        $result['region_code'] = isset($request_data['region_iso_code']) ? $request_data['region_iso_code'] : '';
        $result['city'] = isset($request_data['city']) ? $request_data['city'] : '';
        $result['zip'] = isset($request_data['postal_code']) ? $request_data['postal_code'] : '';
        $result['latitude'] = isset($request_data['latitude']) ? $request_data['latitude'] : '';
        $result['longitude'] = isset($request_data['longitude']) ? $request_data['longitude'] : '';
        $result['time_zone'] = isset($request_data['timezone']->name) ? $request_data['timezone']->name : '';
      }
    }
  }
  elseif ($smart_ip_source == 'x_header') {

    // X-GeoIP
    module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
    $countries = country_get_predefined_list();
    $result['country_code'] = isset($_SERVER['HTTP_X_GEOIP_COUNTRY']) ? $_SERVER['HTTP_X_GEOIP_COUNTRY'] : '';
    $result['country'] = empty($result['country_code']) ? '' : $countries[$result['country_code']];
    $result['region'] = '';
    $result['region_code'] = '';
    $result['city'] = '';
    $result['zip'] = '';
    $result['latitude'] = '';
    $result['longitude'] = '';
    $result['time_zone'] = '';
  }
  elseif ($smart_ip_source == 'es_header') {

    // Akamai EdgeScape IP Geolocation
    module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
    $countries = country_get_predefined_list();
    if (isset($_SERVER['HTTP_X_AKAMAI_EDGESCAPE'])) {
      parse_str(str_replace(',', '&', $_SERVER['HTTP_X_AKAMAI_EDGESCAPE']), $es_header);
    }
    $result['country_code'] = isset($es_header['country_code']) ? $es_header['country_code'] : '';
    $result['country_code'] = strtoupper($result['country_code']);
    $result['country'] = empty($result['country_code']) ? '' : $countries[$result['country_code']];
    $result['region_code'] = isset($es_header['region_code']) ? $es_header['region_code'] : '';
    $region = '';
    if (isset($result['region_code']) && isset($result['country_code'])) {
      $region_result = array();
      $region_result = smart_ip_get_region_static($result['country_code'], $result['region_code']);
      $region = $region_result[$result['country_code']][$result['region_code']];
    }
    $result['region'] = $region;
    $result['city'] = isset($es_header['city']) ? $es_header['city'] : '';
    $result['zip'] = isset($es_header['zip']) ? $es_header['zip'] : '';
    $result['latitude'] = isset($es_header['lat']) ? $es_header['lat'] : '';
    $result['longitude'] = isset($es_header['long']) ? $es_header['long'] : '';
    $result['time_zone'] = isset($es_header['timezone']) ? $es_header['timezone'] : '';
  }
  elseif ($smart_ip_source == 'cf_header') {

    // Cloudflare IP Geolocation
    module_load_include('inc', 'smart_ip', 'includes/smart_ip.country_list');
    $countries = country_get_predefined_list();
    $result['country_code'] = isset($_SERVER['HTTP_CF_IPCOUNTRY']) ? $_SERVER['HTTP_CF_IPCOUNTRY'] : '';
    $result['country_code'] = strtoupper($result['country_code']);
    $result['country'] = empty($result['country_code']) ? '' : $countries[$result['country_code']];
    $result['region'] = '';
    $result['region_code'] = '';
    $result['city'] = '';
    $result['zip'] = '';
    $result['latitude'] = '';
    $result['longitude'] = '';
    $result['time_zone'] = '';
  }
  $result['source'] = SMART_IP_SOURCE_SMART_IP;
  $result['ip_address'] = $ip_address;
  $result['timestamp'] = (int) $_SERVER['REQUEST_TIME'];
  smart_ip_update_fields($result);

  // Allow other modules to modify result via hook_smart_ip_get_location_alter()
  drupal_alter('smart_ip_get_location', $result);
  $results[$ip_address] = $result;
  return $result;
}