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__).'/js/configuration.host.massupdate.js.php';
23
24$hostWidget = (new CWidget())->setTitle(_('Hosts'));
25
26// create form
27$hostView = (new CForm())
28	->setName('hostForm')
29	->setAttribute('aria-labeledby', ZBX_STYLE_PAGE_TITLE)
30	->addVar('action', 'host.massupdate')
31	->addVar('tls_accept', $data['tls_accept'])
32	->setAttribute('id', 'hostForm');
33foreach ($data['hosts'] as $hostid) {
34	$hostView->addVar('hosts['.$hostid.']', $hostid);
35}
36
37// create form list
38$hostFormList = new CFormList('hostFormList');
39
40// replace host groups
41$hostgroups_to_replace = isset($_REQUEST['groups'])
42	? CArrayHelper::renameObjectsKeys(API::HostGroup()->get([
43		'output' => ['groupid', 'name'],
44		'groupids' => $_REQUEST['groups'],
45		'editable' => true
46	]), ['groupid' => 'id'])
47	: [];
48
49$replaceGroups = (new CDiv(
50	(new CMultiSelect([
51		'name' => 'groups[]',
52		'object_name' => 'hostGroup',
53		'data' => $hostgroups_to_replace,
54		'popup' => [
55			'parameters' => [
56				'srctbl' => 'host_groups',
57				'srcfld1' => 'groupid',
58				'dstfrm' => $hostView->getName(),
59				'dstfld1' => 'groups_',
60				'editable' => true
61			]
62		]
63	]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
64))->setId('replaceGroups');
65
66$hostFormList->addRow(
67	(new CVisibilityBox('visible[groups]', 'replaceGroups', _('Original')))
68		->setLabel(_('Replace host groups'))
69		->setChecked(isset($data['visible']['groups']))
70		->setAttribute('autofocus', 'autofocus'),
71	$replaceGroups
72);
73
74// add new or existing host groups
75$hostgroups_to_add = [];
76if (isset($_REQUEST['new_groups'])) {
77	$groupids = [];
78
79	foreach ($_REQUEST['new_groups'] as $newHostGroup) {
80		if (is_array($newHostGroup) && isset($newHostGroup['new'])) {
81			$hostgroups_to_add[] = [
82				'id' => $newHostGroup['new'],
83				'name' => $newHostGroup['new'].' ('._x('new', 'new element in multiselect').')',
84				'isNew' => true
85			];
86		}
87		else {
88			$groupids[] = $newHostGroup;
89		}
90	}
91
92	$hostgroups_to_add = array_merge($hostgroups_to_add, $groupids
93		? CArrayHelper::renameObjectsKeys(API::HostGroup()->get([
94			'output' => ['groupid', 'name'],
95			'groupids' => $groupids
96		]), ['groupid' => 'id'])
97		: []);
98}
99
100$hostFormList->addRow(
101	(new CVisibilityBox('visible[new_groups]', 'newGroups', _('Original')))
102		->setLabel((CWebUser::getType() == USER_TYPE_SUPER_ADMIN)
103			? _('Add new or existing host groups')
104			: _('New host group')
105		)
106		->setChecked(isset($data['visible']['new_groups'])),
107	(new CDiv(
108		(new CMultiSelect([
109			'name' => 'new_groups[]',
110			'object_name' => 'hostGroup',
111			'add_new' => (CWebUser::getType() == USER_TYPE_SUPER_ADMIN),
112			'data' => $hostgroups_to_add,
113			'popup' => [
114				'parameters' => [
115					'srctbl' => 'host_groups',
116					'srcfld1' => 'groupid',
117					'dstfrm' => $hostView->getName(),
118					'dstfld1' => 'new_groups_',
119					'editable' => true
120				]
121			]
122		]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
123	))->setId('newGroups')
124);
125
126// Get list of host groups to remove if unsuccessful submit.
127$host_groups_to_remove = getRequest('remove_groups')
128	? CArrayHelper::renameObjectsKeys(API::HostGroup()->get([
129		'output' => ['groupid', 'name'],
130		'groupids' => getRequest('remove_groups'),
131		'editable' => true
132	]), ['groupid' => 'id'])
133	: [];
134
135// Remove host groups control.
136$hostFormList->addRow(
137	(new CVisibilityBox('visible[remove_groups]', 'remove_groups', _('Original')))
138		->setLabel(_('Remove host groups'))
139		->setChecked(array_key_exists('remove_groups', $data['visible'])),
140	(new CDiv(
141		(new CMultiSelect([
142			'name' => 'remove_groups[]',
143			'object_name' => 'hostGroup',
144			'data' => $host_groups_to_remove,
145			'popup' => [
146				'parameters' => [
147					'srctbl' => 'host_groups',
148					'srcfld1' => 'groupid',
149					'dstfrm' => $hostView->getName(),
150					'dstfld1' => 'remove_groups_',
151					'editable' => true
152				]
153			]
154		]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
155	))->setId('remove_groups')
156);
157
158// append description to form list
159$hostFormList->addRow(
160	(new CVisibilityBox('visible[description]', 'description', _('Original')))
161		->setLabel(_('Description'))
162		->setChecked(isset($data['visible']['description'])),
163	(new CTextArea('description', $data['description']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
164);
165
166// append proxy to form list
167$proxyComboBox = new CComboBox('proxy_hostid', $data['proxy_hostid']);
168$proxyComboBox->addItem(0, _('(no proxy)'));
169foreach ($data['proxies'] as $proxie) {
170	$proxyComboBox->addItem($proxie['hostid'], $proxie['host']);
171}
172$hostFormList->addRow(
173	(new CVisibilityBox('visible[proxy_hostid]', 'proxy_hostid', _('Original')))
174		->setLabel(_('Monitored by proxy'))
175		->setChecked(isset($data['visible']['proxy_hostid'])),
176	$proxyComboBox
177);
178
179// append status to form list
180$hostFormList->addRow(
181	(new CVisibilityBox('visible[status]', 'status', _('Original')))
182		->setLabel(_('Status'))
183		->setChecked(isset($data['visible']['status'])),
184	new CComboBox('status', $data['status'], null, [
185		HOST_STATUS_MONITORED => _('Enabled'),
186		HOST_STATUS_NOT_MONITORED => _('Disabled')
187	])
188);
189
190$templatesFormList = new CFormList('templatesFormList');
191
192// append templates table to from list
193$newTemplateTable = (new CTable())
194	->addRow([
195		(new CMultiSelect([
196			'name' => 'templates[]',
197			'object_name' => 'templates',
198			'data' => $data['linkedTemplates'],
199			'popup' => [
200				'parameters' => [
201					'srctbl' => 'templates',
202					'srcfld1' => 'hostid',
203					'srcfld2' => 'host',
204					'dstfrm' => $hostView->getName(),
205					'dstfld1' => 'templates_'
206				]
207			]
208		]))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
209	])
210	->addRow([
211		(new CList())
212			->addClass(ZBX_STYLE_LIST_CHECK_RADIO)
213			->addItem((new CCheckBox('mass_replace_tpls'))
214				->setLabel(_('Replace'))
215				->setChecked($data['mass_replace_tpls'] == 1)
216			)
217			->addItem((new CCheckBox('mass_clear_tpls'))
218				->setLabel(_('Clear when unlinking'))
219				->setChecked($data['mass_clear_tpls'] == 1)
220			)
221	]);
222
223$templatesFormList->addRow(
224	(new CVisibilityBox('visible[templates]', 'templateDiv', _('Original')))
225		->setLabel(_('Link templates'))
226		->setChecked(isset($data['visible']['templates'])),
227	(new CDiv($newTemplateTable))
228		->setId('templateDiv')
229		->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
230		->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
231);
232
233$ipmiFormList = new CFormList('ipmiFormList');
234
235// append ipmi to form list
236$ipmiFormList->addRow(
237	(new CVisibilityBox('visible[ipmi_authtype]', 'ipmi_authtype', _('Original')))
238		->setLabel(_('IPMI authentication algorithm'))
239		->setChecked(isset($data['visible']['ipmi_authtype'])),
240	new CComboBox('ipmi_authtype', $data['ipmi_authtype'], null, ipmiAuthTypes())
241);
242
243$ipmiFormList->addRow(
244	(new CVisibilityBox('visible[ipmi_privilege]', 'ipmi_privilege', _('Original')))
245		->setLabel(_('IPMI privilege level'))
246		->setChecked(isset($data['visible']['ipmi_privilege'])),
247	new CComboBox('ipmi_privilege', $data['ipmi_privilege'], null, ipmiPrivileges())
248);
249
250$ipmiFormList->addRow(
251	(new CVisibilityBox('visible[ipmi_username]', 'ipmi_username', _('Original')))
252		->setLabel(_('IPMI username'))
253		->setChecked(isset($data['visible']['ipmi_username'])),
254	(new CTextBox('ipmi_username', $data['ipmi_username']))
255		->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
256		->disableAutocomplete()
257);
258
259$ipmiFormList->addRow(
260	(new CVisibilityBox('visible[ipmi_password]', 'ipmi_password', _('Original')))
261		->setLabel(_('IPMI password'))
262		->setChecked(isset($data['visible']['ipmi_password'])),
263	(new CTextBox('ipmi_password', $data['ipmi_password']))
264		->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
265		->disableAutocomplete()
266);
267
268$inventoryFormList = new CFormList('inventoryFormList');
269
270// append inventories to form list
271$inventoryFormList->addRow(
272	(new CVisibilityBox('visible[inventory_mode]', 'inventory_mode_div', _('Original')))
273		->setLabel(_('Inventory mode'))
274		->setChecked(isset($data['visible']['inventory_mode'])),
275	(new CDiv(
276		(new CRadioButtonList('inventory_mode', (int) $data['inventory_mode']))
277			->addValue(_('Disabled'), HOST_INVENTORY_DISABLED)
278			->addValue(_('Manual'), HOST_INVENTORY_MANUAL)
279			->addValue(_('Automatic'), HOST_INVENTORY_AUTOMATIC)
280			->setModern(true)
281	))->setId('inventory_mode_div')
282);
283
284$hostInventoryTable = DB::getSchema('host_inventory');
285foreach ($data['inventories'] as $field => $fieldInfo) {
286	if (!isset($data['host_inventory'][$field])) {
287		$data['host_inventory'][$field] = '';
288	}
289
290	if ($hostInventoryTable['fields'][$field]['type'] == DB::FIELD_TYPE_TEXT) {
291		$fieldInput = (new CTextArea('host_inventory['.$field.']', $data['host_inventory'][$field]))
292			->setWidth(ZBX_TEXTAREA_BIG_WIDTH);
293	}
294	else {
295		$field_length = $hostInventoryTable['fields'][$field]['length'];
296
297		if ($field_length < 39) {
298			$width = ZBX_TEXTAREA_SMALL_WIDTH;
299		}
300		elseif ($field_length < 64) {
301			$width = ZBX_TEXTAREA_STANDARD_WIDTH;
302		}
303		else {
304			$width = ZBX_TEXTAREA_BIG_WIDTH;
305		}
306
307		$fieldInput = (new CTextBox('host_inventory['.$field.']', $data['host_inventory'][$field]))
308			->setWidth($width)
309			->setAttribute('maxlength', $field_length);
310	}
311
312	$inventoryFormList->addRow(
313		(new CVisibilityBox('visible['.$field.']', $fieldInput->getId(), _('Original')))
314			->setLabel($fieldInfo['title'])
315			->setChecked(isset($data['visible'][$field])),
316		$fieldInput, null, 'formrow-inventory'
317	);
318}
319
320// Encryption
321$encryption_form_list = new CFormList('encryption');
322
323$encryption_table = (new CTable())
324	->addRow([_('Connections to host'),
325		(new CRadioButtonList('tls_connect', (int) $data['tls_connect']))
326			->addValue(_('No encryption'), HOST_ENCRYPTION_NONE)
327			->addValue(_('PSK'), HOST_ENCRYPTION_PSK)
328			->addValue(_('Certificate'), HOST_ENCRYPTION_CERTIFICATE)
329			->setModern(true)
330	])
331	->addRow([_('Connections from host'),
332		(new CList())
333			->addClass(ZBX_STYLE_LIST_CHECK_RADIO)
334			->addItem((new CCheckBox('tls_in_none'))->setLabel(_('No encryption')))
335			->addItem((new CCheckBox('tls_in_psk'))->setLabel(_('PSK')))
336			->addItem((new CCheckBox('tls_in_cert'))->setLabel(_('Certificate')))
337	])
338	->addRow([_('PSK identity'),
339		(new CTextBox('tls_psk_identity', $data['tls_psk_identity'], false, 128))->setWidth(ZBX_TEXTAREA_BIG_WIDTH)
340	])
341	->addRow([_('PSK'),
342		(new CTextBox('tls_psk', $data['tls_psk'], false, 512))
343			->setWidth(ZBX_TEXTAREA_BIG_WIDTH)
344			->disableAutocomplete()
345	])
346	->addRow([_('Issuer'),
347		(new CTextBox('tls_issuer', $data['tls_issuer'], false, 1024))->setWidth(ZBX_TEXTAREA_BIG_WIDTH)
348	])
349	->addRow([_x('Subject', 'encryption certificate'),
350		(new CTextBox('tls_subject', $data['tls_subject'], false, 1024))->setWidth(ZBX_TEXTAREA_BIG_WIDTH)
351	]);
352
353$encryption_form_list->addRow(
354	(new CVisibilityBox('visible[encryption]', 'encryption_div', _('Original')))
355		->setLabel(_('Connections'))
356		->setChecked(isset($data['visible']['encryption'])),
357	(new CDiv($encryption_table))
358		->setId('encryption_div')
359		->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
360		->setAttribute('style', 'min-width: '.ZBX_TEXTAREA_BIG_WIDTH.'px;')
361);
362
363// append tabs to form
364$hostTab = (new CTabView())
365	->addTab('hostTab', _('Host'), $hostFormList)
366	->addTab('templatesTab', _('Templates'), $templatesFormList)
367	->addTab('ipmiTab', _('IPMI'), $ipmiFormList)
368	->addTab('inventoryTab', _('Inventory'), $inventoryFormList);
369// reset the tab when opening the form for the first time
370if (!hasRequest('masssave') && !hasRequest('inventory_mode')) {
371	$hostTab->setSelected(0);
372}
373$hostTab->addTab('encryptionTab', _('Encryption'), $encryption_form_list);
374
375// append buttons to form
376$hostTab->setFooter(makeFormFooter(
377	new CSubmit('masssave', _('Update')),
378	[new CButtonCancel(url_param('groupid'))]
379));
380
381$hostView->addItem($hostTab);
382
383$hostWidget->addItem($hostView);
384
385return $hostWidget;
386