$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(); } ?>
'; } function get_repo_file($repo_id, $filename = 'package.manifest', $javascript = false) { return false; } function get_repo_manifest_file($repo_id) { return get_repo_file($repo_id, 'package.manifest'); } function is_tmp_writable() { $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; } function package_prepare_import_array(&$templates, &$files, $package_name, $package_filename, $import_info) { global $hash_type_names; /** * 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' * ); * * $files[$package_filename|$package_name] = array( * 'filename' => 'somefilename' * ); */ if (cacti_sizeof($import_info)) { if (isset($import_info[1]) && cacti_sizeof($import_info[1])) { foreach($import_info[1] as $filename => $status) { $files["$package_filename|$package_name"][$filename] = $status; } } if (isset($import_info[0]) && cacti_sizeof($import_info[0])) { foreach ($import_info[0] as $type => $type_array) { if ($type == 'files') { continue; } foreach ($type_array as $index => $vals) { $hash = $vals['hash']; $templates[$hash]['package'] = $package_name; $templates[$hash]['package_file'] = $package_filename; $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'])) { unset($vals['dep']); } if (cacti_sizeof($vals)) { $templates[$hash]['vals'][$package_name] = $vals; } } } } } }