2019-02-06 12:51:02 +01:00
|
|
|
<?php
|
2019-01-31 19:13:51 +01:00
|
|
|
|
|
|
|
// Pandora FMS - http://pandorafms.com
|
|
|
|
// ==================================================
|
|
|
|
// Copyright (c) 2005-2019 Artica Soluciones Tecnologicas
|
|
|
|
// Please see http://pandorafms.org for full contribution list
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation for version 2.
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
// Warning: This file may be required into the metaconsole's setup
|
|
|
|
// Load global vars
|
|
|
|
global $config;
|
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
require_once $config['homedir'].'/include/functions_notifications.php';
|
2019-02-05 13:36:16 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
check_login();
|
2019-01-31 19:13:51 +01:00
|
|
|
|
|
|
|
if (! check_acl($config['id_user'], 0, 'PM') && ! is_user_admin($config['id_user'])) {
|
|
|
|
db_pandora_audit('ACL Violation', 'Trying to access Setup Management');
|
2019-02-06 12:51:02 +01:00
|
|
|
include 'general/noaccess.php';
|
2019-01-31 19:13:51 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
// AJAX actions.
|
2019-02-05 18:14:10 +01:00
|
|
|
$source_id = get_parameter('source_id', '');
|
|
|
|
$users = get_parameter('users', '');
|
2019-02-06 12:51:02 +01:00
|
|
|
$elements = get_parameter('elements', []);
|
2019-02-05 18:14:10 +01:00
|
|
|
$id = empty($source_id) ? 0 : get_notification_source_id($source_id);
|
2019-02-06 12:51:02 +01:00
|
|
|
$is_users = $users === 'users';
|
2019-02-05 13:36:16 +01:00
|
|
|
if (get_parameter('get_selection_two_ways_form', 0)) {
|
2019-02-06 12:51:02 +01:00
|
|
|
$info_selec = $is_users ? notifications_get_user_source_not_configured($id) : notifications_get_group_source_not_configured($id);
|
2019-02-05 13:36:16 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
echo notifications_print_two_ways_select(
|
|
|
|
$info_selec,
|
|
|
|
$users,
|
|
|
|
$source_id
|
|
|
|
);
|
|
|
|
return;
|
2019-02-05 13:36:16 +01:00
|
|
|
}
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
if (get_parameter('add_source_to_database', 0)) {
|
2019-02-06 12:51:02 +01:00
|
|
|
$res = $is_users ? notifications_add_users_to_source($id, $elements) : notifications_add_group_to_source($id, $elements);
|
|
|
|
$result = ['result' => $res];
|
|
|
|
echo json_encode($result);
|
|
|
|
return;
|
2019-02-05 17:40:44 +01:00
|
|
|
}
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-05 18:14:10 +01:00
|
|
|
if (get_parameter('remove_source_on_database', 0)) {
|
2019-02-06 12:51:02 +01:00
|
|
|
$res = $is_users ? notifications_remove_users_from_source($id, $elements) : notifications_remove_group_from_source($id, $elements);
|
|
|
|
$result = ['result' => $res];
|
|
|
|
echo json_encode($result);
|
|
|
|
return;
|
2019-02-05 18:14:10 +01:00
|
|
|
}
|
2019-02-05 17:40:44 +01:00
|
|
|
|
2019-01-31 19:13:51 +01:00
|
|
|
if (get_parameter('update_config', 0)) {
|
2019-02-07 17:53:09 +01:00
|
|
|
$source = (int) get_parameter('source', 0);
|
|
|
|
$element = (string) get_parameter('element', '');
|
|
|
|
$value = (int) get_parameter('value', 0);
|
|
|
|
|
|
|
|
// Update the label value.
|
|
|
|
ob_clean();
|
|
|
|
$res = false;
|
|
|
|
switch ($element) {
|
|
|
|
// All users has other action.
|
|
|
|
case 'all_users':
|
|
|
|
$res = $value ? notifications_add_group_to_source($source, [0]) : notifications_remove_group_from_source($source, [0]);
|
|
|
|
break;
|
2019-02-06 12:51:02 +01:00
|
|
|
|
2019-02-07 17:53:09 +01:00
|
|
|
default:
|
|
|
|
$res = (bool) db_process_sql_update(
|
2019-02-06 12:51:02 +01:00
|
|
|
'tnotification_source',
|
2019-02-07 17:53:09 +01:00
|
|
|
[$element => $value],
|
|
|
|
['id' => $source]
|
2019-02-06 12:51:02 +01:00
|
|
|
);
|
2019-02-07 17:53:09 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
echo json_encode(['result' => $res]);
|
|
|
|
return;
|
2019-01-31 19:13:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Notification table. It is just a wrapper.
|
|
|
|
$table_content = new StdClass();
|
2019-02-06 12:51:02 +01:00
|
|
|
$table_content->data = [];
|
2019-01-31 19:13:51 +01:00
|
|
|
$table_content->width = '100%';
|
|
|
|
$table_content->id = 'notifications-wrapper';
|
|
|
|
$table_content->class = 'databox filters';
|
|
|
|
$table_content->size['name'] = '30%';
|
|
|
|
|
|
|
|
// Print each source configuration
|
2019-02-06 12:51:02 +01:00
|
|
|
$table_content->data = array_map(
|
|
|
|
function ($source) {
|
|
|
|
return notifications_print_global_source_configuration($source);
|
|
|
|
},
|
|
|
|
notifications_get_all_sources()
|
|
|
|
);
|
2019-01-31 19:13:51 +01:00
|
|
|
|
|
|
|
html_print_table($table_content);
|
|
|
|
|
2019-02-04 11:02:52 +01:00
|
|
|
?>
|
|
|
|
<script>
|
|
|
|
// Get the source id
|
|
|
|
function notifications_get_source_id(id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
var matched = id.match(/.*-(.*)/);
|
|
|
|
if (matched == null) return '';
|
|
|
|
return matched[1];
|
2019-02-04 11:02:52 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
// Get index of two ways element dialog.
|
|
|
|
function notifications_two_ways_element_get_dialog (id, source_id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
return 'global_config_notifications_dialog_add-' + id + '-' + source_id;
|
2019-02-05 17:40:44 +01:00
|
|
|
}
|
2019-02-05 18:14:10 +01:00
|
|
|
|
2019-02-05 17:40:44 +01:00
|
|
|
// Get index of two ways element form.
|
|
|
|
function notifications_two_ways_element_get_sufix (id, source_id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
return 'multi-' + id + '-' + source_id;
|
2019-02-05 17:40:44 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 18:14:10 +01:00
|
|
|
// Disable or enable the select seeing the checked value of notify all users
|
|
|
|
function notifications_disable_source(event) {
|
2019-02-06 12:51:02 +01:00
|
|
|
var id = notifications_get_source_id(event.target.id);
|
|
|
|
var is_checked = document.getElementById(event.target.id).checked;
|
|
|
|
var selectors = ['groups', 'users'];
|
|
|
|
selectors.map(function (select) {
|
|
|
|
document.getElementById(notifications_two_ways_element_get_sufix(select, id)).disabled = is_checked;
|
|
|
|
});
|
2019-02-05 18:14:10 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 13:36:16 +01:00
|
|
|
// Open a dialog with selector of source elements.
|
|
|
|
function add_source_dialog(users, source_id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
// Display the dialog
|
|
|
|
var dialog_id = notifications_two_ways_element_get_dialog(users, source_id);
|
|
|
|
// Clean id element.
|
|
|
|
var previous_dialog = document.getElementById(dialog_id);
|
|
|
|
if (previous_dialog !== null) previous_dialog.remove();
|
|
|
|
// Create or recreate the content.
|
|
|
|
var not_dialog = document.createElement('div');
|
|
|
|
not_dialog.setAttribute('class', 'global_config_notifications_dialog_add_wrapper');
|
|
|
|
not_dialog.setAttribute('id', dialog_id);
|
|
|
|
document.body.appendChild(not_dialog);
|
|
|
|
$("#" + dialog_id).dialog({
|
|
|
|
resizable: false,
|
|
|
|
draggable: true,
|
|
|
|
modal: true,
|
|
|
|
dialogClass: "global_config_notifications_dialog_add_full",
|
|
|
|
overlay: {
|
|
|
|
opacity: 0.5,
|
|
|
|
background: "black"
|
|
|
|
},
|
|
|
|
closeOnEscape: true,
|
|
|
|
modal: true
|
|
|
|
});
|
2019-02-05 13:36:16 +01:00
|
|
|
|
2019-02-06 12:51:02 +01:00
|
|
|
jQuery.post ("ajax.php",
|
|
|
|
{"page" : "godmode/setup/setup_notifications",
|
|
|
|
"get_selection_two_ways_form" : 1,
|
|
|
|
"users" : users,
|
|
|
|
"source_id" : source_id
|
|
|
|
},
|
|
|
|
function (data, status) {
|
|
|
|
not_dialog.innerHTML = data
|
|
|
|
},
|
|
|
|
"html"
|
|
|
|
);
|
2019-02-05 13:36:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move from selected and not selected source elements.
|
|
|
|
function notifications_modify_two_ways_element (id, source_id, operation) {
|
2019-02-06 12:51:02 +01:00
|
|
|
var index_sufix = notifications_two_ways_element_get_sufix (id, source_id);
|
|
|
|
var start_id = operation === 'add' ? 'all-' : 'selected-';
|
|
|
|
var end_id = operation !== 'add' ? 'all-' : 'selected-';
|
|
|
|
var select = document.getElementById(
|
|
|
|
start_id + index_sufix
|
|
|
|
);
|
|
|
|
var select_end = document.getElementById(
|
|
|
|
end_id + index_sufix
|
|
|
|
);
|
|
|
|
for (var i = select.options.length - 1; i >= 0; i--) {
|
|
|
|
if(select.options[i].selected){
|
|
|
|
select_end.appendChild(select.options[i]);
|
|
|
|
}
|
|
|
|
}
|
2019-02-05 13:36:16 +01:00
|
|
|
}
|
2019-02-05 17:40:44 +01:00
|
|
|
|
|
|
|
// Add elements to database and close dialog
|
|
|
|
function notifications_add_source_element_to_database(id, source_id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
var index = 'selected-' + notifications_two_ways_element_get_sufix (id, source_id);
|
|
|
|
var select = document.getElementById(index);
|
|
|
|
var selected = [];
|
|
|
|
for (var i = select.options.length - 1; i >= 0; i--) {
|
|
|
|
selected.push(select.options[i].value);
|
|
|
|
}
|
|
|
|
jQuery.post ("ajax.php",
|
|
|
|
{"page" : "godmode/setup/setup_notifications",
|
|
|
|
"add_source_to_database" : 1,
|
|
|
|
"users" : id,
|
|
|
|
"source_id" : source_id,
|
|
|
|
"elements": selected
|
|
|
|
},
|
|
|
|
function (data, status) {
|
|
|
|
if (data.result) {
|
|
|
|
// Append to other element
|
|
|
|
var out_select = document.getElementById(
|
|
|
|
notifications_two_ways_element_get_sufix(id, source_id)
|
|
|
|
);
|
|
|
|
for (var i = select.options.length - 1; i >= 0; i--) {
|
|
|
|
out_select.appendChild(select.options[i]);
|
|
|
|
}
|
|
|
|
// Close the dialog
|
|
|
|
$("#" + notifications_two_ways_element_get_dialog(id, source_id)).dialog("close");
|
|
|
|
} else {
|
|
|
|
console.log("Cannot update element.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
2019-02-05 17:40:44 +01:00
|
|
|
}
|
2019-02-05 18:14:10 +01:00
|
|
|
|
|
|
|
// Add elements to database and remove it form main select
|
|
|
|
function remove_source_elements(id, source_id) {
|
2019-02-06 12:51:02 +01:00
|
|
|
var index = notifications_two_ways_element_get_sufix(id, source_id);
|
|
|
|
var select = document.getElementById(index);
|
|
|
|
var selected = [];
|
|
|
|
var selected_index = [];
|
|
|
|
for (var i = select.options.length - 1; i >= 0; i--) {
|
|
|
|
if(select.options[i].selected){
|
|
|
|
selected.push(select.options[i].value);
|
|
|
|
selected_index.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
jQuery.post ("ajax.php",
|
|
|
|
{"page" : "godmode/setup/setup_notifications",
|
|
|
|
"remove_source_on_database" : 1,
|
|
|
|
"users" : id,
|
|
|
|
"source_id" : source_id,
|
|
|
|
"elements": selected
|
|
|
|
},
|
|
|
|
function (data, status) {
|
|
|
|
if (data.result) {
|
|
|
|
// Append to other element
|
|
|
|
for (var i = selected_index.length - 1; i >= 0; i--) {
|
|
|
|
select.remove(selected_index[i]);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
console.log("Cannot delete elements.");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
);
|
2019-02-05 18:14:10 +01:00
|
|
|
}
|
2019-02-07 17:53:09 +01:00
|
|
|
|
|
|
|
function notifications_handle_change_element(event) {
|
|
|
|
event.preventDefault();
|
|
|
|
var match = /nt-([0-9]+)-(.*)/.exec(event.target.id);
|
|
|
|
if (!match) {
|
|
|
|
console.error(
|
|
|
|
"Cannot handle change element. Id not valid: ", event.target.id
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var action = {source: match[1], bit: match[2]};
|
|
|
|
var element = document.getElementById(event.target.id);
|
|
|
|
if (element === null) {
|
|
|
|
console.error(
|
|
|
|
"Cannot get element. Id: ", event.target.id
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var value;
|
|
|
|
switch (action.bit) {
|
|
|
|
case 'enabled':
|
|
|
|
case 'also_mail':
|
|
|
|
case 'user_editable':
|
|
|
|
case 'all_users':
|
|
|
|
value = element.checked ? 1 : 0;
|
|
|
|
break;
|
|
|
|
case 'max_postpone_time':
|
|
|
|
value = element.value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("Unregonized action", action.bit, '.');
|
|
|
|
return;
|
|
|
|
|
|
|
|
}
|
|
|
|
jQuery.post ("ajax.php",
|
|
|
|
{
|
|
|
|
"page" : "godmode/setup/setup_notifications",
|
|
|
|
"update_config" : 1,
|
|
|
|
"source" : match[1],
|
|
|
|
"element" : match[2],
|
|
|
|
"value": value
|
|
|
|
},
|
|
|
|
function (data, status) {
|
|
|
|
if (!data.result) {
|
|
|
|
console.error("Error changing configuration in database.");
|
|
|
|
} else {
|
|
|
|
switch (action.bit) {
|
|
|
|
case 'enabled':
|
|
|
|
case 'also_mail':
|
|
|
|
case 'user_editable':
|
|
|
|
case 'all_users':
|
|
|
|
element.checked = !element.checked;
|
|
|
|
break;
|
|
|
|
case 'max_postpone_time':
|
|
|
|
value = element.value;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
console.error("Unregonized action (insert on db)", action.bit, '.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
"json"
|
|
|
|
)
|
|
|
|
.done(function(m){})
|
|
|
|
.fail(function(xhr, textStatus, errorThrown){
|
|
|
|
console.error(
|
|
|
|
"Cannot change configuration in database. Server error.",
|
|
|
|
xhr.responseText
|
|
|
|
);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
(function(){
|
|
|
|
// Add listener to all componentes marked
|
|
|
|
var all_clickables = document.getElementsByClassName('elem-clickable');
|
|
|
|
for (var i = 0; i < all_clickables.length; i++) {
|
|
|
|
all_clickables[i].addEventListener(
|
|
|
|
'click', notifications_handle_change_element, false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
var all_changes = document.getElementsByClassName('elem-changeable');
|
|
|
|
for (var i = 0; i < all_changes.length; i++) {
|
|
|
|
all_changes[i].addEventListener(
|
|
|
|
'change', notifications_handle_change_element, false
|
|
|
|
);
|
|
|
|
}
|
|
|
|
})();
|
2019-02-04 11:02:52 +01:00
|
|
|
</script>
|