2009-07-19 13:54:11 +00:00
|
|
|
<?php
|
|
|
|
// This file is part of Moodle - http://moodle.org/
|
|
|
|
//
|
|
|
|
// Moodle is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Moodle is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
/**
|
2010-08-09 16:10:02 +00:00
|
|
|
* Displays IP address on map.
|
|
|
|
*
|
|
|
|
* This script is not compatible with IPv6.
|
2009-07-19 13:54:11 +00:00
|
|
|
*
|
2012-08-19 20:07:27 +02:00
|
|
|
* @package core_iplookup
|
2009-07-19 13:54:11 +00:00
|
|
|
* @copyright 2008 Petr Skoda (http://skodak.org)
|
|
|
|
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
|
|
|
|
*/
|
2005-07-18 08:06:16 +00:00
|
|
|
|
2008-01-02 16:47:57 +00:00
|
|
|
require('../config.php');
|
2010-08-09 16:10:02 +00:00
|
|
|
require_once('lib.php');
|
2005-07-18 08:06:16 +00:00
|
|
|
|
2014-10-28 12:04:08 +08:00
|
|
|
require_login(0, false);
|
|
|
|
if (isguestuser()) {
|
|
|
|
// Guest users cannot perform lookups.
|
|
|
|
throw new require_login_exception('Guests are not allowed here.');
|
|
|
|
}
|
2005-07-18 08:06:16 +00:00
|
|
|
|
2022-12-23 11:59:37 +01:00
|
|
|
$ip = optional_param('ip', getremoteaddr(), PARAM_RAW);
|
2010-08-09 16:10:02 +00:00
|
|
|
$user = optional_param('user', 0, PARAM_INT);
|
2022-12-23 11:59:37 +01:00
|
|
|
$width = optional_param('width', 0, PARAM_INT);
|
|
|
|
$height = optional_param('height', 0, PARAM_INT);
|
|
|
|
$ispopup = optional_param('popup', 0, PARAM_INT);
|
2005-07-18 08:06:16 +00:00
|
|
|
|
2008-01-02 16:47:57 +00:00
|
|
|
if (isset($CFG->iplookup)) {
|
2012-08-19 20:07:27 +02:00
|
|
|
// Clean up of old settings.
|
2008-01-02 16:47:57 +00:00
|
|
|
set_config('iplookup', NULL);
|
|
|
|
}
|
|
|
|
|
2022-12-23 11:59:37 +01:00
|
|
|
$urlparams = [
|
|
|
|
'id' => $ip,
|
|
|
|
'user' => $user,
|
|
|
|
];
|
|
|
|
|
|
|
|
// Params width and height are set, we assume to have a popup.
|
|
|
|
if ($width > 0 && $height > 0) {
|
|
|
|
$urlparams['width'] = $width;
|
|
|
|
$urlparams['height'] = $height;
|
|
|
|
$ispopup = 1;
|
|
|
|
} else if ($ispopup === 1) { // Param popup was set, then we know that we want a popup.
|
|
|
|
$urlparams['ispopup'] = 1;
|
|
|
|
}
|
|
|
|
// Set the page layout accordingly.
|
|
|
|
if ($ispopup) {
|
|
|
|
$PAGE->set_pagelayout('popup');
|
|
|
|
} else {
|
|
|
|
$PAGE->set_pagelayout('standard');
|
|
|
|
}
|
|
|
|
|
|
|
|
$PAGE->set_url('/iplookup/index.php', $urlparams);
|
2012-08-02 11:20:48 +08:00
|
|
|
$PAGE->set_context(context_system::instance());
|
2009-07-19 13:54:11 +00:00
|
|
|
|
2008-01-02 16:47:57 +00:00
|
|
|
$info = array($ip);
|
|
|
|
$note = array();
|
|
|
|
|
2015-11-26 13:00:20 +00:00
|
|
|
if (cleanremoteaddr($ip) === false) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('invalidipformat', 'error');
|
2008-01-02 16:47:57 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 13:00:20 +00:00
|
|
|
if (!ip_is_public($ip)) {
|
2022-04-12 09:38:41 +05:30
|
|
|
throw new \moodle_exception('iplookupprivate', 'error');
|
2008-01-02 16:47:57 +00:00
|
|
|
}
|
|
|
|
|
2010-08-09 16:10:02 +00:00
|
|
|
$info = iplookup_find_location($ip);
|
2008-01-02 16:47:57 +00:00
|
|
|
|
2010-08-09 16:10:02 +00:00
|
|
|
if ($info['error']) {
|
2012-08-19 20:07:27 +02:00
|
|
|
// Can not display.
|
2010-08-09 16:10:02 +00:00
|
|
|
notice($info['error']);
|
|
|
|
}
|
2008-01-02 16:47:57 +00:00
|
|
|
|
2010-08-09 16:10:02 +00:00
|
|
|
if ($user) {
|
|
|
|
if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
|
|
|
|
// note: better not show full names to everybody
|
2012-08-02 11:20:48 +08:00
|
|
|
if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) {
|
2010-08-09 16:10:02 +00:00
|
|
|
array_unshift($info['title'], fullname($user));
|
2008-01-02 16:47:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-23 11:59:37 +01:00
|
|
|
$title = $ip;
|
|
|
|
foreach ($info['title'] as $component) {
|
|
|
|
if (!empty(trim($component))) {
|
|
|
|
$title .= ' - ' . $component;
|
|
|
|
}
|
|
|
|
}
|
2010-08-09 16:10:02 +00:00
|
|
|
$PAGE->set_title(get_string('iplookup', 'admin').': '.$title);
|
|
|
|
$PAGE->set_heading($title);
|
|
|
|
echo $OUTPUT->header();
|
2008-01-02 16:47:57 +00:00
|
|
|
|
2022-12-23 11:59:37 +01:00
|
|
|
// The map dimension is here as big as the popup/page is, so max with and at least 360px height.
|
|
|
|
if ($ispopup) {
|
2023-08-03 14:33:34 +10:00
|
|
|
echo '<h1 class="iplookup h2">' . htmlspecialchars($title, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE) . '</h1>';
|
2022-12-23 11:59:37 +01:00
|
|
|
$mapdim = 'width: '
|
|
|
|
. (($width > 0) ? $width . 'px' : '100%')
|
|
|
|
. '; height: '
|
|
|
|
. (($height > 0) ? $height . 'px;' : '100%; min-height:360px;');
|
2012-08-19 19:52:35 +02:00
|
|
|
} else {
|
2022-12-23 11:59:37 +01:00
|
|
|
$mapdim = 'width:100%; height:100%;min-height:360px';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (empty($CFG->googlemapkey3)) { // No Google API key is set, we use OSM.
|
|
|
|
|
|
|
|
// Have a fixed zoom factor to calculate corners of the map.
|
|
|
|
$fkt = 4;
|
|
|
|
$bboxleft = $info['longitude'] - $fkt;
|
|
|
|
$bboxbottom = $info['latitude'] - $fkt;
|
|
|
|
$bboxright = $info['longitude'] + $fkt;
|
|
|
|
$bboxtop = $info['latitude'] + $fkt;
|
|
|
|
|
|
|
|
echo '<div id="map" style="' . $mapdim . '">'
|
|
|
|
. '<object data="https://www.openstreetmap.org/export/embed.html?bbox='
|
|
|
|
. $bboxleft . '%2C' . $bboxbottom . '%2C' . $bboxright . '%2C' . $bboxtop
|
|
|
|
. '&layer=mapnik&marker=' . $info['latitude'] . '%2C' . $info['longitude'] . '" style="' . $mapdim . '"></object>'
|
|
|
|
. '</div>'
|
|
|
|
. '<div id="note">' . $info['note'] . '</div>';
|
|
|
|
|
|
|
|
|
|
|
|
} else { // Google API key is set, then use Google Maps.
|
|
|
|
$PAGE->requires->js(new moodle_url(
|
|
|
|
'https://maps.googleapis.com/maps/api/js',
|
|
|
|
[
|
|
|
|
'key' => $CFG->googlemapkey3,
|
|
|
|
'sensor' => 'false'
|
|
|
|
]
|
|
|
|
));
|
2012-08-19 19:52:35 +02:00
|
|
|
$module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js');
|
2022-12-23 11:59:37 +01:00
|
|
|
$PAGE->requires->js_init_call('M.core_iplookup.init3', [$info['latitude'], $info['longitude'], $ip], true, $module);
|
2012-08-19 19:52:35 +02:00
|
|
|
|
2022-12-23 11:59:37 +01:00
|
|
|
echo '<div id="map" style="' . $mapdim . '"></div>';
|
2010-08-09 16:10:02 +00:00
|
|
|
echo '<div id="note">'.$info['note'].'</div>';
|
2008-01-02 16:47:57 +00:00
|
|
|
}
|
2005-07-18 08:06:16 +00:00
|
|
|
|
2010-08-09 16:10:02 +00:00
|
|
|
echo $OUTPUT->footer();
|