summaryrefslogtreecommitdiffstats
path: root/lib/class/catalog.class.php
blob: 72ed8cbe796806a732898e54d2b0e1c35928cad5 (plain)
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
='#n175'>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 1978 1979 1980 1981 1982 1983 1984 1985 1986
<?
/*

 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.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
/*!
	@header Catalog Class
	This class handles all actual work in regards to the catalog, it contains functions for creating/listing/updated the catalogs.
*/

class Catalog {

	var $name;
	var $last_update;
	var $last_add;
	var $id3_set_command;
	var $rename_pattern;
	var $sort_pattern;
	var $catalog_type;

	// Used in functions
	var $albums	= array();
	var $artists	= array();
	var $genres	= array();

	/*!
		@function Catalog
		@discussion Catalog class constructor, pulls catalog information
		@param $catalog_id 	The ID of the catalog you want to build information from
	 */
	function Catalog($catalog_id = 0) {

		/* If we have passed an id then do something */
		if ($catalog_id) {
			/* Assign id for use in get_info() */
			$this->id = $catalog_id;

			/* Get the information from the db */
			$info = $this->get_info();

			/* Assign Vars */
			$this->path        	= $info->path;
			$this->name        	= $info->name;
			$this->last_update 	= $info->last_update;
			$this->last_add	   	= $info->last_add;
			$this->id3_set_command 	= $info->id3_set_command;
			$this->rename_pattern 	= $info->rename_pattern;
			$this->sort_pattern 	= $info->sort_pattern;
			$this->catalog_type	= $info->catalog_type;
		} //catalog_id

	} //constructor


	/*!
		@function get_info
		@discussion get's the vars for $this out of the database
		@param $this->id	Taken from the object
	*/
	function get_info() {

		/* Grab the basic information from the catalog and return it */
		$sql = "SELECT * FROM catalog WHERE id='$this->id'";
		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_object($db_results);

		return $results;

	} //get_info


	/*!
		@function get_catalogs
		@discussion Pull all the current catalogs
	*/
	function get_catalogs() {

		$sql = "SELECT id FROM catalog";
		$db_results = mysql_query($sql, dbh());

		while ($r = mysql_fetch_object($db_results)) {
			$results[] = new Catalog($r->id);
		}

		return $results;

	} // get_catalogs


	/*!
		@function get_catalog_stats
		@discussion Pulls information about number of songs etc for a specifc catalog, or all catalogs
			    calls many other internal functions, returns an object containing results
		@param $catalog_id If set tells us to pull from a single catalog, rather than all catalogs
	*/
	function get_catalog_stats($catalog_id=0) {

		$results->songs 	= $this->count_songs($catalog_id);
		$results->albums 	= $this->count_albums($catalog_id);
		$results->artists	= $this->count_artists($catalog_id);
		$results->size		= $this->get_song_size($catalog_id);
		$results->time		= $this->get_song_time($catalog_id);

	} // get_catalog_stats


	/*!
		@function get_song_time
		@discussion Get the total amount of time (song wise) in all or specific catalog
		@param $catalog_id If set tells ut to pick a specific catalog
	*/
	function get_song_time($catalog_id=0) {

		$sql = "SELECT SUM(song.time) FROM song";
		if ($catalog_id) {
			$sql .= " WHERE catalog='$catalog_id'";
		}

		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_field($db_results);

		/* Do some conversion to get Hours Min Sec */


		return $results;

	} // get_song_time


	/*!
		@function get_song_size
		@discussion Get the total size of songs in all or a specific catalog
		@param $catalog_id If set tells us to pick a specific catalog
	*/
	function get_song_size($catalog_id=0) {

		$sql = "SELECT SUM(song.size) FROM song";
		if ($catalog_id) {
			$sql .= " WHERE catalog='$catalog_id'";
		}

		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_field($db_results);

		/* Convert it into MB */
		$results = ($results / 1048576);

		return $results;

	} // get_song_size


	/*!
		@function count_artists
		@discussion Count the number of artists in all catalogs or in a specific one
		@param $catalog_id If set tells us to pick a specific catalog
	*/
	function count_artists($catalog_id=0) {

		$sql = "SELECT DISTINCT(song.artist) FROM song";
		if ($catalog_id) {
			$sql .= " WHERE catalog='$catalog_id'";
		}

		$db_results = mysql_query($sql,dbh());

		$results = mysql_num_rows($db_results);

		return $results;

	} // count_artists


	/*!
		@function count_albums
		@discussion Count the number of albums in all catalogs or in a specific one
		@param $catalog_id If set tells us to pick a specific catalog
	*/
	function count_albums($catalog_id=0) {

		$sql = "SELECT DISTINCT(song.album) FROM song";
		if ($catalog_id) {
			$sql .=" WHERE catalog='$catalog_id'";
		}

		$db_results = mysql_query($sql, dbh());

		$results = mysql_num_rows($db_results);

		return $results;

	} // count_albums


	/*!
		@function count_songs
		@discussion Count the number of songs in all catalogs, or a specific one
		@param $catalog_id If set tells us to pick a specific catalog
	*/
	function count_songs($catalog_id=0) {

		$sql = "SELECT count(*) FROM song";
		if ($catalog_id) {
			$sql .= " WHERE catalog='$catalog_id'";
		}

		$db_results = mysql_query($sql, dbh());
		$results = mysql_fetch_field($db_results);

		return $results;

	} // count_songs

	/*!
		@function add_file
		@discussion adds a single file
	*/
	function add_file($filename) { 

		$file_size = @filesize($filename);
		$pattern = "/\.[" . conf('catalog_file_pattern') . "]$/i";	
		
		if ( preg_match($pattern ,$filename) && ($file_size > 0) && (!preg_match('/\.AppleDouble/', $filename))  ) {
			if(!$this->check_local_mp3($filename,$gather_type)) { 
				$this->insert_local_song($filename,$file_size);
			}
		} // if valid file


	} // add_file


	/*!
		@function add_files
		@discussion  Recurses throught $this->path and pulls out all mp3s and returns the full
			     path in an array. Passes gather_type to determin if we need to check id3
			     information against the db.
		@param $path 		The root path you want to start grabing files from
		@param $gather_type=0   Determins if we need to check the id3 tags of the file or not
	 */
	function add_files($path,$gather_type=0,$parse_m3u=0) {
		/* Strip existing escape slashes and then add them again 
		   This is done because we keep adding to the dir (slashed) + (non slashed)
		   and a double addslashes would pooch things
		*/

		/* Open up the directory */
		$handle = opendir(stripslashes($path));

		if (!is_resource($handle)) {
                        if (conf('debug')) { log_event($_SESSION['userdata']['username'],'read',"Unable to Open $path",'ampache-catalog'); }
			echo "<font class=\"error\">" . _("Error: Unable to open") . " $path</font><br />\n";
		}

		/* Recurse through this dir and create the files array */
		while ( false !== ( $file = readdir($handle) ) ) {

			// Fix Found by Naund
			// Needed to protect from ' in filenames
			$file = sql_escape($file);
			
			// Prevent the script from timing out
			set_time_limit(0);
			
			/* if not .. or . */
			if ($file != "." AND $file != "..") {

				if (conf('debug')) { 
					log_event($_SESSION['userdata']['username'],'read',"Starting work on $file inside $path",'ampache-catalog');
				}

				/* Change the dir so is_dir works correctly */
				if (!@chdir(stripslashes($path))) {
					if (conf('debug')) { log_event($_SESSION['userdata']['username'],'read',"Unable to chdir $path",'ampache-catalog'); }
					echo "<font class=\"error\">" . _("Error: Unable to change to directory") . " $path</font><br />\n";
				}

				/* Create the new path */
				$full_file = stripslashes($path."/".$file);
				$full_file = str_replace("//","/",$full_file);
			
				if (conf('no_symlinks')) {
					if (is_link($full_file)) { $failed_check = 1; }
				}

				/* If it's a dir run this function again! */
				if (is_dir($full_file) AND !$failed_check) {
					$this->add_files($full_file,$gather_type,$parse_m3u);
					unset($failed_check);
				} //it's a directory

				/* If it's not a dir let's roll with it */
				else {
					/* Get the file information */
					$file_size = @filesize($full_file);

					if (!$file_size && $file_size != '0') { 
						echo "<font class=\"error\">" . _("Error: Unable to get filesize for") . " $full_file <br />";
						if (conf('debug')) { log_event($GLOBALS['user']->username,' read ',"Error: Unable to get filesize for $full_file",'ampache-catalog'); }
					} // if no filesize

					$pattern = "/\.(" . conf('catalog_file_pattern');
					if ($parse_m3u) { 
						$pattern .= "|m3u)$/i";
					}
					else { 
						$pattern .= ")$/i";
					}
					
					/* see if this is an mp3 file and if it is greater than 0 bytes */
					if ( preg_match($pattern ,$file) && ($file_size > 0) && (!preg_match('/\.AppleDouble/', $file))  ) {

						if (is_readable($full_file)) {

							if (substr($file,-3,3) == 'm3u') { 
								if ($this->import_m3u($full_file)) { 
									echo "&nbsp;&nbsp;&nbsp;" . _("Added Playlist From") . " $file . . . .<br />\n";
									flush();
								}
							} // if it's an m3u

							else {
						
							/* see if the current song is in the catalog */
							$found = $this->check_local_mp3($full_file,$gather_type);

							/* If not found then insert, gets id3 information
							 * and then inserts it into the database
							 */
							if (!$found) {
								$this->insert_local_song($full_file,$file_size);

								/* Stupid little cutesie thing */
								$this->count++;
								if ( !($this->count%conf('catalog_echo_count')) ) {
									echo _("Added") . " $this->count. . . . <br />\n";
									flush();
								} //echos song count

							} // not found

							} // if it's not an m3u
						
						} // is readable
						else {
							// not readable, warn user
				                        if (conf('debug')) { log_event($_SESSION['userdata']['username'],'read',"$full_file is not readable by ampache",'ampache-catalog'); }
							echo "$full_file " . _("is not readable by ampache") . ".<br />\n";

						}

					} //if it's a mp3 and is greater than 0 bytes

					else {
						if (conf('debug')) { 
							log_event($_SESSION['userdata']['username'],'read',"$full_file ignored, non audio file or 0 bytes",'ampache-catalog');
						}
					} // else not an audio file or 0 size

				} //else it's not a directory

			} //end if not . or ..

		} //end while

		if (conf('debug')) { 
			log_event($_SESSION['userdata']['username'],' closedir ',"Finished reading $path closing handle",'ampache-catalog');
		}

		/* Close the dir handle */
		@closedir($handle);

	} //add_files

	/*!
		@function get_albums
		@discussion This gets albums for all songs passed in an array
	*/
	function get_albums($songs=array()) { 

		foreach ($songs as $song_id) { 
			$sql = "SELECT album FROM song WHERE id='$song_id'";
			$db_results = mysql_query($sql, dbh());
			$results = mysql_fetch_array($db_results);
			$albums[] = new Album($results[0]);
		} // files

		return $albums;

	} // get_albums

	/*!
		@function get_album_art
		@discussion This runs through all of the albums and trys to find the 
			art for them from the mp3s
		//FIXME: Make the display a table so it all lines up
	*/
	function get_album_art($catalog_id=0,$methods=array()) { 

		if (!$catalog_id) { $catalog_id = $this->id; }

		// Get all of the albums in this catalog
		$albums = $this->get_catalog_albums($catalog_id);	
		
		// Run through them an get the art!
		foreach ($albums as $album) { 
			flush();
			if ($debug) { echo "&nbsp;&nbsp;&nbsp;&nbsp;" . $album->name . " -- "; }

			if ($methods['id3']) { 
				$found = $album->get_id3_art(); 
				if ($found && $debug) { echo _("Found in ID3") . "<br />\n"; }
			}
			if ($methods['amazon'] && !$found) { 
				$found = $album->get_amazon_art(); 	
				if ($found && $debug) { echo _("Found on Amazon") . "<br />\n"; }
			}
			if ($methods['folder'] && !$found) { 
				$found = $album->get_folder_art(); 
				if ($found && $debug) { echo _("Found in Folder") . "<br />\n"; }
			}
			if (count($methods) == '0' && !$found) { 
				$found = $album->get_art();
				if ($found && $debug) { echo _("Found") . "<br />\n"; }
			} 
	
			if (!$found && $debug) { echo "<font class=\"error\">" . _("Not Found") . "</font><br />\n"; }
			
			if ($found) { $art_found++; }	
			

			/* Stupid little cutesie thing */
                        $search_count++;
                        if ( !($search_count%conf('catalog_echo_count')) ) {
                                echo _("Searched") . " $search_count. . . . <br />\n";
	                        flush();
                        } //echos song count

			
			// Prevent the script from timing out
			set_time_limit(0);

			unset($found);

		} // foreach albums
		
		echo "$art_found album's with art. . .<br />\n";
		flush();

	} // get_album_art

	/*!
		@function get_catalog_albums()
		@discussion Returns an array of the albums from a catalog
	*/
	function get_catalog_albums($catalog_id=0) { 

		$results = array();

		/* Use $this->id if nothing is passed */
		if (!$catalog_id) { $catalog_id = $this->id; }
		
		$sql = "SELECT DISTINCT(album.id) FROM album,song WHERE song.catalog='$catalog_id' AND song.album=album.id";
		$db_results = mysql_query($sql, dbh());

		while ($r = mysql_fetch_object($db_results)) { 
			$results[] = new Album($r->id);
		}

		return $results;

	} // get_catalog_albums
	

	/*!
		@function get_catalog_files
		@discussion Returns an array of song objects from a catalog
		@param $catalog_id=0	Specify the catalog ID you want to get the files of
	*/
	function get_catalog_files($catalog_id=0) {

		$results = array();

		/* Use $this->id if nothing passed */
		if (!$catalog_id) { $catalog_id = $this->id; }

		$sql = "SELECT id FROM song WHERE catalog='$catalog_id' AND status='enabled'";
		$db_results = mysql_query($sql, dbh());

                $results = array(); // return an emty array instead of nothing if no objects
		while ($r = mysql_fetch_object($db_results)) {
			$results[] = new Song($r->id);
		} //end while

		return $results;

	} //get_catalog_files


	/*!
		@function get_disabled
		@discussion Gets an array of the disabled songs for all catalogs
			and returns full song objects with them
	*/
	function get_disabled() {
		global $conf;

		$results = array();

		$sql = "SELECT id FROM song WHERE status='disabled'";
		$db_results = mysql_query($sql, dbh());

		while ($r = mysql_fetch_array($db_results)) {
			$results[] = new Song($r['id']);
		}

		return $results;

	} // get_disabled


	/*!
		@function get_files
		@discussion  Get's an array of .mp3s and returns the filenames
		@param $path 	Get files starting at root $path
	*/
	function get_files($path) {

		/* Set it as an empty array */
		$files = array();

		$path = stripslashes($path);

		/* Open up the directory */
		$handle = @opendir($path);

		if (!is_resource($handle)) { echo "<font class=\"error\">" . _("Error: Unable to open") . " $path</font><br />\n"; }

		/* Change dir so we can tell if it's a directory */
		if (!@chdir($path)) {
			echo "<font class=\"error\">Error: Unable to change to $path directory</font><br />\n";
		}

		/* Recurse through this dir and create the files array */
		while ( FALSE !== ($file = @readdir($handle)) ) {

			$full_file = stripslashes($path . "/" . $file);
			$full_file = str_replace("//","/",$full_file);

			if (conf('no_symlinks')) {
				if (is_link($full_file)) { $failed_check = true; }
			}

			/* It's a dir */
			if (is_dir($full_file) AND $file != "." AND $file != ".." AND !$failed_check) {
				/* Merge the results of the get_files with the current results */
				$files = array_merge($files,$this->get_files($full_file));
			} //isdir

			/* Get the file information */
			$file_info = filesize($full_file);

			$pattern = "/\.[" . conf('catalog_file_pattern') . "]$/i";

			if ( preg_match($pattern ,$file) && ($file_info > 0) && (!preg_match("/\.AppleDouble/", $file)) ) {
				$files[] = $full_file;
			} //is mp3 of at least some size

		} //end while

		/* Close the dir handle */
		@closedir($handle);

		/* Return the files array */
		return $files;

	} //get_files

	/*!
		@function dump_album_art (Added by Cucumber 20050216)
		@discussion This runs through all of the albums and trys to dump the
			art for them into the 'folder.jpg' file in the appropriate dir
	*/
	function dump_album_art($catalog_id=0,$methods=array()) {
	        if (!$catalog_id) { $catalog_id = $this->id; }

	        // Get all of the albums in this catalog
	        $albums = $this->get_catalog_albums($catalog_id);

		echo "<br /><b>" . _("Starting Dump Album Art") . ". . .</b><br><br />\n";

		// Run through them an get the art!
		foreach ($albums as $album) {
	                flush();
                	if ($image = $album->get_db_art()) {
				/* Get the first song in the album */
                                $songs = $album->get_songs(1);
                                $song = $songs[0];
                                $dir = dirname($song->file);
				$extension = substr($image->art_mime,strlen($image->art_mime)-3,3);

	                        $preferred_filename = conf('album_art_preferred_filename');
	                        if (!$preferred_filename) { $preferred_filename = "folder.$extension"; }

	                        $file = "$dir/$preferred_filename";
	                        if ($file_handle = @fopen($file,"w")) {
		                        if (fwrite($file_handle, $image->art)) {
			                        $i++;
        	                                if ( !($i%conf('catalog_echo_count')) ) {
	                	                        echo _("Written") . " $i. . . <br />\n";
				                        flush();
	                                        } //echos song count
						if (conf('debug')) { log_event($_SESSION['userdata']['username'],'art_write',"$album->name Art written to $file"); }
	                                }
	                                        fclose($file_handle);
	                       	} // end if fopen
				else {
					if (conf('debug')) { log_event($_SESSION['userdata']['username'],'art_write',"Unable to open $file for writting"); }
					echo "<font class=\"error\">" . _("Error unable to open file for writting") . " [$file] </font><br />\n";
				}

                	} // end if image

	        } // end foreach

	        echo "<br /><b>" . _("Album Art Dump Complete") . "</b> &nbsp;&nbsp;";
	 	echo "<a href=\"" . conf('web_path') . "/admin/catalog.php" . "\">[" . _("Return") . "]</a>";

		flush();

	} // dump_album_art 

	/*!
		@function update_last_update
		@discussion updates the last_update of the catalog
	*/
	function update_last_update() {

		$date = time();
		$sql = "UPDATE catalog SET last_update='$date' WHERE id='$this->id'";
		$db_results = mysql_query($sql, dbh());

	} // update_last_update


	/*!
		@function update_last_add
		@discussion updates the last_add of the catalog
	*/
	function update_last_add() {

		$date = time();
		$sql = "UPDATE catalog SET last_add='$date' WHERE id='$this->id'";
		$db_results = mysql_query($sql, dbh());

	} // update_last_add


	/*!
		@function new_catalog
		@discussion  The Main array for making a new catalog calls many other child functions within this class
		@param $path Root path to start from for catalog
		@param $name Name of the new catalog
	*/
	function new_catalog($path,$name, $id3cmd=0, $ren=0, $sort=0, $type=0,$gather_art=0,$parse_m3u=0,$art=array()) {

		/* Record the time.. time the catalog gen */
		$start_time = time();

		/* Flush anything that has happened so they don't think it's locked */
		flush();

		/*
		 * Step one Add this to the catalog table if it's not
		 * already there returns the new catalog_id
		 */
		$catalog_id = $this->check_catalog($path);

		if (!$catalog_id) {
			$catalog_id = $this->create_catalog_entry($path,$name,$id3cmd, $ren, $sort, $type);
		}

		/* Setup the $this with the new information */
		$this->id = $catalog_id;
		$this->path = $path;
		$this->name = $name;
		$this->id3_set_command = ($id3cmd)?$id3cmd:'';
		$this->rename_pattern = ($ren)?$ren:'';
		$this->sort_pattern = ($sort)?$sort:'';
		$this->catalog_type = $type;

		/* Fluf */
		echo _("Starting Catalog Build") . " [$name]<br />\n";
		flush();


	       if ($this->catalog_type == 'remote') {
                        echo _("Running Remote Sync") . ". . .<br /><br />";
                        flush();
                        $this->get_remote_catalog($type=0);
                        return true;
                }
		/* Get the songs and then insert them into the db */
		$this->add_files($this->path,$type,$parse_m3u);

		/* Now Adding Album Art? */
		if ($gather_art) { 
                        echo "<br />\n<b>" . _("Starting Album Art Search") . ". . .</b><br />\n";
                        flush();
                        $this->get_album_art(0,$art);
		} // if we want to gather album art

		/* Do a little stats mojo here */
		$current_time = time();

		$time_diff = $current_time - $start_time;
		if ($time_diff) { $song_per_sec = intval($this->count/$time_diff); }
		echo _("Catalog Finished") . ". . . " . _("Total Time") . " [" . date("i:s",$time_diff) . "] " . _("Total Songs") . " [" . $this->count . "] " . 
			_("Songs Per Seconds") . " [" . $song_per_sec . "]<br />\n";

		return $catalog_id;

	} //new_catalog

	/*!
		@function update_single_item
		@discussion updates a single album,artist,song
	*/
	function update_single_item($type,$id) { 

		$songs = array();

		switch ($type) {
			case 'album':
				$album = new Album($id);
				$songs = $album->get_songs();
				break;
			case 'artist':
				$artist = new Artist($id);
				$songs = $artist->get_songs();
				break;
			case 'song':
				$songs[0] = new Song($id);
				break;
		} // end switch type

		foreach($songs as $song) { 

			$info = $this->update_song_from_tags($song);

                        if ($info['change']) {
                                echo "<dl>\n\t<li>";
                                echo "<b>$song->file " . _("Updated") . "</b>\n";
                                echo $info['text'];
                                echo "\t</li>\n</dl><hr align=\"left\" width=\"50%\" />";
                        	flush();
	                } // if change
			else {
				echo"<dl>\n\t<li>";
				echo "<b>$song->file</b><br />" . _("No Update Needed") . "\n";
				echo "\t</li>\n</dl><hr align=\"left\" width=\"50%\" />";
				flush();
			}
		} // foreach songs

	} // update_single_item

        /*!
                @function update_song_from_tags
                @discussion updates the song info based on tags
        */
        function update_song_from_tags($song) {

                $info = new Audioinfo();
                $results = $info->Info($song->file);

                /* Find the correct key */
                $key = get_tag_type($results);

		/* Fill Missing Information */
		$results = $song->fill_info($results,$this->sort_pattern . "/" . $this->rename_pattern, $this->id, $key);

                /* Clean up the tags */
                $results = clean_tag_info($results,$key,$song->file);

                /* Setup the vars */
		$new_song 		= new Song();
                $new_song->file         = $results['file'];
                $new_song->title        = $results['title'];
                $new_song->year         = $results['year'];
                $new_song->comment      = $results['comment'];
                $new_song->bitrate      = $results['bitrate'];
                $new_song->rate         = $results['rate'];
                $new_song->mode         = $results['mode'];
                $new_song->size         = $results['size'];
                $new_song->time         = $results['time'];
                $new_song->track        = $results['track'];
                $artist                 = $results['artist'];
                $album                  = $results['album'];
                $genre                  = $results['genre'];

		/* Clean up Old Vars */
		unset($results,$key,$info);

                /*
                * We have the artist/genre/album name need to check it in the tables
                * If found then add & return id, else return id
                */
                $new_song->artist       = $this->check_artist($artist);
                $new_song->f_artist     = $artist;
                $new_song->genre        = $this->check_genre($genre);
                $new_song->f_genre      = $genre;
                $new_song->album        = $this->check_album($album,$new_song->year);
                $new_song->f_album      = $album . " - " . $new_song->year;
                $new_song->title        = $this->check_title($new_song->title,$new_song->file);

                $info = $song->compare_song_information($song,$new_song);

                if ($info['change']) {
                        $song->update_song($song->id,$new_song);
                }

                return $info;

        } // update_song_from_tags

	/*!
		@function add_to_catalog
		@discussion this function adds new files to an
			existing catalog
	*/
	function add_to_catalog($type=0) { 

		echo _("Starting New Song Search on") . " <b>[$this->name]</b> " . _("catalog") . "<br /><br />\n";
		flush();

		if ($this->catalog_type == 'remote') { 
			echo _("Running Remote Update") . ". . .<br /><br />";
			flush();
			$this->get_remote_catalog($type=0);
			return true;
		} 

		/* Set the Start time */
		$start_time = time();

		/* Get the songs and then insert them into the db */
		$this->add_files($this->path,$type);

		/* Do a little stats mojo here */
		$current_time = time();

		if ($type != "fast_add") { 	
			echo "<b>" . _("Starting Album Art Search") . ". . .</b><br />\n"; 
			flush();
			$this->get_album_art(); 
		} 

		/* Update the Catalog last_update */
		$this->update_last_add();

		$time_diff = $current_time - $start_time;
		if ($time_diff) {
			$song_per_sec = intval($this->count/$time_diff);
		}
		if (!$song_per_sec) {
			$song_per_sec = "N/A";
		}
		if (!$this->count) {
			$this->count = 0;
		}

		echo "\n<br />" . _("Catalog Update Finished") . "... " . _("Total Time") . " [" . date("i:s",$time_diff) . "] " .
			_("Total Songs") . " [" . $this->count . "] " . _("Songs Per Seconds") . " [" . $song_per_sec . "]<br /><br />";

	} // add_to_catalog


	/**
	 * get_remote_catalog
	 * get a remote catalog and runs update if needed
	 * @package XMLRPC
	 * @catagory Client
	 * @author Karl Vollmer
	 * @todo Add support for something besides port 80
	 * @todo Add a Pub/Private Key swap in here for extra security
	 */
	function get_remote_catalog($type=0) { 

		/* Make sure the xmlrpc lib is loaded */
		if (!class_exists('xmlrpc_client')) { 
                        if (conf('debug')) { log_event($_SESSION['userdata']['username'],'xmlrpc',"Unable to load XMLRPC library"); }
			echo "<font class=\"error\"><b>" . _("Error") . "</b>: " . _("Unable to load XMLRPC library, make sure XML-RPC is enabled") . "<br />\n";
			return false;
		} // end check for class

	        // first, glean out the information from the path about the server and remote path
		// this can't contain the http
	        preg_match("/http:\/\/([^\/]+)\/*(.*)/", $this->path, $match);
	        $server = $match[1];
	        $path   = $match[2];
	
	        if ( ! $path ) {
	                $client = new xmlrpc_client("/server.php", $server, 80);
	        }
	        else {
	                $client = new xmlrpc_client("/$path/server.php", $server, 80);
	        }
	        
		$f = new xmlrpcmsg('remote_catalog_query', array(new xmlrpcval( conf('web_path'), "string")) );
		
	        if (conf('debug')) { $client->setDebug(1); }
		
	        $response = $client->send($f,30);
	        $value = $response->value();

	        if ( !$response->faultCode() ) {
	                $data = php_xmlrpc_decode($value);
			
			// Print out the catalogs we are going to sync
	                foreach ($data as $vars) { 
				$catalog_name 	= $vars[0];
				$count		= $vars[1];
	                        print("<b>Reading Remote Catalog: $catalog_name ($count Songs)</b> [$this->path]<br />\n");
				$total += $count;
	                } 
			// Flush the output
			flush();

	        } // if we didn't get an error
	        else {
			$error_msg = _("Error connecting to") . " " . $server . " " . _("Code") . ": " . $response->faultCode() . " " . _("Reason") . ": " . $response->faultString();
			if (conf('debug')) { log_event($_SESSION['userdata']['username'],'xmlrpc',$error_msg); }
			echo "<p class=\"error\">$error_msg</p>";
	                return;
	        }

		// Hardcoded for now
		$step = '500';
		$current = '0';

		while ($total >= $current) { 
			$start 	= $current;
			$current += $step;
			$this->get_remote_song($client,$start,$step);
		}

	        echo "<p>" . _("Completed updating remote catalog(s)") . ".</p><hr>\n";
		flush();

		return true;

	} // get_remote_catalog

	/** 
	 * get_remote_song
	 * This functions takes a start and end point for gathering songs from a remote server. It is broken up
	 * in attempt to get around the problem of very large target catalogs
	 * @package XMLRPC
	 * @catagory Client
	 * @todo Allow specificion of single catalog
	 */
	function get_remote_song($client,$start,$end) { 

		$query_array = array(new xmlrpcval($start, "int"),new xmlrpcval($end,"int")); 

                $f = new xmlrpcmsg('remote_song_query',$query_array);
                /* Depending upon the size of the target catalog this can be a very slow/long process */
                set_time_limit(0);
                        
		// Sixty Second time out per chunk
                $response = $client->send($f,60);
                $value = $response->value();

                if ( !$response->faultCode() ) {
                        $data = php_xmlrpc_decode($value);
                        $this->update_remote_catalog($data,$this->path);
			$total = $start + $end;
			echo "Added $total...<br />";
			flush();
                }
                else {
                        $error_msg = _("Error connecting to") . " " . $server . " " . _("Code") . ": " . $response->faultCode() . " " . _("Reason") . ": " . $response->faultString();
                        log_event($_SESSION['userdata']['username'],'xmlrpc',$error_msg);
                        echo "<p class=\"error\">$error_msg</p>";
                }

		return;

	} // get_remote_song


	/**
	 * update_remote_catalog
	 * actually updates from the remote data, takes an array of songs that are base64 encoded and parses them
	 * @package XMLRPC
	 * @catagory Client
	 * @todo This should be based off of seralize
	 */
	function update_remote_catalog($songs,$root_path) {
	        global $settings, $dbh, $artists;


		/* 
		   We need to check the incomming songs
		   to see which ones need to be added
		*/
		foreach ($songs as $song) {
	
			// Prevent a timeout
                        set_time_limit(0);
			
	                $song = base64_decode($song);

	                $data = explode("::", $song);

			$new_song->artist 	= $this->check_artist($data[0]);
			$new_song->album	= $this->check_album($data[1],$data[4]);
			$new_song->title	= $data[2];
			$new_song->comment	= $data[3];
			$new_song->year		= $data[4];
			$new_song->bitrate	= $data[5];
			$new_song->rate		= $data[6];
			$new_song->mode		= $data[7];
			$new_song->size		= $data[8];
			$new_song->time		= $data[9];
			$new_song->track	= $data[10];
			$new_song->genre	= $this->check_genre($data[11]);
			$new_song->file		= $root_path . "/play/index.php?song=" . $data[12];
			$new_song->catalog	= $this->id;
	     
			if (!$song_id = $this->check_remote_song($new_song->file)) { 
				$this->insert_remote_song($new_song);
			} 
	     
		} // foreach new Songs

		//FIXME: Delete Songs that were not updated (gone)

	        // now delete invalid entries
		$this->clean_albums();
		$this->clean_stats();
		$this->clean_artists();
		$this->clean_flagged();

	} // update_remote_catalog


	/*!
		@function clean_catalog
		@discussion  Cleans the Catalog of files that no longer exist grabs from $this->id or $id passed 
  	  		     Doesn't actually delete anything, disables errored files, and returns them in an array
		@param $catalog_id=0	Take the ID of the catalog you want to clean
		@param $action=0	Delete/Disable, default is disable
	*/
	function clean_catalog($catalog_id=0,$action=0) {

		/* Define the Arrays we will need */
		$dead_files = array();

		if (!$catalog_id) { $catalog_id = $this->id; }

		echo "Cleaning the <b>[" . $this->name . "]</b> Catalog...<br /><br />";
		flush();

		/* Get all songs in this catalog */
		$sql = "SELECT id,file FROM song WHERE catalog='$catalog_id' AND status='enabled'";
		$db_results = mysql_query($sql, dbh());

		/* Recurse through files, put @ to prevent errors poping up */
		while ($results = mysql_fetch_object($db_results)) {

			/* Remove slashes while we are checking for its existance */
			$results->file = stripslashes($results->file);

                        /* Stupid little cutesie thing */
                        $this->count++;
                        if ( !($this->count%conf('catalog_echo_count')) ) {
                                echo _("Checking") . " $this->count. . . . <br />\n";
	                        flush();
                        } //echos song count

			/* Also check the file information */
			$file_info = @filesize($results->file);

			/* If it errors somethings splated, or the files empty */
			if (!file_exists($results->file) OR $file_info < 1) {
			
				/* Add Error */
				echo "<font class=\"error\">Error File Not Found or 0 Bytes: " . $results->file . "</font><br />";
				flush();

				/* Add this file to the list for removal from the db */
				$dead_files[] = $results;
			} //if error

		} //while gettings songs

		/* Incase there's been a snafo with a mount point on something
		 * don't actually delete from DB here, simply disable and list
		 */
		if (count($dead_files)) {
			foreach ($dead_files as $data) {

				//FIXME: Until I fix the form, assume delete
				//if ($action === 'delete_dead') { 
					$sql = "DELETE FROM song WHERE id='$data->id'";
				//} 
				//
				//else {
				//	$sql = "UPDATE song SET status='disabled' WHERE id='$data->id'";
				//}

				$db_results = mysql_query($sql, dbh());

				/* DB Error occured */
				if (!$db_results) {
					/* Add Error */
				} //if error

			} //end foreach

		} // end if dead files

		/* Step two find orphaned Arists/Albums
		 * This finds artists and albums that no
		 * longer have any songs associated with them
		 */
		$this->clean_albums();
		$this->clean_artists();
		$this->clean_stats();
		$this->clean_playlists();
		$this->clean_flagged();

		/* Return dead files, so they can be listed */
		echo "<b>" . _("Catalog Clean Done") . " [" . count($dead_files) . "] " . _("files removed") . "</b><br />\n";
		flush();
		return $dead_files;

	} //clean_catalog


	/*!
		@function clean_albums
		@discussion  This function cleans out unused albums
		@param $this->id Depends on the current object
	*/
	function clean_albums() {

		/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
		if (preg_match("/^3\./",mysql_get_server_info())) { 
			$sql = "SELECT album.id FROM album LEFT JOIN song ON song.album = album.id WHERE song.id IS NULL";
			$db_results = mysql_query($sql, dbh());

			$results = array();

			while ($r = mysql_fetch_row($db_results)) { 
				$results[] = $r;
			}

			foreach ($results as $dead) { 

				$sql = "DELETE FROM album WHERE id='$dead[0]'";
				$db_results = mysql_query($sql,dbh());
			} 
			return true;
		}

		/* Do a complex delete to get albums where there are no songs */
		$sql = "DELETE FROM album USING album LEFT JOIN song ON song.album = album.id WHERE song.id IS NULL";
		$db_results = mysql_query($sql, dbh());

	} //clean_albums

	/*!
		@function clean_flagged
		@discussion This functions cleans ou unused flagged items
	*/
	function clean_flagged() { 

		/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
		if (preg_match("/^3\./",mysql_get_server_info())) { 
			$sql = "SELECT flagged.id FROM flagged LEFT JOIN song ON song.id=flagged.song WHERE song.id IS NULL";
			$db_results = mysql_query($sql, dbh());

			$results = array();

			while ($r = mysql_fetch_row($db_results)) { 
				$results[] = $r;
			} 

			foreach ($results as $dead) { 
				$sql = "DELETE FROM flagged WHERE id='$dead[0]'";
				$db_results = mysql_query($sql, dbh());
			}
			return true;
		}
		
		/* Do a complex delete to get flagged items where the songs are now gone */
		$sql = "DELETE FROM flagged USING flagged LEFT JOIN song ON song.id = flagged.song WHERE song.id IS NULL";
		$db_results = mysql_query($sql, dbh());

	} // clean_flagged


	/*!
		@function clean_artists
		@discussion This function cleans out unused artists
		@param $this->id Depends on the current object
	*/
	function clean_artists() {

                /* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
                if (preg_match("/^3\./",mysql_get_server_info())) {
                        $sql = "SELECT artist.id FROM artist LEFT JOIN song ON song.artist = artist.id WHERE song.id IS NULL";
                        $db_results = mysql_query($sql, dbh());

			$results = array();

                        while ($r = mysql_fetch_row($db_results)) {
                                $results[] = $r;
                        }

                        foreach ($results as $dead) {

                                $sql = "DELETE FROM artist WHERE id='$dead[0]'";
                                $db_results = mysql_query($sql,dbh());
                        }                                           
                        return true;                                                
                }                                                                   


		/* Do a complex delete to get artists where there are no songs */
		$sql = "DELETE FROM artist USING artist LEFT JOIN song ON song.artist = artist.id WHERE song.id IS NULL";
		$db_results = mysql_query($sql, dbh());

	} //clean_artists

	/*
		@function clean_playlists
		@discussion cleans out dead files from playlists
		@param $this->id depends on the current object
	*/
	function clean_playlists() { 

		/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
		if (preg_match("/^3\./",mysql_get_server_infO())) { 
			$sql = "SELECT playlist_data.song FROM playlist_data LEFT JOIN song ON song.id = playlist_data.song WHERE song.file IS NULL";
			$db_results = mysql_query($sql, dbh());

			$results = array();

			while ($r = mysql_fetch_row($db_results)) { 
				$results[] = $r;
			}

			foreach ($results as $dead) { 
				$sql = "DELETE FROM playlist_data WHERE song='$dead[0]'";
				$db_results = mysql_query($sql, dbh());
			}
			return true;
		}

		/* Do a complex delete to get playlist songs where there are no songs */
		$sql = "DELETE FROM playlist_data USING playlist_data LEFT JOIN song ON song.id = playlist_data.song WHERE song.file IS NULL";
		$db_results = mysql_query($sql, dbh());

	} // clean_playlists

	/*!
		@function clean_stats
		@discussion This functions removes stats for songs/albums that no longer exist
		@param $catalog_id The ID of the catalog to clean
	*/
	function clean_stats() {

		$version = mysql_get_server_info();

                /* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
                if (preg_match("/^3\./",$version)) {
                        $sql = "SELECT object_count.id FROM object_count LEFT JOIN song ON song.id = object_count.object_id WHERE object_type='song' AND song.id IS NULL";
                        $db_results = mysql_query($sql, dbh());

			$results = array();

                        while ($r = mysql_fetch_row($db_results)) {
                                $results[] = $r;
                        }

                        foreach ($results as $dead) {

                                $sql = "DELETE FROM object_count WHERE id='$dead[0]'";
                                $db_results = mysql_query($sql,dbh());
                        }                                           
                        
                }                                                                   
		// We assume this will be 4.0+
		else {
			/* Crazy SQL Mojo to remove stats where there are no songs */
			$sql = "DELETE FROM object_count USING object_count LEFT JOIN song ON song.id=object_count.object_id WHERE object_type='song' AND song.id IS NULL";
			$db_results = mysql_query($sql, dbh());
		}

                /* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
                if (preg_match("/^3\./",$version)) {
                        $sql = "SELECT object_count.id FROM object_count LEFT JOIN album ON album.id = object_count.object_id WHERE object_type='album' AND album.id IS NULL";
                        $db_results = mysql_query($sql, dbh());

			$results = array();

                        while ($r = mysql_fetch_row($db_results)) {
                                $results[] = $r;
                        }

                        foreach ($results as $dead) {

                                $sql = "DELETE FROM object_count WHERE id='$dead[0]'";
                                $db_results = mysql_query($sql,dbh());
                        }                                           
                }                                                                   
		// We assume 4.0+ Here
		else {
			/* Crazy SQL Mojo to remove stats where there are no albums */
			$sql = "DELETE FROM object_count USING object_count LEFT JOIN album ON album.id=object_count.object_id WHERE object_type='album' AND album.id IS NULL";
			$db_results = mysql_query($sql, dbh());
		}
		
                /* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
                if (preg_match("/^3\./",$version)) {
                        $sql = "SELECT object_count.id FROM object_count LEFT JOIN artist ON artist.id = object_count.object_id WHERE object_type='artist' AND artist.id IS NULL";
                        $db_results = mysql_query($sql, dbh());
	
			$results = array();

                        while ($r = mysql_fetch_row($db_results)) {
                                $results[] = $r;
                        }

                        foreach ($results as $dead) {

                                $sql = "DELETE FROM object_count WHERE id='$dead[0]'";
                                $db_results = mysql_query($sql,dbh());
                        }                                           
                }                                                                   
		// We assume 4.0+ here
		else { 
			/* Crazy SQL Mojo to remove stats where ther are no artists */
			$sql = "DELETE FROM object_count USING object_count LEFT JOIN artist ON artist.id=object_count.object_id WHERE object_type='artist' AND artist.id IS NULL";
			$db_results = mysql_query($sql, dbh());
		}


	} // clean_stats
	

	/*!
		@function verify_catalog
		@discussion This function compares the DB's information with the ID3 tags
		@param $catalog_id The ID of the catalog to compare
	*/
	function verify_catalog($catalog_id=0,$gather_type=0) {

		/* Create and empty song for us to use */
		$total_updated = 0;

		/* Set it to this if they don't pass anything */
		if (!$catalog_id) {
			$catalog_id = $this->id;
		}

		/* First get the filenames for the catalog */
		$sql = "SELECT id FROM song WHERE catalog='$catalog_id' ORDER BY id";
		$db_results = mysql_query($sql, dbh());
		$number = mysql_num_rows($db_results);
		
		echo _("Updating the") . " <b>[ $this->name ]</b> " . _("Catalog") . "<br />\n";
		echo $number . " " . _("songs found checking tag information.") . "<br /><br />\n\n";
		flush();

		/* Magical Fix so we don't run out of time */
		set_time_limit(0);

		/* Recurse through this catalogs files
		 * and get the id3 tage information,
		 * if it's not blank, and different in
		 * in the file then update!
		 */
		while ($results = mysql_fetch_object($db_results)) {

			$song = new Song($results->id);

			if (is_readable($song->file)) {
				unset($skip);

				/* If they have specified fast_update check the file
				   filemtime to make sure the file has actually 
				   changed
				*/
				if ($gather_type === "fast_update") {
					$file_date = filemtime($song->file);
					if ($file_date < $this->last_update) { $skip = true; }
				} // if gather_type
				
				if ($song->update_time >= $this->last_update) { 
					$skip = true;
					$song->update_utime($song->id,time()+86400);
				}

				// if the file hasn't been modified since the last_update
				if (!$skip) {

					$info = $this->update_song_from_tags($song);
					$album_id = $song->album;
					if ($info['change']) {
						echo "<dl>\n\t<li>";
						echo "<b>$song->file " . _("Updated") . "</b>\n";
						echo $info['text'];
					/* If we aren't doing a fast update re-gather album art */
						if ($gather_type !== "fast_update" AND !isset($searched_albums[$album_id])) { 
							$album = new Album($song->album);
							$searched_albums[$album_id] = 1;
							$found = $album->get_art();
							unset($album);
							if ($found) { $is_found = _(" FOUND"); }
							echo "<br /><b>" . _("Searching for new Album Art") . ". . .$is_found</b><br />\n";
							unset($found,$is_found);
						}
						elseif (isset($searched_albums[$album_id])) { 
							echo "<br /><b>" . _("Album Art Already Found") . ". . .</b><br />\n";
						}
						echo "\t</li>\n</dl>\n<hr align=\"left\" width=\"50%\" />\n";
						flush();
						$total_updated++;
					}

					unset($info);

				} // end skip

                                /* Stupid little cutesie thing */
                                $this->count++;
                                if ( !($this->count%conf('catalog_echo_count')) ) {
                                	echo "Checked $this->count. . . . <br />\n";
                                        flush();
                                } //echos song count
				
			} // end if file exists

			else {
				echo "<dl>\n  <li>";
				echo "<b>$song->file does not exist or is not readable</b>\n";
				echo "  </li>\n</dl>\n<hr align=\"left\" width=\"50%\" />\n";
				// Should we remove it from catalog?
			}


		} //end foreach


		/* After we have updated all the songs with the new information clear any empty albums/artists */
		$this->clean_albums();
		$this->clean_artists();
		$this->clean_stats();
		$this->clean_flagged();

		// Update the last_update
		$this->update_last_update();

		echo "Update Finished. Checked $this->count. $total_updated songs updated.<br /><br />";

	} //verify_catalog


	/*!
		@function create_catalog_entry
		@discussion Creates a new catalog from path and type
		@param $path The root path for this catalog
		@param $name The name of the new catalog
	*/
	function create_catalog_entry($path,$name,$id3cmd=0,$ren=0,$sort=0, $type='local') {

		// Current time
		$date = time();

		$path = sql_escape($path);
		$name = sql_escape($name);

		if($id3cmd && $ren && $sort) {
			$sql = "INSERT INTO catalog (path,name,last_update,id3_set_command,rename_pattern,sort_pattern,catalog_type) " .
				" VALUES ('$path','$name','$date', '$id3cmd', '$ren', '$sort','$type')";
		}
		else {
			$sql = "INSERT INTO catalog (path,name,last_update) VALUES ('$path','$name','$date')";
		}

		$db_results = mysql_query($sql, dbh());
		$catalog_id = mysql_insert_id(dbh());

	        return $catalog_id;

	} //create_catalog_entry


	/*!
		@function check_catalog
		@discussion  Checks for the $path already in the catalog table
		@param $path The root path for the catalog we are checking
	*/
	function check_catalog($path) {
		
		$path = sql_escape($path);

		$sql = "SELECT id FROM catalog WHERE path='$path'";
		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_object($db_results);

		return $results->id;

	} //check_catalog


	/*!
		@function check_artist
		@discussion Takes $artist checks if there then return id else insert and return id
		@param $artist The name of the artist
	*/
	function check_artist($artist) {

		// Only get the var ones.. less func calls
		$cache_limit = conf('artist_cache_limit');

		/* Clean up the artist */
		$artist = trim($artist);
		$artist = sql_escape($artist);


		/* Ohh no the artist has lost it's mojo! */
		if (!$artist) {
			$artist = "Unknown (Orphaned)";
		}

		// Remove the prefix so we can sort it correctly
		preg_match("/^(The\s|An\s|A\s)(.*)/i",$artist,$matches);

		if (count($matches)) {
			$artist = $matches[2];
			$prefix = $matches[1];
		}

		// Check to see if we've seen this artist before
		if (isset($this->artists[$artist])) {
			return $this->artists[$artist];
		} // if we've seen this artist before

		/* Setup the checking sql statement */
		$sql = "SELECT id FROM artist WHERE name LIKE '$artist' ";
		$db_results = mysql_query($sql, dbh());

		/* If it's found */
		if ($r = mysql_fetch_object($db_results)) {
			$artist_id = $r->id;
		} //if found

		/* If not found create */
		else {

			$sql = "INSERT INTO artist (name, prefix) VALUES ('$artist', '$prefix')";
			$db_results = mysql_query($sql, dbh());
			$artist_id = mysql_insert_id(dbh());


			if (!$db_results) {
				echo "Error Inserting Artist:$artist <br />";
				flush();
			}

		} //not found

		if ($cache_limit) {

			$artist_count = count($this->artists);
			if ($artist_count == $cache_limit) {
				$this->artists = array_slice($this->artists,1);
			}
			if (conf('debug')) { log_event($_SESSION['userdata']['username'],'cache',"Adding $artist with $artist_id to Cache",'ampache-catalog'); }
			$array = array($artist => $artist_id);
			$this->artists = array_merge($this->artists, $array);
			unset($array);

		} // if cache limit is on..

		return $artist_id;

	} //check_artist


	/*!
		@function check_album
		@disucssion Takes $album and checks if there then return id else insert and return id 
		@param $album The name of the album
	*/
	function check_album($album,$album_year=0) {

		/* Clean up the album name */
		$album = trim($album);
		$album = sql_escape($album);
		$album_year = intval($album_year);

		// Set it once to reduce function calls
		$cache_limit = conf('album_cache_limit');

		/* Ohh no the album has lost it's mojo */
		if (!$album) {
			$album = "Unknown (Orphaned)";
		}

		// Remove the prefix so we can sort it correctly
		preg_match("/^(The\s|An\s|A\s)(.*)/i",$album,$matches);

		if (count($matches)) {
			$album = $matches[2];
			$prefix = $matches[1];
		}

		// Check to see if we've seen this album before
		if (isset($this->albums[$album])) {
			return $this->albums[$album];
		}

		/* Setup the Query */
		$sql = "SELECT id FROM album WHERE name LIKE '$album'";
		if ($album_year) { $sql .= " AND year='$album_year'"; }
		$db_results = mysql_query($sql, dbh());

		/* If it's found */
		if ($r = mysql_fetch_object($db_results)) {
			$album_id = $r->id;

		} //if found

		/* If not found create */
		else {

			$sql = "INSERT INTO album (name, prefix,year) VALUES ('$album', '$prefix','$album_year')";
			$db_results = mysql_query($sql, dbh());
			$album_id = mysql_insert_id(dbh());

			if (!$db_results) {
				echo "Error Inserting Album:$album <br />";
				flush();
			}

		} //not found

                if ($cache_limit > 0) {

                        $albums_count = count($this->albums);

                        if ($albums_count == $cache_limit) {
        	                $this->albums = array_slice($this->albums,1);
                        }
			$array = array($album => $album_id);
			$this->albums = array_merge($this->albums,$array);	               
			unset($array);

                } // if cache limit is on..

		return $album_id;

	} //check_album


	/*!
		@function check_genre
		@discussion Finds the Genre_id from the text name
		@param $genre The name of the genre
	*/
	function check_genre($genre) {

		if (!$genre) {
			return false;
		}

		if ($this->genres[$genre]) {
			return $this->genres[$genre];
		}

		/* Look in the genre table */
		$genre = sql_escape($genre);
		$sql = "SELECT id FROM genre WHERE name LIKE '$genre'";	
		$db_results = mysql_query($sql, dbh());

		$results = mysql_fetch_object($db_results);

		if (!$results->id) { 
			$sql = "INSERT INTO genre (name) VALUES ('$genre')";
			$db_results = mysql_query($sql, dbh());
			$results->id = mysql_insert_id(dbh());
		}

		$this->genres[$genre] = $results->id;

		return $results->id;

	} //check_genre


	/*!
		@function check_title
		@discussion this checks to make sure something is
			set on the title, if it isn't it looks at the
			filename and trys to set the title based on that
	*/
	function check_title($title,$file=0) {

		if (strlen(trim($title)) < 1) {
			preg_match("/.+\/(.*)\.....?$/",$file,$matches);
			$title = sql_escape($matches[1]);
		}

		return $title;


	} //check_title


	/*!
		@function insert_local_song
		@discussion Insert a song that isn't already in the database this
			    function is in here so we don't have to create a song object
		@param $file The file name we are adding (full path)
		@param $file_info The information of the file, size etc taken from stat()
	*/
	function insert_local_song($file,$file_info) {

		/* Create the Audioinfo object and get info */
		$audio_info 	 = new Audioinfo();
		$song_obj	 = new Song();
		$results 	 = $audio_info->Info($file);
		$results['file'] = $file;

		$key = get_tag_type($results);

		/* Fill Empty info from filename/path */
		$results = $song_obj->fill_info($results,$this->sort_pattern . "/" . $this->rename_pattern,$this->id,$key);

		/* Clean Up the tags */
		$results = clean_tag_info($results,$key,$file);
	
		/* Set the vars here... so we don't have to do the '" . $blah['asd'] . "' */
		$title 		= sql_escape($results['title']);
		$artist 	= $results['artist'];
		$album 		= $results['album'];
		$genre 		= $results['genre'];
		$bitrate 	= $results['bitrate'];
		$rate	 	= $results['rate'];
		$mode 		= $results['mode'];
		$size	 	= $results['size'];
		$song_time 	= $results['time'];
		$track	 	= $results['track'];
		$year		= $results['year'];
		$comment	= $results['comment'];
		$current_time 	= time();

		/*
		 * We have the artist/genre/album name need to check it in the tables
		 * If found then add & return id, else return id
		 */
		$artist_id	= $this->check_artist($artist);
		$genre_id	= $this->check_genre($genre);
		$album_id	= $this->check_album($album,$year);
		$title		= $this->check_title($title,$file);
		$add_file	= sql_escape($results['file']);

		$sql = "INSERT INTO song (file,catalog,album,artist,title,bitrate,rate,mode,size,time,track,genre,addition_time,year,comment)" .
			" VALUES ('$add_file','$this->id','$album_id','$artist_id','$title','$bitrate','$rate','$mode','$size','$song_time','$track','$genre_id','$current_time','$year','$comment')";

		$db_results = mysql_query($sql, dbh());

		if (!$db_results) {
			if (conf('debug')) { log_event($_SESSION['userdata']['username'],'insert',"Unable to insert $file -- $sql",'ampache-catalog'); }
			echo "<span style=\"color: #F00;\">Error Adding $file </span><br />$sql<br />";
			flush();
		}

		/* Clear Variables */
		unset($results,$audio_info,$song_obj);

	} // insert_local_song

	/*!
		@function insert_remote_song
		@discussion takes the information gotten from XML-RPC and 
			inserts it into the local database. The filename
			ends up being the url.
	*/
	function insert_remote_song($song) {

		$url 		= sql_escape($song->file);
		$title		= $this->check_title($song->title);
		$title		= sql_escape($title);
		$comment	= sql_escape($song->comment);
		$current_time	= time();	
		
		$sql = "INSERT INTO song (file,catalog,album,artist,title,bitrate,rate,mode,size,time,track,genre,addition_time,year,comment)" .
			" VALUES ('$url','$song->catalog','$song->album','$song->artist','$title','$song->bitrate','$song->rate','$song->mode','$song->size','$song->time','$song->track','$song->genre','$current_time','$song->year','$comment')";
		$db_results = mysql_query($sql, dbh());

		if (!$db_results) { 
                        if (conf('debug')) { log_event($_SESSION['userdata']['username'],'insert',"Unable to Add Remote $url -- $sql",'ampache-catalog'); }
			echo "<span style=\"color: #FOO;\">Error Adding Remote $url </span><br />$sql<br />\n";
			flush();
		}

	} // insert_remote_song


	/*!
		@function check_remote_song
		@discussion checks to see if a remote song exists in the database or not
			if it find a song it returns the UID
	*/
	function check_remote_song($url) { 

		$url = sql_escape($url);

		$sql = "SELECT id FROM song WHERE file='$url'";
		$db_results = mysql_query($sql, dbh());

		if ($r = mysql_fetch_object($db_results)) { 
			return $r->id;
		}
		
		return false;

	} // check_remote_song


	/*!
		@function check_local_mp3
		@discussion Checks the song to see if it's there already returns true if found, false if not 
		@param $full_file The full file name that we are checking
		@param $gather_type=0 If we need to check id3 tags or not
	*/
	function check_local_mp3($full_file, $gather_type=0) {

		if ($gather_type == 'fast_add') {
			$file_date = filemtime($full_file);
			if ($file_date < $this->last_add) {
				return true;
			}
		}

		$full_file = sql_escape($full_file);

		$sql = "SELECT id FROM song WHERE file = '$full_file'";
		$db_results = mysql_query($sql, dbh());

		//If it's found then return true
		if (@mysql_fetch_row($db_results)) {
			return true;
		}

	return false;

	} //check_local_mp3

	/*!
		@function import_m3u
		@discussion this takes m3u filename and then attempts
			to create a Public Playlist based on the filenames
			listed in the m3u
	*/
	function import_m3u($filename) { 

		$m3u_handle = @fopen($filename,'r');
		
		$data = @fread($m3u_handle,filesize($filename));
		
		$results = explode("\n",$data);

		foreach ($results as $value) {
			// Remove extra whitespace
			$value = trim($value);
			if (preg_match("/\.[A-Za-z0-9]{3}$/",$value)) { 
				$file[0] = str_replace("/","\\",$value);
				$file[1] = str_replace("\\","/",$value);
				/* Search for this filename, cause it's a audio file */
				$sql = "SELECT id FROM song WHERE file LIKE '%" . sql_escape($file[0]) . "' OR file LIKE '%" . sql_escape($file[1]) . "'";
				$db_results = mysql_query($sql, dbh());
				$song_id = mysql_result($db_results,'id');
				if ($song_id) { $songs[] = $song_id; }
			} // if it's a file

		} // end foreach line

		if (conf('debug')) { log_event($GLOBALS['user']->username,' m3u_parse ',"Parsing $filename - Found: " . count($songs) . " Songs"); }

		if (count($songs)) { 
			$playlist = new Playlist();
			$playlist_name = "M3U - " . basename($filename);
			$playlist->create_playlist($playlist_name,$GLOBALS['user']->id,'public');
			$playlist->add_songs($songs);
			return true;
		}

		return false;

	} // import_m3u

	/*!
		@function delete_catalog
		@discussion Deletes the catalog and everything assoicated with it
			    assumes $this
	*/
	function delete_catalog() {

		// Do some crazyness to delete all the songs in this catalog
		// from playlists...
		$sql = "SELECT playlist_data.song FROM song,playlist_data,catalog WHERE catalog.id=song.catalog AND playlist_data.song=song.id AND catalog.id='$this->id'";
		$db_results = mysql_query($sql, dbh());

		$results = array();

		while ($r = mysql_fetch_object($db_results)) { 
			$results[] = $r;
		}

		foreach ($results as $r) { 
			// Clear Playlist Data
			$sql = "DELETE FROM playlist_data WHERE song='$r->song'";
			$db_results = mysql_query($sql, dbh());
			
		} // End Foreach

		// First remove the songs in this catalog
		$sql = "DELETE FROM song WHERE catalog = '$this->id'";
		$db_results = mysql_query($sql, dbh());

		// Next Remove the Catalog Entry it's self
		$sql = "DELETE FROM catalog WHERE id = '$this->id'";
		$db_results = mysql_query($sql, dbh());

		// Run the Aritst/Album Cleaners...
		$this->clean_albums();
		$this->clean_artists();
		$this->clean_stats();
		$this->clean_playlists();
		$this->clean_flagged();

	} // delete_catalog


	/*!
		@function remove_songs
		@discussion removes all songs sent in $songs array from the
			database, it doesn't actually delete them...
	*/
	function remove_songs($songs) {

		foreach($songs as $song) {
			$sql = "DELETE FROM song WHERE id = '$song'";
			$db_results = mysql_query($sql, dbh());
		}

	} // remove_songs

} //end of catalog class

?>