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';
23
24$page['file'] = 'conf.import.php';
25$page['title'] = _('Configuration import');
26$page['type'] = detect_page_type(PAGE_TYPE_HTML);
27
28ob_start();
29
30require_once dirname(__FILE__).'/include/page_header.php';
31
32$fields = [
33	'rules' => [T_ZBX_STR, O_OPT, null, null, null],
34	'import' => [T_ZBX_STR, O_OPT, P_SYS|P_ACT, null, null],
35	'rules_preset' => [T_ZBX_STR, O_OPT, null, null, null],
36	'backurl' => [T_ZBX_STR, O_OPT, null, null, null],
37	'cancel' => [T_ZBX_STR, O_OPT, P_SYS, null, null]
38];
39check_fields($fields);
40
41
42if (isset($_REQUEST['cancel'])) {
43	ob_end_clean();
44	redirect(CWebUser::$data['last_page']['url']);
45}
46ob_end_flush();
47
48$data = [
49	'rules' => [
50		'groups' => ['createMissing' => false],
51		'hosts' => ['updateExisting' => false, 'createMissing' => false],
52		'templates' => ['updateExisting' => false, 'createMissing' => false],
53		'templateScreens' => ['updateExisting' => false, 'createMissing' => false, 'deleteMissing' => false],
54		'templateLinkage' => ['createMissing' => false],
55		'applications' => ['createMissing' => false, 'deleteMissing' => false],
56		'items' => ['updateExisting' => false, 'createMissing' => false, 'deleteMissing' => false],
57		'discoveryRules' => ['updateExisting' => false, 'createMissing' => false, 'deleteMissing' => false],
58		'triggers' => ['updateExisting' => false, 'createMissing' => false, 'deleteMissing' => false],
59		'graphs' => ['updateExisting' => false, 'createMissing' => false, 'deleteMissing' => false],
60		'screens' => ['updateExisting' => false, 'createMissing' => false],
61		'maps' => ['updateExisting' => false, 'createMissing' => false],
62		'images' => ['updateExisting' => false, 'createMissing' => false],
63		'valueMaps' => ['updateExisting' => false, 'createMissing' => false]
64	],
65	'backurl' => getRequest('backurl', '')
66];
67
68if (!CHtmlUrlValidator::validate($data['backurl'], false, true)) {
69	$data['backurl'] = 'zabbix.php?action=dashboard.view';
70}
71
72// rules presets
73if (hasRequest('rules_preset') && !hasRequest('rules')) {
74	switch (getRequest('rules_preset')) {
75		case 'host':
76			$data['rules']['groups'] = ['createMissing' => true];
77			$data['rules']['hosts'] = ['updateExisting' => true, 'createMissing' => true];
78			$data['rules']['applications'] = ['createMissing' => true, 'deleteMissing' => false];
79			$data['rules']['items'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
80			$data['rules']['discoveryRules'] = ['updateExisting' => true, 'createMissing' => true,
81				'deleteMissing' => false
82			];
83			$data['rules']['triggers'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
84			$data['rules']['graphs'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
85			$data['rules']['templateLinkage'] = ['createMissing' => true];
86			$data['rules']['valueMaps'] = ['updateExisting' => false, 'createMissing' => true];
87
88			$data['backurl'] = 'hosts.php';
89			break;
90
91		case 'template':
92			$data['rules']['groups'] = ['createMissing' => true];
93			$data['rules']['templates'] = ['updateExisting' => true, 'createMissing' => true];
94			$data['rules']['templateScreens'] = ['updateExisting' => true, 'createMissing' => true,
95				'deleteMissing' => false
96			];
97			$data['rules']['applications'] = ['createMissing' => true, 'deleteMissing' => false];
98			$data['rules']['items'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
99			$data['rules']['discoveryRules'] = ['updateExisting' => true, 'createMissing' => true,
100				'deleteMissing' => false
101			];
102			$data['rules']['triggers'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
103			$data['rules']['graphs'] = ['updateExisting' => true, 'createMissing' => true, 'deleteMissing' => false];
104			$data['rules']['templateLinkage'] = ['createMissing' => true];
105			$data['rules']['valueMaps'] = ['updateExisting' => false, 'createMissing' => true];
106
107			$data['backurl'] = 'templates.php';
108			break;
109
110		case 'map':
111			$data['rules']['maps'] = ['updateExisting' => true, 'createMissing' => true];
112
113			$data['backurl'] = 'sysmaps.php';
114			break;
115
116		case 'screen':
117			$data['rules']['screens'] = ['updateExisting' => true, 'createMissing' => true];
118
119			$data['backurl'] = 'screenconf.php';
120			break;
121
122		case 'valuemap':
123			$data['rules']['valueMaps'] = ['updateExisting' => false, 'createMissing' => true];
124
125			$data['backurl'] = 'adm.valuemapping.php';
126			break;
127
128	}
129}
130
131if (hasRequest('rules')) {
132	$requestRules = getRequest('rules', []);
133	// if form was submitted with some checkboxes unchecked, those values are not submitted
134	// so that we set missing values to false
135	foreach ($data['rules'] as $ruleName => $rule) {
136		if (!array_key_exists($ruleName, $requestRules)) {
137			if (array_key_exists('updateExisting', $rule)) {
138				$requestRules[$ruleName]['updateExisting'] = false;
139			}
140
141			if (array_key_exists('createMissing', $rule)) {
142				$requestRules[$ruleName]['createMissing'] = false;
143			}
144
145			if (array_key_exists('deleteMissing', $rule)) {
146				$requestRules[$ruleName]['deleteMissing'] = false;
147			}
148		}
149
150		if (!isset($requestRules[$ruleName]['updateExisting']) && isset($rule['updateExisting'])) {
151			$requestRules[$ruleName]['updateExisting'] = false;
152		}
153
154		if (!isset($requestRules[$ruleName]['createMissing']) && isset($rule['createMissing'])) {
155			$requestRules[$ruleName]['createMissing'] = false;
156		}
157
158		if (!isset($requestRules[$ruleName]['deleteMissing']) && isset($rule['deleteMissing'])) {
159			$requestRules[$ruleName]['deleteMissing'] = false;
160		}
161	}
162
163	$data['rules'] = $requestRules;
164}
165
166if (isset($_FILES['import_file'])) {
167	$result = false;
168
169	// CUploadFile throws exceptions, so we need to catch them
170	try {
171		$file = new CUploadFile($_FILES['import_file']);
172
173		$result = API::Configuration()->import([
174			'format' => CImportReaderFactory::fileExt2ImportFormat($file->getExtension()),
175			'source' => $file->getContent(),
176			'rules' => $data['rules']
177		]);
178	}
179	catch (Exception $e) {
180		error($e->getMessage());
181	}
182
183	show_messages($result, _('Imported successfully'), _('Import failed'));
184}
185
186$view = new CView('conf.import', $data);
187$view->render();
188$view->show();
189
190require_once dirname(__FILE__).'/include/page_footer.php';
191