You are here

function smart_ip_get_location in Smart IP 6

Same name and namespace in other branches
  1. 6.2 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

Return value

FALSE if the lookup failed to find a location for this IP

4 calls to smart_ip_get_location()
smart_ip_admin_settings_submit in includes/smart_ip.admin.inc
Process Forms submitted by IP to Country administration page
smart_ip_get_current_visitor_location_data in ./smart_ip.module
Get current visitor's location information
smart_ip_user in ./smart_ip.module
Implements hook_user_login().
_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 292
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) {

  // Use a static cache if this function is called more often
  // for the same ip on the same page.
  static $results;
  $result = array();
  if (isset($results[$ip_address])) {
    return $results[$ip_address];
  }
  $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']];
      }
    }
  }
  if ($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'] = $_SERVER['GEOIP_COUNTRY_NAME'];
      $result['country_code'] = $_SERVER['GEOIP_COUNTRY_CODE'];
      $result['region'] = $_SERVER['GEOIP_REGION_NAME'];
      $result['region_code'] = strtoupper($_SERVER['GEOIP_REGION']);
      $result['city'] = $_SERVER['GEOIP_CITY'];
      $result['zip'] = $_SERVER['GEOIP_POSTAL_CODE'];
      $result['latitude'] = $_SERVER['GEOIP_LATITUDE'];
      $result['longitude'] = $_SERVER['GEOIP_LONGITUDE'];
      $result['time_zone'] = '';
    }
  }
  if ($smart_ip_source == 'maxmindgeoip_service') {

    // Maxmind GeoIP service
    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));
    if (!empty($request)) {
      switch (variable_get('smart_ip_maxmind_service', 'country')) {
        case 'country':

          // For Maxmind GeoIp Country
          // 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_isp_org':

          // For Maxmind GeoIP 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'] = '';
          $result['latitude'] = $response[3];
          $result['longitude'] = $response[4];
          $result['time_zone'] = '';
          break;
        case 'omni':

          //@TODO For Maxmind GeoIP Omni Web Service
          break;
      }
    }
  }
  if ($smart_ip_source == 'ip2location_bin') {

    // IP2Location binary file
    module_load_include('inc', 'smart_ip', 'includes/IP2Location');
    switch (variable_get('smart_ip_ip2location_bin_cache', 'no_cache')) {
      case 'shared_memory':
        $location_data = new IP2Location(variable_get('smart_ip_ip2location_bin_path', ''), IP2Location::SHARED_MEMORY);
        break;
      case 'memory_cache':
        $location_data = new IP2Location(variable_get('smart_ip_ip2location_bin_path', ''), IP2Location::MEMORY_CACHE);
        break;
      case 'no_cache':
      default:
        $location_data = new IP2Location(variable_get('smart_ip_ip2location_bin_path', ''), IP2Location::FILE_IO);
        break;
    }
    $record = $location_data
      ->lookup($ip_address, IP2Location::ALL);
    $result['country_code'] = strtoupper($record->countryCode);
    $result['country'] = ucwords(strtolower($record->countryName));
    $result['region'] = ucwords(strtolower($record->regionName));
    $result['region_code'] = '';
    $result['city'] = ucwords(strtolower($record->cityName));
    $result['zip'] = $record->zipCode;
    $result['latitude'] = $record->latitude;
    $result['longitude'] = $record->longitude;
    $result['time_zone'] = $record->timeZone;
  }
  if ($smart_ip_source == 'ipinfodb_service' || empty($result)) {

    // 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));
    $request_data = isset($request->data) ? (array) json_decode($request->data) : '';
    if (!empty($request_data)) {
      foreach ($request_data as &$datum) {
        if ($datum == '-') {
          $datum = '';
        }
      }
      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'] = '';
      }
    }
  }
  if ($smart_ip_source == 'x_header') {
    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'] = '';
  }
  if (!empty($result)) {

    // If coordinates are (0, 0) there was no match
    if ($result['latitude'] == 0 && $result['longitude'] == 0) {
      $result['latitude'] = NULL;
      $result['longitude'] = NULL;
    }
  }
  $result['ip_address'] = $ip_address;
  $result['timestamp'] = time();

  // 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;
}