1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2010 Artica Soluciones Tecnologicas
6// Please see http://pandorafms.org for full contribution list
7
8// This program is free software; you can redistribute it and/or
9// modify it under the terms of the GNU General Public License
10// as published by the Free Software Foundation for version 2.
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
17// Load global vars
18global $config;
19
20require_once ($config['homedir'] . "/include/functions_alerts.php");
21enterprise_include_once ('meta/include/functions_alerts_meta.php');
22
23check_login ();
24
25if (! check_acl ($config['id_user'], 0, "LM")) {
26	db_pandora_audit("ACL Violation",
27		"Trying to access Alert Management");
28	require ("general/noaccess.php");
29	exit;
30}
31
32if (is_metaconsole())
33	$sec = 'advanced';
34else
35	$sec = 'galertas';
36
37$pure = (int)get_parameter('pure', 0);
38$update_command = (bool) get_parameter ('update_command');
39$create_command = (bool) get_parameter ('create_command');
40$delete_command = (bool) get_parameter ('delete_command');
41
42if (is_ajax ()) {
43	$get_alert_command = (bool) get_parameter ('get_alert_command');
44	if ($get_alert_command) {
45		$id = (int) get_parameter ('id', 0);
46		$get_recovery_fields = (int)  get_parameter('get_recovery_fields', 1);
47
48		// If command ID is not provided, check for action id
49		if ($id == 0) {
50			$id_action = (int) get_parameter ('id_action');
51			$id = alerts_get_alert_action_alert_command_id($id_action);
52		}
53
54		$command = alerts_get_alert_command ($id);
55
56		// If is setted a description, we change the carriage return by <br> tags
57		if (isset($command['description'])) {
58			$command['description'] = io_safe_input(str_replace("\r\n","<br>", io_safe_output($command['description'])));
59		}
60
61		// Get the html rows of the fields form
62		switch ($config["dbtype"]) {
63			case "mysql":
64			case "postgresql":
65				// Descriptions are stored in json
66				$fields_descriptions = empty($command['fields_descriptions']) ?
67					'' : json_decode(io_safe_output($command['fields_descriptions']), true);
68
69				// Fields values are stored in json
70				$fields_values = empty($command['fields_values']) ?
71					'' : io_safe_output(json_decode($command['fields_values'], true));
72				break;
73			case "oracle":
74				// Descriptions are stored in json
75				$description_field = str_replace("\\\"","\"",$command['fields_descriptions']);
76				$description_field = str_replace("\\","",$description_field);
77
78				$fields_descriptions = empty($command['fields_descriptions']) ?
79					'' : json_decode(io_safe_output($description_field), true);
80
81				// Fields values are stored in json
82				$values_fields = str_replace("\\\"","\"",$command['fields_values']);
83				$values_fields = str_replace("\\","",$values_fields);
84
85				$fields_values = empty($command['fields_values']) ?
86					'' : io_safe_output(json_decode($values_fields, true));
87
88				break;
89		}
90
91		$fields_rows = array();
92		for ($i = 1; $i <= 10; $i++) {
93
94			if (($i == 5) && ($command['id'] == 3)){
95				continue;
96			}
97
98			$field_description = $fields_descriptions[$i - 1];
99			$field_value = $fields_values[$i - 1];
100
101			if (!empty($field_description)) {
102				//If the value is 5,  this because severity in snmp alerts is not permit to show
103				if (($i > 5) && ($command['id'] == 3)){
104					$fdesc = $field_description .
105						' <br><span style="font-size:xx-small; font-weight:normal;">' . sprintf(__('Field %s'), $i - 1) . '</span>';
106				}
107				else{
108					$fdesc = $field_description .
109						' <br><span style="font-size:xx-small; font-weight:normal;">' . sprintf(__('Field %s'), $i) . '</span>';
110				}
111				//If the field is the number one, print the help message
112				if ($i == 1) {
113					$fdesc .= ui_print_help_icon ('alert_macros', true);
114				}
115			}
116			else {
117				// If the macro hasn't description and doesnt appear in command, set with empty description to dont show it
118				if (($i > 5) && ($command['id'] == 3)){
119					if (substr_count($command['command'], "_field" . $i - 1 . "_") > 0) {
120						$fdesc = sprintf(__('Field %s'), $i - 1);
121					}
122					else {
123						$fdesc = '';
124					}
125				}
126				else{
127					if (substr_count($command['command'], "_field" . $i . "_") > 0) {
128						$fdesc = sprintf(__('Field %s'), $i);
129					}
130					else {
131						$fdesc = '';
132					}
133				}
134			}
135
136			if (!empty($field_value)) {
137				$field_value = io_safe_output($field_value);
138				// HTML type
139				if (preg_match ("/^_html_editor_$/i", $field_value)) {
140
141					$editor_type_chkbx = "<div style=\"padding: 4px 0px;\"><b><small>";
142					$editor_type_chkbx .= __('Basic') . "&nbsp;&nbsp;";
143					$editor_type_chkbx .= html_print_radio_button_extended ('editor_type_value_'.$i, 0, '', false, false, "removeTinyMCE('textarea_field".$i."_value')", '', true);
144					$editor_type_chkbx .= "&nbsp;&nbsp;&nbsp;&nbsp;";
145					$editor_type_chkbx .= __('Advanced') . "&nbsp;&nbsp;";
146					$editor_type_chkbx .= html_print_radio_button_extended ('editor_type_value_'.$i, 0, '', true, false, "addTinyMCE('textarea_field".$i."_value')", '', true);
147					$editor_type_chkbx .= "</small></b></div>";
148					$ffield = $editor_type_chkbx;
149					$ffield .= html_print_textarea ('field'.$i.'_value', 1, 1, '', 'class="fields"', true);
150
151					$editor_type_chkbx = "<div style=\"padding: 4px 0px;\"><b><small>";
152					$editor_type_chkbx .= __('Basic') . "&nbsp;&nbsp;";
153					$editor_type_chkbx .= html_print_radio_button_extended ('editor_type_recovery_value_'.$i, 0, '', false, false, "removeTinyMCE('textarea_field".$i."_recovery_value')", '', true);
154					$editor_type_chkbx .= "&nbsp;&nbsp;&nbsp;&nbsp;";
155					$editor_type_chkbx .= __('Advanced') . "&nbsp;&nbsp;";
156					$editor_type_chkbx .= html_print_radio_button_extended ('editor_type_recovery_value_'.$i, 0, '', true, false, "addTinyMCE('textarea_field".$i."_recovery_value')", '', true);
157					$editor_type_chkbx .= "</small></b></div>";
158					$rfield = $editor_type_chkbx;
159					$rfield .= html_print_textarea ('field'.$i.'_recovery_value', 1, 1, '', 'class="fields_recovery"', true);
160				}
161				// Select type
162				else {
163					$fields_value_select = array();
164					$fv = explode(';', $field_value);
165
166					if (count($fv) > 1) {
167						if (!empty($fv)) {
168							foreach ($fv as $fv_option) {
169								$fv_option = explode(',', $fv_option);
170
171								if (empty($fv_option))
172									continue;
173
174								if (!isset($fv_option[1]))
175									$fv_option[1] = $fv_option[0];
176
177								$fields_value_select[$fv_option[0]] = $fv_option[1];
178							}
179						}
180
181						$ffield = html_print_select($fields_value_select,
182							'field'.$i.'_value', '', '', '', 0, true, false, false, 'fields');
183						$rfield = html_print_select($fields_value_select,
184							'field'.$i.'_recovery_value', '', '', '', 0, true, false, false, 'fields_recovery');
185					}
186					else {
187						$ffield = html_print_textarea ('field' . $i . '_value',1, 1, $fv[0],
188											'style="min-height:40px" class="fields"', true);
189						$rfield = html_print_textarea ('field' . $i . '_recovery_value', 1, 1, $fv[0],
190											'style="min-height:40px" class="fields_recovery"', true);
191					}
192				}
193			}
194			else {
195				$ffield = html_print_textarea ('field' . $i . '_value',
196					1, 1, '', 'style="min-height:40px" class="fields"', true);
197				$rfield = html_print_textarea (
198					'field' . $i . '_recovery_value', 1, 1, '',
199					'style="min-height:40px" class="fields_recovery"', true);
200			}
201
202
203			// The empty descriptions will be ignored
204			if ($fdesc == '') {
205				$fields_rows[$i] = '';
206			}
207			else {
208				$fields_rows[$i] =
209					'<tr id="table_macros-field' . $i . '" class="datos">';
210				$fields_rows[$i] .=	'<td style="font-weight:bold;width:20%" class="datos">' . $fdesc . '</td>';
211				$fields_rows[$i] .=	'<td class="datos">' . $ffield . '</td>';
212				if ($get_recovery_fields) {
213					$fields_rows[$i] .=	'<td class="datos recovery_col">' . $rfield . '</td>';
214				}
215				$fields_rows[$i] .=	'</tr>';
216			}
217		}
218
219		//If command is PandoraFMS event, field 5 must be empty because "severity" must be set by the alert
220		if ($command['id'] == 3){
221			$fields_rows[5] = '';
222		}
223
224		$command['fields_rows'] = $fields_rows;
225
226		echo json_encode ($command);
227	}
228	return;
229}
230
231enterprise_hook('open_meta_frame');
232
233if ($update_command) {
234	require_once("configure_alert_command.php");
235	return;
236}
237
238// Header
239if (defined('METACONSOLE'))
240	alerts_meta_print_header();
241else
242	ui_print_page_header (__('Alerts').' &raquo; '.__('Alert commands'), "images/gm_alerts.png", false, "alerts_config", true);
243
244
245
246
247if ($create_command) {
248	$name = (string) get_parameter ('name');
249	$command = (string) get_parameter ('command');
250	$description = (string) get_parameter ('description');
251
252	$fields_descriptions = array();
253	$fields_values = array();
254	$info_fields = '';
255	$values = array();
256	for ($i=1;$i<=10;$i++) {
257		$fields_descriptions[] = (string) get_parameter ('field'.$i.'_description');
258		$fields_values[] = (string) get_parameter ('field'.$i.'_values');
259		$info_fields .= ' Field'.$i.': ' . $fields_values[$i - 1];
260	}
261
262	$values['fields_values'] = io_json_mb_encode($fields_values);
263	$values['fields_descriptions'] = io_json_mb_encode($fields_descriptions);
264	$values['description'] = $description;
265
266	$name_check = db_get_value ('name', 'talert_commands', 'name', $name);
267
268	if (!$name_check) {
269		$result = alerts_create_alert_command ($name, $command,
270			$values);
271
272		$info = 'Name: ' . $name . ' Command: ' . $command . ' Description: ' . $description. ' ' .$info_fields;
273	}
274	else {
275		$result = '';
276	}
277
278	if ($result) {
279		db_pandora_audit("Command management", "Create alert command #" . $result, false, false, $info);
280	}
281	else {
282		db_pandora_audit("Command management", "Fail try to create alert command", false, false);
283	}
284
285	ui_print_result_message ($result,
286		__('Successfully created'),
287		__('Could not be created'));
288}
289
290
291if ($delete_command) {
292	$id = (int) get_parameter ('id');
293
294	// Internal commands cannot be deleted
295	if (alerts_get_alert_command_internal ($id)) {
296		db_pandora_audit("ACL Violation",
297			"Trying to access Alert Management");
298		require ("general/noaccess.php");
299		return;
300	}
301
302	$result = alerts_delete_alert_command ($id);
303
304	if ($result) {
305		db_pandora_audit("Command management", "Delete alert command #" . $id);
306	}
307	else {
308		db_pandora_audit("Command management", "Fail try to delete alert command #" . $id);
309	}
310
311	ui_print_result_message ($result,
312		__('Successfully deleted'),
313		__('Could not be deleted'));
314
315
316}
317
318$table->width = '100%';
319$table->class = 'databox data';
320
321$table->data = array ();
322$table->head = array ();
323$table->head[0] = __('Name');
324$table->head[1] = __('ID');
325$table->head[2] = __('Description');
326$table->head[3] = __('Delete');
327$table->style = array ();
328$table->style[0] = 'font-weight: bold';
329$table->size = array ();
330$table->size[3] = '40px';
331$table->align = array ();
332$table->align[3] = 'left';
333
334$commands = db_get_all_rows_in_table ('talert_commands');
335if ($commands === false)
336	$commands = array ();
337
338foreach ($commands as $command) {
339	$data = array ();
340
341	$data[0] = '<span style="font-size: 7.5pt">';
342	if (! $command['internal'])
343		$data[0] .= '<a href="index.php?sec='.$sec.'&sec2=godmode/alerts/configure_alert_command&id='.$command['id'].'&pure='.$pure.'">'.
344			$command['name'].'</a>';
345	else
346		$data[0] .= $command['name'];
347	$data[0] .= '</span>';
348	$data[1] = $command['id'];
349	$data[2] = str_replace("\r\n","<br>",
350		io_safe_output($command['description']));
351	$data[3] = '';
352	if (! $command['internal']) {
353		$data[3] = '<a href="index.php?sec='.$sec.'&sec2=godmode/alerts/alert_commands&delete_command=1&id='.$command['id'].'&pure='.$pure.'"
354			onClick="if (!confirm(\''.__('Are you sure?').'\')) return false;">'.
355			html_print_image("images/cross.png", true) . '</a>';
356	}
357
358	array_push ($table->data, $data);
359}
360
361if (isset($data)) {
362	html_print_table ($table);
363}
364else {
365	ui_print_info_message ( array('no_close'=>true, 'message'=>  __('No alert commands configured') ) );
366}
367
368echo '<div class="action-buttons" style="width: ' . $table->width . '">';
369echo '<form method="post" action="index.php?sec=' . $sec . '&sec2=godmode/alerts/configure_alert_command&pure='.$pure.'">';
370html_print_submit_button (__('Create'), 'create', false, 'class="sub next"');
371html_print_input_hidden ('create_alert', 1);
372echo '</form>';
373echo '</div>';
374
375enterprise_hook('close_meta_frame');
376
377?>
378