You are here

function smart_ip_update_user_location in Smart IP 7.2

Same name and namespace in other branches
  1. 6.2 smart_ip.module \smart_ip_update_user_location()

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.

1 call to smart_ip_update_user_location()
smart_ip_session_get in ./smart_ip.module
Read session variable.

File

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

Code

function smart_ip_update_user_location() {

  // Check if the user permitted to share location
  $share_location = smart_ip_session_get('smart_ip_user_share_location_permitted', FALSE, TRUE);
  if ($share_location) {
    global $user;
    $in_debug_mode = FALSE;
    $dont_geolocate = TRUE;
    $debug_mode_ip = '';
    $roles_debug = variable_get('smart_ip_roles_in_debug_mode', array());
    $roles_debug_ip = variable_get('smart_ip_roles_in_debug_mode_ip', array());
    $roles_geolocate = variable_get('smart_ip_roles_to_geolocate', array(
      DRUPAL_AUTHENTICATED_RID,
    ));
    foreach ($user->roles as $role_id => $role) {
      if (in_array($role_id, $roles_geolocate)) {

        // This user role is in the list of "Roles to Geolocate"
        $dont_geolocate = FALSE;
      }
      if ($role_id != DRUPAL_AUTHENTICATED_RID && isset($roles_debug[$role_id]) && $roles_debug[$role_id]) {

        // Prioritize other roles than 'authenticated'
        $in_debug_mode = TRUE;
        $debug_mode_ip = $roles_debug_ip[$role_id];
        break;
      }
    }
    if (!$dont_geolocate) {
      if ($in_debug_mode) {

        // Use debug information instead of real information
        $ip = $debug_mode_ip;
      }
      elseif (user_is_logged_in() && isset($roles_debug[DRUPAL_AUTHENTICATED_RID]) && $roles_debug[DRUPAL_AUTHENTICATED_RID]) {

        // The 'authenticated' role should be the last to check if it is in
        // debug mode then use debug information instead of real information
        $ip = $roles_debug_ip[DRUPAL_AUTHENTICATED_RID];
      }
      else {
        $ip = ip_address();
      }
      $smart_ip_session = smart_ip_session_get('smart_ip', FALSE);
      if (!isset($smart_ip_session['location']['ip_address']) || isset($smart_ip_session['location']['ip_address']) && $smart_ip_session['location']['ip_address'] != $ip) {

        // Update the user's location if the IP address stored in session
        // is not the same as the IP address detected by the server
        smart_ip_delete_location_data();
        $location = smart_ip_get_location($ip);
        user_location_fallback($location);
        $smart_ip_session['location'] = $location;
        smart_ip_session_set('smart_ip', $smart_ip_session);
        smart_ip_set_user_data($user, $location);
      }
    }
  }
}