File: import.php

package info (click to toggle)
cacti 0.8.6c-7sarge5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,424 kB
  • ctags: 8,367
  • sloc: php: 35,994; sh: 4,033; sql: 2,357; xml: 680; perl: 109; makefile: 87
file content (887 lines) | stat: -rw-r--r-- 34,157 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004 Ian Berry                                            |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | cacti: a php-based graphing solution                                    |
 +-------------------------------------------------------------------------+
 | Most of this code has been designed, written and is maintained by       |
 | Ian Berry. See about.php for specific developer credit. Any questions   |
 | or comments regarding this code should be directed to:                  |
 | - iberry@raxnet.net                                                     |
 +-------------------------------------------------------------------------+
 | - raXnet - http://www.raxnet.net/                                       |
 +-------------------------------------------------------------------------+
*/

function &import_xml_data(&$xml_data) {
	global $config, $hash_type_codes, $hash_version_codes;

	include_once($config["library_path"] . "/xml.php");

	$xml_array = xml2array($xml_data);

	if (sizeof($xml_array) == 0) {
		raise_message(7); /* xml parse error */
		return;
	}

	while (list($hash, $hash_array) = each($xml_array)) {
		/* parse information from the hash */
		$parsed_hash = parse_xml_hash($hash);

		/* invalid/wrong hash */
		if ($parsed_hash == false) { return ; }

		if (isset($dep_hash_cache{$parsed_hash["type"]})) {
			array_push($dep_hash_cache{$parsed_hash["type"]}, $parsed_hash);
		}else{
			$dep_hash_cache{$parsed_hash["type"]} = array($parsed_hash);
		}
	}

	$hash_cache = array();

	/* 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 dependenecies 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<count($dep_hash_cache[$type]); $i++) {
				$hash_array = $xml_array{"hash_" . $hash_type_codes{$dep_hash_cache[$type][$i]["type"]} . $hash_version_codes{$dep_hash_cache[$type][$i]["version"]} . $dep_hash_cache[$type][$i]["hash"]};

				switch($type) {
				case 'graph_template':
					$hash_cache += xml_to_graph_template($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'data_template':
					$hash_cache += xml_to_data_template($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'host_template':
					$hash_cache += xml_to_host_template($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'data_input_method':
					$hash_cache += xml_to_data_input_method($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'data_query':
					$hash_cache += xml_to_data_query($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'gprint_preset':
					$hash_cache += xml_to_gprint_preset($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'cdef':
					$hash_cache += xml_to_cdef($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				case 'round_robin_archive':
					$hash_cache += xml_to_round_robin_archive($dep_hash_cache[$type][$i]["hash"], $hash_array, $hash_cache);
					break;
				}

				$info_array[$type]{isset($info_array[$type]) ? count($info_array[$type]) : 0} = $_SESSION["import_debug_info"];

				kill_session_var("import_debug_info");
			}
		}
	}

	return $info_array;
}

function &xml_to_graph_template($hash, &$xml_array, &$hash_cache) {
	global $struct_graph, $struct_graph_item, $fields_graph_template_input_edit, $hash_version_codes;

	/* import into: graph_templates */
	$_graph_template_id = db_fetch_cell("select id from graph_templates where hash='$hash'");
	$save["id"] = (empty($_graph_template_id) ? "0" : $_graph_template_id);
	$save["hash"] = $hash;
	$save["name"] = $xml_array["name"];
	$graph_template_id = sql_save($save, "graph_templates");

	$hash_cache["graph_template"][$hash] = $graph_template_id;

	/* import into: graph_templates_graph */
	unset($save);
	$save["id"] = (empty($_graph_template_id) ? "0" : db_fetch_cell("select graph_templates_graph.id from graph_templates,graph_templates_graph where graph_templates.id=graph_templates_graph.graph_template_id and graph_templates.id=$graph_template_id and graph_templates_graph.local_graph_id=0"));
	$save["graph_template_id"] = $graph_template_id;

	/* parse information from the hash */
	$parsed_hash = parse_xml_hash($hash);

	reset($struct_graph);
	while (list($field_name, $field_array) = each($struct_graph)) {
		/* make sure this field exists in the xml array first */
		if (isset($xml_array["graph"]{"t_" . $field_name})) {
			$save{"t_" . $field_name} = $xml_array["graph"]{"t_" . $field_name};
		}

		/* make sure this field exists in the xml array first */
		if (isset($xml_array["graph"][$field_name])) {
			if (($field_name == "unit_exponent_value") && (get_version_index($parsed_hash["version"]) < get_version_index("0.8.5")) && ($xml_array["graph"][$field_name] == "0")) { /* backwards compatability */
				$save[$field_name] = "";
			}else{
				$save[$field_name] = addslashes($xml_array["graph"][$field_name]);
			}
		}
	}

	$graph_template_graph_id = sql_save($save, "graph_templates_graph");

	/* import into: graph_templates_item */
	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);
			$_graph_template_item_id = db_fetch_cell("select id from graph_templates_item where hash='" . $parsed_hash["hash"] . "' and graph_template_id=$graph_template_id and local_graph_id=0");
			$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 (ereg("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") && (ereg("^[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("select id from colors where hex='" . $item_array[$field_name] . "'");

						if (empty($color_id)) {
							db_execute("insert into colors (hex) values ('" . $item_array[$field_name] . "')");
							$color_id = db_fetch_insert_id();
						}

						$save[$field_name] = $color_id;
					}else{
						$save[$field_name] = addslashes($item_array[$field_name]);
					}
				}
			}

			$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("select id from graph_template_input where hash='" . $parsed_hash["hash"] . "' and graph_template_id=$graph_template_id");
			$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] = addslashes($item_array[$field_name]);
				}
			}

			$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<count($hash_items); $i++) {
					/* parse information from the hash */
					$parsed_hash = parse_xml_hash($hash_items[$i]);

					/* invalid/wrong hash */
					if ($parsed_hash == false) { return false; }

					if (isset($hash_cache["graph_template_item"]{$parsed_hash["hash"]})) {
						db_execute("replace into graph_template_input_defs (graph_template_input_id,graph_template_item_id) values ($graph_template_input_id," . $hash_cache["graph_template_item"]{$parsed_hash["hash"]} . ")");
					}
				}
			}
		}
	}

	/* status information that will be presented to the user */
	$_SESSION["import_debug_info"]["type"] = (empty($_graph_template_id) ? "new" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($graph_template_id) ? "fail" : "success");

	return $hash_cache;
}

function &xml_to_data_template($hash, &$xml_array, &$hash_cache) {
	global $struct_data_source, $struct_data_source_item;

	/* import into: data_template */
	$_data_template_id = db_fetch_cell("select id from data_template where hash='$hash'");
	$save["id"] = (empty($_data_template_id) ? "0" : $_data_template_id);
	$save["hash"] = $hash;
	$save["name"] = $xml_array["name"];

	$data_template_id = sql_save($save, "data_template");

	$hash_cache["data_template"][$hash] = $data_template_id;

	/* import into: data_template_data */
	unset($save);
	$save["id"] = (empty($_data_template_id) ? "0" : db_fetch_cell("select data_template_data.id from data_template,data_template_data where data_template.id=data_template_data.data_template_id and data_template.id=$data_template_id and data_template_data.local_data_id=0"));
	$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 (ereg("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] = addslashes($xml_array["ds"][$field_name]);
			}
		}
	}

	$data_template_data_id = sql_save($save, "data_template_data");

	/* import into: data_template_data_rra */
	$hash_items = explode("|", $xml_array["ds"]["rra_items"]);

	if (!empty($hash_items[0])) {
		for ($i=0; $i<count($hash_items); $i++) {
			/* parse information from the hash */
			$parsed_hash = parse_xml_hash($hash_items[$i]);

			/* invalid/wrong hash */
			if ($parsed_hash == false) { return false; }

			if (isset($hash_cache["round_robin_archive"]{$parsed_hash["hash"]})) {
				db_execute("replace into data_template_data_rra (data_template_data_id,rra_id) values ($data_template_data_id," . $hash_cache["round_robin_archive"]{$parsed_hash["hash"]} . ")");
			}
		}
	}

	/* 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("select id from data_template_rrd where hash='" . $parsed_hash["hash"] . "' and data_template_id=$data_template_id and local_data_id=0");
			$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 (ereg("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] = addslashes($item_array[$field_name]);
					}
				}
			}

			$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 (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"] = $item_array["value"];

			sql_save($save, "data_input_data", array("data_template_data_id", "data_input_field_id"));
		}
	}

	/* status information that will be presented to the user */
	$_SESSION["import_debug_info"]["type"] = (empty($_data_template_id) ? "new" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (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;

	/* import into: snmp_query */
	$_data_query_id = db_fetch_cell("select id from snmp_query where hash='$hash'");
	$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 (ereg("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] = addslashes(xml_character_decode($xml_array[$field_name]));
			}
		}
	}

	$data_query_id = sql_save($save, "snmp_query");

	$hash_cache["data_query"][$hash] = $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("select id from snmp_query_graph where hash='" . $parsed_hash["hash"] . "' and snmp_query_id=$data_query_id");
			$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 (ereg("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] = addslashes($item_array[$field_name]);
					}
				}
			}

			$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"));
				}
			}

			/* 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("select id from snmp_query_graph_sv where hash='" . $parsed_hash["hash"] . "' and snmp_query_graph_id=$data_query_graph_id");
					$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"] = $sub_item_array["text"];

					$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("select id from snmp_query_graph_rrd_sv where hash='" . $parsed_hash["hash"] . "' and snmp_query_graph_id=$data_query_graph_id");
					$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"] = $sub_item_array["text"];

					$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" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($data_query_id) ? "fail" : "success");

	return $hash_cache;
}

function &xml_to_gprint_preset($hash, &$xml_array, &$hash_cache) {
	global $fields_grprint_presets_edit;

	/* import into: graph_templates_gprint */
	$_gprint_preset_id = db_fetch_cell("select id from graph_templates_gprint where hash='$hash'");
	$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] = addslashes($xml_array[$field_name]);
		}
	}

	$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" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($gprint_preset_id) ? "fail" : "success");

	return $hash_cache;
}

function &xml_to_round_robin_archive($hash, &$xml_array, &$hash_cache) {
	global $fields_rra_edit;

	/* import into: rra */
	$_rra_id = db_fetch_cell("select id from rra where hash='$hash'");
	$save["id"] = (empty($_rra_id) ? "0" : $_rra_id);
	$save["hash"] = $hash;

	reset($fields_rra_edit);
	while (list($field_name, $field_array) = each($fields_rra_edit)) {
		/* make sure this field exists in the xml array first */
		if (isset($xml_array[$field_name])) {
			$save[$field_name] = addslashes($xml_array[$field_name]);
		}
	}

	$rra_id = sql_save($save, "rra");

	$hash_cache["round_robin_archive"][$hash] = $rra_id;

	/* import into: rra_cf */
	$hash_items = explode("|", $xml_array["cf_items"]);

	if (!empty($hash_items[0])) {
		for ($i=0; $i<count($hash_items); $i++) {
			db_execute("replace into rra_cf (rra_id,consolidation_function_id) values ($rra_id," . $hash_items[$i] . ")");
		}
	}

	/* status information that will be presented to the user */
	$_SESSION["import_debug_info"]["type"] = (empty($_rra_id) ? "new" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($rra_id) ? "fail" : "success");

	return $hash_cache;
}

function &xml_to_host_template($hash, &$xml_array, &$hash_cache) {
	global $fields_host_template_edit;

	/* import into: graph_templates_gprint */
	$_host_template_id = db_fetch_cell("select id from host_template where hash='$hash'");
	$save["id"] = (empty($_host_template_id) ? "0" : $_host_template_id);
	$save["hash"] = $hash;

	reset($fields_host_template_edit);
	while (list($field_name, $field_array) = each($fields_host_template_edit)) {
		/* make sure this field exists in the xml array first */
		if (isset($xml_array[$field_name])) {
			$save[$field_name] = addslashes($xml_array[$field_name]);
		}
	}

	$host_template_id = sql_save($save, "host_template");

	$hash_cache["host_template"][$hash] = $host_template_id;

	/* import into: host_template_graph */
	$hash_items = explode("|", $xml_array["graph_templates"]);

	if (!empty($hash_items[0])) {
		for ($i=0; $i<count($hash_items); $i++) {
			/* parse information from the hash */
			$parsed_hash = parse_xml_hash($hash_items[$i]);

			/* invalid/wrong hash */
			if ($parsed_hash == false) { return false; }

			if (isset($hash_cache["graph_template"]{$parsed_hash["hash"]})) {
				db_execute("replace into host_template_graph (host_template_id,graph_template_id) values ($host_template_id," . $hash_cache["graph_template"]{$parsed_hash["hash"]} . ")");
			}
		}
	}

	/* import into: host_template_snmp_query */
	$hash_items = explode("|", $xml_array["data_queries"]);

	if (!empty($hash_items[0])) {
		for ($i=0; $i<count($hash_items); $i++) {
			/* parse information from the hash */
			$parsed_hash = parse_xml_hash($hash_items[$i]);

			/* invalid/wrong hash */
			if ($parsed_hash == false) { return false; }

			if (isset($hash_cache["data_query"]{$parsed_hash["hash"]})) {
				db_execute("replace into host_template_snmp_query (host_template_id,snmp_query_id) values ($host_template_id," . $hash_cache["data_query"]{$parsed_hash["hash"]} . ")");
			}
		}
	}

	/* status information that will be presented to the user */
	$_SESSION["import_debug_info"]["type"] = (empty($_host_template_id) ? "new" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($host_template_id) ? "fail" : "success");

	return $hash_cache;
}

function &xml_to_cdef($hash, &$xml_array, &$hash_cache) {
	global $fields_cdef_edit;

	$fields_cdef_item_edit = array(
		"sequence" => "sequence",
		"type" => "type",
		"value" => "value"
		);

	/* import into: cdef */
	$_cdef_id = db_fetch_cell("select id from cdef where hash='$hash'");
	$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] = addslashes($xml_array[$field_name]);
		}
	}

	$cdef_id = sql_save($save, "cdef");

	$hash_cache["cdef"][$hash] = $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("select id from cdef_items where hash='" . $parsed_hash["hash"] . "' and cdef_id=$cdef_id");
			$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])) {
					$save[$field_name] = addslashes($item_array[$field_name]);
				}
			}

			$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" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($cdef_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;

	/* aggregate field arrays */
	$fields_data_input_field_edit += $fields_data_input_field_edit_1;

	/* import into: data_input */
	$_data_input_id = db_fetch_cell("select id from data_input where hash='$hash'");
	$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])) {
			$save[$field_name] = addslashes(xml_character_decode($xml_array[$field_name]));
		}
	}

	$data_input_id = sql_save($save, "data_input");

	$hash_cache["data_input_method"][$hash] = $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("select id from data_input_fields where hash='" . $parsed_hash["hash"] . "' and data_input_id=$data_input_id");
			$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] = addslashes($item_array[$field_name]);
				}
			}

			$data_input_field_id = sql_save($save, "data_input_fields");

			$hash_cache["data_input_field"]{$parsed_hash["hash"]} = $data_input_field_id;
		}
	}

	/* update field use counter cache if possible */
	if ((isset($xml_array["input_string"])) && (!empty($data_input_id))) {
		generate_data_input_field_sequences($xml_array["input_string"], $data_input_id, "in");
	}

	/* status information that will be presented to the user */
	$_SESSION["import_debug_info"]["type"] = (empty($_data_input_id) ? "new" : "update");
	$_SESSION["import_debug_info"]["title"] = $xml_array["name"];
	$_SESSION["import_debug_info"]["result"] = (empty($data_input_id) ? "fail" : "success");

	return $hash_cache;
}

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 = "(<em>" . $hash_type_names{$parsed_hash["type"]} . "</em>) ";
	}else{
		$prepend = "";
	}

	switch ($parsed_hash["type"]) {
	case 'graph_template':
		return $prepend . db_fetch_cell("select name from graph_templates where hash='" . $parsed_hash["hash"] . "'");
	case 'data_template':
		return $prepend . db_fetch_cell("select name from data_template where hash='" . $parsed_hash["hash"] . "'");
	case 'data_template_item':
		return $prepend . db_fetch_cell("select data_source_name from data_template_rrd where hash='" . $parsed_hash["hash"] . "'");
	case 'host_template':
		return $prepend . db_fetch_cell("select name from host_template where hash='" . $parsed_hash["hash"] . "'");
	case 'data_input_method':
		return $prepend . db_fetch_cell("select name from data_input where hash='" . $parsed_hash["hash"] . "'");
	case 'data_input_field':
		return $prepend . db_fetch_cell("select name from data_input_fields where hash='" . $parsed_hash["hash"] . "'");
	case 'data_query':
		return $prepend . db_fetch_cell("select name from snmp_query where hash='" . $parsed_hash["hash"] . "'");
	case 'gprint_preset':
		return $prepend . db_fetch_cell("select name from graph_templates_gprint where hash='" . $parsed_hash["hash"] . "'");
	case 'cdef':
		return $prepend . db_fetch_cell("select name from cdef where hash='" . $parsed_hash["hash"] . "'");
	case 'round_robin_archive':
		return $prepend . db_fetch_cell("select name from rra where hash='" . $parsed_hash["hash"] . "'");
	}
}

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 (ereg("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 occured */
		if (($parsed_hash["type"] == false) || ($parsed_hash["version"] == false)) {
			return false;
		}
	}else{
		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++;
	}

	return -1;
}

function xml_character_decode($text) {
	$trans_tbl = get_html_translation_table(HTML_ENTITIES);
	$trans_tbl = array_flip($trans_tbl);
	return strtr($text, $trans_tbl);
}

?>