Skip to content

Commit

Permalink
MDL-12796 iplookup rewrite
Browse files Browse the repository at this point in the history
  • Loading branch information
skodak committed Jan 2, 2008
1 parent 25c3f91 commit 7c09710
Show file tree
Hide file tree
Showing 36 changed files with 5,129 additions and 2,044 deletions.
10 changes: 3 additions & 7 deletions admin/settings/location.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,9 @@
$options[0] = get_string('choose') .'...';
$temp->add(new admin_setting_configselect('country', get_string('country', 'admin'), get_string('configcountry', 'admin'), 0, $options));

$iplookups = array();
if ($plugins = get_list_of_plugins('iplookup')) {
foreach ($plugins as $plugin) {
$iplookups[$plugin] = $plugin;
}
}
$temp->add(new admin_setting_configselect('iplookup', get_string('iplookup', 'admin'), get_string('configiplookup', 'admin'), 'hostip', $iplookups));
$temp->add(new admin_setting_heading('iplookup', get_string('iplookup', 'admin'), get_string('iplookupinfo', 'admin')));
$temp->add(new admin_setting_configfile('geoipfile', get_string('geoipfile', 'admin'), get_string('configgeoipfile', 'admin', $CFG->dataroot.'/geoip/'), $CFG->dataroot.'/geoip/GeoLiteCity.dat'));
$temp->add(new admin_setting_configtext('googlemapkey', get_string('googlemapkey', 'admin'), get_string('configgooglemapkey', 'admin', $CFG->wwwroot), ''));

$ADMIN->add('location', $temp);

Expand Down
4 changes: 2 additions & 2 deletions course/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,14 @@ function print_log($course, $user=0, $date=0, $order="l.time ASC", $page=0, $per
echo "<td class=\"cell c1\" align=\"right\">".userdate($log->time, '%a').
' '.userdate($log->time, $strftimedatetime)."</td>\n";
echo "<td class=\"cell c2\">\n";
link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 400, 700);
link_to_popup_window("/iplookup/index.php?ip=$log->ip&amp;user=$log->userid", 'iplookup',$log->ip, 440, 700);
echo "</td>\n";
$fullname = fullname($log, has_capability('moodle/site:viewfullnames', get_context_instance(CONTEXT_COURSE, $course->id)));
echo "<td class=\"cell c3\">\n";
echo " <a href=\"$CFG->wwwroot/user/view.php?id={$log->userid}&amp;course={$log->course}\">$fullname</a>\n";
echo "</td>\n";
echo "<td class=\"cell c4\">\n";
link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 400, 600);
link_to_popup_window( make_log_url($log->module,$log->url), 'fromloglive',"$log->module $log->action", 440, 700);
echo "</td>\n";;
echo "<td class=\"cell c5\">{$log->info}</td>\n";
echo "</tr>\n";
Expand Down
31 changes: 19 additions & 12 deletions iplookup/README.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
iplookup
--------
iplookup info
-------------

These plugins will show you something about an IP address.
1/ old plugins are not supported anymore

Moodle scripts call the index.php in this directory like this:

/iplookup/index.php?ip=222.222.222.222&user=1
2/ general information in admin settings "Site Administration/Location/Location settings"

Both parameters are optional, they default to the current user.
3/ technical info:

index.php loads the lib.php from a specified subdirectory
to actually display some sort of map or description.
xplanet commadline
xplanet -projection rectangular -latitude 0.00 -longitude 0.00 -num_times 1 -geometry 620x310 -output earth.jpeg -quality 90 -config config.txt

The current plugin is selected using $CFG->iplookup.
config.txt
[earth]
shade=100

Cheers,
Martin
original Earth map from:
http://www.radcyberzine.com/xglobe/

marker.gif
custom made in Inkscape

Petr Skoda (skodak), January 2008

$Id$
Binary file added iplookup/earth.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 0 additions & 22 deletions iplookup/hostip/lib.php

This file was deleted.

176 changes: 167 additions & 9 deletions iplookup/index.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,177 @@
<?php // $Id$
// Do an IP lookup of a user, using selected plugin
///////////////////////////////////////////////////////////////////////////
// //
// NOTICE OF COPYRIGHT //
// //
// Moodle - Modular Object-Oriented Dynamic Learning Environment //
// http://moodle.org //
// //
// Copyright (C) 2008 onwards Petr Skoda (skodak) //
// //
// This program is free software; you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation; either version 2 of the License, or //
// (at your option) any later version. //
// //
// This program is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY; without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details: //
// //
// http://www.gnu.org/copyleft/gpl.html //
// //
///////////////////////////////////////////////////////////////////////////

require('../config.php');
require('../config.php');
require_once($CFG->libdir.'/filelib.php');
require_once($CFG->libdir.'/geoip/geoipcity.inc');

require_login();
require_login();

$ip = optional_param('ip', getremoteaddr());
$user = optional_param('user', $USER->id);
$ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
$user = optional_param('user', $USER->id, PARAM_INT);

if (empty($CFG->iplookup)) {
set_config('iplookup', 'hostip');
if (isset($CFG->iplookup)) {
//clean up of old settings
set_config('iplookup', NULL);
}

$info = array($ip);
$note = array();

if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
print_error('invalidipformat', 'error');
}

if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
print_error('invalidipformat', 'error');
}

if ($match[1] == '127' or $match[1] == '10' or ($match[1] == '172' and $match[2] >= '16' and $match[2] <= '31') or ($match[1] == '192' and $match[2] == '168')) {
print_error('iplookupprivate', 'error');
}

if ($user) {
if ($user = get_record('user', 'id', $user, 'deleted', 0)) {
$info[] = fullname($user);
}
}

if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) {
$gi = geoip_open($CFG->geoipfile, GEOIP_STANDARD);
$location = geoip_record_by_addr($gi, $ip);
geoip_close($gi);

if (empty($location)) {
print_error('iplookupfailed', 'error', '', $ip);
}
if (!empty($location->city)) {
$info[] = $location->city;
}

if (!empty($location->country_code)) {
$countries = get_list_of_countries();
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) {
error('Can not connect to NetGeo server at http://netgeo.caida.org, please check proxy settings or better install MaxMind GeoLite City data file.');
}
$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];

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

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



if (empty($CFG->googlemapkey)) {
$info = implode(' - ', $info);
$note = implode('<br />', $note);

$imgwidth = 620;
$imgheight = 310;
$dotwidth = 18;
$dotheight = 30;

$dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
$dy = round((($latitude + 90) * ($imgheight / 180)));

print_header(get_string('iplookup', 'admin').': '.$info, $info);

echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">';
echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />';
echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />';
echo '</div>';
echo '<div id="note">'.$note.'</div>';
print_footer('empty');

} else {
$info = implode(' - ', $info);
$note = implode('<br />', $note);

$meta = '
<script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$CFG->googlemapkey.'" type="text/javascript"></script>
<script type="text/javascript">
//<![CDATA[
function load() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
var point = new GLatLng('.$latitude.', '.$longitude.');
map.setCenter(point, 4);
map.addOverlay(new GMarker(point));
map.setMapType(G_HYBRID_MAP);
}
}
//]]>
</script>
';

require("$CFG->dirroot/iplookup/$CFG->iplookup/lib.php");
print_header(get_string('iplookup', 'admin').': '.$info, $info, '', '', $meta, false, '&nbsp;', '', false, 'onload="load()" onunload="GUnload()"');

iplookup_display($ip, $user);
echo '<div id="map" style="width: 650px; height: 360px"></div>';
echo '<div id="note">'.$note.'</div>';
print_footer('empty');
}

?>
Loading

0 comments on commit 7c09710

Please sign in to comment.