1<?php
2/*
3 +-------------------------------------------------------------------------+
4 | Copyright (C) 2004-2021 The Cacti Group                                 |
5 |                                                                         |
6 | This program is free software; you can redistribute it and/or           |
7 | modify it under the terms of the GNU General Public License             |
8 | as published by the Free Software Foundation; either version 2          |
9 | of the License, or (at your option) any later version.                  |
10 |                                                                         |
11 | This program is distributed in the hope that it will be useful,         |
12 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
14 | GNU General Public License for more details.                            |
15 +-------------------------------------------------------------------------+
16 | Cacti: The Complete RRDtool-based Graphing Solution                     |
17 +-------------------------------------------------------------------------+
18 | This code is designed, written, and maintained by the Cacti Group. See  |
19 | about.php and/or the AUTHORS file for specific developer information.   |
20 +-------------------------------------------------------------------------+
21 | http://www.cacti.net/                                                   |
22 +-------------------------------------------------------------------------+
23*/
24
25/* since we'll have additional headers, tell php when to flush them */
26ob_start();
27
28$guest_account = true;
29$auth_text     = true;
30$gtype = 'png';
31
32include('./include/auth.php');
33include_once('./lib/rrd.php');
34
35/* ================= input validation ================= */
36get_filter_request_var('graph_start');
37get_filter_request_var('graph_end');
38get_filter_request_var('graph_height');
39get_filter_request_var('graph_width');
40get_filter_request_var('local_graph_id');
41
42if (isset_request_var('graph_nolegend')) {
43	set_request_var('graph_nolegend', 'true');
44}
45
46get_filter_request_var('graph_theme', FILTER_CALLBACK, array('options' => 'sanitize_search_string'));
47/* ==================================================== */
48
49api_plugin_hook_function('graph_image');
50
51$graph_data_array = array();
52
53// Determine the graph type of the output
54if (!isset_request_var('image_format')) {
55	$type   = db_fetch_cell_prepared('SELECT image_format_id FROM graph_templates_graph WHERE local_graph_id = ?', array(get_request_var('local_graph_id')));
56	switch($type) {
57	case '1':
58		$gtype = 'png';
59		break;
60	case '3':
61		$gtype = 'svg+xml';
62		break;
63	}
64} else {
65	switch(strtolower(get_nfilter_request_var('image_format'))) {
66	case 'png':
67		$gtype = 'png';
68		break;
69	case 'svg':
70		$gtype = 'svg+xml';
71		break;
72	default:
73		$gtype = 'png';
74		break;
75	}
76}
77
78$graph_data_array['image_format'] = $gtype;
79
80cacti_session_close();
81
82/* override: graph start time (unix time) */
83if (!isempty_request_var('graph_start') && get_request_var('graph_start') < FILTER_VALIDATE_MAX_DATE_AS_INT) {
84	$graph_data_array['graph_start'] = get_request_var('graph_start');
85}
86
87/* override: graph end time (unix time) */
88if (!isempty_request_var('graph_end') && get_request_var('graph_end') < FILTER_VALIDATE_MAX_DATE_AS_INT) {
89	$graph_data_array['graph_end'] = get_request_var('graph_end');
90}
91
92/* override: graph height (in pixels) */
93if (!isempty_request_var('graph_height') && get_request_var('graph_height') < 3000) {
94	$graph_data_array['graph_height'] = get_request_var('graph_height');
95}
96
97/* override: graph width (in pixels) */
98if (!isempty_request_var('graph_width') && get_request_var('graph_width') < 3000) {
99	$graph_data_array['graph_width'] = get_request_var('graph_width');
100}
101
102/* override: skip drawing the legend? */
103if (!isempty_request_var('graph_nolegend')) {
104	$graph_data_array['graph_nolegend'] = get_request_var('graph_nolegend');
105}
106
107/* print RRDtool graph source? */
108if (!isempty_request_var('show_source')) {
109	$graph_data_array['print_source'] = get_request_var('show_source');
110}
111
112/* disable cache check */
113if (isset_request_var('disable_cache')) {
114	$graph_data_array['disable_cache'] = true;
115}
116
117/* set the theme */
118if (isset_request_var('graph_theme')) {
119	$graph_data_array['graph_theme'] = get_request_var('graph_theme');
120}
121
122if (isset_request_var('rra_id')) {
123	if (get_nfilter_request_var('rra_id') == 'all') {
124		$rra_id = 'all';
125	} else {
126		$rra_id = get_filter_request_var('rra_id');
127	}
128} else {
129	$rra_id = null;
130}
131
132$null_param = array();
133$output = rrdtool_function_graph(get_request_var('local_graph_id'), $rra_id, $graph_data_array, '', $null_param, $_SESSION['sess_user_id']);
134
135if ($output !== false && $output != '') {
136	/* flush the headers now */
137	ob_end_clean();
138
139	header('Content-type: image/'. $gtype);
140	header('Cache-Control: max-age=15');
141
142	print $output;
143} else {
144	ob_start();
145
146	/* get the error string */
147	$graph_data_array['get_error'] = true;
148	$null_param = array();
149	rrdtool_function_graph(get_request_var('local_graph_id'), $rra_id, $graph_data_array, '', $null_param, $_SESSION['sess_user_id']);
150
151	$error = ob_get_contents();
152
153	if (read_config_option('stats_poller') == '') {
154		$error = __('The Cacti Poller has not run yet.');
155	}
156
157	if (isset($graph_data_array['graph_width']) && isset($graph_data_array['graph_height'])) {
158		$image = rrdtool_create_error_image($error, $graph_data_array['graph_width'], $graph_data_array['graph_height']);
159	} else {
160		$image = rrdtool_create_error_image($error);
161	}
162
163	ob_end_clean();
164
165	header('Content-type: image/png');
166	header('Cache-Control: max-age=15');
167
168	if ($image !== false) {
169		print $image;
170	} else {
171		print file_get_contents(__DIR__ . '/images/cacti_error_image.png');
172	}
173}
174
175