"MDL-14129,fix print_error"
[moodle.git] / iplookup / index.php
CommitLineData
f4fb66d7 1<?php // $Id$
7c09710c 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///////////////////////////////////////////////////////////////////////////
f4fb66d7 24
7c09710c 25require('../config.php');
26require_once($CFG->libdir.'/filelib.php');
27require_once($CFG->libdir.'/geoip/geoipcity.inc');
f4fb66d7 28
7c09710c 29require_login();
f4fb66d7 30
7c09710c 31$ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
32$user = optional_param('user', $USER->id, PARAM_INT);
f4fb66d7 33
7c09710c 34if (isset($CFG->iplookup)) {
35 //clean up of old settings
36 set_config('iplookup', NULL);
37}
38
39$info = array($ip);
40$note = array();
41
42if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
43 print_error('invalidipformat', 'error');
44}
45
46if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
47 print_error('invalidipformat', 'error');
48}
49
50if ($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');
52}
53
54if ($user) {
ab122f46 55 if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
7c09710c 56 $info[] = fullname($user);
57 }
58}
59
60if (!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);
64
65 if (empty($location)) {
66 print_error('iplookupfailed', 'error', '', $ip);
f4fb66d7 67 }
7c09710c 68 if (!empty($location->city)) {
69 $info[] = $location->city;
70 }
71
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;
79 }
80 }
81 $longitude = $location->longitude;
82 $latitude = $location->latitude;
83 $note[] = get_string('iplookupmaxmindnote', 'admin');
84
85} else {
86 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
87 if ($ipdata === false) {
d7bdb1c2 88 print_error('cannotnetgeo');
7c09710c 89 }
90 $matches = null;
91 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
92 print_error('iplookupfailed', 'error', '', $ip);
93 }
94 $latitude = (float)$matches[1];
95 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
96 print_error('iplookupfailed', 'error', '', $ip);
97 }
98 $longitude = (float)$matches[1];
99
100 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
101 if (!empty($matches[1])) {
102 $info[] = s($matches[1]);
103 }
104 }
105
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;
115 }
116 }
117 }
118 $note[] = get_string('iplookupnetgeonote', 'admin');
119}
120
121
122
123if (empty($CFG->googlemapkey)) {
124 $info = implode(' - ', $info);
125 $note = implode('<br />', $note);
126
127 $imgwidth = 620;
128 $imgheight = 310;
129 $dotwidth = 18;
130 $dotheight = 30;
131
132 $dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
133 $dy = round((($latitude + 90) * ($imgheight / 180)));
134
135 print_header(get_string('iplookup', 'admin').': '.$info, $info);
136
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');
143
144} else {
145 $info = implode(' - ', $info);
146 $note = implode('<br />', $note);
147
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">
151
152//<![CDATA[
153
154function 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);
163 }
164}
165
166//]]>
167</script>
168';
f4fb66d7 169
7c09710c 170 print_header(get_string('iplookup', 'admin').': '.$info, $info, '', '', $meta, false, '&nbsp;', '', false, 'onload="load()" onunload="GUnload()"');
f4fb66d7 171
7c09710c 172 echo '<div id="map" style="width: 650px; height: 360px"></div>';
173 echo '<div id="note">'.$note.'</div>';
174 print_footer('empty');
175}
f4fb66d7 176
177?>