3 // This file is part of Moodle - http://moodle.org/
5 // Moodle is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
10 // Moodle is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
19 * IP Lookup utility functions
22 * @subpackage iplookup
23 * @copyright 2010 Petr Skoda {@link http://skodak.org}
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 defined('MOODLE_INTERNAL') ||
die();
30 * Returns location information
34 function iplookup_find_location($ip) {
37 $info = array('city'=>null, 'country'=>null, 'longitude'=>null, 'latitude'=>null, 'error'=>null, 'note'=>'', 'title'=>array());
39 if (!empty($CFG->geoipfile
) and file_exists($CFG->geoipfile
)) {
40 require_once('Net/GeoIP.php');
42 $geoip = Net_GeoIP
::getInstance($CFG->geoipfile
, Net_GeoIP
::STANDARD
);
43 $location = $geoip->lookupLocation($ip);
46 if (empty($location)) {
47 $info['error'] = get_string('iplookupfailed', 'error', $ip);
50 if (!empty($location->city
)) {
51 $info['city'] = textlib
::convert($location->city
, 'iso-8859-1', 'utf-8');
52 $info['title'][] = $info['city'];
55 if (!empty($location->country_code
)) {
56 $countries = get_string_manager()->get_list_of_countries(true);
57 if (isset($countries[$location->country_code
])) {
58 // prefer our localized country names
59 $info['country'] = $countries[$location->country_code
];
61 $info['country'] = $location->country_name
;
63 $info['title'][] = $info['country'];
65 $info['longitude'] = $location->longitude
;
66 $info['latitude'] = $location->latitude
;
67 $info['note'] = get_string('iplookupmaxmindnote', 'admin');
72 require_once($CFG->libdir
.'/filelib.php');
74 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
75 if ($ipdata === false) {
76 $info['error'] = get_string('cannotnetgeo', 'error');
80 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
81 $info['error'] = get_string('iplookupfailed', 'error', $ip);
84 $info['latitude'] = (float)$matches[1];
85 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
86 $info['error'] = get_string('iplookupfailed', 'error', $ip);
89 $info['longitude'] = (float)$matches[1];
91 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
92 if (!empty($matches[1])) {
93 $info['city'] = s($matches[1]);
94 $info['title'][] = $info['city'];
98 if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) {
99 if (!empty($matches[1])) {
100 $countrycode = $matches[1];
101 $countries = get_string_manager()->get_list_of_countries(true);
102 if (isset($countries[$countrycode])) {
103 // prefer our localized country names
104 $info['country'] = $countries[$countrycode];
106 $info['country'] = $countrycode;
108 $info['title'][] = $info['country'];
111 $info['note'] = get_string('iplookupnetgeonote', 'admin');