Merge branch 'MDL-36877_27' of git://github.com/jmvedrine/moodle into MOODLE_27_STABLE
[moodle.git] / iplookup / index.php
blob129b59f7f15f860361c702d9483d9ab7962198b5
1 <?php
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.
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/>.
17 /**
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);
31 if (isguestuser()) {
32 // Guest users cannot perform lookups.
33 throw new require_login_exception('Guests are not allowed here.');
36 $ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
37 $user = optional_param('user', 0, PARAM_INT);
39 if (isset($CFG->iplookup)) {
40 // Clean up of old settings.
41 set_config('iplookup', NULL);
44 $PAGE->set_url('/iplookup/index.php', array('id'=>$ip, 'user'=>$user));
45 $PAGE->set_pagelayout('popup');
46 $PAGE->set_context(context_system::instance());
48 $info = array($ip);
49 $note = array();
51 if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
52 print_error('invalidipformat', 'error');
55 if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
56 print_error('invalidipformat', 'error');
59 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')) {
60 print_error('iplookupprivate', 'error');
63 $info = iplookup_find_location($ip);
65 if ($info['error']) {
66 // Can not display.
67 notice($info['error']);
70 if ($user) {
71 if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
72 // note: better not show full names to everybody
73 if (has_capability('moodle/user:viewdetails', context_user::instance($user->id))) {
74 array_unshift($info['title'], fullname($user));
78 array_unshift($info['title'], $ip);
80 $title = implode(' - ', $info['title']);
81 $PAGE->set_title(get_string('iplookup', 'admin').': '.$title);
82 $PAGE->set_heading($title);
83 echo $OUTPUT->header();
85 if (empty($CFG->googlemapkey3)) {
86 $imgwidth = 620;
87 $imgheight = 310;
88 $dotwidth = 18;
89 $dotheight = 30;
91 $dx = round((($info['longitude'] + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
92 $dy = round((($info['latitude'] + 90) * ($imgheight / 180)));
94 echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">';
95 echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />';
96 echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />';
97 echo '</div>';
98 echo '<div id="note">'.$info['note'].'</div>';
100 } else {
101 if (strpos($CFG->wwwroot, 'https:') === 0) {
102 $PAGE->requires->js(new moodle_url('https://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false')));
103 } else {
104 $PAGE->requires->js(new moodle_url('http://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false')));
106 $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js');
107 $PAGE->requires->js_init_call('M.core_iplookup.init3', array($info['latitude'], $info['longitude'], $ip), true, $module);
109 echo '<div id="map" style="width: 650px; height: 360px"></div>';
110 echo '<div id="note">'.$info['note'].'</div>';
113 echo $OUTPUT->footer();