1<?php
2
3// Pandora FMS - http://pandorafms.com
4// ==================================================
5// Copyright (c) 2005-2009 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
17check_login ();
18
19if (! check_acl ($config['id_user'], 0, "AW")) {
20	db_pandora_audit("ACL Violation",
21		"Trying to access massive agent deletion section");
22	require ("general/noaccess.php");
23	return;
24}
25
26require_once ('include/functions_agents.php');
27require_once ('include/functions_alerts.php');
28require_once ('include/functions_users.php');
29
30if (is_ajax ()) {
31	$get_alerts = (bool) get_parameter ('get_alerts');
32
33	if ($get_alerts) {
34		$id_agents = get_parameter ('id_agents');
35		if (empty($id_agents)) {
36			echo json_encode ('');
37			return;
38		}
39
40		if (is_array($id_agents) && count($id_agents) == 1 && $id_agents[0] == '') {
41			$id_agents = false;
42		}
43
44		$alert_templates = agents_get_alerts_simple ($id_agents);
45		echo json_encode (index_array ($alert_templates, 'id_alert_template', 'template_name'));
46		return;
47	}
48	return;
49}
50
51$id_group = (int) get_parameter ('id_group');
52$id_agents = get_parameter ('id_agents');
53$id_alert_templates = (array) get_parameter ('id_alert_templates');
54$recursion = get_parameter ('recursion');
55
56$add = (bool) get_parameter_post ('add');
57
58if ($add) {
59	if (empty($id_agents) || $id_agents[0] == 0)
60		ui_print_result_message (false, '', __('Could not be added').". ".__('No agents selected'));
61	else {
62		$actions = get_parameter ('action');
63		$fires_min = (int) get_parameter ('fires_min');
64		$fires_max = (int) get_parameter ('fires_max');
65
66		if (!empty($actions)) {
67			$agent_alerts = agents_get_alerts($id_agents);
68			$cont = 0;
69			$agent_alerts_id = array();
70			foreach ($agent_alerts['simple'] as $agent_alert) {
71				if (in_array($agent_alert['id_alert_template'], $id_alert_templates)) {
72					$agent_alerts_id[$cont] = $agent_alert['id'];
73					$cont = $cont + 1;
74				}
75			}
76
77			$options = array();
78
79			if ($fires_min > 0)
80				$options['fires_min'] = $fires_min;
81			if ($fires_max > 0)
82				$options['fires_max'] = $fires_max;
83
84			if (empty($agent_alerts_id)) {
85				ui_print_result_message (false, '', __('Could not be added').". ".__('No alerts selected'));
86			}
87			else {
88				$results = true;
89				foreach ($agent_alerts_id as $agent_alert_id) {
90					foreach ($actions as $action) {
91						$result = alerts_add_alert_agent_module_action($agent_alert_id, $action, $options);
92						if ($result === false)
93							$results = false;
94					}
95				}
96
97				db_pandora_audit("Massive management", "Add alert action " . json_encode($id_agents), false, false, 'Agents: ' .
98					json_encode($id_agents) . ' Alerts : ' . json_encode($agent_alerts) .
99					' Fires Min: ' . $fires_min . ' Fires Max: ' . $fires_max . ' Actions: ' . implode(',',$actions));
100
101				ui_print_result_message ($results, __('Successfully added'), __('Could not be added'));
102			}
103		}
104		else {
105			ui_print_result_message (false, '', __('Could not be added').". ".__('No action selected'));
106		}
107	}
108
109}
110
111$groups = users_get_groups ();
112$own_info = get_user_info($config['id_user']);
113if (!$own_info['is_admin'] && !check_acl ($config['id_user'], 0, "AW"))
114	$return_all_group = false;
115else
116	$return_all_group = true;
117
118// Avoid php warning
119if (empty($alert_templates)) {
120	$alert_templates = '';
121}
122
123$table->id = 'delete_table';
124$table->width = '98%';
125$table->data = array ();
126$table->style = array ();
127$table->style[0] = 'font-weight: bold; vertical-align:top';
128$table->style[2] = 'font-weight: bold';
129$table->size = array ();
130$table->size[0] = '15%';
131$table->size[1] = '35%';
132$table->size[2] = '15%';
133$table->size[3] = '35%';
134
135$table->data = array ();
136$table->data[0][0] = __('Group');
137$table->data[0][1] = html_print_select_groups(false, "AW", $return_all_group,
138	'id_group', $id_group, false, '', '', true);
139$table->data[0][2] = __('Group recursion');
140$table->data[0][3] = html_print_checkbox ("recursion", 1, $recursion, true, false);
141
142$table->data[1][0] = __('Agents with templates');
143$table->data[1][0] .= '<span id="agent_loading" class="invisible">';
144$table->data[1][0] .= html_print_image('images/spinner.png', true);
145$table->data[1][0] .= '</span>';
146$table->data[1][1] = html_print_select (array(),'id_agents[]', 0, false, __('Any'), '', true, true);
147
148$table->data[2][0] = __('Alert templates');
149$table->data[2][0] .= '<span id="template_loading" class="invisible">';
150$table->data[2][0] .= html_print_image('images/spinner.png', true);
151$table->data[2][0] .= '</span>';
152$table->data[2][1] = html_print_select (array(), 'id_alert_templates[]', '', '', '', '', true, true, true, '', $alert_templates == 0);
153
154$actions = alerts_get_alert_actions ();
155$table->data[3][0] = __('Action');
156$table->data[3][1] = html_print_select ($actions, 'action[]', '', '', '', '', true, true);
157$table->data[3][1] .= '<span><a href="#" class="show_advanced_actions">'.__('Advanced options').' &raquo; </a></span>';
158$table->data[3][1] .= '<span id="advanced_actions" class="advanced_actions invisible">';
159$table->data[3][1] .= __('Number of alerts match from').' ';
160$table->data[3][1] .= html_print_input_text ('fires_min', 0, '', 4, 10, true);
161$table->data[3][1] .= ' '.__('to').' ';
162$table->data[3][1] .= html_print_input_text ('fires_max', 0, '', 4, 10, true);
163$table->data[3][1] .= ui_print_help_icon ("alert-matches", true);
164$table->data[3][1] .= '</span>';
165
166echo '<form method="post" id="form_alerts" action="index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=add_action_alerts">';
167html_print_table ($table);
168
169$sql = 'SELECT id_agente FROM tagente_modulo WHERE id_agente_modulo IN (SELECT id_agent_module FROM talert_template_modules)';
170$agents_with_templates = db_get_all_rows_sql($sql);
171$agents_with_templates_json = array();
172foreach ($agents_with_templates as $ag) {
173	$agents_with_templates_json[] = $ag['id_agente'];
174}
175$agents_with_templates_json = json_encode($agents_with_templates_json);
176
177echo "<input type='hidden' id='hidden-agents_with_templates' value='$agents_with_templates_json'>";
178
179echo '<div class="action-buttons" style="width: ' . $table->width . '" onsubmit="if (!confirm(\' '.__('Are you sure?').'\')) return false;">';
180html_print_input_hidden ('add', 1);
181html_print_submit_button (__('Add'), 'go', false, 'class="sub add"');
182echo '</div>';
183echo '</form>';
184
185echo '<h3 class="error invisible" id="message"> </h3>';
186
187ui_require_jquery_file ('form');
188ui_require_jquery_file ('pandora.controls');
189?>
190
191<script type="text/javascript">
192var limit_parameters_massive = <?php echo $config['limit_parameters_massive']; ?>;
193
194$(document).ready (function () {
195	$("#form_alerts").submit(function() {
196		var get_parameters_count = window.location.href.slice(
197			window.location.href.indexOf('?') + 1).split('&').length;
198		var post_parameters_count = $("#form_alerts").serializeArray().length;
199
200		var count_parameters =
201			get_parameters_count + post_parameters_count;
202
203		if (count_parameters > limit_parameters_massive) {
204			alert("<?php echo __('Unsucessful sending the data, please contact with your administrator or make with less elements.'); ?>");
205			return false;
206		}
207	});
208
209
210	update_alerts();
211
212	var recursion;
213	$("#checkbox-recursion").click(function () {
214		recursion = this.checked;
215		$("#id_group").trigger("change");
216	});
217
218	var filter_agents_json = $("#hidden-agents_with_templates").val();
219
220	$("#id_group").pandoraSelectGroupAgent ({
221		agentSelect: "select#id_agents",
222		privilege: "AW",
223		add_alert_bulk_op: true,
224		recursion: function() {return recursion},
225		filter_agents_json: filter_agents_json,
226		callbackPost: function () {
227			var $select_template = $("#id_alert_templates").disable ();
228			$("option", $select_template).remove ();
229		}
230	});
231
232	$("#id_agents").change (function () {
233		update_alerts();
234	});
235
236	function update_alerts() {
237		var idAgents = Array();
238		jQuery.each ($("#id_agents option:selected"), function (i, val) {
239			idAgents.push($(val).val());
240		});
241		$("#template_loading").show();
242
243		var $select_template = $("#id_alert_templates").disable ();
244
245		jQuery.post ("ajax.php", {
246				"page" : "godmode/massive/massive_add_action_alerts",
247				"get_alerts" : 1,
248				"id_agents[]" : idAgents
249			},
250			function (data, status) {
251				$("option", $select_template).remove ();
252				options = "";
253				jQuery.each (data, function (id, value) {
254					options += "<option value=\""+id+"\">"+value+"</option>";
255				});
256
257				if (options == "") {
258					options += "<option><?php echo __('None'); ?></option>";
259				}
260
261				$("#id_alert_templates").append (options);
262				$("#template_loading").hide ();
263				$select_template.enable ();
264			},
265			"json"
266		);
267	}
268
269	$("a.show_advanced_actions").click (function () {
270		/* It can be done in two different sites, so it must use two different selectors */
271		actions = $(this).parents ("form").children ("span.advanced_actions");
272		if (actions.length == 0)
273			actions = $(this).parents ("div").children ("span.advanced_actions")
274		$("#advanced_actions").removeClass("advanced_actions invisible");
275		$(this).remove ();
276
277		return false;
278	});
279
280	$('#id_group').trigger('change');
281});
282/* ]]> */
283</script>
284