';print_r($_FILES);print '';exit;
if (($_FILES['import_file']['tmp_name'] != 'none') && ($_FILES['import_file']['tmp_name'] != '')) {
// file upload
$fp = fopen($_FILES['import_file']['tmp_name'],'r');
$xml_data = fread($fp,filesize($_FILES['import_file']['tmp_name']));
is_resource($fp) && fclose($fp);
} else {
header('Location: templates_import.php');
exit;
}
if (gfrv('import_data_source_profile') == '0') {
$import_as_new = true;
$profile_id = db_fetch_cell('SELECT id FROM data_source_profiles ORDER BY `default` DESC LIMIT 1');
} else {
$import_as_new = false;
$profile_id = grv('import_data_source_profile');
}
if (gnrv('preview_only') == 'true') {
$preview_only = true;
} else {
$preview_only = false;
}
if (isrv('remove_orphans') && gnrv('remove_orphans') == 'on') {
$remove_orphans = true;
} else {
$remove_orphans = false;
}
if (isrv('replace_svalues') && gnrv('replace_svalues') == 'on') {
$replace_svalues = true;
} else {
$replace_svalues = false;
}
$import_hashes = [];
// loop through each of the graphs selected on the previous page and get more info about them
foreach ($_POST as $var => $val) {
if (str_contains($var, 'chk_')) {
$id = base64_decode(str_replace('chk_', '', $var), true);
$id = json_decode((string) $id, true);
if (isset($id['hash'])) {
$import_hashes[] = $id['hash'];
}
}
}
// obtain debug information if it's set
$debug_data = import_xml_data($xml_data, $import_as_new, $profile_id, $remove_orphans, $replace_svalues, $import_hashes);
if (!$preview_only) {
raise_message('import_success', __('The Template Import Succeeded.'), MESSAGE_LEVEL_INFO);
header('Location: templates_import.php');
} elseif ($debug_data !== false && cacti_sizeof($debug_data)) {
// print '
';print_r($debug_data);print '
';exit;
$templates = prepare_template_display($debug_data);
// print '';print_r($templates);print '
';
display_template_data($templates);
exit;
} else {
cacti_log(sprintf('ERROR: Import or Preview failed for XML file %s!', $_FILES['import_file']['name']), false, 'IMPORT');
$message_text = '';
/**
* The variables $import_messages is populated as a global variable
* set in the function import_xml_data(). It includes all the
* errors encountered during the import pre-processing.
*/
if (cacti_sizeof($import_messages)) {
foreach ($import_messages as $message) {
if (isset($messages[$message])) {
$message_text .= ($message_text != '' ? '
' : '') . $messages[$message]['message'];
}
}
}
raise_message_javascript(__('Error in Template', 'package'), __('The Template XML file "%s" validation failed', $_FILES['import_file']['name']), __('See the cacti.log for more information, and review the XML file for proper syntax. The error details are shown below.
Errors:
%s', $message_text));
}
}
}
function prepare_template_display(array &$import_info) : array {
global $hash_type_names;
$templates = [];
/**
* This function will create an array of item types and their status
* the user will have an option to import select items based upon
* these values.
*
* $templates['template_hash'] = array(
* 'package' => 'some_package_name',
* 'package_file' => 'some_package_filename',
* 'type' => 'some_type',
* 'type_name' => 'some_type_name',
* 'name' => 'some_name',
* 'status' => 'some_status'
* );
*/
if (cacti_sizeof($import_info)) {
foreach ($import_info as $type => $type_array) {
if ($type == 'files') {
$templates['files'] = $type_array;
continue;
}
foreach ($type_array as $index => $vals) {
$hash = $vals['hash'];
if (!isset($templates[$hash])) {
$templates[$hash]['status'] = $vals['type'];
} else {
$templates[$hash]['status'] .= '
' . $vals['type'];
}
$templates[$hash]['type'] = $type;
$templates[$hash]['type_name'] = $hash_type_names[$type];
$templates[$hash]['name'] = $vals['title'];
unset($vals['title']);
unset($vals['result']);
unset($vals['hash']);
unset($vals['type']);
if (isset($vals['dep'])) {
$template[$hash]['deps'] = $vals['dep'];
}
if (cacti_sizeof($vals)) {
$templates[$hash]['vals'] = $vals;
}
}
}
}
return $templates;
}
function display_template_data(array &$templates) : void {
if (isset($templates['files'])) {
$files = $templates['files'];
unset($templates['files']);
html_start_box(__('Import Files [ If Files are missing, locate and install before using ]'), '100%', false, 3, 'center', '');
$display_text = [
[
'display' => __('File Name')
],
[
'display' => __('Status')
]
];
html_header($display_text);
$id = 0;
foreach ($files as $path => $status) {
if ($status == 'found') {
$status = "" . __('Exists') . '';
} elseif ($status == 'notreadable') {
$status = "" . __('Not Readable') . '';
} else {
$status = "" . __('Not Found') . '';
}
form_alternate_row('line_' . $id);
form_selectable_ecell($path, $id);
form_selectable_cell($status, $id);
form_end_row();
}
html_end_box();
}
if (cacti_sizeof($templates)) {
html_start_box(__('Import Templates [ None selected imports all, Check to import selectively ]'), '100%', false, 3, 'center', '');
$display_text = [
[
'display' => __('Template Type')
],
[
'display' => __('Template Name')
],
[
'display' => __('Status')
],
[
'display' => __('Dependencies')
],
[
'display' => __('Changes/Diffferences')
]
];
html_header_checkbox($display_text, false, '', true, 'import');
$templates = array_reverse($templates);
foreach ($templates as $hash => $detail) {
$id = base64_encode(
json_encode(
[
'hash' => $hash,
'type' => $detail['type_name'],
'name' => $detail['name'],
'status' => $detail['status']
]
)
);
if ($detail['status'] == 'updated') {
$status = "" . __('Updated') . '';
} elseif ($detail['status'] == 'new') {
$status = "" . __('New') . '';
} elseif ($detail['status'] == 'damaged') {
$status = "" . __('Damaged') . '';
} else {
$status = "" . __('Unchanged') . '';
}
form_alternate_row('line_import_' . $detail['status'] . '_' . $id);
form_selectable_ecell($detail['type_name'], $id);
form_selectable_ecell($detail['name'], $id);
form_selectable_cell($status, $id);
if (isset($detail['deps'])) {
$dep_details = [];
$unmet_count = 0;
$met_count = 0;
foreach ($detail['deps'] as $hash => $dep) {
if ($dep == 'met') {
$dep_details[$dep] = $dep;
$met_count++;
} else {
$dep_details[$dep] = $dep;
$unmet_count++;
}
}
if (isset($dep_details['met'])) {
$dep_details['met'] = __('Met: %d', $met_count);
}
if (isset($dep_details['unmet'])) {
$dep_details['unmet'] = __('Unmet: %d', $unmet_count);
}
form_selectable_cell(implode(', ', $dep_details), $id, '', 'white-space:pre-wrap');
} else {
form_selectable_cell(__('None'), $id);
}
if ($detail['status'] == 'damaged') {
form_selectable_cell(__('Some CDEF Items will not import due to an export error! Contact Template provider for an updated export.'), $id);
} elseif (isset($detail['vals'])) {
$diff_details = '';
$diff_array = [];
$orphan_array = [];
foreach ($detail['vals'] as $type => $diffs) {
if ($type == 'differences') {
foreach ($diffs as $item) {
$diff_array[$item] = $item;
}
} elseif ($type == 'orphans') {
foreach ($diffs as $item) {
$orphan_array[$item] = $item;
}
}
}
if (cacti_sizeof($diff_array)) {
$diff_details .= __('Differences', 'package') . '
' . implode('
', $diff_array);
}
if (cacti_sizeof($orphan_array)) {
$diff_details .= ($diff_details != '' ? '
' : '') . __('Orphans', 'package') . '
' . implode('
', $orphan_array);
}
form_selectable_cell($diff_details, $id, '', 'white-space:pre-wrap');
} else {
form_selectable_cell(__('None'), $id);
}
form_checkbox_cell($detail['name'], $id);
form_end_row();
}
html_end_box();
}
}
function bad_tmp() : void {
html_start_box(__('Import Template'), '60%', false, 3, 'center', '');
form_alternate_row();
print "" . __('ERROR') . ': ' . __('Failed to access temporary folder, import functionality is disabled') . " | \n";
html_end_box();
}
function import() : void {
global $hash_type_names, $fields_template_import;
$default_profile = db_fetch_cell('SELECT id FROM data_source_profiles WHERE `default`="on"');
if (empty($default_profile)) {
$default_profile = db_fetch_cell('SELECT id FROM data_source_profiles ORDER BY id LIMIT 1');
}
form_start('templates_import.php', 'import', true);
// ================= input validation and session storage =================
$filters = [
'preview_only' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '(true|false)']],
'default' => 'on'
],
'replace_svalues' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '(true|false)']],
'default' => read_config_option('replace_svalues')
],
'remove_orphans' => [
'filter' => FILTER_VALIDATE_REGEXP,
'options' => ['options' => ['regexp' => '(true|false)']],
'default' => read_config_option('remove_orphans')
],
'data_source_profile' => [
'filter' => FILTER_VALIDATE_INT,
'default' => $default_profile
],
'image_format' => [
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('default_image_format')
],
'graph_width' => [
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('default_graph_width')
],
'graph_height' => [
'filter' => FILTER_VALIDATE_INT,
'default' => read_config_option('default_graph_height')
],
];
validate_store_request_vars($filters, 'sess_pimport');
// ================= input validation =================
$fields_template_import['import_data_source_profile']['default'] = $default_profile;
if (isrv('replace_svalues') && gnrv('replace_svalues') == 'true') {
$fields_template_import['replace_svalues']['value'] = 'on';
} else {
$fields_template_import['replace_svalues']['value'] = '';
}
if (isrv('remove_orphans') && gnrv('remove_orphans') == 'true') {
$fields_template_import['remove_orphans']['value'] = 'on';
} else {
$fields_template_import['remove_orphans']['value'] = '';
}
if (isrv('image_format')) {
$fields_template_import['image_format']['value'] = gfrv('image_format');
} else {
$fields_template_import['image_format']['value'] = read_config_option('default_image_format');
}
if (isrv('graph_width')) {
$fields_template_import['graph_width']['value'] = gfrv('graph_width');
} else {
$fields_template_import['graph_width']['value'] = read_config_option('default_graph_width');
}
if (isrv('graph_height')) {
$fields_template_import['graph_height']['value'] = gfrv('graph_height');
} else {
$fields_template_import['graph_height']['value'] = read_config_option('default_graph_height');
}
html_start_box(__('Import Template'), '100%', true, 3, 'center', '');
draw_edit_form(
[
'config' => ['no_form_tag' => true],
'fields' => $fields_template_import
]
);
html_end_box(true, true);
print '';
form_hidden_box('save_component_import','1','');
form_save_button('', 'import', 'import', false);
?>