You are here

function device_geolocation_detector_ajax in Smart IP 6.2

Same name and namespace in other branches
  1. 6 modules/device_geolocation/device_geolocation.module \device_geolocation_detector_ajax()
  2. 7.2 modules/device_geolocation/device_geolocation.module \device_geolocation_detector_ajax()
  3. 7 modules/device_geolocation/device_geolocation.module \device_geolocation_detector_ajax()

Google Geocoding ajax callback function data recipient.

1 string reference to 'device_geolocation_detector_ajax'
device_geolocation_menu in modules/device_geolocation/device_geolocation.module
Implements hook_menu().

File

modules/device_geolocation/device_geolocation.module, line 231
Provides visitor's geographical location using client device location source that implements W3C Geolocation API and Google Geocoding service.

Code

function device_geolocation_detector_ajax() {
  if (isset($_POST['latitude']) && isset($_POST['longitude'])) {
    global $user;
    $smart_ip_session = smart_ip_session_get('smart_ip');
    $ip_address = $smart_ip_session['location']['ip_address'];
    $smart_ip_session['maxmind_location'] = $smart_ip_session['location'];
    smart_ip_session_set('device_geolocation', NULL);
    unset($smart_ip_session['location']);
    $smart_ip_session['location']['ip_address'] = $ip_address;
    $smart_ip_session['location']['country'] = '';
    $smart_ip_session['location']['country_code'] = '';
    $smart_ip_session['location']['region'] = '';
    $smart_ip_session['location']['region_code'] = '';
    $smart_ip_session['location']['city'] = '';
    $smart_ip_session['location']['zip'] = '';
    $smart_ip_session['location']['latitude'] = '';
    $smart_ip_session['location']['longitude'] = '';
    $smart_ip_session['location']['time_zone'] = '';
    $smart_ip_session['location']['is_eu_country'] = '';
    $smart_ip_session['location']['is_gdpr_country'] = '';
    foreach ($_POST as $label => $address) {
      if (!empty($address) && is_string($address) && is_string($label)) {
        $label = check_plain($label);
        if (!is_array($address)) {
          $smart_ip_session['location'][$label] = check_plain($address);
        }
        else {
          $smart_ip_session['location'][$label] = $address;
        }
        smart_ip_session_set('device_geolocation', TRUE);
      }
    }
    $smart_ip_session['location']['timestamp'] = (int) $_SERVER['REQUEST_TIME'];

    // Allow other modules to modify result via hook_device_geolocation_detector_ajax_alter()
    drupal_alter('device_geolocation_detector_ajax', $smart_ip_session['location']);
    if (isset($smart_ip_session['location']) && $user->uid) {
      $user_obj = user_load($user->uid);
      user_save($user_obj, array(
        'geoip_location' => $smart_ip_session['location'],
      ));
    }
    smart_ip_session_set('smart_ip', $smart_ip_session);
  }
}