diff --git a/iplookup/index.php b/iplookup/index.php index 0b7302edc1e4a..afb5383919e81 100644 --- a/iplookup/index.php +++ b/iplookup/index.php @@ -16,7 +16,9 @@ // along with Moodle. If not, see . /** - * Displays IP address on map + * Displays IP address on map. + * + * This script is not compatible with IPv6. * * @package core * @subpackage iplookup @@ -25,13 +27,12 @@ */ require('../config.php'); -require_once($CFG->libdir.'/filelib.php'); -require_once('Net/GeoIP.php'); +require_once('lib.php'); require_login(); $ip = optional_param('ip', getremoteaddr(), PARAM_HOST); -$user = optional_param('user', $USER->id, PARAM_INT); +$user = optional_param('user', 0, PARAM_INT); if (isset($CFG->iplookup)) { //clean up of old settings @@ -57,116 +58,50 @@ print_error('iplookupprivate', 'error'); } -if ($user) { - if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) { - $info[] = fullname($user); - } -} - -$textlib = textlib_get_instance(); - -if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) { - $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD); - $location = $geoip->lookupLocation($ip); - $geoip->close(); - - if (empty($location)) { - print_error('iplookupfailed', 'error', '', $ip); - } - if (!empty($location->city)) { - $info[] = $textlib->convert($location->city, 'iso-8859-1', 'utf-8'); - } - - if (!empty($location->country_code)) { - $countries = get_string_manager()->get_list_of_countries(true); - if (isset($countries[$location->country_code])) { - // prefer our localized country names - $info[] = $countries[$location->country_code]; - } else { - $info[] = $location->country_name; - } - } - $longitude = $location->longitude; - $latitude = $location->latitude; - $note[] = get_string('iplookupmaxmindnote', 'admin'); - -} else { - $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip); - if ($ipdata === false) { - print_error('cannotnetgeo'); - } - $matches = null; - if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { - print_error('iplookupfailed', 'error', '', $ip); - } - $latitude = (float)$matches[1]; - if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { - print_error('iplookupfailed', 'error', '', $ip); - } - $longitude = (float)$matches[1]; +$info = iplookup_find_location($ip); - if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) { - if (!empty($matches[1])) { - $info[] = s($matches[1]); - } - } +if ($info['error']) { + // can not display + notice($info['error']); +} - 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[] = $countries[$countrycode]; - } else { - $info[] = $countrycode; - } +if ($user) { + if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) { + // note: better not show full names to everybody + if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_USER, $user->id))) { + array_unshift($info['title'], fullname($user)); } } - $note[] = get_string('iplookupnetgeonote', 'admin'); } +array_unshift($info['title'], $ip); - +$title = implode(' - ', $info['title']); +$PAGE->set_title(get_string('iplookup', 'admin').': '.$title); +$PAGE->set_heading($title); +echo $OUTPUT->header(); if (empty($CFG->googlemapkey)) { - $info = implode(' - ', $info); - $note = implode('
', $note); - $imgwidth = 620; $imgheight = 310; $dotwidth = 18; $dotheight = 30; - $dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2); - $dy = round((($latitude + 90) * ($imgheight / 180))); - - $PAGE->set_title(get_string('iplookup', 'admin').': '.$info); - $PAGE->set_heading($info); - echo $OUTPUT->header(); + $dx = round((($info['longitude'] + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2); + $dy = round((($info['latitude'] + 90) * ($imgheight / 180))); echo '
'; echo ''; echo ''; echo '
'; - echo '
'.$note.'
'; - - echo $OUTPUT->footer(); + echo '
'.$info['note'].'
'; } else { - $info = implode(' - ', $info); - $note = implode('
', $note); - $PAGE->requires->js(new moodle_url("http://maps.google.com/maps?file=api&v=2&key=$CFG->googlemapkey")); $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js'); - $PAGE->requires->js_init_call('M.core_iplookup.init', array($latitude, $longitude), true, $module); - - $PAGE->set_title(get_string('iplookup', 'admin').': '.$info); - $PAGE->set_heading($info); - echo $OUTPUT->header(); + $PAGE->requires->js_init_call('M.core_iplookup.init', array($info['latitude'], $info['longitude']), true, $module); echo '
'; - echo '
'.$note.'
'; - - echo $OUTPUT->footer(); + echo '
'.$info['note'].'
'; } +echo $OUTPUT->footer(); diff --git a/iplookup/lib.php b/iplookup/lib.php new file mode 100644 index 0000000000000..920f55551f0a0 --- /dev/null +++ b/iplookup/lib.php @@ -0,0 +1,118 @@ +. + +/** + * IP Lookup utility functions + * + * @package core + * @subpackage iplookup + * @copyright 2010 Petr Skoda {@link http://skodak.org} + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +defined('MOODLE_INTERNAL') || die(); + +/** + * Returns location information + * @param string $ip + * @return array + */ +function iplookup_find_location($ip) { + global $CFG; + + $info = array('city'=>null, 'country'=>null, 'longitude'=>null, 'latitude'=>null, 'error'=>null, 'note'=>'', 'title'=>array()); + + if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) { + require_once('Net/GeoIP.php'); + + $textlib = textlib_get_instance(); + + $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD); + $location = $geoip->lookupLocation($ip); + $geoip->close(); + + if (empty($location)) { + $info['error'] = get_string('iplookupfailed', 'error', $ip); + return $info; + } + if (!empty($location->city)) { + $info['city'] = $textlib->convert($location->city, 'iso-8859-1', 'utf-8'); + $info['title'][] = $info['city']; + } + + if (!empty($location->country_code)) { + $countries = get_string_manager()->get_list_of_countries(true); + if (isset($countries[$location->country_code])) { + // prefer our localized country names + $info['country'] = $countries[$location->country_code]; + } else { + $info['country'] = $location->country_name; + } + $info['title'][] = $info['country']; + } + $info['longitude'] = $location->longitude; + $info['latitude'] = $location->latitude; + $info['note'] = get_string('iplookupmaxmindnote', 'admin'); + + return $info; + + } 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; + } + $matches = null; + if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { + $info['error'] = get_string('iplookupfailed', 'error', $ip); + 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['longitude'] = (float)$matches[1]; + + if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) { + if (!empty($matches[1])) { + $info['city'] = s($matches[1]); + $info['title'][] = $info['city']; + } + } + + 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'); + + return $info; + } + +} \ No newline at end of file