diff --git a/pandora_console/godmode/wizards/HostDevices.class.php b/pandora_console/godmode/wizards/HostDevices.class.php index ef383e8233..f0e8325459 100755 --- a/pandora_console/godmode/wizards/HostDevices.class.php +++ b/pandora_console/godmode/wizards/HostDevices.class.php @@ -28,9 +28,10 @@ require_once __DIR__.'/Wizard.main.php'; require_once $config['homedir'].'/include/functions_users.php'; +require_once $config['homedir'].'/include/class/CustomNetScan.class.php'; +require_once $config['homedir'].'/include/class/ManageNetScanScripts.class.php'; + enterprise_include_once('include/class/CSVImportAgents.class.php'); -enterprise_include_once('include/class/CustomNetScan.class.php'); -enterprise_include_once('include/class/ManageNetScanScripts.class.php'); enterprise_include_once('include/functions_hostdevices.php'); /** @@ -126,20 +127,20 @@ class HostDevices extends Wizard 'icon' => ENTERPRISE_DIR.'/images/wizard/csv.png', 'label' => __('Import CSV'), ]; - - $buttons[] = [ - 'url' => $this->url.'&mode=customnetscan', - 'icon' => ENTERPRISE_DIR.'/images/wizard/customnetscan.png', - 'label' => __('Custom NetScan'), - ]; - - $buttons[] = [ - 'url' => $this->url.'&mode=managenetscanscripts', - 'icon' => ENTERPRISE_DIR.'/images/wizard/managenetscanscripts.png', - 'label' => __('Manage NetScan scripts'), - ]; } + $buttons[] = [ + 'url' => $this->url.'&mode=customnetscan', + 'icon' => '/images/wizard/customnetscan.png', + 'label' => __('Custom NetScan'), + ]; + + $buttons[] = [ + 'url' => $this->url.'&mode=managenetscanscripts', + 'icon' => '/images/wizard/managenetscanscripts.png', + 'label' => __('Manage NetScan scripts'), + ]; + $this->prepareBreadcrum( [ [ @@ -165,22 +166,22 @@ class HostDevices extends Wizard ); return $csv_importer->runCSV(); } + } - if ($mode === 'customnetscan') { - $customnetscan_importer = new CustomNetScan( - $this->page, - $this->breadcrum - ); - return $customnetscan_importer->runCustomNetScan(); - } + if ($mode === 'customnetscan') { + $customnetscan_importer = new CustomNetScan( + $this->page, + $this->breadcrum + ); + return $customnetscan_importer->runCustomNetScan(); + } - if ($mode === 'managenetscanscripts') { - $managenetscanscript_importer = new ManageNetScanScripts( - $this->page, - $this->breadcrum - ); - return $managenetscanscript_importer->runManageNetScanScript(); - } + if ($mode === 'managenetscanscripts') { + $managenetscanscript_importer = new ManageNetScanScripts( + $this->page, + $this->breadcrum + ); + return $managenetscanscript_importer->runManageNetScanScript(); } if ($mode == 'netscan') { diff --git a/pandora_console/images/wizard/customnetscan.png b/pandora_console/images/wizard/customnetscan.png new file mode 100644 index 0000000000..edc036fa39 Binary files /dev/null and b/pandora_console/images/wizard/customnetscan.png differ diff --git a/pandora_console/images/wizard/managenetscanscripts.png b/pandora_console/images/wizard/managenetscanscripts.png new file mode 100644 index 0000000000..4694ec1e7a Binary files /dev/null and b/pandora_console/images/wizard/managenetscanscripts.png differ diff --git a/pandora_console/include/class/CustomNetScan.class.php b/pandora_console/include/class/CustomNetScan.class.php new file mode 100644 index 0000000000..a0f8af019d --- /dev/null +++ b/pandora_console/include/class/CustomNetScan.class.php @@ -0,0 +1,758 @@ +url = ui_get_full_url( + 'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=hd' + ); + $this->page = $page; + $this->breadcrum = $breadcrum; + } + + + /** + * Retrieves and validates information given by user in NetScan wizard. + * + * @return boolean Data OK or not. + */ + public function parseNetScan() + { + if (isset($this->page) === true && $this->page === 0) { + // Check if we're updating a task. + $task_id = get_parameter('task', null); + + if (isset($task_id) === true) { + // We're updating this task. + $task = db_get_row( + 'trecon_task', + 'id_rt', + $task_id + ); + + if ($task !== false) { + $this->task = $task; + } + } + + return true; + } + + // Validate response from page 0. No, not a bug, we're always 1 page + // from 'validation' page. + if (isset($this->page) === true && $this->page === 1) { + $task_id = get_parameter('task', null); + $taskname = get_parameter('taskname', ''); + $comment = get_parameter('comment', ''); + $server_id = get_parameter('id_recon_server', ''); + $id_group = get_parameter('id_group', ''); + $interval = get_parameter('interval', 0); + + if (isset($task_id) === true) { + // We're updating this task. + $task = db_get_row( + 'trecon_task', + 'id_rt', + $task_id + ); + + if ($task !== false) { + $this->task = $task; + } + } else if (isset($taskname) === true) { + // Avoid double creation. + $task = db_get_row_filter( + 'trecon_task', + ['name' => $taskname] + ); + + if ($task !== false) { + $this->task = $task; + $this->msg = __('This task has been already defined. Please edit it or create a new one.'); + return false; + } + } + + if ($task_id !== null + && $taskname == null + && $server_id == null + && $id_group == null + && $server == null + && $datacenter == '' + && $user == '' + && $pass == '' + && $encrypt == null + && $interval == 0 + ) { + // Default values, no data received. + // User is accesing directly to this page. + if (users_is_admin() !== true && check_acl( + $config['id_usuario'], + $this->task['id_group'], + 'PM' + ) !== true + ) { + $this->msg = __('You have no access to edit this task.'); + return false; + } + } else { + if (isset($this->task['id_rt']) === false) { + // Disabled 2 Implies wizard non finished. + $this->task['disabled'] = 2; + } + + if ($taskname == '') { + $this->msg = __('You must provide a task name.'); + return false; + } + + if ($server_id == '') { + $this->msg = __('You must select a Discovery Server.'); + return false; + } + + if ($id_group == '') { + $this->msg = __('You must select a valid group.'); + return false; + } + + // Assign fields. + $this->task['name'] = $taskname; + $this->task['description'] = $comment; + $this->task['id_recon_server'] = $server_id; + $this->task['id_group'] = $id_group; + $this->task['interval_sweep'] = $interval; + + if (isset($this->task['id_rt']) === false) { + // Create. + $this->task['id_rt'] = db_process_sql_insert( + 'trecon_task', + $this->task + ); + } else { + // Update. + db_process_sql_update( + 'trecon_task', + $this->task, + ['id_rt' => $this->task['id_rt']] + ); + } + } + + return true; + } + + // Validate response from page 1. + if ($this->page == 2) { + $id_rt = get_parameter('task', -1); + + $task = db_get_row( + 'trecon_task', + 'id_rt', + $id_rt + ); + + if ($task !== false) { + $this->task = $task; + } else { + $this->msg = __('Failed to find network scan task.'); + return false; + } + + $id_recon_script = get_parameter('id_recon_script', null); + $field1 = get_parameter('_field1_', ''); + $field2 = get_parameter('_field2_', ''); + $field3 = get_parameter('_field3_', ''); + $field4 = get_parameter('_field4_', ''); + + // Get macros. + $macros = get_parameter('macros', null); + + if (empty($macros) === false) { + $macros = json_decode( + base64_decode($macros), + true + ); + + foreach ($macros as $k => $m) { + $macros[$k]['value'] = get_parameter($m['macro'], ''); + } + } + + $this->task['id_recon_script'] = $id_recon_script; + $this->task['macros'] = io_json_mb_encode($macros); + $this->task['field1'] = $field1; + $this->task['field2'] = $field2; + $this->task['field3'] = $field3; + $this->task['field4'] = $field4; + + if ($this->task['disabled'] == 2) { + // Wizard finished. + $this->task['disabled'] = 0; + } + + // Update. + $res = db_process_sql_update( + 'trecon_task', + $this->task, + ['id_rt' => $this->task['id_rt']] + ); + + return true; + } + + return false; + } + + + /** + * Run function. It will be call into HostsDevices class. + * Page 0: Upload form. + * Page 1: Task resume. + * + * @return void + */ + public function runCustomNetScan() + { + global $config; + + if (!check_acl($config['id_user'], 0, 'PM')) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access Custom Net Scan.' + ); + include 'general/noaccess.php'; + return; + } + + if ($this->parseNetScan() === false) { + // Error. + ui_print_error_message( + $this->msg + ); + + $form = [ + 'form' => [ + 'method' => 'POST', + 'action' => $this->url.'&mode=customnetscan&page='.($this->page - 1).'&task='.$this->task['id_rt'], + ], + 'inputs' => [ + [ + 'arguments' => [ + 'type' => 'hidden', + 'name' => 'task', + 'value' => $this->task['id_rt'], + ], + ], + [ + 'arguments' => [ + 'name' => 'submit', + 'label' => __('Go back'), + 'type' => 'submit', + 'attributes' => 'class="sub cancel"', + 'return' => true, + ], + ], + ], + ]; + + // Check ACL. If user is not able to manage target task, + // redirect him to main page. + if (users_is_admin() !== true && check_acl( + $config['id_usuario'], + $this->task['id_group'], + 'PM' + ) !== true + ) { + $form['form']['action'] = $this->url.'&mode=customnetscan&page='.($this->page - 1); + } + + $this->printForm($form); + return null; + } + + $run_url = 'index.php?sec=gservers&sec2=godmode/servers/discovery'; + + $task_url = ''; + if (isset($this->task['id_rt']) === true) { + $task_url = '&task='.$this->task['id_rt']; + } + + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd', + 'label' => __('Host & Devices'), + ]; + + for ($i = 0; $i < $this->MAXPAGES; $i++) { + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd&mode=customnetscan&page='.$i.$task_url, + 'label' => __($this->pageLabels[$i]), + 'selected' => (($i == $this->page) ? 1 : 0), + ]; + } + + if ($this->page < $this->MAXPAGES) { + // Avoid to print header out of wizard. + $this->prepareBreadcrum($breadcrum); + $this->printHeader(); + } + + $task_url = ''; + if (isset($this->task['id_rt'])) { + $task_url = '&task='.$this->task['id_rt']; + } + + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd', + 'label' => __($this->label), + ]; + for ($i = 0; $i < $this->maxPagesNetScan; $i++) { + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd&mode=customnetscan&page='.$i.$task_url, + 'label' => $this->pageLabelsNetScan[$i], + 'selected' => (($i == $this->page) ? 1 : 0), + ]; + } + + if ($this->page < $this->maxPagesNetScan) { + // Avoid to print header out of wizard. + $this->prepareBreadcrum($breadcrum); + $this->printHeader(); + } + + if (isset($this->page) === true + && $this->page !== 0 + && isset($this->task['id_rt']) === false + ) { + // Error. + ui_print_error_message( + __('Internal error, please re-run this wizard.') + ); + + $form = [ + 'form' => [ + 'method' => 'POST', + 'action' => $this->url.'&mode=customnetscan&page=0', + ], + 'inputs' => [ + [ + 'arguments' => [ + 'type' => 'hidden', + 'name' => 'page', + 'value' => 0, + ], + ], + [ + 'arguments' => [ + 'name' => 'submit', + 'label' => __('Go back'), + 'type' => 'submit', + 'attributes' => 'class="sub cancel"', + 'return' => true, + ], + ], + ], + ]; + + $this->printForm($form); + return null; + } + + // -------------------------------. + // Page 0. wizard starts HERE. + // -------------------------------. + if (isset($this->page) === true || $this->page == 0) { + if (isset($this->page) === false + || $this->page == 0 + ) { + $form = []; + + // Input task name. + $form['inputs'][] = [ + 'label' => ''.__('Task name').'', + 'arguments' => [ + 'name' => 'taskname', + 'value' => $this->task['name'], + 'type' => 'text', + 'size' => 25, + ], + ]; + + if (isset($this->task['id_rt']) === true) { + // Propagate id. + $form['inputs'][] = [ + 'arguments' => [ + 'name' => 'task', + 'value' => $this->task['id_rt'], + 'type' => 'hidden', + ], + ]; + } + + // Input task description. + $form['inputs'][] = [ + 'label' => ''.__('Comment').'', + 'arguments' => [ + 'name' => 'comment', + 'value' => $this->task['description'], + 'type' => 'text', + 'size' => 25, + ], + ]; + + // Input Discovery Server. + $form['inputs'][] = [ + 'label' => ''.__('Discovery server').''.ui_print_help_tip( + __('You must select a Discovery Server to run the Task, otherwise the Recon Task will never run'), + true + ), + 'arguments' => [ + 'type' => 'select_from_sql', + 'sql' => sprintf( + 'SELECT id_server, name + FROM tserver + WHERE server_type = %d + ORDER BY name', + SERVER_TYPE_DISCOVERY + ), + 'name' => 'id_recon_server', + 'selected' => $this->task['id_recon_server'], + 'return' => true, + ], + ]; + + // Input Group. + $form['inputs'][] = [ + 'label' => ''.__('Group').'', + 'arguments' => [ + 'name' => 'id_group', + 'returnAllGroup' => false, + 'privilege' => 'PM', + 'type' => 'select_groups', + 'selected' => $this->task['id_group'], + 'return' => true, + ], + ]; + + // Interval and schedules. + $interv_manual = 0; + if ((int) $this->task['interval_sweep'] == 0) { + $interv_manual = 1; + } + + // Schedule. + $form['inputs'][] = [ + 'label' => ''.__('Interval').''.ui_print_help_tip( + __('Manual interval means that it will be executed only On-demand'), + true + ), + 'arguments' => [ + 'type' => 'select', + 'selected' => $interv_manual, + 'fields' => [ + 0 => __('Defined'), + 1 => __('Manual'), + ], + 'name' => 'interval_manual_defined', + 'return' => true, + ], + 'extra' => ''.html_print_extended_select_for_time( + 'interval', + $this->task['interval_sweep'], + '', + '', + '0', + false, + true, + false, + false + ).ui_print_help_tip( + __('The minimum recomended interval for Recon Task is 5 minutes'), + true + ).'', + ]; + + $str = __('Next'); + + if (isset($this->task['id_rt']) === true) { + $str = __('Update and continue'); + } + + // Submit button. + $form['inputs'][] = [ + 'arguments' => [ + 'name' => 'submit', + 'label' => $str, + 'type' => 'submit', + 'attributes' => 'class="sub next"', + 'return' => true, + ], + ]; + + $task_url = ''; + if (isset($this->task['id_rt'])) { + $task_url = '&task='.$this->task['id_rt']; + } + + $form['form'] = [ + 'method' => 'POST', + 'action' => $this->url.'&mode=customnetscan&page='.($this->page + 1).$task_url, + ]; + + // Default. + $interval = 600; + $unit = 60; + if (isset($this->task['interval_sweep']) === true) { + $interval = $this->task['interval_sweep']; + $unit = $this->getTimeUnit($interval); + } + + $form['js'] = ' + $("select#interval_manual_defined").change(function() { + if ($("#interval_manual_defined").val() == 1) { + $("#interval_manual_container").hide(); + $("#text-interval_text").val(0); + $("#hidden-interval").val(0); + } + else { + $("#interval_manual_container").show(); + $("#text-interval_text").val(10); + $("#hidden-interval").val('.$interval.'); + $("#interval_units").val('.$unit.'); + } + }).change(); + '; + + // XXX: Could be improved validating inputs before continue (JS) + // Print NetScan page 0. + $this->printForm($form); + } + } + + if (isset($this->page) === true && $this->page === 1) { + $name_ipam = 'IPAM Recon'; + // Recon script. + $form['inputs'][] = [ + 'label' => ''.__('Recon script').'', + 'arguments' => [ + 'type' => 'select_from_sql', + 'sql' => sprintf( + 'SELECT id_recon_script, name FROM trecon_script WHERE name <> "%s" ORDER BY name', + $name_ipam + ), + 'name' => 'id_recon_script', + 'selected' => $this->task['id_recon_script'], + 'return' => true, + ], + ]; + + $form['inputs'][] = [ + 'hidden' => 1, + 'arguments' => [ + 'type' => 'hidden', + 'name' => 'task', + 'value' => $this->task['id_rt'], + ], + ]; + + $form['inputs'][] = [ + 'hidden' => 1, + 'arguments' => [ + 'type' => 'hidden_extended', + 'name' => 'macros', + 'value' => base64_encode($this->task['macros']), + 'return' => true, + ], + ]; + + // Explanation. + $explanation = db_get_value( + 'description', + 'trecon_script', + 'id_recon_script', + $this->task['id_recon_script'] + ); + + $form['inputs'][] = [ + 'label' => ''.__('Explanation').'', + 'arguments' => [ + 'type' => 'textarea', + 'rows' => 4, + 'columns' => 60, + 'name' => 'explanation', + 'value' => $explanation, + 'return' => true, + 'attributes' => 'style="width: 388px;"', + ], + ]; + + $form['inputs'][] = [ + 'hidden' => 1, + 'id' => 'table_recon-macro_field', + 'label' => ''.__('macro_desc').''.ui_print_help_tip('macro_help', true), + 'arguments' => [ + 'name' => 'macro_name', + 'value' => 'macro_value', + 'type' => 'text', + 'size' => 100, + 'return' => true, + ], + ]; + + if (empty($this->task['macros']) === false) { + $macros = json_decode($this->task['macros'], true); + foreach ($macros as $k => $m) { + $label_macro = ''; + $label_macro .= ''.$m['desc'].''; + if (!empty($m['help'])) { + $label_macro .= ui_print_help_tip( + $m['help'], + true + ); + } + + if ($m['hide']) { + $form['inputs'][] = [ + 'label' => $label_macro, + 'id' => 'table_recon-macro'.$m['macro'], + 'class' => 'macro_field', + 'arguments' => [ + 'name' => $m['macro'], + 'value' => $m['value'], + 'type' => 'password', + 'size' => 100, + 'return' => true, + ], + ]; + } else { + $form['inputs'][] = [ + 'label' => $label_macro, + 'id' => 'table_recon-macro'.$m['macro'], + 'class' => 'macro_field', + 'arguments' => [ + 'name' => $m['macro'], + 'value' => $m['value'], + 'type' => 'text', + 'size' => 100, + 'return' => true, + ], + ]; + } + } + } + + // Submit button. + $form['inputs'][] = [ + 'arguments' => [ + 'name' => 'submit', + 'label' => __('Finish'), + 'type' => 'submit', + 'attributes' => 'class="sub next"', + 'return' => true, + ], + ]; + + $form['form'] = [ + 'method' => 'POST', + 'action' => $this->url.'&mode=customnetscan&page='.($this->page + 1).'&task='.$this->task['id_rt'], + ]; + + $id_task = (isset($this->task['id_rt']) === true) ? $this->task['id_rt'] : 0; + + $url_ajax = $config['homeurl'].'ajax.php'; + + $change = ''; + if (empty($this->task['macros']) !== false) { + $change = '.change();'; + } + + $form['js'] = ' + $("select#id_recon_script").change(function() { + get_explanation_recon_script($(this).val(), "'.$id_task.'", "'.$url_ajax.'"); + })'.$change; + + $this->printForm($form); + } + + if (isset($this->page) === true && $this->page === 2) { + if ($this->task['id_rt']) { + // 0 - Is OK. + $this->result = 0; + $this->msg = __('Task configured.'); + } else { + // 1 - Is NOT OK. + $this->result = 1; + $this->msg = __('Wizard failed. Cannot configure task.'); + } + + return [ + 'result' => $this->result, + 'id' => $this->task['id_rt'], + 'msg' => $this->msg, + ]; + } + + ui_require_javascript_file('pandora_modules'); + } + + +} diff --git a/pandora_console/include/class/ManageNetScanScripts.class.php b/pandora_console/include/class/ManageNetScanScripts.class.php new file mode 100644 index 0000000000..377259478a --- /dev/null +++ b/pandora_console/include/class/ManageNetScanScripts.class.php @@ -0,0 +1,759 @@ +url = ui_get_full_url( + 'index.php?sec=gservers&sec2=godmode/servers/discovery&wiz=hd' + ); + $this->page = $page; + $this->breadcrum = $breadcrum; + } + + + /** + * Run function. It will be call into HostsDevices class. + * Page 0: Upload form. + * Page 1: Task resume. + * + * @return void + */ + public function runManageNetScanScript() + { + global $config; + + if (check_acl($config['id_user'], 0, 'AW') === 0) { + db_pandora_audit( + 'ACL Violation', + 'Trying to access Net Scan Script.' + ); + include 'general/noaccess.php'; + return; + } + + $run_url = 'index.php?sec=gservers&sec2=godmode/servers/discovery'; + + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd', + 'label' => __('Host & Devices'), + ]; + + for ($i = 0; $i < $this->MAXPAGES; $i++) { + $breadcrum[] = [ + 'link' => $run_url.'&wiz=hd&mode=managenetscanscripts&page='.$i, + 'label' => __($this->pageLabels[$i]), + 'selected' => (($i == $this->page) ? 1 : 0), + ]; + } + + if ($this->page < $this->MAXPAGES) { + // Avoid to print header out of wizard. + $this->prepareBreadcrum($breadcrum); + $this->printHeader(); + } + + $id_script = get_parameter('id_script', 0); + + // Initialize msg. + $msg = []; + + // Operations. + $operation_scp = get_parameter('operation_scp', ''); + if ($operation_scp !== '') { + switch ($operation_scp) { + case 'update_scp': + $msg = $this->updateScanScripts($id_script); + break; + + case 'delete_scp': + $msg = $this->deleteScanScripts($id_script); + break; + + case 'create_scp': + $msg = $this->createScanScripts($id_script); + break; + + default: + // Nothing for doing. Never exist other operation. + break; + } + } + + if (!isset($this->page) || $this->page === 0) { + $this->printListNetScanScripts($msg); + } + + if (!isset($this->page) || $this->page === 1) { + $this->printFormScanScripts($id_script); + } + } + + + /** + * Create net scan script. + * + * @return array Check msg successfully or problem + */ + private function createScanScripts() + { + $result = []; + + $reconscript_name = get_parameter('form_name', ''); + $reconscript_description = get_parameter('form_description', ''); + $reconscript_script = get_parameter('form_script', ''); + + // Get macros. + $i = 1; + $macros = []; + while (1) { + $macro = (string) get_parameter('field'.$i.'_macro'); + if ($macro == '') { + break; + } + + $desc = (string) get_parameter('field'.$i.'_desc'); + $help = (string) get_parameter('field'.$i.'_help'); + $value = (string) get_parameter('field'.$i.'_value'); + $hide = get_parameter('field'.$i.'_hide'); + + $macros[$i]['macro'] = $macro; + $macros[$i]['desc'] = $desc; + $macros[$i]['help'] = $help; + $macros[$i]['value'] = $value; + $macros[$i]['hide'] = $hide; + $i++; + } + + $macros = io_json_mb_encode($macros); + + $values = [ + 'name' => $reconscript_name, + 'description' => $reconscript_description, + 'script' => $reconscript_script, + 'macros' => $macros, + ]; + + $result_crt = false; + if ($values['name'] !== '' && $values['script'] !== '') { + $result_crt = db_process_sql_insert('trecon_script', $values); + if (!$result_crt) { + $result = [ + 'error' => 1, + 'msg' => __('Problem creating'), + ]; + } else { + $result = [ + 'error' => 0, + 'msg' => __('Created successfully'), + ]; + } + } else { + $result = [ + 'error' => 1, + 'msg' => __('Name or Script fullpath they can not be empty'), + ]; + } + + return $result; + } + + + /** + * Update net scan script. + * + * @param integer $id_script Id script. + * + * @return array Check msg successfully or problem + */ + private function updateScanScripts(int $id_script) + { + $result = []; + if (isset($id_script) === false || $id_script === 0) { + $result = [ + 'error' => 1, + 'msg' => __('Problem deleting Net scan Scripts, Not selected script'), + ]; + + return $result; + } + + // If modified any parameter. + $reconscript_name = get_parameter('form_name', ''); + $reconscript_description = get_parameter('form_description', ''); + $reconscript_script = get_parameter('form_script', ''); + + // Get macros. + $i = 1; + $macros = []; + while (1) { + $macro = (string) get_parameter('field'.$i.'_macro'); + if ($macro == '') { + break; + } + + $desc = (string) get_parameter('field'.$i.'_desc'); + $help = (string) get_parameter('field'.$i.'_help'); + $value = (string) get_parameter('field'.$i.'_value'); + $hide = get_parameter('field'.$i.'_hide'); + + $macros[$i]['macro'] = $macro; + $macros[$i]['desc'] = $desc; + $macros[$i]['help'] = $help; + $macros[$i]['value'] = $value; + $macros[$i]['hide'] = $hide; + $i++; + } + + $macros = io_json_mb_encode($macros); + + $sql_update = sprintf( + "UPDATE trecon_script SET + name = '%s', + description = '%s', + script = '%s', + macros = '%s' + WHERE id_recon_script = %d", + $reconscript_name, + $reconscript_description, + $reconscript_script, + $macros, + $id_script + ); + + $result_upd = false; + if ($reconscript_name !== '' && $reconscript_script !== '') { + $result_upd = db_process_sql($sql_update); + if (!$result_upd) { + $result = [ + 'error' => 1, + 'msg' => __('Problem updating'), + ]; + } else { + $result = [ + 'error' => 0, + 'msg' => __('Updated successfully'), + ]; + } + } else { + $result = [ + 'error' => 1, + 'msg' => __('Name or Script fullpath they can not be empty'), + ]; + } + + return $result; + } + + + /** + * Delete net scan script. + * + * @param integer $id_script Id script. + * + * @return array Check msg successfully or problem + */ + private function deleteScanScripts(int $id_script) + { + $result = []; + if (isset($id_script) === false || $id_script === 0) { + $result = [ + 'error' => 1, + 'msg' => __('Problem deleting Net scan Scripts, Not selected script'), + ]; + + return $result; + } + + $result_dlt = db_process_sql_delete( + 'trecon_script', + ['id_recon_script' => $id_script] + ); + + if (!$result_dlt) { + $result = [ + 'error' => 1, + 'msg' => __('Problem deleting Net scan Scripts'), + ]; + } else { + $result_dlt2 = db_process_sql_delete( + 'trecon_task', + ['id_recon_script' => $id_script] + ); + + if (!$result_dlt2) { + $result = [ + 'error' => 1, + 'msg' => __('Problem deleting Net scan Scripts'), + ]; + } else { + $result = [ + 'error' => 0, + 'msg' => __('Deleted successfully'), + ]; + } + } + + return $result; + + } + + + /** + * Print list Net scan scripts and messages operations. + * + * @param array $msg Print msg if necessary. + * + * @return void + */ + private function printListNetScanScripts(array $msg) + { + if (count($msg) > 0) { + if ($msg['error'] === 1) { + ui_print_error_message($msg['msg']); + } else { + ui_print_success_message($msg['msg']); + } + } + + $url = 'index.php?sec=gservers&sec2=godmode/servers/discovery'; + $url .= '&wiz=hd&mode=managenetscanscripts'; + + // List available Net scan scripts. + $rows = db_get_all_rows_in_table('trecon_script'); + + if ($rows !== false) { + echo ''; + echo ''; + echo ''; + echo ''; + $color = 0; + foreach ($rows as $row) { + if ($color == 1) { + $tdcolor = 'datos'; + $color = 0; + } else { + $tdcolor = 'datos2'; + $color = 1; + } + + echo ''; + echo "'; + echo "'; + } + + echo '
'.__('Name').''.__('Description').''.__('Delete').'
"; + echo ''; + echo $row['name']; + echo '"; + $desc = io_safe_output( + $row['description'] + ); + + $desc = str_replace( + "\n", + '
', + $desc + ); + + echo $desc.'

'; + echo ''.__('Command').': '.$row['script'].''; + echo "
"; + // Delete. + echo '
'; + echo html_print_input_hidden('page', 0, true); + echo html_print_input_hidden( + 'operation_scp', + 'delete_scp', + true + ); + echo html_print_input_hidden( + 'id_script', + $row['id_recon_script'], + true + ); + echo html_print_input_image( + 'delete', + 'images/cross.png', + 1, + '', + true, + [ + 'title' => __('Delete Script'), + ] + ); + echo '
'; + echo '
'; + + echo "
"; + echo html_print_input_hidden('page', 1, true); + echo ""; + echo '
'; + } else { + ui_print_info_message( + [ + 'no_close' => true, + 'message' => __( + 'There are no net scan scripts in the system' + ), + ] + ); + } + } + + + /** + * Print form net scan scripts. + * + * @param integer $id_script Id script. + * + * @return void + */ + private function printFormScanScripts(int $id_script) + { + // Initialize vars. + if ($id_script !== 0) { + $form_id = $id_script; + $reconscript = db_get_row( + 'trecon_script', + 'id_recon_script', + $form_id + ); + $form_name = $reconscript['name']; + $form_description = $reconscript['description']; + $form_script = $reconscript['script']; + $macros = $reconscript['macros']; + } else { + $form_name = ''; + $form_description = ''; + $form_script = ''; + $macros = ''; + } + + $url = 'index.php?sec=gservers&sec2=godmode/servers/discovery'; + $url .= '&wiz=hd&mode=managenetscanscripts'; + + if ($id_script !== 0) { + echo '
'; + echo html_print_input_hidden('page', 0, true); + echo html_print_input_hidden( + 'operation_scp', + 'update_scp', + true + ); + } else { + echo ''; + echo html_print_input_hidden('page', 0, true); + echo html_print_input_hidden( + 'operation_scp', + 'create_scp', + true + ); + } + + $table = new stdClass(); + $table->width = '100%'; + $table->id = 'table-form'; + $table->class = 'databox filters'; + $table->style = []; + $table->style[0] = 'font-weight: bold'; + $table->style[2] = 'font-weight: bold'; + $table->data = []; + + $data = []; + $data[0] = __('Name'); + $data[0] .= ui_print_help_icon( + 'reconscript_definition', + true, + '', + 'images/help_w.png' + ); + + $data[1] = ''; + $table->data['recon_name'] = $data; + $table->colspan['recon_name'][1] = 3; + + $data = []; + $data[0] = __('Script fullpath'); + $data[1] = ''; + $table->data['recon_fullpath'] = $data; + $table->colspan['recon_fullpath'][1] = 3; + + $data = []; + $data[0] = __('Description'); + $data[1] = ''; + $table->data['recon_description'] = $data; + $table->colspan['recon_description'][1] = 3; + + $macros = json_decode($macros, true); + + // This code is ready to add locked feature as plugins. + $locked = false; + + // The next row number is recon_3. + $next_name_number = 3; + $i = 1; + while (1) { + // Always print at least one macro. + if ((!isset($macros[$i]) || $macros[$i]['desc'] == '') && $i > 1) { + break; + } + + $macro_desc_name = 'field'.$i.'_desc'; + $macro_desc_value = ''; + $macro_help_name = 'field'.$i.'_help'; + $macro_help_value = ''; + $macro_value_name = 'field'.$i.'_value'; + $macro_value_value = ''; + $macro_name_name = 'field'.$i.'_macro'; + $macro_name = '_field'.$i.'_'; + $macro_hide_value_name = 'field'.$i.'_hide'; + $macro_hide_value_value = 0; + + if (isset($macros[$i]['desc'])) { + $macro_desc_value = $macros[$i]['desc']; + } + + if (isset($macros[$i]['help'])) { + $macro_help_value = $macros[$i]['help']; + } + + if (isset($macros[$i]['value'])) { + $macro_value_value = $macros[$i]['value']; + } + + if (isset($macros[$i]['hide'])) { + $macro_hide_value_value = $macros[$i]['hide']; + } + + $datam = []; + $datam[0] = __('Description'); + $datam[0] .= " ( "; + $datam[0] .= $macro_name; + $datam[0] .= ' )'; + $datam[0] .= html_print_input_hidden( + $macro_name_name, + $macro_name, + true + ); + $datam[1] = html_print_input_text_extended( + $macro_desc_name, + $macro_desc_value, + 'text-'.$macro_desc_name, + '', + 30, + 255, + $locked, + '', + "class='command_advanced_conf'", + true + ); + if ($locked) { + $datam[1] .= html_print_image( + 'images/lock.png', + true, + ['class' => 'command_advanced_conf'] + ); + } + + $datam[2] = __('Default value'); + $datam[2] .= " ( "; + $datam[2] .= $macro_name; + $datam[2] .= ' ) '; + $datam[3] = html_print_input_text_extended( + $macro_value_name, + $macro_value_value, + 'text-'.$macro_value_name, + '', + 30, + 255, + $locked, + '', + "class='command_component command_advanced_conf'", + true + ); + if ($locked) { + $datam[3] .= html_print_image( + 'images/lock.png', + true, + ['class' => 'command_advanced_conf'] + ); + } + + $table->data['recon_'.$next_name_number] = $datam; + + $next_name_number++; + + $table->colspan['recon_'.$next_name_number][1] = 3; + + $datam = []; + $datam[0] = __('Hide value'); + $datam[0] .= ui_print_help_tip( + __('This field will show up as dots like a password'), + true + ); + + $datam[1] = html_print_checkbox_extended( + $macro_hide_value_name, + 1, + $macro_hide_value_value, + 0, + '', + ['class' => 'command_advanced_conf'], + true, + 'checkbox-'.$macro_hide_value_name + ); + + $table->data['recon_'.$next_name_number] = $datam; + $next_name_number++; + + $table->colspan['recon_'.$next_name_number][1] = 3; + + $datam = []; + $datam[0] = __('Help'); + $datam[0] .= " ( "; + $datam[0] .= $macro_name; + $datam[0] .= ' )


'; + + $tadisabled = ($locked === true) ? ' disabled' : ''; + + $datam[1] = html_print_textarea( + $macro_help_name, + 6, + 100, + $macro_help_value, + 'class="command_advanced_conf" style="width: 97%;"'.$tadisabled, + true + ); + + if ($locked) { + $datam[1] .= html_print_image( + 'images/lock.png', + true, + ['class' => 'command_advanced_conf'] + ); + } + + $datam[1] .= '


'; + + $table->data['recon_'.$next_name_number] = $datam; + $next_name_number++; + $i++; + } + + if (!$locked) { + $datam = []; + $datam[0] = ''; + $datam[0] .= __('Add macro').''; + $datam[0] .= ''; + $datam[0] .= html_print_image( + 'images/add.png', + true + ); + $datam[0] .= ''; + $datam[0] .= ''; + $datam[0] .= ''; + + $delete_macro_style = ''; + if ($i <= 2) { + $delete_macro_style = 'display:none;'; + } + + $datam[2] = '
'; + $datam[2] .= __('Delete macro'); + $datam[2] .= ''; + $datam[2] .= html_print_image( + 'images/delete.png', + true + ); + $datam[2] .= '
'; + + $table->colspan['recon_action'][0] = 2; + $table->rowstyle['recon_action'] = 'text-align:center'; + $table->colspan['recon_action'][2] = 2; + $table->data['recon_action'] = $datam; + } + + html_print_table($table); + + echo ''; + echo '
'; + + if ($id_script === 0) { + echo ""; + } else { + echo ""; + } + + echo '
'; + + ui_require_javascript_file('pandora_modules'); + } + + +}