diff --git a/iplookup/lib.php b/iplookup/lib.php index 44a2c80c52e7d..fea3ed22b952f 100644 --- a/iplookup/lib.php +++ b/iplookup/lib.php @@ -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; } -} \ No newline at end of file +} diff --git a/lang/en/admin.php b/lang/en/admin.php index 6af4828d2beeb..ef72e0afb9877 100644 --- a/lang/en/admin.php +++ b/lang/en/admin.php @@ -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 192.168.10.1) which matches a single host; or partial address (such as 192.168.) which matches any address starting with those numbers; or CIDR notation (such as 231.54.211.0/20); or a range of IP addresses (such as 231.3.56.10-20) 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'] = 'geoPlugin 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 wildly incorrect data. It is recommended to install local copy of free GeoLite City database from MaxMind.
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 http://www.maxmind.com/.'; -$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'; diff --git a/lang/en/error.php b/lang/en/error.php index 0d45834b6e2c3..ee6eb507e91c5 100644 --- a/lang/en/error.php +++ b/lang/en/error.php @@ -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'; @@ -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}';