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$widget = (new CWidget())->setTitle(_('Web monitoring'));
23
24// append host summary to widget header
25if (!empty($this->data['hostid'])) {
26	$widget->addItem(get_header_host_table('web', $this->data['hostid']));
27}
28
29// create form
30$http_form = (new CForm())
31	->setName('httpForm')
32	->setId('httpForm')
33	->setAttribute('aria-labeledby', ZBX_STYLE_PAGE_TITLE)
34	->addVar('form', $this->data['form'])
35	->addVar('hostid', $this->data['hostid'])
36	->addVar('templated', $this->data['templated']);
37
38if (!empty($this->data['httptestid'])) {
39	$http_form->addVar('httptestid', $this->data['httptestid']);
40}
41
42/*
43 * Scenario tab
44 */
45$http_form_list = new CFormList();
46
47// Parent http tests
48if (!empty($this->data['templates'])) {
49	$http_form_list->addRow(_('Parent web scenarios'), $this->data['templates']);
50}
51
52// Name
53$name_text_box = (new CTextBox('name', $this->data['name'], $this->data['templated'], 64))
54	->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
55	->setAriaRequired();
56if (!$this->data['templated']) {
57	$name_text_box->setAttribute('autofocus', 'autofocus');
58}
59$http_form_list->addRow((new CLabel(_('Name'), 'name'))->setAsteriskMark(), $name_text_box);
60
61// Application
62if ($this->data['application_list']) {
63	$applications = zbx_array_merge([''], $this->data['application_list']);
64	$http_form_list->addRow(_('Application'),
65		new CComboBox('applicationid', $this->data['applicationid'], null, $applications)
66	);
67}
68else {
69	$http_form_list->addRow(_('Application'), new CSpan(_('No applications found.')));
70}
71
72// New application
73$http_form_list
74	->addRow(new CLabel(_('New application'), 'new_application'),
75		(new CSpan(
76			(new CTextBox('new_application', $this->data['new_application']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
77		))->addClass(ZBX_STYLE_FORM_NEW_GROUP)
78	)
79	->addRow((new CLabel(_('Update interval'), 'delay'))->setAsteriskMark(),
80		(new CTextBox('delay', $data['delay']))
81			->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
82			->setAriaRequired()
83	)
84	->addRow(
85		(new CLabel(_('Attempts'), 'retries'))->setAsteriskMark(),
86		(new CNumericBox('retries', $this->data['retries'], 2))
87			->setAriaRequired()
88			->setWidth(ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH)
89	);
90
91$agent_combo_box = new CComboBox('agent', $this->data['agent']);
92
93$user_agents_all = userAgents();
94$user_agents_all[_('Others')][ZBX_AGENT_OTHER] = _('other').' ...';
95
96foreach ($user_agents_all as $user_agent_group => $user_agents) {
97	$agent_combo_box->addItemsInGroup($user_agent_group, $user_agents);
98}
99
100$http_form_list->addRow(_('Agent'), $agent_combo_box);
101
102$http_form_list->addRow(_('User agent string'),
103	(new CTextBox('agent_other', $this->data['agent_other']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH),
104	'row_agent_other'
105);
106
107// append HTTP proxy to form list
108$http_form_list
109	->addRow(_('HTTP proxy'),
110		(new CTextBox('http_proxy', $this->data['http_proxy'], false, 255))
111			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
112			->setAttribute('placeholder', '[protocol://][user[:password]@]proxy.example.com[:port]')
113			->disableAutocomplete()
114	);
115
116$http_form_list->addRow(_('Variables'), (new CDiv(
117	(new CTable())
118		->addClass('httpconf-dynamic-row')
119		->setAttribute('data-type', 'variables')
120		->setAttribute('style', 'width: 100%;')
121		->setHeader(['', _('Name'), '', _('Value'), ''])
122		->addRow((new CRow([
123			(new CCol(
124				(new CButton(null, _('Add')))
125					->addClass(ZBX_STYLE_BTN_LINK)
126					->addClass('element-table-add')
127			))->setColSpan(5)
128		])))
129))
130	->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
131	->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;')
132);
133
134$http_form_list->addRow(_('Headers'), (new CDiv(
135	(new CTable())
136		->addClass('httpconf-dynamic-row')
137		->setAttribute('data-type', 'headers')
138		->setAttribute('style', 'width: 100%;')
139		->setHeader(['', _('Name'), '', _('Value'), ''])
140		->addRow((new CRow([
141			(new CCol(
142				(new CButton(null, _('Add')))
143					->addClass(ZBX_STYLE_BTN_LINK)
144					->addClass('element-table-add')
145			))->setColSpan(5)
146		])))
147))
148	->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
149	->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;')
150);
151
152$http_form_list->addRow(_('Enabled'), (new CCheckBox('status'))->setChecked(!$this->data['status']));
153
154/*
155 * Authentication tab
156 */
157$http_authentication_form_list = new CFormList();
158
159// Authentication type
160$http_authentication_form_list->addRow(_('HTTP authentication'),
161	new CComboBox('authentication', $this->data['authentication'], null, httptest_authentications())
162);
163
164$http_authentication_form_list
165	->addRow(new CLabel(_('User'), 'http_user'),
166		(new CTextBox('http_user', $this->data['http_user'], false, 64))
167			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
168			->disableAutocomplete()
169	)
170	->addRow(new CLabel(_('Password'), 'http_password'),
171		(new CTextBox('http_password', $this->data['http_password'], false, 64))
172			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
173			->disableAutocomplete()
174	)
175	->addRow(_('SSL verify peer'),
176		(new CCheckBox('verify_peer'))->setChecked($this->data['verify_peer'] == 1)
177	)
178	->addRow(_('SSL verify host'),
179		(new CCheckBox('verify_host'))->setChecked($this->data['verify_host'] == 1)
180	)
181	->addRow(_('SSL certificate file'),
182		(new CTextBox('ssl_cert_file', $this->data['ssl_cert_file'], false, 255))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
183	)
184	->addRow(_('SSL key file'),
185		(new CTextBox('ssl_key_file', $this->data['ssl_key_file'], false, 255))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
186	)
187	->addRow(_('SSL key password'),
188		(new CTextBox('ssl_key_password', $this->data['ssl_key_password'], false, 64))
189			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
190			->disableAutocomplete()
191	);
192
193/*
194 * Step tab
195 */
196$http_step_form_list = new CFormList();
197$steps_table = (new CTable())
198	->addClass('httpconf-steps-dynamic-row')
199	->setHeader([
200		(new CColHeader())->setWidth('15'),
201		(new CColHeader())->setWidth('15'),
202		(new CColHeader(_('Name')))->setWidth('150'),
203		(new CColHeader(_('Timeout')))->setWidth('50'),
204		(new CColHeader(_('URL')))->setWidth('200'),
205		(new CColHeader(_('Required')))->setWidth('75'),
206		(new CColHeader(_('Status codes')))
207			->addClass(ZBX_STYLE_NOWRAP)
208			->setWidth('90'),
209		(new CColHeader(_('Action')))->setWidth('50')
210	]);
211
212if (!$this->data['templated']) {
213	$steps_table->addRow(
214		(new CCol(
215			(new CButton(null, _('Add')))
216				->addClass('element-table-add')
217				->addClass(ZBX_STYLE_BTN_LINK)
218		))->setColSpan(8)
219	);
220}
221else {
222	$steps_table->addRow(
223		(new CCol(null))->setColSpan(8)->addClass('element-table-add')
224	);
225}
226
227$http_step_form_list->addRow((new CLabel(_('Steps'), $steps_table->getId()))->setAsteriskMark(),
228	(new CDiv($steps_table))
229		->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
230		->setAriaRequired()
231);
232
233// append tabs to form
234$http_tab = (new CTabView())
235	->addTab('scenarioTab', _('Scenario'), $http_form_list)
236	->addTab('stepTab', _('Steps'), $http_step_form_list)
237	->addTab('authenticationTab', _('Authentication'), $http_authentication_form_list);
238if (!$this->data['form_refresh']) {
239	$http_tab->setSelected(0);
240}
241
242// append buttons to form
243if (!empty($this->data['httptestid'])) {
244	$buttons = [new CSubmit('clone', _('Clone'))];
245
246	if ($this->data['host']['status'] == HOST_STATUS_MONITORED
247			|| $this->data['host']['status'] == HOST_STATUS_NOT_MONITORED) {
248
249		$buttons[] = new CButtonQMessage(
250			'del_history',
251			_('Clear history and trends'),
252			_('History clearing can take a long time. Continue?')
253		);
254	}
255
256	$buttons[] = (new CButtonDelete(_('Delete web scenario?'), url_params(['form', 'httptestid', 'hostid'])))
257		->setEnabled(!$data['templated']);
258	$buttons[] = new CButtonCancel();
259
260	$http_tab->setFooter(makeFormFooter(new CSubmit('update', _('Update')), $buttons));
261}
262else {
263	$http_tab->setFooter(makeFormFooter(
264		new CSubmit('add', _('Add')),
265		[new CButtonCancel()]
266	));
267}
268
269$http_form->addItem($http_tab);
270$widget->addItem($http_form);
271
272$this->data['scenario_tab_data'] = [
273	'agent_visibility' => [],
274	'pairs' => [
275		'variables' => [],
276		'headers' => []
277	]
278];
279
280foreach ($data['pairs'] as $field) {
281	zbx_subarray_push($this->data['scenario_tab_data']['pairs'], $field['type'], $field);
282}
283
284zbx_subarray_push($this->data['scenario_tab_data']['agent_visibility'], ZBX_AGENT_OTHER, 'agent_other');
285zbx_subarray_push($this->data['scenario_tab_data']['agent_visibility'], ZBX_AGENT_OTHER, 'row_agent_other');
286
287require_once dirname(__FILE__).'/js/configuration.httpconf.edit.js.php';
288
289return $widget;
290