From bfc1289ba60ff0351384e61e5b7367aea3bfd360 Mon Sep 17 00:00:00 2001 From: Daniel Maya Date: Wed, 19 Oct 2022 13:59:16 +0200 Subject: [PATCH] #9105 Added collections satellite --- .../godmode/servers/modificar_server.php | 10 +- .../class/SatelliteCollection.class.php | 689 ++++++++++++++++++ 2 files changed, 698 insertions(+), 1 deletion(-) create mode 100644 pandora_console/include/class/SatelliteCollection.class.php diff --git a/pandora_console/godmode/servers/modificar_server.php b/pandora_console/godmode/servers/modificar_server.php index 34524c6aa7..17f2020d13 100644 --- a/pandora_console/godmode/servers/modificar_server.php +++ b/pandora_console/godmode/servers/modificar_server.php @@ -137,7 +137,13 @@ if (isset($_GET['server'])) { if ($server_type === SERVER_TYPE_ENTERPRISE_SATELLITE) { $buttons['agent_editor'] = [ 'active' => false, - 'text' => ''.html_print_image('images/agent.png', true, ['title' => __('Advanced editor')]).'', + 'text' => ''.html_print_image('images/agent.png', true, ['title' => __('Manage agents')]).'', + + ]; + + $buttons['collections'] = [ + 'active' => false, + 'text' => ''.html_print_image('images/collection.png', true, ['title' => __('Collections')]).'', ]; } @@ -160,6 +166,8 @@ if (isset($_GET['server'])) { } } else if ($tab === 'agent_editor' && $server_type === SERVER_TYPE_ENTERPRISE_SATELLITE) { $advanced_editor = 'agent_editor'; + } else if ($tab === 'collections' && $server_type === SERVER_TYPE_ENTERPRISE_SATELLITE) { + $advanced_editor = 'collections'; } enterprise_include('godmode/servers/server_disk_conf_editor.php'); diff --git a/pandora_console/include/class/SatelliteCollection.class.php b/pandora_console/include/class/SatelliteCollection.class.php new file mode 100644 index 0000000000..34348fdc7a --- /dev/null +++ b/pandora_console/include/class/SatelliteCollection.class.php @@ -0,0 +1,689 @@ +ajaxController = $ajaxController; + // Capture all parameters before start. + $this->satellite_server = (int) get_parameter('server_remote'); + if ($this->satellite_server !== 0) { + $this->satellite_name = servers_get_name($this->satellite_server); + $this->satellite_config = (array) config_satellite_get_config_file($this->satellite_name); + } + } + + + /** + * Run view + * + * @return void + */ + public function run() + { + // Javascript. + ui_require_jquery_file('pandora'); + // CSS. + ui_require_css_file('wizard'); + ui_require_css_file('discovery'); + + $this->createBlock(); + + if (is_metaconsole() === true) { + // Only in case of Metaconsole, format the frame. + open_meta_frame(); + } + + // Datatables list. + try { + $columns = [ + 'name', + 'dir', + 'description', + 'actions', + ]; + + $column_names = [ + __('Name'), + __('Dir'), + __('Description'), + __('Actions'), + ]; + + $this->tableId = 'satellite_collections'; + + // 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', + 'server_remote' => $this->satellite_server, + ], + 'ajax_postprocces' => 'process_datatables_item(item)', + 'no_sortable_columns' => [ + 0, + 1, + 2, + 3, + ], + 'search_button_class' => 'sub filter float-right', + 'form' => [ + 'inputs' => [ + [ + 'label' => __('Search'), + 'type' => 'text', + 'name' => 'filter_search', + 'size' => 12, + ], + ], + ], + ] + ); + } catch (Exception $e) { + echo $e->getMessage(); + } + + if (is_metaconsole() === true) { + // Close the frame. + close_meta_frame(); + } + + echo ''; + echo ''; + + // Load own javascript file. + echo $this->loadJS(); + } + + + /** + * Get the data for draw the table. + * + * @return void. + */ + public function draw() + { + global $config; + + // 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(true); + $filters = get_parameter('filter', []); + + try { + ob_start(); + + // Gets all collections (database). + $collections = collection_get_collections(null, $filters['filter_search']); + if (empty($collections) === false) { + $data = $collections; + } + + // All satellite conf collections. + foreach ($this->satellite_config as $line) { + $regex = '/^file_collection\s(\S+)/m'; + + if (preg_match($regex, $line, $matches, PREG_OFFSET_CAPTURE, 0) > 0) { + $key = array_search($matches[1][0], array_column($data, 'short_name')); + if ($key !== false) { + $data[$key]['delete'] = true; + } + } + } + + if (empty($data) === false) { + $data = array_reduce( + $data, + function ($carry, $item) { + // Transforms array of arrays $data into an array + // of objects, making a post-process of certain fields. + $tmp = (object) $item; + + $delete = (int) isset($tmp->delete); + + $tmp->dir = $tmp->short_name; + + $tmp->actions = ''; + $tmp->actions .= html_print_image( + ($delete === 0) ? 'images/add.png' : 'images/cross.png', + true, + [ + 'border' => '0', + 'class' => 'action_button_img mrgn_lft_05em invert_filter', + 'onclick' => ($delete === 0) + ? 'add_collection(\''.$tmp->short_name.'\')' + : 'delete_collection(\''.$tmp->short_name.'\')', + ] + ); + + $carry[] = $tmp; + return $carry; + } + ); + } + + if (empty($data) === true) { + $total = 0; + $data = []; + } else { + $total = count($data); + $data = array_slice($data, $start, $length, false); + } + + echo json_encode( + [ + 'data' => $data, + 'recordsTotal' => $total, + 'recordsFiltered' => $total, + ] + ); + // 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; + } + + + /** + * Add collection to satellite conf. + * + * @return void + */ + public function addCollection() + { + $short_name = get_parameter('short_name'); + + if ($this->parseSatelliteConf('add', $short_name) === false) { + $this->ajaxMsg('error', __('Error adding collection')); + } else { + $this->ajaxMsg('result', _('Collection '.$short_name.' added.')); + } + + exit; + } + + + /** + * Delete collection to satellite conf. + * + * @return void + */ + public function deleteCollection() + { + $short_name = get_parameter('short_name'); + + if ($this->parseSatelliteConf('delete', $short_name) === false) { + $this->ajaxMsg('error', __('Error deleting collection')); + } else { + $this->ajaxMsg('result', _('Collection '.$short_name.' deleted.')); + } + + exit; + } + + + /** + * Parse satellite configuration . + * + * @param string $action Action to perform (add, delete). + * @param string $short_name Short name. + * + * @return boolean + */ + private function parseSatelliteConf(string $action, string $short_name) + { + switch ($action) { + case 'delete': + $pos = preg_grep('/^file_collection '.$short_name.'/', $this->satellite_config); + if (empty($pos) === false) { + $key_pos = 0; + foreach ($pos as $key => $value) { + $key_pos = $key; + break; + } + + unset($this->satellite_config[$key_pos]); + } + + $conf = implode('', $this->satellite_config); + break; + + default: + case 'add': + $pos = preg_grep('/^file_collection/', $this->satellite_config); + if (empty($pos) === false) { + $string_collection = 'file_collection '.$short_name."\n"; + + $key_pos = array_keys($pos)[(count($pos) - 1)]; + $array1 = array_slice($this->satellite_config, 0, ($key_pos + 1)); + $array2 = array_slice($this->satellite_config, ($key_pos + 1)); + $array_merge = array_merge($array1, [$string_collection], $array2); + $this->satellite_config = $array_merge; + + // Check config. + if (empty($this->satellite_config) === true) { + return false; + } + } else { + $pos = preg_grep('/^\#\sFile\scollections/', $this->satellite_config); + $string_collection = 'file_collection '.$short_name."\n"; + + $key_pos = 0; + foreach ($pos as $key => $value) { + $key_pos = $key; + break; + } + + $key_pos++; + + $array1 = array_slice($this->satellite_config, 0, $key_pos); + $array2 = array_slice($this->satellite_config, $key_pos); + // Add collection to conf. + $array_merge = array_merge($array1, [$string_collection], $array2); + $this->satellite_config = $array_merge; + + // Check config. + if (empty($this->satellite_config) === true) { + return false; + } + } + + $conf = implode('', $this->satellite_config); + break; + } + + return $this->saveAgent($conf); + } + + + /** + * Saves agent to satellite cofiguration file. + * + * @param string $new_conf Config file. + * + * @return boolean|void + */ + private function saveAgent(string $new_conf) + { + global $config; + + if (empty($new_conf) === true) { + return false; + } + + db_pandora_audit( + AUDIT_LOG_SYSTEM, + 'Update remote config for server '.$this->satellite_name + ); + + // Convert to config file encoding. + $encoding = config_satellite_get_encoding($new_conf); + if ($encoding !== false) { + $converted_server_config = mb_convert_encoding($new_conf, $encoding, 'UTF-8'); + if ($converted_server_config !== false) { + $new_conf = $converted_server_config; + } + } + + // Get filenames. + if ($this->satellite_server !== false) { + $files = config_satellite_get_satellite_config_filenames($this->satellite_name); + } else { + $files = []; + $files['conf'] = $config['remote_config'].'/conf/'.md5($this->satellite_name).'.srv.conf'; + $files['md5'] = $config['remote_config'].'/md5/'.md5($this->satellite_name).'.srv.md5'; + } + + // Save configuration. + $result = file_put_contents($files['conf'], $new_conf); + + if ($result === false) { + return false; + } + + // Save configuration md5. + $result = file_put_contents($files['md5'], md5($new_conf)); + } + + + /** + * 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); + } + + + /** + * Minor function to dump json message as ajax response. + * + * @param string $type Type: result || error. + * @param string $msg Message. + * + * @return void + */ + private function ajaxMsg(string $type, string $msg) + { + if ($type === 'error') { + echo json_encode( + [ + $type => ui_print_error_message( + __($msg), + '', + true + ), + ] + ); + } else { + echo json_encode( + [ + $type => ui_print_success_message( + __($msg), + '', + true + ), + ] + ); + } + + exit; + } + + + /** + * Create file_collections blocks + * + * @return void + */ + public function createBlock() + { + $init = preg_grep('/^\#\sFile\scollections/', $this->satellite_config); + + if (empty($init) === true) { + $collection = "# File collections\n"; + + array_push($this->satellite_config, "\n"); + array_push($this->satellite_config, $collection); + array_push($this->satellite_config, "\n"); + + $conf = implode('', $this->satellite_config); + $this->saveAgent($conf); + } + } + + + /** + * Load Javascript code. + * + * @return string. + */ + public function loadJS() + { + // Nothing for this moment. + ob_start(); + + // Javascript content. + ?> + +