Issues (2963)

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

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
global $debug;
6
7
// Push $_GET into $vars to be compatible with web interface naming
8
foreach ($_GET as $name => $value) {
9
    $vars[$name] = $value;
10
}
11
12
[$type, $subtype] = extract_graph_type($vars['type']);
13
14
if (is_numeric($vars['device'])) {
15
    $device = device_by_id_cache($vars['device']);
16
} elseif (! empty($vars['device'])) {
17
    $device = device_by_name($vars['device']);
18
}
19
20
// FIXME -- remove these
21
$width = $vars['width'];
22
$height = $vars['height'];
23
$title = $vars['title'];
24
$vertical = $vars['vertical'];
25
$legend = $vars['legend'];
26
$output = (! empty($vars['output']) ? $vars['output'] : 'default');
27
$from = parse_at_time($_GET['from']) ?: Config::get('time.day');
28
$to = parse_at_time($_GET['to']) ?: Config::get('time.now');
29
$period = ($to - $from);
30
$prev_from = ($from - $period);
31
32
$graph_image_type = $vars['graph_type'] ?? Config::get('webui.graph_type');
33
$rrd_options = '';
34
35
require Config::get('install_dir') . "/includes/html/graphs/$type/auth.inc.php";
36
37
//set default graph title
38
$graph_title = format_hostname($device);
39
40
if ($auth && is_customoid_graph($type, $subtype)) {
41
    $unit = $vars['unit'];
42
    include Config::get('install_dir') . '/includes/html/graphs/customoid/customoid.inc.php';
43
} elseif ($auth && is_file(Config::get('install_dir') . "/includes/html/graphs/$type/$subtype.inc.php")) {
44
    include Config::get('install_dir') . "/includes/html/graphs/$type/$subtype.inc.php";
45
} else {
46
    graph_error("$type*$subtype ");
47
    // Graph Template Missing");
48
}
49
50
if (! empty($error_msg)) {
51
    // We have an error :(
52
    graph_error($error_msg);
53
54
    return;
55
}
56
57
if ($auth === null) {
58
    // We are unauthenticated :(
59
    graph_error($width < 200 ? 'No Auth' : 'No Authorization');
60
61
    return;
62
}
63
64
if ($graph_image_type === 'svg') {
65
    $rrd_options .= ' --imgformat=SVG';
66
    if ($width < 350) {
67
        $rrd_options .= ' -m 0.75 -R light';
68
    }
69
}
70
71
// command output requested
72
if (! empty($command_only)) {
73
    echo "<div class='infobox'>";
74
    echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Command</p>";
75
    echo "<pre class='rrd-pre'>";
76
    echo escapeshellcmd('rrdtool ' . Rrd::buildCommand('graph', Config::get('temp_dir') . '/' . strgen(), $rrd_options));
77
    echo '</pre>';
78
    try {
79
        Rrd::graph($rrd_options);
80
    } catch (\LibreNMS\Exceptions\RrdGraphException $e) {
81
        echo "<p style='font-size: 16px; font-weight: bold;'>RRDTool Output</p>";
82
        echo "<pre class='rrd-pre'>";
83
        echo $e->getMessage();
84
        echo '</pre>';
85
    }
86
    echo '</div>';
87
88
    return;
89
}
90
91
// graph sent file not found flag
92
if (! empty($no_file)) {
93
    graph_error($width < 200 ? 'No Data' : 'No Data file ' . $no_file);
94
95
    return;
96
}
97
98
if (empty($rrd_options)) {
99
    graph_error($width < 200 ? 'Def Error' : 'Graph Definition Error');
100
101
    return;
102
}
103
104
// Generating the graph!
105
try {
106
    $image_data = Rrd::graph($rrd_options);
107
108
    // output the graph
109
    if (\LibreNMS\Util\Debug::isEnabled()) {
110
        echo '<img src="data:' . get_image_type($graph_image_type) . ';base64,' . base64_encode($image_data) . '" alt="graph" />';
111
    } else {
112
        header('Content-type: ' . get_image_type(Config::get('webui.graph_type')));
113
        echo $output === 'base64' ? base64_encode($image_data) : $image_data;
114
    }
115
} catch (\LibreNMS\Exceptions\RrdGraphException $e) {
116
    if (isset($rrd_filename) && ! Rrd::checkRrdExists($rrd_filename)) {
117
        graph_error($width < 200 ? 'No Data' : 'No Data file ' . basename($rrd_filename));
118
    } else {
119
        graph_error($width < 200 ? 'Draw Error' : 'Error Drawing Graph: ' . $e->getMessage());
120
    }
121
}
122