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
26$widget = (new CWidget())->setTitle(_('Web monitoring'));
27
28// append host summary to widget header
29if (!empty($this->data['hostid'])) {
30	$widget->setNavigation(getHostNavigation('web', $this->data['hostid']));
31}
32
33$url = (new CUrl('httpconf.php'))
34	->setArgument('context', $data['context'])
35	->getUrl();
36
37// create form
38$http_form = (new CForm('post', $url))
39	->setId('http-form')
40	->setName('httpForm')
41	->setAttribute('aria-labeledby', ZBX_STYLE_PAGE_TITLE)
42	->addVar('form', $this->data['form'])
43	->addVar('hostid', $this->data['hostid'])
44	->addVar('templated', $this->data['templated']);
45
46if (!empty($this->data['httptestid'])) {
47	$http_form->addVar('httptestid', $this->data['httptestid']);
48}
49
50/*
51 * Scenario tab
52 */
53$http_form_list = new CFormList();
54
55// Parent http tests
56if (!empty($this->data['templates'])) {
57	$http_form_list->addRow(_('Parent web scenarios'), $this->data['templates']);
58}
59
60// Name
61$name_text_box = (new CTextBox('name', $this->data['name'], $this->data['templated'], 64))
62	->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
63	->setAriaRequired();
64if (!$this->data['templated']) {
65	$name_text_box->setAttribute('autofocus', 'autofocus');
66}
67
68$http_form_list
69	->addRow((new CLabel(_('Name'), 'name'))->setAsteriskMark(), $name_text_box)
70	->addRow((new CLabel(_('Update interval'), 'delay'))->setAsteriskMark(),
71		(new CTextBox('delay', $data['delay']))
72			->setWidth(ZBX_TEXTAREA_SMALL_WIDTH)
73			->setAriaRequired()
74	)
75	->addRow(
76		(new CLabel(_('Attempts'), 'retries'))->setAsteriskMark(),
77		(new CNumericBox('retries', $this->data['retries'], 2))
78			->setAriaRequired()
79			->setWidth(ZBX_TEXTAREA_NUMERIC_STANDARD_WIDTH)
80	);
81
82$agent_select = (new CSelect('agent'))
83	->setId('agent')
84	->setFocusableElementId('label-agent')
85	->setValue($this->data['agent']);
86
87$user_agents_all = userAgents();
88$user_agents_all[_('Others')][ZBX_AGENT_OTHER] = _('other').' ...';
89
90foreach ($user_agents_all as $user_agent_group => $user_agents) {
91	$agent_select->addOptionGroup((new CSelectOptionGroup($user_agent_group))
92		->addOptions(CSelect::createOptionsFromArray($user_agents))
93	);
94}
95
96$http_form_list->addRow(new CLabel(_('Agent'), $agent_select->getFocusableElementId()), $agent_select);
97
98$http_form_list->addRow(_('User agent string'),
99	(new CTextBox('agent_other', $this->data['agent_other']))->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH),
100	'row_agent_other'
101);
102
103// append HTTP proxy to form list
104$http_form_list
105	->addRow(_('HTTP proxy'),
106		(new CTextBox('http_proxy', $this->data['http_proxy'], false, 255))
107			->setWidth(ZBX_TEXTAREA_STANDARD_WIDTH)
108			->setAttribute('placeholder', '[protocol://][user[:password]@]proxy.example.com[:port]')
109			->disableAutocomplete()
110	);
111
112$http_form_list->addRow(_('Variables'), (new CDiv(
113	(new CTable())
114		->addClass('httpconf-dynamic-row')
115		->setAttribute('data-type', 'variables')
116		->setAttribute('style', 'width: 100%;')
117		->setHeader(['', _('Name'), '', _('Value'), ''])
118		->addRow((new CRow([
119			(new CCol(
120				(new CButton(null, _('Add')))
121					->addClass(ZBX_STYLE_BTN_LINK)
122					->addClass('element-table-add')
123			))->setColSpan(5)
124		])))
125))
126	->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
127	->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;')
128);
129
130$http_form_list->addRow(_('Headers'), (new CDiv(
131	(new CTable())
132		->addClass('httpconf-dynamic-row')
133		->setAttribute('data-type', 'headers')
134		->setAttribute('style', 'width: 100%;')
135		->setHeader(['', _('Name'), '', _('Value'), ''])
136		->addRow((new CRow([
137			(new CCol(
138				(new CButton(null, _('Add')))
139					->addClass(ZBX_STYLE_BTN_LINK)
140					->addClass('element-table-add')
141			))->setColSpan(5)
142		])))
143))
144	->addClass(ZBX_STYLE_TABLE_FORMS_SEPARATOR)
145	->setAttribute('style', 'min-width: ' . ZBX_TEXTAREA_BIG_WIDTH . 'px;')
146);
147
148$http_form_list->addRow(_('Enabled'), (new CCheckBox('status'))->setChecked(!$this->data['status']));
149
150/*
151 * Authentication tab
152 */
153$http_authentication_form_list = new CFormList();
154
155// Authentication type
156$http_authentication_form_list->addRow(new CLabel(_('HTTP authentication'), 'label-authentication'),
157	(new CSelect('authentication'))
158		->setId('authentication')
159		->setFocusableElementId('label-authentication')
160		->setValue($this->data['authentication'])
161		->addOptions(CSelect::createOptionsFromArray(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, TAB_INDICATOR_STEPS)
237	->addTab('tags-tab', _('Tags'),
238		new CPartial('configuration.tags.tab', [
239			'source' => 'httptest',
240			'tags' => $data['tags'],
241			'show_inherited_tags' => $data['show_inherited_tags'],
242			'readonly' => false
243		]),
244		TAB_INDICATOR_TAGS
245	)
246	->addTab('authenticationTab', _('Authentication'), $http_authentication_form_list, TAB_INDICATOR_HTTP_AUTH);
247if (!$this->data['form_refresh']) {
248	$http_tab->setSelected(0);
249}
250
251// append buttons to form
252if (!empty($this->data['httptestid'])) {
253	$buttons = [new CSubmit('clone', _('Clone'))];
254
255	if ($this->data['host']['status'] == HOST_STATUS_MONITORED
256			|| $this->data['host']['status'] == HOST_STATUS_NOT_MONITORED) {
257
258		$buttons[] = new CButtonQMessage(
259			'del_history',
260			_('Clear history and trends'),
261			_('History clearing can take a long time. Continue?')
262		);
263	}
264
265	$buttons[] = (new CButtonDelete(_('Delete web scenario?'), url_params(['form', 'httptestid', 'hostid', 'context']),
266		'context'
267	))->setEnabled(!$data['templated']);
268	$buttons[] = new CButtonCancel(url_param('context'));
269
270	$http_tab->setFooter(makeFormFooter(new CSubmit('update', _('Update')), $buttons));
271}
272else {
273	$http_tab->setFooter(makeFormFooter(
274		new CSubmit('add', _('Add')),
275		[new CButtonCancel(url_param('context'))]
276	));
277}
278
279$http_form->addItem($http_tab);
280$widget->addItem($http_form);
281
282$this->data['scenario_tab_data'] = [
283	'agent_visibility' => [],
284	'pairs' => [
285		'variables' => [],
286		'headers' => []
287	]
288];
289
290foreach ($data['pairs'] as $field) {
291	zbx_subarray_push($this->data['scenario_tab_data']['pairs'], $field['type'], $field);
292}
293
294zbx_subarray_push($this->data['scenario_tab_data']['agent_visibility'], ZBX_AGENT_OTHER, 'agent_other');
295zbx_subarray_push($this->data['scenario_tab_data']['agent_visibility'], ZBX_AGENT_OTHER, 'row_agent_other');
296
297require_once dirname(__FILE__).'/js/configuration.httpconf.edit.js.php';
298
299$widget->show();
300