You are here

function autoban_get_ip_location in Automatic IP ban (Autoban) 7

Check own IP to prevent from ban.

Parameters

string $hostname: IP address.

bool $get_real_hostname: Retrieve hostname by ipstack.

Return value

string IP address location info.

2 calls to autoban_get_ip_location()
autoban_test in ./autoban.admin.inc
Menu callback. Test autoban rule page.
_autoban_watchdog_event_extras_event_rows in autoban_watchdog_event_extras/autoban_watchdog_event_extras.module
Implements hook_watchdog_event_extras_alter().

File

./autoban.module, line 705
Main file for autoban module.

Code

function autoban_get_ip_location($hostname, $get_real_hostname = FALSE) {
  if (empty($hostname)) {
    return '';
  }
  $access_key = variable_get('autoban_ipstack_apikey', '');
  if (empty($access_key)) {
    return '';
  }
  $options = array(
    'access_key' => $access_key,
    'output' => 'json',
    'fields' => 'main',
  );
  if ($get_real_hostname) {
    $options['hostname'] = 1;
  }
  $ipstack_url = url("http://api.ipstack.com/{$hostname}", array(
    'query' => $options,
    'absolute' => TRUE,
    'external' => TRUE,
  ));
  $req = drupal_http_request($ipstack_url);
  if ($req->code != 200) {
    return '';
  }
  $data = json_decode($req->data);
  $res = array(
    $data->country_name,
    $data->region_name,
    $data->city,
  );
  foreach ($res as $key => $item) {
    if (empty($item)) {
      unset($res[$key]);
    }
  }
  $location = implode(', ', $res);
  if ($get_real_hostname) {
    return array(
      'hostname' => $data->hostname,
      'location' => $location,
    );
  }
  else {
    return $location;
  }
}