| 1 |
<?php
|
| 2 |
/* $Id: geoapi_iplocator_maxmind.module,v 1.1 2008/03/25 10:14:42 thegreenman Exp $ */
|
| 3 |
|
| 4 |
/**
|
| 5 |
* This is a smallmodule that adds IP location defaults to the geonames_cck edit form.
|
| 6 |
*
|
| 7 |
* This uses Maxmind's commercial IP location webservice, an will require a key.
|
| 8 |
*
|
| 9 |
*/
|
| 10 |
|
| 11 |
/**
|
| 12 |
* implementation of hook_geoapi_iplocation.
|
| 13 |
* Implement this hook to add new iplocation modules
|
| 14 |
*
|
| 15 |
* @param string $ip Ip address to look up
|
| 16 |
*/
|
| 17 |
function geoapi_iplocator_maxmind_goapi_iplocation($ip) {
|
| 18 |
return maxmind_ip_lookup($ip);
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* implementation of hook menu
|
| 23 |
*
|
| 24 |
* @param unknown_type $may_cache
|
| 25 |
* @return unknown
|
| 26 |
*/
|
| 27 |
function geoapi_iplocator_maxmind_menu($may_cache) {
|
| 28 |
if ($may_cache) {
|
| 29 |
$items[] = array(
|
| 30 |
'path' => 'admin/settings/geonamescck/maxmind',
|
| 31 |
'title' => t('Maxmind'),
|
| 32 |
'description' => t('Maxmind config'),
|
| 33 |
'callback' => 'drupal_get_form',
|
| 34 |
'callback arguments' => 'geoapi_iplocator_maxmind_admin_form',
|
| 35 |
'access' => user_access('administer site configuration'),
|
| 36 |
'type' => MENU_LOCAL_TASK,
|
| 37 |
);
|
| 38 |
}
|
| 39 |
return $items;
|
| 40 |
}
|
| 41 |
|
| 42 |
/**
|
| 43 |
* admin form
|
| 44 |
*
|
| 45 |
*/
|
| 46 |
function geoapi_iplocator_maxmind_admin_form() {
|
| 47 |
$form['geoapi_maxmind_key'] = array(
|
| 48 |
'#type' => 'textfield',
|
| 49 |
'#title' => t('Maxmind Account Key'),
|
| 50 |
'#default_value' => variable_get('geoapi_maxmind_key', ''),
|
| 51 |
'#description' => t('Maxmind is a commercial service, so you will need to get an account key. Enter it here.'),
|
| 52 |
);
|
| 53 |
return system_settings_form($form);
|
| 54 |
}
|
| 55 |
|
| 56 |
|
| 57 |
/**
|
| 58 |
* Get location information for an ip address using maxmind's web services
|
| 59 |
*
|
| 60 |
* @param unknown_type $ip
|
| 61 |
*/
|
| 62 |
function maxmind_ip_lookup($ip) {
|
| 63 |
static $locations = array();
|
| 64 |
if (!$location = $locations[$ip]) {
|
| 65 |
// nothing in our cache, lets do a lookup
|
| 66 |
$maxmind_key = variable_get('geoapi_maxmind_key', '');
|
| 67 |
$url = 'http://geoip1.maxmind.com/b?l='. $maxmind_key .'&i='. $ip;
|
| 68 |
$result = drupal_http_request($url);
|
| 69 |
if ($result -> code == '200') {
|
| 70 |
// successful response
|
| 71 |
$location = _maxmind_parse_result($result -> data);
|
| 72 |
$locations[$ip] = $location;
|
| 73 |
}
|
| 74 |
}
|
| 75 |
return $location;
|
| 76 |
}
|
| 77 |
/**
|
| 78 |
* Parse a result from maxmind, and return a structured array
|
| 79 |
*
|
| 80 |
* @param string $data A http result from maxmind
|
| 81 |
*/
|
| 82 |
function _maxmind_parse_result($data) {
|
| 83 |
$location = array();
|
| 84 |
list($location['country'], $location['region'], $location['city']) = explode(',', $data);
|
| 85 |
if (trim($location['region']) == '(null)') {
|
| 86 |
$location['region'] = '';
|
| 87 |
}
|
| 88 |
if (trim($location['city']) == '(null)') {
|
| 89 |
$location['city'] = '';
|
| 90 |
}
|
| 91 |
return $location;
|
| 92 |
}
|