Issues (2963)

includes/html/pages/graphs.inc.php (1 issue)

Labels
Severity
1
<?php
2
3
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...
4
5
unset($vars['page']);
6
7
// Setup here
8
9
if (session('widescreen')) {
10
    $graph_width = 1700;
11
    $thumb_width = 180;
12
} else {
13
    $graph_width = 1075;
14
    $thumb_width = 113;
15
}
16
17
$vars['from'] = parse_at_time($vars['from']) ?: Config::get('time.day');
18
$vars['to'] = parse_at_time($vars['to']) ?: Config::get('time.now');
19
20
preg_match('/^(?P<type>[A-Za-z0-9]+)_(?P<subtype>.+)/', $vars['type'], $graphtype);
21
22
$type = basename($graphtype['type']);
23
$subtype = basename($graphtype['subtype']);
24
$id = $vars['id'];
25
26
if (is_numeric($vars['device'])) {
27
    $device = device_by_id_cache($vars['device']);
28
} elseif (! empty($vars['device'])) {
29
    $device = device_by_name($vars['device']);
30
}
31
32
if (is_file('includes/html/graphs/' . $type . '/auth.inc.php')) {
33
    require 'includes/html/graphs/' . $type . '/auth.inc.php';
34
}
35
36
if (! $auth) {
37
    require 'includes/html/error-no-perm.inc.php';
38
} else {
39
    if (Config::has("graph_types.$type.$subtype.descr")) {
40
        $title .= ' :: ' . Config::get("graph_types.$type.$subtype.descr");
41
    } elseif ($type == 'device' && $subtype == 'collectd') {
42
        $title .= ' :: ' . \LibreNMS\Util\StringHelpers::niceCase($subtype) . ' :: ' . $vars['c_plugin'];
43
        if (isset($vars['c_plugin_instance'])) {
44
            $title .= ' - ' . $vars['c_plugin_instance'];
45
        }
46
        $title .= ' - ' . $vars['c_type'];
47
        if (isset($vars['c_type_instance'])) {
48
            $title .= ' - ' . $vars['c_type_instance'];
49
        }
50
    } else {
51
        $title .= ' :: ' . \LibreNMS\Util\StringHelpers::niceCase($subtype);
52
    }
53
54
    $graph_array = $vars;
55
    $graph_array['height'] = '60';
56
    $graph_array['width'] = $thumb_width;
57
    $graph_array['legend'] = 'no';
58
    $graph_array['to'] = Config::get('time.now');
59
60
    print_optionbar_start();
61
    echo $title;
62
63
    // FIXME allow switching between types for sensor and wireless also restrict types to ones that have data
64
    if ($type != 'sensor') {
65
        echo '<div style="float: right;"><form action="">';
66
        echo csrf_field();
67
        echo "<select name='type' id='type' onchange=\"window.open(this.options[this.selectedIndex].value,'_top')\" >";
68
69
        foreach (get_graph_subtypes($type, $device) as $avail_type) {
70
            echo "<option value='" . \LibreNMS\Util\Url::generate($vars, ['type' => $type . '_' . $avail_type, 'page' => 'graphs']) . "'";
71
            if ($avail_type == $subtype) {
72
                echo ' selected';
73
            }
74
            $display_type = \LibreNMS\Util\StringHelpers::niceCase($avail_type);
75
            echo ">$display_type</option>";
76
        }
77
        echo '</select></form></div>';
78
    }
79
80
    print_optionbar_end();
81
82
    $thumb_array = Config::get('graphs.row.normal');
83
84
    echo '<table width=100% class="thumbnail_graph_table"><tr>';
85
86
    foreach ($thumb_array as $period => $text) {
87
        $graph_array['from'] = Config::get("time.$period");
88
89
        $link_array = $vars;
90
        $link_array['from'] = $graph_array['from'];
91
        $link_array['to'] = $graph_array['to'];
92
        $link_array['page'] = 'graphs';
93
        $link = \LibreNMS\Util\Url::generate($link_array);
94
95
        echo '<td style="text-align: center;">';
96
        echo '<b>' . $text . '</b>';
97
        echo '<a href="' . $link . '">';
98
        echo \LibreNMS\Util\Url::lazyGraphTag($graph_array);
99
        echo '</a>';
100
        echo '</td>';
101
    }
102
103
    echo '</tr></table>';
104
105
    $graph_array = $vars;
106
    $graph_array['height'] = Config::get('webui.min_graph_height');
107
    $graph_array['width'] = $graph_width;
108
109
    if ($screen_width = Session::get('screen_width')) {
110
        if ($screen_width > 800) {
111
            $graph_array['width'] = ($screen_width - ($screen_width / 10));
112
        } else {
113
            $graph_array['width'] = ($screen_width - ($screen_width / 4));
114
        }
115
    }
116
117
    if ($screen_height = Session::get('screen_height')) {
118
        if ($screen_height > 960) {
119
            $graph_array['height'] = ($screen_height - ($screen_height / 2));
120
        } else {
121
            $graph_array['height'] = max($graph_array['height'], ($screen_height - ($screen_height / 1.5)));
122
        }
123
    }
124
125
    echo '<hr />';
126
127
    include_once 'includes/html/print-date-selector.inc.php';
128
129
    echo '<div style="padding-top: 5px";></div>';
130
    echo '<center>';
131
    if ($vars['legend'] == 'no') {
132
        echo generate_link('Show Legend', $vars, ['page' => 'graphs', 'legend' => null]);
133
    } else {
134
        echo generate_link('Hide Legend', $vars, ['page' => 'graphs', 'legend' => 'no']);
135
    }
136
137
    // FIXME : do this properly
138
    //  if ($type == "port" && $subtype == "bits")
139
    //  {
140
    echo ' | ';
141
    if ($vars['previous'] == 'yes') {
142
        echo generate_link('Hide Previous', $vars, ['page' => 'graphs', 'previous' => null]);
143
    } else {
144
        echo generate_link('Show Previous', $vars, ['page' => 'graphs', 'previous' => 'yes']);
145
    }
146
    //  }
147
148
    echo ' | ';
149
    if ($vars['showcommand'] == 'yes') {
150
        echo generate_link('Hide RRD Command', $vars, ['page' => 'graphs', 'showcommand' => null]);
151
    } else {
152
        echo generate_link('Show RRD Command', $vars, ['page' => 'graphs', 'showcommand' => 'yes']);
153
    }
154
155
    if ($vars['type'] == 'port_bits') {
156
        echo ' | ';
157
        if ($vars['port_speed_zoom'] ?? Config::get('graphs.port_speed_zoom')) {
158
            echo generate_link('Zoom to Traffic', $vars, ['page' => 'graphs', 'port_speed_zoom' => 0]);
159
        } else {
160
            echo generate_link('Zoom to Port Speed', $vars, ['page' => 'graphs', 'port_speed_zoom' => 1]);
161
        }
162
        echo ' | To show trend, set to future date';
163
    }
164
165
    echo '</center>';
166
167
    echo generate_graph_js_state($graph_array);
168
169
    echo '<div style="width: ' . $graph_array['width'] . '; margin: auto;"><center>';
170
    if (Config::get('webui.dynamic_graphs', false) === true) {
171
        echo generate_dynamic_graph_js($graph_array);
172
        echo generate_dynamic_graph_tag($graph_array);
173
    } else {
174
        echo \LibreNMS\Util\Url::lazyGraphTag($graph_array);
175
    }
176
    echo '</center></div>';
177
178
    if (Config::has('graph_descr.' . $vars['type'])) {
179
        print_optionbar_start();
180
        echo '<div style="float: left; width: 30px;">
181
            <div style="margin: auto auto;">
182
            <i class="fa fa-info-circle fa-lg icon-theme" aria-hidden="true"></i>
183
            </div>
184
            </div>';
185
        echo Config::get('graph_descr.' . $vars['type']);
186
        print_optionbar_end();
187
    }
188
189
    if ($vars['showcommand']) {
190
        $_GET = $graph_array;
191
        $command_only = 1;
192
193
        require 'includes/html/graphs/graph.inc.php';
194
    }
195
}
196