Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / iplookup / index.php
blob7e75bd80df342e4cd0c3ff112e9294002b0b15af
1 <?php // $Id$
2 ///////////////////////////////////////////////////////////////////////////
3 // //
4 // NOTICE OF COPYRIGHT //
5 // //
6 // Moodle - Modular Object-Oriented Dynamic Learning Environment //
7 // http://moodle.org //
8 // //
9 // Copyright (C) 2008 onwards Petr Skoda (skodak) //
10 // //
11 // This program is free software; you can redistribute it and/or modify //
12 // it under the terms of the GNU General Public License as published by //
13 // the Free Software Foundation; either version 2 of the License, or //
14 // (at your option) any later version. //
15 // //
16 // This program is distributed in the hope that it will be useful, //
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of //
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
19 // GNU General Public License for more details: //
20 // //
21 // http://www.gnu.org/copyleft/gpl.html //
22 // //
23 ///////////////////////////////////////////////////////////////////////////
25 require('../config.php');
26 require_once($CFG->libdir.'/filelib.php');
27 require_once($CFG->libdir.'/geoip/geoipcity.inc');
29 require_login();
31 $ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
32 $user = optional_param('user', $USER->id, PARAM_INT);
34 if (isset($CFG->iplookup)) {
35 //clean up of old settings
36 set_config('iplookup', NULL);
39 $info = array($ip);
40 $note = array();
42 if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
43 print_error('invalidipformat', 'error');
46 if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
47 print_error('invalidipformat', 'error');
50 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')) {
51 print_error('iplookupprivate', 'error');
54 if ($user) {
55 if ($user = get_record('user', 'id', $user, 'deleted', 0)) {
56 $info[] = fullname($user);
60 if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) {
61 $gi = geoip_open($CFG->geoipfile, GEOIP_STANDARD);
62 $location = geoip_record_by_addr($gi, $ip);
63 geoip_close($gi);
65 if (empty($location)) {
66 print_error('iplookupfailed', 'error', '', $ip);
68 if (!empty($location->city)) {
69 $info[] = $location->city;
72 if (!empty($location->country_code)) {
73 $countries = get_list_of_countries();
74 if (isset($countries[$location->country_code])) {
75 // prefer our localized country names
76 $info[] = $countries[$location->country_code];
77 } else {
78 $info[] = $location->country_name;
81 $longitude = $location->longitude;
82 $latitude = $location->latitude;
83 $note[] = get_string('iplookupmaxmindnote', 'admin');
85 } else {
86 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
87 if ($ipdata === false) {
88 error('Can not connect to NetGeo server at http://netgeo.caida.org, please check proxy settings or better install MaxMind GeoLite City data file.');
90 $matches = null;
91 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
92 print_error('iplookupfailed', 'error', '', $ip);
94 $latitude = (float)$matches[1];
95 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
96 print_error('iplookupfailed', 'error', '', $ip);
98 $longitude = (float)$matches[1];
100 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
101 if (!empty($matches[1])) {
102 $info[] = s($matches[1]);
106 if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) {
107 if (!empty($matches[1])) {
108 $countrycode = $matches[1];
109 $countries = get_list_of_countries();
110 if (isset($countries[$countrycode])) {
111 // prefer our localized country names
112 $info[] = $countries[$countrycode];
113 } else {
114 $info[] = $countrycode;
118 $note[] = get_string('iplookupnetgeonote', 'admin');
123 if (empty($CFG->googlemapkey)) {
124 $info = implode(' - ', $info);
125 $note = implode('<br />', $note);
127 $imgwidth = 620;
128 $imgheight = 310;
129 $dotwidth = 18;
130 $dotheight = 30;
132 $dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
133 $dy = round((($latitude + 90) * ($imgheight / 180)));
135 print_header(get_string('iplookup', 'admin').': '.$info, $info);
137 echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">';
138 echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />';
139 echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />';
140 echo '</div>';
141 echo '<div id="note">'.$note.'</div>';
142 print_footer('empty');
144 } else {
145 $info = implode(' - ', $info);
146 $note = implode('<br />', $note);
148 $meta = '
149 <script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='.$CFG->googlemapkey.'" type="text/javascript"></script>
150 <script type="text/javascript">
152 //<![CDATA[
154 function load() {
155 if (GBrowserIsCompatible()) {
156 var map = new GMap2(document.getElementById("map"));
157 map.addControl(new GSmallMapControl());
158 map.addControl(new GMapTypeControl());
159 var point = new GLatLng('.$latitude.', '.$longitude.');
160 map.setCenter(point, 4);
161 map.addOverlay(new GMarker(point));
162 map.setMapType(G_HYBRID_MAP);
166 //]]>
167 </script>
170 print_header(get_string('iplookup', 'admin').': '.$info, $info, '', '', $meta, false, '&nbsp;', '', false, 'onload="load()" onunload="GUnload()"');
172 echo '<div id="map" style="width: 650px; height: 360px"></div>';
173 echo '<div id="note">'.$note.'</div>';
174 print_footer('empty');