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'] = 'chart6.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, P_NZERO,	BETWEEN(20, 65535),	null],
38	'height' =>			[T_ZBX_INT,			O_OPT, P_NZERO,	'{} > 0',	null],
39	'graph3d' =>		[T_ZBX_INT,			O_OPT, P_NZERO,	IN('0,1'),	null],
40	'legend' =>			[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null],
41	'widget_view' =>	[T_ZBX_INT,			O_OPT, null,	IN('0,1'),	null]
42];
43if (!check_fields($fields)) {
44	exit();
45}
46validateTimeSelectorPeriod(getRequest('from'), getRequest('to'));
47
48/*
49 * Permissions
50 */
51$dbGraph = API::Graph()->get([
52	'output' => API_OUTPUT_EXTEND,
53	'selectGraphItems' => ['itemid', 'sortorder', 'color', 'calc_fnc', 'type'],
54	'selectHosts' => ['name'],
55	'graphids' => $_REQUEST['graphid']
56]);
57
58if (!$dbGraph) {
59	access_deny();
60}
61else {
62	$dbGraph = reset($dbGraph);
63}
64
65/*
66 * Display
67 */
68$timeline = getTimeSelectorPeriod([
69	'profileIdx' => getRequest('profileIdx'),
70	'profileIdx2' => getRequest('profileIdx2'),
71	'from' => getRequest('from'),
72	'to' => getRequest('to')
73]);
74
75$graph = new CPieGraphDraw($dbGraph['graphtype']);
76$graph->setPeriod($timeline['to_ts'] - $timeline['from_ts']);
77$graph->setSTime($timeline['from_ts']);
78
79$width = getRequest('width', 0);
80if ($width <= 0) {
81	$width = $dbGraph['width'];
82}
83
84$height = getRequest('height', 0);
85if ($height <= 0) {
86	$height = $dbGraph['height'];
87}
88
89if (getRequest('widget_view') === '1') {
90	$graph->draw_header = false;
91	$graph->with_vertical_padding = false;
92}
93
94$graph->setWidth($width);
95$graph->setHeight($height);
96
97// array sorting
98CArrayHelper::sort($dbGraph['gitems'], [
99	['field' => 'sortorder', 'order' => ZBX_SORT_UP]
100]);
101
102// get graph items
103foreach ($dbGraph['gitems'] as $gItem) {
104	$graph->addItem(
105		$gItem['itemid'],
106		$gItem['calc_fnc'],
107		$gItem['color'],
108		$gItem['type']
109	);
110}
111
112$hostName = '';
113
114foreach ($dbGraph['hosts'] as $gItemHost) {
115	if ($hostName === '') {
116		$hostName = $gItemHost['name'];
117	}
118	elseif ($hostName !== $gItemHost['name']) {
119		$hostName = '';
120		break;
121	}
122}
123
124$graph->setHeader(($hostName === '') ? $dbGraph['name'] : $hostName.NAME_DELIMITER.$dbGraph['name']);
125
126if ($dbGraph['show_3d']) {
127	$graph->switchPie3D();
128}
129$graph->showLegend(getRequest('legend', $dbGraph['show_legend']));
130$graph->draw();
131
132require_once dirname(__FILE__).'/include/page_footer.php';
133