diff --git a/pandora_console/extras/mr/67.sql b/pandora_console/extras/mr/67.sql new file mode 100644 index 0000000000..92db1c919c --- /dev/null +++ b/pandora_console/extras/mr/67.sql @@ -0,0 +1,6 @@ +START TRANSACTION; + +ALTER TABLE `tevento` +ADD COLUMN `custom_field` TEXT NULL AFTER `module_status`; + +COMMIT; diff --git a/pandora_console/godmode/events/custom_events.php b/pandora_console/godmode/events/custom_events.php index c91a2df8fe..bfa6dce937 100644 --- a/pandora_console/godmode/events/custom_events.php +++ b/pandora_console/godmode/events/custom_events.php @@ -115,6 +115,7 @@ $fields_available['module_status'] = __('Module Status'); $fields_available['mini_severity'] = __('Severity mini'); $fields_available['module_custom_id'] = __('Module custom ID'); $fields_available['custom_data'] = __('Custom data'); +$fields_available['custom_field'] = __('Custom field'); // Remove fields already selected. diff --git a/pandora_console/include/ajax/events.php b/pandora_console/include/ajax/events.php index f9bf50b7ae..517777aacc 100644 --- a/pandora_console/include/ajax/events.php +++ b/pandora_console/include/ajax/events.php @@ -92,6 +92,7 @@ $get_id_source_event = get_parameter('get_id_source_event'); $node_id = (int) get_parameter('node_id', 0); $settings_modal = get_parameter('settings', 0); $parameters_modal = get_parameter('parameters', 0); +$update_custom_field = get_parameter('update_custom_field', 0); // User private filter. $current_filter = get_parameter('current_filter', 0); $private_filter_event = get_parameter('private_filter_event', 0); @@ -2759,3 +2760,50 @@ if ($draw_row_response_info === true) { echo $output; return; } + +if ($update_custom_field) { + $custom_field = get_parameter('custom_field_value'); + $event_id = get_parameter('event_id'); + $server_id = 0; + if (is_metaconsole() === true) { + $server_id = (int) get_parameter('server_id'); + } + + // Safe custom fields for hacks. + if (preg_match('/script/i', io_safe_output($custom_field))) { + $return = false; + } else { + try { + if (is_metaconsole() === true + && $server_id > 0 + ) { + $node = new Node($server_id); + $node->connect(); + } + + $return = events_custom_field( + $event_id, + $custom_field + ); + } catch (\Exception $e) { + // Unexistent agent. + if (is_metaconsole() === true + && $server_id > 0 + ) { + $node->disconnect(); + } + + $return = false; + } finally { + if (is_metaconsole() === true + && $server_id > 0 + ) { + $node->disconnect(); + } + } + } + + echo ($return === true) ? 'update_ok' : 'update_error'; + + return; +} \ No newline at end of file diff --git a/pandora_console/include/functions_api.php b/pandora_console/include/functions_api.php index 7e22befbee..dc6bdeb30f 100644 --- a/pandora_console/include/functions_api.php +++ b/pandora_console/include/functions_api.php @@ -17783,3 +17783,45 @@ function api_token_check(string $token) return db_get_value('id_user', 'tusuario', 'api_token', $token); } } + + +/** + * Set custom field value in tevento + * + * @param mixed $id_event Event id. + * @param mixed $custom_field Custom field to set. + * @return void + */ +function api_set_event_custom_field($id, $value) +{ + // Get the event + $event = events_get_event($id, false, is_metaconsole()); + // If event not exists, end the execution. + if ($event === false) { + returnError( + 'event_not_exists', + 'Event not exists' + ); + $result = false; + } + + // Safe custom fields for hacks. + if (preg_match('/script/i', io_safe_output($value))) { + $result = false; + } + + $result = events_custom_field( + $id, + $value + ); + + // If update results failed + if (empty($result) === true || $result === false) { + returnError( + 'The event could not be updated' + ); + return false; + } else { + returnData('string', ['data' => 'Event updated.']); + } +} diff --git a/pandora_console/include/functions_events.php b/pandora_console/include/functions_events.php index 7068d554e6..c40a1eb9c7 100644 --- a/pandora_console/include/functions_events.php +++ b/pandora_console/include/functions_events.php @@ -219,6 +219,7 @@ function events_get_all_fields() $columns['module_status'] = __('Module status'); $columns['module_custom_id'] = __('Module custom id'); $columns['custom_data'] = __('Custom data'); + $columns['custom_field'] = __('Custom field'); return $columns; } @@ -322,6 +323,9 @@ function events_get_column_name($field, $table_alias=false) case 'custom_data': return __('Custom data'); + case 'custom_field': + return __('Custom field'); + default: return __($field); } @@ -4632,6 +4636,22 @@ function events_page_details($event, $server_id=0) $table_details->data[] = $data; + $data = []; + $data[0] = __('Custom Field'); + $data[1] = '
'.html_print_input_text('custom_field', $event['custom_field'], '', false, 255, true, false, false, '', 'w60p'); + $data[1] .= html_print_button( + __('Update'), + 'update_custom_field', + false, + 'update_custom_field('.$event['id_evento'].', '.$event['server_id'].');', + [ + 'icon' => 'next', + 'mode' => 'link', + ], + true + ).'
'; + $table_details->data[] = $data; + $details = '
'.html_print_table($table_details, true).'
'; if (is_metaconsole() === true && empty($server_id) === false) { @@ -6203,3 +6223,57 @@ function event_get_counter_extraId(array $event, ?array $filters) return $counters; } + + +/** + * Update event detail custom field + * + * @param mixed $id_event Event ID or array of events. + * @param string $custom_field Custom_field to be update. + * + * @return boolean Whether or not it was successful + */ +function events_custom_field( + $id_event, + $custom_field, +) { + global $config; + // Cleans up the selection for all unwanted values also casts any single + // values as an array. + $id_event = (array) safe_int($id_event, 1); + // Check ACL. + foreach ($id_event as $k => $id) { + $event_group = events_get_group($id); + if (check_acl($config['id_user'], $event_group, 'EW') == 0) { + db_pandora_audit( + AUDIT_LOG_ACL_VIOLATION, + 'Attempted updating event #'.$id + ); + + unset($id_event[$k]); + } + } + + if (empty($id_event) === true) { + return false; + } + + // Get the current event comments. + $first_event = $id_event; + if (is_array($id_event) === true) { + $first_event = reset($id_event); + } + + // Update comment. + $ret = db_process_sql_update( + 'tevento', + ['custom_field' => $custom_field], + ['id_evento' => $first_event] + ); + + if (($ret === false) || ($ret === 0)) { + return false; + } + + return true; +} diff --git a/pandora_console/include/javascript/pandora_events.js b/pandora_console/include/javascript/pandora_events.js index 940fd3ce5d..ce782769cb 100644 --- a/pandora_console/include/javascript/pandora_events.js +++ b/pandora_console/include/javascript/pandora_events.js @@ -492,6 +492,37 @@ function event_comment(current_event) { return false; } +// Save custom_field into an event. +function update_custom_field(event_id, server_id) { + var custom_field_value = $("#text-custom_field").val(); + + var params = { + page: "include/ajax/events", + update_custom_field: 1, + custom_field_value: custom_field_value, + event_id: event_id, + server_id: server_id + }; + + $("#button-update_custom_field").attr("disabled", "disabled"); + $("#response_loading").show(); + + jQuery.ajax({ + data: params, + type: "POST", + url: getUrlAjax(), + dataType: "html", + success: function(data) { + if (data === "update_error") { + alert("Custom field not valid"); + } + $("#button-update_custom_field").removeAttr("disabled"); + $("#response_loading").hide(); + $("#button-events_form_search_bt").trigger("click"); + } + }); +} + var processed = 0; function update_event(table, id_evento, type, event_rep, row, server_id) { var inputs = $("#events_form :input"); diff --git a/pandora_console/include/lib/Dashboard/Widgets/events_list.php b/pandora_console/include/lib/Dashboard/Widgets/events_list.php index b7ad581dc0..b84f11aa4a 100644 --- a/pandora_console/include/lib/Dashboard/Widgets/events_list.php +++ b/pandora_console/include/lib/Dashboard/Widgets/events_list.php @@ -837,6 +837,7 @@ class EventsListWidget extends Widget 'mini_severity' => __('Severity mini'), 'module_custom_id' => __('Module custom ID'), 'custom_data' => __('Custom data'), + 'custom_field' => __('Custom field'), ]; } diff --git a/pandora_console/operation/events/events.php b/pandora_console/operation/events/events.php index 059c8f90ec..bde20209a4 100644 --- a/pandora_console/operation/events/events.php +++ b/pandora_console/operation/events/events.php @@ -377,6 +377,7 @@ if (is_ajax() === true) { 'te.owner_user', 'if(te.ack_utimestamp > 0, te.ack_utimestamp,"") as ack_utimestamp', 'te.custom_data', + 'te.custom_field', 'te.data', 'te.module_status', 'ta.alias as agent_name', diff --git a/pandora_console/pandoradb.sql b/pandora_console/pandoradb.sql index 662beb93af..030ef3a76f 100644 --- a/pandora_console/pandoradb.sql +++ b/pandora_console/pandoradb.sql @@ -723,6 +723,7 @@ CREATE TABLE IF NOT EXISTS `tevento` ( `custom_data` TEXT, `data` TINYTEXT, `module_status` INT NOT NULL DEFAULT 0, + `custom_field` TEXT, PRIMARY KEY (`id_evento`), KEY `idx_agente` (`id_agente`), KEY `idx_agentmodule` (`id_agentmodule`),