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