Skip to content

Commit

Permalink
MDL-32793 use geoLookup for IP address lookup by default
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed May 8, 2012
1 parent e16e230 commit 8c6136a
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 35 deletions.
54 changes: 21 additions & 33 deletions iplookup/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,46 +71,34 @@ function iplookup_find_location($ip) {
} else {
require_once($CFG->libdir.'/filelib.php');

$ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
if ($ipdata === false) {
$info['error'] = get_string('cannotnetgeo', 'error');
return $info;
$ipdata = download_file_content('http://www.geoplugin.net/json.gp?ip='.$ip);
if ($ipdata) {
$ipdata = preg_replace('/^geoPlugin\((.*)\)\s*$/s', '$1', $ipdata);
$ipdata = json_decode($ipdata, true);
}
$matches = null;
if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
$info['error'] = get_string('iplookupfailed', 'error', $ip);
if (!is_array($ipdata)) {
$info['error'] = get_string('cannotgeoplugin', 'error');
return $info;
}
$info['latitude'] = (float)$matches[1];
if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
$info['error'] = get_string('iplookupfailed', 'error', $ip);
return $info;
$info['latitude'] = (float)$ipdata['geoplugin_latitude'];
$info['longitude'] = (float)$ipdata['geoplugin_longitude'];
$info['city'] = s($ipdata['geoplugin_city']);

$countrycode = $ipdata['geoplugin_countryCode'];
$countries = get_string_manager()->get_list_of_countries(true);
if (isset($countries[$countrycode])) {
// prefer our localized country names
$info['country'] = $countries[$countrycode];
} else {
$info['country'] = s($ipdata['geoplugin_countryName']);
}
$info['longitude'] = (float)$matches[1];

if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
if (!empty($matches[1])) {
$info['city'] = s($matches[1]);
$info['title'][] = $info['city'];
}
}
$info['note'] = get_string('iplookupgeoplugin', 'admin');

if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) {
if (!empty($matches[1])) {
$countrycode = $matches[1];
$countries = get_string_manager()->get_list_of_countries(true);
if (isset($countries[$countrycode])) {
// prefer our localized country names
$info['country'] = $countries[$countrycode];
} else {
$info['country'] = $countrycode;
}
$info['title'][] = $info['country'];
}
}
$info['note'] = get_string('iplookupnetgeonote', 'admin');
$info['title'][] = $info['country'];
$info['title'][] = $info['city'];

return $info;
}

}
}
2 changes: 1 addition & 1 deletion lang/en/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -587,11 +587,11 @@
$string['ipblocker'] = 'IP blocker';
$string['ipblockersyntax'] = 'Put every entry on one line. Valid entries are either full IP address (such as <b>192.168.10.1</b>) which matches a single host; or partial address (such as <b>192.168.</b>) which matches any address starting with those numbers; or CIDR notation (such as <b>231.54.211.0/20</b>); or a range of IP addresses (such as <b>231.3.56.10-20</b>) where the range applies to the last part of the address. Text domain names (like \'example.com\') are not supported. Blank lines are ignored.';
$string['iplookup'] = 'IP address lookup';
$string['iplookupgeoplugin'] = '<a href="http://www.geoplugin.com">geoPlugin</a> service is currently being used to look up geographical information. For more accurate results we recommend installing a local copy of the MaxMind GeoLite database.';
$string['iplookupinfo'] = 'By default Moodle uses the free online NetGeo (The Internet Geographic Database) server to lookup location of IP addresses, unfortunately this database is not maintained anymore and may return <em>wildly incorrect</em> data.
It is recommended to install local copy of free GeoLite City database from MaxMind.<br />
IP address location is displayed on simple map or using Google Maps. Please note that you need to have a Google account and apply for free Google Maps API key to enable interactive maps.';
$string['iplookupmaxmindnote'] = 'This product includes GeoLite data created by MaxMind, available from <a href="http://www.maxmind.com/">http://www.maxmind.com/</a>.';
$string['iplookupnetgeonote'] = 'The NetGeo server is currently being used to look up geographical information. For more accurate results we recommend installing a local copy of the MaxMind GeoLite database.';
$string['keeptagnamecase'] = 'Keep tag name casing';
$string['lang'] = 'Default language';
$string['langcache'] = 'Cache language menu';
Expand Down
2 changes: 1 addition & 1 deletion lang/en/error.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
$string['cannotfindlang'] = 'Cannot find "{$a}" language pack!';
$string['cannotfindteacher'] = 'Cannot find teacher';
$string['cannotfinduser'] = 'Cannot find user named "{$a}"';
$string['cannotgeoplugin'] = 'Cannot connect to geoPlugin server at http://www.geoplugin.com, please check proxy settings or better install MaxMind GeoLite City data file';
$string['cannotgetblock'] = 'Could not retrieve blocks from the database';
$string['cannotgetcats'] = 'Cannot get category record';
$string['cannotgetdata'] = 'Cannot get data';
Expand All @@ -104,7 +105,6 @@
$string['cannotmodulename'] = 'Cannot get the module name in build navigation';
$string['cannotmoduletype'] = 'Cannot get the module type in build navigation';
$string['cannotmoverolewithid'] = 'Cannot move role with ID {$a}';
$string['cannotnetgeo'] = 'Cannot connect to NetGeo server at http://netgeo.caida.org, please check proxy settings or better install MaxMind GeoLite City data file';
$string['cannotopencsv'] = 'Cannot open CSV file';
$string['cannotopenfile'] = 'Cannot open file ({$a})';
$string['cannotopenforwrit'] = 'Cannot open for writing: {$a}';
Expand Down

0 comments on commit 8c6136a

Please sign in to comment.