1<script type="text/javascript">
2	jQuery(function($) {
3		// proxy mode: active or passive
4		$('#status').change(function() {
5			if ($(this).val() == <?= HOST_STATUS_PROXY_ACTIVE ?>) {
6				$('#ip').closest('li').hide();
7			}
8			else {
9				$('#ip').closest('li').show();
10			}
11
12			toggleEncryptionFields();
13		});
14
15		// clone button, special processing because of list of hosts
16		$('#clone').click(function() {
17			var url = new Curl('zabbix.php?action=proxy.edit');
18			url.setArgument('host', $('#host').val());
19			url.setArgument('status', $('#status').val());
20			url.setArgument('description', $('#description').val());
21			url.setArgument('ip', $('#ip').val());
22			url.setArgument('dns', $('#dns').val());
23			url.setArgument('useip', $('input[name=useip]:checked').val());
24			url.setArgument('port', $('#port').val());
25			url.setArgument('tls_connect', $('input[name=tls_connect]:checked').val());
26			url.setArgument('tls_psk_identity', $('#tls_psk_identity').val());
27			url.setArgument('tls_psk', $('#tls_psk').val());
28			url.setArgument('tls_issuer', $('#tls_issuer').val());
29			url.setArgument('tls_subject', $('#tls_subject').val());
30			url.setArgument('tls_accept', getTlsAccept());
31			redirect(url.getUrl(), 'post', 'action');
32		});
33
34		// Refresh field visibility on document load.
35		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_NONE ?>) == <?= HOST_ENCRYPTION_NONE ?>) {
36			$('#tls_in_none').prop('checked', true);
37		}
38
39		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_PSK ?>) == <?= HOST_ENCRYPTION_PSK ?>) {
40			$('#tls_in_psk').prop('checked', true);
41		}
42
43		if (($('#tls_accept').val() & <?= HOST_ENCRYPTION_CERTIFICATE ?>) == <?= HOST_ENCRYPTION_CERTIFICATE ?>) {
44			$('#tls_in_cert').prop('checked', true);
45		}
46
47		$('input[name=tls_connect]').trigger('change');
48
49		// Trim spaces on submit and depending on checkboxes, create a value for hidden field 'tls_accept'.
50		$('#proxyForm').submit(function() {
51			$(this).trimValues([
52				'#host', '#ip', '#dns', '#port', '#description', '#tls_psk_identity', '#tls_psk', '#tls_issuer',
53				'#tls_subject'
54			]);
55			$('#tls_accept').val(getTlsAccept());
56		});
57
58		// Refresh field visibility on document load.
59		$('#status').trigger('change');
60
61		$('#tls_connect, #tls_in_psk, #tls_in_cert').change(function() {
62			displayAdditionalEncryptionFields();
63		});
64
65		/**
66		 * Enabling or disabling connections to/from proxy based on proxy mode:
67		 *  if proxy is active, disable "Connections to proxy" field and enable "Connections from proxy";
68		 *  if proxy is active, "Connections to proxy" field is disabled and "Connections from proxy" is enabled.
69		 */
70		function toggleEncryptionFields() {
71			if ($('#status').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>) {
72				$('input[name=tls_connect]').prop('disabled', true);
73				$('#tls_in_none, #tls_in_psk, #tls_in_cert').prop('disabled', false);
74			}
75			else {
76				$('input[name=tls_connect]').prop('disabled', false);
77				$('#tls_in_none, #tls_in_psk, #tls_in_cert').prop('disabled', true);
78			}
79
80			displayAdditionalEncryptionFields();
81		}
82
83		/**
84		 * Show/hide them based on connections to/from proxy fields and enabling or disabling additional encryption
85		 * fields based on proxy mode:
86		 *  if selected or checked certificate then show "Issuer" and "Subject" fields;
87		 *  if selected or checked PSK then show "PSK identity" and "PSK" fields;
88		 *  if selected or checked certificate, but it disabled based on proxy status, then "Issuer" and "Subject"
89		 *   fields will be disabled;
90		 *  if selected or checked PSK, but it disabled based on proxy status, then "PSK identity" and "PSK"
91		 *   fields will be disabled;
92		 */
93		function displayAdditionalEncryptionFields() {
94			// If certificate is selected or checked.
95			if ($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_CERTIFICATE ?>
96					|| $('#tls_in_cert').is(':checked')) {
97				$('#tls_issuer, #tls_subject').closest('li').show();
98			}
99			else {
100				$('#tls_issuer, #tls_subject').closest('li').hide();
101			}
102
103			if (($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_CERTIFICATE ?>
104					&& $('#status').val() == <?= HOST_STATUS_PROXY_PASSIVE ?>)
105					|| ($('#tls_in_cert').is(':checked')
106					&& $('#status').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>)) {
107				$('#tls_issuer, #tls_subject').prop('disabled', false);
108			}
109			else {
110				$('#tls_issuer, #tls_subject').prop('disabled', true);
111			}
112
113			// If PSK is selected or checked.
114			if ($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_PSK ?>
115					|| $('#tls_in_psk').is(':checked')) {
116				$('#tls_psk, #tls_psk_identity').closest('li').show();
117			}
118			else {
119				$('#tls_psk, #tls_psk_identity').closest('li').hide();
120			}
121
122			if (($('input[name=tls_connect]:checked').val() == <?= HOST_ENCRYPTION_PSK ?>
123					&& $('#status').val() == <?= HOST_STATUS_PROXY_PASSIVE ?>)
124					|| ($('#tls_in_psk').is(':checked')
125					&& $('#status').val() == <?= HOST_STATUS_PROXY_ACTIVE ?>)) {
126				$('#tls_psk, #tls_psk_identity').prop('disabled', false);
127			}
128			else {
129				$('#tls_psk, #tls_psk_identity').prop('disabled', true);
130			}
131		}
132
133		/**
134		 * Get tls_accept value.
135		 *
136		 * @return int
137		 */
138		function getTlsAccept() {
139			var tls_accept = 0x00;
140
141			if ($('#tls_in_none').is(':checked')) {
142				tls_accept |= <?= HOST_ENCRYPTION_NONE ?>;
143			}
144			if ($('#tls_in_psk').is(':checked')) {
145				tls_accept |= <?= HOST_ENCRYPTION_PSK ?>;
146			}
147			if ($('#tls_in_cert').is(':checked')) {
148				tls_accept |= <?= HOST_ENCRYPTION_CERTIFICATE ?>;
149			}
150
151			return tls_accept;
152		}
153	});
154</script>
155