$val) {
if (strpos($var, 'chk_file_') !== false) {
$id = base64_decode(str_replace('chk_file_', '', $var));
$id = json_decode($id, true);
if (strpos($id['pfile'], '/') !== false) {
$parts = explode('/', $id['pfile']);
} elseif (strpos($id['pfile'], '\\') !== false) {
$parts = explode('\\', $id['pfile']);
} else {
$parts = array($id['pfile']);
}
foreach($parts as $index => $p) {
if ($p == 'scripts') {
break;
} elseif ($p == 'resource') {
break;
} else {
unset($parts[$index]);
}
}
$id['pfile'] = implode('/', $parts);
$files[] = $id['pfile'];
}
if (strpos($var, 'chk_import_') !== false) {
$id = base64_decode(str_replace('chk_import_', '', $var));
$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";
}
$package_name = import_package_get_name($xmlfile);
$details = import_package_get_details($xmlfile);
if (isset($details['class'])) {
$class = $details['class'];
} else {
$class = '';
}
/* obtain debug information if it's set */
$data = import_package($xmlfile, $profile_id, $remove_orphans, $replace_svalues, $preview_only, false, false, $hashes, $files, $class);
if ($preview_only) {
package_prepare_import_array($templates, $files, $package_name, $xmlfile, $data);
import_display_package_data($templates, $files, $package_name, $xmlfile, $data);
unlink($xmlfile);
} else {
if ($data !== false) {
raise_message('import_success', __('The Package %s Imported Successfully', $package_name), MESSAGE_LEVEL_INFO);
} else {
raise_message('import_fail', __('The Package %s Import Failed', $package_name), MESSAGE_LEVEL_ERROR);
}
unlink($xmlfile);
unset($_SESSION['sess_import_package']);
header('Location: package_import.php');
exit;
}
}
}
function package_file_get_contents($filename) {
if (isset($_SESSION['sess_import_package'])) {
$xmlfile = sys_get_temp_dir() . '/package_import_' . rand();
file_put_contents($xmlfile, $_SESSION['sess_import_package']);
$data = import_read_package_data($xmlfile, $binary_signature);
if (isset($data['publickey'])) {
$public_key = base64_decode($data['publickey']);
} else {
$public_key = get_public_key();
}
$fdata = false;
foreach ($data['files']['file'] as $file) {
if ($file['name'] == $filename) {
$binary_signature = base64_decode($file['filesignature']);
$fdata = base64_decode($file['data']);
if (strlen($public_key) < 200) {
$ok = openssl_verify($fdata, $binary_signature, $public_key, OPENSSL_ALGO_SHA1);
} else {
$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() {
global $config;
$package_location = get_filter_request_var('package_location');
$package_file = get_nfilter_request_var('package_file');
$filename = get_nfilter_request_var('filename');
$options = array(
'ignoreWhitespace' => true,
'ignoreCase' => false
);
$newfile = package_file_get_contents($filename);
if ($newfile !== false) {
$newfile = str_replace("\n\r", "\n", $newfile);
$newfile = explode("\n", $newfile);
}
$oldfile = file_get_contents($config['base_path'] . '/' . $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);
} else {
print "New file does not exist";
}
} else {
print "Old file does not exist";
}
}
function package_get_details() {
$package_ids = get_filter_request_var('package_ids', FILTER_VALIDATE_IS_NUMERIC_LIST);
$package_location = get_filter_request_var('package_location');
$profile_id = get_filter_request_var('data_source_profile');
$remove_orphans = isset_request_var('remove_orphans') ? true:false;
$replace_svalues = isset_request_var('replace_svalues') ? true:false;
$preview = true;
$repo = json_decode(get_repo_manifest_file($package_location), true);
$manifest = $repo['manifest'];
if ($package_ids != '') {
$package_ids = explode(',', $package_ids);
$templates = array();
$files = array();
foreach($package_ids as $package_id) {
$filename = $manifest[$package_id]['filename'];
$package_name = $manifest[$package_id]['name'];
$data = get_repo_file($package_location, $filename, false);
if ($data !== false) {
$tmp_dir = sys_get_temp_dir() . '/package' . $_SESSION['sess_user_id'];
if (!is_dir($tmp_dir)) {
mkdir($tmp_dir);
}
$xmlfile = $tmp_dir . '/' . $filename;
file_put_contents($xmlfile, $data);
$validated = import_validate_public_key($xmlfile, false);
if ($validated === false) {
$public_key = get_public_key();
} else {
$public_key = $validated;
}
$data = import_package($xmlfile, $profile_id, $remove_orphans, $replace_svalues, $preview);
package_prepare_import_array($templates, $files, $package_name, $filename, $data);
unlink($xmlfile);
} else {
raise_message_javascript(__('Error in Package'), __('The package "%s" download or validation failed', $package_name), __('See the cacti.log for more information. It could be that you had either an API Key error or the package was tamered with, or the location is not available.'));
}
}
import_display_package_data($templates, $files, $package_name, $filename, $data);
} else {
raise_message_javascript(__('Error in Package'), __('The package download or validation failed'), __('See the cacti.log for more information. It could be that you had either an API Key error or the package was tamered with, or the location is not available'));
}
}
function import_validate_public_key($xmlfile, $accept = false) {
$public_key = get_public_key();
if (!file_exists($xmlfile)) {
return false;
}
$filename = "compress.zlib://$xmlfile";
$data = file_get_contents($filename);
if ($data != '') {
$name = '';
$author = '';
$homepage = '';
$email = '';
$package_keyname = '';
$package_publickey = '';
$xml = xml2array($data);
if (cacti_sizeof($xml)) {
if (isset($xml['info']['name'])) {
$name = $xml['info']['name'];
}
if (isset($xml['info']['author'])) {
$author = $xml['info']['author'];
}
if (isset($xml['info']['homepage'])) {
$homepage = $xml['info']['homepage'];
}
if (isset($xml['info']['email'])) {
$email = $xml['info']['email'];
}
if (isset($xml['publickeyname'])) {
$package_keyname = $xml['publickeyname'];
}
if (isset($xml['publickey'])) {
$package_publickey = base64_decode($xml['publickey']);
}
if ($package_publickey != '') {
return $package_publickey;
} else {
return get_public_key();
}
} else {
raise_message_javascript(__('Error in Package'), __('Package XML File Damaged.'), __('The XML files appears to be invalid. Please contact the package author'));
}
} else {
raise_message_javascript(__('Error in Package'), __('The XML files for the package does not exist'), __('Check the package repository file for files that should exist and find the one that is missing'));
}
return false;
}
function package_validate_signature($xmlfile) {
global $config;
// Cacti public key first
$cacti_key = get_public_key();
$package_key = import_package_get_public_key($xmlfile);
// Other trusted keys next
$keys = array_rekey(
db_fetch_assoc('SELECT public_key FROM package_public_keys'),
'public_key', 'public_key'
);
$keys[$cacti_key] = $cacti_key;
if (in_array($package_key, $keys, true)) {
return true;
} else {
return false;
}
}
function import_display_package_data($templates, $files, $package_name, $xmlfile, $data) {
global $config, $device_classes;
$details = import_package_get_details($xmlfile);
html_start_box(__('Packages Details'), '100%', '', '1', 'center', '');
$display_text = array(
array(
'display' => __('Author'),
),
array(
'display' => __('Homepage'),
),
array(
'display' => __('Email')
),
array(
'display' => __('Device class')
),
array(
'display' => __('Version')
),
array(
'display' => __('Copyright')
)
);
html_header($display_text);
$id = 99;
form_alternate_row('line_' . $id);
form_selectable_cell($details['author'], $id);
form_selectable_cell($details['homepage'], $id);
form_selectable_cell($details['email'], $id);
if (isset($details['class']) && array_key_exists($details['class'], $device_classes)) {
form_selectable_cell($device_classes[$details['class']], $id);
} else {
form_selectable_cell(__('Unknown'), $id);
}
form_selectable_cell($details['version'], $id);
form_selectable_cell($details['copyright'], $id);
form_end_row();
html_end_box();
// Show the filename status'
if (cacti_sizeof($files)) {
html_start_box(__('Import Package Filenames [ None selected imports all, Check to import selectively ]'), '100%', '', '1', 'center', '');
$display_text = array(
array(
'display' => __('Package'),
),
array(
'display' => __('Filename'),
),
array(
'display' => __('Status')
)
);
html_header_checkbox($display_text, false, '', true, 'file');
foreach($files as $pdata => $pfiles) {
$pdata_parts = explode('|', $pdata);
$file_package_file = $pdata_parts[0];
if (isset($pdata_parts[1])) {
$file_package_name = $pdata_parts[1];
} else {
$file_package_name = 'Not Set';
}
if (cacti_sizeof($pfiles)) {
foreach($pfiles as $pfile => $status) {
$id = 'file_' . base64_encode(
json_encode(
array(
'package' => $file_package_name,
'filename' => $file_package_file,
'pfile' => $pfile,
)
)
);
form_alternate_row('line_' . $id);
form_selectable_cell($file_package_name, $id);
form_selectable_cell($pfile, $id);
$status = explode(',', $status);
$nstatus = '';
foreach($status as $s) {
$s = trim($s);
if ($s == 'differences') {
$url = 'package_import.php' .
'?action=diff' .
'&package_location=0' .
'&package_file=' . $file_package_file .
'&package_name=' . $file_package_name .
'&filename=' . str_replace($config['base_path'] . '/', '', $pfile);
$nstatus .= ($nstatus != '' ? ', ':'') .
"" . __('Differences') . '';
} elseif ($s == 'identical') {
$nstatus .= ($nstatus != '' ? ', ':'') . __('Unchanged');
} elseif ($s == 'not writable') {
$nstatus .= ($nstatus != '' ? ', ':'') . __('Not Writable');
} elseif ($s == 'writable') {
$nstatus .= ($nstatus != '' ? ', ':'') . __('Writable');
} elseif ($s == 'new') {
$nstatus .= ($nstatus != '' ? ', ':'') . __('New');
} else {
$nstatus .= ($nstatus != '' ? ', ':'') . __('Unknown');
}
}
form_selectable_cell($nstatus, $id);
form_checkbox_cell($pfile, $id);
form_end_row();
}
}
}
html_end_box();
}
if (cacti_sizeof($templates)) {
html_start_box(__('Import Package Templates [ None selected imports all, Check to import selectively ]'), '100%', '', '1', 'center', '');
$display_text = array(
array(
'display' => __('Template Type')
),
array(
'display' => __('Template Name')
),
array(
'display' => __('Status')
),
array(
'display' => __('Changes/Diffferences')
)
);
html_header_checkbox($display_text, false, '', true, 'import');
$templates = array_reverse($templates);
foreach($templates as $hash => $detail) {
$files = explode('
', $detail['package_file']);
$id = 'import_' . base64_encode(
json_encode(
array(
'package' => $detail['package'],
'hash' => $hash,
'type' => $detail['type_name'],
'name' => $detail['name'],
'status' => $detail['status'],
'files' => $files
)
)
);
if ($detail['status'] == 'updated') {
$status = "" . __('Updated') . '';
} elseif ($detail['status'] == 'new') {
$status = "" . __('New') . '';
} else {
$status = "" . __('Unchanged') . '';
}
form_alternate_row('line_import_' . $detail['status'] . '_' . $id);
form_selectable_cell($detail['type_name'], $id);
form_selectable_cell($detail['name'], $id);
form_selectable_cell($status, $id);
if (isset($detail['vals'])) {
$diff_details = '';
$diff_array = array();
$orphan_array = array();
foreach($detail['vals'] as $package => $diffs) {
if (isset($diffs['differences'])) {
foreach($diffs['differences'] as $item) {
$diff_array[$item] = $item;
}
}
if (isset($diffs['orphans'])) {
foreach($diffs['orphans'] as $item) {
$orphan_array[$item] = $item;
}
}
}
if (cacti_sizeof($diff_array)) {
$diff_details .= __('Differences') . '
' . implode('
', $diff_array);
}
if (cacti_sizeof($orphan_array)) {
$diff_details .= ($diff_details != '' ? '
':'') . __('Orphans') . '
' . 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();
}
?>