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
|
<?php
/*
Copyright (c) 2004
Ampache.org
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 Album Class
*/
class Album {
/* Variables from DB */
var $id;
var $name;
var $year;
var $prefix;
/*!
@function Album
@discussion Album class, for modifing a song.
@param $album_id The ID of the song
*/
function Album($album_id = 0) {
/* If we have passed an id then do something */
if ($album_id) {
/* Assign id for use in get_info() */
$this->id = $album_id;
/* Get the information from the db */
if ($info = $this->get_info()) {
/* Assign Vars */
$this->name = trim($info->prefix . " " . $info->album_name);
$this->songs = $info->song_count;
$this->artist_count = $info->artist_count;
$this->year = $info->year;
$this->artist = trim($info->artist_prefix . " " . $info->artist_name);
$this->artist_id = $info->art_id;
$this->album = $info->album_name;
$this->prefix = $info->prefix;
} // if info
} // if album_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 COUNT(DISTINCT(song.artist)) as artist_count,album.prefix,album.year,album.name AS album_name,COUNT(song.id) AS song_count," .
"artist.name AS artist_name,artist.id AS art_id,artist.prefix AS artist_prefix ".
"FROM song,artist,album WHERE album.id='$this->id' AND song.album=album.id AND song.artist=artist.id GROUP BY song.album";
$db_results = mysql_query($sql, dbh());
$results = mysql_fetch_object($db_results);
return $results;
} //get_info
/*!
@function get_songs
@discussion gets the songs for this album
*/
function get_songs($limit = 0) {
$results = array();
$sql = "SELECT id FROM song WHERE album='$this->id' ORDER BY track, title";
if ($limit) { $sql .= " LIMIT $limit"; }
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_object($db_results)) {
$results[] = new Song($r->id);
}
return $results;
} // get_songs
/*!
@function format_album
@dicussion reformats this object with f_name, f_songs and f_artist
that contain links etc...
*/
function format_album() {
$web_path = conf('web_path');
/* Truncate the string if it's to long */
$name = htmlspecialchars(truncate_with_ellipse($this->name,conf('ellipse_threshold_album')));
$artist = htmlspecialchars($this->artist);
$this->f_name = "<a href=\"$web_path/albums.php?action=show&album=" . $this->id . "\" title=\"" . $name . "\">" . $name . "</a>";
$this->f_songs = "<div align=\"center\">" . $this->songs . "</div>";
if ($this->artist_count == '1') {
$this->f_artist = "<a href=\"$web_path/artists.php?action=show&artist=" . $this->artist_id . "\">" . $artist . "</a>";
}
else {
$this->f_artist = _("Various");
}
if ($this->year == '0') {
$this->year = "N/A";
}
} // format_album
/*!
@function get_art
@discussion get art wrapper function
*/
function get_art($fast = 0) {
/* Check DB first */
if ($image = $this->get_db_art()) {
return $image;
}
/* Stop here if we are doing the fast art */
if ($fast) { return false; }
/* Create Base Vars */
$album_art_order = array();
/* Attempt to retrive the album art order */
$config_value = conf('album_art_order');
$class_methods = get_class_methods('Album');
/* If it's not set */
if (empty($config_value)) {
$album_art_order = array('id3','folder','amazon');
}
elseif (!is_array($config_value)) {
$album_art_order = array_push($album_art_order,$config_value);
}
else {
$album_art_order = array_merge($album_art_order, conf('album_art_order'));
}
foreach ($album_art_order AS $method) {
$method_name = "get_" . $method . "_art";
if (in_array($method_name,$class_methods)) {
if ($this->{$method_name}()) {
return $this->get_db_art();
} // if method finds the art
} // if the method exists
} // end foreach
return false;
} // get_art
/*!
@function get_id3_art
@discussion looks for art from the id3 tags
*/
function get_id3_art() {
$songs = $this->get_songs();
// Foreach songs in this album
foreach ($songs as $song) {
// If we find a good one, stop looking
$getID3 = new getID3();
$id3 = $getID3->analyze($song->file);
if ($id3['format_name'] == "WMA") {
$image = $id3['asf']['extended_content_description_object']['content_descriptors']['13'];
}
else {
$image = $id3['id3v2']['APIC']['0'];
}
if ($image) {
$art = $image['data'];
$mime = $image['mime'];
// Stick it in the db for next time
$sql = "UPDATE album SET art = '" . sql_escape($art) . "'," .
" art_mime = '" . sql_escape($mime) . "'" .
" WHERE id = '" . $this->id . "'";
$db_result = mysql_query($sql, dbh());
return true;
} // end if image
} // end foreach
return false;
} // get_id3_art
/*!
@function get_folder_art()
@discussion returns the album art from the folder of the mp3s
*/
function get_folder_art() {
$songs = $this->get_songs();
/* See if we are looking for a specific filename */
$preferred_filename = conf('album_art_preferred_filename');
/* Thanks to dromio for origional code */
/* Added search for any .jpg, png or .gif - Vollmer */
foreach($songs as $song) {
$dir = dirname($song->file);
/* Open up the directory */
$handle = @opendir($dir);
if (!is_resource($handle)) {
echo "<font class=\"error\">" . _("Error: Unable to open") . " $dir</font><br />\n";
if (conf('debug')) { log_event($GLOBALS['user']->username,' read ',"Error: Unable to open $dir for album art read"); }
}
/* Recurse through this dir and create the files array */
while ( FALSE !== ($file = @readdir($handle)) ) {
$extension = substr($file,strlen($file)-3,4);
/* If it's an image file */
if ($extension == "jpg" || $extension == "gif" || $extension == "png" || $extension == "jp2") {
if ($file == $preferred_filename) {
$found = 1;
$album_art_filename = array('file' => $file, 'ext' => $extension);
break;
}
elseif (!$preferred_filename) {
$found = 1;
$album_art_filename = array('file' => $file, 'ext' => $extension);
break;
}
else {
$found = 1;
$album_art_filename = array('file' => $file, 'ext' => $extension);
}
} // end if it's an image
} // end while reading dir
@closedir($handle);
if ($found) {
$handle = fopen($dir."/".$album_art_filename['file'], "rb");
$mime = "image/" . $album_art_filename['ext'];
$art = '';
while(!feof($handle)) {
$art .= fread($handle, 1024);
}
fclose($handle);
$sql = "UPDATE album SET art = '" . sql_escape($art) . "'," .
" art_mime = '" . sql_escape($mime) . "'" .
" WHERE id = '$this->id'";
$db_results = mysql_query($sql, dbh());
return true;
} // if found
} // end foreach songs
return false;
} // get_folder_art()
/*!
@function get_db_art()
@discussion returns the album art from the db
*/
function get_db_art() {
$sql = "SELECT art,art_mime FROM album WHERE id='$this->id' AND art_mime IS NOT NULL";
$db_results = mysql_query($sql, dbh());
$results = mysql_fetch_object($db_results);
return $results;
} // get_db_art
/*!
@function get_amazon_art
@discussion searches amazon for the
album art
*/
function get_amazon_art() {
return $this->find_art();
} // get_amazon_art
/*!
@function get_random_songs
@discussion gets a random number, and
a random assortment of songs from this
album
*/
function get_random_songs() {
$results = array();
$sql = "SELECT id FROM song WHERE album='$this->id' ORDER BY RAND() LIMIT " . rand(1,$this->songs);
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_array($db_results)) {
$results[] = $r[0];
}
return $results;
} // get_random_songs
/*!
@function clear_art
@discussion clears the album art from the DB
*/
function clear_art() {
$sql = "UPDATE album SET art=NULL, art_mime=NULL WHERE id='$this->id'";
$db_results = mysql_query($sql, dbh());
} // clear_art
/*!
@function find_art
@discussion searches amazon or a url
for the album art
//FIXME: Rename this POS
*/
function find_art($coverurl = '') {
// No coverurl specified search amazon
if (empty($coverurl)) {
$amazon = new AmazonSearch(conf('amazon_developer_key'));
// Prevent the script from timing out
set_time_limit(0);
$search_term = $this->artist . " " . $this->name;
$amazon->search(array('artist' => $this->artist, 'album' => $this->name, 'keywords' => $serch_term));
// Only do the second search if the first actually returns something
if (count($amazon->results)) {
$amazon->lookup($amazon->results);
}
/* Log this if we're doin debug */
if (conf('debug')) {
log_event($_SESSION['userdata']['username'],' amazon-xml ',"Searched using $search_term with " . conf('amazon_developer_key') . " as key " . count($amazon->results) . " results found");
}
//FIXME: For now just pull the first one we find
foreach ($amazon->results as $key=>$value) {
$results = $value;
break;
} //FIXME:
} // if no cover
// If we've specified a coverurl, create a fake Amazon array with it
else {
$results = array('LargeImage' => $coverurl);
}
// If we have results of some kind
if (is_array($results)) {
/* Recurse through the images found */
$possible_keys = array("LargeImage","MediumImage","SmallImage");
foreach ($possible_keys as $key) {
if (strlen($results[$key])) {
break;
}
} // foreach
// Rudimentary image type detection, only JPG and GIF allowed.
if (substr($results[$key], -4 == ".jpg")) {
$mime = "image/jpg";
}
elseif (substr($results[$key], -4 == ".gif")) {
$mime = "image/gif";
}
else {
return false;
}
/* Create Snoopy Object and pull info */
$snoopy = new Snoopy;
$snoopy->fetch($results[$key]);
$art = $snoopy->results;
// Skip 1x1 size images
if (function_exists('ImageCreateFromString')) {
$im = @ImageCreateFromString($art);
if (@imagesx($im) == 1 || @imagesy($im) == 1 && $im) {
return false;
}
}
// Push the image into the database
$sql = "UPDATE album SET art = '" . sql_escape($art) . "'," .
" art_mime = '" . sql_escape($mime) . "'" .
" WHERE id = '$this->id'";
$db_results = mysql_query($sql, dbh());
return true;
} // if we've got something
/* Default to false */
return false;
} // find_art
/*!
@function get_song_ids
@discussion returns a list of song_ids on the album
get_songs returns a list of song objects
*/
// it seems get_songs really should call this,
// but I don't feel comfortable imposing that - RCR
function get_song_ids( $limit = 0 ) {
$results = array();
$sql = "SELECT id FROM song WHERE album='$this->id' ORDER BY track, title";
if ($limit) { $sql .= " LIMIT $limit"; }
$db_results = mysql_query($sql, dbh());
while ($r = mysql_fetch_object($db_results)) {
$results[] = $r->id;
}
return( $results );
} // get_song_ids
} //end of album class
?>
|