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