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_modules.php');
29require_once ('include/functions_users.php');
30
31function process_manage_delete ($id_agents) {
32	if (empty ($id_agents)) {
33		ui_print_error_message(__('No agents selected'));
34		return false;
35	}
36
37	$id_agents = (array) $id_agents;
38
39	$copy_modules = (bool) get_parameter ('copy_modules');
40	$copy_alerts = (bool) get_parameter ('copy_alerts');
41
42	$error = false;
43	$count_deleted = 0;
44	$agent_id_restore = 0;
45	foreach ($id_agents as $id_agent) {
46		$success = agents_delete_agent ($id_agent);
47		if (! $success) {
48			$agent_id_restore = $id_agent;
49			break;
50		}
51		$count_deleted++;
52	}
53
54	if (! $success) {
55		ui_print_error_message(
56			sprintf(
57				__('There was an error deleting the agent, the operation has been cancelled Could not delete agent %s'),
58				agents_get_name ($agent_id_restore)));
59
60		return false;
61	}
62	else {
63		ui_print_success_message(sprintf(__('Successfully deleted (%s)',
64			$count_deleted)));
65
66		return true;
67	}
68}
69
70$id_group = (int) get_parameter ('id_group');
71$id_agents = get_parameter ('id_agents');
72$recursion = get_parameter('recursion');
73
74$delete = (bool) get_parameter_post ('delete');
75
76if ($delete) {
77	$result = process_manage_delete ($id_agents);
78
79	if ($result) {
80		db_pandora_audit("Massive management", "Delete agent ", false, false,
81			'Agent: ' . json_encode($id_agents));
82	}
83	else {
84		db_pandora_audit("Massive management", "Fail try to delete agent", false, false,
85			'Agent: ' . json_encode($id_agents));
86	}
87}
88
89$groups = users_get_groups ();
90
91$table->id = 'delete_table';
92$table->class = 'databox filters';
93$table->width = '100%';
94$table->data = array ();
95$table->style = array ();
96$table->style[0] = 'font-weight: bold;';
97$table->style[2] = 'font-weight: bold';
98$table->size = array ();
99$table->size[0] = '15%';
100$table->size[1] = '35%';
101$table->size[2] = '15%';
102$table->size[3] = '35%';
103
104$table->data = array ();
105$table->data[0][0] = __('Group');
106$table->data[0][1] = html_print_select_groups(false, "AW", true,
107	'id_group', $id_group, false, '', '', true);
108$table->data[0][2] = __('Group recursion');
109$table->data[0][3] = html_print_checkbox ("recursion", 1, $recursion,
110	true, false);
111
112$status_list = array ();
113$status_list[AGENT_STATUS_NORMAL] = __('Normal');
114$status_list[AGENT_STATUS_WARNING] = __('Warning');
115$status_list[AGENT_STATUS_CRITICAL] = __('Critical');
116$status_list[AGENT_STATUS_UNKNOWN] = __('Unknown');
117$status_list[AGENT_STATUS_NOT_NORMAL] = __('Not normal');
118$status_list[AGENT_STATUS_NOT_INIT] = __('Not init');
119$table->data[1][0] = __('Status');
120$table->data[1][1] = html_print_select($status_list, 'status_agents', 'selected',
121	'', __('All'), AGENT_STATUS_ALL, true);
122
123$table->data[2][0] = __('Agents');
124$table->data[2][0] .= '<span id="agent_loading" class="invisible">';
125$table->data[2][0] .= html_print_image('images/spinner.png', true);
126$table->data[2][0] .= '</span>';
127$table->data[2][1] = html_print_select(
128	agents_get_group_agents(array_keys (users_get_groups ($config["id_user"], "AW", false)), false, "none"),
129	'id_agents[]', 0, false, '', '', true, true);
130
131echo '<form method="post" id="form_agents" action="index.php?sec=gmassive&sec2=godmode/massive/massive_operations&option=delete_agents">';
132html_print_table ($table);
133
134echo '<div class="action-buttons" style="width: '.$table->width.'" onsubmit="if (!confirm(\' '.__('Are you sure?').'\')) return false;">';
135html_print_input_hidden ('delete', 1);
136html_print_submit_button (__('Delete'), 'go', false, 'class="sub delete"');
137echo '</div>';
138echo '</form>';
139
140echo '<h3 class="error invisible" id="message"> </h3>';
141
142ui_require_jquery_file ('form');
143ui_require_jquery_file ('pandora.controls');
144?>
145
146<script type="text/javascript">
147	var limit_parameters_massive = <?php echo $config['limit_parameters_massive']; ?>;
148
149	$(document).ready (function () {
150		$("#form_agents").submit(function() {
151			var get_parameters_count = window.location.href.slice(
152				window.location.href.indexOf('?') + 1).split('&').length;
153			var post_parameters_count = $("#form_agents").serializeArray().length;
154
155			var count_parameters =
156				get_parameters_count + post_parameters_count;
157
158			if (count_parameters > limit_parameters_massive) {
159				alert("<?php echo __('Unsucessful sending the data, please contact with your administrator or make with less elements.'); ?>");
160				return false;
161			}
162		});
163
164
165		var recursion;
166
167		$("#checkbox-recursion").click(function () {
168			recursion = this.checked ? 1 : 0;
169
170			$("#id_group").trigger("change");
171		});
172
173		$("#id_group").pandoraSelectGroupAgent ({
174			status_agents: function () {
175				return $("#status_agents").val();
176			},
177			agentSelect: "select#id_agents",
178			privilege: "AW",
179			recursion: function() {
180				return recursion;
181			}
182		});
183
184		$("#status_agents").change(function() {
185			$("#id_group").trigger("change");
186		});
187	});
188</script>
189