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