Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
/iplookup/ -> index.php (source)

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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.
  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  /**
  18   * Displays IP address on map.
  19   *
  20   * This script is not compatible with IPv6.
  21   *
  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
  25   */
  26  
  27  require('../config.php');
  28  require_once ('lib.php');
  29  
  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.');
  34  }
  35  
  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);
  41  
  42  if (isset($CFG->iplookup)) {
  43      // Clean up of old settings.
  44      set_config('iplookup', NULL);
  45  }
  46  
  47  $urlparams = [
  48      'id' => $ip,
  49      'user' => $user,
  50  ];
  51  
  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;
  56      $ispopup = 1;
  57  } else if ($ispopup === 1) {  // Param popup was set, then we know that we want a popup.
  58      $urlparams['ispopup'] = 1;
  59  }
  60  // Set the page layout accordingly.
  61  if ($ispopup) {
  62      $PAGE->set_pagelayout('popup');
  63  } else {
  64      $PAGE->set_pagelayout('standard');
  65  }
  66  
  67  $PAGE->set_url('/iplookup/index.php', $urlparams);
  68  $PAGE->set_context(context_system::instance());
  69  
  70  $info = array($ip);
  71  $note = array();
  72  
  73  if (cleanremoteaddr($ip) === false) {
  74      throw new \moodle_exception('invalidipformat', 'error');
  75  }
  76  
  77  if (!ip_is_public($ip)) {
  78      throw new \moodle_exception('iplookupprivate', 'error');
  79  }
  80  
  81  $info = iplookup_find_location($ip);
  82  
  83  if ($info['error']) {
  84      // Can not display.
  85      notice($info['error']);
  86  }
  87  
  88  if ($user) {
  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));
  93          }
  94      }
  95  }
  96  
  97  $title = $ip;
  98  foreach ($info['title'] as $component) {
  99      if (!empty(trim($component))) {
 100          $title .= ' - ' . $component;
 101      }
 102  }
 103  $PAGE->set_title(get_string('iplookup', 'admin').': '.$title);
 104  $PAGE->set_heading($title);
 105  echo $OUTPUT->header();
 106  
 107  // The map dimension is here as big as the popup/page is, so max with and at least 360px height.
 108  if ($ispopup) {
 109      echo '<h1 class="iplookup h2">' . htmlspecialchars($title, ENT_QUOTES | ENT_HTML401 | ENT_SUBSTITUTE) . '</h1>';
 110      $mapdim = 'width: '
 111          . (($width > 0) ? $width . 'px' : '100%')
 112          . '; height: '
 113          . (($height > 0) ? $height . 'px;' : '100%; min-height:360px;');
 114  } else {
 115      $mapdim = 'width:100%; height:100%;min-height:360px';
 116  }
 117  
 118  if (empty($CFG->googlemapkey3)) { // No Google API key is set, we use OSM.
 119  
 120      // Have a fixed zoom factor to calculate corners of the map.
 121      $fkt = 4;
 122      $bboxleft = $info['longitude'] - $fkt;
 123      $bboxbottom = $info['latitude'] - $fkt;
 124      $bboxright = $info['longitude'] + $fkt;
 125      $bboxtop = $info['latitude'] + $fkt;
 126  
 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>'
 131          . '</div>'
 132          . '<div id="note">' . $info['note'] . '</div>';
 133  
 134  
 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',
 138          [
 139              'key' => $CFG->googlemapkey3,
 140              'sensor' => 'false'
 141          ]
 142      ));
 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);
 145  
 146      echo '<div id="map" style="' . $mapdim . '"></div>';
 147      echo '<div id="note">'.$info['note'].'</div>';
 148  }
 149  
 150  echo $OUTPUT->footer();