1dd6e2d
diff -ruBbd 0.8.7e/cli/repair_templates.php 0.8.7/cli/repair_templates.php
1dd6e2d
--- 0.8.7e/cli/repair_templates.php	2009-08-18 22:03:22.000000000 -0400
1dd6e2d
+++ 0.8.7/cli/repair_templates.php	2009-08-20 07:43:54.000000000 -0400
1dd6e2d
@@ -0,0 +1,135 @@
1dd6e2d
+
1dd6e2d
+/*
1dd6e2d
+ +-------------------------------------------------------------------------+
1dd6e2d
+ | Copyright (C) 2004-2009 The Cacti Group                                 |
1dd6e2d
+ |                                                                         |
1dd6e2d
+ | This program is free software; you can redistribute it and/or           |
1dd6e2d
+ | modify it under the terms of the GNU General Public License             |
1dd6e2d
+ | as published by the Free Software Foundation; either version 2          |
1dd6e2d
+ | of the License, or (at your option) any later version.                  |
1dd6e2d
+ |                                                                         |
1dd6e2d
+ | This program is distributed in the hope that it will be useful,         |
1dd6e2d
+ | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
1dd6e2d
+ | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
1dd6e2d
+ | GNU General Public License for more details.                            |
1dd6e2d
+ +-------------------------------------------------------------------------+
1dd6e2d
+ | Cacti: The Complete RRDTool-based Graphing Solution                     |
1dd6e2d
+ +-------------------------------------------------------------------------+
1dd6e2d
+ | This code is designed, written, and maintained by the Cacti Group. See  |
1dd6e2d
+ | about.php and/or the AUTHORS file for specific developer information.   |
1dd6e2d
+ +-------------------------------------------------------------------------+
1dd6e2d
+ | http://www.cacti.net/                                                   |
1dd6e2d
+ +-------------------------------------------------------------------------+
1dd6e2d
+*/
1dd6e2d
+
1dd6e2d
+/* do NOT run this script through a web browser */
1dd6e2d
+if (!isset($_SERVER["argv"][0]) || isset($_SERVER['REQUEST_METHOD'])  || isset($_SERVER['REMOTE_ADDR'])) {
1dd6e2d
+	die("
This script is only meant to run at the command line.");
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+$no_http_headers = true;
1dd6e2d
+
1dd6e2d
+include(dirname(__FILE__) . "/../include/global.php");
1dd6e2d
+include_once("../lib/utility.php");
1dd6e2d
+include_once("../lib/template.php");
1dd6e2d
+
1dd6e2d
+/* process calling arguments */
1dd6e2d
+$parms = $_SERVER["argv"];
1dd6e2d
+array_shift($parms);
1dd6e2d
+
1dd6e2d
+$execute = FALSE;
1dd6e2d
+
1dd6e2d
+foreach($parms as $parameter) {
1dd6e2d
+	@list($arg, $value) = @explode("=", $parameter);
1dd6e2d
+
1dd6e2d
+	switch ($arg) {
1dd6e2d
+	case "--execute":
1dd6e2d
+		$execute = TRUE;
1dd6e2d
+		break;
1dd6e2d
+	case "-h":
1dd6e2d
+	case "-v":
1dd6e2d
+	case "-V":
1dd6e2d
+	case "--version":
1dd6e2d
+	case "--help":
1dd6e2d
+		display_help();
1dd6e2d
+		exit;
1dd6e2d
+	default:
1dd6e2d
+		print "ERROR: Invalid Parameter " . $parameter . "\n\n";
1dd6e2d
+		display_help();
1dd6e2d
+		exit;
1dd6e2d
+	}
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+if ($execute) {
1dd6e2d
+	echo "NOTE: Repairing All Duplicated Templates\n";
1dd6e2d
+} else {
1dd6e2d
+	echo "NOTE: Performing Check of Templates\n";
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+/* repair data templates first */
1dd6e2d
+if ($execute) {
1dd6e2d
+	echo "NOTE: Repairing Data Templates\n";
1dd6e2d
+} else {
1dd6e2d
+	echo "NOTE: Performing Check of Data Templates\n";
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+$damaged_template_ids = db_fetch_assoc("SELECT DISTINCT data_template_id FROM data_template_rrd WHERE hash='' AND local_data_id=0");
1dd6e2d
+if (sizeof($damaged_template_ids)) {
1dd6e2d
+	foreach($damaged_template_ids as $id) {
		$template_name = db_fetch_cell("SELECT name FROM data_template WHERE id=" . $id["data_template_id"]);
1dd6e2d
+		echo "NOTE: Data Template '$template_name' is Damaged and can be repaired\n";
1dd6e2d
+	}
1dd6e2d
+
1dd6e2d
+	$damaged_templates = db_fetch_assoc("SELECT * FROM data_template_rrd WHERE hash='' AND local_data_id=0");
1dd6e2d
+	if (sizeof($damaged_templates)) {
1dd6e2d
+		echo "NOTE: -- Damaged Data Templates Objects Found is '" . sizeof($damaged_templates) . "'\n";
1dd6e2d
+		if ($execute) {
1dd6e2d
+			foreach($damaged_templates as $template) {
1dd6e2d
+				$hash = get_hash_data_template($template["local_data_template_rrd_id"], "data_template_item");
1dd6e2d
+				db_execute("UPDATE data_template_rrd SET hash='$hash' WHERE id=" . $template["id"]);
1dd6e2d
+			}
1dd6e2d
+		}
1dd6e2d
+	}
1dd6e2d
+} else {
1dd6e2d
+	echo "NOTE: No Damaged Data Templates Found\n";
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+/* reset the array */
1dd6e2d
+$damaged_templates = array();
1dd6e2d
+
1dd6e2d
+/* repair graph templates */
1dd6e2d
+if ($execute) {
1dd6e2d
+	echo "NOTE: Repairing Graph Templates\n";
1dd6e2d
+} else {
1dd6e2d
+	echo "NOTE: Performing Check of Graph Templates\n";
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+$damaged_template_ids = db_fetch_assoc("SELECT DISTINCT graph_template_id FROM graph_template_input WHERE hash=''");
1dd6e2d
+if (sizeof($damaged_template_ids)) {
1dd6e2d
+	foreach($damaged_template_ids as $id) {
1dd6e2d
+		$template_name = db_fetch_cell("SELECT name FROM graph_templates WHERE id=" . $id["graph_template_id"]);
1dd6e2d
+		echo "NOTE: Graph Template '$template_name' is Damaged and can be repaired\n";
1dd6e2d
+	}
1dd6e2d
+
1dd6e2d
+	$damaged_templates = db_fetch_assoc("SELECT * FROM graph_template_input WHERE hash=''");
1dd6e2d
+	if (sizeof($damaged_templates)) {
1dd6e2d
+		echo "NOTE: -- Damaged Graph Templates Objects Found is '" . sizeof($damaged_templates) . "'\n";
1dd6e2d
+		if ($execute) {
1dd6e2d
+			foreach($damaged_templates as $template) {
1dd6e2d
+				$hash = get_hash_graph_template(0, "graph_template_input");
1dd6e2d
+				db_execute("UPDATE graph_template_input SET hash='$hash' WHERE id=" . $template["id"]);
1dd6e2d
+			}
1dd6e2d
+		}
1dd6e2d
+	}
1dd6e2d
+} else {
1dd6e2d
+	echo "NOTE: No Damaged Graph Templates Found\n";
1dd6e2d
+}
1dd6e2d
+
1dd6e2d
+
1dd6e2d
+/* display_help - displays the usage of the function */
1dd6e2d
+function display_help () {
1dd6e2d
+	print "Cacti Database Template Repair Tool v1.0, Copyright 2004-2009 - The Cacti Group\n\n";
1dd6e2d
+	print "usage: repair_templates.php --execute [--help]\n\n";
1dd6e2d
+	print "--execute        - Perform the repair\n";
1dd6e2d
+	print "--help           - display this help message\n";
1dd6e2d
+}
1dd6e2d
+?>
1dd6e2d
diff -ruBbd 0.8.7e/docs/README 0.8.7/docs/README
1dd6e2d
--- 0.8.7e/docs/README	2009-08-18 21:57:30.000000000 -0400
1dd6e2d
+++ 0.8.7/docs/README	2009-08-18 21:58:09.000000000 -0400
1dd6e2d
@@ -90,6 +90,9 @@
1dd6e2d
                             table
1dd6e2d
   poller_reindex_hosts.php - Cause data query reindex on hosts
1dd6e2d
   rebuild_poller_cache.php - Rebuilds the poller cache
1dd6e2d
+  repair_templates.php     - Certain templates, when created using the "duplicate"
1dd6e2d
+      function in Cacti, do not import/export well.  This utility repairs 
1dd6e2d
+      those templates.
1dd6e2d
 
1dd6e2d
 
1dd6e2d
 
1dd6e2d
diff -ruBbd 0.8.7e/lib/export.php 0.8.7/lib/export.php
1dd6e2d
--- 0.8.7e/lib/export.php	2009-08-18 21:56:47.000000000 -0400
1dd6e2d
+++ 0.8.7/lib/export.php	2009-08-18 21:57:50.000000000 -0400
1dd6e2d
@@ -811,7 +811,9 @@
1dd6e2d
 }
1dd6e2d
 
1dd6e2d
 function xml_character_encode($text) {
1dd6e2d
-
1dd6e2d
+	if (function_exists("htmlspecialchars")) {
1dd6e2d
+		return htmlspecialchars($text, ENT_QUOTES, "UTF-8");
1dd6e2d
+	} else {
1dd6e2d
 	$text = str_replace("&", "&", $text);
1dd6e2d
 	$text = str_replace(">", ">", $text);
1dd6e2d
 	$text = str_replace("<", "<", $text);
1dd6e2d
@@ -819,6 +821,7 @@
1dd6e2d
 	$text = str_replace("\'", "'", $text);
1dd6e2d
 
1dd6e2d
 	return $text;
1dd6e2d
+	}
1dd6e2d
 }
1dd6e2d
 
1dd6e2d
 ?>
1dd6e2d
diff -ruBbd 0.8.7e/lib/import.php 0.8.7/lib/import.php
1dd6e2d
--- 0.8.7e/lib/import.php	2009-08-18 21:56:59.000000000 -0400
1dd6e2d
+++ 0.8.7/lib/import.php	2009-08-18 21:57:55.000000000 -0400
1dd6e2d
@@ -36,10 +36,6 @@
1dd6e2d
 		return $info_array;
1dd6e2d
 	}
1dd6e2d
 
1dd6e2d
-	if (isset($xml_array["name"])) {
1dd6e2d
-		$xml_array["name"] = htmlspecialchars($xml_array["name"]);
1dd6e2d
-	}
1dd6e2d
-
1dd6e2d
 	while (list($hash, $hash_array) = each($xml_array)) {
1dd6e2d
 		/* parse information from the hash */
1dd6e2d
 		$parsed_hash = parse_xml_hash($hash);
1dd6e2d
@@ -115,7 +111,7 @@
1dd6e2d
 	$_graph_template_id = db_fetch_cell("select id from graph_templates where hash='$hash'");
1dd6e2d
 	$save["id"] = (empty($_graph_template_id) ? "0" : $_graph_template_id);
1dd6e2d
 	$save["hash"] = $hash;
1dd6e2d
-	$save["name"] = htmlspecialchars($xml_array["name"]);
1dd6e2d
+	$save["name"] = $xml_array["name"];
1dd6e2d
 	$graph_template_id = sql_save($save, "graph_templates");
1dd6e2d
 
1dd6e2d
 	$hash_cache["graph_template"][$hash] = $graph_template_id;
1dd6e2d
@@ -914,9 +910,13 @@
1dd6e2d
 }
1dd6e2d
 
1dd6e2d
 function xml_character_decode($text) {
1dd6e2d
+	if (function_exists("html_entity_decode")) {
1dd6e2d
+		return html_entity_decode($text, ENT_QUOTES, "UTF-8");
1dd6e2d
+	} else {
1dd6e2d
 	$trans_tbl = get_html_translation_table(HTML_ENTITIES);
1dd6e2d
 	$trans_tbl = array_flip($trans_tbl);
1dd6e2d
 	return strtr($text, $trans_tbl);
1dd6e2d
+	}
1dd6e2d
 }
1dd6e2d
 
1dd6e2d
 ?>
1dd6e2d
diff -ruBbd 0.8.7e/lib/utility.php 0.8.7/lib/utility.php
1dd6e2d
--- 0.8.7e/lib/utility.php	2009-08-18 21:57:08.000000000 -0400
1dd6e2d
+++ 0.8.7/lib/utility.php	2009-08-18 21:58:00.000000000 -0400
1dd6e2d
@@ -346,6 +346,7 @@
1dd6e2d
 			$save["name"] = $graph_template_input["name"];
1dd6e2d
 			$save["description"] = $graph_template_input["description"];
1dd6e2d
 			$save["column_name"] = $graph_template_input["column_name"];
1dd6e2d
+			$save["hash"]              = get_hash_graph_template(0, "graph_template_input");
1dd6e2d
 
1dd6e2d
 			$graph_template_input_id = sql_save($save, "graph_template_input");
1dd6e2d
 
1dd6e2d
@@ -436,6 +437,11 @@
1dd6e2d
 		$save["local_data_id"] = (isset($local_data_id) ? $local_data_id : 0);
1dd6e2d
 		$save["local_data_template_rrd_id"] = (isset($data_template_rrd["local_data_template_rrd_id"]) ? $data_template_rrd["local_data_template_rrd_id"] : 0);
1dd6e2d
 		$save["data_template_id"] = (!empty($_local_data_id) ? $data_template_rrd["data_template_id"] : $data_template_id);
1dd6e2d
+		if ($save["local_data_id"] == 0) {
1dd6e2d
+			$save["hash"]                   = get_hash_data_template($data_template_rrd["local_data_template_rrd_id"], "data_template_item");
1dd6e2d
+		} else {
1dd6e2d
+			$save["hash"] = '';
1dd6e2d
+		}
1dd6e2d
 
1dd6e2d
 		while (list($field, $array) = each($struct_data_source_item)) {
1dd6e2d
 			$save{$field} = $data_template_rrd{$field};