From 0d7ea0d056030ffd5d2300a23ab6c122f72133d5 Mon Sep 17 00:00:00 2001 From: fermin831 Date: Thu, 14 Feb 2019 16:56:03 +0100 Subject: [PATCH] Added CSV report and copy a lot of not revisted code Former-commit-id: 005362b157450862501963770df3fd0419bc573a --- .../godmode/wizards/HostDevices.class.php | 200 ++++++++++++++++-- .../godmode/wizards/Wizard.main.php | 2 +- 2 files changed, 189 insertions(+), 13 deletions(-) diff --git a/pandora_console/godmode/wizards/HostDevices.class.php b/pandora_console/godmode/wizards/HostDevices.class.php index 421ea07484..8a6a14f892 100755 --- a/pandora_console/godmode/wizards/HostDevices.class.php +++ b/pandora_console/godmode/wizards/HostDevices.class.php @@ -8,6 +8,10 @@ require_once $config['homedir'].'/include/functions_users.php'; */ class HostDevices extends Wizard { + // CSV constants. + const HDW_CSV_NOT_DATA = 0; + const HDW_CSV_DUPLICATED = 0; + const HDW_CSV_GROUP_EXISTS = 0; /** * Undocumented variable @@ -114,11 +118,10 @@ class HostDevices extends Wizard if ($mode === null) { $this->setBreadcrum(['Host&devices']); $this->printHeader(); - if (extensions_is_enabled_extension('csv_import')) { - echo 'Importar csv'; - } + echo 'Importar csv'; echo 'Escanear red'; + return; } @@ -126,7 +129,7 @@ class HostDevices extends Wizard $this->setBreadcrum( [ 'Host&devices', - 'Import CSV', + 'Import CSV', ] ); $this->printHeader(); @@ -178,8 +181,7 @@ class HostDevices extends Wizard { global $config; - if (!check_acl($config['id_user'], 0, 'AW') - ) { + if (!check_acl($config['id_user'], 0, 'AW')) { db_pandora_audit( 'ACL Violation', 'Trying to access db status' @@ -188,17 +190,133 @@ class HostDevices extends Wizard return; } - if (!extensions_is_enabled_extension('csv_import')) { - ui_print_error_message( + if (!isset($this->page) || $this->page == 0) { + $this->printForm( [ - 'message' => __('Extension CSV Import is not enabled.'), - 'no_close' => true, + 'form' => [ + 'action' => '#', + 'method' => 'POST', + 'enctype' => 'multipart/form-data', + ], + 'inputs' => [ + [ + 'arguments' => [ + 'type' => 'hidden', + 'name' => 'import_file', + 'value' => 1, + 'return' => true, + ], + ], + [ + 'label' => __('Upload file'), + 'arguments' => [ + 'type' => 'file', + 'name' => 'file', + 'return' => true, + ], + ], + [ + 'label' => __('Server'), + 'arguments' => [ + 'type' => 'select', + 'fields' => servers_get_names(), + 'name' => 'server', + 'return' => true, + ], + ], + [ + 'label' => __('Separator'), + 'arguments' => [ + 'type' => 'select', + 'fields' => [ + ',' => ',', + ';' => ';', + ':' => ':', + '.' => '.', + '#' => '#', + ], + 'name' => 'separator', + 'return' => true, + ], + ], + [ + 'arguments' => [ + 'name' => 'page', + 'value' => 1, + 'type' => 'hidden', + 'return' => true, + ], + ], + [ + 'arguments' => [ + 'name' => 'submit', + 'label' => __('Go'), + 'type' => 'submit', + 'attributes' => 'class="sub next"', + 'return' => true, + ], + ], + ], ] ); - return; } - include_once $config['homedir'].'/enterprise/extensions/csv_import/main.php'; + if (isset($this->page) && $this->page == 1) { + $server = get_parameter('server'); + $separator = get_parameter('separator'); + + if (isset($_FILES['file'])) { + $file_status_code = get_file_upload_status('file'); + $file_status = translate_file_upload_status($file_status_code); + + if ($file_status === true) { + $error_message = []; + $line = -1; + $file = fopen($_FILES['file']['tmp_name'], 'r'); + if (! empty($file)) { + while (($data = fgetcsv($file, 1000, $separator)) !== false) { + $result = $this->processCsvData($data, $server); + $line++; + if ($result === HDW_CSV_NOT_DATA || $result === HDW_CSV_DUPLICATED || $result === HDW_CSV_GROUP_EXISTS) { + if ($result === HDW_CSV_NOT_DATA) { + $error_message[] = __('No data or wrong separator in line ').$line.'
'; + } else if ($result === HDW_CSV_DUPLICATED) { + $error_message[] = __('Agent ').io_safe_input($data[0]).__(' duplicated').'
'; + } else { + $error_message[] = __("Id group %s in line %s doesn't exist in %s", $data[4], $line, get_product_name()).'
'; + } + + continue; + } + + ui_print_result_message( + $result !== false, + __('Created agent %s', $result['agent_name']), + __('Could not create agent %s', $result['agent_name']) + ); + } + } + + fclose($file); + + if (empty($error_message)) { + ui_print_success_message(__('File processed')); + } else { + foreach ($error_message as $msg) { + ui_print_error_message($msg); + } + } + } else { + ui_print_error_message($file_status); + } + + @unlink($_FILES['file']['tmp_name']); + } else { + ui_print_error_message(__('No input file detected')); + } + + echo $this->breadcrum[0]; + } } @@ -405,4 +523,62 @@ class HostDevices extends Wizard } + /** + * Process the csv of agent. + * + * @param array $data Data of agent. + * @param string $server Name of server. + * + * @return array with data porcessed. + */ + private static function processCsvData($data, $server='') + { + if (empty($data) || count($data) < 5) { + return HDW_CSV_NOT_DATA; + } + + $data['network_components'] = array_slice($data, 6); + $data['agent_name'] = io_safe_input($data[0]); + $data['alias'] = io_safe_input($data[0]); + $data['ip_address'] = $data[1]; + $data['id_os'] = $data[2]; + $data['interval'] = $data[3]; + $data['id_group'] = $data[4]; + $data['comentarios'] = io_safe_input($data[5]); + + $exists = (bool) agents_get_agent_id($data['agent_name']); + if ($exists) { + return HDW_CSV_DUPLICATED; + } + + $group_exists_in_pandora = (bool) groups_get_group_by_id($data['id_group']); + if (!$group_exists_in_pandora) { + return HDW_CSV_GROUP_EXISTS; + } + + $data['id_agent'] = agents_create_agent( + $data['agent_name'], + $data['id_group'], + $data['interval'], + $data['ip_address'], + [ + 'id_os' => $data['id_os'], + 'server_name' => $server, + 'modo' => 1, + 'alias' => $data['alias'], + 'comentarios' => $data['comentarios'], + ] + ); + + foreach ($data['network_components'] as $id_network_component) { + network_components_create_module_from_network_component( + (int) $id_network_component, + $data['id_agent'] + ); + } + + return $data; + } + + } diff --git a/pandora_console/godmode/wizards/Wizard.main.php b/pandora_console/godmode/wizards/Wizard.main.php index e8722ea8e8..3298a2e0e7 100644 --- a/pandora_console/godmode/wizards/Wizard.main.php +++ b/pandora_console/godmode/wizards/Wizard.main.php @@ -308,7 +308,7 @@ class Wizard $inputs = $data['inputs']; $js = $data['js']; - $output = '
'; $output .= '