" . __n('Click \'Continue\' to Import the following Package.', 'Click \'Continue\' to Import all following Packages.', cacti_sizeof($pkg_array)) . "
$pkg_list
";
if ($skp_list != '') {
print "
" . __n(
'The following Selected Package will be skipped as no Files or Template Items were selected.',
'The following selected Packages will be skipped as no Files or Template Items were selected.', cacti_sizeof($skp_array)) . "
$skp_list
";
}
if ($pkg_file_list != '') {
print '
' . __('Files to be Imported') . '
';
print "
" . __('The Files in the list below will be imported.') . "
" . __('Package') . '
' . __('File') . "
$pkg_file_list
";
}
if ($pkg_import_list != '') {
print '
' . __('Template Items to be Imported') . '
';
print "
" . __('The Template Items in the list below will be imported.') . "
" . __('Package') . '
' . __('Type') . '
' . __('Name') . '
' . __('Status') . "
$pkg_import_list
";
}
$save_html = "';
} else {
raise_message('no_selection', __('You must select either a File or a Template Item to import before proceeding'), MESSAGE_LEVEL_ERROR);
header('Location: package_import.php');
exit;
}
}
} else {
raise_message(40);
header('Location: package_import.php');
exit;
}
print "
$save_html
";
html_end_box();
form_end();
bottom_footer();
}
function form_save() : void {
global $preview_only;
validate_request_vars();
if (isrv('save_component_import')) {
if (isset($_FILES['import_file']['tmp_name']) &&
($_FILES['import_file']['tmp_name'] != 'none') &&
($_FILES['import_file']['tmp_name'] != '')) {
// file upload
$xmlfile = $_FILES['import_file']['tmp_name'];
$_SESSION['sess_import_package'] = file_get_contents($xmlfile);
} elseif (isset($_SESSION['sess_import_package'])) {
$xmlfile = sys_get_temp_dir() . '/package_import_' . rand();
file_put_contents($xmlfile, $_SESSION['sess_import_package']);
unset($_SESSION['sess_import_package']);
} else {
header('Location: package_import.php');
exit;
}
if (isrv('trust_signer') && gnrv('trust_signer') == 'on') {
import_validate_public_key($xmlfile, true);
}
if (gfrv('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('data_source_profile');
}
if (gnrv('preview_only') == 'on') {
$preview_only = true;
} elseif (gnrv('import_confirmed') == 'on') {
$preview_only = false;
} else {
$preview_only = true;
}
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;
}
$hashes = [];
$files = [];
$templates = [];
// 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_file_')) {
$id = base64_decode(str_replace('chk_file_', '', $var), true);
$id = json_decode($id, true);
if (str_contains($id['pfile'], '/')) {
$parts = explode('/', $id['pfile']);
} elseif (str_contains($id['pfile'], '\\')) {
$parts = explode('\\', $id['pfile']);
} else {
$parts = [$id['pfile']];
}
foreach ($parts as $index => $p) {
if ($p == 'scripts') {
break;
}
if ($p == 'resource') {
break;
} else {
unset($parts[$index]);
}
}
$id['pfile'] = implode('/', $parts);
$files[] = $id['pfile'];
}
if (str_contains($var, 'chk_import_')) {
$id = base64_decode(str_replace('chk_import_', '', $var), true);
$id = json_decode($id, true);
$hashes[] = $id['hash'];
}
}
if (cacti_sizeof($files) && !cacti_sizeof($hashes)) {
$hashes[] = 'dont import';
} elseif (cacti_sizeof($hashes) && !cacti_sizeof($files)) {
$files[] = 'dont import';
}
if (file_exists($xmlfile)) {
cacti_log("Importing $xmlfile");
} else {
cacti_log("Unable to import $xmlfile");
}
$package_name = import_package_get_name($xmlfile);
cacti_log('Package name is ' . $package_name);
// obtain debug information if it's set
$data = import_package($xmlfile, $profile_id, $remove_orphans, $replace_svalues, $preview_only, false, false, $hashes, $files);
if ($preview_only) {
package_prepare_import_array($templates, $files, $package_name, $xmlfile, $data);
import_display_package_data($templates, $files, $package_name, $xmlfile, $data, false);
} else {
if ($data !== false) {
raise_message('import_success_' . md5($xmlfile), __('The Package %s Imported Successfully', $package_name), MESSAGE_LEVEL_INFO);
} else {
raise_message('import_fail_' . md5($xmlfile), __('The Package %s Import Failed', $package_name), MESSAGE_LEVEL_ERROR);
}
unlink($xmlfile);
unset($_SESSION['sess_import_package']);
header('Location: package_import.php?package_location=0');
exit;
}
}
}
function package_file_get_contents(string $package_location, string $package_file, string $filename) : mixed {
$package_found = false;
if ($package_location > 0) {
$repo = json_decode(get_repo_manifest_file($package_location), true);
$manifest = $repo['manifest'];
if (cacti_sizeof($manifest)) {
foreach ($manifest as $m) {
if ($m['filename'] == $package_file) {
$author = $m['metadata']['author'];
$homepage = $m['metadata']['homepage'];
$email = $m['metadata']['email'];
$package_found = true;
break;
}
}
}
} elseif (isset($_SESSION['sess_import_package'])) {
$xmlfile = sys_get_temp_dir() . '/package_import_' . rand();
$binary_signature = '';
file_put_contents($xmlfile, $_SESSION['sess_import_package']);
$data = import_read_package_data($xmlfile, $binary_signature, true);
$public_key = import_package_get_public_key($xmlfile);
$fdata = false;
foreach ($data['files']['file'] as $file) {
if ($file['name'] == $filename) {
$binary_signature = base64_decode($file['filesignature'], true);
$fdata = base64_decode($file['data'], true);
// provide two checks against the public key
$ok = openssl_verify($fdata, $binary_signature, $public_key, OPENSSL_ALGO_SHA1);
if ($ok != 1) {
$ok = openssl_verify($fdata, $binary_signature, $public_key, OPENSSL_ALGO_SHA256);
}
if ($ok != 1) {
$fdata = false;
}
break;
}
}
unlink($xmlfile);
return $fdata;
}
if ($package_found) {
$data = get_repo_file($package_location, $package_file, true);
if ($data !== false) {
$tmp_dir = sys_get_temp_dir() . '/package' . $_SESSION[SESS_USER_ID];
if (!is_dir($tmp_dir)) {
mkdir($tmp_dir);
}
$binary_signature = '';
$xmlfile = $tmp_dir . '/' . $package_file;
file_put_contents($xmlfile, $data);
$public_key = import_package_get_public_key($xmlfile);
$data = import_read_package_data($xmlfile, $binary_signature, true);
$fdata = false;
foreach ($data['files']['file'] as $file) {
if ($file['name'] == $filename) {
$binary_signature = base64_decode($file['filesignature'], true);
$fdata = base64_decode($file['data'], true);
// provide two checks against the public key
$ok = openssl_verify($fdata, $binary_signature, $public_key, OPENSSL_ALGO_SHA1);
if ($ok != 1) {
$ok = openssl_verify($fdata, $binary_signature, $public_key, OPENSSL_ALGO_SHA256);
}
if ($ok != 1) {
$fdata = false;
}
break;
}
}
unlink($xmlfile);
return $fdata;
}
}
return false;
}
function package_diff_file() : void {
$package_location = gfrv('package_location');
$package_file = grv('package_file');
$filename = grv('filename');
$options = [
'ignoreWhitespace' => true,
'ignoreCase' => false
];
$newfile = package_file_get_contents($package_location, $package_file, $filename);
if ($newfile !== false) {
$newfile = str_replace("\n\r", "\n", $newfile);
$newfile = explode("\n", $newfile);
}
$oldfile = file_get_contents(CACTI_PATH_BASE . '/' . $filename);
if ($oldfile !== false) {
$oldfile = str_replace("\n\r", "\n", $oldfile);
$oldfile = explode("\n", $oldfile);
}
if (cacti_sizeof($oldfile)) {
if (cacti_sizeof($newfile)) {
$diff = new Diff($oldfile, $newfile, $options);
$renderer = new Diff_Renderer_Html_Inline;
print '' . $diff->render($renderer) . '