fixed misspelled CSS, MDL-23030
[moodle.git] / iplookup / index.php
CommitLineData
48fb4cd6 1<?php
2
3// This file is part of Moodle - http://moodle.org/
4//
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.
9//
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.
14//
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/>.
17
18/**
75c7b84f 19 * Displays IP address on map
48fb4cd6 20 *
8061887a 21 * @package core
48fb4cd6 22 * @subpackage iplookup
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 */
f4fb66d7 26
7c09710c 27require('../config.php');
28require_once($CFG->libdir.'/filelib.php');
bbb13f06 29require_once('Net/GeoIP.php');
f4fb66d7 30
7c09710c 31require_login();
f4fb66d7 32
7c09710c 33$ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
34$user = optional_param('user', $USER->id, PARAM_INT);
f4fb66d7 35
7c09710c 36if (isset($CFG->iplookup)) {
37 //clean up of old settings
38 set_config('iplookup', NULL);
39}
40
a6855934 41$PAGE->set_url('/iplookup/index.php', array('id'=>$ip, 'user'=>$user));
78946b9b 42$PAGE->set_pagelayout('popup');
8fb1c9af 43$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
48fb4cd6 44
7c09710c 45$info = array($ip);
46$note = array();
47
48if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
49 print_error('invalidipformat', 'error');
50}
51
52if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
53 print_error('invalidipformat', 'error');
54}
55
56if ($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')) {
57 print_error('iplookupprivate', 'error');
58}
59
60if ($user) {
ab122f46 61 if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
7c09710c 62 $info[] = fullname($user);
63 }
64}
65
8061887a
PS
66$textlib = textlib_get_instance();
67
7c09710c 68if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) {
bbb13f06
PS
69 $geoip = Net_GeoIP::getInstance($CFG->geoipfile, Net_GeoIP::STANDARD);
70 $location = $geoip->lookupLocation($ip);
71 $geoip->close();
7c09710c 72
73 if (empty($location)) {
74 print_error('iplookupfailed', 'error', '', $ip);
f4fb66d7 75 }
7c09710c 76 if (!empty($location->city)) {
8061887a 77 $info[] = $textlib->convert($location->city, 'iso-8859-1', 'utf-8');
7c09710c 78 }
79
80 if (!empty($location->country_code)) {
0aa759b0 81 $countries = get_string_manager()->get_list_of_countries(true);
7c09710c 82 if (isset($countries[$location->country_code])) {
83 // prefer our localized country names
84 $info[] = $countries[$location->country_code];
85 } else {
86 $info[] = $location->country_name;
87 }
88 }
89 $longitude = $location->longitude;
90 $latitude = $location->latitude;
91 $note[] = get_string('iplookupmaxmindnote', 'admin');
92
93} else {
94 $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip);
95 if ($ipdata === false) {
d7bdb1c2 96 print_error('cannotnetgeo');
7c09710c 97 }
98 $matches = null;
99 if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
100 print_error('iplookupfailed', 'error', '', $ip);
101 }
102 $latitude = (float)$matches[1];
103 if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) {
104 print_error('iplookupfailed', 'error', '', $ip);
105 }
106 $longitude = (float)$matches[1];
107
108 if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) {
109 if (!empty($matches[1])) {
110 $info[] = s($matches[1]);
111 }
112 }
113
114 if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) {
115 if (!empty($matches[1])) {
116 $countrycode = $matches[1];
0aa759b0 117 $countries = get_string_manager()->get_list_of_countries(true);
7c09710c 118 if (isset($countries[$countrycode])) {
119 // prefer our localized country names
120 $info[] = $countries[$countrycode];
121 } else {
122 $info[] = $countrycode;
123 }
124 }
125 }
126 $note[] = get_string('iplookupnetgeonote', 'admin');
127}
128
129
130
131if (empty($CFG->googlemapkey)) {
132 $info = implode(' - ', $info);
133 $note = implode('<br />', $note);
134
135 $imgwidth = 620;
136 $imgheight = 310;
137 $dotwidth = 18;
138 $dotheight = 30;
139
140 $dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
141 $dy = round((($latitude + 90) * ($imgheight / 180)));
142
4aa31a61 143 $PAGE->set_title(get_string('iplookup', 'admin').': '.$info);
144 $PAGE->set_heading($info);
145 echo $OUTPUT->header();
7c09710c 146
147 echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">';
148 echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />';
149 echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />';
150 echo '</div>';
151 echo '<div id="note">'.$note.'</div>';
4aa31a61 152
153 echo $OUTPUT->footer();
7c09710c 154
155} else {
156 $info = implode(' - ', $info);
157 $note = implode('<br />', $note);
158
789ff38c
PS
159 $PAGE->requires->js(new moodle_url("http://maps.google.com/maps?file=api&v=2&key=$CFG->googlemapkey"));
160 $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js');
161 $PAGE->requires->js_init_call('M.core_iplookup.init', array($latitude, $longitude), true, $module);
f4fb66d7 162
4aa31a61 163 $PAGE->set_title(get_string('iplookup', 'admin').': '.$info);
164 $PAGE->set_heading($info);
165 echo $OUTPUT->header();
f4fb66d7 166
7c09710c 167 echo '<div id="map" style="width: 650px; height: 360px"></div>';
168 echo '<div id="note">'.$note.'</div>';
48fb4cd6 169
48fb4cd6 170 echo $OUTPUT->footer();
7c09710c 171}
f4fb66d7 172