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 | * |
21 | * @package moodlecore |
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 |
27 | require('../config.php'); |
28 | require_once($CFG->libdir.'/filelib.php'); |
29 | require_once($CFG->libdir.'/geoip/geoipcity.inc'); |
f4fb66d7 |
30 | |
7c09710c |
31 | require_login(); |
f4fb66d7 |
32 | |
7c09710c |
33 | $ip = optional_param('ip', getremoteaddr(), PARAM_HOST); |
34 | $user = optional_param('user', $USER->id, PARAM_INT); |
f4fb66d7 |
35 | |
7c09710c |
36 | if (isset($CFG->iplookup)) { |
37 | //clean up of old settings |
38 | set_config('iplookup', NULL); |
39 | } |
40 | |
48fb4cd6 |
41 | $PAGE->set_url('iplookup/index.php', array('id'=>$ip, 'user'=>$user)); |
42 | $PAGE->set_generaltype('popup'); |
43 | |
7c09710c |
44 | $info = array($ip); |
45 | $note = array(); |
46 | |
47 | if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) { |
48 | print_error('invalidipformat', 'error'); |
49 | } |
50 | |
51 | if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) { |
52 | print_error('invalidipformat', 'error'); |
53 | } |
54 | |
55 | 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')) { |
56 | print_error('iplookupprivate', 'error'); |
57 | } |
58 | |
59 | if ($user) { |
ab122f46 |
60 | if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) { |
7c09710c |
61 | $info[] = fullname($user); |
62 | } |
63 | } |
64 | |
65 | if (!empty($CFG->geoipfile) and file_exists($CFG->geoipfile)) { |
66 | $gi = geoip_open($CFG->geoipfile, GEOIP_STANDARD); |
67 | $location = geoip_record_by_addr($gi, $ip); |
68 | geoip_close($gi); |
69 | |
70 | if (empty($location)) { |
71 | print_error('iplookupfailed', 'error', '', $ip); |
f4fb66d7 |
72 | } |
7c09710c |
73 | if (!empty($location->city)) { |
74 | $info[] = $location->city; |
75 | } |
76 | |
77 | if (!empty($location->country_code)) { |
78 | $countries = get_list_of_countries(); |
79 | if (isset($countries[$location->country_code])) { |
80 | // prefer our localized country names |
81 | $info[] = $countries[$location->country_code]; |
82 | } else { |
83 | $info[] = $location->country_name; |
84 | } |
85 | } |
86 | $longitude = $location->longitude; |
87 | $latitude = $location->latitude; |
88 | $note[] = get_string('iplookupmaxmindnote', 'admin'); |
89 | |
90 | } else { |
91 | $ipdata = download_file_content('http://netgeo.caida.org/perl/netgeo.cgi?target='.$ip); |
92 | if ($ipdata === false) { |
d7bdb1c2 |
93 | print_error('cannotnetgeo'); |
7c09710c |
94 | } |
95 | $matches = null; |
96 | if (!preg_match('/LAT:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { |
97 | print_error('iplookupfailed', 'error', '', $ip); |
98 | } |
99 | $latitude = (float)$matches[1]; |
100 | if (!preg_match('/LONG:\s*(-?\d+\.\d+)/s', $ipdata, $matches)) { |
101 | print_error('iplookupfailed', 'error', '', $ip); |
102 | } |
103 | $longitude = (float)$matches[1]; |
104 | |
105 | if (preg_match('/CITY:\s*([^<]*)/', $ipdata, $matches)) { |
106 | if (!empty($matches[1])) { |
107 | $info[] = s($matches[1]); |
108 | } |
109 | } |
110 | |
111 | if (preg_match('/COUNTRY:\s*([^<]*)/', $ipdata, $matches)) { |
112 | if (!empty($matches[1])) { |
113 | $countrycode = $matches[1]; |
114 | $countries = get_list_of_countries(); |
115 | if (isset($countries[$countrycode])) { |
116 | // prefer our localized country names |
117 | $info[] = $countries[$countrycode]; |
118 | } else { |
119 | $info[] = $countrycode; |
120 | } |
121 | } |
122 | } |
123 | $note[] = get_string('iplookupnetgeonote', 'admin'); |
124 | } |
125 | |
126 | |
127 | |
128 | if (empty($CFG->googlemapkey)) { |
129 | $info = implode(' - ', $info); |
130 | $note = implode('<br />', $note); |
131 | |
132 | $imgwidth = 620; |
133 | $imgheight = 310; |
134 | $dotwidth = 18; |
135 | $dotheight = 30; |
136 | |
137 | $dx = round((($longitude + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2); |
138 | $dy = round((($latitude + 90) * ($imgheight / 180))); |
139 | |
4aa31a61 |
140 | $PAGE->set_title(get_string('iplookup', 'admin').': '.$info); |
141 | $PAGE->set_heading($info); |
142 | echo $OUTPUT->header(); |
7c09710c |
143 | |
144 | echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">'; |
145 | echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />'; |
146 | echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />'; |
147 | echo '</div>'; |
148 | echo '<div id="note">'.$note.'</div>'; |
4aa31a61 |
149 | |
150 | echo $OUTPUT->footer(); |
7c09710c |
151 | |
152 | } else { |
153 | $info = implode(' - ', $info); |
154 | $note = implode('<br />', $note); |
155 | |
48fb4cd6 |
156 | $PAGE->requires->js("http://maps.google.com/maps?file=api&v=2&key=$CFG->googlemapkey", true)->in_head(); |
157 | $PAGE->requires->js('/iplookup/functions.js')->in_head(); |
4aa31a61 |
158 | $PAGE->requires->yui_lib('event'); |
159 | $PAGE->requires->js_function_call('iplookup_load', array($latitude, $longitude)); |
f4fb66d7 |
160 | |
4aa31a61 |
161 | $PAGE->set_title(get_string('iplookup', 'admin').': '.$info); |
162 | $PAGE->set_heading($info); |
163 | echo $OUTPUT->header(); |
f4fb66d7 |
164 | |
7c09710c |
165 | echo '<div id="map" style="width: 650px; height: 360px"></div>'; |
166 | echo '<div id="note">'.$note.'</div>'; |
48fb4cd6 |
167 | |
48fb4cd6 |
168 | echo $OUTPUT->footer(); |
7c09710c |
169 | } |
f4fb66d7 |
170 | |