File: graph_export.php

package info (click to toggle)
cacti 0.8.8b%2Bdfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 9,796 kB
  • sloc: php: 52,273; sql: 2,463; xml: 691; sh: 434; perl: 151; makefile: 30
file content (1977 lines) | stat: -rw-r--r-- 71,880 bytes parent folder | download | duplicates (2)
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
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2013 The Cacti Group                                 |
 |                                                                         |
 | 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: The Complete RRDTool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

function graph_export() {
	/* take time to log performance data */
	list($micro,$seconds) = explode(" ", microtime());
	$start = $seconds + $micro;

	if (read_config_option("export_timing") != "disabled") {
		switch (read_config_option("export_timing")) {
			case "classic":
				if (read_config_option("path_html_export_ctr") >= read_config_option("path_html_export_skip")) {
					db_execute("UPDATE settings SET value='1' WHERE name='path_html_export_ctr'");
					$total_graphs_created = config_graph_export();
					config_export_stats($start, $total_graphs_created);
				} elseif (read_config_option("path_html_export_ctr") == "") {
					db_execute("DELETE FROM settings WHERE name='path_html_export_ctr' OR name='path_html_export_skip'");
					db_execute("REPLACE INTO settings (name,value) VALUES ('path_html_export_ctr','1')");
					db_execute("REPLACE INTO settings (name,value) VALUES ('path_html_export_skip','1')");
				} else {
					db_execute("update settings set value='" . (read_config_option("path_html_export_ctr") + 1) . "' where name='path_html_export_ctr'");
				}
				break;
			case "export_hourly":
				$export_minute = read_config_option('export_hourly');
				$poller_minute = read_config_option('poller_interval') / 60;
				if (empty($export_minute)) {
					db_execute("REPLACE INTO settings (name,value) VALUES ('export_hourly','0')");
				} elseif (floor((date('i') / $poller_minute)) == floor((read_config_option('export_hourly') / $poller_minute))) {
					$total_graphs_created = config_graph_export();
					config_export_stats($start, $total_graphs_created);
				}
				break;
			case "export_daily":
				if (strstr(read_config_option('export_daily'), ':')) {
					$export_daily_time = explode(':', read_config_option('export_daily'));
					$poller_minute = read_config_option('poller_interval') / 60;
					if (date('G') == $export_daily_time[0]) {
						if (floor((date('i') / $poller_minute)) == floor(($export_daily_time[1] / $poller_minute))) {
							$total_graphs_created = config_graph_export();
							config_export_stats($start, $total_graphs_created);
						}
					}
				} else {
					db_execute("REPLACE INTO settings (name,value) VALUES ('export_daily','00:00')");
				}
				break;
			default:
				export_log("Export timing not specified. Updated config to disable exporting.");
				db_execute("REPLACE INTO settings (name,value) VALUES ('export_timing','disabled')");
		}
	}
}

function config_graph_export() {
	$total_graphs_created = 0;

	switch (read_config_option("export_type")) {
		case "local":
			$total_graphs_created = export();
			break;
		case "sftp_php":
			if (!function_exists("ftp_ssl_connect")) {
				export_fatal("Secure FTP Function does not exist.  Export can not continue.");
			}
		case "ftp_php":
			/* set the temp directory */
			if (strlen(read_config_option("export_temporary_directory")) == 0) {
				$stExportDir = $_ENV["TMP"] . '/cacti-ftp-temp';
			}else{
				$stExportDir = read_config_option("export_temporary_directory") . '/cacti-ftp-temp';
			}

			$total_graphs_created = export_pre_ftp_upload($stExportDir);
			export_log("Using PHP built-in FTP functions.");
			export_ftp_php_execute($stExportDir, read_config_option("export_type"));
			export_post_ftp_upload($stExportDir);
			break;
		case "ftp_ncftpput":
			if (strstr(PHP_OS, "WIN")) export_fatal("ncftpput only available in unix environment!  Export can not continue.");

			/* set the temp directory */
			if (strlen(read_config_option("export_temporary_directory")) == 0) {
				$stExportDir = $_ENV["TMP"] . '/cacti-ftp-temp';
			}else{
				$stExportDir = read_config_option("export_temporary_directory") . '/cacti-ftp-temp';
			}

			$total_graphs_created = export_pre_ftp_upload($stExportDir);
			export_log("Using ncftpput.");
			export_ftp_ncftpput_execute($stExportDir);
			export_post_ftp_upload($stExportDir);
			break;
		case "disabled":
			break;
		default:
			export_fatal("Export method not specified. Exporting can not continue.  Please set method properly in Cacti configuration.");
	}

	return $total_graphs_created;
}

function config_export_stats($start, $total_graphs_created) {
	/* take time to log performance data */
	list($micro,$seconds) = explode(" ", microtime());
	$end = $seconds + $micro;

	$export_stats = sprintf(
		"ExportDate:%s ExportDuration:%01.4f TotalGraphsExported:%s",
		date("Y-m-d_G:i:s"), round($end - $start,4), $total_graphs_created);

	cacti_log("STATS: " . $export_stats, true, "EXPORT");

	/* insert poller stats into the settings table */
	db_execute("replace into settings (name,value) values ('stats_export','$export_stats')");
}

function export_fatal($stMessage) {
	cacti_log("FATAL ERROR: " . $stMessage, true, "EXPORT");
	exit;
}

function export_log($stMessage) {
	if (read_config_option("log_verbosity") >= POLLER_VERBOSITY_HIGH) {
		cacti_log($stMessage, true, "EXPORT");
	}
}

function export_pre_ftp_upload($stExportDir) {
	global $config, $aFtpExport;

	/* export variable as global */
	$config["config_options_array"]["path_html_export"] = $stExportDir;

	/* clean-up after last cacti instance */
	if (is_dir($stExportDir)) {
		del_directory($stExportDir, FALSE);
	}else {
		@mkdir($stExportDir);
	}

	/* go export */
	$total_graphs_created = export();

	/* force reaing of the variable from the database */
	unset($config["config_options_array"]["path_html_export"]);

	$aFtpExport['server'] = read_config_option('export_ftp_host');
	if (empty($aFtpExport['server'])) {
		export_fatal("FTP Hostname is not expected to be blank!");
	}

	$aFtpExport['remotedir'] = read_config_option('path_html_export');
	if (empty($aFtpExport['remotedir'])) {
		export_fatal("FTP Remote export path is not expected to be blank!");
	}

	$aFtpExport['port'] = read_config_option('export_ftp_port');
	$aFtpExport['port'] = empty($aFtpExport['port']) ? '21' : $aFtpExport['port'];

	$aFtpExport['username'] = read_config_option('export_ftp_user');
	$aFtpExport['password'] = read_config_option('export_ftp_password');

	if (empty($aFtpExport['username'])) {
		$aFtpExport['username'] = 'Anonymous';
		$aFtpExport['password'] = '';
		export_log("Using Anonymous transfer method.");
	}

	if (read_config_option('export_ftp_passive') == 'on') {
		$aFtpExport['passive'] = TRUE;
		export_log("Using passive transfer method.");
	}else {
		$aFtpExport['passive'] = FALSE;
		export_log("Using active transfer method.");
	}

	return $total_graphs_created;
}

function export_ftp_php_execute($stExportDir, $stFtpType = "ftp_php") {
	global $aFtpExport;

	/* connect to foreign system */
	switch($stFtpType) {
	case "ftp_php":
		$oFtpConnection = ftp_connect($aFtpExport['server'], $aFtpExport['port']);

		if (!$oFtpConnection) {
			export_fatal("FTP Connection failed! Check hostname and port.  Export can not continue.");
		}else {
			export_log("Conection to remote server was successful.");
		}
		break;
	case "sftp_php":
		$oFtpConnection = ftp_ssl_connect($aFtpExport['server'], $aFtpExport['port']);

		if (!$oFtpConnection) {
			export_fatal("SFTP Connection failed! Check hostname and port.  Export can not continue.");
		}else {
			export_log("Conection to remote server was successful.");
		}
		break;
	}

	/* login to foreign system */
	if (!ftp_login($oFtpConnection, $aFtpExport['username'], $aFtpExport['password'])) {
		ftp_close($oFtpConnection);
		export_fatal("FTP Login failed! Check username and password.  Export can not continue.");
	}else {
		export_log("Remote login was successful.");
	}

	/* set connection type */
	if ($aFtpExport['passive']) {
		ftp_pasv($oFtpConnection, TRUE);
	}else {
		ftp_pasv($oFtpConnection, FALSE);
	}

	/* change directories into the remote upload directory */
	if (!@ftp_chdir($oFtpConnection, $aFtpExport['remotedir'])) {
		ftp_close($oFtpConnection);
		export_fatal("FTP Remote directory '" . $aFtpExport['remotedir'] . "' does not exist!.  Export can not continue.");
	}

	/* sanitize the remote location if the user has asked so */
	if (read_config_option('export_ftp_sanitize') == 'on') {
		export_log("Deleting remote files.");

		/* get rid of the files first */
		$aFtpRemoteFiles = ftp_nlist($oFtpConnection, $aFtpExport['remotedir']);

		if (is_array($aFtpRemoteFiles)) {
			foreach ($aFtpRemoteFiles as $stFile) {
				export_log("Deleting remote file '" . $stFile . "'");
				@ftp_delete($oFtpConnection, $stFile);
			}
		}

		/* if the presentation is tree, you will have some directories too */
		if (read_config_option("export_presentation") == "tree") {
			$aFtpRemoteDirs = ftp_nlist($oFtpConnection, $aFtpExport['remotedir']);

			foreach ($aFtpRemoteDirs as $remote_dir) {
				if (ftp_chdir($oFtpConnection, addslashes($remote_dir))) {
					$aFtpRemoteFiles = ftp_nlist($oFtpConnection, ".");
					if (is_array($aFtpRemoteFiles)) {
						foreach ($aFtpRemoteFiles as $stFile) {
							export_log("Deleting Remote File '" . $stFile . "'");
							ftp_delete($oFtpConnection, $stFile);
						}
					}
					ftp_chdir($oFtpConnection, "..");

					export_log("Removing Remote Directory '" . $remote_dir . "'");
					ftp_rmdir($oFtpConnection, $remote_dir);
				}else{
					ftp_close($oFtpConnection);
					export_fatal("Unable to cd on remote system");
				}
			}
		}

		$aFtpRemoteFiles = ftp_nlist($oFtpConnection, $aFtpExport['remotedir']);
		if (sizeof($aFtpRemoteFiles) > 0) {
			ftp_close($oFtpConnection);
			export_fatal("Problem sanitizing remote ftp location, must exit.");
		}
	}

	/* upload files to remote system */
	export_log("Uploading files to remote location.");
	ftp_chdir($oFtpConnection, $aFtpExport['remotedir']);
	export_ftp_php_uploaddir($stExportDir,$oFtpConnection);

	/* end connection */
	export_log("Closing ftp connection.");
	ftp_close($oFtpConnection);
}

function export_ftp_php_uploaddir($dir,$oFtpConnection) {
	global $aFtpExport;

	export_log("Uploading directory: '$dir' to remote location.");
	if($dh = opendir($dir)) {
		export_log("Uploading files to remote location.");
		while(($file = readdir($dh)) !== false) {
			$filePath = $dir . "/" . $file;
			if($file != "." && $file != ".." && !is_dir($filePath)) {
				if(!ftp_put($oFtpConnection, $file, $filePath, FTP_BINARY)) {
					export_log("Failed to upload '$file'.");
				}
			}

			if (($file != ".") &&
				($file != "..") &&
				(is_dir($filePath))) {

				export_log("Create remote directory: '$file'.");
				ftp_mkdir($oFtpConnection,$file);

				export_log("Change remote directory to: '$file'.");
				ftp_chdir($oFtpConnection,$file);
				export_ftp_php_uploaddir($filePath,$oFtpConnection);

				export_log("Change remote directory: one up.");
				ftp_cdup($oFtpConnection);
			}
		}
		closedir($dh);
	}
}

function export_ftp_ncftpput_execute($stExportDir) {
	global $aFtpExport;

	chdir($stExportDir);

	/* set the initial command structure */
	$stExecute = 'ncftpput -R -V -r 1 -u ' . cacti_escapeshellarg($aFtpExport['username']) . ' -p ' . cacti_escapeshellarg($aFtpExport['password']);

	/* if the user requested passive mode, use it */
	if ($aFtpExport['passive']) {
		$stExecute .= ' -F ';
	}

	/* setup the port, server, remote directory and all files */
	$stExecute .= ' -P ' . cacti_escapeshellarg($aFtpExport['port']) . ' ' . cacti_escapeshellarg($aFtpExport['server']) . ' ' . cacti_escapeshellarg($aFtpExport['remotedir']) . ".";

	/* run the command */
	$iExecuteReturns = 0;
	system($stExecute, $iExecuteReturns);

	$aNcftpputStatusCodes = array (
		'Success.',
		'Could not connect to remote host.',
		'Could not connect to remote host - timed out.',
		'Transfer failed.',
		'Transfer failed - timed out.',
		'Directory change failed.',
		'Directory change failed - timed out.',
		'Malformed URL.',
		'Usage error.',
		'Error in login configuration file.',
		'Library initialization failed.',
		'Session initialization failed.');

	export_log('Ncftpput returned: ' . $aNcftpputStatusCodes[$iExecuteReturns]);
}

function export_post_ftp_upload($stExportDir) {
	/* clean-up after ftp-put */
	if ($dh = opendir($stExportDir)) {
		while (($file = readdir($dh)) !== false) {
			$filePath = $stExportDir . "/" . $file;
			if ($file != "." && $file != ".." && !is_dir($filePath)) {
				export_log("Removing Local File '" . $file . "'");
				unlink($filePath);
			}

			/* if the directory turns out to be a sub-directory, delete it too */
			if ($file != "." && $file != ".." && is_dir($filePath)) {
				export_log("Removing Local Directory '" . $filePath . "'");
				export_post_ftp_upload($filePath);
			}
		}
		closedir($dh);

		/* don't delete the root of the temporary export directory */
		if (read_config_option("export_temporary_directory") != $stExportDir) {
			rmdir($stExportDir);
		}
	}
}

function export() {
	global $config;

	/* count how many graphs are created */
	$total_graphs_created = 0;

	$cacti_root_path = $config["base_path"];
	$cacti_export_path = read_config_option("path_html_export");

	/* if the path is not a directory, don't continue */
	if (!is_dir($cacti_export_path)) {
		export_fatal("Export path '" . $cacti_export_path . "' does not exist!  Export can not continue.");
	}

	/* blank paths are not good */
	if (strlen($cacti_export_path) == 0) {
		export_fatal("Export path is null!  Export can not continue.");
	}

	/* can not be the web root */
	if ((strcasecmp($cacti_root_path, $cacti_export_path) == 0) &&
		(read_config_option("export_type") == "local")) {
		export_fatal("Export path '" . $cacti_export_path . "' is the Cacti web root.  Can not continue.");
	}

	/* can not be a parent of the Cacti web root */
	if (strncasecmp($cacti_root_path, $cacti_export_path, strlen($cacti_export_path))== 0) {
		export_fatal("Export path '" . $cacti_export_path . "' is a parent folder from the Cacti web root.  Can not continue.");
	}

	/* check for bad directories within the cacti path */
	if (strcasecmp($cacti_root_path, $cacti_export_path) < 0) {
		$cacti_system_paths = array(
			"include",
			"lib",
			"install",
			"rra",
			"log",
			"scripts",
			"plugins",
			"images",
			"resource");

		foreach($cacti_system_paths as $cacti_system_path) {
			if (substr_count(strtolower($cacti_export_path), strtolower($cacti_system_path)) > 0) {
				export_fatal("Export path '" . $cacti_export_path . "' is potentially within a Cacti system path '" . $cacti_system_path . "'.  Can not continue.");
			}
		}
	}

	/* don't allow to export to system paths */
	$system_paths = array(
		"/boot",
		"/lib",
		"/usr",
		"/usr/bin",
		"/bin",
		"/sbin",
		"/usr/sbin",
		"/usr/lib",
		"/var/lib",
		"/root",
		"/etc",
		"windows",
		"winnt",
		"program files");

	foreach($system_paths as $system_path) {
		if (substr($system_path, 0, 1) == "/") {
			if ($system_path == substr($cacti_export_path, 0, strlen($system_path))) {
				export_fatal("Export path '" . $cacti_export_path . "' is within a system path '" . $system_path . "'.  Can not continue.");
			}
		}elseif (substr_count(strtolower($cacti_export_path), strtolower($system_path)) > 0) {
			export_fatal("Export path '" . $cacti_export_path . "' is within a system path '" . $system_path . "'.  Can not continue.");
		}
	}

	export_log("Running graph export");

	/* delete all files and directories in the cacti_export_path */
	del_directory($cacti_export_path, false);

	/* test how will the export will be made */
	if (read_config_option('export_presentation') == 'tree') {
		export_log("Running graph export with tree organization");
		$total_graphs_created = tree_export();
	}else {
		export_log("Running graph export with legacy organization");
		$total_graphs_created = classical_export($cacti_root_path, $cacti_export_path);
	}

	return $total_graphs_created;
}

function classical_export($cacti_root_path, $cacti_export_path) {
	global $config;
	include_once($config["base_path"] . "/lib/time.php");

	$total_graphs_created = 0;

	/* copy the css/images on the first time */
	if (file_exists("$cacti_export_path/main.css") == false) {
		copy("$cacti_root_path/include/main.css", "$cacti_export_path/main.css");
		copy("$cacti_root_path/images/tab_cacti.gif", "$cacti_export_path/tab_cacti.gif");
		copy("$cacti_root_path/images/cacti_backdrop.gif", "$cacti_export_path/cacti_backdrop.gif");
		copy("$cacti_root_path/images/transparent_line.gif", "$cacti_export_path/transparent_line.gif");
		copy("$cacti_root_path/images/shadow.gif", "$cacti_export_path/shadow.gif");
	}

	/* create the base directory */
	if (!is_dir("$cacti_export_path/graphs")) {
		if (!mkdir("$cacti_export_path/graphs", 0755)) {
			export_fatal("Create directory '$cacti_export_path/graphs' failed.  Can not continue");
		}
	}

	/* determine the number of columns to write */
	$classic_columns = read_config_option("export_num_columns");

	/* if the index file already exists, delete it */
	check_remove($cacti_export_path . "/index.html");

	export_log("Creating File  '" . $cacti_export_path . "/index.html'");

	/* open pointer to the new index file */
	$fp_index = fopen($cacti_export_path . "/index.html", "w");

	/* get a list of all graphs that need exported */
	$exportuser = read_config_option("export_user_id");
	$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));

	$sql_where = "WHERE " . get_graph_permissions_sql($current_user["policy_graphs"], $current_user["policy_hosts"], $current_user["policy_graph_templates"]);
	$sql_join = "LEFT JOIN host ON (host.id=graph_local.host_id)
		LEFT JOIN graph_templates ON (graph_templates.id=graph_local.graph_template_id)
		LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id AND user_auth_perms.type=1 AND user_auth_perms.user_id=$exportuser)
		 OR (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=$exportuser)
		 OR (graph_templates.id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id=$exportuser))";

	$sql_base = "FROM (graph_templates_graph,graph_local)
		$sql_join
		$sql_where
		" . (empty($sql_where) ? "WHERE" : "AND") . " graph_templates_graph.local_graph_id > 0
		AND graph_templates_graph.export='on'
		AND graph_templates_graph.local_graph_id=graph_local.id";

	$graphs = db_fetch_assoc("SELECT
		graph_templates_graph.local_graph_id,
		graph_templates_graph.title_cache,
		graph_templates_graph.height,
		graph_templates_graph.width
		$sql_base
		GROUP BY graph_templates_graph.local_graph_id");

	$rras = db_fetch_assoc("SELECT
		rra.id,
		rra.name
		FROM rra
		ORDER BY timespan");

	/* write the html header data to the index file */
	$stats = "Export Date: " . date(date_time_format()) . "<br>"; 
	$stats.= "Total Graphs: " . sizeof($graphs);
	fwrite($fp_index, HTML_HEADER_CLASSIC);
	fwrite($fp_index, HTML_GRAPH_HEADER_ONE_CLASSIC);
	fwrite($fp_index, $stats);
	fwrite($fp_index, HTML_GRAPH_HEADER_TWO_CLASSIC);

	/* open a pipe to rrdtool for writing */
	$rrdtool_pipe = rrd_init();

	/* for each graph... */
	$i = 0; $k = 0;
	if ((sizeof($graphs) > 0) && (sizeof($rras) > 0)) {
		foreach ($graphs as $graph) {
			check_remove($cacti_export_path . "graphs/thumb_" . $graph["local_graph_id"] . ".png");
			check_remove($cacti_export_path . "graph_" . $graph["local_graph_id"] . ".html");

			/* settings for preview graphs */
			$graph_data_array["graph_height"] = read_config_option("export_default_height");
			$graph_data_array["graph_width"] = read_config_option("export_default_width");
			$graph_data_array["graph_nolegend"] = true;
			$graph_data_array["export"] = true;
			$graph_data_array["export_filename"] = "graphs/thumb_" . $graph["local_graph_id"] . ".png";

			export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");

			@rrdtool_function_graph($graph["local_graph_id"], 0, $graph_data_array, $rrdtool_pipe);

			$total_graphs_created++;

			/* generate html files for each graph */
			if (!file_exists($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html")) {
				export_log("Creating File  '" . $cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html");

				$fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");

				fwrite($fp_graph_index, HTML_HEADER_CLASSIC);
				fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE_CLASSIC);
				fwrite($fp_graph_index, "<strong>Graph - " . $graph["title_cache"] . "</strong>");
				fwrite($fp_graph_index, HTML_GRAPH_HEADER_TWO_CLASSIC);
				fwrite($fp_graph_index, "<td>");
			}else{
				$fp_graph_index = NULL;
			}

			/* reset vars for actual graph image creation */
			reset($rras);
			unset($graph_data_array);

			/* generate graphs for each rra */
			foreach ($rras as $rra) {
				$graph_data_array["export"] = true;
				$graph_data_array["export_filename"] = "graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";

				export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");

				@rrdtool_function_graph($graph["local_graph_id"], $rra["id"], $graph_data_array, $rrdtool_pipe);

				$total_graphs_created++;

				/* write image related html */
				fwrite($fp_graph_index, "<div align=center><img src='graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png' border=0></div>\n
					<div align=center><strong>" . $rra["name"] . "</strong></div><br>");
			}

			fwrite($fp_graph_index, "</td></table>");
			fwrite($fp_graph_index, HTML_GRAPH_FOOTER_CLASSIC);
			fwrite($fp_graph_index, HTML_FOOTER_CLASSIC);
			fclose($fp_graph_index);

			/* main graph page html */
			fwrite($fp_index, "<td align='center' width='" . round(100 / $classic_columns,0) . "%'><a href='graph_" . $graph["local_graph_id"] . ".html'><img src='graphs/thumb_" . $graph["local_graph_id"] . ".png' border='0' alt='" . $graph["title_cache"] . "'></a></td>\n");

			$i++;
			$k++;

			if ((($i % $classic_columns) == 0) && ($k < count($graphs))) {
				fwrite($fp_index, "</tr><tr>");
			}
  		}
	}else{
		fwrite($fp_index, "<td><em>No Graphs Found.</em></td>");
	}

	/* close the rrdtool pipe */
	rrd_close($rrdtool_pipe);

	fwrite($fp_index, HTML_GRAPH_FOOTER_CLASSIC);
	fwrite($fp_index, HTML_FOOTER_CLASSIC);
	fclose($fp_index);

	return $total_graphs_created;
}

function tree_export() {
	global $config;
	include_once($config["base_path"] . "/lib/time.php");

	$total_graphs_created = 0;

	/* set the user to utilize for establishing export permissions */
	$export_user = read_config_option("export_user_id");

	$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id='" . $export_user . "'");

	$cacti_root_path   = $config["base_path"];
	$cacti_export_path = read_config_option("path_html_export");

	/* if the selected user has default rights, show all the graphs */
	if ($current_user["policy_trees"] == 1) {
		$trees = db_fetch_assoc("SELECT
			id,
			name
			FROM graph_tree");
	}else{
		/* otherwise, show only those tree's that the user has access to */
		$trees = db_fetch_assoc("SELECT
			graph_tree.id AS id,
			graph_tree.name AS name
			FROM user_auth_perms
			INNER JOIN graph_tree
			ON (user_auth_perms.item_id = graph_tree.id)
			WHERE user_auth_perms.user_id ='" . $current_user["id"] . "'");
	}

	/* if tree isolation is off, create the treeview and graphs directories for the initial hierarchy */
	if (read_config_option("export_tree_isolation") == "off") {
		/* create directory structure */
		create_export_directory_structure($cacti_root_path, $cacti_export_path);

		/* export graphs */
		foreach($trees as $tree) {
			$total_graphs_created += export_tree_graphs_and_graph_html("", $tree["id"]);
		}

		/* build base index files first */
		$stats["timestamp"] = date(date_time_format());
		$stats["total_graphs_created"] = $total_graphs_created;
		build_html_file(0, "index", $stats);

		foreach($trees as $tree) {
			$leaf["tree_id"] = $tree["id"];
			$leaf["title"] = "";
			$leaf["name"] = $tree["name"];

			build_html_file($leaf, "tree");

			/* build remainder of html files */
			export_tree_html("", clean_up_export_name($tree["name"]) . "_index.html", $tree["id"], 0);
		}
	}else{
		/* now let's populate all the sub trees */
		foreach ($trees as $tree) {
			/* create the base directory */
			if (!is_dir("$cacti_export_path/" . clean_up_export_name($tree["name"]))) {
				if (!mkdir("$cacti_export_path/" . clean_up_export_name($tree["name"]), 0755)) {
					export_fatal("Create directory '" . clean_up_export_name($tree["name"]) . "' failed.  Can not continue");
				}
			}

			create_export_directory_structure($cacti_root_path, $cacti_export_path . "/" . clean_up_export_name($tree["name"]));

			/* build base index files first */
			$stats["timestamp"] = date(date_time_format());
			$stats["total_graphs_created"] = $total_graphs_created;
			build_html_file($tree["id"], "index", $stats);

			$leaf["tree_id"] = $tree["id"];
			$leaf["title"] = "";
			$leaf["name"] = $tree["name"];

			build_html_file($leaf, "tree");

			$total_graphs_created += export_tree_graphs_and_graph_html(clean_up_export_name($tree["name"]), $tree["id"]);
			export_tree_html("graphs", "index.html", $tree["id"], 0);
		}
	}

	return $total_graphs_created;
}

function export_tree_html($path, $filename, $tree_id, $parent_tree_item_id) {
	/* auth check for hosts on the trees */
	if (read_config_option("auth_method") != 0) {
		$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));

		$sql_join = "LEFT JOIN user_auth_perms ON (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . read_config_option("export_user_id") . ")";

		if ($current_user["policy_hosts"] == "1") {
			$sql_where = "AND !(user_auth_perms.user_id IS NOT NULL AND graph_tree_items.host_id>0)";
		}elseif ($current_user["policy_hosts"] == "2") {
			$sql_where = "AND !(user_auth_perms.user_id IS NULL AND graph_tree_items.host_id>0)";
		}
	}else{
		$sql_join  = "";
		$sql_where = "";
	}

	if ($tree_id == 0) {
		$sql_where = "WHERE graph_tree_items.local_graph_id=0
			$sql_where";
	}else{
		$sql_where = "WHERE graph_tree_items.graph_tree_id=" . $tree_id . "
			$sql_where
			AND graph_tree_items.local_graph_id=0";
	}

	$hier_sql = "SELECT DISTINCT
		graph_tree.name,
		graph_tree.id AS tree_id,
		graph_tree_items.id,
		graph_tree_items.title,
		graph_tree_items.order_key,
		graph_tree_items.host_id,
		graph_tree_items.host_grouping_type,
		host.description AS hostname
		FROM graph_tree
		LEFT JOIN (graph_tree_items
		LEFT JOIN host ON (graph_tree_items.host_id=host.id)
		$sql_join)
		ON graph_tree.id = graph_tree_items.graph_tree_id
		$sql_where
		ORDER BY graph_tree.name, graph_tree_items.order_key";

	$hierarchy = db_fetch_assoc($hier_sql);

	/* build all the html files */
	if (sizeof($hierarchy) > 0) {
		foreach ($hierarchy as $leaf) {
			if ($leaf["host_id"] > 0) {
				build_html_file($leaf, "host");

				if (read_config_option("export_tree_expand_hosts") == "on") {
					if ($leaf["host_grouping_type"] == HOST_GROUPING_GRAPH_TEMPLATE) {
						$graph_templates = db_fetch_assoc("SELECT
							graph_templates.id,
							graph_templates.name,
							graph_templates_graph.local_graph_id,
							graph_templates_graph.title_cache
							FROM (graph_local,graph_templates,graph_templates_graph)
							WHERE graph_local.id=graph_templates_graph.local_graph_id
							AND graph_templates_graph.graph_template_id=graph_templates.id
							AND graph_local.host_id=" . $leaf["host_id"] . "
							AND graph_templates_graph.export='on'
							GROUP BY graph_templates.id
							ORDER BY graph_templates.name");

						if (sizeof($graph_templates)) {
							foreach($graph_templates as $graph_template) {
								build_html_file($leaf, "gt", $graph_template);
							}
						}
					}else if ($leaf["host_grouping_type"] == HOST_GROUPING_DATA_QUERY_INDEX) {
						$data_queries = db_fetch_assoc("SELECT
							snmp_query.id,
							snmp_query.name
							FROM (graph_local,snmp_query)
							WHERE graph_local.snmp_query_id=snmp_query.id
							AND graph_local.host_id=" . $leaf["host_id"] . "
							GROUP BY snmp_query.id
							ORDER BY snmp_query.name");

						array_push($data_queries, array(
							"id" => "0",
							"name" => "Graph Template Based"
							));

						foreach ($data_queries as $data_query) {
							build_html_file($leaf, "dq", $data_query);

							/* fetch a list of field names that are sorted by the preferred sort field */
							$sort_field_data = get_formatted_data_query_indexes($leaf["host_id"], $data_query["id"]);

							if ($data_query["id"] > 0) {
								while (list($snmp_index, $sort_field_value) = each($sort_field_data)) {
									build_html_file($leaf, "dqi", $data_query, $snmp_index);
								}
							}
						}
					}
				}
			}else{
				build_html_file($leaf, "leaf");
			}
		}
	}
}

function build_html_file($leaf, $type = "", $array_data = array(), $snmp_index = "") {
	$cacti_export_path = read_config_option("path_html_export");

	/* auth check for hosts on the trees */
	$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));

	$sql_join  = "LEFT JOIN user_auth_perms ON (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . read_config_option("export_user_id") . ")";

	$sql_where = get_graph_permissions_sql($current_user["policy_graphs"], $current_user["policy_hosts"], $current_user["policy_graph_templates"]);
	$sql_where = (empty($sql_where) ? "" : "AND $sql_where");

	switch ($type) {
	case "index":
		$sql_where = "";

		$filename = "index.html";

		break;
	case "tree":
		$sql_join = "LEFT JOIN user_auth_perms ON (graph_templates_graph.local_graph_id=user_auth_perms.item_id AND user_auth_perms.type=1 AND user_auth_perms.user_id=" . read_config_option("export_user_id") . ")";

		/* searching for the graph_tree_items of the tree_id which are graphs */
		if ($leaf["tree_id"] == 0) {
			$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
				$sql_where
				AND graph_templates_graph.export='on'";

			$filename = "index.html";
		}else{
			$search_key = "";

			/* get the "starting leaf" if the user clicked on a specific branch */
			if (!empty($leaf["id"])) {
				$search_key = substr($leaf["order_key"], 0, (tree_tier($leaf["order_key"]) * CHARS_PER_TIER));
			}

			$sql_where = "WHERE graph_tree_items.graph_tree_id=" . $leaf["tree_id"] . "
				$sql_where
				AND graph_templates_graph.local_graph_id!=0
				AND graph_templates_graph.export='on'
				AND graph_tree_items.order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'";

			if ($current_user["policy_graphs"] == 2) {
				$sql_where .= " AND user_auth_perms.item_id=graph_tree_items.local_graph_id";
			}

			$filename = clean_up_export_name(get_tree_name($leaf["tree_id"])) . "_leaf.html";
		}

		break;
	case "leaf":
		$sql_join = "LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id and user_auth_perms.type=1 and user_auth_perms.user_id=" . $current_user["id"] . ") OR (host.id=user_auth_perms.item_id and user_auth_perms.type=3 and user_auth_perms.user_id=" . $current_user["id"] . ") OR (graph_templates.id=user_auth_perms.item_id and user_auth_perms.type=4 and user_auth_perms.user_id=" . $current_user["id"] . "))";

		/* searching for the graph_tree_items of the tree_id which are graphs */
		if ($leaf["tree_id"] == 0) {
			$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
				$sql_where
				AND graph_templates_graph.export='on'";

			$filename = "index.html";
		}else{
			$search_key = "";

			/* get the "starting leaf" if the user clicked on a specific branch */
			if (!empty($leaf["id"])) {
				$search_key = substr($leaf["order_key"], 0, (tree_tier($leaf["order_key"]) * CHARS_PER_TIER));
			}

			$sql_where = "WHERE graph_tree_items.graph_tree_id=" . $leaf["tree_id"] . "
				$sql_where
				AND graph_templates_graph.local_graph_id!=0
				AND graph_templates_graph.export='on'
				AND graph_tree_items.order_key like '$search_key" . str_repeat('_', CHARS_PER_TIER) . str_repeat('0', (MAX_TREE_DEPTH * CHARS_PER_TIER) - (strlen($search_key) + CHARS_PER_TIER)) . "'";

			if ($current_user["policy_graphs"] == 2) {
				$sql_where .= " AND user_auth_perms.item_id=graph_tree_items.local_graph_id";
			}

			if (strlen($leaf["title"])) {
				$filename = clean_up_export_name(get_tree_name($leaf["tree_id"]) . "_" . $leaf["title"]) . "_" . $leaf["id"] . "_leaf.html";
			}else{
				$filename = clean_up_export_name(get_tree_name($leaf["tree_id"])) . "_leaf.html";
			}
		}

		break;
	case "host":
		$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
			$sql_where
			AND graph_local.host_id=" . $leaf["host_id"] . "
			AND graph_templates_graph.export='on'";

		$filename = clean_up_export_name($leaf["hostname"]) . "_" . $leaf["id"] . ".html";

		break;
	case "gt":
		$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
			$sql_where
			AND graph_local.host_id=" . $leaf["host_id"] . "
			AND graph_templates_graph.export='on'
			AND graph_templates_graph.graph_template_id=" . $array_data["id"];

		$filename = clean_up_export_name($leaf["hostname"]) . "_gt_" . $leaf["id"] . "_" . $array_data["id"] . ".html";

		break;
	case "dq":
		$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
			$sql_where
			AND graph_local.host_id=" . $leaf["host_id"] . "
			AND graph_local.snmp_query_id=" . $array_data["id"] . "
			AND graph_templates_graph.export='on'";

		$filename = clean_up_export_name($leaf["hostname"]) . "_dq_" . $leaf["id"] . "_" . $array_data["id"] . ".html";

		break;
	case "dqi":
		$sql_where = "WHERE graph_templates_graph.local_graph_id<>0
			$sql_where
			AND graph_local.host_id=" . $leaf["host_id"] . "
			AND graph_local.snmp_query_id=" . $array_data["id"] . "
			AND graph_local.snmp_index=" . $snmp_index . "
			AND graph_templates_graph.export='on'";

		$filename = clean_up_export_name($leaf["hostname"]) . "_dqi_" . $leaf["id"] . "_" . $array_data["id"] . "_" . $snmp_index . ".html";

		break;
	}

	switch ($type) {
	case "index":
		break;
	case "tree":
	case "leaf":
		$request = "SELECT DISTINCT
			graph_tree_items.id,
			graph_tree_items.title,
			graph_tree_items.local_graph_id,
			graph_tree_items.rra_id,
			graph_tree_items.order_key,
			graph_templates_graph.title_cache as title_cache
			FROM (graph_tree_items,graph_local)
			LEFT JOIN host ON (host.id=graph_local.host_id)
			LEFT JOIN graph_templates_graph ON (graph_tree_items.local_graph_id=graph_templates_graph.local_graph_id AND graph_tree_items.local_graph_id>0)
			LEFT JOIN graph_templates ON (graph_templates_graph.graph_template_id=graph_templates.id)
			$sql_join
			$sql_where
			GROUP BY graph_tree_items.id
			ORDER BY graph_tree_items.order_key";

		break;
	case "host":
	case "gt":
	case "dq":
	case "dqi":
		$request = "SELECT DISTINCT
			graph_templates_graph.id,
			graph_templates_graph.local_graph_id,
			graph_templates_graph.height,
			graph_templates_graph.width,
			graph_templates_graph.title_cache,
			graph_templates.name
			FROM (graph_tree_items, graph_templates_graph)
			LEFT JOIN graph_templates ON (graph_templates_graph.graph_template_id=graph_templates.id)
			LEFT JOIN graph_local ON (graph_templates_graph.local_graph_id=graph_local.id)
			LEFT JOIN host ON (host.id=graph_local.host_id)
			$sql_join
			$sql_where" . (strlen($sql_where) ? " AND ":"WHERE ") .
			"graph_templates_graph.export='on'
			ORDER BY graph_templates_graph.title_cache";

		break;
	}

	if ($type == "index") {
		$graphs = array();
	}else{
		$graphs = db_fetch_assoc($request);
	}

	/* get the path name */
	if (read_config_option("export_tree_isolation") == "off") {
		$path = "";
	}else{
		$path = clean_up_export_name(get_tree_name($leaf["tree_id"]));
	}

	export_log("Creating File  '" . $cacti_export_path . "/" . $path . "/" . $filename . "'");

	/* open pointer to the new file */
	$fp = fopen($cacti_export_path . "/" . $path . "/" . $filename, "w");

	/* begin old stuff */
	$cacti_export_path = read_config_option("path_html_export");

	/* write the html header data to the file */
	fwrite($fp, HTML_HEADER_TREE);

	/* write the code for the tree at the left */
	draw_html_left_tree($fp, $leaf["tree_id"]);

	/* write the associated graphs for this graph_tree_item or graph_tree*/
	fwrite($fp, HTML_GRAPH_HEADER_ONE_TREE);
	switch($type) {
	case "index":
		fwrite($fp, "<strong>Graphs Last Updated on :</strong></td></tr>" .
				"<tr bgcolor='#a9b7cb'>" .
					"<td colspan='3' class='textHeaderDark'>" . 
						"Export Date: " . $array_data["timestamp"] . "<br>" .
						"Total Graphs: " . $array_data["total_graphs_created"] .
					"</td>" .
				"</tr><tr>");
		break;
	case "tree":
		fwrite($fp, "<strong>Tree:</strong> " . get_tree_name($leaf["tree_id"]) . " - Associated Graphs" . "</td></tr><tr>");
		break;
	case "leaf":
		fwrite($fp, "<strong>Tree:</strong> " . get_tree_name($leaf["tree_id"]) . "</td></tr><tr bgcolor='#a9b7cb'><td colspan='3' class='textHeaderDark'><strong>Leaf:</strong> " . $leaf["title"] . " - Associated Graphs" . "</td></tr><tr>");
		break;
	case "host":
		fwrite($fp, "<strong>Host:</strong> " . $leaf["hostname"] . " - Associated Graphs" . "</td></tr><tr>");
		break;
	case "gt":
		fwrite($fp, "<strong>Host:</strong> " . $leaf["hostname"] . "</td></tr><tr bgcolor='#a9b7cb'><td colspan='3' class='textHeaderDark'><strong>Graph Template:</strong> " . $array_data["name"] . " - Associated Graphs" . "</td></tr><tr>");
		break;
	case "dq":
		fwrite($fp, "<strong>Host:</strong> " . $leaf["hostname"] . "</td></tr><tr bgcolor='#a9b7cb'><td colspan='3' class='textHeaderDark'><strong>Data Query:</strong> " . $array_data["name"] . " - Associated Graphs" . "</td></tr><tr>");
		break;
	case "dqi":
		fwrite($fp, "<strong>Host:</strong> " . $leaf["hostname"] . "</td></tr><tr bgcolor='#a9b7cb'><td colspan='3' class='textHeaderDark'><strong>Data Query Index:</strong> " . $array_data["name"] . " " . $snmp_index . " - Graph" . "</td></tr><tr>");
		break;
	}

	$i = 0;
	if (sizeof($graphs)) {
	foreach($graphs as $graph) {
		/* write the right pane syntax */
		if ($leaf["tree_id"] != 0) {
			/* main graph page html */
			fwrite($fp, "<td align='center'><a href='" . "graph_" . $graph["local_graph_id"] . ".html'><img src='graphs/thumb_" . $graph["local_graph_id"] . ".png' border='0' alt='" . $graph["title_cache"] . "'></a></td>\n");

			/* do new column processing */
			$i++;
			if ($i >= read_config_option("export_num_columns")) {
				fwrite($fp, "</tr><tr>");
				$i = 0;
			}
		}
	}
	}

	/* write the html footer to the file */
	fwrite($fp, HTML_FOOTER_TREE);
	fclose($fp);

}

function explore_tree($path, $tree_id, $parent_tree_item_id) {
	/* seek graph_tree_items of the tree_id which are NOT graphs but headers */
	$links = db_fetch_assoc("SELECT
		id,
		title,
		host_id
		FROM graph_tree_items
		WHERE rra_id = 0
		AND graph_tree_id = " . $tree_id);

	$total_graphs_created = 0;

	foreach($links as $link) {
		/* this test gives us the parent of the curent graph_tree_item */
		if (get_parent_id($link["id"], "graph_tree_items", "graph_tree_id = " . $tree_id) == $parent_tree_item_id) {
			if (get_tree_item_type($link["id"]) == "host") {
				if (read_config_option("export_tree_isolation") == "off") {
					$total_graphs_created += export_build_tree_single(clean_up_export_name($path), clean_up_export_name(get_host_description($link["host_id"]) . "_" . $link["id"] . ".html"), $tree_id, $link["id"]);
				}else{
					$total_graphs_created += export_build_tree_isolated(clean_up_export_name($path), clean_up_export_name(get_host_description($link["host_id"]) . "_" . $link["id"] . ".html"), $tree_id, $link["id"]);
				}
			}else {
				/*now, this graph_tree_item is the parent of others graph_tree_items*/
				if (read_config_option("export_tree_isolation") == "off") {
					$total_graphs_created += export_build_tree_single(clean_up_export_name($path), clean_up_export_name($link["title"] . "_" . $link["id"] . ".html"), $tree_id, $link["id"]);
				}else{
					$total_graphs_created += export_build_tree_isolated(clean_up_export_name($path), clean_up_export_name($link["title"] . "_" . $link["id"] . ".html"), $tree_id, $link["id"]);
				}
			}
		}
	}

	return $total_graphs_created;
}

/* export_is_tree_allowed - determines whether the export user is allowed to view a certain graph tree
   @arg $tree_id - (int) the ID of the graph tree to check permissions for
   @returns - (bool) whether the current user is allowed the view the specified graph tree or not */
function export_is_tree_allowed($tree_id) {
	$current_user = db_fetch_row("select policy_trees from user_auth where id=" . read_config_option("export_user_id"));

	$trees = db_fetch_assoc("select
		user_id
		from user_auth_perms
		where user_id=" . read_config_option("export_user_id") . "
		and type=2
		and item_id=$tree_id");

	/* policy == allow AND matches = DENY */
	if ((sizeof($trees) > 0) && ($current_user["policy_trees"] == "1")) {
		return false;
	/* policy == deny AND matches = ALLOW */
	}elseif ((sizeof($trees) > 0) && ($current_user["policy_trees"] == "2")) {
		return true;
	/* policy == allow AND no matches = ALLOW */
	}elseif ((sizeof($trees) == 0) && ($current_user["policy_trees"] == "1")) {
		return true;
	/* policy == deny AND no matches = DENY */
	}elseif ((sizeof($trees) == 0) && ($current_user["policy_trees"] == "2")) {
		return false;
	}
}

function export_tree_graphs_and_graph_html($path, $tree_id) {
	global $colors, $config;
	include_once($config["library_path"] . "/tree.php");
	include_once($config["library_path"] . "/data_query.php");

	/* start the count of graphs */
	$total_graphs_created = 0;
	$exported_files = array();

	$cacti_export_path = read_config_option("path_html_export");

	/* auth check for hosts on the trees */
	$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));

	if (!export_is_tree_allowed($tree_id)) {
		return 0;
	}

	$sql_join = "LEFT JOIN graph_local ON (graph_templates_graph.local_graph_id=graph_local.id)
		LEFT JOIN graph_templates ON (graph_templates.id=graph_local.graph_template_id)
		LEFT JOIN host ON (host.id=graph_local.host_id)
		LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id and user_auth_perms.type=1 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (graph_templates.id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id=" . $current_user["id"] . "))";

	$sql_where = get_graph_permissions_sql($current_user["policy_graphs"], $current_user["policy_hosts"], $current_user["policy_graph_templates"]);
	$sql_where = (empty($sql_where) ? "" : "AND $sql_where");

	$graphs = array();

	if ($tree_id == 0) {
		$hosts = db_fetch_assoc("SELECT DISTINCT host_id FROM graph_tree_items");
	}else{
		$hosts = db_fetch_assoc("SELECT DISTINCT host_id FROM graph_tree_items WHERE graph_tree_id=" . $tree_id);
	}

	/* get a list of host graphs first */
	if (sizeof($hosts)) {
	foreach ($hosts as $host) {
		$hosts_sql = "SELECT DISTINCT
			graph_templates_graph.id,
			graph_templates_graph.local_graph_id,
			graph_templates_graph.height,
			graph_templates_graph.width,
			graph_templates_graph.title_cache,
			graph_templates.name,
			graph_local.host_id,
			graph_tree_items.rra_id
			FROM (graph_tree_items, graph_templates_graph)
			$sql_join
			WHERE ((graph_templates_graph.local_graph_id<>0)
			$sql_where
			AND (graph_local.host_id=" . $host["host_id"] . ")
			AND (graph_templates_graph.export='on'))
			ORDER BY graph_templates_graph.title_cache";

		$host_graphs = db_fetch_assoc($hosts_sql);

		if (sizeof($host_graphs)) {
			if (sizeof($graphs)) {
				$graphs = array_merge($host_graphs, $graphs);
			}else{
				$graphs = $host_graphs;
			}
		}
	}
	}

	/* now get the list of graphs placed within the tree */
	if ($tree_id == 0) {
		$sql_where = "WHERE graph_templates_graph.local_graph_id!=0
			$sql_where
			AND graph_templates_graph.export='on'";
	}else{
		$sql_where = "WHERE graph_tree_items.graph_tree_id =" . $tree_id . "
			$sql_where
			AND graph_templates_graph.local_graph_id!=0
			AND graph_templates_graph.export='on'";
	}

	$non_host_sql = "SELECT
		graph_templates_graph.id,
		graph_templates_graph.local_graph_id,
		graph_templates_graph.height,
		graph_templates_graph.width,
		graph_templates_graph.title_cache,
		graph_templates.name,
		graph_local.host_id,
		graph_tree_items.rra_id,
		graph_tree_items.id AS gtid
		FROM (graph_tree_items, graph_templates_graph)
		$sql_join
		$sql_where
		AND graph_tree_items.local_graph_id = graph_templates_graph.local_graph_id
		AND graph_templates_graph.export='on'
		ORDER BY graph_templates_graph.title_cache";

	$non_host_graphs = db_fetch_assoc($non_host_sql);

	if (sizeof($non_host_graphs)) {
		if (sizeof($graphs)) {
			$graphs = array_merge($non_host_graphs, $graphs);
		}else{
			$graphs = $non_host_graphs;
		}
	}

	/* open a pipe to rrdtool for writing */
	$rrdtool_pipe = rrd_init();

	/* for each graph... */
	$i = 0;
	if (sizeof($graphs) > 0) {
	foreach($graphs as $graph)  {
		$rras = get_associated_rras($graph["local_graph_id"]);

		/* settings for preview graphs */
		$graph_data_array["graph_height"] = read_config_option("export_default_height");
		$graph_data_array["graph_width"] = read_config_option("export_default_width");
		$graph_data_array["graph_nolegend"] = true;
		$graph_data_array["export"] = true;

		if (read_config_option("export_tree_isolation") == "on") {
			$graph_data_array["export_filename"] =  "/" . $path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
			$export_filename = $cacti_export_path . "/" . $path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
		}else{
			$graph_data_array["export_filename"] = "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
			$export_filename = $cacti_export_path . "/graphs/thumb_" . $graph["local_graph_id"] . ".png";
		}

		if (!array_search($export_filename, $exported_files)) {
			/* add the graph to the exported list */
			array_push($exported_files, $export_filename);

			export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");

			/* generate the graph */
			@rrdtool_function_graph($graph["local_graph_id"], $graph["rra_id"], $graph_data_array, $rrdtool_pipe);
			$total_graphs_created++;

			/* generate html files for each graph */
			if (read_config_option("export_tree_isolation") == "on") {
				export_log("Creating File  '" . $cacti_export_path . "/" . $path ."/graph_" . $graph["local_graph_id"] . ".html'");
				$fp_graph_index = fopen($cacti_export_path . "/" . $path ."/graph_" . $graph["local_graph_id"] . ".html", "w");
			}else{
				export_log("Creating File  '" . $cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html'");
				$fp_graph_index = fopen($cacti_export_path . "/graph_" . $graph["local_graph_id"] . ".html", "w");
			}

			fwrite($fp_graph_index, HTML_HEADER_TREE);

			/* write the code for the tree at the left */
			draw_html_left_tree($fp_graph_index, $tree_id);

			fwrite($fp_graph_index, HTML_GRAPH_HEADER_ONE_TREE);
			fwrite($fp_graph_index, "<strong>Graph - " . $graph["title_cache"] . "</strong></td></tr>");
			fwrite($fp_graph_index, HTML_GRAPH_HEADER_TWO_TREE);
			fwrite($fp_graph_index, "<td>");

			/* reset vars for actual graph image creation */
			reset($rras);
			unset($graph_data_array);

			/* generate graphs for each rra */
			foreach ($rras as $rra) {
				$graph_data_array["export"] = true;

				if (read_config_option("export_tree_isolation") == "on") {
					$graph_data_array["export_filename"] = "/" . $path . "/graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
				}else{
					$graph_data_array["export_filename"] = "/graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png";
				}

				export_log("Creating Graph '" . $cacti_export_path . $graph_data_array["export_filename"] . "'");

				@rrdtool_function_graph($graph["local_graph_id"], $rra["id"], $graph_data_array, $rrdtool_pipe);
				$total_graphs_created++;

				/* write image related html */
				if (read_config_option("export_tree_isolation") == "off") {
					fwrite($fp_graph_index, "<div align=center><img src='graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png' border=0></div>\n
						<div align=center><strong>".$rra["name"]."</strong></div><br>");
				}else{
					fwrite($fp_graph_index, "<div align=center><img src='" . "graphs/graph_" . $graph["local_graph_id"] . "_" . $rra["id"] . ".png' border=0></div>\n
						<div align=center><strong>".$rra["name"]."</strong></div><br>");
				}
			}

			fwrite($fp_graph_index, "</td></tr></table></td></tr></table>");
			fwrite($fp_graph_index, HTML_FOOTER_TREE);
			fclose($fp_graph_index);
		}
	}
	}

	/* close the rrdtool pipe */
	rrd_close($rrdtool_pipe);

	return $total_graphs_created;
}

function draw_html_left_tree($fp, $tree_id)  {
	/* create the treeview representation for the html data */
	grow_dhtml_trees_export($fp,$tree_id);

	fwrite($fp,"</td>\n");
	fwrite($fp,"<td valign='top'>\n");
}

function grow_dhtml_trees_export($fp, $tree_id) {
	global $colors, $config, $dhtml_trees;
	include_once($config["library_path"] . "/tree.php");
	include_once($config["library_path"] . "/data_query.php");

	fwrite($fp, "<div id=\"jtree\">\n");

	if (read_config_option("export_tree_isolation") == "off") {
		$dhtml_tree_base = 0;
	}else{
		$dhtml_tree_base = $tree_id;
	}

	if (!isset($dhtml_trees[$dhtml_tree_base])) {
		$dhtml_tree = create_dhtml_tree_export($dhtml_tree_base);
		$dhtml_trees[$dhtml_tree_base] = $dhtml_tree;
	}else{
		$dhtml_tree = $dhtml_trees[$dhtml_tree_base];
	}

	foreach($dhtml_tree as $key => $item){
		if ($key > 1) {
			fwrite($fp,$item);
		}
	}

	fwrite($fp, "</div>\n");
	fwrite($fp, "<script type=\"text/javascript\">\n");
	fwrite($fp, "$(function () {
	$(\"#jtree\")
		.jstree({
                        \"plugins\" : [\"ui\",\"themes\",\"html_data\",\"cookies\"],
        	        \"themes\" : {\"icons\" : false,
				\"url\" : \"./js/style.css\"},
			\"cookies\" : {
				\"save_opened\" : \"Cacti_jstree_open\",
				\"save_selected\" : \"Cacti_jstree_select\"
				}

                })

                // Make sure that the nodes are actually used as links
                // We need reselect to prevent endless loops
                // https://groups.google.com/d/topic/jstree/j6XNq9hQdeA/discussion
                .bind(\"reselect.jstree\", function (e, data) {
                      data.inst.get_container().bind(\"select_node.jstree\", function (e, data) {
                           // data.rstl.obj is the object that was selected.
                           document.location.href = data.rslt.obj.children(\"a\").attr(\"href\");
                       });
                });

});\n");
	fwrite($fp, "</script>\n");

}

/* get_graph_tree_array_export - returns a list of graph trees taking permissions into account if
     necessary
   @arg $return_sql - (bool) Whether to return the SQL to create the dropdown rather than an array
	@arg $force_refresh - (bool) Force the refresh of the array from the database
   @returns - (array) an array containing a list of graph trees */
function get_graph_tree_array_export($return_sql = false, $force_refresh = false) {
	global $config;

	/* set the tree update time if not already set */
	if (!isset($config["config_options_array"]["tree_update_time"])) {
		$config["config_options_array"]["tree_update_time"] = time();
	}

	/* build tree array */
	if (!isset($config["config_options_array"]["tree_array"]) || ($force_refresh) ||
		(($config["config_options_array"]["tree_update_time"] + read_graph_config_option("page_refresh")) < time())) {

		if (read_config_option("auth_method") != 0) {
			$current_user = db_fetch_row("SELECT id, policy_trees FROM user_auth WHERE id=" . read_config_option("export_user_id"));

			if ($current_user["policy_trees"] == "1") {
				$sql_where = "WHERE user_auth_perms.user_id IS NULL";
			}elseif ($current_user["policy_trees"] == "2") {
				$sql_where = "WHERE user_auth_perms.user_id IS NOT NULL";
			}

			$sql = "SELECT
				graph_tree.id,
				graph_tree.name,
				user_auth_perms.user_id
				FROM graph_tree
				LEFT JOIN user_auth_perms ON (graph_tree.id=user_auth_perms.item_id AND user_auth_perms.type=2 AND user_auth_perms.user_id=" . $current_user["id"] . ")
				$sql_where
				ORDER BY graph_tree.name";
		}else{
			$sql = "SELECT * FROM graph_tree ORDER BY name";
		}

		$config["config_options_array"]["tree_array"] = $sql;
		$config["config_options_array"]["tree_update_time"] = time();
	} else {
		$sql = $config["config_options_array"]["tree_array"];
	}

	if ($return_sql == true) {
		return $sql;
	}else{
		return db_fetch_assoc($sql);
	}
}

function create_dhtml_tree_export($tree_id) {
	/* record start time */
	list($micro,$seconds) = explode(" ", microtime());
	$start = $seconds + $micro;
	$search_key = "";

	$dhtml_tree = array();
	$dhtml_tree[0] = $start;
	$dhtml_tree[1] = read_graph_config_option("expand_hosts");
	$i = 1;

	$tree_list = get_graph_tree_array_export();

	/* auth check for hosts on the trees */
	$current_user = db_fetch_row("SELECT * FROM user_auth WHERE id=" . read_config_option("export_user_id"));

	$sql_join  = "LEFT JOIN user_auth_perms ON ((graph_templates_graph.local_graph_id=user_auth_perms.item_id and user_auth_perms.type=1 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (host.id=user_auth_perms.item_id AND user_auth_perms.type=3 AND user_auth_perms.user_id=" . $current_user["id"] . ") OR (graph_templates.id=user_auth_perms.item_id AND user_auth_perms.type=4 AND user_auth_perms.user_id=" . $current_user["id"] . "))";

	if ($current_user["policy_hosts"] == "1") {
		$sql_where = "AND !(user_auth_perms.user_id IS NOT NULL AND graph_tree_items.host_id>0)";
	}elseif ($current_user["policy_hosts"] == "2") {
		$sql_where = "AND !(user_auth_perms.user_id IS NULL AND graph_tree_items.host_id>0)";
	}

	if (sizeof($tree_list) > 0) {
	foreach ($tree_list as $tree) {
		if (((read_config_option("export_tree_isolation") == "on") && ($tree_id == $tree["id"])) ||
			(read_config_option("export_tree_isolation") == "off")) {


			$hier_sql = "SELECT DISTINCT
					graph_tree_items.id,
					graph_tree_items.title,
					graph_tree_items.order_key,
					graph_tree_items.host_id,
					graph_tree_items.host_grouping_type,
					host.description as hostname
					FROM (graph_tree_items, graph_templates_graph)
					LEFT JOIN host ON (host.id=graph_tree_items.host_id)
					LEFT JOIN graph_templates ON (graph_templates_graph.graph_template_id=graph_templates.id)
					$sql_join
					WHERE graph_tree_items.graph_tree_id=" . $tree["id"] . "
					$sql_where
					AND graph_tree_items.local_graph_id = 0
					ORDER BY graph_tree_items.order_key";

			$hierarchy = db_fetch_assoc($hier_sql);

			$dhtml_tree_id = 0;

			if (sizeof($hierarchy) > 0) {
				$last_tier = 1;
				$openli = false;
				$lasthost = false;
				$opentree = false;
				foreach ($hierarchy as $leaf) {
					if ($dhtml_tree_id <> $tree["id"]) {
						if ($opentree) {
							$i++;
							$dhtml_tree[$i] = "\t\t\t</ul>\n\t\t</li>\n\t</ul>\n";
						}
						$i++;
						$clean_id = clean_up_export_name(get_tree_name($tree["id"]));
						$dhtml_tree[$i] = "\t<ul>\n\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . "_leaf.html\">" . get_tree_name($tree["id"]) . "</a>\n\t\t\t<ul>\n";
						$opentree = true;
					}
					$dhtml_tree_id = $tree["id"];

					$tier = tree_tier($leaf["order_key"]);

					if ($leaf["host_id"] > 0) {  //It's a host
						if ($tier > $last_tier) {
							$i++;
							$dhtml_tree[$i] = "\t\t\t<ul>\n";
						} elseif ($tier < $last_tier) {
							if (!$lasthost) {
								$i++;
								$dhtml_tree[$i] = "\t\t\t\t</li>\n";
							}
							for ($x = $tier; $x < $last_tier; $x++) {
								$i++;
								$dhtml_tree[$i] = "\t\t\t</ul>\n\t\t\t\t</li>\n";
								$openli = false;
							}
						} elseif ($openli && !$lasthost) {
							$i++;
							$dhtml_tree[$i] = "\t\t\t\t</li>\n";
							$openli = false;
						}
						$last_tier = $tier;
						$lasthost = true;
						$i++;
						$clean_id = clean_up_export_name($leaf["hostname"] . "_" . $leaf["id"]);
						$dhtml_tree[$i] = "\t\t\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . ".html\">Host: " . htmlspecialchars($leaf["hostname"]) . "</a>\n";

						if (read_config_option("export_tree_expand_hosts") == "on") {
							$i++;
							$dhtml_tree[$i] = "\t\t\t\t\t<ul>\n";
							if ($leaf["host_grouping_type"] == HOST_GROUPING_GRAPH_TEMPLATE) {
								$graph_templates = db_fetch_assoc("SELECT
									graph_templates.id,
									graph_templates.name,
									graph_templates_graph.local_graph_id,
									graph_templates_graph.title_cache
									FROM (graph_local,graph_templates,graph_templates_graph)
									WHERE graph_local.id=graph_templates_graph.local_graph_id
									AND graph_templates_graph.graph_template_id=graph_templates.id
									AND graph_local.host_id=" . $leaf["host_id"] . "
									AND graph_templates_graph.export='on'
									GROUP BY graph_templates.id
									ORDER BY graph_templates.name");

							 	if (sizeof($graph_templates) > 0) {
									foreach ($graph_templates as $graph_template) {
										$i++;
										$clean_id = clean_up_export_name($leaf["hostname"] . "_gt_" . $leaf["id"] . "_" . $graph_template["id"]);
										$dhtml_tree[$i] = "\t\t\t\t\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . ".html\">" . htmlspecialchars($graph_template["name"]) . "</a></li>\n";
									}
								}
							}else if ($leaf["host_grouping_type"] == HOST_GROUPING_DATA_QUERY_INDEX) {
								$data_queries = db_fetch_assoc("SELECT
									snmp_query.id,
									snmp_query.name
									FROM (graph_local,snmp_query)
									WHERE graph_local.snmp_query_id=snmp_query.id
									AND graph_local.host_id=" . $leaf["host_id"] . "
									GROUP BY snmp_query.id
									ORDER BY snmp_query.name");

								array_push($data_queries, array(
									"id" => "0",
									"name" => "Non Query Based"
									));

								if (sizeof($data_queries) > 0) {
									foreach ($data_queries as $data_query) {
										$i++;
										$clean_id = clean_up_export_name($leaf["hostname"] . "_dq_" . $leaf["title"] . "_" . $leaf["id"] . "_" . $data_query["id"]);
										$dhtml_tree[$i] = "\t\t\t\t\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . ".html\">" . htmlspecialchars($data_query["name"]) . "</a>\n";

										/* fetch a list of field names that are sorted by the preferred sort field */
										$sort_field_data = get_formatted_data_query_indexes($leaf["host_id"], $data_query["id"]);

										if ($data_query["id"] > 0) {
											$i++;
											$dhtml_tree[$i] = "\t\t\t\t\t\t\t<ul>\n";
											while (list($snmp_index, $sort_field_value) = each($sort_field_data)) {
												$i++;
												$clean_id = clean_up_export_name($leaf["hostname"] . "_dqi_" . $leaf["id"] . "_" . $data_query["id"] . "_" . $snmp_index);
												$dhtml_tree[$i] = "\t\t\t\t\t\t\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . ".html\">" . htmlspecialchars($sort_field_value) . "</a></li>\n";
											}
											$i++;
											$dhtml_tree[$i] = "\t\t\t\t\t\t\t</ul>\n";
										}
										$i++;
										$dhtml_tree[$i] = "\t\t\t\t\t\t</li>\n";
									}
								}
							}
							$i++;
							$dhtml_tree[$i] = "\t\t\t\t\t</ul>\n";
						}
						$i++;
						$dhtml_tree[$i] = "\t\t\t\t</li>\n";
					}else { //It's not a host
						if ($tier > $last_tier) {
							$i++;
							$dhtml_tree[$i] = "\t\t\t<ul>\n";
						} elseif ($tier < $last_tier) {
							if (!$lasthost) {
								$i++;
								$dhtml_tree[$i] = "</li>\n";
							}
							for ($x = $tier; $x < $last_tier; $x++) {
								$i++;
								$dhtml_tree[$i] = "\t\t\t\t</ul>\n\t\t\t\t</li>\n";
								$openli = false;
							}
						} elseif ($openli && !$lasthost) {
							$i++;
							$dhtml_tree[$i] = "</li>\n";
							$openli = false;
						}
						$last_tier = $tier;
						$i++;
						$clean_id = clean_up_export_name(get_tree_name($tree["id"]) . "_" . $leaf["title"] . "_" . $leaf["id"]);
						$dhtml_tree[$i] = "\t\t\t\t<li id=\"" . $clean_id . "\"><a href=\"" . $clean_id . "_leaf.html\">" . htmlspecialchars($leaf["title"]) . "</a>\n";
						$openli = true;
						$lasthost = false;
					}
				}
				for ($x = $last_tier; $x > 1; $x--) {
					$i++;
					$dhtml_tree[$i] = "\t\t\t\t\t</ul>\n\t\t\t\t</li>\n";
				}
				$i++;
				$dhtml_tree[$i] = "\t\t\t</ul>\n\t\t</li>\n\t</ul>\n";
			}else{
				if ($dhtml_tree_id <> $tree["id"]) {
					$i++;
					$clean_id = clean_up_export_name(get_tree_name($tree["id"]));
					$dhtml_tree[$i] = "\t<ul>\n\t\t<li id=\"" . $clean_id . "_leaf\"><a href=\"" . $clean_id . "_leaf.html\">" . get_tree_name($tree["id"]) . "</a></li>\n\t</ul>";
				}
			}
		}
	}
	}

	return $dhtml_tree;
}

/* create_export_directory_structure - builds the export directory strucutre and copies
   graphics and treeview scripts to those directories.
   @arg $cacti_root_path - the directory where Cacti is installed
        $dir - the export directory where graphs will either be staged or located.
*/
function create_export_directory_structure($cacti_root_path, $dir) {
	/* create the jquery sub-directory */
	if (!is_dir("$dir/js")) {
		if (!mkdir("$dir/js", 0755)) {
			export_fatal("Create directory '" . $dir . "/js' failed.  Can not continue");
		}
	}

	/* create the graphs sub-directory */
	if (!is_dir("$dir/graphs")) {
		if (!mkdir("$dir/graphs", 0755)) {
			export_fatal("Create directory '" . $dir . "/graphs' failed.  Can not continue");
		}
	}

	/* css */
	copy("$cacti_root_path/include/main.css", "$dir/main.css");

	/* images for html */
	copy("$cacti_root_path/images/tab_cacti.gif", "$dir/tab_cacti.gif");
	copy("$cacti_root_path/images/cacti_backdrop.gif", "$dir/cacti_backdrop.gif");
	copy("$cacti_root_path/images/transparent_line.gif", "$dir/transparent_line.gif");
	copy("$cacti_root_path/images/shadow.gif", "$dir/shadow.gif");
	copy("$cacti_root_path/images/shadow_gray.gif", "$dir/shadow_gray.gif");

	/* java scripts for the tree */
	copy("/usr/share/javascript/jquery/jquery.js", "$dir/js/jquery.js");
	copy("$cacti_root_path/include/js/jquery/jquery.jstree.js", "$dir/js/jquery.jstree.js");
	copy("/usr/share/javascript/jquery-cookie/jquery.cookie.js", "$dir/js/jquery.cookie.js");

	/* theme info for java scripts */
	copy("$cacti_root_path/include/js/jquery/themes/default/style.css", "$dir/js/style.css");
	copy("$cacti_root_path/include/js/jquery/themes/default/d.png", "$dir/js/d.png");
	copy("$cacti_root_path/include/js/jquery/themes/default/d.gif", "$dir/js/d.gif");
	copy("$cacti_root_path/include/js/jquery/themes/default/throbber.gif", "$dir/js/throbber.gif");
}

function get_host_description($host_id) {
	$host = db_fetch_row("SELECT description FROM host WHERE id='".$host_id."'");
	return $host["description"];
}

function get_host_id($tree_item_id) {
	$graph_tree_item=db_fetch_row("SELECT host_id FROM graph_tree_items WHERE id='".$tree_item_id."'");
	return $graph_tree_item["host_id"];
}

function get_tree_name($tree_id) {
	$graph_tree=db_fetch_row("SELECT id, name FROM graph_tree WHERE id='".$tree_id."'");
	return $graph_tree["name"];
}

function get_tree_item_title($tree_item_id) {
	if (get_tree_item_type($tree_item_id) == "host")  {
		$tree_item=db_fetch_row("SELECT host_id FROM graph_tree_items WHERE id='".$tree_item_id."'");
		return get_host_description($tree_item["host_id"]);
	}else{
		$tree_item=db_fetch_row("SELECT title FROM graph_tree_items WHERE id='".$tree_item_id."'");
		return $tree_item["title"];
	}
}

/* clean_up_export_name - runs a string through a series of regular expressions designed to
     eliminate "bad" characters
   @arg $string - the string to modify/clean
   @returns - the modified string */
function clean_up_export_name($string) {
	$string = preg_replace("/[\s\ ]+/", "_", $string);
	$string = preg_replace("/[^a-zA-Z0-9_.]+/", "", $string);
	$string = preg_replace("/_{2,}/", "_", $string);

	return $string;
}

/* $path to the directory to delete or clean */
/* $deldir (optionnal parameter, true as default) delete the diretory (true) or just clean it (false) */
function del_directory($path, $deldir = true) {
	/* check if the directory name have a "/" at the end, add if not */
	if ($path[strlen($path)-1] != "/") {
		$path .= "/";
	}

	/* cascade through the directory structure(s) until they are all delected */
	if (is_dir($path)) {
		$d = opendir($path);
		while ($f = readdir($d)) {
			if ($f != "." && $f != "..") {
				$rf = $path . $f;

				/* if it is a directory, recursive call to the function */
				if (is_dir($rf)) {
					del_directory($rf);
				}else if (is_file($rf) && is_writable($rf)) {
					unlink($rf);
				}
			}
		}
		closedir($d);

		/* if $deldir is true, remove the directory */
		if ($deldir && is_writable($path)) {
			rmdir($path);
		}
	}
}

function check_remove($filename) {
	if (file_exists($filename) && is_writable($filename)) {
		unlink($filename);
	}
}

define("HTML_HEADER_TREE",
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
<html>
<head>
	<title>Cacti</title>
	<link href='main.css' rel='stylesheet'>
	<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
	<meta http-equiv=refresh content='300'; url='index.html'>
	<meta http-equiv=Pragma content=no-cache>
	<meta http-equiv=cache-control content=no-cache>
	<script type=\"text/javascript\" src=\"./js/jquery.js\" language=\"javascript\"></script>
	<script type=\"text/javascript\" src=\"./js/jquery.cookie.js\" language=\"javascript\"></script>
	<script type=\"text/javascript\" src=\"./js/jquery.jstree.js\" language=\"javascript\"></script>
</head>
<body>
<table style='width:100%;height:100%;' cellspacing='0' cellpadding='0'>
	<tr style='height:37px;' bgcolor='#a9a9a9'>
		<td colspan='2' valign='bottom' nowrap>
			<table width='100%' cellspacing='0' cellpadding='0'>
				<tr>
					<td nowrap>
						&nbsp;<a href='http://www.cacti.net/'><img src='tab_cacti.gif' alt='Cacti - http://www.cacti.net/' align='middle' border='0'></a>
					</td>
					<td align='right'>
						<img src='cacti_backdrop.gif' align='middle'>
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr style='height:2px;' colspan='2' bgcolor='#183c8f'>
		<td colspan='2'>
			<img src='transparent_line.gif' style='width:170px;height:2px;' border='0'><br>
		</td>
	</tr>
	<tr style='height:5px;' bgcolor='#e9e9e9'>
		<td colspan='2'>
			<table width='100%'>
				<tr>
					<td>
						Exported Graphs
					</td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
		<td bgcolor='#efefef' colspan='1' style='height:8px;background-image: url(shadow_gray.gif); background-repeat: repeat-x; border-right: #aaaaaa 1px solid;'>
			<img src='transparent_line.gif' width='200' style='height:2px;' border='0'><br>
		</td>
		<td bgcolor='#ffffff' colspan='1' style='height:8px;background-image: url(shadow.gif); background-repeat: repeat-x;'>
		</td>
	</tr>
	<tr>
		<td valign='top' style='padding: 5px; border-right: #aaaaaa 1px solid;' bgcolor='#efefef' width='200'>
			<table border=0 cellpadding=0 cellspacing=0><tr><td><font size=-2><a style='font-size:7pt;text-decoration:none;color:silver' href='http://www.treemenu.net/' target=_blank></a></font></td></tr></table>\n"
);

define("HTML_GRAPH_HEADER_ONE_TREE", "
	<table width='100%' style='background-color: #f5f5f5; border: 1px solid #bbbbbb;' align='center' cellpadding='3'>
		<tr bgcolor='#6d88ad'>
			<td width='390' colspan='3' class='textHeaderDark'>");

define("HTML_GRAPH_HEADER_TWO_TREE", "
		<tr>"
);

define("HTML_GRAPH_HEADER_ICE_TREE", "</td></tr></table><br><br>			<br>
		</td>
	</tr>
</table>\n");

define("HTML_GRAPH_FOOTER_TREE", "
	</tr></table>\n"
);

define("HTML_FOOTER_TREE", "
				</tr>
			</table>
		</td>
	</tr>
</table>
</body>
</html>"
);

/* HTML header for the Classic Presentation */
define("HTML_HEADER_CLASSIC", "
	<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">
	<html>
	<head>
		<title>cacti</title>
		<link href='main.css' rel='stylesheet'>
		<meta http-equiv='Content-Type' content='text/html;charset=utf-8'>
		<meta http-equiv=refresh content='300'; url='index.html'>
		<meta http-equiv=Pragma content=no-cache>
		<meta http-equiv=cache-control content=no-cache>
	</head>
	<body>
	<table width='100%' cellspacing='0' cellpadding='0'>
		<tr style='height:37px;' bgcolor='#a9a9a9'>
			<td valign='bottom' colspan='3' nowrap>
				<table width='100%' cellspacing='0' cellpadding='0'>
					<tr>
						<td valign='bottom'>
							&nbsp;<a href='http://www.cacti.net/'><img src='tab_cacti.gif' alt='Cacti - http://www.cacti.net/' align='middle' border='0'></a>
						</td>
						<td align='right'>
							<img src='cacti_backdrop.gif' align='middle'>
						</td>
					</tr>
				</table>
			</td>
		</tr>\n
		<tr style='height:2px;' bgcolor='#183c8f'>
			<td colspan='3'>
				<img src='transparent_line.gif' style='width:170px;height:2px;' border='0'><br>
			</td>
		</tr>\n
		<tr style='height:5px;' bgcolor='#e9e9e9'>
			<td colspan='3'>
				<table width='100%'>
					<tr>
						<td>
							Exported Graphs
						</td>
					</tr>
				</table>
			</td>
		</tr>\n
		<tr>
			<td colspan='3' style='height:8px;background-image: url(shadow.gif); background-repeat: repeat-x;' bgcolor='#ffffff'>

			</td>
		</tr>\n
	</table>

	<br>"
);

/* Traditional Graph Export Representation Graph Headers */
define("HTML_GRAPH_HEADER_ONE_CLASSIC", "
	<table width='100%' style='background-color: #f5f5f5; border: 1px solid #bbbbbb;' align='center'>
		<tr bgcolor='#" . $colors["header_panel"] . "'>
			<td colspan='" . read_config_option("export_num_columns") . "'>
				<table width='100%' cellspacing='0' cellpadding='3' border='0'>
					<tr>
						<td align='center' class='textHeaderDark'>"
);

define("HTML_GRAPH_HEADER_TWO_CLASSIC", "
					</tr>
				</table>
			</td>
		</tr>
		<tr>"
);

define("HTML_GRAPH_FOOTER_CLASSIC", "
	</tr></table>\n
	<br><br>"
);

define("HTML_FOOTER_CLASSIC", "
	<br>

	</body>
	</html>"
);

?>