summaryrefslogtreecommitdiffstats
path: root/lib/class
diff options
context:
space:
mode:
Diffstat (limited to 'lib/class')
-rw-r--r--lib/class/access.class.php14
-rw-r--r--lib/class/album.class.php75
-rw-r--r--lib/class/artist.class.php42
-rw-r--r--lib/class/catalog.class.php227
-rw-r--r--lib/class/dba.class.php50
-rw-r--r--lib/class/error.class.php142
-rw-r--r--lib/class/flag.class.php16
-rw-r--r--lib/class/song.class.php90
-rw-r--r--lib/class/stats.class.php49
-rw-r--r--lib/class/stream.class.php27
-rw-r--r--lib/class/update.class.php42
-rw-r--r--lib/class/user.class.php90
-rwxr-xr-xlib/class/vainfo.class.php516
13 files changed, 996 insertions, 384 deletions
diff --git a/lib/class/access.class.php b/lib/class/access.class.php
index ba4548dd..fc01adfb 100644
--- a/lib/class/access.class.php
+++ b/lib/class/access.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
@@ -145,11 +145,13 @@ class Access {
} // delete
- /*!
- @function check
- @discussion check to see if they have rights
- */
- function check($type,$ip,$user,$level,$key='') {
+ /**
+ * check
+ * This takes a type, ip, user, level and key
+ * and then returns true or false if they have access to this
+ * the IP is passed as a dotted quad
+ */
+ public static function check($type,$ip,$user,$level,$key='') {
// They aren't using access control
// lets just keep on trucking
diff --git a/lib/class/album.class.php b/lib/class/album.class.php
index afa96df3..29dc9ee2 100644
--- a/lib/class/album.class.php
+++ b/lib/class/album.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
@@ -73,7 +73,7 @@ class Album {
@discussion get's the vars for $this out of the database
@param $this->id Taken from the object
*/
- function _get_info() {
+ private function _get_info() {
$this->id = intval($this->id);
@@ -81,10 +81,9 @@ class Album {
$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,album.art AS has_art ".
"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 = Dba::query($sql);
- $db_results = mysql_query($sql, dbh());
-
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
// If there is art then set it to 1, if not set it to 0, we don't want to cary
// around the full blob with every object because it can be pretty big
@@ -147,10 +146,10 @@ class Album {
*/
function format() {
- $web_path = conf('web_path');
+ $web_path = Config::get('web_path');
/* Truncate the string if it's to long */
- $name = scrub_out(truncate_with_ellipse($this->name,conf('ellipse_threshold_album')));
+ $name = scrub_out(truncate_with_ellipse($this->name,Config::get('ellipse_threshold_album')));
$artist = scrub_out($this->artist);
$this->f_name = "<a href=\"$web_path/albums.php?action=show&amp;album=" . $this->id . "\" title=\"" . scrub_out($this->name) . "\">" . $name . "</a>";
$this->f_link = "<a href=\"$web_path/albums.php?action=show&amp;album=" . scrub_out($this->id) . "\" title=\"" . scrub_out($this->name) . "\">" . $name . "</a>";
@@ -182,12 +181,20 @@ class Album {
/**
* get_art
- * This function only pulls art from the database, nothing else
- * It should not be called when trying to find new art
+ * This function only pulls art from the database, if thumb is passed
+ * it trys to pull the resized art instead, if resized art is found then
+ * it returns an additional resized=true in the array
*/
function get_art() {
- return $this->get_db_art();
+ // Attempt to get the resized art first
+ $art = $this->get_resized_db_art();
+
+ if (!is_array($art)) {
+ $art = $this->get_db_art();
+ }
+
+ return $art;
} // get_art
@@ -371,15 +378,37 @@ class Album {
} // get_folder_art()
/**
- * get_db_art()
+ * get_resized_db_art
+ * This looks to see if we have a resized thumbnail that we can load rather then taking
+ * the fullsized and resizing that
+ */
+ public function get_resized_db_art() {
+
+ $id = Dba::escape($this->id);
+
+ $sql = "SELECT `thumb` AS `art`,`thumb_mime` AS `art_mime` FROM `album` WHERE `id`='$id'";
+ $db_results = Dba::query($sql);
+
+ $results = Dba::fetch_assoc($db_results);
+ if (strlen($results['art_mime'])) {
+ $results['resized'] = true;
+ }
+ else { return false; }
+
+ return $results;
+
+ } // get_resized_db_art
+
+ /**
+ * get_db_art
* returns the album art from the db along with the mime type
*/
- function get_db_art() {
+ public 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());
+ $sql = "SELECT `art`,`art_mime` FROM `album` WHERE `id`='$this->id'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
if (!$results['art']) { return array(); }
@@ -585,6 +614,22 @@ class Album {
} // insert_art
+ /**
+ * save_resized_art
+ * This takes data from a gd resize operation and saves
+ * it back into the database as a thumbnail
+ */
+ public static function save_resized_art($data,$mime,$album) {
+
+ $data = Dba::escape($data);
+ $mime = Dba::escape($mime);
+ $album = Dba::escape($album);
+
+ $sql = "UPDATE `album` SET `thumb`='$data',`thumb_mime`='$mime' " .
+ "WHERE `album`.`id`='$album'";
+ $db_results = Dba::query($sql);
+
+ } // save_resized_art
} //end of album class
diff --git a/lib/class/artist.class.php b/lib/class/artist.class.php
index 6348339e..18fa31cf 100644
--- a/lib/class/artist.class.php
+++ b/lib/class/artist.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
@@ -31,10 +31,10 @@ class Artist {
var $albums;
var $prefix;
- /*!
- @function Artist
- @discussion Artist class, for modifing a artist
- @param $artist_id The ID of the artist
+ /**
+ * Artist
+ * Artist class, for modifing a artist
+ * Takes the ID of the artist and pulls the info from the db
*/
function Artist($artist_id = 0) {
@@ -46,32 +46,30 @@ class Artist {
/* Get the information from the db */
$info = $this->_get_info();
- if (count($info)) {
- /* Assign Vars */
- $this->name = $info['name'];
- $this->prefix = $info['prefix'];
- } // if info
+
+ foreach ($info as $key=>$value) {
+ $this->$key = $value;
+ } // foreach info
return true;
} //constructor
- /*!
- @function _get_info
- @discussion get's the vars for $this out of the database
- @param $this->id Taken from the object
+ /**
+ * _get_info
+ * get's the vars for $this out of the database taken from the object
*/
- function _get_info() {
+ private function _get_info() {
/* Grab the basic information from the catalog and return it */
- $sql = "SELECT * FROM artist WHERE id='" . sql_escape($this->id) . "'";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT * FROM artist WHERE id='" . Dba::escape($this->id) . "'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
return $results;
- } //get_info
+ } // _get_info
/*!
@function get_albums
@@ -161,9 +159,9 @@ class Artist {
$albums = 0;
$sql = "SELECT COUNT(song.id) FROM song WHERE song.artist='$this->id' GROUP BY song.album";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_array($db_results)) {
+ while ($r = Dba::fetch_row($db_results)) {
$songs += $r[0];
$albums++;
}
@@ -193,7 +191,7 @@ class Artist {
$this->full_name = scrub_out(trim($this->prefix . " " . $this->name));
//FIXME: This should be f_link
- $this->link = "<a href=\"" . conf('web_path') . "/artists.php?action=show&amp;artist=" . $this->id . "\" title=\"" . $this->full_name . "\">" . $name . "</a>";
+ $this->f_link = "<a href=\"" . Config::get('web_path') . "/artists.php?action=show&amp;artist=" . $this->id . "\" title=\"" . $this->full_name . "\">" . $name . "</a>";
// Get the counts
$this->get_count();
diff --git a/lib/class/catalog.class.php b/lib/class/catalog.class.php
index 50db6749..2f78949b 100644
--- a/lib/class/catalog.class.php
+++ b/lib/class/catalog.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All Rights Reserved
This program is free software; you can redistribute it and/or
@@ -27,22 +27,22 @@
*/
class Catalog {
- var $name;
- var $last_update;
- var $last_add;
- var $key;
- var $rename_pattern;
- var $sort_pattern;
- var $catalog_type;
+ public $name;
+ public $last_update;
+ public $last_add;
+ public $key;
+ public $rename_pattern;
+ public $sort_pattern;
+ public $catalog_type;
/* This is a private var that's used during catalog builds */
- var $_playlists = array();
- var $_art_albums = array();
+ private $_playlists = array();
+ private $_art_albums = array();
// Used in functions
- var $albums = array();
- var $artists = array();
- var $genres = array();
+ public $albums = array();
+ public $artists = array();
+ public $genres = array();
/**
* Catalog
@@ -52,57 +52,47 @@ class Catalog {
*/
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 = intval($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->key = $info->key;
- $this->rename_pattern = $info->rename_pattern;
- $this->sort_pattern = $info->sort_pattern;
- $this->catalog_type = $info->catalog_type;
- } //catalog_id
+ if (!$catalog_id) { return false; }
- } //constructor
+ /* Assign id for use in get_info() */
+ $this->id = intval($catalog_id);
+ /* Get the information from the db */
+ $info = $this->_get_info();
- /*!
- @function get_info
- @discussion get's the vars for $this out of the database
- @param $this->id Taken from the object
- */
- function get_info() {
+ foreach ($info as $key=>$value) {
+ $this->$key = $value;
+ }
+
+ } //constructor
+
+ /**
+ * _get_info
+ * get's the vars for $this out of the database requires an id
+ */
+ private 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());
+ $sql = "SELECT * FROM `catalog` WHERE `id`='$this->id'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_object($db_results);
+ $results = Dba::fetch_assoc($db_results);
return $results;
- } //get_info
+ } // _get_info
+ /**
+ * get_catalogs
+ *Pull all the current catalogs
+ */
+ public static function get_catalogs() {
- /*!
- @function get_catalogs
- @discussion Pull all the current catalogs
- */
- function get_catalogs() {
-
- $sql = "SELECT id FROM catalog";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT `id` FROM `catalog`";
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_object($db_results)) {
- $results[] = new Catalog($r->id);
+ while ($r = Dba::fetch_assoc($db_results)) {
+ $results[] = new Catalog($r['id']);
}
return $results;
@@ -115,10 +105,10 @@ class Catalog {
*/
function get_catalog_ids() {
- $sql = "SELECT id FROM catalog";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT `id` FROM `catalog`";
+ $db_results = Dba::qery($sql);
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$results[] = $r['id'];
}
@@ -126,46 +116,89 @@ class Catalog {
} // get_catalog_ids
- /*!
- @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_stats
+ * This returns an hash with the #'s for the different
+ * objects that are assoicated with this catalog. This is used
+ * to build the stats box, it also calculates time
+ */
+ public static function get_stats($catalog_id=0) {
- } // get_catalog_stats
+ $results = self::count_songs($catalog_id);
+ $results = array_merge(self::count_users($catalog_id),$results);
+// $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);
- /*!
- @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) {
+ return $results;
- $sql = "SELECT SUM(song.time) FROM song";
- if ($catalog_id) {
- $sql .= " WHERE catalog='$catalog_id'";
- }
+ } // get_stats
- $db_results = mysql_query($sql, dbh());
+ /**
+ * count_songs
+ * This returns the current # of songs, albums, artists, genres
+ * in this catalog
+ */
+ public static function count_songs($catalog_id='') {
+
+ if ($catalog_id) { $catalog_search = "WHERE `catalog`='" . Dba::escape($catalog_id) . "'"; }
+
+ $sql = "SELECT `id`,`album`,`artist`,`genre`,`file`,`size`,`time` FROM `song` $catalog_search";
+ $db_results = Dba::query($sql);
+
+ while ($data = Dba::fetch_assoc($db_results)) {
+ $albums[$data['album']] = true;
+ $artists[$data['artist']] = true;
+ $genres[$data['genre']] = true;
+ $dir = basename($data['file']);
+ $folders[$dir] = true;
+ $size += $data['size'];
+ $time += $data['time'];
+ $songs++;
+ }
- $results = mysql_fetch_field($db_results);
+ $results['songs'] = $songs;
+ $results['albums'] = count($albums);
+ $results['artists'] = count($artists);
+ $results['genres'] = count($genres);
+ $results['folders'] = count($folders);
+ $results['size'] = $size;
+ $results['time'] = $time;
- /* Do some conversion to get Hours Min Sec */
+ return $results;
+ } // count_songs
- return $results;
+ /**
+ * count_users
+ * This returns the total number of users in the ampache instance
+ */
+ public static function count_users($catalog_id='') {
+
+ // Count total users
+ $sql = "SELECT COUNT(id) FROM `user`";
+ $db_results = Dba::query($sql);
+ $data = Dba::fetch_row($db_results);
+ $results['users'] = $data['0'];
+
+ // Get the connected users
+ $time = time();
+ $last_seen_time = $time - 1200;
+ $sql = "SELECT count(DISTINCT s.username) FROM session AS s " .
+ "INNER JOIN user AS u ON s.username = u.username " .
+ "WHERE s.expire > " . $time . " " .
+ "AND u.last_seen > " . $last_seen_time;
+ $db_results = Dba::query($sql);
+ $data = Dba::fetch_row($db_results);
+
+ $results['connected'] = $data['0'];
- } // get_song_time
+ return $results;
+ } // count_users
/*!
@function get_song_size
@@ -234,25 +267,6 @@ class Catalog {
/*!
- @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
*/
@@ -585,16 +599,16 @@ class Catalog {
* Gets an array of the disabled songs for all catalogs
* and returns full song objects with them
*/
- function get_disabled($count=0) {
+ public static function get_disabled($count=0) {
$results = array();
if ($count) { $limit_clause = " LIMIT $count"; }
$sql = "SELECT id FROM song WHERE enabled='0' $limit_clause";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_array($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$results[] = new Song($r['id']);
}
@@ -602,7 +616,6 @@ class Catalog {
} // get_disabled
-
/*!
@function get_files
@discussion Get's an array of .mp3s and returns the filenames
diff --git a/lib/class/dba.class.php b/lib/class/dba.class.php
index 97810613..b37ca39d 100644
--- a/lib/class/dba.class.php
+++ b/lib/class/dba.class.php
@@ -21,6 +21,15 @@
/* Make sure they aren't directly accessing it */
if (INIT_LOADED != '1') { exit; }
+/**
+ * Dba
+ * This is the database abstraction class
+ * It duplicates the functionality of mysql_???
+ * with a few exceptions, the row and assoc will always
+ * return an array, simplifying checking on the far end
+ * it will also auto-connect as needed, and has a default
+ * database simplifying queries in most cases.
+ */
class Dba {
private static $_default_db;
@@ -66,6 +75,7 @@ class Dba {
/**
* fetch_assoc
* This emulates the mysql_fetch_assoc and takes a resource result
+ * we force it to always return an array, albit an empty one
*/
public static function fetch_assoc($resource) {
@@ -80,6 +90,7 @@ class Dba {
/**
* fetch_row
* This emulates the mysql_fetch_row and takes a resource result
+ * we force it to always return an array, albit an empty one
*/
public static function fetch_row($resource) {
@@ -92,16 +103,31 @@ class Dba {
} // fetch_row
/**
+ * num_rows
+ * This emulates the mysql_num_rows function which is really
+ * just a count of rows returned by our select statement, this
+ * doesn't work for updates or inserts
+ */
+ public static function num_rows($resource) {
+
+ $result = mysql_num_rows($resource);
+
+ if (!$result) { return '0'; }
+
+ return $result;
+ } // num_rows
+
+ /**
* _connect
* This connects to the database, used by the DBH function
*/
private static function _connect($db_name) {
if (self::$_default_db == $db_name) {
- $username = Config::get('mysql_username');
- $hostname = Config::get('mysql_hostname');
- $password = Config::get('mysql_password');
- $database = Config::get('mysql_database');
+ $username = Config::get('database_username');
+ $hostname = Config::get('database_hostname');
+ $password = Config::get('database_password');
+ $database = Config::get('database_name');
}
else {
// Do this later
@@ -111,7 +137,7 @@ class Dba {
if (!$dbh) { debug_event('Database','Error unable to connect to database' . mysql_error(),'1'); }
$select_db = mysql_select_db($database,$dbh);
-
+
return $dbh;
} // _connect
@@ -125,13 +151,16 @@ class Dba {
if (!$database) { $database = self::$_default_db; }
- if (!is_resource(self::$config->get($database))) {
+ // Assign the Handle name that we are going to store
+ $handle = 'dbh_' . $database;
+
+ if (!is_resource(Config::get($handle))) {
$dbh = self::_connect($database);
- self::$config->set($database,$dbh,1);
+ Config::set($handle,$dbh,1);
return $dbh;
}
else {
- return self::$config->get($database);
+ return Config::get($handle);
}
@@ -154,10 +183,9 @@ class Dba {
* This is the auto init function it sets up the config class
* and also sets the default database
*/
- public static function auto_init() {
+ public static function _auto_init() {
- self::$_default_db = Config::get('mysql_database');
- self::$config = new Config();
+ self::$_default_db = Config::get('database_name');
return true;
diff --git a/lib/class/error.class.php b/lib/class/error.class.php
index 013a2e08..bc839bdb 100644
--- a/lib/class/error.class.php
+++ b/lib/class/error.class.php
@@ -1,92 +1,80 @@
<?php
-/*
-
- Copyright (c) 2001 - 2006 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
- 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 Error handler requires error_results() function
-
-*/
-class Error {
-
- //Basic Componets
- var $error_state=0;
-
- /* Generated values */
- var $errors = array();
-
- /*!
- @function error
- @discussion this is the constructor for the error class
- */
- function Error() {
-
- return true;
-
- } //constructor
+/**
+ * Error class
+ * This is the baic error class, its better now that we can use php5
+ * hello static functions and variables
+ */
+class Error {
+
+ public static $state = false; // set to one when an error occurs
+ public static $errors = array(); // Errors array key'd array with errors that have occured
+
+ /**
+ * __constructor
+ * This does nothing... amazing isn't it!
+ */
+ private function __construct() {
+
+ // Rien a faire
+
+ } // __construct
+
+ /**
+ * add
+ * This is a public static function it adds a new error message to the array
+ * It can optionally clobber rather then adding to the error message
+ */
+ public static function add($name,$message,$clobber=0) {
+
+ // Make sure its set first
+ if (!isset(Error::$errors[$name])) {
+ Error::$errors[$name] = $message;
+ Error::$state = 1;
+ return true;
+ }
- /*!
- @function add_error
- @discussion adds an error to the static array stored in
- error_results()
- */
- function add_error($name,$description) {
+ // They want us to clobber it
+ if ($clobber) {
+ Error::$state = 1;
+ Error::$errors[$name] = $message;
+ return true;
+ }
- $array = array($name=>$description);
+ // They want us to append the error, add a BR\n and then the message
+ else {
+ Error::$state = 1;
+ Error::$errors[$name] .= "<br />\n" . $message;
+ return true;
+ }
- error_results($array,1);
- $this->error_state = 1;
- return true;
-
- } // add_error
+ } // add
+ /**
+ * get
+ * This returns an error by name
+ */
+ public static function get($name) {
- /*!
- @function has_error
- @discussion returns true if the name given has an error,
- false if it doesn't
- */
- function has_error($name) {
+ if (!isset(Error::$errors[$name])) { return ''; }
- $results = error_results($name);
+ return Error::$errors[$name];
- if (!empty($results)) {
- return true;
- }
+ } // get
- return false;
+ /**
+ * display
+ * This prints the error out with a standard Error class span
+ * Ben Goska: Renamed from print to display, print is reserved
+ */
+ public static function display($name) {
- } // has_error
+ // Be smart about this, if no error don't print
+ if (!isset(Error::$errors[$name])) { return ''; }
- /*!
- @function print_error
- @discussion prints out the error for a name if it exists
- */
- function print_error($name) {
+ echo '<span class="error">' . Error::$errors[$name] . '</span>';
- if ($this->has_error($name)) {
- echo "<div class=\"fatalerror\">" . error_results($name) . "</div>\n";
- }
+ } // display
- } // print_error
-} //end error class
-?>
+} // Error
diff --git a/lib/class/flag.class.php b/lib/class/flag.class.php
index 20ed5d8b..d54879f5 100644
--- a/lib/class/flag.class.php
+++ b/lib/class/flag.class.php
@@ -70,14 +70,14 @@ class Flag {
* _get_info
* Private function for getting the information for this object from the database
*/
- function _get_info() {
+ private function _get_info() {
- $id = sql_escape($this->id);
+ $id = Dba::escape($this->id);
- $sql = "SELECT * FROM flagged WHERE id='$id'";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT * FROM `flagged` WHERE `id`='$id'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
return $results;
@@ -88,16 +88,16 @@ class Flag {
* This returns the id's of the most recently flagged songs, it takes an int
* as an argument which is the count of the object you want to return
*/
- function get_recent($count=0) {
+ public static function get_recent($count=0) {
if ($count) { $limit = " LIMIT " . intval($count); }
$results = array();
$sql = "SELECT id FROM flagged ORDER BY date " . $limit;
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$results[] = $r['id'];
}
diff --git a/lib/class/song.class.php b/lib/class/song.class.php
index 64c99509..7711ea95 100644
--- a/lib/class/song.class.php
+++ b/lib/class/song.class.php
@@ -57,39 +57,19 @@ class Song {
*/
function Song($song_id = 0) {
- /* If we have passed an id then do something */
- if ($song_id) {
-
- /* Assign id for use in get_info() */
- $this->id = intval($song_id);
-
- /* Get the information from the db */
- if ($info = $this->_get_info()) {
-
- /* Assign Vars */
- $this->file = $info->file;
- $this->album = $info->album;
- $this->artist = $info->artist;
- $this->title = $info->title;
- $this->comment = $info->comment;
- $this->year = $info->year;
- $this->bitrate = $info->bitrate;
- $this->rate = $info->rate;
- $this->mode = $info->mode;
- $this->size = $info->size;
- $this->time = $info->time;
- $this->track = $info->track;
- $this->genre = $info->genre;
- $this->addition_time = $info->addition_time;
- $this->catalog = $info->catalog;
- $this->played = $info->played;
- $this->update_time = $info->update_time;
- $this->enabled = $info->enabled;
+ if (!$song_id) { return false; }
+ /* Assign id for use in get_info() */
+ $this->id = intval($song_id);
+
+ /* Get the information from the db */
+ if ($info = $this->_get_info()) {
+
+ foreach ($info as $key=>$value) {
+ $this->$key = $value;
+ }
// Format the Type of the song
$this->format_type();
- }
-
}
} //constructor
@@ -100,16 +80,16 @@ class Song {
@discussion get's the vars for $this out of the database
@param $this->id Taken from the object
*/
- function _get_info() {
+ private function _get_info() {
/* Grab the basic information from the catalog and return it */
$sql = "SELECT song.id,file,catalog,album,year,artist,".
"title,bitrate,rate,mode,size,time,track,genre,played,song.enabled,update_time,".
- "addition_time FROM song WHERE song.id = '$this->id'";
+ "addition_time FROM `song` WHERE `song`.`id` = '$this->id'";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_object($db_results);
+ $results = Dba::fetch_assoc($db_results);
return $results;
@@ -229,10 +209,10 @@ class Song {
if (!$album_id) { $album_id = $this->album; }
- $sql = "SELECT name,prefix FROM album WHERE id='" . sql_escape($album_id) . "'";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT `name`,`prefix` FROM `album` WHERE `id`='" . Dba::escape($album_id) . "'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
if ($results['prefix']) {
return $results['prefix'] . " " .$results['name'];
@@ -251,10 +231,10 @@ class Song {
if (!$artist_id) { $artist_id = $this->artist; }
- $sql = "SELECT name,prefix FROM artist WHERE id='" . sql_escape($artist_id) . "'";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT name,prefix FROM artist WHERE id='" . Dba::escape($artist_id) . "'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
if ($results['prefix']) {
return $results['prefix'] . " " . $results['name'];
@@ -274,10 +254,10 @@ class Song {
if (!$genre_id) { $genre_id = $this->genre; }
- $sql = "SELECT name FROM genre WHERE id='" . sql_escape($genre_id) . "'";
- $db_results = mysql_query($sql, dbh());
+ $sql = "SELECT name FROM genre WHERE id='" . Dba::escape($genre_id) . "'";
+ $db_results = Dba::query($sql);
- $results = mysql_fetch_assoc($db_results);
+ $results = Dba::fetch_assoc($db_results);
return $results['name'];
@@ -681,19 +661,19 @@ class Song {
// Format the album name
$this->f_album_full = $this->get_album_name();
- $this->f_album = truncate_with_ellipse($this->f_album_full,conf('ellipse_threshold_album'));
+ $this->f_album = truncate_with_ellipse($this->f_album_full,Config::get('ellipse_threshold_album'));
// Format the artist name
$this->f_artist_full = $this->get_artist_name();
- $this->f_artist = truncate_with_ellipse($this->f_artist_full,conf('ellipse_threshold_artist'));
+ $this->f_artist = truncate_with_ellipse($this->f_artist_full,Config::get('ellipse_threshold_artist'));
// Format the title
- $this->f_title = truncate_with_ellipse($this->title,conf('ellipse_threshold_title'));
+ $this->f_title = truncate_with_ellipse($this->title,Config::get('ellipse_threshold_title'));
// Create Links for the different objects
- $this->f_link = "<a href=\"" . conf('web_path') . "/song.php?action=single_song&amp;song_id=" . $this->id . "\">$this->f_title</a>";
- $this->f_album_link = "<a href=\"" . conf('web_path') . "/albums.php?action=show&amp;album=" . $this->album . "\">$this->f_album</a>";
- $this->f_artist_link = "<a href=\"" . conf('web_path') . "/artists.php?action=show&amp;artist=" . $this->artist . "\">$this->f_artist</a>";
+ $this->f_link = "<a href=\"" . Config::get('web_path') . "/stream.php?action=single_song&amp;song_id=" . $this->id . "\">$this->f_title</a>";
+ $this->f_album_link = "<a href=\"" . Config::get('web_path') . "/albums.php?action=show&amp;album=" . $this->album . "\">$this->f_album</a>";
+ $this->f_artist_link = "<a href=\"" . Config::get('web_path') . "/artists.php?action=show&amp;artist=" . $this->artist . "\">$this->f_artist</a>";
// Format the Bitrate
$this->f_bitrate = intval($this->bitrate/1000) . "-" . strtoupper($this->mode);
@@ -802,7 +782,7 @@ class Song {
$user_id = scrub_out($GLOBALS['user']->id);
$song_id = $this->id;
- if (conf('require_session')) {
+ if (Config::get('require_session')) {
if ($session_id) {
$session_string = "&sid=" . $session_id;
}
@@ -823,10 +803,10 @@ class Song {
$this->format();
$song_name = rawurlencode($this->f_artist_full . " - " . $this->title . "." . $type);
- $web_path = conf('web_path');
+ $web_path = Config::get('web_path');
- if (conf('force_http_play') OR !empty($force_http)) {
- $port = conf('http_port');
+ if (Config::get('force_http_play') OR !empty($force_http)) {
+ $port = Config::get('http_port');
if (preg_match("/:\d+/",$web_path)) {
$web_path = str_replace("https://", "http://",$web_path);
$web_path = preg_replace("/:\d+/",":$port",$web_path);
@@ -854,9 +834,9 @@ class Song {
$conf_var = 'transcode_' . $this->type;
$conf_type = 'transcode_' . $this->type . '_target';
- if (conf($conf_var)) {
+ if (Config::get($conf_var)) {
$this->_transcode = true;
- $this->format_type(conf($conf_type));
+ $this->format_type(Config::get($conf_type));
debug_event('auto_transcode','Transcoding to ' . $this->type,'5');
return false;
}
diff --git a/lib/class/stats.class.php b/lib/class/stats.class.php
index 965b3362..5eef86d7 100644
--- a/lib/class/stats.class.php
+++ b/lib/class/stats.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
@@ -77,7 +77,7 @@ class Stats {
/* If they don't pass one, then use the preference */
if (!$threshold) {
- $threshold = conf('stats_threshold');
+ $threshold = Config::get('stats_threshold');
}
$count = intval($count);
@@ -88,11 +88,11 @@ class Stats {
$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" .
" WHERE object_type='$type' AND date >= '$date'" .
" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
$results = array();
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$results[] = $r;
}
@@ -105,29 +105,30 @@ class Stats {
* This gets all stats for atype based on user with thresholds and all
* If full is passed, doesn't limit based on date
*/
- function get_user($count,$type,$user,$full='') {
+ public static function get_user($count,$type,$user,$full='') {
$count = intval($count);
- $type = $this->validate_type($type);
- $user = sql_escape($user);
+ $type = self::validate_type($type);
+ $user = Dba::escape($user);
/* If full then don't limit on date */
if ($full) {
$date = '0';
}
else {
- $date = time() - (86400*conf('stats_threshold'));
+ $date = time() - (86400*Config::get('stats_threshold'));
}
/* Select Objects based on user */
+ //FIXME:: Requires table can, look at improving
$sql = "SELECT object_id,COUNT(id) AS `count` FROM object_count" .
" WHERE object_type='$type' AND date >= '$date' AND user = '$user'" .
" GROUP BY object_id ORDER BY `count` DESC LIMIT $count";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
$results = array();
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$results[] = $r;
}
@@ -140,7 +141,7 @@ class Stats {
* This function takes a type and returns only those
* which are allowed, ensures good data gets put into the db
*/
- function validate_type($type) {
+ public static function validate_type($type) {
switch ($type) {
case 'artist':
@@ -160,5 +161,31 @@ class Stats {
} // validate_type
+ /**
+ * get_newest
+ * This returns an array of the newest artists/albums/whatever
+ * in this ampache instance
+ */
+ public static function get_newest($type,$limit='') {
+
+ if (!$limit) { $limit = Config::get('popular_threshold'); }
+
+ $type = self::validate_type($type);
+ $object_name = ucfirst($type);
+
+ $sql = "SELECT DISTINCT($type) FROM `song` ORDER BY `addition_time` DESC " .
+ "LIMIT $limit";
+ $db_results = Dba::query($sql);
+
+ while ($r = Dba::fetch_row($db_results)) {
+ $object = new $object_name($r['0']);
+ $object->format();
+ $items[] = $object;
+ } // end while results
+
+ return $items;
+
+ } // get_newest
+
} //Stats class
?>
diff --git a/lib/class/stream.class.php b/lib/class/stream.class.php
index 8d081824..375e04fe 100644
--- a/lib/class/stream.class.php
+++ b/lib/class/stream.class.php
@@ -1,7 +1,7 @@
<?php
/*
- Copyright (c) 2001 - 2006 Ampache.org
+ Copyright (c) 2001 - 2007 Ampache.org
All rights reserved.
This program is free software; you can redistribute it and/or
@@ -43,16 +43,16 @@ class Stream {
$this->type = $type;
$this->songs = $song_ids;
- $this->web_path = conf('web_path');
+ $this->web_path = Config::get('web_path');
- if (conf('force_http_play')) {
- $port = conf('http_port');
+ if (Config::get('force_http_play')) {
+ $port = Config::get('http_port');
$this->web_path = preg_replace("/https/", "http",$this->web_path);
$this->web_path = preg_replace("/:\d+/",":$port",$this->web_path);
}
$this->sess = session_id();
- $this->user_id = $_SESSION['userdata']['username'];
+ $this->user_id = $GLOBALS['user']->id;
} //constructor
@@ -120,24 +120,25 @@ class Stream {
} // simple_m3u
- /*!
- @function create_m3u
- @discussion creates an m3u file
- */
- function create_m3u() {
+ /**
+ * create_m3u
+ * creates an m3u file, this includes the EXTINFO and as such can be
+ * large with very long playlsits
+ */
+ public function create_m3u() {
// Send the client an m3u playlist
header("Cache-control: public");
header("Content-Disposition: filename=playlist.m3u");
header("Content-Type: audio/x-mpegurl;");
echo "#EXTM3U\n";
+
foreach($this->songs as $song_id) {
$song = new Song($song_id);
- $song->format_song();
- if ($song->type == ".flac") { $song->type = ".ogg"; }
+ $song->format();
$song_name = $song->f_artist_full . " - " . $song->title . "." . $song->type;
echo "#EXTINF:$song->time," . $song->f_artist_full . " - " . $song->title . "\n";
- $sess = $_COOKIE[conf('sess_name')];
+ $sess = $_COOKIE[Config::get('sess_name')];
if($GLOBALS['user']->prefs['play_type'] == 'downsample') {
$ds = $GLOBALS['user']->prefs['sample_rate'];
}
diff --git a/lib/class/update.class.php b/lib/class/update.class.php
index bb93a9e5..8c7ebe1e 100644
--- a/lib/class/update.class.php
+++ b/lib/class/update.class.php
@@ -37,9 +37,9 @@
class Update {
- var $key;
- var $value;
- var $versions; // array containing version information
+ public $key;
+ public $value;
+ public static $versions; // array containing version information
/*!
@function Update
@@ -77,16 +77,16 @@ class Update {
because we may not have the update_info table we have to check
for it's existance first.
*/
- function get_version() {
+ public static function get_version() {
/* Make sure that update_info exits */
$sql = "SHOW TABLES LIKE 'update_info'";
- $db_results = mysql_query($sql, dbh());
- if (!is_resource(dbh())) { header("Location: test.php"); }
+ $db_results = Dba::query($sql);
+ if (!is_resource(Dba::dbh())) { header("Location: test.php"); }
// If no table
- if (!mysql_num_rows($db_results)) {
+ if (!Dba::num_rows($db_results)) {
$version = '310000';
@@ -95,9 +95,9 @@ class Update {
else {
// If we've found the update_info table, let's get the version from it
$sql = "SELECT * FROM update_info WHERE `key`='db_version'";
- $db_results = mysql_query($sql, dbh());
- $results = mysql_fetch_object($db_results);
- $version = $results->value;
+ $db_results = Dba::query($sql);
+ $results = Dba::fetch_assoc($db_results);
+ $version = $results['value'];
}
return $version;
@@ -117,24 +117,24 @@ class Update {
} // format_version
- /*!
- @function need_update
- @discussion checks to see if we need to update
- maintain at all
- */
- function need_update() {
+ /**
+ * need_update
+ * checks to see if we need to update
+ * maintain at all
+ */
+ public static function need_update() {
- $current_version = $this->get_version();
+ $current_version = self::get_version();
- if (!is_array($this->versions)) {
- $this->versions = $this->populate_version();
+ if (!is_array(self::$versions)) {
+ self::$versions = self::populate_version();
}
/*
Go through the versions we have and see if
we need to apply any updates
*/
- foreach ($this->versions as $update) {
+ foreach (self::$versions as $update) {
if ($update['version'] > $current_version) {
return true;
}
@@ -175,7 +175,7 @@ class Update {
@discussion just sets an array the current differences
that require an update
*/
- function populate_version() {
+ public static function populate_version() {
/* Define the array */
$version = array();
diff --git a/lib/class/user.class.php b/lib/class/user.class.php
index dfd50222..e50b6bb8 100644
--- a/lib/class/user.class.php
+++ b/lib/class/user.class.php
@@ -54,16 +54,11 @@ class User {
$info = $this->_get_info();
if (!count($info)) { return false; }
+ foreach ($info as $key=>$value) {
+ $this->$key = $value;
+ }
$this->uid = $info->id;
- $this->username = $info->username;
- $this->fullname = $info->fullname;
- $this->access = $info->access;
- $this->disabled = $info->disabled;
- $this->email = $info->email;
- $this->last_seen = $info->last_seen;
- $this->create_date = $info->create_date;
- $this->validation = $info->validation;
$this->set_preferences();
// Make sure the Full name is always filled
@@ -77,17 +72,36 @@ class User {
*/
function _get_info() {
- $id = sql_escape($this->id);
+ $id = Dba::escape($this->id);
$sql = "SELECT * FROM `user` WHERE `id`='" . $id . "'";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- return mysql_fetch_object($db_results);
+ return Dba::fetch_assoc($db_results);
} // _get_info
/**
+ * get_from_username
+ * This returns a built user from a username. This is a
+ * static function so it doesn't require an instance
+ */
+ public static function get_from_username($username) {
+
+ $username = Dba::escape($username);
+
+ $sql = "SELECT `id` FROM `user` WHERE `username`='$username'";
+ $db_results = Dba::query($sql);
+ $results = Dba::fetch_assoc($db_results);
+
+ $user = new User($results['id']);
+
+ return $user;
+
+ } // get_from_username
+
+ /**
* get_preferences
* This is a little more complicate now that we've got many types of preferences
* This funtions pulls all of them an arranges them into a spiffy little array
@@ -140,10 +154,11 @@ class User {
$sql = "SELECT preferences.name,user_preference.value FROM preferences,user_preference WHERE user_preference.user='$this->id' " .
"AND user_preference.preference=preferences.id AND preferences.type != 'system'";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_object($db_results)) {
- $this->prefs[$r->name] = $r->value;
+ while ($r = Dba::fetch_assoc($db_results)) {
+ $key = $r['name'];
+ $this->prefs[$key] = $r['value'];
}
} // get_preferences
@@ -153,10 +168,9 @@ class User {
*/
function get_favorites($type) {
- $web_path = conf('web_path');
+ $web_path = Config::get('web_path');
- $stats = new Stats();
- $results = $stats->get_user(conf('popular_threshold'),$type,$this->uid,1);
+ $results = Stats::get_user(Config::get('popular_threshold'),$type,$this->id,1);
$items = array();
@@ -165,7 +179,7 @@ class User {
if ($type == 'song') {
$data = new Song($r['object_id']);
$data->count = $r['count'];
- $data->format_song();
+ $data->format();
$data->f_name = $data->f_link;
$items[] = $data;
}
@@ -173,23 +187,23 @@ class User {
elseif ($type == 'album') {
$data = new Album($r['object_id']);
$data->count = $r['count'];
- $data->format_album();
+ $data->format();
$items[] = $data;
}
/* If its an artist */
elseif ($type == 'artist') {
$data = new Artist($r['object_id']);
$data->count = $r['count'];
- $data->format_artist();
- $data->f_name = $data->link;
+ $data->format();
+ $data->f_name = $data->f_link;
$items[] = $data;
}
/* If it's a genre */
elseif ($type == 'genre') {
$data = new Genre($r['object_id']);
$data->count = $r['count'];
- $data->format_genre();
- $data->f_name = $data->link;
+ $data->format();
+ $data->f_name = $data->f_link;
$items[] = $data;
}
@@ -208,24 +222,24 @@ class User {
/* First pull all of your ratings of this type */
$sql = "SELECT object_id,user_rating FROM ratings " .
- "WHERE object_type='" . sql_escape($type) . "' AND user='" . sql_escape($this->id) . "'";
- $db_results = mysql_query($sql,dbh());
+ "WHERE object_type='" . Dba::escape($type) . "' AND user='" . Dba::escape($this->id) . "'";
+ $db_results = Dba::query($sql);
// Incase they only have one user
$users = array();
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
/* Store the fact that you rated this */
$key = $r['object_id'];
$ratings[$key] = true;
/* Build a key'd array of users with this same rating */
- $sql = "SELECT user FROM ratings WHERE object_type='" . sql_escape($type) . "' " .
- "AND user !='" . sql_escape($this->id) . "' AND object_id='" . sql_escape($r['object_id']) . "' " .
- "AND user_rating ='" . sql_escape($r['user_rating']) . "'";
- $user_results = mysql_query($sql,dbh());
+ $sql = "SELECT user FROM ratings WHERE object_type='" . Dba::escape($type) . "' " .
+ "AND user !='" . Dba::escape($this->id) . "' AND object_id='" . Dba::escape($r['object_id']) . "' " .
+ "AND user_rating ='" . Dba::escape($r['user_rating']) . "'";
+ $user_results = Dba::query($sql);
- while ($user_info = mysql_fetch_assoc($user_results)) {
+ while ($user_info = Dba::fetch_assoc($user_results)) {
$key = $user_info['user'];
$users[$key]++;
}
@@ -243,11 +257,11 @@ class User {
/* Find everything they've rated at 4+ */
$sql = "SELECT object_id,user_rating FROM ratings " .
- "WHERE user='" . sql_escape($user_id) . "' AND user_rating >='4' AND " .
- "object_type = '" . sql_escape($type) . "' ORDER BY user_rating DESC";
- $db_results = mysql_query($sql,dbh());
+ "WHERE user='" . Dba::escape($user_id) . "' AND user_rating >='4' AND " .
+ "object_type = '" . Dba::escape($type) . "' ORDER BY user_rating DESC";
+ $db_results = Dba::query($sql);
- while ($r = mysql_fetch_assoc($db_results)) {
+ while ($r = Dba::fetch_assoc($db_results)) {
$key = $r['object_id'];
if (isset($ratings[$key])) { continue; }
@@ -290,7 +304,7 @@ class User {
*/
function has_access($needed_level) {
- if (!conf('use_auth') || conf('demo_mode')) { return true; }
+ if (!Config::get('use_auth') || Config::get('demo_mode')) { return true; }
if ($this->access >= $needed_level) { return true; }
@@ -474,7 +488,7 @@ class User {
function update_last_seen() {
$sql = "UPDATE user SET last_seen='" . time() . "' WHERE `id`='$this->id'";
- $db_results = mysql_query($sql, dbh());
+ $db_results = Dba::query($sql);
} // update_last_seen
@@ -664,7 +678,7 @@ class User {
}
$item = "[$data->count] - $data->f_name";
- $results[]->link = $item;
+ $results[]->f_link = $item;
} // end foreach items
return $results;
diff --git a/lib/class/vainfo.class.php b/lib/class/vainfo.class.php
new file mode 100755
index 00000000..cdf793ab
--- /dev/null
+++ b/lib/class/vainfo.class.php
@@ -0,0 +1,516 @@
+<?php
+/*
+
+ Copyright (c) 2001 - 2006 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
+ 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.
+
+*/
+
+/**
+ * vainfo
+ * This class takes the information pulled from getID3 and returns it in a
+ * Ampache friendly way.
+ */
+class vainfo {
+
+ /* Default Encoding */
+ var $encoding = '';
+
+ /* Loaded Variables */
+ var $filename = '';
+ var $type = '';
+ var $tags = array();
+
+ /* Internal Information */
+ var $_raw = array();
+ var $_getID3 = '';
+ var $_iconv = false;
+ var $_file_encoding = '';
+ var $_file_pattern = '';
+ var $_dir_pattern = '';
+
+ /**
+ * Constructor
+ * This function just sets up the class, it doesn't
+ * actually pull the information
+ */
+ function vainfo($file,$encoding='',$dir_pattern,$file_pattern) {
+
+ $this->filename = $file;
+ if ($encoding) {
+ $this->encoding = $encoding;
+ }
+ else {
+ $this->encoding = conf('site_charset');
+ }
+
+ /* These are needed for the filename mojo */
+ $this->_file_pattern = $file_pattern;
+ $this->_dir_pattern = $dir_pattern;
+
+ // Initialize getID3 engine
+ $this->_getID3 = new getID3();
+ $this->_getID3->option_md5_data = false;
+ $this->_getID3->option_md5_data_source = false;
+ $this->_getID3->option_tags_html = false;
+ $this->_getID3->option_extra_info = false;
+ $this->_getID3->option_tag_lyrics3 = false;
+ $this->_getID3->encoding = $this->encoding;
+
+ /* Check for ICONV */
+ if (function_exists('iconv')) {
+ $this->_iconv = true;
+ }
+
+ } // vainfo
+
+
+ /**
+ * get_info
+ * This function takes a filename and returns the $_info array
+ * all filled up with tagie goodness or if specified filename
+ * pattern goodness
+ */
+ function get_info() {
+
+ /* Get the Raw file information */
+ $this->_raw = $this->_getID3->analyze($this->filename);
+
+ /* Figure out what type of file we are dealing with */
+ $this->type = $this->_get_type();
+
+ /* This is very important, figure out th encoding of the
+ * file
+ */
+ $this->_set_encoding();
+
+ /* Get the general information about this file */
+ $info = $this->_get_info();
+
+
+ /* Gets the Tags */
+ $this->tags = $this->_get_tags();
+ $this->tags['info'] = $info;
+
+ unset($this->_raw);
+
+ } // get_info
+
+ /**
+ * _set_encoding
+ * This function trys to figure out what the encoding
+ * is based on the file type and sets the _file_encoding
+ * var to whatever it finds, the default is UTF-8 if we
+ * can't find anything
+ */
+ function _set_encoding() {
+ /* Switch on the file type */
+ switch ($this->type) {
+ case 'mp3':
+ case 'ogg':
+ case 'flac':
+ default:
+ $this->_file_encoding = $this->_raw['encoding'];
+ break;
+ } // end switch
+
+ } // _get_encoding
+
+ /**
+ * _get_type
+ * This function takes the raw information and figures out
+ * what type of file we are dealing with for use by the tag
+ * function
+ */
+ function _get_type() {
+
+ /* There are a few places that the file type can
+ * come from, in the end we trust the encoding
+ * type
+ */
+ if ($type = $this->_raw['audio']['streams']['0']['dataformat']) {
+ $this->_clean_type($type);
+ return $type;
+ }
+ if ($type = $this->_raw['audio']['dataformat']) {
+ $this->_clean_type($type);
+ return $type;
+ }
+ if ($type = $this->_raw['fileformat']) {
+ $this->_clean_type($type);
+ return $type;
+ }
+
+ return false;
+
+ } // _get_type
+
+
+ /**
+ * _get_tags
+ * This function takes the raw information and the type and
+ * attempts to gather the tags and then normalize them into
+ * ['tag_name']['var'] = value
+ */
+ function _get_tags() {
+
+ $results = array();
+
+ /* Gather Tag information from the filenames */
+ $results['file'] = $this->_parse_filename($this->filename);
+
+ /* Return false if we don't have
+ * any tags to look at
+ */
+ if (!is_array($this->_raw['tags'])) {
+ return false;
+ }
+
+ /* The tags can come in many different shapes and colors
+ * depending on the encoding time of day and phase of the
+ * moon
+ */
+ foreach ($this->_raw['tags'] as $key=>$tag_array) {
+ switch ($key) {
+ case 'vorbiscomment':
+ $results[$key] = $this->_parse_vorbiscomment($tag_array);
+ break;
+ case 'id3v1':
+ $results[$key] = $this->_parse_id3v1($tag_array);
+ break;
+ case 'id3v2':
+ $results[$key] = $this->_parse_id3v2($tag_array);
+ break;
+ case 'ape':
+ $results[$key] = $this->_parse_ape($tag_array);
+ break;
+ case 'quicktime':
+ $results[$key] = $this->_parse_quicktime($tag_array);
+ break;
+ case 'riff':
+ $results[$key] = $this->_parse_riff($tag_array);
+ break;
+ default:
+ debug_event('vainfo','Error: Unable to determine tag type of ' . $key . ' for file ' . $this->filename . ' Assuming id3v2','5');
+ $results[$key] = $this->_parse_id3v2($tag_array);
+ break;
+ } // end switch
+
+ } // end foreach
+
+ return $results;
+
+ } // _get_tags
+
+ /**
+ * _get_info
+ * This function gathers and returns the general information
+ * about a song, vbr/cbr sample rate channels etc
+ */
+ function _get_info() {
+
+ $array = array();
+
+ /* Try to pull the information directly from
+ * the audio array
+ */
+ if ($this->_raw['audio']['bitrate_mode']) {
+ $array['bitrate_mode'] = $this->_raw['audio']['bitrate_mode'];
+ }
+ if ($this->_raw['audio']['bitrate']) {
+ $array['bitrate'] = $this->_raw['audio']['bitrate'];
+ }
+ if ($this->_raw['audio']['channels']) {
+ $array['channels'] = intval($this->_raw['audio']['channels']);
+ }
+ if ($this->_raw['audio']['sample_rate']) {
+ $array['sample_rate'] = intval($this->_raw['audio']['sample_rate']);
+ }
+ if ($this->_raw['filesize']) {
+ $array['filesize'] = intval($this->_raw['filesize']);
+ }
+ if ($this->_raw['encoding']) {
+ $array['encoding'] = $this->_raw['encoding'];
+ }
+ if ($this->_raw['mime_type']) {
+ $array['mime'] = $this->_raw['mime_type'];
+ }
+ if ($this->_raw['playtime_seconds']) {
+ $array['playing_time'] = $this->_raw['playtime_seconds'];
+ }
+
+ return $array;
+
+ } // _get_info
+
+ /**
+ * _clean_type
+ * This standardizes the type that we are given into a reconized
+ * type
+ */
+ function _clean_type($type) {
+
+ switch ($type) {
+ case 'mp3':
+ case 'mp2':
+ case 'mpeg3':
+ return 'mp3';
+ break;
+ case 'flac':
+ return 'flac';
+ break;
+ case 'vorbis':
+ return 'ogg';
+ break;
+ default:
+ /* Log the fact that we couldn't figure it out */
+ debug_event('vainfo','Unable to determine file type from ' . $type . ' on file ' . $this->filename,'5');
+ return 'unknown';
+ break;
+ } // end switch on type
+
+ } // _clean_type
+
+ /**
+ * _parse_vorbiscomment
+ * This function takes a vorbiscomment from getid3() and then
+ * returns the elements translated using iconv if needed in a
+ * pretty little format
+ */
+ function _parse_vorbiscomment($tags) {
+
+ /* Results array */
+ $array = array();
+
+ /* go through them all! */
+ foreach ($tags as $tag=>$data) {
+
+ /* We need to translate a few of these tags */
+ switch ($tag) {
+ case 'tracknumber':
+ $array['track'] = $this->_clean_tag($data['0']);
+ break;
+ case 'date':
+ $array['year'] = $this->_clean_tag($data['0']);
+ break;
+ } // end switch
+
+ $array[$tag] = $this->_clean_tag($data['0']);
+
+ } // end foreach
+
+ return $array;
+
+ } // _parse_vorbiscomment
+
+ /**
+ * _parse_id3v1
+ * This function takes a id3v1 tag set from getid3() and then
+ * returns the elements translated using iconv if needed in a
+ * pretty little format
+ */
+ function _parse_id3v1($tags) {
+
+ $array = array();
+
+ /* Go through all the tags */
+ foreach ($tags as $tag=>$data) {
+
+ /* This is our baseline for naming
+ * so no translation needed
+ */
+ $array[$tag] = $this->_clean_tag($data['0'],$this->_file_encoding);
+
+ } // end foreach
+
+ return $array;
+
+ } // _parse_id3v1
+
+ /**
+ * _parse_id3v2
+ * This function takes a id3v2 tag set from getid3() and then
+ * returns the lelements translated using iconv if needed in a
+ * pretty little format
+ */
+ function _parse_id3v2($tags) {
+
+ $array = array();
+
+ /* Go through the tags */
+ foreach ($tags as $tag=>$data) {
+
+ /**
+ * the new getid3 handles this differently
+ * so we now need to account for it :(
+ */
+ switch ($tag) {
+ case 'track_number':
+ $array['track'] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ //case 'content_type':
+ // $array['genre'] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ case 'comments':
+ $array['comment'] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ default:
+ $array[$tag] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ } // end switch on tag
+
+ } // end foreach
+
+ return $array;
+
+ } // _parse_id3v2
+
+ /**
+ * _parse_ape
+ * This function takes ape tags set by getid3() and then
+ * returns the elements translated using iconv if needed in a
+ * pretty little format
+ */
+ function _parse_ape($tags) {
+
+ foreach ($tags as $tag=>$data) {
+
+ $array[$tag] = $this->_clean_tag($data['0'],$this->_file_encoding);
+
+ } // end foreach tags
+
+ return $array;
+
+ } // _parse_ape
+
+ /**
+ * _parse_riff
+ * this function takes the riff take information passed by getid3() and
+ * then reformats it so that it matches the other formats. May require iconv
+ */
+ function _parse_riff($tags) {
+
+ foreach ($tags as $tag=>$data) {
+
+ switch ($tag) {
+ case 'product':
+ $array['album'] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ default:
+ $array[$tag] = $this->_clean_tag($data['0'],$this->_file_encoding);
+ break;
+ } // end switch on tag
+
+ } // foreach tags
+
+ return $array;
+
+ } // _parse_riff
+
+ /**
+ * _parse_quicktime
+ * this function takes the quicktime tags set by getid3() and then
+ * returns the elements translated using iconv if needed in a
+ * pretty little format
+ */
+ function _parse_quicktime($tags) {
+
+ /* Results array */
+ $array = array();
+
+ /* go through them all! */
+ foreach ($tags as $tag=>$data) {
+
+ /* We need to translate a few of these tags */
+ switch ($tag) {
+ case 'creation_date':
+ if (strlen($data['0']) > 4) {
+ /* Weird Date format, attempt to normalize */
+ $data['0'] = date("Y",strtotime($data['0']));
+ }
+ $array['year'] = $this->_clean_tag($data['0']);
+ break;
+ } // end switch
+
+ $array[$tag] = $this->_clean_tag($data['0']);
+
+ } // end foreach
+
+ return $array;
+
+ } // _parse_quicktime
+
+
+ /**
+ * _parse_filename
+ * This function uses the passed file and dir patterns
+ * To pull out extra tag information and populate it into
+ * it's own array
+ */
+ function _parse_filename($filename) {
+
+ $results = array();
+
+ $pattern = $this->_dir_pattern . '/' . $this->_file_pattern;
+ preg_match_all("/\%\w/",$pattern,$elements);
+
+ $preg_pattern = preg_quote($pattern);
+ $preg_pattern = preg_replace("/\%\w/","(.+)",$preg_pattern);
+ $preg_pattern = str_replace("/","\/",$preg_pattern);
+ $preg_pattern = "/" . $preg_pattern . "\..+$/";
+ preg_match($preg_pattern,$filename,$matches);
+ /* Cut out the Full line, we don't need that */
+ array_shift($matches);
+
+ /* Foreach through what we've found */
+ foreach ($matches as $key=>$value) {
+ $new_key = translate_pattern_code($elements['0'][$key]);
+ if ($new_key) {
+ $results[$new_key] = $value;
+ }
+ } // end foreach matches
+
+ return $results;
+
+ } // _parse_filename
+
+ /**
+ * _clean_tag
+ * This function cleans up the tag that it's passed using Iconv
+ * if we've got it. It also takes an optional encoding param
+ * for the cases where we know what the tags source encoding
+ * is, and or if it's different then the encoding recorded
+ * in the file
+ */
+ function _clean_tag($tag,$encoding='') {
+
+ /* Guess that it's UTF-8 */
+ if (!$encoding) { $encoding = 'UTF-8'; }
+
+ if ($this->_iconv AND strcasecmp($encoding,$this->encoding) != 0) {
+ $charset = $this->encoding . '//TRANSLIT';
+ $tag = iconv($encoding,$charset,$tag);
+ }
+
+ return $tag;
+
+
+
+ } // _clean_tag
+
+} // end class vainfo
+?>