File: class.script.js

package info (click to toggle)
zabbix 1%3A7.0.10%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 272,688 kB
  • sloc: sql: 946,050; ansic: 389,440; php: 292,698; javascript: 83,388; sh: 5,680; makefile: 3,285; java: 1,420; cpp: 694; perl: 64; xml: 56
file content (367 lines) | stat: -rw-r--r-- 12,809 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
** without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
** See the GNU Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/


class Script {

	/**
	 * Executes script, handling optional manual input and confirmation.
	 *
	 * @param {string|null} scriptid                    Script ID.
	 * @param {string}      confirmation                Confirmation text.
	 * @param {Node}        trigger_element             UI element that was clicked to open overlay dialogue.
	 * @param {string|null} hostid                      Host ID.
	 * @param {string|null} eventid                     Event ID.
	 * @param {string|null} csrf_token                  CSRF token.
	 * @param {string|null} manualinput                 Manual input enabled/disabled or null.
	 * @param {string|null} manualinput_prompt          Manual input prompt text.
	 * @param {int|null}    manualinput_validator_type  Manual input type - 0 (string) or 1 (dropdown).
	 * @param {string|null} manualinput_validator       Validation rule - regular expression or list of allowed values.
	 * @param {string|null} manualinput_default_value   Default value of manual input.
	 */
	static execute(scriptid, confirmation, trigger_element, hostid = null, eventid = null, csrf_token = null,
			manualinput = null, manualinput_prompt = null, manualinput_validator_type = null,
			manualinput_validator = null, manualinput_default_value = null) {
		if (manualinput == ZBX_SCRIPT_MANUALINPUT_ENABLED) {
			const overlay = Script.#getManualInput(scriptid, confirmation, trigger_element, hostid, eventid,
				manualinput, manualinput_prompt, manualinput_validator_type, manualinput_validator,
				manualinput_default_value
			);

			overlay.$dialogue[0].addEventListener('manualinput.ready', (e) => {
				if (confirmation !== '') {
					Script.#confirm({
						dialogue_title: t('Execution confirmation'),
						confirmation: e.detail.confirmation,
						confirm_button_title: t('Execute'),
						confirm_button_enabled: hostid !== null || eventid !== null,
						trigger_element
					})
						.then(() => {
							overlayDialogueDestroy(overlay.dialogueid);

							Script.#execute(scriptid, eventid, hostid, e.detail.manualinput_value, csrf_token,
								trigger_element
							);
						})
						.catch(() => {
							overlay.unsetLoading();
							overlay.recoverFocus();
							overlay.containFocus();
						});
				}
				else {
					overlayDialogueDestroy(overlay.dialogueid);

					Script.#execute(scriptid, eventid, hostid, e.detail.manualinput_value, csrf_token, trigger_element);
				}
			});
		}
		else if (confirmation !== '') {
			Script.#confirm({
				dialogue_title: t('Execution confirmation'),
				confirmation,
				confirm_button_title: t('Execute'),
				confirm_button_enabled: hostid !== null || eventid !== null,
				trigger_element
			})
				.then(() => {
					Script.#execute(scriptid, eventid, hostid, null, csrf_token, trigger_element);
				})
				.catch(() => {});
		}
		else {
			Script.#execute(scriptid, eventid, hostid, null, csrf_token, trigger_element);
		}
	}

	/**
	 * Redirects user to the provided URL, handling optional manual input and confirmation.
	 *
	 * @param {string|null} scriptid                    Script ID.
	 * @param {string}      confirmation                Confirmation text.
	 * @param {Node}        trigger_element             UI element that was clicked to open overlay dialogue.
	 * @param {string|null} hostid                      Host ID.
	 * @param {string|null} eventid                     Event ID.
	 * @param {string|null} url                         Script URL.
	 * @param {string|null} url_target                  Script URL target.
	 * @param {string|null} manualinput                 Manual input enabled/disabled or null.
	 * @param {string|null} manualinput_prompt          Manual input prompt text.
	 * @param {int|null}    manualinput_validator_type  Manual input type - 0 (string) or 1 (dropdown).
	 * @param {string|null} manualinput_validator       Validation rule - regular expression or list of allowed values.
	 * @param {string|null} manualinput_default_value   Default value of manual input.
	 */
	static openUrl(scriptid, confirmation, trigger_element, hostid = null, eventid = null, url = null,
			url_target = '', manualinput = null, manualinput_prompt = null, manualinput_validator_type = null,
			manualinput_validator = null, manualinput_default_value = null) {
		if (manualinput == ZBX_SCRIPT_MANUALINPUT_ENABLED) {
			const overlay = Script.#getManualInput(scriptid, confirmation, trigger_element, hostid, eventid,
				manualinput, manualinput_prompt, manualinput_validator_type, manualinput_validator,
				manualinput_default_value
			);

			overlay.$dialogue[0].addEventListener('manualinput.ready', (e) => {
				const form = overlay.$dialogue.$body[0].querySelector('form');

				try {
					const url = new URL(e.detail.url, window.location.href);

					if (confirmation !== '') {
						Script.#confirm({
							dialogue_title: t('URL opening confirmation'),
							confirmation: e.detail.confirmation,
							confirm_button_title: t('Open URL'),
							confirm_button_enabled: hostid !== null || eventid !== null,
							trigger_element
						})
							.then(() => {
								overlayDialogueDestroy(overlay.dialogueid);

								if (url_target !== '') {
									window.open(url, url_target);
								}
								else {
									location.href = url.href;
								}
							})
							.catch(() => {
								overlay.unsetLoading();
								overlay.recoverFocus();
								overlay.containFocus();
							});
					}
					else {
						overlayDialogueDestroy(overlay.dialogueid);

						if (url_target !== '') {
							window.open(url, url_target);
						}
						else {
							location.href = url.href;
						}
					}
				}
				catch (exception) {
					overlay.unsetLoading();
					overlay.recoverFocus();
					overlay.containFocus();

					const messages = [sprintf(t('Invalid URL: %1$s'), e.detail.url)];
					const title = t('Cannot open URL');
					const message_box = makeMessageBox('bad', messages, title)[0];

					form.parentNode.insertBefore(message_box, form);
				}
			});
		}
		else if (confirmation !== '') {
			Script.#confirm({
				dialogue_title: t('URL opening confirmation'),
				confirmation,
				confirm_button_title: t('Open URL'),
				confirm_button_enabled: hostid !== null || eventid !== null,
				trigger_element
			})
				.then(() => {
					if (url_target !== '') {
						window.open(url, url_target);
					}
					else {
						location.href = url;
					}
				})
				.catch(() => {});
		}
		else {
			if (url_target !== '') {
				window.open(url, url_target);
			}
			else {
				location.href = url;
			}
		}
	}

	/**
	 * Open manualinput popup and retrieve user submitted manualinput value.
	 *
	 * @param {string|null} scriptid                    Script ID.
	 * @param {string}      confirmation                Confirmation text.
	 * @param {Node}        trigger_element             UI element that was clicked to open overlay dialogue.
	 * @param {string|null} hostid                      Host ID.
	 * @param {string|null} eventid                     Event ID.
	 * @param {string|null} manualinput                 Manual input enabled/disabled or null.
	 * @param {string|null} manualinput_prompt          Manual input prompt text.
	 * @param {int|null}    manualinput_validator_type  Manual input type - 0 (string) or 1 (dropdown).
	 * @param {string|null} manualinput_validator       Validation rule - regular expression or list of allowed values.
	 * @param {string|null} manualinput_default_value   Default value of manual input.
	 */
	static #getManualInput(scriptid, confirmation, trigger_element, hostid, eventid, manualinput, manualinput_prompt,
			manualinput_validator_type, manualinput_validator, manualinput_default_value) {
		const overlay = PopUp('script.userinput.edit', {
			manualinput_prompt,
			manualinput_default_value,
			manualinput_validator_type,
			manualinput_validator,
			confirmation
		}, {
			dialogueid: 'script-userinput-form',
			dialogue_class: 'modal-popup-small position-middle',
			trigger_element
		});

		let abort_controller = null;

		overlay.$dialogue[0].addEventListener('dialogue.close', () => {
			if (abort_controller !== null) {
				abort_controller.abort();
			}
		});

		overlay.$dialogue[0].addEventListener('dialogue.submit', (e) => {
			const form = overlay.$dialogue.$body[0].querySelector('form');

			const curl = new Curl('jsrpc.php');

			curl.setArgument('type', PAGE_TYPE_TEXT_RETURN_JSON);
			curl.setArgument('scriptid', scriptid);
			curl.setArgument('manualinput', e.detail.data.manualinput);

			if (hostid !== null) {
				curl.setArgument('method', 'get_scripts_by_hosts');
				curl.setArgument('hostid', hostid);
			}
			else if (eventid !== null) {
				curl.setArgument('method', 'get_scripts_by_events');
				curl.setArgument('eventid', eventid);
			}

			abort_controller = new AbortController();

			fetch(curl.getUrl(), {signal: abort_controller.signal})
				.then((response) => response.json())
				.then((response) => {
					if ('error' in response.result) {
						throw {error: response.result.error};
					}

					for (const element of form.parentNode.children) {
						if (element.matches('.msg-good, .msg-bad, .msg-warning')) {
							element.parentNode.removeChild(element);
						}
					}

					response.result.manualinput_value = e.detail.data.manualinput;

					overlay.$dialogue[0].dispatchEvent(new CustomEvent('manualinput.ready', {detail: response.result}));
				})
				.catch((exception) => {
					if (abort_controller.signal.aborted) {
						return;
					}

					overlay.unsetLoading();

					for (const element of form.parentNode.children) {
						if (element.matches('.msg-good, .msg-bad, .msg-warning')) {
							element.parentNode.removeChild(element);
						}
					}

					let title = null;
					let messages = [];

					if (typeof exception === 'object' && 'error' in exception) {
						messages = exception.error;
					}
					else {
						title = t('Unexpected server error.');
					}

					const message_box = makeMessageBox('bad', messages, title)[0];

					form.parentNode.insertBefore(message_box, form);
				})
				.finally(() => {
					abort_controller = null;
				});
		});

		return overlay;
	}

	/**
	 * Open confirmation popup.
	 *
	 * @param {string}  dialogue_title          Dialogue title.
	 * @param {string}  confirmation            Confirmation text.
	 * @param {string}  confirm_button_title    Confirmation button title.
	 * @param {boolean} confirm_button_enabled  Boolean whether confirmation button must be enabled or not.
	 * @param {object}  trigger_element         UI element that was clicked to open overlay dialogue.
	 */
	static #confirm({dialogue_title, confirmation, confirm_button_title, confirm_button_enabled, trigger_element}) {
		return new Promise((resolve, reject) => {
			const content = document.createElement('span');
			content.classList.add('confirmation-msg');
			content.textContent = confirmation;

			overlayDialogue({
				title: dialogue_title,
				content: content.outerHTML,
				class: 'modal-popup modal-popup-small position-middle',
				buttons: [
					{
						title: t('Cancel'),
						class: 'btn-alt',
						focused: !confirm_button_enabled,
						cancel: true,
						action: reject
					},
					{
						title: confirm_button_title,
						enabled: confirm_button_enabled,
						focused: confirm_button_enabled,
						action: resolve
					}
				]
			}, trigger_element);
		});
	}

	/**
	 * Execute script.
	 *
	 * @param {string}      scriptid         Script ID.
	 * @param {string|null} eventid          Event ID.
	 * @param {string|null} hostid           Host ID.
	 * @param {string|null} manualinput      Manual input value.
	 * @param {string}      csrf_token       CSRF token.
	 * @param {object}      trigger_element  UI element that was clicked to open overlay dialogue.
	 */
	static #execute(scriptid, eventid, hostid, manualinput, csrf_token, trigger_element) {
		if (hostid === null && eventid === null) {
			return;
		}

		PopUp('popup.scriptexec', {
			scriptid,
			eventid: eventid ?? undefined,
			hostid: hostid ?? undefined,
			manualinput: manualinput ?? undefined,
			[CSRF_TOKEN_NAME]: csrf_token
		}, {
			dialogue_class: 'modal-popup-medium', trigger_element
		});
	}
}