diff options
author | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2007-04-23 07:31:05 +0000 |
---|---|---|
committer | Karl 'vollmerk' Vollmer <vollmer@ampache.org> | 2007-04-23 07:31:05 +0000 |
commit | a31560aec4f004e58930277758f5412d86c62adc (patch) | |
tree | 845ff6947d26b22a0f4527901dbefc97bca89d78 /lib | |
parent | 8b27d66add7ca9ba57d7e9488612cb54be4b11c1 (diff) | |
download | ampache-a31560aec4f004e58930277758f5412d86c62adc.tar.gz ampache-a31560aec4f004e58930277758f5412d86c62adc.tar.bz2 ampache-a31560aec4f004e58930277758f5412d86c62adc.zip |
it technically logs in and streams.. but thats it, complete rewrite almost everything broken
Diffstat (limited to 'lib')
-rw-r--r-- | lib/album.lib.php | 18 | ||||
-rw-r--r-- | lib/class/access.class.php | 14 | ||||
-rw-r--r-- | lib/class/album.class.php | 75 | ||||
-rw-r--r-- | lib/class/artist.class.php | 42 | ||||
-rw-r--r-- | lib/class/catalog.class.php | 227 | ||||
-rw-r--r-- | lib/class/dba.class.php | 50 | ||||
-rw-r--r-- | lib/class/error.class.php | 142 | ||||
-rw-r--r-- | lib/class/flag.class.php | 16 | ||||
-rw-r--r-- | lib/class/song.class.php | 90 | ||||
-rw-r--r-- | lib/class/stats.class.php | 49 | ||||
-rw-r--r-- | lib/class/stream.class.php | 27 | ||||
-rw-r--r-- | lib/class/update.class.php | 42 | ||||
-rw-r--r-- | lib/class/user.class.php | 90 | ||||
-rwxr-xr-x | lib/class/vainfo.class.php | 516 | ||||
-rw-r--r-- | lib/debug.lib.php | 196 | ||||
-rw-r--r-- | lib/flag.php | 342 | ||||
-rw-r--r-- | lib/general.lib.php | 236 | ||||
-rw-r--r-- | lib/gettext.php | 10 | ||||
-rw-r--r-- | lib/init.php | 208 | ||||
-rw-r--r-- | lib/log.lib.php | 4 | ||||
-rw-r--r-- | lib/preferences.php | 66 | ||||
-rw-r--r-- | lib/song.php | 16 | ||||
-rw-r--r-- | lib/stream.lib.php | 8 | ||||
-rw-r--r-- | lib/themes.php | 10 | ||||
-rw-r--r-- | lib/ui.lib.php | 155 |
25 files changed, 1285 insertions, 1364 deletions
diff --git a/lib/album.lib.php b/lib/album.lib.php index 29c9e90a..410b189b 100644 --- a/lib/album.lib.php +++ b/lib/album.lib.php @@ -68,15 +68,21 @@ function get_random_albums($count='') { if (!$count) { $count = 5; } - $count = sql_escape($count); + $count = Dba::escape($count); - $sql = "SELECT id FROM album WHERE art IS NOT NULL ORDER BY RAND() LIMIT $count"; - $db_results = mysql_query($sql,dbh()); + // We avoid a table scan by using the id index and then using a rand to pick a row # + $sql = "SELECT `id` FROM `album` WHERE `art` IS NOT NULL"; + $db_results = Dba::query($sql); - $results = array(); + while ($r = Dba::fetch_assoc($db_results)) { + $albums[] = $r['id']; + } + + $total = count($albums); - while ($r = mysql_fetch_assoc($db_results)) { - $results[] = $r['id']; + for ($i=0; $i <= $count; $i++) { + $record = rand(0,$total); + $results[] = $albums[$record]; } return $results; 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&album=" . $this->id . "\" title=\"" . scrub_out($this->name) . "\">" . $name . "</a>"; $this->f_link = "<a href=\"$web_path/albums.php?action=show&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&artist=" . $this->id . "\" title=\"" . $this->full_name . "\">" . $name . "</a>"; + $this->f_link = "<a href=\"" . Config::get('web_path') . "/artists.php?action=show&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&song_id=" . $this->id . "\">$this->f_title</a>"; - $this->f_album_link = "<a href=\"" . conf('web_path') . "/albums.php?action=show&album=" . $this->album . "\">$this->f_album</a>"; - $this->f_artist_link = "<a href=\"" . conf('web_path') . "/artists.php?action=show&artist=" . $this->artist . "\">$this->f_artist</a>"; + $this->f_link = "<a href=\"" . Config::get('web_path') . "/stream.php?action=single_song&song_id=" . $this->id . "\">$this->f_title</a>"; + $this->f_album_link = "<a href=\"" . Config::get('web_path') . "/albums.php?action=show&album=" . $this->album . "\">$this->f_album</a>"; + $this->f_artist_link = "<a href=\"" . Config::get('web_path') . "/artists.php?action=show&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 +?> diff --git a/lib/debug.lib.php b/lib/debug.lib.php index ca0d55cb..10ad838f 100644 --- a/lib/debug.lib.php +++ b/lib/debug.lib.php @@ -21,52 +21,29 @@ */ -/* - @header Debug Library - This library is loaded when somehow our mojo has - been lost, it contains functions for checking sql - connections, web paths etc.. +/** + * Debug Library + * This library is loaded when somehow our mojo has + * been lost, it contains functions for checking sql + * connections, web paths etc.. */ /*! - @function read_config_file - @discussion checks to see if the config - file is readable, overkill I know.. - @param level 0 is readable, 1 detailed info -*/ - - -function read_config_file($file,$level=0) { - - $fp = @fopen($file, 'r'); - - if (!$level) { - return is_resource($fp); - } - - -} // read_config_file - -/*! @function check_database @discussion checks the local mysql db and make sure life is good */ -function check_database($host,$username,$pass,$level=0) { +function check_database($host,$username,$pass,$database) { $dbh = @mysql_connect($host, $username, $pass); if (!is_resource($dbh)) { - $error['error_state'] = true; - $error['mysql_error'] = mysql_errno() . ": " . mysql_error() . "\n"; + return false; } if (!$host || !$username || !$pass) { - $error['error_state'] = true; - $error['mysql_error'] .= "<br />HOST:$host<br />User:$username<br />Pass:$pass<br />"; + return false; } - if ($error['error_state']) { return false; } - return $dbh; } // check_database @@ -101,13 +78,10 @@ function check_database_inserted($dbh,$db_name) { */ function check_php_ver($level=0) { - if (strcmp('4.1.2',phpversion()) > 0) { - $error['error_state'] = true; - $error['php_ver'] = phpversion(); + if (strcmp('5.0.0',phpversion()) > 0) { + return false; } - if ($error['error_state']) { return false; } - return true; } // check_php_ver @@ -119,12 +93,9 @@ function check_php_ver($level=0) { function check_php_mysql() { if (!function_exists('mysql_query')) { - $error['error_state'] = true; - $error['php_mysql'] = false; + return false; } - if ($error['error_state']) { return false; } - return true; } // check_php_mysql @@ -137,12 +108,9 @@ function check_php_mysql() { function check_php_session() { if (!function_exists('session_set_save_handler')) { - $error['error_state'] = true; - $error['php_session'] = false; + return false; } - if ($error['error_state']) { return false; } - return true; } // check_php_session @@ -154,12 +122,9 @@ function check_php_session() { function check_php_iconv() { if (!function_exists('iconv')) { - $error['error_state'] = true; - $error['php_iconv'] = false; + return false; } - if ($error['error_state']) { return false; } - return true; } // check_php_iconv @@ -172,12 +137,9 @@ function check_php_iconv() { function check_php_pcre() { if (!function_exists('preg_match')) { - $error['error_state'] = true; - $error['php_pcre'] = false; + return false; } - if ($error['error_state']) { return false; } - return true; } // check_php_pcre @@ -188,34 +150,33 @@ function check_php_pcre() { least set the needed variables */ function check_config_values($conf) { - $error = new Error(); - if (!$conf['local_host']) { + + if (!$conf['database_hostname']) { return false; } - if (!$conf['local_db']) { + if (!$conf['database_name']) { return false; } - if (!$conf['local_username']) { + if (!$conf['database_username']) { return false; } - if (!$conf['local_pass']) { + if (!$conf['database_password']) { return false; } - if (!$conf['local_length']) { + if (!$conf['session_length']) { return false; } - if (!$conf['sess_name']) { + if (!$conf['session_name']) { return false; } - if (!isset($conf['sess_cookielife'])) { + if (!isset($conf['session_cookielife'])) { return false; } - if (!isset($conf['sess_cookiesecure'])) { + if (!isset($conf['session_cookiesecure'])) { return false; } if (isset($conf['debug'])) { if (!isset($conf['log_path'])) { - $error->add_error('log_path',_("You defined the option \"debug = on\" but didn't define a log path for the log to be stored")); return false; } } @@ -224,117 +185,6 @@ function check_config_values($conf) { } // check_config_values -/*! - @function debug_read_config - @discussion this is the same as the read config function - except it will pull config values with a # before them - (basicly adding a #config="value" check) and not - ever dieing on a config file error -*/ -function debug_read_config($config_file,$debug) { - - $fp = @fopen($config_file,'r'); - if(!is_resource($fp)) return false; - $file_data = fread($fp,filesize($config_file)); - fclose($fp); - - // explode the var by \n's - $data = explode("\n",$file_data); - if($debug) echo "<pre>"; - $count = 0; - - if (!count($data)) { - debug_event('debug_read_config','Error Unable to Read config file','1'); - return false; - } - - $results = array(); - - foreach($data as $value) { - $count++; - - $value = trim($value); - - if (preg_match("/^#?([\w\d]+)\s+=\s+[\"]{1}(.*?)[\"]{1}$/",$value,$matches) - || preg_match("/^#?([\w\d]+)\s+=\s+[\']{1}(.*?)[\']{1}$/", $value, $matches) - || preg_match("/^#?([\w\d]+)\s+=\s+[\'\"]{0}(.*)[\'\"]{0}$/",$value,$matches)) { - - - if (is_array($results[$matches[1]]) && isset($matches[2]) ) { - if($debug) echo "Adding value <strong>$matches[2]</strong> to existing key <strong>$matches[1]</strong>\n"; - array_push($results[$matches[1]], $matches[2]); - } - - elseif (isset($results[$matches[1]]) && isset($matches[2]) ) { - if($debug) echo "Adding value <strong>$matches[2]</strong> to existing key $matches[1]</strong>\n"; - $results[$matches[1]] = array($results[$matches[1]],$matches[2]); - } - - elseif ($matches[2] !== "") { - if($debug) echo "Adding value <strong>$matches[2]</strong> for key <strong>$matches[1]</strong>\n"; - $results[$matches[1]] = $matches[2]; - } - - // if there is something there and it's not a comment - elseif ($value{0} !== "#" AND strlen(trim($value)) > 0 AND !$test AND strlen($matches[2]) > 0) { - echo "Error Invalid Config Entry --> Line:$count"; return false; - } // elseif it's not a comment and there is something there - - else { - if($debug) echo "Key <strong>$matches[1]</strong> defined, but no value set\n"; - } - - } // end else - - } // foreach - - if (isset($config_name) && isset(${$config_name}) && count(${$config_name})) { - $results[$config_name] = ${$config_name}; - } - - if($debug) echo "</pre>"; - - return $results; - -} // debug_read_config - -/*! - @function debug_compare_configs - @discussion this takes two config files, and then compares - the results and returns an array of the values - that are missing from the first one passed -*/ -function debug_compare_configs($config,$dist_config) { - - - - /* Get the results from the two difference configs including #'d values */ - $results = debug_read_config($config,0); - $dist_results = debug_read_config($dist_config,0); - - $missing = array(); - - foreach ($dist_results as $key=>$value) { - - if (!isset($results[$key])) { - /* If it's an array we need to split it out */ - if (is_array($value)) { - foreach ($value as $element) { - $missing[$key][] = $element; - } - } - else { - $missing[$key] = $value; - } // end else not array - } // if it's not set - - } // end foreach conf - - return $missing; - -} // debug_compare_configs - - /** * check_putenv * This checks to see if we can manually set the diff --git a/lib/flag.php b/lib/flag.php deleted file mode 100644 index 8f3d0dff..00000000 --- a/lib/flag.php +++ /dev/null @@ -1,342 +0,0 @@ -<?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. - -*/ - -// -// - -function add_to_edit_queue($flags=0) -{ - $oldflags = 0; - if(empty($flags)) $flags = 0; - if($_SESSION['edit_queue']) - { - $oldflags = $_SESSION['edit_queue']; - if(!is_array($oldflags)) $oldflags = array($oldflags); - } - - if(!is_array($flags)) - { - if($flags !== 0) $flags = array($flags); - } - - if(is_array($flags)) - { - if(is_array($oldflags)) $new_array = array_merge($flags, $oldflags); - else $new_array = $flags; - } - elseif (is_array($oldflags)) $new_array = $oldflags; - - if(count($new_array)) - { - $_SESSION['edit_queue'] = $new_array; - return count($new_array); - } - else - { - unset($_SESSION['edit_queue']); - return 0; - } -} - -function show_edit_flagged($flag=0) -{ - if(empty($flag)||$flag === 0) - { - $flag = array_pop($_SESSION['edit_queue']); - } - $flaginfo = get_flag($flag); - if($flaginfo['type'] === 'badid3') - { - show_edit_badid3($flaginfo['song'],$flag); - } - else - { - echo "I don't know how to edit already edited songs yet: $flag.<br />"; - } -} - -function show_edit_badid3($songid,$flagid) -{ - $song = get_song_info($songid); - require(conf('prefix')."/templates/song_edit.inc"); -} - -function get_flag($id) -{ - if(!is_array($id)) $id = array($id); - $results = array(); - $newid = array_pop($id); - $sql = "SELECT flagged.id,user.username,type,song,date,comment" . - " FROM flagged,user WHERE flagged.user = user.username AND (flagged.song = '$newid'"; - foreach($id as $num) - { - $sql .= " OR flagged.song = '$num'"; - } - $sql .= ")"; - $result = mysql_query($sql, dbh()); - while ($row = mysql_fetch_array($result)) - { - $results[] = $row; - } - if(count($results) == 1) return $results[0]; - else return $results; -} - - -function get_flagged_songs($user = 0) -{ - $sql = "SELECT flagged.id,user.username,type,song,date,comment" . - " FROM flagged,user WHERE flagged.user = user.username AND flagged.type <> 'notify' AND flagged.type <> 'done'"; - - // If the user is not an admin, they can only see songs they've flagged - if($user) - { - if($_SESSION['userdata']['access'] === 'admin') - { - $sql .= " AND user.username = '$user'"; - } - else - { - $sql .= " AND user.username = '".$_SESSION['userdata']['username']."'"; - } - } - - $sql .= " ORDER BY date"; - $result = mysql_query($sql, dbh()); - - $arr = array(); - - while ($flag = mysql_fetch_array($result)) - { - $arr[] = $flag; - } - return $arr; -} - -function accept_new_tags($flags) -{ - if(!is_array($flags)) $flags = array($flags); - foreach($flags as $flag) - { - copy_updated_tag($flag); - } - set_flag_value($flags, 'setid3'); -} - - -function reject_new_tags($flags) -{ - if(!is_array($flags)) $flags = array($flags); - $oldflags = $flags; - $flag = array_pop($flags); - $sql = "DELETE FROM flagged_songs WHERE song = '$flag'"; - - foreach($flags as $flag) - { - $sql .= " OR song = '$flag'"; - } - $result = mysql_query($sql, dbh()); - $user = $_SESSION['userdata']['username']; - set_flag_value($oldflags, 'notify', "Tag changes rejected by $user"); -} - -function set_flag_value($flags, $val, $comment = '') -{ - if(!is_array($flags)) $flags = array($flags); - $user = $_SESSION['userdata']['id']; -/* $flagid = array_pop($flags);*/ - $dbh = dbh(); - foreach($flags as $flagid) - { - $sql = "REPLACE INTO flagged (type,song,comment,user,date)". - " VALUES ('$val','$flagid','$comment','$user','".time()."')"; - $result = mysql_query($sql, $dbh); - } - return $result; -} - -function copy_updated_tag($flag) -{ - $flagdata = get_flag($flag); - $sql = "SELECT * FROM flagged_song WHERE song = '".$flagdata['song']."'"; - $result = mysql_query($sql, dbh()); - $newtag = mysql_fetch_array($result); - - if($newtag['new_artist']) - { - $newtag['artist'] = insert_artist($newtag['new_artist']); - } - if($newtag['new_album']) - { - $newtag['album'] = insert_album($newtag['new_album']); - } - - $sql = "UPDATE song SET ". - "title = '".$newtag['title']."',". - "artist = '".$newtag['artist']."',". - "album = '".$newtag['album']."',". - "track = '".$newtag['track']."',". - "genre = '".$newtag['genre']."',". - "year = '".$newtag['year']."' ". - "WHERE song.id = '".$newtag['song']."'"; - $result = mysql_query($sql, dbh()); - if($result) - { - $sql2 = "DELETE FROM flagged_song WHERE song='".$flagdata['song']."'"; - $result2 = mysql_query($sql2, dbh()); - } - return ($result && $result2); - -} - -function update_flags($songs) -{ - $accepted = array(); - $rejected = array(); - $newflags = array(); - foreach($songs as $song) - { - $accept = scrub_in($_REQUEST[$song.'_accept']); - if($accept === 'accept') $accepted[] = $song; - elseif ($accept === 'reject') $rejected[] = $song; - else - { - $newflag = scrub_in($_REQUEST[$song.'_newflag']); - $newflags[$song] = $newflag; - } - } - - if(count($accepted)) - { - accept_new_tags($accepted); - } - if(count($rejected)) - { - reject_new_tags($rejected); - } - if(count($newflags)) - { - foreach($newflags as $flag=>$type) - { - set_flag_value($flag, $type); - } - } - -} - - -function update_song_info($song) -{ - $user = $_SESSION['userdata']; - - $title = scrub_in($_REQUEST['title']); - $track = scrub_in($_REQUEST['track']); - $genre = scrub_in($_REQUEST['genre']); - $year = scrub_in($_REQUEST['year']); - - if(isset($_REQUEST['update_id3'])) - $update_id3 = 1; - - if(isset($_REQUEST['new_artist']) && $_REQUEST['new_artist'] !== '') - { - $create_artist = 1; - $artist = scrub_in($_REQUEST['new_artist']); - } - else - $artist = scrub_in($_REQUEST['artist']); - - if(isset($_REQUEST['new_album']) && $_REQUEST['new_album'] !== '') - { - $create_album = 1; - $album = scrub_in($_REQUEST['new_album']); - } - else - $album = scrub_in($_REQUEST['album']); - - if(is_array($_REQUEST['genre'])) { - $genre = $genre[0]; - } - - if($user['access'] == 'admin') - // Update the file directly - { - if($create_artist) - { - $artist = insert_artist($artist); - } - if($create_album) - { - $album = insert_album($album); - } - // Escape data (prevent " or ' snafu's) - $title = sql_escape($title); - $artist = sql_escape($artist); - $album = sql_escape($album); - $genre = sql_escape($genre); - $year = sql_escape($year); - - $sql = "UPDATE song SET" . - " title = '$title'," . - " track = '$track'," . - " genre = '$genre'," . - " year = '$year'," . - " artist = '$artist',". - " album = '$album'," . - " update_time = '".time()."'" . - " WHERE id = '$song' LIMIT 1"; - $result = mysql_query($sql, dbh() ); - if($result && $update_id3 ) - { - //Add to flagged table so we can fix the id3 tags - $date = time(); - $sql = "REPLACE INTO flagged SET " . - " type = 'setid3', song = '$song', date = '$date', user = '".$user['id']."'"; - $result = mysql_query($sql, dbh()); - } - } - - else - // Stick in the flagged_songs table to be updated by an admin - { - if($create_artist) $artist_field = 'new_artist'; - else $artist_field = 'artist'; - - if($create_album) $album_field = 'new_album'; - else $album_field = 'album'; - - $sql = "INSERT INTO flagged_song(song,title,track,genre,year,$artist_field,$album_field,update_time) " . - "VALUES ('$song','$title','$track','$genre','$year','$artist','$album','".time()."')"; - $result = mysql_query($sql, dbh() ); - - if($result && $update_id3 ) - { - //Add to flagged table so we can fix the id3 tags - $date = time(); - $sql = "REPLACE INTO flagged SET " . - " type = 'newid3', song = '$song', date = '$date', user = '".$user['id']."'"; - $result = mysql_query($sql, dbh()); - } - echo "Thanks for helping to keep the catalog up to date. Someone will review your changes, and you will be notified on the main page when they're approved."; - - } -} - diff --git a/lib/general.lib.php b/lib/general.lib.php index cb4a1dea..bff578a1 100644 --- a/lib/general.lib.php +++ b/lib/general.lib.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 @@ -28,29 +28,6 @@ */ /*! - @function sql_escape - @discussion this takes a sql statement - and properly escapes it before a query is run - against it. -*/ -function sql_escape($sql,$dbh=0) { - - if (!is_resource($dbh)) { - $dbh = dbh(); - } - - if (function_exists('mysql_real_escape_string')) { - $sql = mysql_real_escape_string($sql,$dbh); - } - else { - $sql = mysql_escape_string($sql); - } - - return $sql; - -} // sql_escape - -/*! @function ip2int @discussion turns a dotted quad ip into an int @@ -74,89 +51,6 @@ function int2ip($i) { return "$d[0].$d[1].$d[2].$d[3]"; } // int2ip -/*! - @function show_template - @discussion show a template from the /templates directory, automaticly appends .inc - to the passed filename - @param $template Name of Template -*/ -function show_template($template) { - global $user; - - /* Check for a 'Theme' template */ - if (is_readable(conf('prefix') . conf('theme_path') . "/templates/$template".".inc")) { - require (conf('prefix') . conf('theme_path') . "/templates/$template".".inc"); - } - else { - require (conf('prefix') . "/templates/$template".".inc"); - } - -} // show_template - - -/*! - @function read_config - @discussion reads the config file for ampache -*/ -function read_config($config_file, $debug=0, $test=0) { - - $fp = @fopen($config_file,'r'); - if(!is_resource($fp)) return false; - $file_data = fread($fp,filesize($config_file)); - fclose($fp); - - // explode the var by \n's - $data = explode("\n",$file_data); - if($debug) echo "<pre>"; - - $count = 0; - - foreach($data as $value) { - $count++; - - $value = trim($value); - - if (substr($value,0,1) == '#') { continue; } - - if (preg_match("/^([\w\d]+)\s+=\s+[\"]{1}(.*?)[\"]{1}$/",$value,$matches) - || preg_match("/^([\w\d]+)\s+=\s+[\']{1}(.*?)[\']{1}$/", $value, $matches) - || preg_match("/^([\w\d]+)\s+=\s+[\'\"]{0}(.*)[\'\"]{0}$/",$value,$matches)) { - - - if (is_array($results[$matches[1]]) && isset($matches[2]) ) { - if($debug) echo "Adding value <strong>$matches[2]</strong> to existing key <strong>$matches[1]</strong>\n"; - array_push($results[$matches[1]], $matches[2]); - } - - elseif (isset($results[$matches[1]]) && isset($matches[2]) ) { - if($debug) echo "Adding value <strong>$matches[2]</strong> to existing key $matches[1]</strong>\n"; - $results[$matches[1]] = array($results[$matches[1]],$matches[2]); - } - - elseif ($matches[2] !== "") { - if($debug) echo "Adding value <strong>$matches[2]</strong> for key <strong>$matches[1]</strong>\n"; - $results[$matches[1]] = $matches[2]; - } - - // if there is something there and it's not a comment - elseif ($value{0} !== "#" AND strlen(trim($value)) > 0 AND !$test AND strlen($matches[2]) > 0) { - echo "Error Invalid Config Entry --> Line:$count"; return false; - } // elseif it's not a comment and there is something there - - else { - if($debug) echo "Key <strong>$matches[1]</strong> defined, but no value set\n"; - } - - } // end else - - } // foreach - - if ($debug) { echo "</pre>\n"; } - - return $results; - -} // read_config - /* * Conf function by Robert Hopson * call it with a $parm name to retrieve @@ -164,7 +58,7 @@ function read_config($config_file, $debug=0, $test=0) { * to reset a var pass the array plus * Clobber! replaces global $conf; */ -function conf($param,$clobber=0) +/*function conf($param,$clobber=0) { static $params = array(); @@ -215,37 +109,7 @@ function error_results($param,$clobber=0) else return; } } //error_results - - -/** - * dbh - * Alias for the vauth_dbh function - */ -function dbh() { - - return vauth_dbh(); - -} // dbh - -/*! - @function fix_preferences - @discussion cleans up the preferences */ -function fix_preferences($results) { - - foreach ($results as $key=>$data) { - if (strcasecmp($data, "yes") == "0") { $data = 1; } - if (strcasecmp($data,"true") == "0") { $data = 1; } - if (strcasecmp($data,"enabled") == "0") { $data = 1; } - if (strcasecmp($data,"disabled") == "0") { $data = 0; } - if (strcasecmp($data,"false") == "0") { $data = 0; } - if (strcasecmp($data,"no") == "0") { $data = 0; } - $results[$key] = $data; - } - - return $results; - -} // fix_preferences /** * session_exists @@ -259,10 +123,10 @@ function session_exists($sid,$xml_rpc=0) { $found = true; - $sql = "SELECT * FROM session WHERE id = '$sid'"; - $db_results = mysql_query($sql, dbh()); + $sql = "SELECT * FROM `session` WHERE `id` = '$sid'"; + $db_results = Dba::query($sql); - if (!mysql_num_rows($db_results)) { + if (!Dba::num_rows($db_results)) { $found = false; } @@ -306,12 +170,12 @@ function session_exists($sid,$xml_rpc=0) { */ function extend_session($sid) { - $new_time = time() + conf('local_length'); + $new_time = time() + Config::get('local_length'); if ($_COOKIE['amp_longsess'] == '1') { $new_time = time() + 86400*364; } - $sql = "UPDATE session SET expire='$new_time' WHERE id='$sid'"; - $db_results = mysql_query($sql, dbh()); + $sql = "UPDATE `session` SET `expire`='$new_time' WHERE `id`='$sid'"; + $db_results = Dba::query($sql); } // extend_session @@ -601,8 +465,8 @@ function cleanup_and_exit($playing_id) { function get_global_popular($type) { $stats = new Stats(); - $count = conf('popular_threshold'); - $web_path = conf('web_path'); + $count = Config::get('popular_threshold'); + $web_path = Config::get('web_path'); /* Pull the top */ $results = $stats->get_top($count,$type); @@ -653,43 +517,6 @@ function get_global_popular($type) { } // get_global_popular /** - * gen_newest - * Get a list of newest $type (which can then be handed to show_info_box - * @package Web Interface - * @catagory Get - * @todo Add Genre - */ -function get_newest ($type = 'artist',$limit='') { - - $dbh = dbh(); - - if (!$limit) { $limit = conf('popular_threshold'); } - - $sql = "SELECT DISTINCT $type FROM song ORDER BY addition_time " . - "DESC LIMIT " . conf('popular_threshold'); - $db_result = mysql_query($sql, $dbh); - - $items = array(); - - while ($r = mysql_fetch_array($db_result)) { - if ( $type == 'artist' ) { - $artist = new Artist($r[0]); - $artist->format_artist(); - $items[] = $artist; - - } - elseif ( $type == 'album' ) { - $album = new Album($r[0]); - $album->format(); - $album->link = $album->f_link; - $items[] = $album; - } - } - - return $items; -} // get_newest - -/** * show_info_box * This shows the basic box that popular and newest stuff goes into * @package Web Interface @@ -698,9 +525,9 @@ function get_newest ($type = 'artist',$limit='') { */ function show_info_box ($title, $type, $items) { - $web_path = conf('web_path'); - $popular_threshold = conf('popular_threshold'); - require (conf('prefix') . '/templates/show_box.inc.php'); + $web_path = Config::get('web_path'); + $popular_threshold = Config::get('popular_threshold'); + require Config::get('prefix') . '/templates/show_box.inc.php'; } // show_info_box @@ -784,7 +611,7 @@ function scrub_out($str) { $str = stripslashes($str); } - $str = htmlentities($str,ENT_QUOTES,conf('site_charset')); + $str = htmlentities($str,ENT_QUOTES,Config::get('site_charset')); return $str; @@ -890,7 +717,7 @@ function logout() { vauth_logout(session_id()); /* Redirect them to the login page */ - header ('Location: ' . conf('web_path') . '/login.php'); + header ('Location: ' . Config::get('web_path') . '/login.php'); return true; @@ -976,10 +803,12 @@ function invert_boolean($value) { */ function get_user_from_username($username) { - $sql = "SELECT `id` FROM `user` WHERE `username`='" . sql_escape($username) . "'"; - $db_results = mysql_query($sql, dbh()); + $username = Dba::escape($username); - $results = mysql_fetch_assoc($db_results); + $sql = "SELECT `id` FROM `user` WHERE `username`='$username'"; + $db_results = Dba::query($sql); + + $results = Dba::fetch_assoc($db_results); $user = new User($results['id']); @@ -1037,5 +866,30 @@ function unhtmlentities ($string) { } // unhtmlentities +/** + * __autoload + * This function automatically loads any missing + * classes as they are called so that we don't have to have + * a million include statements, and load more then we need + */ +function __autoload($class) { + // Lowercase the class + $class = strtolower($class); + + $file = Config::get('prefix') . "/lib/class/$class.class.php"; + + // See if it exists + if (is_readable($file)) { + require_once $file; + if (is_callable($class . '::_auto_init')) { + call_user_func(array($class, '_auto_init')); + } + } + // Else log this as a fatal error + else { + debug_event('__autoload', "'$class' not found!",'1'); + } + +} // __autoload ?> diff --git a/lib/gettext.php b/lib/gettext.php index 4af41a59..ed929241 100644 --- a/lib/gettext.php +++ b/lib/gettext.php @@ -27,22 +27,22 @@ function load_gettext() { /* If we have gettext */ if (function_exists('bindtextdomain')) { - $lang = conf('lang'); + $lang = Config::get('lang'); putenv("LANG=" . $lang); putenv("LANGUAGE=" . $lang); /* Try lang, lang + charset and lang + utf-8 */ setlocale(LC_ALL, $lang, - $lang . '.'. conf('site_charset'), + $lang . '.'. Config::get('site_charset'), $lang . '.UTF-8', $lang . '.utf-8', - $lang . '.' . conf('lc_charset')); + $lang . '.' . Config::get('lc_charset')); /* Bind the Text Domain */ - bindtextdomain('messages', conf('prefix') . "/locale/"); + bindtextdomain('messages', Config::get('prefix') . "/locale/"); textdomain('messages'); if (function_exists('bind_textdomain_codeset')) { - bind_textdomain_codeset('messages',conf('site_charset')); + bind_textdomain_codeset('messages',Config::get('site_charset')); } // if we can codeset the textdomain } // If bindtext domain exists diff --git a/lib/init.php b/lib/init.php index 01d0c6c4..477b525e 100644 --- a/lib/init.php +++ b/lib/init.php @@ -31,7 +31,8 @@ error_reporting(E_ALL ^ E_NOTICE); $ampache_path = dirname(__FILE__); $prefix = realpath($ampache_path . "/../"); $configfile = "$prefix/config/ampache.cfg.php"; -require_once($prefix . "/lib/general.lib.php"); +require_once $prefix . '/lib/general.lib.php'; +require_once $prefix . '/lib/class/config.class.php'; /* Check to see if this is Http or https @@ -55,11 +56,10 @@ if (!file_exists($configfile)) { exit(); } -/* - Try to read the config file, if it fails give them - an explanation -*/ -if (!$results = read_config($configfile,0)) { +// Use the built in PHP function, supress errors here so we can handle it properly +$results = @parse_ini_file($configfile); + +if (!count($results)) { $path = preg_replace("/(.*)\/(\w+\.php)$/","\${1}", $_SERVER['PHP_SELF']); $link = $http_type . $_SERVER['HTTP_HOST'] . $path . "/test.php"; header ("Location: $link"); @@ -67,7 +67,7 @@ if (!$results = read_config($configfile,0)) { } /** This is the version.... fluf nothing more... **/ -$results['version'] = '3.4-Alpha1 (Build 001)'; +$results['version'] = '3.4-Alpha1 (Build 002)'; $results['int_config_version'] = '2'; $results['raw_web_path'] = $results['web_path']; @@ -87,96 +87,67 @@ if (!$results['raw_web_path']) { if (!$_SERVER['SERVER_NAME']) { $_SERVER['SERVER_NAME'] = ''; } -if (!isset($results['auth_methods'])) { - $results['auth_methods'] = array('mysql'); -} -if (!is_array($results['auth_methods'])) { - $results['auth_methods'] = array($results['auth_methods']); -} if (!$results['user_ip_cardinality']) { $results['user_ip_cardinality'] = 42; } if (!$results['local_length']) { - $results['local_length'] = '9000'; + $results['local_length'] = '900'; } /* Variables needed for vauth Module */ $results['cookie_path'] = $results['raw_web_path']; $results['cookie_domain'] = $_SERVER['SERVER_NAME']; -$results['cookie_life'] = $results['sess_cookielife']; -$results['session_name'] = $results['sess_name']; -$results['cookie_secure'] = $results['sess_cookiesecure']; -$results['session_length'] = $results['local_length']; -$results['mysql_password'] = $results['local_pass']; -$results['mysql_username'] = $results['local_username']; -$results['mysql_hostname'] = $results['local_host']; -$results['mysql_db'] = $results['local_db']; +$results['cookie_life'] = $results['session_cookielife']; +$results['cookie_secure'] = $results['session_cookiesecure']; +$results['mysql_password'] = $results['database_password']; +$results['mysql_username'] = $results['database_username']; +$results['mysql_hostname'] = $results['database_hostname']; +$results['mysql_db'] = $results['database_name']; + +// Define that we've loaded the INIT file +define('INIT_LOADED','1'); + +// Vauth Requires +require_once $prefix . '/modules/vauth/init.php'; + +// Library and module includes we can't do with the autoloader +require_once $prefix . '/lib/album.lib.php'; +require_once $prefix . '/lib/artist.lib.php'; +require_once $prefix . '/lib/song.php'; +require_once $prefix . '/lib/search.php'; +require_once $prefix . '/lib/preferences.php'; +require_once $prefix . '/lib/rss.php'; +require_once $prefix . '/lib/log.lib.php'; +require_once $prefix . '/lib/localplay.lib.php'; +require_once $prefix . '/lib/ui.lib.php'; +require_once $prefix . '/lib/gettext.php'; +require_once $prefix . '/lib/batch.lib.php'; +require_once $prefix . '/lib/themes.php'; +require_once $prefix . '/lib/stream.lib.php'; +require_once $prefix . '/lib/playlist.lib.php'; +require_once $prefix . '/lib/democratic.lib.php'; +require_once $prefix . '/lib/xmlrpc.php'; +require_once $prefix . '/modules/xmlrpc/xmlrpc.inc'; +require_once $prefix . '/modules/catalog.php'; +require_once $prefix . '/modules/getid3/getid3.php'; +require_once $prefix . '/modules/infotools/Snoopy.class.php'; +require_once $prefix . '/modules/infotools/AmazonSearchEngine.class.php'; +//require_once $prefix . '/modules/infotools/jamendoSearch.class.php'; /* Temp Fixes */ $results = fix_preferences($results); // Setup Static Arrays -conf($results); - -// Vauth Requires -require_once(conf('prefix') . '/modules/vauth/init.php'); - -// Librarys -require_once(conf('prefix') . '/lib/album.lib.php'); -require_once(conf('prefix') . '/lib/artist.lib.php'); -require_once(conf('prefix') . '/lib/song.php'); -require_once(conf('prefix') . '/lib/search.php'); -require_once(conf('prefix') . '/lib/preferences.php'); -require_once(conf('prefix') . '/lib/rss.php'); -require_once(conf('prefix') . '/lib/log.lib.php'); -require_once(conf('prefix') . '/lib/localplay.lib.php'); -require_once(conf('prefix') . '/lib/ui.lib.php'); -require_once(conf('prefix') . '/lib/gettext.php'); -require_once(conf('prefix') . '/lib/batch.lib.php'); -require_once(conf('prefix') . '/lib/themes.php'); -require_once(conf('prefix') . '/lib/stream.lib.php'); -require_once(conf('prefix') . '/lib/playlist.lib.php'); -require_once(conf('prefix') . '/lib/democratic.lib.php'); -require_once(conf('prefix') . '/modules/catalog.php'); -require_once(conf('prefix') . "/modules/id3/getid3/getid3.php"); -require_once(conf('prefix') . '/modules/id3/vainfo.class.php'); -require_once(conf('prefix') . '/modules/infotools/Snoopy.class.php'); -require_once(conf('prefix') . '/modules/infotools/AmazonSearchEngine.class.php'); -//require_once(conf('prefix') . '/modules/infotools/jamendoSearch.class.php'); -require_once(conf('prefix') . '/lib/xmlrpc.php'); -require_once(conf('prefix') . '/modules/xmlrpc/xmlrpc.inc'); +Config::set_by_array($results,1); // Modules (These are conditionaly included depending upon config values) -if (conf('ratings')) { - require_once(conf('prefix') . '/lib/class/rating.class.php'); - require_once(conf('prefix') . '/lib/rating.lib.php'); +if (Config::get('ratings')) { + require_once $prefix . '/lib/class/rating.class.php'; + require_once $prefix . '/lib/rating.lib.php'; } - -// Classes -require_once(conf('prefix') . '/lib/class/localplay.class.php'); -require_once(conf('prefix') . '/lib/class/plugin.class.php'); -require_once(conf('prefix') . '/lib/class/stats.class.php'); -require_once(conf('prefix') . '/lib/class/catalog.class.php'); -require_once(conf('prefix') . '/lib/class/stream.class.php'); -require_once(conf('prefix') . '/lib/class/playlist.class.php'); -require_once(conf('prefix') . '/lib/class/tmp_playlist.class.php'); -require_once(conf('prefix') . '/lib/class/song.class.php'); -require_once(conf('prefix') . '/lib/class/view.class.php'); -require_once(conf('prefix') . '/lib/class/update.class.php'); -require_once(conf('prefix') . '/lib/class/user.class.php'); -require_once(conf('prefix') . '/lib/class/album.class.php'); -require_once(conf('prefix') . '/lib/class/artist.class.php'); -require_once(conf('prefix') . '/lib/class/access.class.php'); -require_once(conf('prefix') . '/lib/class/error.class.php'); -require_once(conf('prefix') . '/lib/class/genre.class.php'); -require_once(conf('prefix') . '/lib/class/flag.class.php'); -require_once(conf('prefix') . '/lib/class/audioscrobbler.class.php'); - - /* Set a new Error Handler */ -$old_error_handler = set_error_handler("ampache_error_handler"); - +$old_error_handler = set_error_handler('ampache_error_handler'); /* Initilize the Vauth Library */ vauth_init($results); @@ -192,35 +163,8 @@ if ($results['memory_limit'] < 24) { } set_memory_limit($results['memory_limit']); -// Check Session GC mojo, increase if need be -$gc_probability = @ini_get('session.gc_probability'); -$gc_divisor = @ini_get('session.gc_divisor'); - -if (!$gc_divisor) { - $gc_divisor = '100'; -} -if (!$gc_probability) { - $gc_probability = '1'; -} - -// Force GC on 1:5 page loads -if (($gc_divisor / $gc_probability) > 5) { - $new_gc_probability = $gc_divisor * .2; - ini_set('session.gc_probability',$new_gc_probability); -} - -/* Seed the random number */ -srand((double) microtime() * 1000003); - /**** END Set PHP Vars ****/ -/* Check to see if they've tried to set no_session via get/post */ -if (isset($_POST['no_session']) || isset($_GET['no_session'])) { - /* just incase of register globals */ - unset($no_session); - debug_event('no_session','No Session passed as get/post','1'); -} - /* We have to check for HTTP Auth */ if (in_array("http",$results['auth_methods'])) { @@ -237,24 +181,22 @@ if (in_array("http",$results['auth_methods'])) { } // end if http auth -// If we don't want a session -if (NO_SESSION != '1' AND conf('use_auth')) { +// If we want a session +if (NO_SESSION != '1' AND Config::get('use_auth')) { /* Verify Their session */ if (!vauth_check_session()) { logout(); exit; } /* Create the new user */ - $user = get_user_from_username($_SESSION['userdata']['username']); - + $GLOBALS['user'] = User::get_from_username($_SESSION['userdata']['username']); + /* If they user ID doesn't exist deny them */ - if (!$user->uid AND !conf('demo_mode')) { logout(); exit; } + if (!$GLOBALS['user']->id AND !Config::get('demo_mode')) { logout(); exit; } /* Load preferences and theme */ - init_preferences(); set_theme(); - $user->set_preferences(); - $user->update_last_seen(); + $GLOBALS['user']->update_last_seen(); } -elseif (!conf('use_auth')) { +elseif (!Config::get('use_auth')) { $auth['success'] = 1; $auth['username'] = '-1'; $auth['fullname'] = "Ampache User"; @@ -262,37 +204,38 @@ elseif (!conf('use_auth')) { $auth['access'] = "admin"; $auth['offset_limit'] = 50; if (!vauth_check_session()) { vauth_session_create($auth); } - $user = new User(-1); - $user->fullname = 'Ampache User'; - $user->offset_limit = $auth['offset_limit']; - $user->username = '-1'; - $user->access = $auth['access']; + $GLOBALS['user'] = new User(-1); + $GLOBALS['user']->fullname = 'Ampache User'; + $GLOBALS['user']->offset_limit = $auth['offset_limit']; + $GLOBALS['user']->username = '-1'; + $GLOBALS['user']->access = $auth['access']; $_SESSION['userdata']['username'] = $auth['username']; - $user->set_preferences(); - init_preferences(); set_theme(); } +// If Auth, but no session is set else { if (isset($_REQUEST['sessid'])) { $sess_results = vauth_get_session($_REQUEST['sessid']); session_id(scrub_in($_REQUEST['sessid'])); session_start(); } - $user = get_user_from_username($sess_results['username']); - init_preferences(); + $GLOBALS['user'] = User::get_from_username($sess_results['username']); } +// Load the Preferences from the database +init_preferences(); + /* Add in some variables for ajax done here because we need the user */ $ajax_info['ajax_url'] = $results['web_path'] . '/server/ajax.server.php'; -$ajax_info['ajax_info'] = '&user_id=' . $user->id . '&sessid=' . session_id(); -conf($ajax_info); +$ajax_info['ajax_info'] = '&user_id=' . $GLOBALS['user']->id; +Config::set_by_array($ajax_info); unset($ajax_info); // Load gettext mojo load_gettext(); /* Set CHARSET */ -header ("Content-Type: text/html; charset=" . conf('site_charset')); +header ("Content-Type: text/html; charset=" . Config::get('site_charset')); /* Clean up a bit */ unset($array); @@ -301,19 +244,14 @@ unset($results); /* Setup the flip class */ flip_class(array('odd','even')); -/* Setup the Error Class */ -$error = new Error(); - /* Set the Theme */ -$theme = get_theme(conf('theme_name')); +$theme = get_theme(Config::get('theme_name')); +/* Check to see if we need to perform an update */ if (! preg_match('/update\.php/', $_SERVER['PHP_SELF'])) { - $update = new Update(); - if ($update->need_update()) { - header("Location: " . conf('web_path') . "/update.php"); + if (Update::need_update()) { + header("Location: " . Config::get('web_path') . "/update.php"); exit(); } } - -unset($update); ?> diff --git a/lib/log.lib.php b/lib/log.lib.php index 1b2d33e5..66d7263f 100644 --- a/lib/log.lib.php +++ b/lib/log.lib.php @@ -33,7 +33,7 @@ function log_event($username='Unknown',$event_name,$event_description,$log_name= /* must have some name */ if (!strlen($log_name)) { $log_name = 'ampache'; } - $log_filename = conf('log_path') . "/$log_name." . date("Ymd",$log_time) . ".log"; + $log_filename = Config::get('log_path') . "/$log_name." . date("Ymd",$log_time) . ".log"; $log_line = date("Y-m-d H:i:s",$log_time) . " { $username } ( $event_name ) - $event_description \n"; $log_write = error_log($log_line, 3, $log_filename); @@ -109,7 +109,7 @@ function ampache_error_handler($errno, $errstr, $errfile, $errline) { */ function debug_event($type,$message,$level,$file='',$username='') { - if (!conf('debug') || $level > conf('debug_level')) { + if (!Config::get('debug') || $level > Config::get('debug_level')) { return false; } diff --git a/lib/preferences.php b/lib/preferences.php index b1448d94..7e8cc62c 100644 --- a/lib/preferences.php +++ b/lib/preferences.php @@ -175,29 +175,27 @@ function update_preference($username,$name,$pref_id,$value) { } // update_preference -/*! - @function has_preference_access - @discussion makes sure that the user has sufficient - rights to actually set this preference, handle - as allow all, deny X - //FIXME: - // This is no longer needed, we just need to check against preferences.level -*/ +/** + * has_preference_access + * makes sure that the user has sufficient + * rights to actually set this preference, handle + * as allow all, deny X + */ function has_preference_access($name) { /* If it's a demo they don't get jack */ - if (conf('demo_mode')) { + if (Config::get('demo_mode')) { return false; } - $name = sql_escape($name); + $name = Dba::escape($name); /* Check Against the Database Row */ - $sql = "SELECT level FROM preferences " . - "WHERE name='$name'"; - $db_results = mysql_query($sql, dbh()); + $sql = "SELECT `level` FROM `preferences` " . + "WHERE `name`='$name'"; + $db_results = Dba::query($sql); - $data = mysql_fetch_assoc($db_results); + $data = Dba::fetch_assoc($db_results); $level = $data['level']; @@ -207,7 +205,7 @@ function has_preference_access($name) { return false; -} // has_preference_access +} //has_preference_access /*! @@ -423,28 +421,27 @@ function insert_preference($name,$description,$default,$level,$type,$catagory) { */ function init_preferences() { - /* Get Global Preferences */ $sql = "SELECT preferences.name,user_preference.value FROM preferences,user_preference WHERE user_preference.user='-1' " . " AND user_preference.preference = preferences.id AND preferences.catagory='system'"; - $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)) { $name = $r['name']; $results[$name] = $r['value']; } // end while sys prefs /* Now we need to allow the user to override some stuff that's been set by the above */ $user_id = '-1'; - if ($GLOBALS['user']->username) { - $user_id = sql_escape($GLOBALS['user']->id); + if ($GLOBALS['user']->id) { + $user_id = Dba::escape($GLOBALS['user']->id); } $sql = "SELECT preferences.name,user_preference.value FROM preferences,user_preference WHERE user_preference.user='$user_id' " . " AND user_preference.preference = preferences.id AND preferences.catagory != 'system'"; - $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)) { $name = $r['name']; $results[$name] = $r['value']; } // end while @@ -454,9 +451,7 @@ function init_preferences() { $results['theme_path'] = '/themes/' . $results['theme_name']; } - conf($results,1); - - return true; + Config::set_by_array($results,1); } // init_preferences @@ -529,4 +524,25 @@ function fix_all_users_prefs() { } // fix_all_users_prefs +/** + * fix_preferences + * This takes the preferences, explodes what needs to + * become an array and boolean everythings + */ +function fix_preferences($results) { + + $results['auth_methods'] = explode(",",$results['auth_methods']); + $results['tag_order'] = explode(",",$results['tag_order']); + $results['album_art_order'] = explode(",",$results['album_art_order']); + $results['amazon_base_urls'] = explode(",",$results['amazon_base_urls']); + + foreach ($results as $key=>$data) { + if (strcasecmp($data,"true") == "0") { $results[$key] = 1; } + if (strcasecmp($data,"false") == "0") { $results[$key] = 0; } + } + + return $results; + +} // fix_preferences + ?> diff --git a/lib/song.php b/lib/song.php index 57fab392..5a26d183 100644 --- a/lib/song.php +++ b/lib/song.php @@ -88,12 +88,12 @@ function get_recently_played() { "FROM object_count " . "WHERE object_type='song' " . "ORDER by object_count.date DESC " . - "LIMIT " . conf('popular_threshold'); - $db_results = mysql_query($sql, dbh()); + "LIMIT " . Config::get('popular_threshold'); + $db_results = Dba::query($sql); $results = array(); - while ($r = mysql_fetch_assoc($db_results)) { + while ($r = Dba::fetch_assoc($db_results)) { $results[] = $r; } @@ -101,16 +101,6 @@ function get_recently_played() { } // get_recently_played -/*! - @function format_song - @discussion takes a song array and makes it html friendly -*/ -function format_song($song) { - - return $song; - -} // format_song - /** * get_popular_songs * This returns the current popular songs diff --git a/lib/stream.lib.php b/lib/stream.lib.php index d4163ab5..39901d31 100644 --- a/lib/stream.lib.php +++ b/lib/stream.lib.php @@ -58,13 +58,13 @@ function gc_now_playing() { $time = time(); $expire = $time - 3200; // 86400 seconds = 1 day - $session_id = sql_escape($_REQUEST['sid']); + $session_id = Dba::escape($_REQUEST['sid']); if (strlen($session_id)) { $session_sql = " OR session = '$session_id'"; } $sql = "DELETE FROM now_playing WHERE start_time < $expire" . $session_sql; - $db_result = mysql_query($sql, dbh()); + $db_result = Dba::query($sql); } // gc_now_playing @@ -101,9 +101,9 @@ function insert_now_playing($song_id,$uid,$song_length) { $sql = "INSERT INTO now_playing (`song_id`, `user`, `start_time`,`session`)" . " VALUES ('$song_id', '$uid', '$expire','$session_id')"; - $db_result = mysql_query($sql, dbh()); + $db_result = Dba::query($sql); - $insert_id = mysql_insert_id(dbh()); + $insert_id = Dba::insert_id(); return $insert_id; diff --git a/lib/themes.php b/lib/themes.php index 1ce57dae..f2f27740 100644 --- a/lib/themes.php +++ b/lib/themes.php @@ -68,8 +68,8 @@ function get_theme($name) { if (strlen($name) < 1) { return false; } - $config_file = conf('prefix') . "/themes/" . $name . "/theme.cfg.php"; - $results = read_config($config_file); + $config_file = Config::get('prefix') . "/themes/" . $name . "/theme.cfg.php"; + $results = parse_ini_file($config_file); $results['path'] = $name; return $results; @@ -116,9 +116,9 @@ function set_theme_colors($theme_name,$user_id) { */ function set_theme() { - if (strlen(conf('theme_name')) > 0) { - $theme_path = "/themes/" . conf('theme_name'); - conf(array('theme_path'=>$theme_path),1); + if (strlen(Config::get('theme_name')) > 0) { + $theme_path = "/themes/" . Config::get('theme_name'); + Config::set(array('theme_path'=>$theme_path),1); } } // set_theme diff --git a/lib/ui.lib.php b/lib/ui.lib.php index e86aa185..a4668bc4 100644 --- a/lib/ui.lib.php +++ b/lib/ui.lib.php @@ -76,10 +76,8 @@ function flip_class($array=0) { */ function clear_now_playing() { - $sql = "TRUNCATE TABLE now_playing"; - $db_results = mysql_query($sql, dbh()); - - return true; + $sql = "TRUNCATE TABLE `now_playing`"; + $db_results = Dba::query($sql); } // clear_now_playing @@ -89,11 +87,11 @@ function clear_now_playing() { * if it isn't it defines it as a simple return */ if (!function_exists('_')) { - function _($string) { + function _($string) { return $string; - } // _ + } // if _ isn't defined /** @@ -223,9 +221,9 @@ function truncate_with_ellipsis($text, $max=27) { /* Make sure the functions exist before doing the iconv mojo */ if (function_exists('iconv') && function_exists('iconv_substr') && function_exists('iconv_strlen')) { - if (iconv_strlen($text, conf('site_charset')) > $max) { - $text = iconv_substr($text, 0, $max-3, conf('site_charset')); - $text .= iconv("ISO-8859-1", conf('site_charset'), "..."); + if (iconv_strlen($text, Config::get('site_charset')) > $max) { + $text = iconv_substr($text, 0, $max-3, Config::get('site_charset')); + $text .= iconv("ISO-8859-1", Config::get('site_charset'), "..."); } } @@ -247,7 +245,7 @@ function truncate_with_ellipsis($text, $max=27) { */ function show_footer() { - require_once(conf('prefix') . '/templates/footer.inc'); + require_once Config::get('prefix') . '/templates/footer.inc'; } // show_footer @@ -257,10 +255,9 @@ function show_footer() { */ function show_now_playing() { - $dbh = dbh(); - $web_path = conf('web_path'); + $web_path = Config::get('web_path'); $results = get_now_playing(); - require (conf('prefix') . "/templates/show_now_playing.inc"); + require Config::get('prefix') . '/templates/show_now_playing.inc'; } // show_now_playing @@ -296,15 +293,15 @@ function show_play_selected() { */ function get_now_playing($filter='') { - $sql = "SELECT song_id,user FROM now_playing ORDER BY id DESC"; - $db_results = mysql_query($sql, dbh()); + $sql = "SELECT `song_id`,`user` FROM `now_playing` ORDER BY `id` DESC"; + $db_results = Dba::query($sql); $results = array(); /* While we've got stuff playing */ - while ($r = mysql_fetch_assoc($db_results)) { + while ($r = Dba::fetch_assoc($db_results)) { $song = new Song($r['song_id']); - $song->format_song(); + $song->format(); $np_user = new User($r['user']); $results[] = array('song'=>$song,'user'=>$np_user); } // end while @@ -468,7 +465,7 @@ function show_all_popular() { $songs = get_global_popular('song'); $genres = get_global_popular('genre'); - require_once(conf('prefix') . '/templates/show_all_popular.inc.php'); + require_once Config::get('prefix') . '/templates/show_all_popular.inc.php'; } // show_all_popular @@ -480,12 +477,12 @@ function show_all_popular() { * @catagory Display * @author Karl Vollmer */ -function show_all_recent() { +function show_all_recent($limit='') { - $artists = get_newest('artist'); - $albums = get_newest('album'); + $artists = Stats::get_newest('artist',$limit); + $albums = Stats::get_newest('album',$limit); - require_once(conf('prefix') . '/templates/show_all_recent.inc.php'); + require_once Config::get('prefix') . '/templates/show_all_recent.inc.php'; } // show_all_recent @@ -497,47 +494,24 @@ function show_all_recent() { */ function show_local_catalog_info() { - $dbh = dbh(); - /* Before we display anything make sure that they have a catalog */ $query = "SELECT * FROM catalog"; - $db_results = mysql_query($query, $dbh); - if (!mysql_num_rows($db_results)) { + $db_results = Dba::query($query); + + // Make sure we have something to display + if (!Dba::num_rows($db_results)) { show_box_top(); $items[] = "<span align=\"center\" class=\"error\">" . _('No Catalogs Found!') . "</span><br />"; - $items[] = "<a href=\"" . conf('web_path') . "/admin/catalog.php?action=show_add_catalog\">" ._('Add a Catalog') . "</a>"; + $items[] = "<a href=\"" . Config::get('web_path') . "/admin/catalog.php?action=show_add_catalog\">" ._('Add a Catalog') . "</a>"; show_info_box('','catalog',$items); show_box_bottom(); return false; } - $query = "SELECT count(*) AS songs, SUM(size) AS size, SUM(time) as time FROM song"; - $db_result = mysql_query($query, $dbh); - $songs = mysql_fetch_assoc($db_result); - - $query = "SELECT count(*) FROM album"; - $db_result = mysql_query($query, $dbh); - $albums = mysql_fetch_row($db_result); - - $query = "SELECT count(*) FROM artist"; - $db_result = mysql_query($query, $dbh); - $artists = mysql_fetch_row($db_result); - - $sql = "SELECT count(*) FROM user"; - $db_result = mysql_query($sql, $dbh); - $users = mysql_fetch_row($db_result); + $results = Catalog::get_stats(); - $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_result = mysql_query($sql, $dbh); - $connected_users = mysql_fetch_row($db_result); - - $hours = floor($songs['time']/3600); - $size = $songs['size']/1048576; + $hours = floor($results['time']/3600); + $size = $results['size']/1048576; $days = floor($hours/24); $hours = $hours%24; @@ -556,23 +530,30 @@ function show_local_catalog_info() { $size_unit = "MB"; } - require(conf('prefix') . "/templates/show_local_catalog_info.inc.php"); + require Config::get('prefix') . '/templates/show_local_catalog_info.inc.php'; } // show_local_catalog_info -/*! - @function img_resize - @discussion this automaticly resizes the image for thumbnail viewing - only works on gif/jpg/png this function also checks to make - sure php-gd is enabled +/** + * img_resize + * this automaticly resizes the image for thumbnail viewing + * only works on gif/jpg/png this function also checks to make + * sure php-gd is enabled */ -function img_resize($image,$size,$type){ +function img_resize($image,$size,$type,$album_id) { /* Make sure they even want us to resize it */ - if (!conf('resize_images')) { - return false; + if (!Config::get('resize_images')) { + return $image['art']; + } + // Already resized + if ($image['resized']) { + debug_event('using_resized','using resized image for Album:' . $album_id,'2'); + return $image['art']; } + $image = $image['art']; + if (!function_exists('gd_info')) { return false; } /* First check for php-gd */ @@ -604,6 +585,8 @@ function img_resize($image,$size,$type){ return false; } + ob_start(); + // determine image type and send it to the client switch ($type) { case 'jpg': @@ -618,6 +601,21 @@ function img_resize($image,$size,$type){ break; } + // Grab this image data and save it into the thumbnail + $data = ob_get_contents(); + ob_end_clean(); + + // If our image create failed don't save it, just return + if (!$data) { + debug_event('IMG_RESIZE','Failed to resize Art from Album:' . $album_id,'3'); + return $image; + } + + // Save what we've got + Album::save_resized_art($data,'image/' . $type,$album_id); + + return $data; + } // img_resize /** @@ -692,14 +690,14 @@ function show_artist_pulldown ($artist_id,$select_name='artist') { */ function show_catalog_pulldown ($name='catalog',$style) { - $sql = "SELECT id,name FROM catalog ORDER BY name"; - $db_result = mysql_query($sql, dbh()); + $sql = "SELECT `id`,`name` FROM `catalog` ORDER BY `name`"; + $db_result = Dba::query($sql); echo "\n<select name=\"" . $name . "\" style=\"" . $style . "\">\n"; - echo "<option value=\"-1\">All</option>\n"; + echo "<option value=\"-1\">" . _('All') . "</option>\n"; - while ($r = mysql_fetch_assoc($db_result)) { + while ($r = Dba::fetch_assoc($db_result)) { $catalog_name = scrub_out($r['name']); if ( $catalog == $r['id'] ) { @@ -722,7 +720,7 @@ function show_catalog_pulldown ($name='catalog',$style) { */ function show_submenu($items) { - require (conf('prefix') . '/templates/subnavbar.inc.php'); + require Config::get('prefix') . '/templates/subnavbar.inc.php'; } // show_submenu @@ -750,7 +748,7 @@ function get_location() { } /* Sanatize the $_SERVER['PHP_SELF'] variable */ - $source = str_replace(conf('raw_web_path'),"",$source); + $source = str_replace(Config::get('raw_web_path'),"",$source); $location['page'] = preg_replace("/^\/(.+\.php)\/?.*/","$1",$source); switch ($location['page']) { @@ -850,12 +848,11 @@ function show_preference_box($preferences) { * the currently selected and then the size * */ - function show_genre_pulldown ($name,$selected='',$size=1,$width=0,$style='') { /* Get them genre hippies */ $sql = "SELECT genre.id,genre.name FROM genre ORDER BY genre.name"; - $db_result = mysql_query($sql, dbh()); + $db_result = Dba::query($sql); if ($size > 0) { $multiple_txt = "multiple=\"multiple\" size=\"$size\""; @@ -867,7 +864,7 @@ function show_genre_pulldown ($name,$selected='',$size=1,$width=0,$style='') { echo "<select name=\"" . $name . "[]\" $multiple_txt $style_txt>\n"; echo "\t<option value=\"-1\">" . _("All") . "</option>\n"; - while ($r = mysql_fetch_assoc($db_result)) { + while ($r = Dba::fetch_assoc($db_result)) { if ($width > 0) { $r['name'] = truncate_with_ellipsis($r['name'],$width); @@ -1183,7 +1180,7 @@ function show_user_select($name,$selected='',$style='') { */ function show_box_top($title='') { - require (conf('prefix') . '/templates/show_box_top.inc.php'); + require Config::get('prefix') . '/templates/show_box_top.inc.php'; } // show_box_top @@ -1194,7 +1191,7 @@ function show_box_top($title='') { */ function show_box_bottom() { - require (conf('prefix') . '/templates/show_box_bottom.inc.php'); + require Config::get('prefix') . '/templates/show_box_bottom.inc.php'; } // show_box_bottom @@ -1224,21 +1221,21 @@ function get_user_icon($name,$hover_name='') { $icon_name = 'icon_' . $name . '.png'; /* Build the image url */ - if (file_exists(conf('prefix') . '/themes/' . $GLOBALS['theme']['path'] . '/images/' . $icon_name)) { - $img_url = conf('web_path') . conf('theme_path') . '/images/' . $icon_name; + if (file_exists(Config::get('prefix') . '/themes/' . Config::get('theme_path') . '/images/' . $icon_name)) { + $img_url = Config::get('web_path') . Config::get('theme_path') . '/images/' . $icon_name; } else { - $img_url = conf('web_path') . '/images/' . $icon_name; + $img_url = Config::get('web_path') . '/images/' . $icon_name; } /* If Hover, then build its url */ if (!empty($hover_name)) { $hover_icon = 'icon_' . $hover_name . '.png'; - if (file_exists(conf('prefix') . '/themes/' . $GLOBALS['theme']['path'] . '/images/' . $icon_name)) { - $hov_url = conf('web_path') . conf('theme_path') . '/images/' . $hover_icon; + if (file_exists(Config::get('prefix') . '/themes/' . Config::get('theme_path') . '/images/' . $icon_name)) { + $hov_url = Config::get('web_path') . Config::get('theme_path') . '/images/' . $hover_icon; } else { - $hov_url = conf('web_path') . '/images/' . $hover_icon; + $hov_url = Config::get('web_path') . '/images/' . $hover_icon; } $hov_txt = "onMouseOver=\"this.src='$hov_url'; return true;\" onMouseOut=\"this.src='$img_url'; return true;\""; |