#10014 New filters log viewer

This commit is contained in:
Jonathan 2023-01-16 11:34:28 +01:00
parent 0722503253
commit f3cd19c315
3 changed files with 636 additions and 30 deletions

View File

@ -0,0 +1,14 @@
START TRANSACTION;
CREATE TABLE IF NOT EXISTS `tsesion_filter` (
`id_filter` INT NOT NULL AUTO_INCREMENT,
`id_name` TEXT NULL,
`text` TEXT NULL,
`period` TEXT NULL,
`ip` TEXT NULL,
`type` TEXT NULL,
`user` TEXT NULL,
PRIMARY KEY (`id_filter`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
COMMIT;

View File

@ -0,0 +1,457 @@
<?php
/**
* Manage AJAX response for event pages.
*
* @category Ajax
* @package Pandora FMS
* @subpackage Events
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
* Copyright (c) 2005-2023 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.
* ============================================================================
*/
// Begin.
global $config;
enterprise_include_once('include/functions_audit.php');
// Check access.
check_login();
if (! check_acl($config['id_user'], 0, 'ER')
&& ! check_acl($config['id_user'], 0, 'EW')
&& ! check_acl($config['id_user'], 0, 'EM')
) {
db_pandora_audit(
AUDIT_LOG_ACL_VIOLATION,
'Trying to access event viewer'
);
include 'general/noaccess.php';
return;
}
$save_filter_modal = get_parameter('save_filter_modal', 0);
$load_filter_modal = get_parameter('load_filter_modal', 0);
$get_filter_values = get_parameter('get_filter_values', 0);
$update_log_filter = get_parameter('update_log_filter', 0);
$save_log_filter = get_parameter('save_log_filter', 0);
$recover_aduit_log_select = get_parameter('recover_aduit_log_select', 0);
// Saves an event filter.
if ($save_log_filter) {
$values = [];
$values['id_name'] = get_parameter('id_name');
$values['text'] = get_parameter('text', '');
$values['period'] = get_parameter('period', '');
$values['ip'] = get_parameter('ip', '');
$values['type'] = get_parameter('type', -1);
$values['user'] = get_parameter('user', -1);
$exists = (bool) db_get_value_filter(
'id_filter',
'tsesion_filter',
['id_name' => $values['id_name']]
);
if ($exists) {
echo 'duplicate';
} else {
$result = db_process_sql_insert('tsesion_filter', $values);
if ($result === false) {
echo 'error';
} else {
echo $result;
}
}
}
if ($recover_aduit_log_select) {
echo json_encode(audit_get_audit_filter_select());
}
if ($update_log_filter) {
$values = [];
$id = get_parameter('id');
$values['text'] = get_parameter('text', '');
$values['period'] = get_parameter('period', '');
$values['ip'] = get_parameter('ip', '');
$values['type'] = get_parameter('type', -1);
$values['user'] = get_parameter('user', -1);
$result = db_process_sql_update(
'tsesion_filter',
$values,
['id_filter' => $id]
);
if ($result === false) {
echo 'error';
} else {
echo 'ok';
}
}
if ($get_filter_values) {
$id_filter = get_parameter('id');
$event_filter = audit_get_audit_log_filter($id_filter);
echo json_encode($event_filter);
}
if ($load_filter_modal) {
$filters = audit_get_audit_filter_select();
$user_groups_array = users_get_groups_for_select(
$config['id_user'],
$access,
true,
true,
false
);
echo '<div id="load-filter-select" class="load-filter-modal">';
$table = new StdClass;
$table->id = 'load_filter_form';
$table->width = '100%';
$table->cellspacing = 4;
$table->cellpadding = 4;
$table->class = 'databox';
if (is_metaconsole()) {
$table->cellspacing = 0;
$table->cellpadding = 0;
$table->class = 'databox filters';
}
$table->styleTable = 'font-weight: bold; color: #555; text-align:left;';
$filter_id_width = '200px';
if (is_metaconsole()) {
$filter_id_width = '150px';
}
$data = [];
$table->rowid[3] = 'update_filter_row1';
$data[0] = __('Load filter').$jump;
$data[0] .= html_print_select(
$filters,
'filter_id',
'',
'',
__('None'),
0,
true,
false,
true,
'',
false,
'margin-left:5px; width:'.$filter_id_width.';'
);
$data[1] = html_print_submit_button(
__('Load filter'),
'load_filter',
false,
'class="sub upd" onclick="load_filter_values()"',
true
);
$data[1] .= html_print_input_hidden('load_filter', 1, true);
$table->data[] = $data;
$table->rowclass[] = '';
html_print_table($table);
echo '</div>';
?>
<script type="text/javascript">
function show_filter() {
$("#load-filter-select").dialog({
resizable: true,
draggable: true,
modal: false,
closeOnEscape: true,
width: 450
});
}
function load_filter_values() {
$.ajax({
method: 'POST',
url: '<?php echo ui_get_full_url('ajax.php'); ?>',
dataType: 'json',
data: {
page: 'include/ajax/audit_log',
get_filter_values: 1,
"id" : $('#filter_id :selected').val()
},
success: function(data) {
var options = "";
$.each(data,function(i,value){
if (i == 'text'){
$("#text-filter_text").val(value);
}
if (i == 'period'){
$("#text-filter_period").val(value);
}
if (i == 'ip'){
$("#text-filter_ip").val(value);
}
if (i == 'type'){
$("#filter_type").val(value).change();
}
if (i == 'user'){
$("#filter_user").val(value).change();
}
});
}
});
// Close dialog.
$("#load-filter-select").dialog('close');
}
$(document).ready (function() {
show_filter();
})
</script>
<?php
return;
}
if ($save_filter_modal) {
echo '<div id="save-filter-select" style="width:350px;">';
if (check_acl($config['id_user'], 0, 'EW') === 1 || check_acl($config['id_user'], 0, 'EM') === 1) {
echo '<div id="info_box"></div>';
$table = new StdClass;
$table->id = 'save_filter_form';
$table->width = '100%';
$table->cellspacing = 4;
$table->cellpadding = 4;
$table->class = 'databox';
if (is_metaconsole()) {
$table->class = 'databox filters';
$table->cellspacing = 0;
$table->cellpadding = 0;
}
$table->styleTable = 'font-weight: bold; text-align:left;';
if (!is_metaconsole()) {
$table->style[0] = 'width: 50%; width:50%;';
}
$data = [];
$table->rowid[0] = 'update_save_selector';
$data[0] = html_print_radio_button(
'filter_mode',
'new',
__('New filter'),
true,
true
);
$data[1] = html_print_radio_button(
'filter_mode',
'update',
__('Update filter'),
false,
true
);
$table->data[] = $data;
$table->rowclass[] = '';
$data = [];
$table->rowid[1] = 'save_filter_row1';
$data[0] = __('Filter name').$jump;
$data[0] .= html_print_input_text('id_name', '', '', 15, 255, true);
$data[1] = html_print_submit_button(
__('Save filter'),
'save_filter',
false,
'class="sub wand" onclick="save_new_filter();"',
true
);
$table->data[] = $data;
$table->rowclass[] = '';
$data = [];
$table->rowid[2] = 'save_filter_row2';
$table->data[] = $data;
$table->rowclass[] = '';
$data = [];
$table->rowid[3] = 'update_filter_row1';
$data[0] = __('Overwrite filter').$jump;
$_filters_update = audit_get_audit_filter_select();
$data[0] .= html_print_select(
$_filters_update,
'overwrite_filter',
'',
'',
'',
0,
true
);
$data[1] = html_print_submit_button(
__('Update filter'),
'update_filter',
false,
'class="sub upd" onclick="save_update_filter();"',
true
);
$table->data[] = $data;
$table->rowclass[] = '';
html_print_table($table);
} else {
include 'general/noaccess.php';
}
echo '</div>';
?>
<script type="text/javascript">
function show_save_filter() {
$('#save_filter_row1').show();
$('#save_filter_row2').show();
$('#update_filter_row1').hide();
// Filter save mode selector
$("[name='filter_mode']").click(function() {
if ($(this).val() == 'new') {
$('#save_filter_row1').show();
$('#save_filter_row2').show();
$('#submit-save_filter').show();
$('#update_filter_row1').hide();
}
else {
$('#save_filter_row1').hide();
$('#save_filter_row2').hide();
$('#update_filter_row1').show();
$('#submit-save_filter').hide();
}
});
$("#save-filter-select").dialog({
resizable: true,
draggable: true,
modal: false,
closeOnEscape: true
});
}
function save_new_filter() {
// If the filter name is blank show error
if ($('#text-id_name').val() == '') {
$('#info_box').html("<h3 class='error'><?php echo __('Filter name cannot be left blank'); ?></h3>");
return false;
}
var id_filter_save;
jQuery.post ("<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
{
"page" : "include/ajax/audit_log",
"save_log_filter" : 1,
"id_name" : $("#text-id_name").val(),
"text" : $("#text-filter_text").val(),
"period" : $("#text-filter_period").val(),
"ip" : $('#text-filter_ip').val(),
"type" : $('#filter_type :selected').val(),
"user" : $('#filter_user :selected').val(),
},
function (data) {
$("#info_box").hide();
if (data == 'error') {
$("#info_box").filter(function(i, item) {
if ($(item).data('type_info_box') == "error_create_filter") {
return true;
}
else
return false;
}).show();
} else if (data == 'duplicate') {
$('#info_box').html("<h3 class='error'><?php echo __('Filter name already on use'); ?></h3>");
$('#info_box').show();
} else {
// Close dialog.
$("#save-filter-select").dialog('close');
}
}
);
}
// This updates an event filter
function save_update_filter() {
var id_filter_update = $("#overwrite_filter").val();
var name_filter_update = $("#overwrite_filter option[value='"+id_filter_update+"']").text();
jQuery.post ("<?php echo ui_get_full_url('ajax.php', false, false, false); ?>",
{"page" : "include/ajax/audit_log",
"update_log_filter" : 1,
"id" : $("#overwrite_filter :selected").val(),
"text" : $("#text-filter_text").val(),
"period" : $("#text-filter_period").val(),
"ip" : $('#text-filter_ip').val(),
"type" : $('#filter_type :selected').val(),
"user" : $('#filter_user :selected').val(),
},
function (data) {
$(".info_box").hide();
if (data == 'ok') {
$(".info_box").filter(function(i, item) {
if ($(item).data('type_info_box') == "success_update_filter") {
return true;
}
else
return false;
}).show();
}
else {
$(".info_box").filter(function(i, item) {
if ($(item).data('type_info_box') == "error_create_filter") {
return true;
}
else
return false;
}).show();
}
});
// Close dialog
$('.ui-dialog-titlebar-close').trigger('click');
return false;
}
$(document).ready(function (){
show_save_filter();
});
</script>
<?php
return;
}

View File

@ -156,6 +156,26 @@ class AuditLog extends HTML
open_meta_frame();
}
$buttons = [];
$buttons[] = [
'id' => 'load-filter',
'class' => 'float-left margin-right-2 margin-left-2 sub config',
'text' => __('Load filter'),
'onclick' => '',
];
$buttons[] = [
'id' => 'save-filter',
'class' => 'float-left margin-right-2 sub wand',
'text' => __('Save filter'),
'onclick' => '',
];
// Modal for save/load filters.
echo '<div id="save-modal-filter" style="display:none"></div>';
echo '<div id="load-modal-filter" style="display:none"></div>';
// Load datatables user interface.
ui_print_datatable(
[
@ -174,9 +194,10 @@ class AuditLog extends HTML
],
'search_button_class' => 'sub filter float-right',
'form' => [
'inputs' => [
'extra_buttons' => $buttons,
'inputs' => [
[
'label' => __('Search'),
'label' => __('Free search').ui_print_help_tip(__('Search filter by User, Action, Date, Source IP or Comments fields content'), true),
'type' => 'text',
'class' => 'w200px',
'id' => 'filter_text',
@ -211,7 +232,9 @@ class AuditLog extends HTML
'type' => 'select_from_sql',
'nothing' => __('All'),
'nothing_value' => '-1',
'sql' => 'SELECT id_user, id_user AS text FROM tusuario',
'sql' => 'SELECT id_user, id_user AS text FROM tusuario UNION SELECT "SYSTEM"
AS id_user, "SYSTEM" AS text UNION SELECT "N/A"
AS id_user, "N/A" AS text',
'class' => 'mw250px',
'id' => 'filter_user',
'name' => 'filter_user',
@ -269,7 +292,10 @@ class AuditLog extends HTML
if (empty($this->filterText) === false) {
$filter .= sprintf(
" AND (accion LIKE '%%%s%%' OR descripcion LIKE '%%%s%%')",
" AND (accion LIKE '%%%s%%' OR descripcion LIKE '%%%s%%' OR id_usuario LIKE '%%%s%%' OR fecha LIKE '%%%s%%' OR ip_origen LIKE '%%%s%%')",
$this->filterText,
$this->filterText,
$this->filterText,
$this->filterText,
$this->filterText
);
@ -366,38 +392,147 @@ class AuditLog extends HTML
// Javascript content.
?>
<script type="text/javascript">
function format(d) {
var output = '';
<script type="text/javascript">
var loading = 0;
if (d.extendedInfo === '') {
output = "<?php echo __('There is no additional information to display'); ?>";
} else {
output = d.extendedInfo;
function format(d) {
var output = '';
if (d.extendedInfo === '') {
output = "<?php echo __('There is no additional information to display'); ?>";
} else {
output = d.extendedInfo;
}
return output;
}
return output;
}
$(document).ready(function() {
// Add event listener for opening and closing details
$('#audit_logs tbody').on('click', 'td.show_extended_info', function() {
var tr = $(this).closest('tr');
var table = $("#<?php echo $this->tableId; ?>").DataTable();
var row = table.row(tr);
$(document).ready(function() {
// Add event listener for opening and closing details
$('#audit_logs tbody').on('click', 'td.show_extended_info', function() {
var tr = $(this).closest('tr');
var table = $("#<?php echo $this->tableId; ?>").DataTable();
var row = table.row(tr);
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
});
if (row.child.isShown()) {
// This row is already open - close it
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
$('#save-filter').click(function() {
if ($('#save-filter-select').length) {
$('#save-filter-select').dialog({
width: "20%",
maxWidth: "25%",
title: "<?php echo __('Save filter'); ?>"
});
$('#info_box').html("");
$('#text-id_name').val("");
$.ajax({
method: 'POST',
url: '<?php echo ui_get_full_url('ajax.php'); ?>',
dataType: 'json',
data: {
page: 'include/ajax/audit_log',
recover_aduit_log_select: 1
},
success: function(data) {
var options = "";
$.each(data,function(key,value){
options += "<option value='"+key+"'>"+value+"</option>";
});
$('#overwrite_filter').html(options);
$('#overwrite_filter').select2();
}
});
} else {
if (loading == 0) {
loading = 1
$.ajax({
method: 'POST',
url: '<?php echo ui_get_full_url('ajax.php'); ?>',
data: {
page: 'include/ajax/audit_log',
save_filter_modal: 1,
current_filter: $('#latest_filter_id').val()
},
success: function(data) {
$('#save-modal-filter')
.empty()
.html(data);
loading = 0;
$('#save-filter-select').dialog({
width: "20%",
maxWidth: "25%",
title: "<?php echo __('Save filter'); ?>"
});
}
});
}
}
});
$('#save_filter_form-0-1, #radiobtn0002').click(function(){
$('#overwrite_filter').select2();
});
/* Filter management */
$('#load-filter').click(function (){
if($('#load-filter-select').length) {
$('#load-filter-select').dialog({width: "20%",
maxWidth: "25%",
title: "<?php echo __('Load filter'); ?>"
});
$.ajax({
method: 'POST',
url: '<?php echo ui_get_full_url('ajax.php'); ?>',
dataType: 'json',
data: {
page: 'include/ajax/audit_log',
recover_aduit_log_select: 1
},
success: function(data) {
var options = "";
$.each(data,function(key,value){
options += "<option value='"+key+"'>"+value+"</option>";
});
$('#filter_id').html(options);
$('#filter_id').select2();
}
});
} else {
if (loading == 0) {
loading = 1
$.ajax({
method: 'POST',
url: '<?php echo ui_get_full_url('ajax.php'); ?>',
data: {
page: 'include/ajax/audit_log',
load_filter_modal: 1
},
success: function (data){
$('#load-modal-filter')
.empty()
.html(data);
loading = 0;
$('#load-filter-select').dialog({
width: "20%",
maxWidth: "25%",
title: "<?php echo __('Load filter'); ?>"
});
}
});
}
}
});
});
});
</script>
</script>
<?php
// EOF Javascript content.
return ob_get_clean();