pandorafms/pandora_console/include/class/AuditLog.class.php

582 lines
22 KiB
PHP
Raw Normal View History

<?php
// phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
/**
* Controller for Audit Logs
*
* @category Controller
* @package Pandora FMS
* @subpackage Community
* @version 1.0.0
* @license See below
*
* ______ ___ _______ _______ ________
2023-06-08 12:42:10 +02:00
* | __ \.-----.--.--.--| |.-----.----.-----. | ___| | | __|
* | __/| _ | | _ || _ | _| _ | | ___| |__ |
* |___| |___._|__|__|_____||_____|__| |___._| |___| |__|_|__|_______|
*
* ============================================================================
2023-06-08 11:53:13 +02:00
* Copyright (c) 2005-2023 Pandora FMS
2023-06-08 13:19:01 +02:00
* Please see https://pandorafms.com/community/ 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;
// Necessary classes for extends.
require_once $config['homedir'].'/include/class/HTML.class.php';
enterprise_include_once('godmode/admin_access_logs.php');
/**
* Class AuditLog
*/
class AuditLog extends HTML
{
/**
* Allowed methods to be called using AJAX request.
*
* @var array
*/
2022-09-23 11:35:17 +02:00
public $AJAXMethods = ['draw'];
/**
* Ajax page.
*
* @var string
*/
private $ajaxController;
/**
* Class constructor
*
* @param string $ajaxController Ajax controller.
*/
public function __construct(string $ajaxController)
{
global $config;
check_login();
if (check_acl($config['id_user'], 0, 'PM') === false
&& is_user_admin($config['id_user']) === true
) {
db_pandora_audit(
2022-01-20 10:55:23 +01:00
AUDIT_LOG_ACL_VIOLATION,
'Trying to access Audit Logs'
);
include 'general/noaccess.php';
return;
}
// Set the ajax controller.
$this->ajaxController = $ajaxController;
}
/**
* Run view
*
* @return void
*/
public function run()
{
// Javascript.
ui_require_jquery_file('pandora');
// CSS.
ui_require_css_file('wizard');
ui_require_css_file('discovery');
// Datatables list.
try {
$columns = [
2023-02-03 11:19:58 +01:00
[
'text' => 'id_usuario',
'class' => 'w50px',
],
'accion',
'fecha',
'ip_origen',
'descripcion',
];
$column_names = [
__('User'),
__('Action'),
__('Date'),
__('Source IP'),
__('Comments'),
];
if (enterprise_installed() === true) {
array_push(
$columns,
[
'text' => 'security',
2022-11-30 10:14:42 +01:00
'class' => 'w80px table_action_buttons show_security_info',
],
[
'text' => 'action',
2022-11-30 10:14:42 +01:00
'class' => 'w80px table_action_buttons show_extended_info',
]
);
array_push($column_names, __('S.'), __('A.'));
}
$this->tableId = 'audit_logs';
2023-03-02 12:46:47 +01:00
ui_print_standard_header(
__('%s audit', get_product_name()).' &raquo; '.__('Review Logs'),
2023-04-10 14:16:15 +02:00
'images/gm_log@svg.svg',
2023-03-02 12:46:47 +01:00
false,
'',
false,
[],
[
[
2023-03-02 12:46:47 +01:00
'link' => '',
'label' => __('Admin Tools'),
],
[
'link' => '',
'label' => __('System Audit log'),
],
]
);
2023-01-16 11:34:28 +01:00
$buttons = [];
$buttons[] = [
'id' => 'load-filter',
'class' => 'float-left margin-right-2 margin-left-2 sub config',
'text' => __('Load filter'),
2023-02-27 14:38:39 +01:00
'icon' => 'load',
2023-01-16 11:34:28 +01:00
'onclick' => '',
];
$buttons[] = [
'id' => 'save-filter',
'class' => 'float-left margin-right-2 sub wand',
'text' => __('Save filter'),
2023-02-27 14:38:39 +01:00
'icon' => 'save',
2023-01-16 11:34:28 +01:00
'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(
[
'id' => $this->tableId,
'class' => 'info_table',
'style' => 'width: 100%',
'columns' => $columns,
'column_names' => $column_names,
'ajax_url' => $this->ajaxController,
'ajax_data' => ['method' => 'draw'],
'ajax_postprocces' => 'process_datatables_item(item)',
'no_sortable_columns' => [-1],
'order' => [
'field' => 'date',
'direction' => 'asc',
],
'search_button_class' => 'sub filter float-right',
'form' => [
2023-01-16 11:34:28 +01:00
'extra_buttons' => $buttons,
'inputs' => [
[
2023-01-16 11:34:28 +01:00
'label' => __('Free search').ui_print_help_tip(__('Search filter by User, Action, Date, Source IP or Comments fields content'), true),
'type' => 'text',
2023-02-27 14:38:39 +01:00
'class' => 'w100p',
'id' => 'filter_text',
'name' => 'filter_text',
],
[
'label' => __('Date'),
'type' => 'date_range',
],
[
'label' => __('IP'),
'type' => 'text',
2023-02-27 14:38:39 +01:00
'class' => 'w100p',
'id' => 'filter_ip',
'name' => 'filter_ip',
],
[
'label' => __('Action'),
'type' => 'select_from_sql',
'nothing' => __('All'),
'nothing_value' => '-1',
'sql' => 'SELECT DISTINCT(accion), accion AS text FROM tsesion',
2023-02-03 11:19:58 +01:00
'class' => 'mw200px',
'id' => 'filter_type',
'name' => 'filter_type',
],
[
'label' => __('User'),
'type' => 'select_from_sql',
'nothing' => __('All'),
'nothing_value' => '-1',
2023-01-16 11:34:28 +01:00
'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',
2023-02-03 11:19:58 +01:00
'class' => 'mw200px',
'id' => 'filter_user',
'name' => 'filter_user',
],
],
],
2023-02-27 14:38:39 +01:00
'filter_main_class' => 'box-flat white_table_graph fixed_filter_bar',
]
);
} catch (Exception $e) {
echo $e->getMessage();
}
// Load own javascript file.
echo $this->loadJS();
2023-02-27 14:38:39 +01:00
html_print_action_buttons([], ['type' => 'form_action']);
}
/**
* Get the data for draw the table.
*
* @return void.
*/
public function draw()
{
global $config;
// Initialice filter.
$filter = '1=1';
// Init data.
$data = [];
// Count of total records.
$count = 0;
// Catch post parameters.
$start = get_parameter('start', 0);
$length = get_parameter('length', $config['block_size']);
$order = get_datatable_order();
$filters = get_parameter('filter', []);
$this->filterType = $filters['filter_type'];
$this->filterUser = $filters['filter_user'];
$this->filterText = $filters['filter_text'];
$this->filterPeriod = (empty($filters['filter_period']) === false) ? $filters['filter_period'] : 24;
$this->filterIp = $filters['filter_ip'];
if (empty($this->filterType) === false && $this->filterType !== '-1') {
$filter .= sprintf(" AND accion = '%s'", $this->filterType);
}
if (empty($this->filterUser) === false && $this->filterUser !== '-1') {
$filter .= sprintf(" AND id_usuario = '%s'", $this->filterUser);
}
if (empty($this->filterText) === false) {
$filter .= sprintf(
2023-01-16 11:34:28 +01:00
" 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
);
}
if (empty($this->filterIp) === false) {
$filter .= sprintf(" AND ip_origen LIKE '%%%s%%'", $this->filterIp);
}
// Calculate range dates.
$custom_date = $filters['custom_date'];
if ($custom_date === '1') {
$date_from = ($filters['date_init'].' '.$filters['time_init']);
$date_to = ($filters['date_end'].' '.$filters['time_end']);
} else if ($custom_date === '2') {
$period = ($filters['date_text'] * $filters['date_units']);
$date_to = date('Y-m-d H:i:s');
$date_from = date('Y-m-d H:i:s', (strtotime($date_to) - $period));
} else if (in_array($filters['date'], ['this_week', 'this_month', 'past_week', 'past_month'])) {
if ($filters['date'] === 'this_week') {
$date_from = date('Y-m-d 00:00:00', strtotime('last monday'));
$date_to = date('Y-m-d 23:59:59', strtotime($date_from.' +6 days'));
} else if ($filters['date'] === 'this_month') {
$date_from = date('Y-m-d 23:59:59', strtotime('first day of this month'));
$date_to = date('Y-m-d 00:00:00', strtotime('last day of this month'));
} else if ($filters['date'] === 'past_month') {
$date_from = date('Y-m-d 00:00:00', strtotime('first day of previous month'));
$date_to = date('Y-m-d 23:59:59', strtotime('last day of previous month'));
} else if ($filters['date'] === 'past_week') {
$date_from = date('Y-m-d 00:00:00', strtotime('monday', strtotime('last week')));
$date_to = date('Y-m-d 23:59:59', strtotime('sunday', strtotime('last week')));
}
} else {
$date_to = date('Y-m-d H:i:s');
$date_from = date('Y-m-d H:i:s', (strtotime($date_to) - $filters['date']));
}
$filter .= sprintf(' AND fecha BETWEEN "%s" AND "%s"', $date_from, $date_to);
$count = (int) db_get_value_sql(sprintf('SELECT COUNT(*) as "total" FROM tsesion WHERE %s', $filter));
2023-04-21 09:40:14 +02:00
if ($length !== '-1') {
$sql = sprintf(
'SELECT *
FROM tsesion
WHERE %s
ORDER BY %s
LIMIT %d, %d',
$filter,
$order,
$start,
$length
);
} else {
$sql = sprintf(
'SELECT *
FROM tsesion
WHERE %s
ORDER BY %s',
$filter,
$order
);
}
$data = db_get_all_rows_sql($sql);
if (empty($data) === false) {
$data = array_reduce(
$data,
function ($carry, $item) {
global $config;
// Transforms array of arrays $data into an array
// of objects, making a post-process of certain fields.
$tmp = (object) $item;
$tmp->id_usuario = io_safe_output($tmp->id_usuario);
$tmp->ip_origen = io_safe_output($tmp->ip_origen);
$tmp->descripcion = io_safe_output($tmp->descripcion);
$tmp->accion = ui_print_session_action_icon($tmp->accion, true).$tmp->accion;
$tmp->utimestamp = ui_print_help_tip(
date(
$config['date_format'],
$tmp->utimestamp
),
true
).ui_print_timestamp($tmp->utimestamp, true);
if (enterprise_installed() === true) {
2023-02-03 11:19:58 +01:00
$extendedInfo = enterprise_hook('rowEnterpriseAudit', [$tmp->id_sesion]);
if (empty($extendedInfo) === false) {
$tmp->security = enterprise_hook('cell1EntepriseAudit', [$tmp->id_sesion]);
$tmp->action = enterprise_hook('cell2EntepriseAudit', []);
$tmp->extendedInfo = $extendedInfo;
}
}
$carry[] = $tmp;
return $carry;
}
);
}
echo json_encode(
[
'data' => $data,
'recordsTotal' => $count,
'recordsFiltered' => $count,
]
);
}
/**
* Checks if target method is available to be called using AJAX.
*
* @param string $method Target method.
*
* @return boolean True allowed, false not.
*/
public function ajaxMethod(string $method)
{
return in_array($method, $this->AJAXMethods);
}
/**
* Load Javascript code.
*
* @return string.
*/
public function loadJS()
{
// Nothing for this moment.
ob_start();
// Javascript content.
?>
2023-01-16 11:34:28 +01:00
<script type="text/javascript">
var loading = 0;
2022-09-23 11:35:17 +02:00
2023-01-16 11:34:28 +01:00
function format(d) {
var output = '';
2023-01-16 11:34:28 +01:00
if (d.extendedInfo === '') {
output = "<?php echo __('There is no additional information to display'); ?>";
} else {
2023-01-16 11:34:28 +01:00
output = d.extendedInfo;
}
2023-01-16 11:34:28 +01:00
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);
if (row.child.isShown()) {
2023-01-24 16:15:22 +01:00
// This row is already open - close it
2023-01-16 11:34:28 +01:00
row.child.hide();
tr.removeClass('shown');
} else {
// Open this row
row.child(format(row.data())).show();
tr.addClass('shown');
}
2023-01-24 16:15:22 +01:00
$('#audit_logs').css('table-layout','fixed');
$('#audit_logs').css('width','95% !important');
2023-01-16 11:34:28 +01:00
});
2023-03-08 15:30:48 +01:00
$('#button-save-filter').click(function() {
2023-01-16 11:34:28 +01:00
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){
2023-07-12 10:19:08 +02:00
options += "<option value='"+value+"'>"+key+"</option>";
2023-01-16 11:34:28 +01:00
});
$('#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 */
2023-03-08 15:30:48 +01:00
$('#button-load-filter').click(function (){
2023-01-16 11:34:28 +01:00
if($('#load-filter-select').length) {
2023-07-12 10:19:08 +02:00
$('#load-filter-select').dialog({
resizable: true,
draggable: true,
modal: false,
closeOnEscape: true,
width: "auto",
2023-01-16 11:34:28 +01:00
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 = "";
2023-07-12 10:19:08 +02:00
console.log(data)
2023-01-16 11:34:28 +01:00
$.each(data,function(key,value){
2023-07-12 10:19:08 +02:00
options += "<option value='"+value+"'>"+key+"</option>";
2023-01-16 11:34:28 +01:00
});
$('#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'); ?>"
});
}
});
}
}
});
2022-09-23 11:35:17 +02:00
});
2023-01-16 11:34:28 +01:00
</script>
<?php
// EOF Javascript content.
return ob_get_clean();
}
}