You are here

function smart_ip_user in Smart IP 6

Implements hook_user_login().

Detects IP and geo location upon user login.

File

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

Code

function smart_ip_user($op, &$edit, &$account, $category = NULL) {
  switch ($op) {
    case 'login':

      // Now check to see if this user has "administer smart_ip" permission
      // and if debug mode set.  If both are TRUE, use debug information
      // instead of real information
      if (user_access('administer smart_ip') && variable_get('smart_ip_debug', FALSE)) {
        $ip = variable_get('smart_ip_test_ip_address', ip_address());
        $location = smart_ip_get_location($ip);
        if ($location) {
          drupal_set_message(t('Using debug IP: %ip / Country: %country / Region: %region / City: %city / Postal code: %zip / Longitude: %long / Latitude: %lat / Time zone: %time_zone', array(
            '%ip' => $location['ip_address'],
            '%country' => $location['country'],
            '%region' => $location['region'],
            '%city' => $location['city'],
            '%zip' => $location['zip'],
            '%long' => $location['longitude'],
            '%lat' => $location['latitude'],
            '%time_zone' => $location['time_zone'],
          )));
          $smart_ip_session = smart_ip_session_get('smart_ip');
          $smart_ip_session['location'] = $location;
          smart_ip_session_set('smart_ip', $smart_ip_session);
        }
      }
      else {
        $roles_to_geo_locate = variable_get('smart_ip_roles_to_geolocate', array(
          DRUPAL_AUTHENTICATED_RID,
        ));
        foreach ($roles_to_geo_locate as $rid) {
          if (array_key_exists($rid, $account->roles)) {
            $location = smart_ip_get_current_visitor_location_data();
            smart_ip_set_location_data($account, $location);
            break;
          }
        }
      }
      break;
  }
}