Issues (2963)

includes/html/common/worldmap.inc.php (1 issue)

1
<?php
2
/* Copyright (C) 2014 Daniel Preussker <[email protected]>
3
 * This program is free software: you can redistribute it and/or modify
4
 * it under the terms of the GNU General Public License as published by
5
 * the Free Software Foundation, either version 3 of the License, or
6
 * (at your option) any later version.
7
 *
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
11
 * GNU General Public License for more details.
12
 *
13
 * You should have received a copy of the GNU General Public License
14
 * along with this program.  If not, see <https://www.gnu.org/licenses/>. */
15
16
/**
17
 * Custom Frontpage
18
 *
19
 * @author f0o <[email protected]>
20
 * @copyright 2014 f0o, LibreNMS
21
 * @license GPL
22
 */
23
24
use Illuminate\Support\Facades\Auth;
25
use LibreNMS\Alert\AlertUtil;
26
use LibreNMS\Config;
0 ignored issues
show
This use statement conflicts with another class in this namespace, Config. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
27
28
$install_dir = Config::get('install_dir');
29
30
if (Config::get('map.engine', 'leaflet') == 'leaflet') {
31
    $temp_output = '
32
<script src="js/leaflet.js"></script>
33
<script src="js/leaflet.markercluster.js"></script>
34
<script src="js/leaflet.awesome-markers.min.js"></script>
35
<div id="leaflet-map"></div>
36
<script>
37
        ';
38
    $init_lat = Config::get('leaflet.default_lat', 51.48);
39
    $init_lng = Config::get('leaflet.default_lng', 0);
40
    $init_zoom = Config::get('leaflet.default_zoom', 5);
41
    $group_radius = Config::get('leaflet.group_radius', 80);
42
    $tile_url = Config::get('leaflet.tile_url', '{s}.tile.openstreetmap.org');
43
    $show_status = [0, 1];
44
    $map_init = '[' . $init_lat . ', ' . $init_lng . '], ' . sprintf('%01.1f', $init_zoom);
45
    $temp_output .= 'var map = L.map(\'leaflet-map\', { zoomSnap: 0.1 } ).setView(' . $map_init . ');
46
L.tileLayer(\'//' . $tile_url . '/{z}/{x}/{y}.png\', {
47
    attribution: \'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors\'
48
}).addTo(map);
49
50
var markers = L.markerClusterGroup({
51
    maxClusterRadius: ' . $group_radius . ',
52
    iconCreateFunction: function (cluster) {
53
        var markers = cluster.getAllChildMarkers();
54
        var n = 0;
55
        color = "green"
56
        newClass = "Cluster marker-cluster marker-cluster-small leaflet-zoom-animated leaflet-clickable";
57
        for (var i = 0; i < markers.length; i++) {
58
            if (markers[i].options.icon.options.markerColor == "blue" && color != "red") {
59
                color = "blue";
60
            }
61
            if (markers[i].options.icon.options.markerColor == "red") {
62
                color = "red";
63
            }
64
        }
65
        return L.divIcon({ html: cluster.getChildCount(), className: color+newClass, iconSize: L.point(40, 40) });
66
    },
67
  });
68
var redMarker = L.AwesomeMarkers.icon({
69
    icon: \'server\',
70
    markerColor: \'red\', prefix: \'fa\', iconColor: \'white\'
71
  });
72
var blueMarker = L.AwesomeMarkers.icon({
73
      icon: \'server\',
74
      markerColor: \'blue\', prefix: \'fa\', iconColor: \'white\'
75
    });
76
var greenMarker = L.AwesomeMarkers.icon({
77
    icon: \'server\',
78
    markerColor: \'green\', prefix: \'fa\', iconColor: \'white\'
79
  });
80
        ';
81
82
    // Checking user permissions
83
    if (Auth::user()->hasGlobalRead()) {
84
        // Admin or global read-only - show all devices
85
        $sql = "SELECT DISTINCT(`device_id`),`location`,`sysName`,`hostname`,`os`,`status`,`lat`,`lng` FROM `devices`
86
                LEFT JOIN `locations` ON `devices`.`location_id`=`locations`.`id`
87
                WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`location` REGEXP '\[[0-9\.\, ]+\]'))
88
                AND (`lat` IS NOT NULL AND `lng` IS NOT NULL)
89
                AND `status` IN " . dbGenPlaceholders(count($show_status)) .
90
                ' ORDER BY `status` ASC, `hostname`';
91
        $param = $show_status;
92
    } else {
93
        // Normal user - grab devices that user has permissions to
94
        $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
95
96
        $sql = "SELECT DISTINCT(`devices`.`device_id`) as `device_id`,`location`,`sysName`,`hostname`,`os`,`status`,`lat`,`lng`
97
                FROM `devices`
98
                LEFT JOIN `locations` ON `devices`.location_id=`locations`.`id`
99
                WHERE `disabled`=0 AND `ignore`=0 AND ((`lat` != '' AND `lng` != '') OR (`location` REGEXP '\[[0-9\.\, ]+\]'))
100
                AND (`lat` IS NOT NULL AND `lng` IS NOT NULL)
101
                AND `devices`.`device_id` IN " . dbGenPlaceholders(count($device_ids)) .
102
                ' AND `status` IN ' . dbGenPlaceholders(count($show_status)) .
103
                ' ORDER BY `status` ASC, `hostname`';
104
        $param = array_merge($device_ids, $show_status);
105
    }
106
107
    foreach (dbFetchRows($sql, $param) as $map_devices) {
108
        $icon = 'greenMarker';
109
        $z_offset = 0;
110
        $tmp_loc = parse_location($map_devices['location']);
111
        if (is_numeric($tmp_loc['lat']) && is_numeric($tmp_loc['lng'])) {
112
            $map_devices['lat'] = $tmp_loc['lat'];
113
            $map_devices['lng'] = $tmp_loc['lng'];
114
        }
115
        if ($map_devices['status'] == 0) {
116
            if (AlertUtil::isMaintenance($map_devices['device_id'])) {
117
                if ($show_status == 0) { // Don't show icon if only down devices should be shown
118
                    continue;
119
                } else {
120
                    $icon = 'blueMarker';
121
                    $z_offset = 5000;
122
                }
123
            } else {
124
                $icon = 'redMarker';
125
                $z_offset = 10000;  // move marker to foreground
126
            }
127
        }
128
        $temp_output .= "var title = '<a href=\"" . \LibreNMS\Util\Url::deviceUrl((int) $map_devices['device_id']) . '"><img src="' . getIcon($map_devices) . '" width="32" height="32" alt=""> ' . format_hostname($map_devices) . "</a>';
129
var tooltip = '" . format_hostname($map_devices) . "';
130
var marker = L.marker(new L.LatLng(" . $map_devices['lat'] . ', ' . $map_devices['lng'] . "), {title: tooltip, icon: $icon, zIndexOffset: $z_offset});
131
marker.bindPopup(title);
132
    markers.addLayer(marker);\n";
133
    }
134
135
    if (Config::get('network_map_show_on_worldmap')) {
136
        if (Auth::user()->hasGlobalRead()) {
137
            $sql = "
138
            SELECT
139
              ll.id AS left_id,
140
              ll.lat AS left_lat,
141
              ll.lng AS left_lng,
142
              rl.id AS right_id,
143
              rl.lat AS right_lat,
144
              rl.lng AS right_lng,
145
              sum(lp.ifSpeed) AS link_capacity,
146
              sum(lp.ifOutOctets_rate) * 8 / sum(lp.ifSpeed) * 100 as link_out_usage_pct,
147
              sum(lp.ifInOctets_rate) * 8 / sum(lp.ifSpeed) * 100 as link_in_usage_pct
148
            FROM
149
              devices AS ld,
150
              devices AS rd,
151
              links AS l,
152
              locations AS ll,
153
              locations AS rl,
154
              ports as lp
155
            WHERE
156
              l.local_device_id = ld.device_id
157
              AND l.remote_device_id = rd.device_id
158
              AND ld.location_id != rd.location_id
159
              AND ld.location_id = ll.id
160
              AND rd.location_id = rl.id
161
              AND lp.device_id = ld.device_id
162
              AND lp.port_id = l.local_port_id
163
              AND lp.ifType = 'ethernetCsmacd'
164
              AND ld.disabled = 0
165
              AND ld.ignore = 0
166
              AND rd.disabled = 0
167
              AND rd.ignore = 0
168
              AND lp.ifOutOctets_rate != 0
169
              AND lp.ifInOctets_rate != 0
170
              AND lp.ifOperStatus = 'up'
171
              AND ll.lat IS NOT NULL
172
              AND ll.lng IS NOT NULL
173
              AND rl.lat IS NOT NULL
174
              AND rl.lng IS NOT NULL
175
              AND ld.status IN " . dbGenPlaceholders(count($show_status)) . '
176
              AND rd.status IN ' . dbGenPlaceholders(count($show_status)) . '
177
            GROUP BY
178
              left_id, right_id, ll.lat, ll.lng, rl.lat, rl.lng
179
                  ';
180
            $param = array_merge($show_status, $show_status);
181
        } else {
182
            $device_ids = Permissions::devicesForUser()->toArray() ?: [0];
183
            $sql = "
184
            SELECT
185
              ll.id AS left_id,
186
              ll.lat AS left_lat,
187
              ll.lng AS left_lng,
188
              rl.id AS right_id,
189
              rl.lat AS right_lat,
190
              rl.lng AS right_lng,
191
              sum(lp.ifSpeed) AS link_capacity,
192
              sum(lp.ifOutOctets_rate) * 8 / sum(lp.ifSpeed) * 100 as link_out_usage_pct,
193
              sum(lp.ifInOctets_rate) * 8 / sum(lp.ifSpeed) * 100 as link_in_usage_pct
194
            FROM
195
              devices AS ld,
196
              devices AS rd,
197
              links AS l,
198
              locations AS ll,
199
              locations AS rl,
200
              ports as lp
201
            WHERE
202
              l.local_device_id = ld.device_id
203
              AND l.remote_device_id = rd.device_id
204
              AND ld.location_id != rd.location_id
205
              AND ld.location_id = ll.id
206
              AND rd.location_id = rl.id
207
              AND lp.device_id = ld.device_id
208
              AND lp.port_id = l.local_port_id
209
              AND lp.ifType = 'ethernetCsmacd'
210
              AND ld.disabled = 0
211
              AND ld.ignore = 0
212
              AND rd.disabled = 0
213
              AND rd.ignore = 0
214
              AND lp.ifOutOctets_rate != 0
215
              AND lp.ifInOctets_rate != 0
216
              AND lp.ifOperStatus = 'up'
217
              AND ll.lat IS NOT NULL
218
              AND ll.lng IS NOT NULL
219
              AND rl.lat IS NOT NULL
220
              AND rl.lng IS NOT NULL
221
              AND ld.status IN " . dbGenPlaceholders(count($show_status)) . '
222
              AND rd.status IN ' . dbGenPlaceholders(count($show_status)) . '
223
              AND ld.device_id IN ' . dbGenPlaceholders(count($device_ids)) . '
224
              AND rd.device_id IN ' . dbGenPlaceholders(count($device_ids)) . '
225
            GROUP BY
226
              left_id, right_id, ll.lat, ll.lng, rl.lat, rl.lng
227
                  ';
228
            $param = array_merge($show_status, $show_status, $device_ids, $device_ids);
229
        }
230
231
        foreach (dbFetchRows($sql, $param) as $link) {
232
            $icon = 'greenMarker';
233
            $z_offset = 0;
234
235
            $speed = $link['link_capacity'] / 1000000000;
236
            if ($speed > 500000) {
237
                $width = 20;
238
            } else {
239
                $width = round(0.77 * pow($speed, 0.25));
240
            }
241
242
            $link_used = max($link['link_out_usage_pct'], $link['link_in_usage_pct']);
243
            $link_used = round(2 * $link_used, -1) / 2;
244
            if ($link_used > 100) {
245
                $link_used = 100;
246
            }
247
            if (is_nan($link_used)) {
248
                $link_used = 0;
249
            }
250
            $link_color = Config::get("network_map_legend.$link_used");
251
252
            $temp_output .= 'var marker = new L.Polyline([new L.LatLng(' . $link['left_lat'] . ', ' . $link['left_lng'] . '), new L.LatLng(' . $link['right_lat'] . ', ' . $link['right_lng'] . ")], {
253
                color: '" . $link_color . "',
254
                weight: " . $width . ',
255
                opacity: 0.8,
256
                smoothFactor: 1
257
            });
258
            markers.addLayer(marker);
259
            ';
260
        }
261
    }
262
263
    $temp_output .= 'map.addLayer(markers);
264
map.scrollWheelZoom.disable();
265
$(document).ready(function(){
266
    $("#leaflet-map").on("click", function(event) {
267
        map.scrollWheelZoom.enable();
268
    });
269
    $("#leaflet-map").on("mouseleave", function(event) {
270
        map.scrollWheelZoom.disable();
271
    });
272
});
273
</script>';
274
} else {
275
    $temp_output = 'Mapael engine not supported here';
276
}
277
278
unset($common_output);
279
$common_output[] = $temp_output;
280