4 Star 8 Fork 1

Gitee 极速下载/Cacti

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/Cacti/cacti
克隆/下载
templates_import.php 15.50 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
<?php
/*
+-------------------------------------------------------------------------+
| Copyright (C) 2004-2025 The Cacti Group |
| |
| This program is free software; you can redistribute it and/or |
| modify it under the terms of the GNU General Public License |
| as published by the Free Software Foundation; either version 2 |
| of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
+-------------------------------------------------------------------------+
| Cacti: The Complete RRDtool-based Graphing Solution |
+-------------------------------------------------------------------------+
| This code is designed, written, and maintained by the Cacti Group. See |
| about.php and/or the AUTHORS file for specific developer information. |
+-------------------------------------------------------------------------+
| http://www.cacti.net/ |
+-------------------------------------------------------------------------+
*/
require('./include/auth.php');
require_once(CACTI_PATH_LIBRARY . '/import.php');
require_once(CACTI_PATH_LIBRARY . '/poller.php');
require_once(CACTI_PATH_LIBRARY . '/template.php');
require_once(CACTI_PATH_LIBRARY . '/utility.php');
/* set default action */
set_default_action();
$action = get_request_var('action');
$is_save = isset_request_var('save_component_import');
$tmp_dir = sys_get_temp_dir();
$tmp_len = strlen($tmp_dir);
$tmp_dir .= ($tmp_len !== 0 && substr($tmp_dir, -$tmp_len) === '/') ? '': '/';
$is_tmp = is_tmp_writable();
if ($is_tmp && $is_save && $action == 'save') {
form_save();
} else {
top_header();
if ($is_tmp) {
import();
} else {
bad_tmp();
}
bottom_footer();
}
function form_save() {
global $preview_only, $messages, $import_messages;
if (isset_request_var('save_component_import')) {
//print '<pre>';print_r($_FILES);print '</pre>';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']));
fclose($fp);
} else {
header('Location: templates_import.php');
exit;
}
if (get_filter_request_var('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 = get_request_var('import_data_source_profile');
}
if (get_nfilter_request_var('preview_only') == 'true') {
$preview_only = true;
} else {
$preview_only = false;
}
if (isset_request_var('remove_orphans') && get_nfilter_request_var('remove_orphans') == 'on') {
$remove_orphans = true;
} else {
$remove_orphans = false;
}
if (isset_request_var('replace_svalues') && get_nfilter_request_var('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 (strpos($var, 'chk_') !== false) {
$id = base64_decode(str_replace('chk_', '', $var), true);
$id = json_decode($id, true);
if (isset($id['hash'])) {
$import_hashes[] = $id['hash'];
}
}
}
$import_messages = [];
/* 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 '<pre>';print_r($debug_data);print '</pre>';exit;
$templates = prepare_template_display($debug_data);
//print '<pre>';print_r($templates);print '</pre>';
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 = '';
if (cacti_sizeof($import_messages)) {
foreach ($import_messages as $message) {
if (isset($messages[$message])) {
$message_text .= ($message_text != '' ? '<br>':'') . $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.<br><br><b>Errors:</b><br>%s', $message_text));
}
}
}
function prepare_template_display(&$import_info) {
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'] .= '<br>' . $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(&$templates) {
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 = "<span class='deviceUp'>" . __('Exists') . '</span>';
} elseif ($status == 'notreadable') {
$status = "<span class='deviceRecovering'>" . __('Not Readable') . '</span>';
} else {
$status = "<span class='deviceDown'>" . __('Not Found') . '</span>';
}
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 = "<span class='updateObject'>" . __('Updated') . '</span>';
} elseif ($detail['status'] == 'new') {
$status = "<span class='newObject'>" . __('New') . '</span>';
} elseif ($detail['status'] == 'damaged') {
$status = "<span class='deviceDown'>" . __('Damaged') . '</span>';
} else {
$status = "<span class='deviceUp'>" . __('Unchanged') . '</span>';
}
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') . '<br>' . implode('<br>', $diff_array);
}
if (cacti_sizeof($orphan_array)) {
$diff_details .= ($diff_details != '' ? '<br>':'') . __('Orphans', 'package') . '<br>' . implode('<br>', $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() {
html_start_box(__('Import Template'), '60%', false, 3, 'center', '');
form_alternate_row();
print "<td class='textarea'><p><strong>" . __('ERROR') . ':</strong> ' .__('Failed to access temporary folder, import functionality is disabled') . "</p></td></tr>\n";
html_end_box();
}
function import() {
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 (isset_request_var('replace_svalues') && get_nfilter_request_var('replace_svalues') == 'true') {
$fields_template_import['replace_svalues']['value'] = 'on';
} else {
$fields_template_import['replace_svalues']['value'] = '';
}
if (isset_request_var('remove_orphans') && get_nfilter_request_var('remove_orphans') == 'true') {
$fields_template_import['remove_orphans']['value'] = 'on';
} else {
$fields_template_import['remove_orphans']['value'] = '';
}
if (isset_request_var('image_format')) {
$fields_template_import['image_format']['value'] = get_filter_request_var('image_format');
} else {
$fields_template_import['image_format']['value'] = read_config_option('default_image_format');
}
if (isset_request_var('graph_width')) {
$fields_template_import['graph_width']['value'] = get_filter_request_var('graph_width');
} else {
$fields_template_import['graph_width']['value'] = read_config_option('default_graph_width');
}
if (isset_request_var('graph_height')) {
$fields_template_import['graph_height']['value'] = get_filter_request_var('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 '<div id="contents"></div>';
form_hidden_box('save_component_import','1','');
form_save_button('', 'import', 'import', false);
?>
<script type='text/javascript'>
$(function() {
$('#import_file').change(function() {
var form = $('#import')[0];
var data = new FormData(form);
Pace.start();
$.ajax({
type: 'POST',
enctype: 'multipart/form-data',
url: (urlPath != '/' ? urlPath + '/':'') + 'templates_import.php?preview_only=true',
data: data,
processData: false,
contentType: false,
cache: false,
timeout: 600000,
success: function (data) {
if ($('#contents').length == 0) {
$('#main').append('<div id="contents"></div>');
} else {
$('#contents').empty();
}
$('#contents').html(data);
if ($('#templates_import_save2_child').length) {
applySelectorVisibilityAndActions();
$('#templates_import_save2_child').find('tr[id^="line_import_new_"]').each(function(event) {
selectUpdateRow(event, $(this));
});
}
Pace.stop();
},
error: function (e) {
if ($('#contents').length == 0) {
$('#main').append('<div id="contents"></div>');
} else {
$('#contents').empty();
}
$('#contents').html(data);
Pace.stop();
}
});
});
});
</script>
<?php
}
function is_tmp_writable($tmp_dir) {
$tmp_dir = sys_get_temp_dir();
$tmp_len = strlen($tmp_dir);
$tmp_dir .= ($tmp_len !== 0 && substr($tmp_dir, -$tmp_len) === '/') ? '': '/';
$is_tmp = is_resource_writable($tmp_dir);
return $is_tmp;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/Cacti.git
git@gitee.com:mirrors/Cacti.git
mirrors
Cacti
Cacti
develop

搜索帮助