'noaccess']);
}
include 'general/noaccess.php';
exit;
}
// Set baseUrl for use it in several locations in this class.
$this->baseUrl = ui_get_full_url('index.php?sec=gmodules&sec2=godmode/modules/manage_module_templates');
// Capture all parameters before start.
$this->id_np = get_parameter('id_np', -1);
$this->action = get_parameter('action', '');
// Profile exists. Set the attributes with the info.
if ($this->id_np > 0 || empty($this->action)) {
$this->setNetworkProfile();
}
$this->offset = (int) get_parameter('offset', 0);
$this->ajaxController = $ajax_controller;
// Get all of PENs valid for autocomplete.
$getPENs = db_get_all_rows_sql('SELECT pen,manufacturer FROM tpen');
$outputPENs = [];
$this->validPen = '';
$this->penRefs = [];
foreach ($getPENs as $pen) {
$this->validPen .= ((int) $pen['pen']).',';
$this->penRefs[] = [
'label' => io_safe_output($pen['manufacturer']),
'value' => $pen['pen'],
];
// Reverse autocompletion.
$this->penRefs[] = [
'label' => $pen['pen'],
'value' => $pen['pen'],
];
}
chop($this->validPen);
return $this;
}
/**
* Run main page.
*
* @return void
*/
public function run()
{
// CSS.
ui_require_css_file('wizard');
ui_require_css_file('discovery');
// Javascript.
ui_require_javascript_file('jquery.caret.min');
// Breadcrums.
$this->setBreadcrum([]);
if ($this->id_np > 0) {
// Add a breadcrumb with the current template.
$urlToGo = $this->baseUrl.'&id_np='.$this->id_np;
$this->prepareBreadcrum(
[
['label' => __('Configuration')],
['label' => __('Templates')],
[
'link' => $this->baseUrl,
'label' => __('Module template management'),
'selected' => false,
],
[
'link' => $urlToGo,
'label' => $this->name,
'selected' => true,
],
],
true
);
} else {
$this->prepareBreadcrum(
[
['label' => __('Configuration')],
['label' => __('Templates')],
[
'link' => $this->baseUrl,
'label' => __('Module template management'),
'selected' => true,
],
],
true
);
}
// Prints the header.
ui_print_page_header(
__('Module template management'),
'',
false,
'',
true,
'',
false,
'',
GENERIC_SIZE_TEXT,
'',
$this->printHeader(true)
);
// Process the data if action is required.
if (!empty($this->action)) {
$this->processData();
}
if ($this->id_np === -1) {
// List all Module Blocks.
$this->moduleTemplateList();
} else {
// Show form for create or update template.
$this->moduleTemplateForm();
}
$this->loadJS();
}
/**
* Minor function to dump json message as ajax response.
*
* @param string $type Type: result || error.
* @param string $msg Message.
*
* @return void
*/
private function ajaxMsg($type, $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;
}
/**
* Save or Update the data received.
*
* @return void
*/
public function processData()
{
// Only needed if process data.
$modulesToAdd = get_parameter('add-modules-components', '');
// Evaluate the modules allowed.
if (!empty($this->action)) {
// Success variable.
$success = false;
$this->name = io_safe_input(strip_tags(io_safe_output((string) get_parameter('name'))));
$this->description = io_safe_input(strip_tags(io_safe_output((string) get_parameter('description'))));
$this->pen = get_parameter('pen', '');
switch ($this->action) {
case 'update':
if (empty($this->name)) {
$dbResult_tnp = false;
} else {
$dbResult_tnp = db_process_sql_update(
'tnetwork_profile',
[
'name' => $this->name,
'description' => $this->description,
],
['id_np' => $this->id_np]
);
}
if ($dbResult_tnp === false) {
$success = false;
} else {
db_process_sql_delete('tnetwork_profile_pen', ['id_np' => $this->id_np]);
if (empty($this->pen)) {
$success = true;
} else {
$pensList = explode(',', $this->pen);
// Set again the new PENs associated.
foreach ($pensList as $currentPen) {
$dbResult_pen = db_process_sql_insert(
'tnetwork_profile_pen',
[
'pen' => $currentPen,
'id_np' => $this->id_np,
]
);
if ($dbResult_pen === false) {
$success = false;
break;
}
$success = true;
}
}
}
if ($success === true) {
$msg = __('Template %s successfully updated', $this->name);
} else {
$msg = __('Error updating template');
}
break;
case 'create':
if (empty($this->name)) {
$dbResult_tnp = false;
} else {
$dbResult_tnp = db_process_sql_insert(
'tnetwork_profile',
[
'name' => $this->name,
'description' => $this->description,
]
);
}
// The insert gone fine!
if ($dbResult_tnp != false) {
// Set the new id_np.
$this->id_np = $dbResult_tnp;
if (empty($this->pen)) {
$success = true;
} else {
$pensList = explode(',', $this->pen);
// Insert all of new PENs associated with this id_np.
foreach ($pensList as $currentPen) {
$dbResult_pen = db_process_sql_insert(
'tnetwork_profile_pen',
[
'pen' => $currentPen,
'id_np' => $this->id_np,
]
);
// If something is wrong, is better stop.
if ($dbResult_pen === false) {
break;
}
$success = true;
}
}
}
if ($success === true) {
$msg = __('Template %s successfully created', $this->name);
} else {
$msg = __('Error creating template');
}
break;
case 'delete':
$success = db_process_sql_delete('tnetwork_profile', ['id_np' => $this->id_np]);
if ($success != false) {
$msg = __('Template %s successfully deleted', $this->name);
} else {
$msg = __('Error deleting %s template', $this->name);
}
// Reset id_np for show the templates list.
$this->id_np = -1;
break;
case 'export':
global $config;
enterprise_include_once('include/functions_reporting_csv.php');
$id_network_profile = safe_int($this->id_np);
if (empty($id_network_profile)) {
return false;
}
$filter['id_np'] = $id_network_profile;
$profile_info = @db_get_row_filter('tnetwork_profile', $filter, false);
if (empty($profile_info)) {
$success = false;
// ui_print_error_message(__('This template does not exist'));
return;
}
// It's important to keep the structure and order in the same way for backwards compatibility.
switch ($config['dbtype']) {
case 'mysql':
$sql = sprintf(
'
SELECT components.name, components.description, components.type, components.max, components.min, components.module_interval,
components.tcp_port, components.tcp_send, components.tcp_rcv, components.snmp_community, components.snmp_oid,
components.id_module_group, components.id_modulo, components.plugin_user, components.plugin_pass, components.plugin_parameter,
components.max_timeout, components.max_retries, components.history_data, components.min_warning, components.max_warning, components.str_warning, components.min_critical,
components.max_critical, components.str_critical, components.min_ff_event, components.dynamic_interval, components.dynamic_max, components.dynamic_min, components.dynamic_two_tailed, comp_group.name AS group_name, components.critical_instructions, components.warning_instructions, components.unknown_instructions
FROM `tnetwork_component` AS components, tnetwork_profile_component AS tpc, tnetwork_component_group AS comp_group
WHERE tpc.id_nc = components.id_nc
AND components.id_group = comp_group.id_sg
AND tpc.id_np = %d',
$this->id_np
);
break;
case 'postgresql':
$sql = sprintf(
'
SELECT components.name, components.description, components.type, components.max, components.min, components.module_interval,
components.tcp_port, components.tcp_send, components.tcp_rcv, components.snmp_community, components.snmp_oid,
components.id_module_group, components.id_modulo, components.plugin_user, components.plugin_pass, components.plugin_parameter,
components.max_timeout, components.max_retries, components.history_data, components.min_warning, components.max_warning, components.str_warning, components.min_critical,
components.max_critical, components.str_critical, components.min_ff_event, comp_group.name AS group_name, components.critical_instructions, components.warning_instructions, components.unknown_instructions
FROM "tnetwork_component" AS components, tnetwork_profile_component AS tpc, tnetwork_component_group AS comp_group
WHERE tpc.id_nc = components.id_nc
AND components.id_group = comp_group.id_sg
AND tpc.id_np = %d',
$this->id_np
);
break;
case 'oracle':
$sql = sprintf(
'
SELECT components.name, components.description, components.type, components.max, components.min, components.module_interval,
components.tcp_port, components.tcp_send, components.tcp_rcv, components.snmp_community, components.snmp_oid,
components.id_module_group, components.id_modulo, components.plugin_user, components.plugin_pass, components.plugin_parameter,
components.max_timeout, components.max_retries, components.history_data, components.min_warning, components.max_warning, components.str_warning, components.min_critical,
components.max_critical, components.str_critical, components.min_ff_event, comp_group.name AS group_name, components.critical_instructions, components.warning_instructions, components.unknown_instructions
FROM tnetwork_component AS components, tnetwork_profile_component AS tpc, tnetwork_component_group AS comp_group
WHERE tpc.id_nc = components.id_nc
AND components.id_group = comp_group.id_sg
AND tpc.id_np = %d',
$this->id_np
);
break;
}
$components = db_get_all_rows_sql($sql);
$row_names = [];
$inv_names = [];
// Find the names of the rows that we are getting and throw away the duplicate numeric keys
foreach ($components[0] as $row_name => $detail) {
if (is_numeric($row_name)) {
$inv_names[] = $row_name;
} else {
$row_names[] = $row_name;
}
}
$fileName = io_safe_output($profile_info['name']);
// Send headers to tell the browser we're sending a file
header('Content-type: application/octet-stream');
header('Content-Disposition: attachment; filename='.preg_replace('/\s/', '_', $fileName).'.csv');
header('Pragma: no-cache');
header('Expires: 0');
// Clean up output buffering
while (@ob_end_clean()) {
}
// Then print the first line (row names)
echo '"'.implode('"'.$config['csv_divider'].'"', $row_names).'"';
echo "\n";
// Then print the rest of the data. Encapsulate in quotes in case we have comma's in any of the descriptions
foreach ($components as $row) {
foreach ($inv_names as $bad_key) {
unset($row[$bad_key]);
}
if ($config['csv_decimal_separator'] !== '.') {
foreach ($row as $name => $data) {
if (is_numeric($data)) {
// Get the number of decimals, if > 0, format dec comma.
$decimals = strlen(substr(strrchr($data, '.'), 1));
if ($decimals !== 0) {
$row[$name] = csv_format_numeric((float) $data, $decimals, true);
}
}
}
}
echo '"'.implode('"'.$config['csv_divider'].'"', $row).'"';
echo "\n";
}
// We're done here. The original page will still be there
exit;
break;
default:
// There is possible want do an action detailed.
$action_detailed = explode('_', $this->action);
// Action deletion.
if ($action_detailed[0] === 'del') {
// Block or Module is affected.
switch ($action_detailed[1]) {
case 'module':
$success = db_process_sql_delete(
'tnetwork_profile_component',
'id_nc='.$action_detailed[2].' AND id_np='.$this->id_np
);
if ($success != false) {
$msg = __('Module successfully deleted');
} else {
$msg = __('Error deleting module');
}
break;
case 'block':
$success = db_process_sql_delete(
'tnetwork_profile_component',
'id_nc in ('.$action_detailed[2].') AND id_np='.$this->id_np
);
if ($success != false) {
$msg = __('Block successfully deleted');
} else {
$msg = __('Error deleting block');
}
break;
case 'template':
if ($action_detailed[2] === 'all') {
$success = db_process_sql_delete(
'tnetwork_profile',
['1' => 1]
);
if ($success != false) {
$msg = __('All templates deleted');
} else {
$msg = __('Error deleting all templates');
}
} else {
$success = db_process_sql_delete(
'tnetwork_profile',
'id_np in ('.$action_detailed[2].')'
);
if ($success != false) {
$msg = __('Selected templates deleted');
} else {
$msg = __('Error deleting selected templates');
}
}
$this->id_np = -1;
break;
default:
// Do nothing.
break;
}
} else {
$msg = __('Something gone wrong. Please, try again');
}
break;
}
if ($success === false) {
ui_print_error_message($msg);
} else {
ui_print_success_message($msg);
}
} else if ($modulesToAdd != '') {
$modulesToAddList = explode(',', $modulesToAdd);
$modulesAddedList = db_get_all_rows_in_table('tnetwork_profile_component');
$modulesToAdd = [];
foreach ($modulesToAddList as $module) {
$is_added = false;
foreach ($modulesAddedList as $item) {
if ($item['id_nc'] === $module
&& $item['id_np'] === $this->id_np
) {
$is_added = true;
}
}
if ($is_added === false) {
$name = io_safe_output(
db_get_row_filter(
'tnetwork_component',
['id_nc' => $module],
'name'
)
);
$modulesToAdd[] = $name;
db_process_sql_insert(
'tnetwork_profile_component',
[
'id_nc' => $module,
'id_np' => $this->id_np,
]
);
} else {
$message = 'Some modules already exists
';
}
}
if (empty($modulesToAdd)) {
$this->ajaxMsg(
'error',
__('The modules is already added')
);
return false;
}
if ($message === '') {
$message = 'Following modules will be added:';
} else {
$message .= 'Following modules will be added:';
}
$message .= '