Commit | Line | Data |
---|---|---|
48fb4cd6 | 1 | <?php |
48fb4cd6 | 2 | // This file is part of Moodle - http://moodle.org/ |
3 | // | |
4 | // Moodle is free software: you can redistribute it and/or modify | |
5 | // it under the terms of the GNU General Public License as published by | |
6 | // the Free Software Foundation, either version 3 of the License, or | |
7 | // (at your option) any later version. | |
8 | // | |
9 | // Moodle is distributed in the hope that it will be useful, | |
10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | // GNU General Public License for more details. | |
13 | // | |
14 | // You should have received a copy of the GNU General Public License | |
15 | // along with Moodle. If not, see <http://www.gnu.org/licenses/>. | |
16 | ||
17 | /** | |
b9ee1fc7 PS |
18 | * Displays IP address on map. |
19 | * | |
20 | * This script is not compatible with IPv6. | |
48fb4cd6 | 21 | * |
704533fc | 22 | * @package core_iplookup |
48fb4cd6 | 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'); |
b9ee1fc7 | 28 | require_once('lib.php'); |
f4fb66d7 | 29 | |
7c09710c | 30 | require_login(); |
f4fb66d7 | 31 | |
7c09710c | 32 | $ip = optional_param('ip', getremoteaddr(), PARAM_HOST); |
b9ee1fc7 | 33 | $user = optional_param('user', 0, PARAM_INT); |
f4fb66d7 | 34 | |
7c09710c | 35 | if (isset($CFG->iplookup)) { |
704533fc | 36 | // Clean up of old settings. |
7c09710c | 37 | set_config('iplookup', NULL); |
38 | } | |
39 | ||
a6855934 | 40 | $PAGE->set_url('/iplookup/index.php', array('id'=>$ip, 'user'=>$user)); |
78946b9b | 41 | $PAGE->set_pagelayout('popup'); |
bf0f06b1 | 42 | $PAGE->set_context(context_system::instance()); |
48fb4cd6 | 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 | ||
b9ee1fc7 | 59 | $info = iplookup_find_location($ip); |
7c09710c | 60 | |
b9ee1fc7 | 61 | if ($info['error']) { |
704533fc | 62 | // Can not display. |
b9ee1fc7 PS |
63 | notice($info['error']); |
64 | } | |
7c09710c | 65 | |
b9ee1fc7 PS |
66 | if ($user) { |
67 | if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) { | |
68 | // note: better not show full names to everybody | |
bf0f06b1 | 69 | if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) { |
b9ee1fc7 | 70 | array_unshift($info['title'], fullname($user)); |
7c09710c | 71 | } |
72 | } | |
7c09710c | 73 | } |
b9ee1fc7 | 74 | array_unshift($info['title'], $ip); |
7c09710c | 75 | |
b9ee1fc7 PS |
76 | $title = implode(' - ', $info['title']); |
77 | $PAGE->set_title(get_string('iplookup', 'admin').': '.$title); | |
78 | $PAGE->set_heading($title); | |
79 | echo $OUTPUT->header(); | |
7c09710c | 80 | |
704533fc | 81 | if (empty($CFG->googlemapkey3)) { |
7c09710c | 82 | $imgwidth = 620; |
83 | $imgheight = 310; | |
84 | $dotwidth = 18; | |
85 | $dotheight = 30; | |
86 | ||
b9ee1fc7 PS |
87 | $dx = round((($info['longitude'] + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2); |
88 | $dy = round((($info['latitude'] + 90) * ($imgheight / 180))); | |
7c09710c | 89 | |
90 | echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">'; | |
91 | echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />'; | |
92 | echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />'; | |
93 | echo '</div>'; | |
b9ee1fc7 | 94 | echo '<div id="note">'.$info['note'].'</div>'; |
7c09710c | 95 | |
cec7d435 PS |
96 | } else { |
97 | if (strpos($CFG->wwwroot, 'https:') === 0) { | |
98 | $PAGE->requires->js(new moodle_url('https://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false'))); | |
99 | } else { | |
100 | $PAGE->requires->js(new moodle_url('http://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false'))); | |
101 | } | |
102 | $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js'); | |
103 | $PAGE->requires->js_init_call('M.core_iplookup.init3', array($info['latitude'], $info['longitude'], $ip), true, $module); | |
104 | ||
7c09710c | 105 | echo '<div id="map" style="width: 650px; height: 360px"></div>'; |
b9ee1fc7 | 106 | echo '<div id="note">'.$info['note'].'</div>'; |
7c09710c | 107 | } |
f4fb66d7 | 108 | |
b9ee1fc7 | 109 | echo $OUTPUT->footer(); |