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.0 to 3.2
24 */
25class C30ImportConverter extends CConverter {
26
27	public function convert($data) {
28		$data['zabbix_export']['version'] = '3.2';
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('triggers', $data['zabbix_export'])) {
37			$data['zabbix_export']['triggers'] = $this->convertTriggers($data['zabbix_export']['triggers']);
38		}
39		if (array_key_exists('maps', $data['zabbix_export'])) {
40			$data['zabbix_export']['maps'] = $this->convertMaps($data['zabbix_export']['maps']);
41		}
42
43		return $data;
44	}
45
46	/**
47	 * Convert hosts.
48	 *
49	 * @param array $hosts
50	 *
51	 * @return array
52	 */
53	protected function convertHosts(array $hosts) {
54		foreach ($hosts as &$host) {
55			if (array_key_exists('discovery_rules', $host)) {
56				$host['discovery_rules'] = $this->convertDiscoveryRules($host['discovery_rules']);
57			}
58		}
59		unset($host);
60
61		return $hosts;
62	}
63
64	/**
65	 * Convert discovery rule elements.
66	 *
67	 * @param array $discovery_rules
68	 *
69	 * @return array
70	 */
71	protected function convertDiscoveryRules(array $discovery_rules) {
72		foreach ($discovery_rules as &$discovery_rule) {
73			$discovery_rule['trigger_prototypes'] =
74				$this->convertTriggers($discovery_rule['trigger_prototypes']);
75		}
76		unset($discovery_rule);
77
78		return $discovery_rules;
79	}
80
81	/**
82	 * Convert triggers and trigger prototypes.
83	 *
84	 * @param array $triggers
85	 *
86	 * @return array
87	 */
88	protected function convertTriggers(array $triggers) {
89		foreach ($triggers as &$trigger) {
90			$trigger['recovery_mode'] = (string) ZBX_RECOVERY_MODE_EXPRESSION;
91			$trigger['recovery_expression'] = '';
92			$trigger['correlation_mode'] = (string) ZBX_TRIGGER_CORRELATION_NONE;
93			$trigger['correlation_tag'] = '';
94			$trigger['tags'] = [];
95			$trigger['manual_close'] = (string) ZBX_TRIGGER_MANUAL_CLOSE_NOT_ALLOWED;
96
97			if (array_key_exists('dependencies', $trigger)) {
98				$trigger['dependencies'] =
99					$this->convertTriggerDependencies($trigger['dependencies']);
100			}
101		}
102		unset($trigger);
103
104		return $triggers;
105	}
106
107	/**
108	 * Convert trigger and trigger prototype dependencies.
109	 *
110	 * @param array $dependencies
111	 *
112	 * @return array
113	 */
114	protected function convertTriggerDependencies(array $dependencies) {
115		foreach ($dependencies as &$dependency) {
116			$dependency['recovery_expression'] = '';
117		}
118		unset($dependency);
119
120		return $dependencies;
121	}
122
123	/**
124	 * Convert maps.
125	 *
126	 * @param array $maps
127	 *
128	 * @return array
129	 */
130	protected function convertMaps(array $maps) {
131		foreach ($maps as &$map) {
132			$map['selements'] = $this->convertMapElements($map['selements']);
133			$map['links'] = $this->convertMapLinks($map['links']);
134		}
135		unset($map);
136
137		return $maps;
138	}
139
140	/**
141	 * Convert map elements.
142	 *
143	 * @param array $selements
144	 *
145	 * @return array
146	 */
147	protected function convertMapElements(array $selements) {
148		foreach ($selements as &$selement) {
149			if ($selement['elementtype'] == SYSMAP_ELEMENT_TYPE_TRIGGER) {
150				$selement['element']['recovery_expression'] = '';
151			}
152		}
153		unset($selement);
154
155		return $selements;
156	}
157
158	/**
159	 * Convert map links.
160	 *
161	 * @param array $links
162	 *
163	 * @return array
164	 */
165	protected function convertMapLinks(array $links) {
166		foreach ($links as &$link) {
167			$link['linktriggers'] = $this->convertMapLinkTriggers($link['linktriggers']);
168		}
169		unset($link);
170
171		return $links;
172	}
173
174	/**
175	 * Convert map links.
176	 *
177	 * @param array $linktriggers
178	 *
179	 * @return array
180	 */
181	protected function convertMapLinkTriggers(array $linktriggers) {
182		foreach ($linktriggers as &$linktrigger) {
183			$linktrigger['trigger']['recovery_expression'] = '';
184		}
185		unset($linktrigger);
186
187		return $linktriggers;
188	}
189}
190