MDL-23720 improved iplookup coding style + fixed a potential privacy issue
[moodle.git] / iplookup / index.php
CommitLineData
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/**
b9ee1fc7
PS
19 * Displays IP address on map.
20 *
21 * This script is not compatible with IPv6.
48fb4cd6 22 *
8061887a 23 * @package core
48fb4cd6 24 * @subpackage iplookup
25 * @copyright 2008 Petr Skoda (http://skodak.org)
26 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27 */
f4fb66d7 28
7c09710c 29require('../config.php');
b9ee1fc7 30require_once('lib.php');
f4fb66d7 31
7c09710c 32require_login();
f4fb66d7 33
7c09710c 34$ip = optional_param('ip', getremoteaddr(), PARAM_HOST);
b9ee1fc7 35$user = optional_param('user', 0, PARAM_INT);
f4fb66d7 36
7c09710c 37if (isset($CFG->iplookup)) {
38 //clean up of old settings
39 set_config('iplookup', NULL);
40}
41
a6855934 42$PAGE->set_url('/iplookup/index.php', array('id'=>$ip, 'user'=>$user));
78946b9b 43$PAGE->set_pagelayout('popup');
8fb1c9af 44$PAGE->set_context(get_context_instance(CONTEXT_SYSTEM));
48fb4cd6 45
7c09710c 46$info = array($ip);
47$note = array();
48
49if (!preg_match('/(^\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/', $ip, $match)) {
50 print_error('invalidipformat', 'error');
51}
52
53if ($match[1] > 255 or $match[2] > 255 or $match[3] > 255 or $match[4] > 255) {
54 print_error('invalidipformat', 'error');
55}
56
57if ($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')) {
58 print_error('iplookupprivate', 'error');
59}
60
b9ee1fc7 61$info = iplookup_find_location($ip);
7c09710c 62
b9ee1fc7
PS
63if ($info['error']) {
64 // can not display
65 notice($info['error']);
66}
7c09710c 67
b9ee1fc7
PS
68if ($user) {
69 if ($user = $DB->get_record('user', array('id'=>$user, 'deleted'=>0))) {
70 // note: better not show full names to everybody
71 if (has_capability('moodle/user:viewdetails', get_context_instance(CONTEXT_USER, $user->id))) {
72 array_unshift($info['title'], fullname($user));
7c09710c 73 }
74 }
7c09710c 75}
b9ee1fc7 76array_unshift($info['title'], $ip);
7c09710c 77
b9ee1fc7
PS
78$title = implode(' - ', $info['title']);
79$PAGE->set_title(get_string('iplookup', 'admin').': '.$title);
80$PAGE->set_heading($title);
81echo $OUTPUT->header();
7c09710c 82
83if (empty($CFG->googlemapkey)) {
7c09710c 84 $imgwidth = 620;
85 $imgheight = 310;
86 $dotwidth = 18;
87 $dotheight = 30;
88
b9ee1fc7
PS
89 $dx = round((($info['longitude'] + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2);
90 $dy = round((($info['latitude'] + 90) * ($imgheight / 180)));
7c09710c 91
92 echo '<div id="map" style="width:'.($imgwidth+$dotwidth).'px; height:'.$imgheight.'px;">';
93 echo '<img src="earth.jpeg" style="width:'.$imgwidth.'px; height:'.$imgheight.'px" alt="" />';
94 echo '<img src="marker.gif" style="width:'.$dotwidth.'px; height:'.$dotheight.'px; margin-left:'.$dx.'px; margin-bottom:'.$dy.'px;" alt="" />';
95 echo '</div>';
b9ee1fc7 96 echo '<div id="note">'.$info['note'].'</div>';
7c09710c 97
98} else {
789ff38c
PS
99 $PAGE->requires->js(new moodle_url("http://maps.google.com/maps?file=api&v=2&key=$CFG->googlemapkey"));
100 $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js');
b9ee1fc7 101 $PAGE->requires->js_init_call('M.core_iplookup.init', array($info['latitude'], $info['longitude']), true, $module);
f4fb66d7 102
7c09710c 103 echo '<div id="map" style="width: 650px; height: 360px"></div>';
b9ee1fc7 104 echo '<div id="note">'.$info['note'].'</div>';
7c09710c 105}
f4fb66d7 106
b9ee1fc7 107echo $OUTPUT->footer();