summaryrefslogtreecommitdiffstats
path: root/lib/album.lib.php
blob: 4317c799d03fd93fc88d2643b6edfed066ee15a0 (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
<?php
/*
Copyright (c) 2001 - 2007 Ampache.org
All Rights Reserved

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License v2
as published by the Free Software Foundation.

This program is distributed int he hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANT ABILITY 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.

 This library handles album related functions.... wooo!
*/

/*!
	@function get_albums
	@discussion pass a sql statement, and it gets full album info and returns
		an array of the goods.. can be set to format them as well
*/
function get_albums($sql, $action=0) {

	$db_results = mysql_query($sql, dbh());
	while ($r = mysql_fetch_array($db_results)) {
		$album = new Album($r[0]);
		$album->format_album();
		$albums[] = $album;
	}

	return $albums;


} // get_albums

/**
 * get_image_from_source
 * This gets an image for the album art from a source as 
 * defined in the passed array. Because we don't know where
 * its comming from we are a passed an array that can look like
 * ['url']	= URL *** OPTIONAL ***
 * ['file']	= FILENAME *** OPTIONAL ***
 * ['raw']	= Actual Image data, already captured
 */
function get_image_from_source($data) { 

	// Already have the data, this often comes from id3tags
	if (isset($data['raw'])) { 
		return $data['raw'];
	}

	// If it came from the database
	if (isset($data['db'])) { 
		// Repull it 
		$album_id = Dba::escape($data['db']); 
		$sql = "SELECT * FROM `album_data` WHERE `album_id`='$album_id'"; 
		$db_results = Dba::query($sql); 
		$row = Dba::fetch_assoc($db_results); 
		return $row['art']; 
	} // came from the db

	// Check to see if it's a URL
	if (isset($data['url'])) { 
		$snoopy = new Snoopy(); 
		$snoopy->fetch($data['url']); 
		return $snoopy->results; 
	} 

	// Check to see if it's a FILE
	if (isset($data['file'])) { 
		$handle = fopen($data['file'],'rb'); 
		$image_data = fread($handle,filesize($data['file'])); 		
		fclose($handle); 
		return $image_data; 
	} 
	
	// Check to see if it is embedded in id3 of a song
	if (isset($data['song'])) { 
        	// If we find a good one, stop looking
		$getID3 = new getID3();
		$id3 = $getID3->analyze($data['song']);

		if ($id3['format_name'] == "WMA") { 
			return $id3['asf']['extended_content_description_object']['content_descriptors']['13']['data'];
		}
		elseif (isset($id3['id3v2']['APIC'])) { 
			// Foreach incase they have more then one 
			foreach ($id3['id3v2']['APIC'] as $image) { 
				return $image['data'];
			} 
		}
	} // if data song

	return false; 

} // get_image_from_source

/**
 * get_random_albums
 * This returns a random number of albums from the catalogs
 * this is used by the index to return some 'potential' albums to play
 */
function get_random_albums($count=6) {

	// Make sure that we have anything to pick from
	$sql = "SELECT `id` FROM `album` LIMIT 7"; 
	$db_results = Dba::query($sql); 

	$rows = Dba::num_rows($db_results); 
	if ($rows < 7) { return false; } 

        // There's a slight chance with this logic that the number of albums
        // returned will be less than the number requested if the id's for the
        // albums have signifigant gaps, but the speed increase is probably
        // worth it
        // - Vlet

        $sql = 'SELECT ';

        for ($i = 0; $i < ceil($count * 1.5); $i++) {
                if ($i > 0) $sql .= ', ';

                $sql .= 'floor(rand() * count(id))';
        }
        $sql .= ' FROM `album`';
        $db_results = Dba::query($sql);

        $sql = '';

        $row = Dba::fetch_row($db_results);
        
	for ($i = 0; $i < ceil($count * 1.5); $i++) {
                if ($i > 0) $sql .= ' UNION ';
                $sql .= "SELECT `id` FROM (SELECT `id` FROM `album` LIMIT $row[$i],1) t".$i ;
        }
        
	$db_results = Dba::query($sql);

        $results = array();

        for ($i = 0; $i < $count; $i++) {
                $row = Dba::fetch_assoc($db_results);
                $results[] = $row['id'];
        }

        return $results;
} // get_random_albums

?>