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/images.inc.php';
24
25$page['title'] = _('Configuration of images');
26$page['file'] = 'adm.images.php';
27
28require_once dirname(__FILE__).'/include/page_header.php';
29
30// VAR	TYPE	OPTIONAL	FLAGS	VALIDATION	EXCEPTION
31$fields = [
32	'imageid' =>	[T_ZBX_INT, O_NO,	P_SYS,		DB_ID,		'isset({form}) && {form} == "update"'],
33	'name' =>		[T_ZBX_STR, O_NO,	null,		NOT_EMPTY,	'isset({add}) || isset({update})'],
34	'imagetype' =>	[T_ZBX_INT, O_OPT, null,		IN('1,2'),	'isset({add}) || isset({update})'],
35	// actions
36	'add' =>		[T_ZBX_STR, O_OPT, P_SYS|P_ACT, null,		null],
37	'update' =>		[T_ZBX_STR, O_OPT, P_SYS|P_ACT, null,		null],
38	'delete' =>		[T_ZBX_STR, O_OPT, P_SYS|P_ACT, null,		null],
39	'form' =>		[T_ZBX_STR, O_OPT, P_SYS,		null,		null]
40];
41check_fields($fields);
42
43/*
44 * Permissions
45 */
46if (hasRequest('imageid')) {
47	$dbImage = DBfetch(DBselect('SELECT i.imagetype,i.name FROM images i WHERE i.imageid='.zbx_dbstr(getRequest('imageid'))));
48	if (empty($dbImage)) {
49		access_deny();
50	}
51}
52
53/*
54 * Actions
55 */
56if (hasRequest('add') || hasRequest('update')) {
57	if (hasRequest('update')) {
58		$msgOk = _('Image updated');
59		$msgFail = _('Cannot update image');
60	}
61	else {
62		$msgOk = _('Image added');
63		$msgFail = _('Cannot add image');
64	}
65
66	try {
67		DBstart();
68
69		if (isset($_FILES['image'])) {
70			$file = new CUploadFile($_FILES['image']);
71
72			$image = null;
73			if ($file->wasUploaded()) {
74				$file->validateImageSize();
75				$image = base64_encode($file->getContent());
76			}
77		}
78
79		if (hasRequest('update')) {
80			$result = API::Image()->update([
81				'imageid' => getRequest('imageid'),
82				'name' => getRequest('name'),
83				'image' => $image
84			]);
85
86			$audit_action = 'Image ['.getRequest('name').'] updated';
87		}
88		else {
89			$result = API::Image()->create([
90				'name' => $_REQUEST['name'],
91				'imagetype' => $_REQUEST['imagetype'],
92				'image' => $image
93			]);
94
95			$audit_action = 'Image ['.$_REQUEST['name'].'] added';
96		}
97
98		if ($result) {
99			add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_IMAGE, $audit_action);
100			unset($_REQUEST['form']);
101		}
102
103		$result = DBend($result);
104		show_messages($result, $msgOk, $msgFail);
105	}
106	catch (Exception $e) {
107		DBend(false);
108		error($e->getMessage());
109		show_error_message($msgFail);
110	}
111}
112elseif (isset($_REQUEST['delete']) && isset($_REQUEST['imageid'])) {
113	DBstart();
114
115	$image = get_image_by_imageid($_REQUEST['imageid']);
116	$result = API::Image()->delete([getRequest('imageid')]);
117
118	if ($result) {
119		add_audit(AUDIT_ACTION_UPDATE, AUDIT_RESOURCE_IMAGE, 'Image ['.$image['name'].'] deleted');
120		unset($_REQUEST['form'], $image, $_REQUEST['imageid']);
121	}
122
123	$result = DBend($result);
124	show_messages($result, _('Image deleted'), _('Cannot delete image'));
125}
126
127/*
128 * Display
129 */
130$data = [
131	'form' => getRequest('form')
132];
133
134if (!empty($data['form'])) {
135	if (isset($_REQUEST['imageid'])) {
136		$data['imageid'] = $_REQUEST['imageid'];
137		$data['imagename'] = $dbImage['name'];
138		$data['imagetype'] = $dbImage['imagetype'];
139	}
140	else {
141		$data['imageid'] = null;
142		$data['imagename'] = getRequest('name', '');
143		$data['imagetype'] = getRequest('imagetype', IMAGE_TYPE_ICON);
144	}
145
146	$view = new CView('administration.general.image.edit', $data);
147}
148else {
149	$data['imagetype'] = getRequest('imagetype', IMAGE_TYPE_ICON);
150
151	$data['images'] = API::Image()->get([
152		'filter' => ['imagetype' => $data['imagetype']],
153		'output' => ['imageid', 'imagetype', 'name']
154	]);
155	order_result($data['images'], 'name');
156
157	$view = new CView('administration.general.image.list', $data);
158}
159
160$view->render();
161$view->show();
162
163require_once dirname(__FILE__).'/include/page_footer.php';
164