diff --git a/iplookup/index.php b/iplookup/index.php index 19f30cbe66d..02ccbff4a6d 100644 --- a/iplookup/index.php +++ b/iplookup/index.php @@ -33,16 +33,38 @@ if (isguestuser()) { throw new require_login_exception('Guests are not allowed here.'); } -$ip = optional_param('ip', getremoteaddr(), PARAM_RAW); +$ip = optional_param('ip', getremoteaddr(), PARAM_RAW); $user = optional_param('user', 0, PARAM_INT); +$width = optional_param('width', 0, PARAM_INT); +$height = optional_param('height', 0, PARAM_INT); +$ispopup = optional_param('popup', 0, PARAM_INT); if (isset($CFG->iplookup)) { // Clean up of old settings. set_config('iplookup', NULL); } -$PAGE->set_url('/iplookup/index.php', array('id'=>$ip, 'user'=>$user)); -$PAGE->set_pagelayout('popup'); +$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); $PAGE->set_context(context_system::instance()); $info = array($ip); @@ -71,38 +93,57 @@ if ($user) { } } } -array_unshift($info['title'], $ip); -$title = implode(' - ', $info['title']); +$title = $ip; +foreach ($info['title'] as $component) { + if (!empty(trim($component))) { + $title .= ' - ' . $component; + } +} $PAGE->set_title(get_string('iplookup', 'admin').': '.$title); $PAGE->set_heading($title); echo $OUTPUT->header(); -if (empty($CFG->googlemapkey3)) { - $imgwidth = 620; - $imgheight = 310; - $dotwidth = 18; - $dotheight = 30; - - $dx = round((($info['longitude'] + 180) * ($imgwidth / 360)) - $imgwidth - $dotwidth/2); - $dy = round((($info['latitude'] + 90) * ($imgheight / 180))); - - echo '
'; - echo ''; - echo ''; - echo '
'; - echo '
'.$info['note'].'
'; - +// The map dimension is here as big as the popup/page is, so max with and at least 360px height. +if ($ispopup) { + echo '

' . htmlspecialchars($title) . '

'; + $mapdim = 'width: ' + . (($width > 0) ? $width . 'px' : '100%') + . '; height: ' + . (($height > 0) ? $height . 'px;' : '100%; min-height:360px;'); } else { - if (is_https()) { - $PAGE->requires->js(new moodle_url('https://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false'))); - } else { - $PAGE->requires->js(new moodle_url('http://maps.googleapis.com/maps/api/js', array('key'=>$CFG->googlemapkey3, 'sensor'=>'false'))); - } - $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js'); - $PAGE->requires->js_init_call('M.core_iplookup.init3', array($info['latitude'], $info['longitude'], $ip), true, $module); + $mapdim = 'width:100%; height:100%;min-height:360px'; +} - echo '
'; +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 '
' + . '' + . '
' + . '
' . $info['note'] . '
'; + + +} 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' + ] + )); + $module = array('name'=>'core_iplookup', 'fullpath'=>'/iplookup/module.js'); + $PAGE->requires->js_init_call('M.core_iplookup.init3', [$info['latitude'], $info['longitude'], $ip], true, $module); + + echo '
'; echo '
'.$info['note'].'
'; } diff --git a/iplookup/lib.php b/iplookup/lib.php index 4afeb3f89ef..2570d223cb5 100644 --- a/iplookup/lib.php +++ b/iplookup/lib.php @@ -59,7 +59,7 @@ function iplookup_find_location($ip) { $info['title'][] = $info['country']; $info['longitude'] = $record->location->longitude; - $info['latitude'] = $record->location->latitude; + $info['latitude'] = $record->location->latitude; $info['note'] = get_string('iplookupmaxmindnote', 'admin'); return $info; @@ -82,9 +82,10 @@ function iplookup_find_location($ip) { $info['error'] = get_string('cannotgeoplugin', 'error'); return $info; } - $info['latitude'] = (float)$ipdata['geoplugin_latitude']; + $info['latitude'] = (float)$ipdata['geoplugin_latitude']; $info['longitude'] = (float)$ipdata['geoplugin_longitude']; - $info['city'] = s($ipdata['geoplugin_city']); + $info['city'] = s($ipdata['geoplugin_city']); + $info['accuracyRadius'] = (int)$ipdata['geoplugin_locationAccuracyRadius']; // Unit is in Miles. $countrycode = $ipdata['geoplugin_countryCode']; $countries = get_string_manager()->get_list_of_countries(true); diff --git a/report/log/classes/table_log.php b/report/log/classes/table_log.php index f240d8ebfcb..e64dfad0267 100644 --- a/report/log/classes/table_log.php +++ b/report/log/classes/table_log.php @@ -319,7 +319,7 @@ class report_log_table_log extends table_sql { $ip = $logextra['ip']; if (empty($this->download)) { - $url = new moodle_url("/iplookup/index.php?ip={$ip}&user={$event->userid}"); + $url = new moodle_url("/iplookup/index.php?popup=1&ip={$ip}&user={$event->userid}"); $ip = $this->action_link($url, $ip, 'ip'); } return $ip; diff --git a/report/loglive/classes/table_log.php b/report/loglive/classes/table_log.php index b29951e62dc..19ca5e1743c 100644 --- a/report/loglive/classes/table_log.php +++ b/report/loglive/classes/table_log.php @@ -272,7 +272,7 @@ class report_loglive_table_log extends table_sql { // Get extra event data for origin and realuserid. $logextra = $event->get_logextra(); - $url = new moodle_url("/iplookup/index.php?ip={$logextra['ip']}&user=$event->userid"); + $url = new moodle_url("/iplookup/index.php?popup=1&ip={$logextra['ip']}&user=$event->userid"); return $this->action_link($url, $logextra['ip'], 'ip'); } @@ -287,7 +287,7 @@ class report_loglive_table_log extends table_sql { */ protected function action_link(moodle_url $url, $text, $name = 'popup') { global $OUTPUT; - $link = new action_link($url, $text, new popup_action('click', $url, $name, array('height' => 440, 'width' => 700))); + $link = new action_link($url, $text, new popup_action('click', $url, $name, array('height' => 550, 'width' => 700))); return $OUTPUT->render($link); }