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
27<script type="text/javascript">
28	jQuery(function($) {
29		// proxy mode: active or passive
30		$('#status').change(function() {
31			$('#ip').closest('li').toggle($('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_PASSIVE ?>);
32			$('#proxy_address')
33				.closest('li')
34				.toggle($('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>);
35
36			toggleEncryptionFields();
37		});
38
39		$(':radio[name=useip]').change(function() {
40			$(':text[name=ip],:text[name=dns]')
41				.removeAttr('aria-required')
42				.filter(($(this).val() == <?= INTERFACE_USE_IP ?>) ? '[name=ip]' : '[name=dns]')
43				.attr('aria-required', 'true');
44		});
45
46		// clone button, special processing because of list of hosts
47		$('#clone').click(function() {
48			var url = new Curl('zabbix.php?action=proxy.edit');
49			url.setArgument('host', $('#host').val());
50			url.setArgument('status', $('input[name=status]:checked').val());
51			url.setArgument('description', $('#description').val());
52			url.setArgument('ip', $('#ip').val());
53			url.setArgument('dns', $('#dns').val());
54			url.setArgument('useip', $('input[name=useip]:checked').val());
55			url.setArgument('port', $('#port').val());
56			url.setArgument('tls_connect', $('input[name=tls_connect]:checked').val());
57			url.setArgument('tls_psk_identity', $('#tls_psk_identity').val());
58			url.setArgument('tls_psk', $('#tls_psk').val());
59			url.setArgument('psk_edit_mode', $('#psk_edit_mode').val());
60			url.setArgument('tls_issuer', $('#tls_issuer').val());
61			url.setArgument('tls_subject', $('#tls_subject').val());
62			url.setArgument('tls_accept', getTlsAccept());
63			url.setArgument('clone_proxyid', $('#proxyid').val());
64			redirect(url.getUrl(), 'post', 'action');
65		});
66
67		$('#change_psk').click(function() {
68			let input = document.createElement('input');
69			input.setAttribute('type', 'hidden');
70			input.setAttribute('name', 'action');
71			input.setAttribute('value', 'proxy.edit');
72			document.forms['proxy-form'].appendChild(input);
73
74			submitFormWithParam('proxy-form', 'psk_edit_mode', '1');
75		});
76
77		// Refresh field visibility on document load.
78		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_NONE ?>) == <?= HOST_ENCRYPTION_NONE ?>) {
79			$('#tls_in_none').prop('checked', true);
80		}
81
82		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_PSK ?>) == <?= HOST_ENCRYPTION_PSK ?>) {
83			$('#tls_in_psk').prop('checked', true);
84		}
85
86		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_CERTIFICATE ?>) == <?= HOST_ENCRYPTION_CERTIFICATE ?>) {
87			$('#tls_in_cert').prop('checked', true);
88		}
89
90		$('input[name=tls_connect]').trigger('change');
91
92		// Trim spaces on submit and depending on checkboxes, create a value for hidden field 'tls_accept'.
93		$('#proxy-form').submit(function() {
94			$(this).trimValues([
95				'#host', '#ip', '#dns', '#port', '#description', '#tls_psk_identity', '#tls_psk', '#tls_issuer',
96				'#tls_subject'
97			]);
98			$('#tls_accept').val(getTlsAccept());
99		});
100
101		// Refresh field visibility on document load.
102		$('#status,[name=useip]:checked').trigger('change');
103
104		$('#tls_connect, #tls_in_psk, #tls_in_cert').change(function() {
105			displayAdditionalEncryptionFields();
106		});
107
108		/**
109		 * Enabling or disabling connections to/from proxy based on proxy mode:
110		 *  if proxy is active, disable "Connections to proxy" field and enable "Connections from proxy";
111		 *  if proxy is active, "Connections to proxy" field is disabled and "Connections from proxy" is enabled.
112		 */
113		function toggleEncryptionFields() {
114			if ($('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>) {
115				$('input[name=tls_connect]').prop('disabled', true);
116				$('#tls_in_none, #tls_in_psk, #tls_in_cert').prop('disabled', false);
117			}
118			else {
119				$('input[name=tls_connect]').prop('disabled', false);
120				$('#tls_in_none, #tls_in_psk, #tls_in_cert').prop('disabled', true);
121			}
122
123			displayAdditionalEncryptionFields();
124		}
125
126		/**
127		 * Show/hide them based on connections to/from proxy fields and enabling or disabling additional encryption
128		 * fields based on proxy mode:
129		 *  if selected or checked certificate then show "Issuer" and "Subject" fields;
130		 *  if selected or checked PSK then show "PSK identity" and "PSK" fields;
131		 *  if selected or checked certificate, but it disabled based on proxy status, then "Issuer" and "Subject"
132		 *   fields will be disabled;
133		 *  if selected or checked PSK, but it disabled based on proxy status, then "PSK identity" and "PSK"
134		 *   fields will be disabled;
135		 */
136		function displayAdditionalEncryptionFields() {
137			// If certificate is selected or checked.
138			if ($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_CERTIFICATE ?>
139					|| $('#tls_in_cert').is(':checked')) {
140				$('#tls_issuer, #tls_subject').closest('li').show();
141			}
142			else {
143				$('#tls_issuer, #tls_subject').closest('li').hide();
144			}
145
146			if (($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_CERTIFICATE ?>
147					&& $('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_PASSIVE ?>)
148					|| ($('#tls_in_cert').is(':checked')
149					&& $('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>)) {
150				$('#tls_issuer, #tls_subject').prop('disabled', false);
151			}
152			else {
153				$('#tls_issuer, #tls_subject').prop('disabled', true);
154			}
155
156			// If PSK is selected or checked.
157			if ($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_PSK ?>
158					|| $('#tls_in_psk').is(':checked')) {
159				$('#tls_psk, #tls_psk_identity, .tls_psk').closest('li').show();
160			}
161			else {
162				$('#tls_psk, #tls_psk_identity, .tls_psk').closest('li').hide();
163			}
164
165			if (($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_PSK ?>
166					&& $('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_PASSIVE ?>)
167					|| ($('#tls_in_psk').is(':checked')
168					&& $('input[name=status]:checked').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>)) {
169				$('#tls_psk, #tls_psk_identity, #change_psk').prop('disabled', false);
170			}
171			else {
172				$('#tls_psk, #tls_psk_identity, #change_psk').prop('disabled', true);
173			}
174		}
175
176		/**
177		 * Get tls_accept value.
178		 *
179		 * @return int
180		 */
181		function getTlsAccept() {
182			var tls_accept = 0x00;
183
184			if ($('#tls_in_none').is(':checked')) {
185				tls_accept |= <?= HOST_ENCRYPTION_NONE ?>;
186			}
187			if ($('#tls_in_psk').is(':checked')) {
188				tls_accept |= <?= HOST_ENCRYPTION_PSK ?>;
189			}
190			if ($('#tls_in_cert').is(':checked')) {
191				tls_accept |= <?= HOST_ENCRYPTION_CERTIFICATE ?>;
192			}
193
194			return tls_accept;
195		}
196	});
197</script>
198