';print_r($dep_hash_cache);print '';exit; $hash_cache = array(); $repair = 0; /* the order of the $hash_type_codes array is ordered such that the items with the most dependencies are last and the items with no dependencies are first. this means dependencies will just magically work themselves out :) */ reset($hash_type_codes); while (list($type, $code) = each($hash_type_codes)) { /* do we have any matches for this type? */ if (isset($dep_hash_cache[$type])) { /* yes we do. loop through each match for this type */ for ($i=0; $i 0) { $items = db_fetch_assoc_prepared('SELECT * FROM graph_templates_item WHERE graph_template_id = ? AND local_graph_id = 0', array($graph_template_id)); if (sizeof($items)) { foreach($items as $item) { $orphaned_items[$item['hash']] = $item; } } }else{ $orphaned_items = array(); } while (list($item_hash, $item_array) = each($xml_array['items'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* mark present hashes */ if (isset($orphaned_items[$parsed_hash['hash']])) { unset($orphaned_items[$parsed_hash['hash']]); } /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_item_id = db_fetch_cell_prepared('SELECT id FROM graph_templates_item WHERE hash = ? AND graph_template_id = ? AND local_graph_id=0', array($parsed_hash['hash'], $graph_template_id)); if (!empty($_graph_template_item_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM graph_templates_item WHERE id = ?', array($_graph_template_item_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_graph_template_item_id) ? '0' : $_graph_template_item_id); $save['hash'] = $parsed_hash['hash']; $save['graph_template_id'] = $graph_template_id; reset($struct_graph_item); while (list($field_name, $field_array) = each($struct_graph_item)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* is the value of this field a hash or not? */ if (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $item_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($item_array[$field_name], $hash_cache); }elseif (($field_name == 'color_id') && (preg_match('/^[a-fA-F0-9]{6}$/', $item_array[$field_name])) && (get_version_index($parsed_hash['version']) >= get_version_index('0.8.5'))) { /* treat the 'color' field differently */ $color_id = db_fetch_cell_prepared('SELECT id FROM colors WHERE hex = ?', array($item_array[$field_name])); if (empty($color_id) && !$preview_only) { db_execute_prepared('INSERT INTO colors ( hex) VALUES (?)', array($item_array[$field_name])); $color_id = db_fetch_insert_id(); } $save[$field_name] = $color_id; }else{ $save[$field_name] = xml_character_decode($item_array[$field_name]); } } } if (!empty($_graph_template_id)) { if (!sizeof($previous_data)) { $new_items[$parsed_hash['hash']] = $save; } } /* check for status changes */ $status += compare_data($save, $previous_data, 'graph_templates_item'); if (!$preview_only) { $graph_template_item_id = sql_save($save, 'graph_templates_item'); $hash_cache['graph_template_item']{$parsed_hash['hash']} = $graph_template_item_id; } } } /* import into: graph_template_input */ if (is_array($xml_array['inputs'])) { while (list($item_hash, $item_array) = each($xml_array['inputs'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_graph_template_input_id = db_fetch_cell_prepared('SELECT id FROM graph_template_input WHERE hash = ? AND graph_template_id = ?', array($parsed_hash['hash'], $graph_template_id)); if (!empty($_graph_template_input_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM graph_template_input WHERE id = ?', array($_graph_template_input_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_graph_template_input_id) ? '0' : $_graph_template_input_id); $save['hash'] = $parsed_hash['hash']; $save['graph_template_id'] = $graph_template_id; reset($fields_graph_template_input_edit); while (list($field_name, $field_array) = each($fields_graph_template_input_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { $save[$field_name] = xml_character_decode($item_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'graph_template_input'); if (!$preview_only) { $graph_template_input_id = sql_save($save, 'graph_template_input'); $hash_cache['graph_template_input']{$parsed_hash['hash']} = $graph_template_input_id; /* import into: graph_template_input_defs */ $hash_items = explode('|', $item_array['items']); if (!empty($hash_items[0])) { for ($i=0; $i 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($graph_template_id) ? 'fail' : 'success')); if (isset($new_items) && sizeof($new_items)) { $new_text = array(); foreach($new_items as $item) { $new_text[] = 'New Graph Items, Type: ' . $graph_item_types[$item['graph_type_id']] . ', Text Format: ' . $item['text_format'] . ', Value: ' . $item['value']; } $_SESSION['import_debug_info']['new_items'] = $new_text; } if (isset($orphaned_items) && sizeof($orphaned_items)) { $orphan_text = array(); foreach($orphaned_items as $item) { $orphan_text[] = 'Orphaned Graph Items, Type: ' . $graph_item_types[$item['graph_type_id']] . ', Text Format: ' . $item['text_format'] . ', Value: ' . $item['value']; } $_SESSION['import_debug_info']['orphans'] = $orphan_text; } return $hash_cache; } function xml_to_data_template($hash, &$xml_array, &$hash_cache, $import_as_new, $profile_id) { global $struct_data_source, $struct_data_source_item, $import_template_id, $preview_only; /* track changes */ $status = 0; /* import into: data_template */ $_data_template_id = db_fetch_cell_prepared('SELECT id FROM data_template WHERE hash = ?', array($hash)); if (!empty($_data_template_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM data_template WHERE id = ?', array($_data_template_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_template_id) ? '0' : $_data_template_id); $save['hash'] = $hash; $save['name'] = $xml_array['name']; /* check for status changes */ $status += compare_data($save, $previous_data, 'data_template'); if (!$preview_only) { $data_template_id = sql_save($save, 'data_template'); $hash_cache['data_template'][$hash] = $data_template_id; }else{ $data_template_id = $_data_template_id; } /* import into: data_template_data */ unset($save); $save['id'] = (empty($_data_template_id) ? '0' : db_fetch_cell_prepared('SELECT dtd.id FROM data_template AS dt INNER JOIN data_template_data AS dtd ON dt.id=dtd.data_template_id WHERE dt.id = ? AND dtd.local_data_id=0', array($data_template_id))); if (!empty($save['id'])) { $previous_data = db_fetch_row_prepared('SELECT * FROM data_template_data WHERE id = ?', array($save['id'])); }else{ $previous_data = array(); } $save['data_template_id'] = $data_template_id; reset($struct_data_source); while (list($field_name, $field_array) = each($struct_data_source)) { /* make sure this field exists in the xml array first */ if (isset($xml_array['ds']{'t_' . $field_name})) { $save{'t_' . $field_name} = $xml_array['ds']{'t_' . $field_name}; } /* make sure this field exists in the xml array first */ if (isset($xml_array['ds'][$field_name])) { /* is the value of this field a hash or not? */ if ($field_name == 'data_source_profile_id') { $save[$field_name] = $profile_id; }elseif (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $xml_array['ds'][$field_name])) { $save[$field_name] = resolve_hash_to_id($xml_array['ds'][$field_name], $hash_cache); }else{ $save[$field_name] = xml_character_decode($xml_array['ds'][$field_name]); } } } /* use the profiles step if we are not importing a new one */ if ($import_as_new == false) { $save['rrd_step'] = db_fetch_cell_prepared('SELECT step FROM data_source_profiles WHERE id = ?', array($profile_id)); } /* Fix for importing during installation - use the polling interval as the step if we are to use the default rra settings */ if (is_array($profile_id) == true) { $save['rrd_step'] = read_config_option('poller_interval'); } /* check for status changes */ $status += compare_data($save, $previous_data, 'data_template_data'); if (!$preview_only) { $data_template_data_id = sql_save($save, 'data_template_data'); $import_template_id = $data_template_data_id; } /* import into: data_template_rrd */ if (is_array($xml_array['items'])) { while (list($item_hash, $item_array) = each($xml_array['items'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_data_template_rrd_id = db_fetch_cell_prepared('SELECT id FROM data_template_rrd WHERE hash = ? AND data_template_id = ? AND local_data_id=0', array($parsed_hash['hash'], $data_template_id)); if (!empty($_data_template_rrd_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM data_template_rrd WHERE id = ?', array($_data_template_rrd_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_template_rrd_id) ? '0' : $_data_template_rrd_id); $save['hash'] = $parsed_hash['hash']; $save['data_template_id'] = $data_template_id; reset($struct_data_source_item); while (list($field_name, $field_array) = each($struct_data_source_item)) { /* make sure this field exists in the xml array first */ if (isset($item_array{'t_' . $field_name})) { $save{'t_' . $field_name} = $item_array{'t_' . $field_name}; } /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* is the value of this field a hash or not? */ if (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $item_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($item_array[$field_name], $hash_cache); }else{ $save[$field_name] = xml_character_decode($item_array[$field_name]); } } } /* use the profiles step * 2 as the heartbeat if we are not importing a new profile */ if ($import_as_new === false) { $save['rrd_heartbeat'] = db_fetch_cell_prepared('SELECT step FROM data_source_profiles WHERE id = ?', array($profile_id)) * 2; } /* Fix for importing during installation - use the polling interval as the step if we are to use the default rra settings */ if (is_array($profile_id) == true) { $save['rrd_heartbeat'] = read_config_option('poller_interval') * 2; } /* check for status changes */ $status += compare_data($save, $previous_data, 'data_template_rrd'); if (!$preview_only) { $data_template_rrd_id = sql_save($save, 'data_template_rrd'); $hash_cache['data_template_item']{$parsed_hash['hash']} = $data_template_rrd_id; } } } /* import into: data_input_data */ if (!$preview_only) { if (is_array($xml_array['data'])) { while (list($item_hash, $item_array) = each($xml_array['data'])) { unset($save); $save['data_template_data_id'] = $data_template_data_id; $save['data_input_field_id'] = resolve_hash_to_id($item_array['data_input_field_id'], $hash_cache); $save['t_value'] = $item_array['t_value']; $save['value'] = xml_character_decode($item_array['value']); sql_save($save, 'data_input_data', array('data_template_data_id', 'data_input_field_id'), false); } } /* push out field mappings for the data collector */ db_execute_prepared('REPLACE INTO poller_data_template_field_mappings SELECT dtr.data_template_id, dif.data_name, GROUP_CONCAT(dtr.data_source_name ORDER BY dtr.data_source_name) AS data_source_names, NOW() AS last_updated FROM data_template_rrd AS dtr INNER JOIN data_input_fields AS dif ON dtr.data_input_field_id = dif.id WHERE dtr.local_data_id = 0 AND dtr.data_template_id = ? GROUP BY dtr.data_template_id, dif.data_name', array($data_template_id)); } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_data_template_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($data_template_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_data_query($hash, &$xml_array, &$hash_cache) { global $fields_data_query_edit, $fields_data_query_item_edit, $preview_only; /* track changes */ $status = 0; /* import into: snmp_query */ $_data_query_id = db_fetch_cell_prepared('SELECT id FROM snmp_query WHERE hash = ?', array($hash)); if (!empty($_data_query_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM snmp_query WHERE id = ?', array($_data_query_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_query_id) ? '0' : $_data_query_id); $save['hash'] = $hash; reset($fields_data_query_edit); while (list($field_name, $field_array) = each($fields_data_query_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { /* is the value of this field a hash or not? */ if (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $xml_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($xml_array[$field_name], $hash_cache); }else{ $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } } /* check for status changes */ $status += compare_data($save, $previous_data, 'snmp_query'); if (!$preview_only) { $data_query_id = sql_save($save, 'snmp_query'); $hash_cache['data_query'][$hash] = $data_query_id; update_replication_crc(0, 'poller_replicate_snmp_query_crc'); }else{ $data_query_id = $_data_query_id; } /* import into: snmp_query_graph */ if (is_array($xml_array['graphs'])) { while (list($item_hash, $item_array) = each($xml_array['graphs'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_data_query_graph_id = db_fetch_cell_prepared('SELECT id FROM snmp_query_graph WHERE hash = ? AND snmp_query_id = ?', array($parsed_hash['hash'], $data_query_id)); if (!empty($_data_query_graph_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM snmp_query_graph WHERE id = ?', array($_data_query_graph_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_query_graph_id) ? '0' : $_data_query_graph_id); $save['hash'] = $parsed_hash['hash']; $save['snmp_query_id'] = $data_query_id; reset($fields_data_query_item_edit); while (list($field_name, $field_array) = each($fields_data_query_item_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* is the value of this field a hash or not? */ if (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $item_array[$field_name])) { $save[$field_name] = resolve_hash_to_id($item_array[$field_name], $hash_cache); }else{ $save[$field_name] = xml_character_decode($item_array[$field_name]); } } } /* check for status changes */ $status += compare_data($save, $previous_data, 'snmp_query_graph'); if (!$preview_only) { $data_query_graph_id = sql_save($save, 'snmp_query_graph'); $hash_cache['data_query_graph']{$parsed_hash['hash']} = $data_query_graph_id; /* import into: snmp_query_graph_rrd */ if (is_array($item_array['rrd'])) { while (list($sub_item_hash, $sub_item_array) = each($item_array['rrd'])) { unset($save); $save['snmp_query_graph_id'] = $data_query_graph_id; $save['data_template_id'] = resolve_hash_to_id($sub_item_array['data_template_id'], $hash_cache); $save['data_template_rrd_id'] = resolve_hash_to_id($sub_item_array['data_template_rrd_id'], $hash_cache); $save['snmp_field_name'] = $sub_item_array['snmp_field_name']; sql_save($save, 'snmp_query_graph_rrd', array('snmp_query_graph_id', 'data_template_id', 'data_template_rrd_id'), false); } } }else{ $data_query_graph_id = $_data_query_graph_id; } /* import into: snmp_query_graph_sv */ if (is_array($item_array['sv_graph'])) { while (list($sub_item_hash, $sub_item_array) = each($item_array['sv_graph'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($sub_item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_data_query_graph_sv_id = db_fetch_cell_prepared('SELECT id FROM snmp_query_graph_sv WHERE hash = ? AND snmp_query_graph_id = ?', array($parsed_hash['hash'], $data_query_graph_id)); if (!empty($_data_query_graph_sv_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM snmp_query_graph_sv WHERE id = ?', array($_data_query_graph_sv_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_query_graph_sv_id) ? '0' : $_data_query_graph_sv_id); $save['hash'] = $parsed_hash['hash']; $save['snmp_query_graph_id'] = $data_query_graph_id; $save['sequence'] = $sub_item_array['sequence']; $save['field_name'] = $sub_item_array['field_name']; $save['text'] = xml_character_decode($sub_item_array['text']); /* check for status changes */ $status += compare_data($save, $previous_data, 'snmp_query_graph_sv'); if (!$preview_only) { $data_query_graph_sv_id = sql_save($save, 'snmp_query_graph_sv'); $hash_cache['data_query_sv_graph']{$parsed_hash['hash']} = $data_query_graph_sv_id; } } } /* import into: snmp_query_graph_rrd_sv */ if (is_array($item_array['sv_data_source'])) { while (list($sub_item_hash, $sub_item_array) = each($item_array['sv_data_source'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($sub_item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_data_query_graph_rrd_sv_id = db_fetch_cell_prepared('SELECT id FROM snmp_query_graph_rrd_sv WHERE hash = ? AND snmp_query_graph_id = ?', array($parsed_hash['hash'], $data_query_graph_id)); if (!empty($_data_query_graph_rrd_sv_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM snmp_query_graph_rrd_sv WHERE id = ?', array($_data_query_graph_rrd_sv_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_query_graph_rrd_sv_id) ? '0' : $_data_query_graph_rrd_sv_id); $save['hash'] = $parsed_hash['hash']; $save['snmp_query_graph_id'] = $data_query_graph_id; $save['data_template_id'] = resolve_hash_to_id($sub_item_array['data_template_id'], $hash_cache); $save['sequence'] = $sub_item_array['sequence']; $save['field_name'] = $sub_item_array['field_name']; $save['text'] = xml_character_decode($sub_item_array['text']); /* check for status changes */ $status += compare_data($save, $previous_data, 'snmp_query_graph_rrd_sv'); if (!$preview_only) { $data_query_graph_rrd_sv_id = sql_save($save, 'snmp_query_graph_rrd_sv'); $hash_cache['data_query_sv_data_source']{$parsed_hash['hash']} = $data_query_graph_rrd_sv_id; } } } } } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_data_query_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($data_query_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_gprint_preset($hash, &$xml_array, &$hash_cache) { global $fields_grprint_presets_edit, $preview_only; /* track changes */ $status = 0; /* import into: graph_templates_gprint */ $_gprint_preset_id = db_fetch_cell_prepared('SELECT id FROM graph_templates_gprint WHERE hash = ?', array($hash)); if (!empty($_gprint_preset_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM graph_templates_gprint WHERE id = ?', array($_gprint_preset_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_gprint_preset_id) ? '0' : $_gprint_preset_id); $save['hash'] = $hash; reset($fields_grprint_presets_edit); while (list($field_name, $field_array) = each($fields_grprint_presets_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'graph_templates_gprint'); if (!$preview_only) { $gprint_preset_id = sql_save($save, 'graph_templates_gprint'); $hash_cache['gprint_preset'][$hash] = $gprint_preset_id; } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_gprint_preset_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($gprint_preset_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_data_source_profile($hash, &$xml_array, &$hash_cache, $import_as_new, $profile_id) { global $fields_profile_edit, $fields_profile_rra_edit, $import_template_id, $preview_only; if ($import_as_new == true) { $save['id'] = 0; $save['hash'] = get_hash_data_source_profile(0); reset($fields_profile_edit); while (list($field_name, $field_array) = each($fields_profile_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } // Give the Profile a new name $save['name'] .= ' (imported)'; if (!$preview_only) { $dsp_id = sql_save($save, 'data_source_profiles'); if (!empty($dsp_id)) { db_execute_prepared('UPDATE data_template_data SET data_source_profile_id = ? WHERE id = ?', array($dsp_id, $import_template_id)); } $hash_cache['data_source_profiles'][$hash] = $dsp_id; /* import into: data_source_profiles_cf */ $hash_items = explode('|', $xml_array['cf_items']); if (!empty($hash_items[0])) { for ($i=0; $i 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($host_template_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_cdef($hash, &$xml_array, &$hash_cache) { global $fields_cdef_edit, $preview_only; /* track changes */ $status = 0; $fields_cdef_item_edit = array( 'sequence' => 'sequence', 'type' => 'type', 'value' => 'value' ); /* import into: cdef */ $_cdef_id = db_fetch_cell_prepared('SELECT id FROM cdef WHERE hash = ?', array($hash)); if (!empty($_cdef_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM cdef WHERE id = ?', array($_cdef_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_cdef_id) ? '0' : $_cdef_id); $save['hash'] = $hash; reset($fields_cdef_edit); while (list($field_name, $field_array) = each($fields_cdef_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'cdef'); if (!$preview_only) { $cdef_id = sql_save($save, 'cdef'); $hash_cache['cdef'][$hash] = $cdef_id; }else{ $cdef_id = $_cdef_id; } /* import into: cdef_items */ if (is_array($xml_array['items'])) { while (list($item_hash, $item_array) = each($xml_array['items'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_cdef_item_id = db_fetch_cell_prepared('SELECT id FROM cdef_items WHERE hash= ? AND cdef_id = ?', array($parsed_hash['hash'], $cdef_id)); if (!empty($_cdef_item_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM cdef_items WHERE id = ?', array($_cdef_item_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_cdef_item_id) ? '0' : $_cdef_item_id); $save['hash'] = $parsed_hash['hash']; $save['cdef_id'] = $cdef_id; reset($fields_cdef_item_edit); while (list($field_name, $field_array) = each($fields_cdef_item_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { /* check, if an inherited cdef as to be decoded (value == 5) * this whole procedure relies on the sequence during template export * inherited cdef's must come first, this has to be taken care of during export * so we do not have any specific dependency checks here */ if (($field_name == 'value') && ($item_array['type'] == '5')) { /* parse information from the hash, which in this case * is stored as a value of the current item being processed */ $parsed_item_hash = parse_xml_hash($item_array['value']); /* invalid/wrong hash */ if ($parsed_item_hash == false) { return false; } $_cdef_id = db_fetch_cell_prepared('SELECT id FROM cdef WHERE hash = ?', array($parsed_item_hash['hash'])); $save[$field_name] = $_cdef_id; } else { $save[$field_name] = xml_character_decode($item_array[$field_name]); } } } /* check for status changes */ $status += compare_data($save, $previous_data, 'cdef_items'); if (!$preview_only) { $cdef_item_id = sql_save($save, 'cdef_items'); $hash_cache['cdef_item']{$parsed_hash['hash']} = $cdef_item_id; } } } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_cdef_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($cdef_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_vdef($hash, &$xml_array, &$hash_cache) { global $config, $preview_only; include_once($config['library_path'] . 'vdef.php'); /* track changes */ $status = 0; /* import into: vdef */ $_vdef_id = db_fetch_cell_prepared('SELECT id FROM vdef WHERE hash = ?', array($hash)); if (!empty($_vdef_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM vdef WHERE id = ?', array($_vdef_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_vdef_id) ? '0' : $_vdef_id); $save['hash'] = $hash; $fields_vdef_edit = preset_vdef_form_list(); reset($fields_vdef_edit); while (list($field_name, $field_array) = each($fields_vdef_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'vdef'); if (!$preview_only) { $vdef_id = sql_save($save, 'vdef'); $hash_cache['vdef'][$hash] = $vdef_id; }else{ $vdef_id = $_vdef_id; } /* import into: vdef_items */ if (is_array($xml_array['items'])) { while (list($item_hash, $item_array) = each($xml_array['items'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_vdef_item_id = db_fetch_cell_prepared('SELECT id FROM vdef_items WHERE hash = ? AND vdef_id = ?', array($parsed_hash['hash'], $vdef_id)); if (!empty($_vdef_item_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM vdef_items WHERE id = ?', array($_vdef_item_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_vdef_item_id) ? '0' : $_vdef_item_id); $save['hash'] = $parsed_hash['hash']; $save['vdef_id'] = $vdef_id; $fields_vdef_item_edit = preset_vdef_item_form_list(); reset($fields_vdef_item_edit); while (list($field_name, $field_array) = each($fields_vdef_item_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { $save[$field_name] = xml_character_decode($item_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'vdef_items'); if (!$preview_only) { $vdef_item_id = sql_save($save, 'vdef_items'); $hash_cache['vdef_item']{$parsed_hash['hash']} = $vdef_item_id; } } } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_vdef_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($vdef_id) ? 'fail' : 'success')); return $hash_cache; } function xml_to_data_input_method($hash, &$xml_array, &$hash_cache) { global $fields_data_input_edit, $fields_data_input_field_edit, $fields_data_input_field_edit_1, $preview_only; /* track changes */ $status = 0; /* aggregate field arrays */ $fields_data_input_field_edit += $fields_data_input_field_edit_1; /* import into: data_input */ $_data_input_id = db_fetch_cell_prepared('SELECT id FROM data_input WHERE hash = ?', array($hash)); if (!empty($_data_input_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM data_input WHERE id = ?', array($_data_input_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_input_id) ? '0' : $_data_input_id); $save['hash'] = $hash; reset($fields_data_input_edit); while (list($field_name, $field_array) = each($fields_data_input_edit)) { /* make sure this field exists in the xml array first */ if (isset($xml_array[$field_name])) { /* fix issue with data input method importing and white spaces */ if ($field_name == 'input_string') { $xml_array[$field_name] = str_replace('><', '> <', $xml_array[$field_name]); } $save[$field_name] = xml_character_decode($xml_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'data_input'); if (!$preview_only) { $data_input_id = sql_save($save, 'data_input'); $hash_cache['data_input_method'][$hash] = $data_input_id; update_replication_crc(0, 'poller_replicate_data_input_crc'); }else{ $data_input_id = $_data_input_id; } /* import into: data_input_fields */ if (is_array($xml_array['fields'])) { while (list($item_hash, $item_array) = each($xml_array['fields'])) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($item_hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } unset($save); $_data_input_field_id = db_fetch_cell_prepared('SELECT id FROM data_input_fields WHERE hash= ? AND data_input_id = ?', array($parsed_hash['hash'], $data_input_id)); if (!empty($_data_input_field_id)) { $previous_data = db_fetch_row_prepared('SELECT * FROM data_input_fields WHERE id = ?', array($_data_input_field_id)); }else{ $previous_data = array(); } $save['id'] = (empty($_data_input_field_id) ? '0' : $_data_input_field_id); $save['hash'] = $parsed_hash['hash']; $save['data_input_id'] = $data_input_id; reset($fields_data_input_field_edit); while (list($field_name, $field_array) = each($fields_data_input_field_edit)) { /* make sure this field exists in the xml array first */ if (isset($item_array[$field_name])) { $save[$field_name] = xml_character_decode($item_array[$field_name]); } } /* check for status changes */ $status += compare_data($save, $previous_data, 'data_input_fields'); if (!$preview_only) { $data_input_field_id = sql_save($save, 'data_input_fields'); $hash_cache['data_input_field']{$parsed_hash['hash']} = $data_input_field_id; update_replication_crc(0, 'poller_replicate_data_input_fields_crc'); } } } /* update field use counter cache if possible */ if (!$preview_only) { if ((isset($xml_array['input_string'])) && (!empty($data_input_id))) { generate_data_input_field_sequences($xml_array['input_string'], $data_input_id); } } /* status information that will be presented to the user */ $_SESSION['import_debug_info']['type'] = (empty($_data_input_id) ? 'new' : ($status > 0 ? 'updated':'unchanged')); $_SESSION['import_debug_info']['title'] = $xml_array['name']; $_SESSION['import_debug_info']['result'] = ($preview_only ? 'preview':(empty($data_input_id) ? 'fail' : 'success')); return $hash_cache; } function compare_data($save, $previous_data, $table) { global $preview_only; if ($preview_only) { $ignores = array( 'task_item_id', 'gprint_id', 'cdef_id', 'vdef_id', 'consolidation_function_id', 'data_input_id', 'graph_template_id', 'data_template_id', 'data_input_field_id' ); }else{ $ignores = array(); } if (!sizeof($previous_data)) { return 0; }else{ $different = 0; foreach($save as $column => $value) { if (array_search($column, $ignores) !== false) continue; if ($previous_data[$column] != $value) { $different++; $_SESSION['import_debug_info']['differences'][] = 'Table: ' . $table . ', Column: ' . $column . ', New Value: \'' . $value . '\', Old Value: \'' . $previous_data[$column] . '\''; } } return $different; } } function hash_to_friendly_name($hash, $display_type_name) { global $hash_type_names; /* parse information from the hash */ $parsed_hash = parse_xml_hash($hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } if ($display_type_name == true) { $prepend = '(' . $hash_type_names{$parsed_hash['type']} . ') '; }else{ $prepend = ''; } switch ($parsed_hash['type']) { case 'graph_template': return $prepend . db_fetch_cell_prepared('SELECT name FROM graph_templates WHERE hash = ?', array($parsed_hash['hash'])); case 'data_template': return $prepend . db_fetch_cell_prepared('SELECT name FROM data_template WHERE hash = ?', array($parsed_hash['hash'])); case 'data_template_item': return $prepend . db_fetch_cell_prepared('SELECT data_source_name FROM data_template_rrd WHERE hash = ?', array($parsed_hash['hash'])); case 'host_template': return $prepend . db_fetch_cell_prepared('SELECT name FROM host_template WHERE hash = ?', array($parsed_hash['hash'])); case 'data_input_method': return $prepend . db_fetch_cell_prepared('SELECT name FROM data_input WHERE hash = ?', array($parsed_hash['hash'])); case 'data_input_field': return $prepend . db_fetch_cell_prepared('SELECT name FROM data_input_fields WHERE hash = ?', array($parsed_hash['hash'])); case 'data_query': return $prepend . db_fetch_cell_prepared('SELECT name FROM snmp_query WHERE hash = ?', array($parsed_hash['hash'])); case 'gprint_preset': return $prepend . db_fetch_cell_prepared('SELECT name FROM graph_templates_gprint WHERE hash = ?', array($parsed_hash['hash'])); case 'cdef': return $prepend . db_fetch_cell_prepared('SELECT name FROM cdef WHERE hash = ?', array($parsed_hash['hash'])); case 'vdef': return $prepend . db_fetch_cell_prepared('SELECT name FROM vdef WHERE hash = ?', array($parsed_hash['hash'])); case 'data_source_profile': return $prepend . db_fetch_cell_prepared('SELECT name FROM data_source_profile WHERE hash = ?', array($parsed_hash['hash'])); case 'round_robin_archive': return $prepend; } } function resolve_hash_to_id($hash, &$hash_cache_array) { /* parse information from the hash */ $parsed_hash = parse_xml_hash($hash); /* invalid/wrong hash */ if ($parsed_hash == false) { return false; } if (isset($hash_cache_array{$parsed_hash['type']}{$parsed_hash['hash']})) { $_SESSION['import_debug_info']['dep'][$hash] = 'met'; return $hash_cache_array{$parsed_hash['type']}{$parsed_hash['hash']}; }else{ $_SESSION['import_debug_info']['dep'][$hash] = 'unmet'; return 0; } } function parse_xml_hash($hash) { if (preg_match('/hash_([a-f0-9]{2})([a-f0-9]{4})([a-f0-9]{32})/', $hash, $matches)) { $parsed_hash['type'] = check_hash_type($matches[1]); $parsed_hash['version'] = strval(check_hash_version($matches[2])); $parsed_hash['hash'] = $matches[3]; /* an error has occurred */ if (($parsed_hash['type'] === false) || ($parsed_hash['version'] === false)) { return false; } }else{ cacti_log(__FUNCTION__ . ' ERROR wrong hash format', false); return false; } return $parsed_hash; } function check_hash_type($hash_type) { global $hash_type_codes; /* lets not mess up the pointer for other people */ $local_hash_type_codes = $hash_type_codes; reset($local_hash_type_codes); while (list($type, $code) = each($local_hash_type_codes)) { if ($code == $hash_type) { $current_type = $type; } } if (!isset($current_type)) { raise_message(18); /* error: cannot find type */ return false; } return $current_type; } function check_hash_version($hash_version) { global $hash_version_codes, $config; $i = 0; reset($hash_version_codes); while (list($version, $code) = each($hash_version_codes)) { if ($version == $config['cacti_version']) { $current_version_index = $i; } if ($code == $hash_version) { $hash_version_index = $i; $current_version = $version; } $i++; } if (!isset($current_version_index)) { raise_message(15); /* error: current cacti version does not exist! */ return false; }elseif (!isset($hash_version_index)) { raise_message(16); /* error: hash version does not exist! */ return false; }elseif ($hash_version_index > $current_version_index) { raise_message(17); /* error: hash made with a newer version of cacti */ return false; } return $current_version; } function get_version_index($string_version) { global $hash_version_codes; $i = 0; reset($hash_version_codes); while (list($version, $code) = each($hash_version_codes)) { if ($string_version == $version) { return $i; } $i++; } /* version index not found */ return -1; } function xml_character_decode($text) { if (function_exists('html_entity_decode')) { return html_entity_decode($text, ENT_QUOTES, 'UTF-8'); } else { $trans_tbl = get_html_translation_table(HTML_ENTITIES); $trans_tbl = array_flip($trans_tbl); return strtr($text, $trans_tbl); } }