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, 'LM') === 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 ((bool) check_acl($config['id_user'], 0, 'LM') === 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(); } /** * Draw table. * * @return void */ 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) { // 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. ?>