You are here

function uc_ip2country_user in IP-based Determination of a Visitor's Country 5

Implementation of hook_user().

Detects IP and determines country upon user login.

File

./uc_ip2country.module, line 133
Determination of user's Country based on IP

Code

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

      // Successful login. First determine user's country based on IP
      $ip = $_SERVER['REMOTE_ADDR'];
      $country_code = uc_ip2country_get_country($ip);

      // Now check to see if this user has "administer ip2country" permission
      // and if debug mode set.  If both are TRUE, use debug information
      // instead of real information
      if (user_access('administer ip2country') && variable_get('ip2country_debug', FALSE)) {
        $type = variable_get('ip2country_test_type', 0);
        if ($type == 0) {

          // Debug Country entered
          $country_code = db_result(db_query("SELECT country_iso_code_2 FROM {uc_countries} WHERE country_id = %d", variable_get('ip2country_test_country', 'US')));
        }
        else {

          // Debug IP entered
          $ip = variable_get('ip2country_test_ip', $ip);
          $country_code = uc_ip2country_get_country($ip);
        }
        drupal_set_message(t('Using DEBUG value for Country - @country', array(
          '@country' => $country_code,
        )));
      }

      // Finally, save country, if it has been determined
      if ($country_code) {

        // Store the ISO country code in the $user object
        user_save($account, array(
          'country_iso_code_2' => $country_code,
        ));
      }
      break;
  }
}