geoip.module in GeoIP API 5
Same filename and directory in other branches
API for using the MaxMind GeoLite Country database
File
geoip.moduleView source
<?php
/**
* @file
* API for using the MaxMind GeoLite Country database
*/
/**
* Implementation of hook_menu().
*/
function geoip_menu($may_cache) {
$items = array();
if ($may_cache) {
$items[] = array(
'path' => 'admin/settings/geoip',
'title' => t('GeoIP'),
'callback' => 'drupal_get_form',
'callback arguments' => array(
'geoip_admin_settings',
),
'access' => user_access('administer site configuration'),
);
}
return $items;
}
/**
* Implementation of hook_requirements().
*/
function geoip_requirements($phase) {
$requirements = array();
// Ensure translations don't break at install time
$t = get_t();
// Test for a valid GeoIP database
$requirements['geoip_database'] = array(
'title' => $t('GeoIP Database'),
);
$file = variable_get('geoip_data_file', '');
if (!$file || !file_exists($file)) {
$requirements['geoip_database']['value'] = l('Missing', 'admin/settings/geoip');
$requirements['geoip_database']['description'] = $t('The GeoIP database file is missing or not configured. Download the latest file at <a href="@url">MaxMind.com</a>.', array(
'@url' => 'http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz',
));
$requirements['geoip_database']['severity'] = $phase == 'runtime' ? REQUIREMENT_WARNING : REQUIREMENT_ERROR;
}
else {
$mtime = filemtime($file);
if ($mtime < strtotime('1 months ago')) {
$requirements['geoip_database']['value'] = $t('Out of date');
$requirements['geoip_database']['description'] = $t('The GeoIP database file is more than a month old. Download the latest file at <a href="@url">MaxMind.com</a>.', array(
'@url' => 'http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz',
));
$requirements['geoip_database']['severity'] = REQUIREMENT_WARNING;
}
else {
$requirements['geoip_database']['value'] = $t('Installed');
}
}
return $requirements;
}
/**
* /admin/settings/geoip
*/
function geoip_admin_settings() {
$form['geoip_data_file'] = array(
'#type' => 'textfield',
'#title' => t('GeoIP data file location'),
'#description' => t('The path to the GeoIP.dat file. You can download a free GeoIP Country data file from !link.', array(
'!link' => l('MaxMind', 'http://www.maxmind.com/app/geolitecountry'),
)),
'#default_value' => variable_get('geoip_data_file', drupal_get_path('module', 'geoip') . '/GeoIP.dat'),
'#after_build' => array(
'geoip_data_file_validate',
),
);
$form['geoip_debug'] = array(
'#type' => 'checkbox',
'#title' => t('GeoIP debug mode'),
'#description' => t('With this setting enabled, an IP may be passed in through the query string using the GEOIP_DEBUG parameter. This should not be used on production sites.'),
'#default_value' => variable_get('geoip_debug', FALSE),
);
return system_settings_form($form);
}
/**
* Validate that the geoip_data_file exists
*/
function geoip_data_file_validate($form_element) {
$file = $form_element['#value'];
if (!file_exists($file)) {
form_error($form_element, t('The GeoIP data file could not be located at the specified location.'));
}
else {
$mtime = filemtime($file);
if ($mtime < strtotime('1 months ago')) {
drupal_set_message(t('The GeoIP database file is more than a month old. Download the latest file at <a href="@url">MaxMind.com</a>.', array(
'@url' => 'http://www.maxmind.com/download/geoip/database/GeoIP.dat.gz',
)), 'warning');
}
}
return $form_element;
}
/**
* Fetch the geoip info for a given ip address
*/
function _geoip_info($ip) {
include_once drupal_get_path('module', 'geoip') . '/lib/geoip.inc';
$gi = geoip_instance();
if (!$gi) {
return FALSE;
}
$info = array();
$info['ip'] = $ip;
$info['country_code'] = geoip_country_code_by_addr($gi, $ip);
$info['country_name'] = geoip_country_name_by_addr($gi, $ip);
geoip_close($gi);
return $info;
}
/**
* Helper function to get the current ip address
*/
function geoip_ip_address() {
if (variable_get('geoip_debug', FALSE) && $_GET['geoip_debug']) {
$ip = $_GET['geoip_debug'];
drupal_set_message('Using IP: ' . $ip . '.');
}
else {
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
/**
* API function to return the country code for a given IP. Defaults to using the
* current user's IP if not specified.
*/
function geoip_country_code($ip = NULL) {
$ip = $ip ? $ip : geoip_ip_address();
$gi = geoip_instance();
if (!$gi) {
return FALSE;
}
$cc = geoip_country_code_by_addr($gi, $ip);
geoip_close($gi);
return $cc;
}
/**
* Singleton wrapper around geoip_open().
*/
function geoip_instance() {
$data_file = variable_get('geoip_data_file', drupal_get_path('module', 'geoip') . '/GeoIP.dat');
if (!$data_file || !file_exists($data_file)) {
drupal_set_message(t('Please <a href="!url">configure</a> the GeoIP data file location.', array(
'!url' => url('admin/settings/geoip'),
)));
return FALSE;
}
include_once drupal_get_path('module', 'geoip') . '/lib/geoip.inc';
$instance = geoip_open($data_file, GEOIP_STANDARD);
return $instance;
}
Functions
Name | Description |
---|---|
geoip_admin_settings | /admin/settings/geoip |
geoip_country_code | API function to return the country code for a given IP. Defaults to using the current user's IP if not specified. |
geoip_data_file_validate | Validate that the geoip_data_file exists |
geoip_instance | Singleton wrapper around geoip_open(). |
geoip_ip_address | Helper function to get the current ip address |
geoip_menu | Implementation of hook_menu(). |
geoip_requirements | Implementation of hook_requirements(). |
_geoip_info | Fetch the geoip info for a given ip address |