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
20check_login();
21
22enterprise_hook('open_meta_frame');
23
24require_once($config['homedir'] . "/include/functions_groups.php");
25require_once($config['homedir'] . "/include/functions_agents.php");
26require_once($config['homedir'] . '/include/functions_users.php');
27enterprise_include_once ('meta/include/functions_agents_meta.php');
28
29if (is_ajax ()) {
30	if (! check_acl($config['id_user'], 0, "AR")) {
31		db_pandora_audit("ACL Violation", "Trying to access Group Management");
32		require ("general/noaccess.php");
33		return;
34	}
35
36	$get_group_json = (bool) get_parameter ('get_group_json');
37	$get_group_agents = (bool) get_parameter ('get_group_agents');
38
39	if ($get_group_json) {
40		$id_group = (int) get_parameter ('id_group');
41
42		if ($id_group == 0) {
43			$group = array('id_grupo' => 0,
44				'nombre' => 'All',
45				'icon' => 'world',
46				'parent' => 0,
47				'disabled' => 0,
48				'custom_id' => null);
49
50			echo json_encode ($group);
51			return;
52		}
53
54		if (! check_acl ($config['id_user'], $id_group, "AR")) {
55			db_pandora_audit("ACL Violation",
56				"Trying to access Alert Management");
57			echo json_encode (false);
58			return;
59		}
60
61		$group = db_get_row ('tgrupo', 'id_grupo', $id_group);
62
63		echo json_encode ($group);
64		return;
65	}
66
67	if ($get_group_agents) {
68		$id_group = (int) get_parameter ('id_group');
69		$disabled = (int) get_parameter ('disabled', 0);
70		$search = (string) get_parameter ('search', '');
71		$recursion = (int) get_parameter ('recursion', 0);
72		$privilege = (string) get_parameter ('privilege', '');
73		// Is is possible add keys prefix to avoid auto sorting in js object conversion
74		$keys_prefix = (string) get_parameter ('keys_prefix', '');
75		// This attr is for the operation "bulk alert accions add", it controls the query that take the agents
76		// from db
77		$add_alert_bulk_op = get_parameter ('add_alert_bulk_op', false);
78		// Ids of agents to be include in the SQL clause as id_agent IN ()
79		$filter_agents_json = (string) get_parameter ('filter_agents_json', '');
80		$status_agents = (int)get_parameter('status_agents', AGENT_STATUS_ALL);
81		// Juanma (22/05/2014) Fix: If setted remove void agents from result (by default and for compatibility show void agents)
82		$show_void_agents = (int)get_parameter('show_void_agents', 1);
83
84		if (! check_acl ($config['id_user'], $id_group, "AR")) {
85			db_pandora_audit("ACL Violation",
86				"Trying to access Alert Management");
87			echo json_encode (false);
88			return;
89		}
90
91		if ( https_is_running() ) {
92			header('Content-type: application/json');
93		}
94
95		if ($filter_agents_json != '') {
96			$filter['id_agente'] = json_decode(io_safe_output($filter_agents_json), true);
97		}
98
99		$filter['disabled'] = $disabled;
100
101		if ($search != '') {
102			$filter['string'] = $search;
103		}
104
105		if ($status_agents != AGENT_STATUS_ALL) {
106			$filter['status'] = $status_agents;
107		}
108
109		# Juanma (22/05/2014) Fix: If remove void agents setted
110		$_sql_post = ' 1=1 ';
111		if ($show_void_agents == 0) {
112
113			$_sql_post .= ' AND id_agente IN (SELECT a.id_agente FROM tagente a, tagente_modulo b WHERE a.id_agente=b.id_agente AND b.delete_pending=0) AND \'1\'';
114			$filter[$_sql_post] = '1';
115
116		}
117
118		if ( $id_group == 0 && $privilege != '') {
119			$groups = users_get_groups ($config["id_user"], $privilege, false);
120			//  if group ID doesn't matter and $privilege is specified (like 'AW'),
121			//  retruns all agents that current user has $privilege privilege for.
122			$agents = agents_get_group_agents(array_keys($groups), $filter, "none", false, $recursion, false, '|', $add_alert_bulk_op);
123		}
124		else {
125			$agents = agents_get_group_agents($id_group, $filter, "none",
126				false, $recursion, false, '|', $add_alert_bulk_op);
127		}
128
129		// Add keys prefix
130		if ($keys_prefix !== "") {
131			foreach($agents as $k => $v) {
132				$agents[$keys_prefix . $k] = $v;
133				unset($agents[$k]);
134			}
135		}
136
137		echo json_encode ($agents);
138		return;
139	}
140
141	return;
142}
143
144if (! check_acl($config['id_user'], 0, "AW")) {
145	db_pandora_audit("ACL Violation",
146		"Trying to access Group Management");
147	require ("general/noaccess.php");
148	return;
149}
150
151// Header
152if (defined('METACONSOLE')) {
153
154	agents_meta_print_header();
155	$sec = 'advanced';
156
157	echo '<div class="notify">';
158	echo __("Edit or delete groups can cause problems with synchronization");
159	echo '</div>';
160
161}
162else {
163
164	ui_print_page_header (__("Groups defined in Pandora"),
165		"images/group.png", false, "", true, "");
166	$sec = 'gagente';
167
168}
169
170
171$create_group = (bool) get_parameter ('create_group');
172$update_group = (bool) get_parameter ('update_group');
173$delete_group = (bool) get_parameter ('delete_group');
174$pure = get_parameter('pure', 0);
175
176/* Create group */
177if (($create_group) && (check_acl($config['id_user'], 0, "PM"))) {
178	$name = (string) get_parameter ('name');
179	$icon = (string) get_parameter ('icon');
180	$id_parent = (int) get_parameter ('id_parent');
181	$alerts_disabled = (bool) get_parameter ('alerts_disabled');
182	$custom_id = (string) get_parameter ('custom_id');
183	$skin = (string) get_parameter ('skin');
184	$description = (string) get_parameter ('description');
185	$contact = (string) get_parameter ('contact');
186	$other = (string) get_parameter ('other');
187	$check = db_get_value('nombre', 'tgrupo', 'nombre', $name);
188	$propagate = (bool) get_parameter('propagate');
189
190	/*Check if name field is empty*/
191	if ($name != "") {
192		if (!$check) {
193			$values = array(
194				'nombre' => $name,
195				'icon' => empty($icon) ? '' : substr ($icon, 0, -4),
196				'parent' => $id_parent,
197				'disabled' => $alerts_disabled,
198				'custom_id' => $custom_id,
199				'id_skin' => $skin,
200				'description' => $description,
201				'contact' => $contact,
202				'propagate' => $propagate,
203				'other' => $other
204			);
205
206			$result = db_process_sql_insert('tgrupo', $values);
207			if ($result) {
208				ui_print_success_message(__('Group successfully created'));
209			}
210			else {
211				ui_print_error_message(__('There was a problem creating group'));
212			}
213		}
214		else {
215			ui_print_error_message(__('Each group must have a different name'));
216		}
217	}
218	else {
219		//$result = false;
220		ui_print_error_message(__('Group must have a name'));
221	}
222}
223
224/* Update group */
225if ($update_group) {
226	$id_group = (int) get_parameter ('id_group');
227	$name = (string) get_parameter ('name');
228	$icon = (string) get_parameter ('icon');
229	$id_parent = (int) get_parameter ('id_parent');
230	$description = (string) get_parameter ('description');
231	$alerts_enabled = (bool) get_parameter ('alerts_enabled');
232	$custom_id = (string) get_parameter ('custom_id');
233	$propagate = (bool) get_parameter('propagate');
234	$skin = (string) get_parameter ('skin');
235	$description = (string) get_parameter ('description');
236	$contact = (string) get_parameter ('contact');
237	$other = (string) get_parameter ('other');
238
239	/*Check if name field is empty*/
240	if ( $name != "") {
241		switch ($config["dbtype"]) {
242			case "mysql":
243				$sql = sprintf ('UPDATE tgrupo  SET nombre = "%s",
244					icon = "%s", disabled = %d, parent = %d, custom_id = "%s", propagate = %d, id_skin = %d, description = "%s", contact = "%s", other = "%s"
245					WHERE id_grupo = %d',
246					$name, empty($icon) ? '' : substr ($icon, 0, -4), !$alerts_enabled, $id_parent, $custom_id, $propagate, $skin, $description, $contact, $other, $id_group);
247				break;
248			case "postgresql":
249			case "oracle":
250				$sql = sprintf ('UPDATE tgrupo  SET nombre = \'%s\',
251					icon = \'%s\', disabled = %d, parent = %d, custom_id = \'%s\', propagate = %d, id_skin = %d, description = \'%s\', contact = \'%s\', other = \'%s\'
252					WHERE id_grupo = %d',
253					$name, substr ($icon, 0, -4), !$alerts_enabled, $id_parent, $custom_id, $propagate, $skin, $description, $contact, $other, $id_group);
254				break;
255		}
256		$result = db_process_sql ($sql);
257	}
258	else {
259		$result = false;
260	}
261
262	if ($result !== false) {
263		ui_print_success_message(__('Group successfully updated'));
264	}
265	else {
266		ui_print_error_message(__('There was a problem modifying group'));
267	}
268}
269
270/* Delete group */
271if (($delete_group) && (check_acl($config['id_user'], 0, "PM"))) {
272	$id_group = (int) get_parameter ('id_group');
273
274	$usedGroup = groups_check_used($id_group);
275
276	if (!$usedGroup['return']) {
277		$group = db_get_row_filter('tgrupo',
278			array('id_grupo' => $id_group));
279
280		db_process_sql_update('tgrupo',
281			array('parent' => $group['parent']), array('parent' => $id_group));
282
283		$result = db_process_sql_delete('tgroup_stat',
284			array('id_group' => $id_group));
285
286		$result = db_process_sql_delete('tgrupo',
287			array('id_grupo' => $id_group));
288	}
289	else {
290		ui_print_error_message(
291			sprintf(__('The group is not empty. It is use in %s.'), implode(', ', $usedGroup['tables'])));
292	}
293
294	if ($result && (!$usedGroup['return'])) {
295		ui_print_success_message(__('Group successfully deleted'));
296	}
297	else {
298		ui_print_error_message(__('There was a problem deleting group'));
299	}
300}
301db_clean_cache();
302$groups = users_get_groups_tree ($config['id_user'], "AR", true);
303
304$table->width = '100%';
305
306$all_parents = array();
307$groups_count = 0;
308$sons = array();
309
310
311
312foreach ($groups as $k => $g) {
313	if ($g['parent'] != 0) {
314		$all_parents[$g['parent']] = $g['parent'];
315	}
316}
317krsort($all_parents);
318foreach ($all_parents as $parent) {
319	foreach ($groups as $k => $g) {
320		if ($g['parent'] == $parent) {
321			$sons[$g['parent']][] = $g;
322			unset($groups[$k]);
323		}
324	}
325}
326
327
328$groups_count = count($groups);
329
330if (!empty($groups)) {
331	$table->class = "databox data";
332	$table->head = array ();
333	$table->head[0] = __('Name');
334	$table->head[1] = __('ID');
335	$table->head[2] = __('Icon');
336	$table->head[3] = __('Alerts');
337	$table->head[4] = __('Description');
338	$table->head[5] = __('Actions');
339	$table->align = array ();
340	$table->align[2] = 'left';
341	$table->align[5] = 'left';
342	$table->size[4] = '30%';
343	$table->size[5] = '10%';
344	$table->data = array ();
345
346	$offset = (int)get_parameter('offset', 0);
347	$limit = $offset + $config['block_size'];
348
349
350
351	$pagination = ui_pagination($groups_count,
352		false, 0, $config['block_size'], true, 'offset', false);
353
354	$n = -1;
355	$iterator = 0;
356	$branch_classes = array();
357	foreach ($groups as $group) {
358		$n++;
359
360		// Only print the page range
361		if ($n < $offset || $n >= $limit) {
362			continue;
363		}
364
365		$symbolBranchs = ' symbol_branch_' . $group['parent'];
366
367		$data = groups_get_group_to_list($group, $groups_count, $symbolBranchs);
368		array_push ($table->data, $data);
369		$table->rowstyle[$iterator] = '';
370		if ($group['id_grupo'] != 0) {
371			$branch_classes[$group['id_grupo']] = ' branch_0';
372			$table->rowclass[$iterator] = 'parent_' . $group['parent'] . ' branch_0';
373		}
374		$iterator++;
375
376		groups_print_group_sons($group, $sons, $branch_classes,
377			$groups_count, $table, $iterator, $symbolBranchs);
378	}
379
380	echo $pagination;
381
382	html_print_table ($table);
383
384	echo $pagination;
385}
386else {
387	ui_print_info_message ( array('no_close'=>true, 'message'=>  __('There are no defined groups') ) );
388}
389
390if (check_acl($config['id_user'], 0, "PM")) {
391	echo '<form method="post" action="index.php?sec='.$sec.'&sec2=godmode/groups/configure_group&pure='.$pure.'">';
392	echo '<div class="action-buttons" style="width: '.$table->width.'">';
393	html_print_submit_button (__('Create group'), 'crt', false, 'class="sub next"');
394	echo '</div>';
395	echo '</form>';
396}
397
398enterprise_hook('close_meta_frame');
399
400?>
401
402<script type="text/javascript">
403function showBranch(parent) {
404	display = $('.parent_' + parent).css('display');
405
406	if (display != 'none') {
407		$('.symbol_' + parent).html('+');
408		$('.parent_' + parent).css('display', 'none');
409
410		//Close the child branch too
411		$('.branch_' + parent).css('display', 'none');
412		$('.symbol_branch_' + parent).html('+');
413	}
414	else {
415		$('.symbol_' + parent).html('-');
416		$('.parent_' + parent).css('display', '');
417	}
418}
419</script>
420