1<?php
2/*
3** Zabbix
4** Copyright (C) 2001-2021 Zabbix SIA
5**
6** This program is free software; you can redistribute it and/or modify
7** it under the terms of the GNU General Public License as published by
8** the Free Software Foundation; either version 2 of the License, or
9** (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** You should have received a copy of the GNU General Public License
17** along with this program; if not, write to the Free Software
18** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19**/
20
21
22require_once dirname(__FILE__).'/include/config.inc.php';
23require_once dirname(__FILE__).'/include/graphs.inc.php';
24
25$page['file'] = 'chart2.php';
26$page['type'] = PAGE_TYPE_IMAGE;
27
28require_once dirname(__FILE__).'/include/page_header.php';
29
30// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
31$fields = [
32	'graphid' =>		[T_ZBX_INT,			O_MAND, P_SYS,	DB_ID,		null],
33	'from' =>			[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,		null],
34	'to' =>				[T_ZBX_RANGE_TIME,	O_OPT, P_SYS,	null,		null],
35	'profileIdx' =>		[T_ZBX_STR,			O_OPT, null,	null,		null],
36	'profileIdx2' =>	[T_ZBX_STR,			O_OPT, null,	null,		null],
37	'width' =>			[T_ZBX_INT,			O_OPT, null,	BETWEEN(CLineGraphDraw::GRAPH_WIDTH_MIN, 65535),	null],
38	'height' =>			[T_ZBX_INT,			O_OPT, null,	BETWEEN(CLineGraphDraw::GRAPH_HEIGHT_MIN, 65535),	null],
39	'outer' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
40	'onlyHeight' =>		[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
41	'legend' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
42	'widget_view' =>	[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null]
43];
44if (!check_fields($fields)) {
45	exit();
46}
47validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
48
49/*
50 * Permissions
51 */
52$dbGraph = API::Graph()->get([
53	'output' => API_OUTPUT_EXTEND,
54	'selectGraphItems' => API_OUTPUT_EXTEND,
55	'selectHosts' => ['hostid', 'name', 'host'],
56	'selectItems' => ['itemid', 'type', 'master_itemid', 'name', 'delay', 'units', 'hostid', 'history', 'trends',
57		'value_type', 'key_'
58	],
59	'graphids' => $_REQUEST['graphid']
60]);
61
62if (!$dbGraph) {
63	access_deny();
64}
65else {
66	$dbGraph = reset($dbGraph);
67}
68
69/*
70 * Display
71 */
72$timeline = getTimeSelectorPeriod([
73	'profileIdx' => getRequest('profileIdx'),
74	'profileIdx2' => getRequest('profileIdx2'),
75	'from' => getRequest('from'),
76	'to' => getRequest('to')
77]);
78
79$graph = new CLineGraphDraw($dbGraph['graphtype']);
80
81if (getRequest('widget_view') === '1') {
82	$graph->draw_header = false;
83	$graph->with_vertical_padding = false;
84}
85
86// array sorting
87CArrayHelper::sort($dbGraph['gitems'], [
88	['field' => 'sortorder', 'order' => ZBX_SORT_UP],
89	['field' => 'itemid', 'order' => ZBX_SORT_DOWN]
90]);
91
92$hosts = zbx_toHash($dbGraph['hosts'], 'hostid');
93$items = zbx_toHash($dbGraph['items'], 'itemid');
94
95foreach ($dbGraph['gitems'] as $graph_item) {
96	$item = $items[$graph_item['itemid']];
97	$host = $hosts[$item['hostid']];
98
99	$graph->addItem($item + [
100		'host' => $host['host'],
101		'hostname' => $host['name'],
102		'color' => $graph_item['color'],
103		'drawtype' => $graph_item['drawtype'],
104		'yaxisside' => $graph_item['yaxisside'],
105		'calc_fnc' => $graph_item['calc_fnc']
106	]);
107}
108
109$hostName = '';
110
111foreach ($dbGraph['hosts'] as $gItemHost) {
112	if ($hostName === '') {
113		$hostName = $gItemHost['name'];
114	}
115	elseif ($hostName !== $gItemHost['name']) {
116		$hostName = '';
117		break;
118	}
119}
120
121$graph->setHeader(($hostName === '') ? $dbGraph['name'] : $hostName.NAME_DELIMITER.$dbGraph['name']);
122$graph->setPeriod($timeline['to_ts'] - $timeline['from_ts']);
123$graph->setSTime($timeline['from_ts']);
124
125$width = getRequest('width', 0);
126if ($width <= 0) {
127	$width = $dbGraph['width'];
128}
129
130$height = getRequest('height', 0);
131if ($height <= 0) {
132	$height = $dbGraph['height'];
133}
134
135$graph->showLegend(getRequest('legend', $dbGraph['show_legend']));
136$graph->showWorkPeriod($dbGraph['show_work_period']);
137$graph->showTriggers($dbGraph['show_triggers']);
138$graph->setWidth($width);
139$graph->setHeight($height);
140$graph->setYMinAxisType($dbGraph['ymin_type']);
141$graph->setYMaxAxisType($dbGraph['ymax_type']);
142$graph->setYAxisMin($dbGraph['yaxismin']);
143$graph->setYAxisMax($dbGraph['yaxismax']);
144$graph->setYMinItemId($dbGraph['ymin_itemid']);
145$graph->setYMaxItemId($dbGraph['ymax_itemid']);
146$graph->setLeftPercentage($dbGraph['percent_left']);
147$graph->setRightPercentage($dbGraph['percent_right']);
148
149if (hasRequest('outer')) {
150	$graph->setOuter(getRequest('outer'));
151}
152
153$min_dimentions = $graph->getMinDimensions();
154if ($min_dimentions['width'] > $graph->getWidth()) {
155	$graph->setWidth($min_dimentions['width']);
156}
157if ($min_dimentions['height'] > $graph->getHeight()) {
158	$graph->setHeight($min_dimentions['height']);
159}
160
161if (getRequest('onlyHeight', '0') === '1') {
162	$graph->drawDimensions();
163	header('X-ZBX-SBOX-HEIGHT: '.($graph->getHeight() + 1));
164}
165else {
166	$graph->draw();
167}
168
169require_once dirname(__FILE__).'/include/page_footer.php';
170