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
22/**
23 * Converter for converting import data from 3.2 to 3.4
24 */
25class C32ImportConverter extends CConverter {
26
27	public function convert($data) {
28		$data['zabbix_export']['version'] = '3.4';
29
30		if (array_key_exists('hosts', $data['zabbix_export'])) {
31			$data['zabbix_export']['hosts'] = $this->convertHosts($data['zabbix_export']['hosts']);
32		}
33		if (array_key_exists('templates', $data['zabbix_export'])) {
34			$data['zabbix_export']['templates'] = $this->convertHosts($data['zabbix_export']['templates']);
35		}
36		if (array_key_exists('maps', $data['zabbix_export'])) {
37			$data['zabbix_export']['maps'] = $this->convertMaps($data['zabbix_export']['maps']);
38		}
39
40		return $data;
41	}
42
43	/**
44	 * Convert hosts.
45	 *
46	 * @param array $hosts
47	 *
48	 * @return array
49	 */
50	protected function convertHosts(array $hosts) {
51		foreach ($hosts as &$host) {
52			if (array_key_exists('discovery_rules', $host)) {
53				$host['discovery_rules'] = $this->convertDiscoveryRules($host['discovery_rules']);
54			}
55			if (array_key_exists('items', $host)) {
56				$host['items'] = $this->convertItems($host['items'], [
57					'master_item' => []
58				]);
59			}
60			if (array_key_exists('httptests', $host)) {
61				$host['httptests'] = $this->convertHttpTests($host['httptests']);
62			}
63		}
64		unset($host);
65
66		return $hosts;
67	}
68
69	/**
70	 * Convert item elements.
71	 *
72	 * @param array $items
73	 * @param array $default_fields     Default values to add to every item.
74	 *
75	 * @return array
76	 */
77	protected function convertItems(array $items,array $default_fields) {
78		foreach ($items as &$item) {
79			// Item preprocessing.
80			$item['preprocessing'] = [];
81
82			if ($item['data_type'] != ITEM_DATA_TYPE_DECIMAL) {
83				switch ($item['data_type']) {
84					case ITEM_DATA_TYPE_OCTAL:
85						$type = ZBX_PREPROC_OCT2DEC;
86						break;
87					case ITEM_DATA_TYPE_HEXADECIMAL:
88						$type = ZBX_PREPROC_HEX2DEC;
89						break;
90					case ITEM_DATA_TYPE_BOOLEAN:
91						$type = ZBX_PREPROC_BOOL2DEC;
92						break;
93				}
94
95				$item['preprocessing'][] = ['type' => $type, 'params' => ''];
96			}
97
98			if ($item['delta'] == 1) {
99				$item['preprocessing'][] = ['type' => ZBX_PREPROC_DELTA_SPEED, 'params' => ''];
100			}
101			elseif ($item['delta'] == 2) {
102				$item['preprocessing'][] = ['type' => ZBX_PREPROC_DELTA_VALUE, 'params' => ''];
103			}
104
105			if ($item['multiplier'] == 1) {
106				$item['preprocessing'][] = ['type' => ZBX_PREPROC_MULTIPLIER, 'params' => $item['formula']];
107			}
108
109			unset($item['data_type'], $item['delta'], $item['multiplier'], $item['formula']);
110
111			if (!$item['preprocessing']) {
112				unset($item['preprocessing']);
113			}
114
115			// Merge delay_flex into delay separated by a semicolon.
116			$item['delay'] = (string) $item['delay'];
117			if ($item['delay_flex'] !== '') {
118				$item['delay'] .= ';'.$item['delay_flex'];
119			}
120			unset($item['delay_flex']);
121
122			// Convert to days.
123			$item['history'] = (string) $item['history'];
124			if ($item['history'] != 0) {
125				$item['history'] .= 'd';
126			}
127			$item['trends'] = (string) $item['trends'];
128			if ($item['trends'] != 0) {
129				$item['trends'] .= 'd';
130			}
131
132			$item['jmx_endpoint'] = ($item['type'] == ITEM_TYPE_JMX) ? ZBX_DEFAULT_JMX_ENDPOINT : '';
133
134			$item = $item + $default_fields;
135		}
136		unset($item);
137
138		return $items;
139	}
140
141	/**
142	 * Convert discovery rule elements.
143	 *
144	 * @param array $discovery_rules
145	 *
146	 * @return array
147	 */
148	protected function convertDiscoveryRules(array $discovery_rules) {
149		foreach ($discovery_rules as &$discovery_rule) {
150			$discovery_rule['item_prototypes'] = $this->convertItems($discovery_rule['item_prototypes'], [
151				'master_item_prototype'	=> []
152			]);
153			$discovery_rule['jmx_endpoint'] = ($discovery_rule['type'] == ITEM_TYPE_JMX)
154				? ZBX_DEFAULT_JMX_ENDPOINT
155				: '';
156
157			// Merge delay_flex into delay separated by a semicolon.
158			$discovery_rule['delay'] = (string) $discovery_rule['delay'];
159			if ($discovery_rule['delay_flex'] !== '') {
160				$discovery_rule['delay'] .= ';'.$discovery_rule['delay_flex'];
161			}
162			unset($discovery_rule['delay_flex']);
163
164			// Convert to days.
165			if (ctype_digit($discovery_rule['lifetime']) && $discovery_rule['lifetime'] != 0) {
166				$discovery_rule['lifetime'] .= 'd';
167			}
168		}
169		unset($discovery_rule);
170
171		return $discovery_rules;
172	}
173
174	/**
175	 * Convert maps.
176	 *
177	 * @param array $maps
178	 *
179	 * @return array
180	 */
181	protected function convertMaps(array $maps) {
182		$default_shape = [
183			'type' => SYSMAP_SHAPE_TYPE_RECTANGLE,
184			'x' => DB::getDefault('sysmap_shape', 'x'),
185			'y' => DB::getDefault('sysmap_shape', 'y'),
186			'height' => 15,
187			'text' => '{MAP.NAME}',
188			'font' => DB::getDefault('sysmap_shape', 'font'),
189			'font_size' => DB::getDefault('sysmap_shape', 'font_size'),
190			'font_color' => DB::getDefault('sysmap_shape', 'font_color'),
191			'text_halign' => DB::getDefault('sysmap_shape', 'text_halign'),
192			'text_valign' => DB::getDefault('sysmap_shape', 'text_valign'),
193			'border_type' => DB::getDefault('sysmap_shape', 'border_type'),
194			'border_width' => DB::getDefault('sysmap_shape', 'border_width'),
195			'border_color' => DB::getDefault('sysmap_shape', 'border_color'),
196			'background_color' => DB::getDefault('sysmap_shape', 'background_color'),
197			'zindex' => DB::getDefault('sysmap_shape', 'zindex')
198		];
199
200		foreach ($maps as &$map) {
201			$map['selements'] = $this->convertMapElements($map['selements']);
202			$map['shapes'] = [['width' => $map['width']] + $default_shape];
203			$map['lines'] = [];
204		}
205		unset($map);
206
207		return $maps;
208	}
209
210	/**
211	 * Convert map elements.
212	 *
213	 * @param array $selements
214	 *
215	 * @return array
216	 */
217	protected function convertMapElements(array $selements) {
218		foreach ($selements as &$selement) {
219			if (zbx_is_int($selement['elementtype'])) {
220				switch ($selement['elementtype']) {
221					case SYSMAP_ELEMENT_TYPE_HOST:
222					case SYSMAP_ELEMENT_TYPE_MAP:
223					case SYSMAP_ELEMENT_TYPE_HOST_GROUP:
224					case SYSMAP_ELEMENT_TYPE_TRIGGER:
225						$selement['elements'] = [$selement['element']];
226						break;
227				}
228			}
229
230			unset($selement['element']);
231		}
232		unset($selement);
233
234		return $selements;
235	}
236
237	/**
238	 * Convert HTTP fields (headers / variables) into http pair array
239	 *
240	 * @param string $value
241	 * @param string $delimiter
242	 *
243	 * @return array
244	 */
245	protected function convertHttpFields($value, $delimiter) {
246		$pairs = array_values(array_filter(explode("\n", str_replace("\r", "\n", $value))));
247		foreach ($pairs as &$pair) {
248			$pair = explode($delimiter, $pair, 2);
249			$pair = [
250				'name' => $pair[0],
251				'value' => array_key_exists(1, $pair) ? $pair[1] : ''
252			];
253		}
254		unset($pair);
255
256		return $pairs;
257	}
258
259	/**
260	 * Convert httptest step elements.
261	 *
262	 * @param array $http_test_steps
263	 *
264	 * @return array
265	 */
266	protected function convertHttpTestSteps(array $http_test_steps) {
267		foreach ($http_test_steps as &$http_test_step) {
268			$http_test_step['headers'] = $this->convertHttpFields($http_test_step['headers'], ':');
269			$http_test_step['variables'] = $this->convertHttpFields($http_test_step['variables'], '=');
270			$http_test_step['query_fields'] = [];
271		}
272		unset($http_test_step);
273
274		return $http_test_steps;
275	}
276
277	/**
278	 * Convert httptest elements.
279	 *
280	 * @param array $http_tests
281	 *
282	 * @return array
283	 */
284	protected function convertHttpTests(array $http_tests) {
285		foreach ($http_tests as &$http_test) {
286			$http_test['headers'] = $this->convertHttpFields($http_test['headers'], ':');
287			$http_test['variables'] = $this->convertHttpFields($http_test['variables'], '=');
288
289			if (array_key_exists('steps', $http_test)) {
290				$http_test['steps'] = $this->convertHttpTestSteps($http_test['steps']);
291			}
292		}
293		unset($http_test);
294
295		return $http_tests;
296	}
297}
298