diff --git a/pandora_console/godmode/servers/pending_alerts_list.php b/pandora_console/godmode/servers/pending_alerts_list.php new file mode 100644 index 0000000000..054a48aeb0 --- /dev/null +++ b/pandora_console/godmode/servers/pending_alerts_list.php @@ -0,0 +1,71 @@ + '[PendingAlertsList]'.$e->getMessage() ]); + exit; + } else { + echo '[PendingAlertsList]'.$e->getMessage(); + } + + // Stop this execution, but continue 'globally'. + return; +} + +// AJAX controller. +if (is_ajax()) { + $method = get_parameter('method'); + + if (method_exists($adw, $method) === true) { + if ($adw->ajaxMethod($method) === true) { + $adw->{$method}(); + } else { + $adw->error('Unavailable method.'); + } + } else { + $adw->error('Method not found. ['.$method.']'); + } + + // Stop any execution. + exit; +} else { + // Run. + $adw->run(); +} diff --git a/pandora_console/godmode/servers/servers.build_table.php b/pandora_console/godmode/servers/servers.build_table.php index 8cb64950d3..a71dbbaecc 100644 --- a/pandora_console/godmode/servers/servers.build_table.php +++ b/pandora_console/godmode/servers/servers.build_table.php @@ -28,6 +28,7 @@ // Begin. require_once 'include/functions_clippy.php'; +require_once 'pending_alerts_list.php'; global $config; @@ -242,6 +243,19 @@ foreach ($servers as $server) { $data[8] .= ''; } + if ($server['type'] === 'event') { + $data[8] .= ''; + $data[8] .= html_print_image( + 'images/alert@svg.svg', + true, + [ + 'title' => __('Pending alerts list'), + 'class' => 'main_menu_icon invert_filter', + ] + ); + $data[8] .= ''; + } + $data[8] .= ''; $data[8] .= html_print_image( 'images/edit.svg', diff --git a/pandora_console/include/class/AlertsList.class.php b/pandora_console/include/class/AlertsList.class.php new file mode 100644 index 0000000000..99d94fa71c --- /dev/null +++ b/pandora_console/include/class/AlertsList.class.php @@ -0,0 +1,482 @@ +AJAXMethods); + } + + + /** + * Generates a JSON error. + * + * @param string $msg Error message. + * + * @return void + */ + public function error($msg) + { + echo json_encode( + ['error' => $msg] + ); + } + + + /** + * Minor function to dump json message as ajax response. + * + * @param string $type Type: result || error. + * @param string $msg Message. + * @param boolean $delete Deletion messages. + * + * @return void + */ + private function ajaxMsg($type, $msg, $delete=false) + { + if ($type === 'error') { + $msg_title = ($delete === true) ? 'Failed while removing' : 'Failed while saving'; + } else { + $msg_title = ($delete === true) ? 'Successfully deleted' : 'Successfully saved into keystore'; + } + + echo json_encode( + [ $type => __($msg_title).':
'.$msg ] + ); + + exit; + } + + + /** + * Initializes object and validates user access. + * + * @param string $ajax_controller Path of ajaxController, is the 'page' + * variable sent in ajax calls. + * + * @return object + */ + public function __construct($ajax_controller) + { + global $config; + + // Check access. + check_login(); + + if ((bool) check_acl($config['id_user'], 0, 'AR') === false) { + db_pandora_audit( + AUDIT_LOG_ACL_VIOLATION, + 'Trying to access pending alerts list' + ); + + if (is_ajax()) { + echo json_encode(['error' => 'noaccess']); + } else { + include 'general/noaccess.php'; + } + + exit; + } + + $this->ajaxController = $ajax_controller; + + return $this; + } + + + /** + * Prints inputs for modal "Pending alerts list". + * + * @return void + */ + public function loadModal() + { + ob_start(); + echo '
'; + echo $this->getModalContent(); + echo '
'; + echo ob_get_clean(); + } + + + /** + * Run. + * + * @return void + */ + public function run() + { + global $config; + + ui_require_css_file('tables'); + + if (check_acl($config['id_user'], 0, 'AR') === false) { + db_pandora_audit( + AUDIT_LOG_ACL_VIOLATION, + 'Trying to access pending alerts list.' + ); + include 'general/noaccess.php'; + return; + } + + // Auxiliar div for modal. + echo ''; + + echo $this->loadJS(); + } + + + public function drawTable() + { + global $config; + + $start = get_parameter('start', 0); + $length = get_parameter('length', $config['block_size']); + $order = get_datatable_order(true); + + try { + ob_start(); + + $order_by_clause = ''; + + if (in_array($order['field'], ['agentAlias', 'moduleName', 'alertType']) === false) { + $order_by_clause = 'ORDER BY id '.$order['direction']; + } + + if ($length !== '-1') { + $sql = sprintf( + 'SELECT * + FROM talert_execution_queue %s + LIMIT %d, %d', + $order_by_clause, + $start, + $length + ); + } else { + $sql = sprintf( + 'SELECT * FROM talert_execution_queue %s', + $order_by_clause + ); + } + + // Retrieve data and count. + $data = db_get_all_rows_sql($sql); + $count = (int) db_get_sql('SELECT COUNT(*) FROM talert_execution_queue'); + + if ($data) { + $data = array_reduce( + $data, + function ($carry, $item) { + // Check if the item is an array before proceeding. + if (is_array($item) === true) { + // Transforms array of arrays $data into an array + // of objects, making a post-process of certain fields. + $tmp = (object) $item; + $decoded_data = base64_decode($tmp->data); + $decoded_data = json_decode($decoded_data, true); + + if (is_array($decoded_data) === true) { + hd("&&&&&&&&&&&&&&&&&", true); + hd($decoded_data[3]['type'], true); + // Access the second element of $decoded_data (index 1) to get 'alias' and 'type'. + $tmp->agentAlias = isset($decoded_data[1]['alias']) ? $decoded_data[1]['alias'] : null; + $tmp->alertType = isset($decoded_data[3]['type']) ? $decoded_data[3]['type'] : null; + // Access the third element of $decoded_data (index 2) to get 'nombre'. + $tmp->moduleName = isset($decoded_data[2]['nombre']) ? $decoded_data[2]['nombre'] : null; + + $carry[] = $tmp; + } + } + + return $carry; + } + ); + } + + echo json_encode( + [ + 'data' => $data, + 'recordsTotal' => $count, + 'recordsFiltered' => $count, + ] + ); + + // Capture output. + $response = ob_get_clean(); + } catch (Exception $e) { + echo json_encode(['error' => $e->getMessage()]); + exit; + } + + // If not valid, show error with issue. + json_decode($response); + if (json_last_error() == JSON_ERROR_NONE) { + // If valid dump. + echo $response; + } else { + echo json_encode( + ['error' => $response] + ); + } + + exit; + } + + + /** + * Generates content of modal. + * + * @return string Modal content. + */ + public function getModalContent() + { + global $config; + + ob_start(); + + try { + $columns = [ + 'id', + 'agentAlias', + 'moduleName', + 'alertType', + ]; + + $column_names = [ + __('ID'), + __('Agent'), + __('Module'), + __('Type'), + ]; + + $this->tableId = 'pending_alerts'; + ui_print_datatable( + [ + 'id' => $this->tableId, + 'class' => 'info_table', + 'style' => 'width: 99%', + 'columns' => $columns, + 'column_names' => $column_names, + 'ajax_url' => $this->ajaxController, + 'default_pagination' => 7, + 'dom_elements' => 'pfti', + 'ajax_data' => ['method' => 'drawTable'], + 'no_sortable_columns' => [ + 1, + 2, + 3, + ], + 'order' => [ + 'field' => 'id', + 'direction' => 'asc', + ], + ] + ); + } catch (Exception $e) { + echo $e->getMessage(); + } + + return ob_get_clean(); + } + + + /** + * Loads JS content. + * + * @return string JS content. + */ + public function loadJS() + { + ob_start(); + + ui_require_javascript_file('stepper', 'include/javascript/', true); + + // Javascript content. + ?> + +