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// Load global vars
17global $config;
18
19require_once ($config['homedir'] . "/include/functions_alerts.php");
20require_once ($config['homedir'] . '/include/functions_users.php');
21require_once ($config['homedir'] . '/include/functions_groups.php');
22enterprise_include_once ('meta/include/functions_alerts_meta.php');
23
24check_login ();
25
26enterprise_hook('open_meta_frame');
27
28if (! check_acl ($config['id_user'], 0, "LM")) {
29	db_pandora_audit("ACL Violation",
30		"Trying to access Alert actions");
31	require ("general/noaccess.php");
32	exit;
33}
34
35if (is_ajax ()) {
36	$get_alert_action = (bool) get_parameter ('get_alert_action');
37	if ($get_alert_action) {
38		$id = (int) get_parameter ('id');
39		$action = alerts_get_alert_action ($id);
40		$action['command'] = alerts_get_alert_action_alert_command ($action['id']);
41
42		echo json_encode ($action);
43	}
44	return;
45}
46
47$update_action = (bool) get_parameter ('update_action');
48$create_action = (bool) get_parameter ('create_action');
49$delete_action = (bool) get_parameter ('delete_action');
50$copy_action = (bool) get_parameter ('copy_action');
51$pure = get_parameter('pure', 0);
52
53if (defined('METACONSOLE')) {
54	$sec = 'advanced';
55}
56else {
57	$sec = 'galertas';
58}
59
60if ((!$copy_action) && (!$delete_action) && (!$update_action)) {
61	// Header
62	if (defined('METACONSOLE')) {
63		alerts_meta_print_header ();
64	}
65	else {
66		ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
67	}
68}
69
70
71if ($copy_action) {
72	$id = get_parameter ('id');
73
74	$al_action = alerts_get_alert_action ($id);
75
76	if ($al_action !== false) {
77		// If user tries to copy an action with group=ALL
78		if ($al_action['id_group'] == 0) {
79			// then must have "PM" access privileges
80			if (! check_acl ($config['id_user'], 0, "PM")) {
81				db_pandora_audit("ACL Violation",
82					"Trying to access Alert Management");
83				require ("general/noaccess.php");
84				exit;
85			}
86			else {
87				// Header
88				if (defined('METACONSOLE')) {
89					alerts_meta_print_header ();
90				}
91				else {
92					ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
93				}
94			}
95		} // If user tries to copy an action of others groups
96		else {
97			$own_info = get_user_info ($config['id_user']);
98			if ($own_info['is_admin'] || check_acl ($config['id_user'], 0, "PM"))
99				$own_groups = array_keys(users_get_groups($config['id_user'], "LM"));
100			else
101				$own_groups = array_keys(users_get_groups($config['id_user'], "LM", false));
102			$is_in_group = in_array($al_action['id_group'], $own_groups);
103			// Then action group have to be in his own groups
104			if ($is_in_group) {
105				// Header
106				if (defined('METACONSOLE')) {
107					alerts_meta_print_header ();
108				}
109				else {
110					ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
111				}
112			}
113			else {
114				db_pandora_audit("ACL Violation",
115				"Trying to access Alert Management");
116				require ("general/noaccess.php");
117				exit;
118			}
119		}
120	}
121	else {
122		// Header
123		if (defined('METACONSOLE')) {
124			alerts_meta_print_header ();
125		}
126		else {
127			ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
128		}
129	}
130	$result = alerts_clone_alert_action ($id);
131
132	if ($result) {
133		db_pandora_audit("Command management", "Duplicate alert action " . $id . " clone to " . $result);
134	}
135	else {
136		db_pandora_audit("Command management", "Fail try to duplicate alert action " . $id);
137	}
138
139	ui_print_result_message ($result,
140		__('Successfully copied'),
141		__('Could not be copied'));
142}
143
144if ($create_action) {
145	$name = (string) get_parameter ('name');
146	$id_alert_command = (int) get_parameter ('id_command');
147
148	$fields_descriptions = array();
149	$fields_values = array();
150	$info_fields = '';
151	$values = array();
152	for($i=1;$i<=10;$i++) {
153		$values['field'.$i] = (string) get_parameter ('field'.$i.'_value');
154		$info_fields .= ' Field'.$i.': ' . $values['field'.$i];
155		$values['field'.$i.'_recovery'] = (string) get_parameter ('field'.$i.'_recovery_value');
156		$info_fields .= ' Field'.$i.'Recovery: ' . $values['field'.$i.'_recovery'];
157	}
158
159	$values['id_group'] = (string) get_parameter ('group');
160	$values['action_threshold'] = (int) get_parameter ('action_threshold');
161
162	$name_check = db_get_value ('name', 'talert_actions', 'name', $name);
163
164	if ($name_check) {
165		$result = '';
166	}
167	else {
168		$result = alerts_create_alert_action ($name, $id_alert_command,
169			$values);
170
171		$info = 'Name: ' . $name . ' ID alert Command: ' . $id_alert_command .
172			$info_fields . ' Group: ' . $values['id_group'] .
173			' Action threshold: ' . $values['action_threshold'];
174	}
175
176	if ($result) {
177		db_pandora_audit("Command management", "Create alert action #" . $result, false, false, $info);
178	}
179	else {
180		db_pandora_audit("Command management", "Fail try to create alert action", false, false);
181	}
182
183	ui_print_result_message ($result,
184		__('Successfully created'),
185		__('Could not be created'));
186}
187
188if ($update_action) {
189	$id = (string) get_parameter ('id');
190
191	$al_action = alerts_get_alert_action ($id);
192
193	if ($al_action !== false) {
194		if ($al_action['id_group'] == 0) {
195			if (! check_acl ($config['id_user'], 0, "PM")) {
196				db_pandora_audit("ACL Violation",
197					"Trying to access Alert Management");
198				require ("general/noaccess.php");
199				exit;
200			}
201			else {
202				// Header
203				if (defined('METACONSOLE')) {
204					alerts_meta_print_header ();
205				}
206				else {
207					ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
208				}
209			}
210		}
211	}
212	else {
213		// Header
214		if (defined('METACONSOLE')) {
215			alerts_meta_print_header ();
216		}
217		else {
218			ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alerts_config", true);
219		}
220	}
221
222
223	$name = (string) get_parameter ('name');
224	$id_alert_command = (int) get_parameter ('id_command');
225	$group = get_parameter ('group');
226	$action_threshold = (int) get_parameter ('action_threshold');
227
228	$info_fields = '';
229	$values = array();
230
231	for ($i = 1; $i <= 10; $i++) {
232		$values['field'.$i] = (string) get_parameter ('field'.$i.'_value');
233		$info_fields .= ' Field1: ' . $values['field'.$i];
234		$values['field'.$i.'_recovery'] = (string) get_parameter ('field'.$i.'_recovery_value');
235		$info_fields .= ' Field'.$i.'Recovery: ' . $values['field'.$i.'_recovery'];
236	}
237
238	$values['name'] = $name;
239	$values['id_alert_command'] = $id_alert_command;
240	$values['id_group'] = $group;
241	$values['action_threshold'] = $action_threshold;
242
243	if (!$name) {
244		$result = '';
245	}
246	else {
247		$result = alerts_update_alert_action ($id, $values);
248
249		$info = 'Name: ' . $name . ' ID alert Command: ' . $id_alert_command .
250			$info_fields . ' Group: ' . $group .
251			' Action threshold: ' . $action_threshold;
252	}
253
254	if ($result) {
255		db_pandora_audit("Command management", "Update alert action #" . $id, false, false, json_encode($values));
256	}
257	else {
258		db_pandora_audit("Command management", "Fail try to update alert action #" . $id, false, false, json_encode($values));
259	}
260
261	ui_print_result_message ($result,
262		__('Successfully updated'),
263		__('Could not be updated'));
264}
265
266if ($delete_action) {
267	$id = get_parameter ('id');
268
269	$al_action = alerts_get_alert_action ($id);
270
271	if ($al_action !== false) {
272		// If user tries to delete an action with group=ALL
273		if ($al_action['id_group'] == 0) {
274			// then must have "PM" access privileges
275			if (! check_acl ($config['id_user'], 0, "PM")) {
276				db_pandora_audit("ACL Violation",
277					"Trying to access Alert Management");
278				require ("general/noaccess.php");
279				exit;
280			}
281			else {
282				// Header
283				if (defined('METACONSOLE')) {
284					alerts_meta_print_header ();
285				}
286				else {
287					ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alert_action", true);
288				}
289			}
290		// If user tries to delete an action of others groups
291		}
292		else {
293			$own_info = get_user_info ($config['id_user']);
294			if ($own_info['is_admin'] || check_acl ($config['id_user'], 0, "PM"))
295				$own_groups = array_keys(users_get_groups($config['id_user'], "LM"));
296			else
297				$own_groups = array_keys(users_get_groups($config['id_user'], "LM", false));
298			$is_in_group = in_array($al_action['id_group'], $own_groups);
299			// Then action group have to be in his own groups
300			if ($is_in_group) {
301				// Header
302				if (defined('METACONSOLE')) {
303					alerts_meta_print_header ();
304				}
305				else {
306					ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "alert_action", true);
307				}
308			}
309			else {
310				db_pandora_audit("ACL Violation",
311				"Trying to access Alert Management");
312				require ("general/noaccess.php");
313				exit;
314			}
315		}
316	}
317	else
318		// Header
319		ui_print_page_header (__('Alerts').' &raquo; '.__('Alert actions'), "images/gm_alerts.png", false, "", true);
320
321
322	$result = alerts_delete_alert_action ($id);
323
324	if ($result) {
325		db_pandora_audit("Command management", "Delete alert action #" . $id);
326	}
327	else {
328		db_pandora_audit("Command management", "Fail try to delete alert action #" . $id);
329	}
330
331	ui_print_result_message ($result,
332		__('Successfully deleted'),
333		__('Could not be deleted'));
334}
335
336$table->width = '100%';
337$table->class = 'databox data';
338$table->data = array ();
339$table->head = array ();
340$table->head[0] = __('Name');
341$table->head[1] = __('Group');
342$table->head[2] = __('Copy');
343$table->head[3] = __('Delete');
344$table->style = array ();
345$table->style[0] = 'font-weight: bold';
346$table->size = array ();
347$table->size[1] = '200px';
348$table->size[2] = '40px';
349$table->size[3] = '40px';
350$table->align = array ();
351$table->align[1] = 'left';
352$table->align[2] = 'left';
353$table->align[3] = 'left';
354
355$filter = array();
356if (!is_user_admin($config['id_user']))
357	$filter['id_group'] = array_keys(users_get_groups(false, "LM"));
358
359$actions = db_get_all_rows_filter ('talert_actions', $filter);
360if ($actions === false)
361	$actions = array ();
362
363$rowPair = true;
364$iterator = 0;
365foreach ($actions as $action) {
366	if ($rowPair)
367		$table->rowclass[$iterator] = 'rowPair';
368	else
369		$table->rowclass[$iterator] = 'rowOdd';
370	$rowPair = !$rowPair;
371	$iterator++;
372
373	$data = array ();
374
375	$data[0] = '<a href="index.php?sec='.$sec.'&sec2=godmode/alerts/configure_alert_action&id='.$action['id'].'&pure='.$pure.'">'.
376		$action['name'].'</a>';
377	$data[1] = ui_print_group_icon ($action["id_group"], true) .'&nbsp;';
378	$hack_id_group_all = $action["id_group"];
379	if ($hack_id_group_all == 0) {
380		//To avoid check all groups instead the pseudo-group all
381		$hack_id_group_all = -1;
382	}
383	if (check_acl($config['id_user'], $hack_id_group_all, "LM")) {
384		$data[2] = '<a href="index.php?sec='.$sec.'&sec2=godmode/alerts/alert_actions&amp;copy_action=1&amp;id='.$action['id'].'&pure='.$pure.'"
385			onClick="if (!confirm(\''.__('Are you sure?').'\')) return false;">' .
386			html_print_image("images/copy.png", true) . '</a>';
387		$data[3] = '<a href="index.php?sec='.$sec.'&sec2=godmode/alerts/alert_actions&delete_action=1&id='.$action['id'].'&pure='.$pure.'"
388			onClick="if (!confirm(\''.__('Are you sure?').'\')) return false;">'.
389			html_print_image("images/cross.png", true) . '</a>';
390	}
391
392	array_push ($table->data, $data);
393}
394if (isset($data)) {
395	html_print_table ($table);
396}
397else {
398	ui_print_info_message ( array('no_close'=>true, 'message'=>  __('No alert actions configured') ) );
399}
400
401echo '<div class="action-buttons" style="width: '.$table->width.'">';
402echo '<form method="post" action="index.php?sec='.$sec.'&sec2=godmode/alerts/configure_alert_action&pure='.$pure.'">';
403html_print_submit_button (__('Create'), 'create', false, 'class="sub next"');
404html_print_input_hidden ('create_alert', 1);
405echo '</form>';
406echo '</div>';
407
408enterprise_hook('close_meta_frame');
409
410?>
411