2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Displays IP address on map.
20 * This script is not compatible with IPv6.
22 * @package core_iplookup
23 * @copyright 2008 Petr Skoda (http://skodak.org)
24 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 require('../config.php');
28 require_once('lib.php');
30 require_login(0, false);
32 // Guest users cannot perform lookups.
33 throw new require_login_exception('Guests are not allowed here.');
36 $ip = optional_param('ip', getremoteaddr(), PARAM_RAW
);
37 $user = optional_param('user', 0, PARAM_INT
);
38 $width = optional_param('width', 0, PARAM_INT
);
39 $height = optional_param('height', 0, PARAM_INT
);
40 $ispopup = optional_param('popup', 0, PARAM_INT
);
42 if (isset($CFG->iplookup
)) {
43 // Clean up of old settings.
44 set_config('iplookup', NULL);
52 // Params width and height are set, we assume to have a popup.
53 if ($width > 0 && $height > 0) {
54 $urlparams['width'] = $width;
55 $urlparams['height'] = $height;
57 } else if ($ispopup === 1) { // Param popup was set, then we know that we want a popup.
58 $urlparams['ispopup'] = 1;
60 // Set the page layout accordingly.
62 $PAGE->set_pagelayout('popup');
64 $PAGE->set_pagelayout('standard');
67 $PAGE->set_url('/iplookup/index.php', $urlparams);
68 $PAGE->set_context(context_system
::instance());
73 if (cleanremoteaddr($ip) === false) {
74 throw new \
moodle_exception('invalidipformat', 'error');
77 if (!ip_is_public($ip)) {
78 throw new \
moodle_exception('iplookupprivate', 'error');
81 $info = iplookup_find_location($ip);
85 notice($info['error']);
89 if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
90 // note: better not show full names to everybody
91 if (has_capability('moodle/user:viewdetails', context_user
::instance($user->id
))) {
92 array_unshift($info['title'], fullname($user));
98 foreach ($info['title'] as $component) {
99 if (!empty(trim($component))) {
100 $title .= ' - ' . $component;
103 $PAGE->set_title(get_string('iplookup', 'admin').': '.$title);
104 $PAGE->set_heading($title);
105 echo $OUTPUT->header();
107 // The map dimension is here as big as the popup/page is, so max with and at least 360px height.
109 echo '<h1 class="iplookup h2">' . htmlspecialchars($title, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE
) . '</h1>';
111 . (($width > 0) ?
$width . 'px' : '100%')
113 . (($height > 0) ?
$height . 'px;' : '100%; min-height:360px;');
115 $mapdim = 'width:100%; height:100%;min-height:360px';
118 if (empty($CFG->googlemapkey3
)) { // No Google API key is set, we use OSM.
120 // Have a fixed zoom factor to calculate corners of the map.
122 $bboxleft = $info['longitude'] - $fkt;
123 $bboxbottom = $info['latitude'] - $fkt;
124 $bboxright = $info['longitude'] +
$fkt;
125 $bboxtop = $info['latitude'] +
$fkt;
127 echo '<div id="map" style="' . $mapdim . '">'
128 . '<object data="https://www.openstreetmap.org/export/embed.html?bbox='
129 . $bboxleft . '%2C' . $bboxbottom . '%2C' . $bboxright . '%2C' . $bboxtop
130 . '&layer=mapnik&marker=' . $info['latitude'] . '%2C' . $info['longitude'] . '" style="' . $mapdim . '"></object>'
132 . '<div id="note">' . $info['note'] . '</div>';
135 } else { // Google API key is set, then use Google Maps.
136 $PAGE->requires
->js(new moodle_url(
137 'https://maps.googleapis.com/maps/api/js',
139 'key' => $CFG->googlemapkey3
,
143 $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js');
144 $PAGE->requires
->js_init_call('M.core_iplookup.init3', [$info['latitude'], $info['longitude'], $ip], true, $module);
146 echo '<div id="map" style="' . $mapdim . '"></div>';
147 echo '<div id="note">'.$info['note'].'</div>';
150 echo $OUTPUT->footer();