" . _("Error") . ": " . _("Unable to load XMLRPC library, make sure XML-RPC is enabled") . "
\n";
return false;
} // end check for class
// first, glean out the information from the path about the server and remote path
// this can't contain the http
preg_match("/http:\/\/([^\/]+)\/*(.*)/", $this->path, $match);
$server = $match[1];
$path = $match[2];
if ( ! $path ) {
$client = new xmlrpc_client("/server/xmlrpc.server.php", $server, 80);
}
else {
$client = new xmlrpc_client("/$path/server/xmlrpc.server.php", $server, 80);
}
$f = new xmlrpcmsg('remote_catalog_query', array(new xmlrpcval( conf('web_path'), "string")) );
if (conf('debug')) { $client->setDebug(1); }
$response = $client->send($f,30);
$value = $response->value();
if ( !$response->faultCode() ) {
$data = php_xmlrpc_decode($value);
// Print out the catalogs we are going to sync
foreach ($data as $vars) {
$catalog_name = $vars[0];
$count = $vars[1];
print("Reading Remote Catalog: $catalog_name ($count Songs) [$this->path]
\n");
$total += $count;
}
// Flush the output
flush();
} // if we didn't get an error
else {
$error_msg = _("Error connecting to") . " " . $server . " " . _("Code") . ": " . $response->faultCode() . " " . _("Reason") . ": " . $response->faultString();
if (conf('debug')) { log_event($_SESSION['userdata']['username'],' xmlrpc-client ',$error_msg,'ampache-catalog'); }
echo "$error_msg
";
return;
}
// Hardcoded for now
$step = '500';
$current = '0';
while ($total > $current) {
$start = $current;
$current += $step;
$this->get_remote_song($client,$start,$step);
}
echo "" . _("Completed updating remote catalog(s)") . ".
\n";
flush();
return true;
} // get_remote_catalog
/**
* get_remote_song
* This functions takes a start and end point for gathering songs from a remote server. It is broken up
* in attempt to get around the problem of very large target catalogs
* @package XMLRPC
* @catagory Client
* @todo Allow specificion of single catalog
*/
function get_remote_song($client,$start,$end) {
$query_array = array(new xmlrpcval($start, "int"),new xmlrpcval($end,"int"));
$f = new xmlrpcmsg('remote_song_query',$query_array);
/* Depending upon the size of the target catalog this can be a very slow/long process */
set_time_limit(0);
// Sixty Second time out per chunk
$response = $client->send($f,60);
$value = $response->value();
if ( !$response->faultCode() ) {
$data = php_xmlrpc_decode($value);
$this->update_remote_catalog($data,$this->path);
$total = $start + $end;
echo "Added $total...
";
flush();
}
else {
$error_msg = _("Error connecting to") . " " . $server . " " . _("Code") . ": " . $response->faultCode() . " " . _("Reason") . ": " . $response->faultString();
if (conf('debug')) { log_event($_SESSION['userdata']['username'],' xmlrpc-client ',$error_msg,'ampache-catalog'); }
echo "$error_msg
";
}
return;
} // get_remote_song
/**
* update_remote_catalog
* actually updates from the remote data, takes an array of songs that are base64 encoded and parses them
* @package XMLRPC
* @catagory Client
* @todo This should be based off of seralize
* @todo some kind of cleanup of dead songs?
*/
function update_remote_catalog($songs,$root_path) {
/*
We need to check the incomming songs
to see which ones need to be added
*/
foreach ($songs as $song) {
// Prevent a timeout
set_time_limit(0);
$song = base64_decode($song);
$data = explode("::", $song);
$new_song->artist = $this->check_artist($data[0]);
$new_song->album = $this->check_album($data[1],$data[4]);
$new_song->title = $data[2];
$new_song->comment = $data[3];
$new_song->year = $data[4];
$new_song->bitrate = $data[5];
$new_song->rate = $data[6];
$new_song->mode = $data[7];
$new_song->size = $data[8];
$new_song->time = $data[9];
$new_song->track = $data[10];
$new_song->genre = $this->check_genre($data[11]);
$new_song->file = $root_path . "/play/index.php?song=" . $data[12];
$new_song->catalog = $this->id;
if (!$this->check_remote_song($new_song->file)) {
$this->insert_remote_song($new_song);
}
} // foreach new Songs
// now delete invalid entries
$this->clean_albums();
$this->clean_stats();
$this->clean_artists();
$this->clean_genres();
$this->clean_flagged();
} // update_remote_catalog
/*!
@function clean_catalog
@discussion Cleans the Catalog of files that no longer exist grabs from $this->id or $id passed
Doesn't actually delete anything, disables errored files, and returns them in an array
@param $catalog_id=0 Take the ID of the catalog you want to clean
@param $action=0 Delete/Disable, default is disable
*/
function clean_catalog($catalog_id=0,$action=0) {
/* Define the Arrays we will need */
$dead_files = array();
if (!$catalog_id) { $catalog_id = $this->id; }
echo "\nCleaning the [" . $this->name . "] Catalog...
\n";
flush();
/* Get all songs in this catalog */
$sql = "SELECT id,file FROM song WHERE catalog='$catalog_id' AND enabled='1'";
$db_results = mysql_query($sql, dbh());
/* Recurse through files, put @ to prevent errors poping up */
while ($results = mysql_fetch_object($db_results)) {
/* Remove slashes while we are checking for its existance */
$results->file = stripslashes($results->file);
/* Stupid little cutesie thing */
$this->count++;
if ( !($this->count%conf('catalog_echo_count')) ) {
echo _('Checking') . " $this->count. . . .
\n";
flush();
} //echos song count
/* Also check the file information */
$file_info = @filesize($results->file);
/* If it errors somethings splated, or the files empty */
if (!file_exists($results->file) OR $file_info < 1) {
/* Add Error */
echo "Error File Not Found or 0 Bytes: " . $results->file . "
";
flush();
/* Add this file to the list for removal from the db */
$dead_files[] = $results;
} //if error
} //while gettings songs
/* Incase there's been a snafo with a mount point on something
* don't actually delete from DB here, simply disable and list
*/
if (count($dead_files)) {
foreach ($dead_files as $data) {
//FIXME: Until I fix the form, assume delete
//if ($action === 'delete_dead') {
$sql = "DELETE FROM song WHERE id='$data->id'";
//}
//
//else {
// $sql = "UPDATE song SET status='disabled' WHERE id='$data->id'";
//}
$db_results = mysql_query($sql, dbh());
/* DB Error occured */
if (!$db_results) {
/* Add Error */
} //if error
} //end foreach
} // end if dead files
/* Step two find orphaned Arists/Albums
* This finds artists and albums that no
* longer have any songs associated with them
*/
$this->clean_albums();
$this->clean_artists();
$this->clean_stats();
$this->clean_playlists();
$this->clean_flagged();
;$this->clean_genres();
/* Return dead files, so they can be listed */
echo "" . _("Catalog Clean Done") . " [" . count($dead_files) . "] " . _("files removed") . "
\n";
flush();
return $dead_files;
$this->count = 0;
} //clean_catalog
/**
* clean_genres
* This functions cleans up unused genres
* @package Catalog
* @catagory Clean
*/
function clean_genres() {
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",mysql_get_server_info())) {
$sql = "SELECT genre.id FROM genre LEFT JOIN song ON song.genre = genre.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM genre WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
return true;
}
/* Do a complex delete to get albums where there are no songs */
$sql = "DELETE FROM genre USING genre LEFT JOIN song ON song.genre = genre.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
} // clean_genres
/*!
@function clean_albums
@discussion This function cleans out unused albums
@param $this->id Depends on the current object
*/
function clean_albums() {
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",mysql_get_server_info())) {
$sql = "SELECT album.id FROM album LEFT JOIN song ON song.album = album.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM album WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
return true;
}
/* Do a complex delete to get albums where there are no songs */
$sql = "DELETE FROM album USING album LEFT JOIN song ON song.album = album.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
} //clean_albums
/*!
@function clean_flagged
@discussion This functions cleans ou unused flagged items
*/
function clean_flagged() {
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",mysql_get_server_info())) {
$sql = "SELECT flagged.id FROM flagged LEFT JOIN song ON song.id=flagged.song WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM flagged WHERE id='$dead[0]'";
$db_results = mysql_query($sql, dbh());
}
return true;
}
/* Do a complex delete to get flagged items where the songs are now gone */
$sql = "DELETE FROM flagged USING flagged LEFT JOIN song ON song.id = flagged.song WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
} // clean_flagged
/*!
@function clean_artists
@discussion This function cleans out unused artists
@param $this->id Depends on the current object
*/
function clean_artists() {
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",mysql_get_server_info())) {
$sql = "SELECT artist.id FROM artist LEFT JOIN song ON song.artist = artist.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM artist WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
return true;
}
/* Do a complex delete to get artists where there are no songs */
$sql = "DELETE FROM artist USING artist LEFT JOIN song ON song.artist = artist.id WHERE song.id IS NULL";
$db_results = mysql_query($sql, dbh());
} //clean_artists
/*
@function clean_playlists
@discussion cleans out dead files from playlists
@param $this->id depends on the current object
*/
function clean_playlists() {
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",mysql_get_server_infO())) {
$sql = "SELECT playlist_data.song FROM playlist_data LEFT JOIN song ON song.id = playlist_data.song WHERE song.file IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM playlist_data WHERE song='$dead[0]'";
$db_results = mysql_query($sql, dbh());
}
return true;
}
/* Do a complex delete to get playlist songs where there are no songs */
$sql = "DELETE FROM playlist_data USING playlist_data LEFT JOIN song ON song.id = playlist_data.song WHERE song.file IS NULL";
$db_results = mysql_query($sql, dbh());
} // clean_playlists
/*!
@function clean_stats
@discussion This functions removes stats for songs/albums that no longer exist
@param $catalog_id The ID of the catalog to clean
*/
function clean_stats() {
$version = mysql_get_server_info();
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",$version)) {
$sql = "SELECT object_count.id FROM object_count LEFT JOIN song ON song.id = object_count.object_id WHERE object_type='song' AND song.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM object_count WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
}
// We assume this will be 4.0+
else {
/* Crazy SQL Mojo to remove stats where there are no songs */
$sql = "DELETE FROM object_count USING object_count LEFT JOIN song ON song.id=object_count.object_id WHERE object_type='song' AND song.id IS NULL";
$db_results = mysql_query($sql, dbh());
}
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",$version)) {
$sql = "SELECT object_count.id FROM object_count LEFT JOIN album ON album.id = object_count.object_id WHERE object_type='album' AND album.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM object_count WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
}
// We assume 4.0+ Here
else {
/* Crazy SQL Mojo to remove stats where there are no albums */
$sql = "DELETE FROM object_count USING object_count LEFT JOIN album ON album.id=object_count.object_id WHERE object_type='album' AND album.id IS NULL";
$db_results = mysql_query($sql, dbh());
}
/* Mysql 3.23 doesn't support our cool query so we have to do it a different way */
if (preg_match("/^3\./",$version)) {
$sql = "SELECT object_count.id FROM object_count LEFT JOIN artist ON artist.id = object_count.object_id WHERE object_type='artist' AND artist.id IS NULL";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_row($db_results)) {
$results[] = $r;
}
foreach ($results as $dead) {
$sql = "DELETE FROM object_count WHERE id='$dead[0]'";
$db_results = mysql_query($sql,dbh());
}
}
// We assume 4.0+ here
else {
/* Crazy SQL Mojo to remove stats where ther are no artists */
$sql = "DELETE FROM object_count USING object_count LEFT JOIN artist ON artist.id=object_count.object_id WHERE object_type='artist' AND artist.id IS NULL";
$db_results = mysql_query($sql, dbh());
}
} // clean_stats
/*!
@function verify_catalog
@discussion This function compares the DB's information with the ID3 tags
@param $catalog_id The ID of the catalog to compare
*/
function verify_catalog($catalog_id=0,$gather_type='') {
/* Create and empty song for us to use */
$total_updated = 0;
/* Set it to this if they don't pass anything */
if (!$catalog_id) {
$catalog_id = $this->id;
}
/* First get the filenames for the catalog */
$sql = "SELECT id FROM song WHERE catalog='$catalog_id' ORDER BY id";
$db_results = mysql_query($sql, dbh());
$number = mysql_num_rows($db_results);
echo _("Updating the") . " [ $this->name ] " . _("Catalog") . "
\n";
echo $number . " " . _("songs found checking tag information.") . "
\n\n";
flush();
/* Magical Fix so we don't run out of time */
set_time_limit(0);
/* Recurse through this catalogs files
* and get the id3 tage information,
* if it's not blank, and different in
* in the file then update!
*/
while ($results = mysql_fetch_object($db_results)) {
/* Create the object from the existing database information */
$song = new Song($results->id);
if (conf('debug')) { log_event($_SESSION['userdata']['username'],' verify ',"Starting work on $song->file",'ampache-catalog'); }
if (is_readable($song->file)) {
unset($skip);
/* If they have specified fast_update check the file
filemtime to make sure the file has actually
changed
*/
if ($gather_type == 'fast_update') {
$file_date = filemtime($song->file);
if ($file_date < $this->last_update) { $skip = true; }
} // if gather_type
if ($song->update_time >= $this->last_update) {
$skip = true;
$song->update_utime($song->id,time()+86400);
}
// if the file hasn't been modified since the last_update
if (!$skip) {
$info = $this->update_song_from_tags($song);
$album_id = $song->album;
if ($info['change']) {
echo "\n\t
";
echo "$song->file " . _("Updated") . "\n";
echo $info['text'];
/* If we aren't doing a fast update re-gather album art */
if ($gather_type != 'fast_update' AND !isset($searched_albums[$album_id])) {
$album = new Album($song->album);
$searched_albums[$album_id] = 1;
$found = $album->get_art();
unset($album);
if ($found) { $is_found = _(" FOUND"); }
echo "
" . _("Searching for new Album Art") . ". . .$is_found
\n";
unset($found,$is_found);
}
elseif (isset($searched_albums[$album_id])) {
echo "
" . _("Album Art Already Found") . ". . .
\n";
}
echo "\t\n\n
\n";
flush();
$total_updated++;
}
unset($info);
} // end skip
if ($skip) {
if (conf('debug')) { log_event($_SESSION['userdata']['username'],' skip ',"$song->file has been skipped due to newer local update or file mod time",'ampache-catalog'); }
}
/* Stupid little cutesie thing */
$this->count++;
if ( !($this->count%conf('catalog_echo_count')) ) {
echo "Checked $this->count. . . .
\n";
flush();
} //echos song count
} // end if file exists
else {
echo "\n
";
echo "$song->file does not exist or is not readable\n";
echo " \n\n
\n";
if (conf('debug')) { log_event($_SESSION['userdata']['username'],' read-error ',"$song->file does not exist or is not readable",'ampache-catalog'); }
// Should we remove it from catalog?
}
} //end foreach
/* After we have updated all the songs with the new information clear any empty albums/artists */
$this->clean_albums();
$this->clean_artists();
$this->clean_stats();
$this->clean_flagged();
// Update the last_update
$this->update_last_update();
echo "Update Finished. Checked $this->count. $total_updated songs updated.
";
$this->count = 0;
return true;
} //verify_catalog
/*!
@function create_catalog_entry
@discussion Creates a new catalog from path and type
@param $path The root path for this catalog
@param $name The name of the new catalog
*/
function create_catalog_entry($path,$name,$id3cmd=0,$ren=0,$sort=0, $type='local') {
// Current time
$date = time();
$path = sql_escape($path);
$name = sql_escape($name);
if($id3cmd && $ren && $sort) {
$sql = "INSERT INTO catalog (path,name,last_update,id3_set_command,rename_pattern,sort_pattern,catalog_type) " .
" VALUES ('$path','$name','$date', '$id3cmd', '$ren', '$sort','$type')";
}
else {
$sql = "INSERT INTO catalog (path,name,last_update) VALUES ('$path','$name','$date')";
}
$db_results = mysql_query($sql, dbh());
$catalog_id = mysql_insert_id(dbh());
return $catalog_id;
} //create_catalog_entry
/*!
@function check_catalog
@discussion Checks for the $path already in the catalog table
@param $path The root path for the catalog we are checking
*/
function check_catalog($path) {
$path = sql_escape($path);
$sql = "SELECT id FROM catalog WHERE path='$path'";
$db_results = mysql_query($sql, dbh());
$results = mysql_fetch_object($db_results);
return $results->id;
} //check_catalog
/*!
@function check_artist
@discussion Takes $artist checks if there then return id else insert and return id
@param $artist The name of the artist
*/
function check_artist($artist) {
// Only get the var ones.. less func calls
$cache_limit = conf('artist_cache_limit');
/* Clean up the artist */
$artist = trim($artist);
$artist = sql_escape($artist);
/* Ohh no the artist has lost it's mojo! */
if (!$artist) {
$artist = "Unknown (Orphaned)";
}
// Remove the prefix so we can sort it correctly
preg_match("/^(The\s|An\s|A\s)(.*)/i",$artist,$matches);
if (count($matches)) {
$artist = $matches[2];
$prefix = $matches[1];
}
// Check to see if we've seen this artist before
if (isset($this->artists[$artist])) {
return $this->artists[$artist];
} // if we've seen this artist before
/* Setup the checking sql statement */
$sql = "SELECT id FROM artist WHERE name LIKE '$artist' ";
$db_results = mysql_query($sql, dbh());
/* If it's found */
if ($r = mysql_fetch_object($db_results)) {
$artist_id = $r->id;
} //if found
/* If not found create */
else {
$prefix_txt = 'NULL';
if ($prefix) {
$prefix_txt = "'$prefix'";
}
$sql = "INSERT INTO artist (name, prefix) VALUES ('$artist', $prefix_txt)";
$db_results = mysql_query($sql, dbh());
$artist_id = mysql_insert_id(dbh());
if (!$db_results) {
echo "Error Inserting Artist:$artist
";
flush();
}
} //not found
if ($cache_limit) {
$artist_count = count($this->artists);
if ($artist_count == $cache_limit) {
$this->artists = array_slice($this->artists,1);
}
if (conf('debug')) { log_event($_SESSION['userdata']['username'],'cache',"Adding $artist with $artist_id to Cache",'ampache-catalog'); }
$array = array($artist => $artist_id);
$this->artists = array_merge($this->artists, $array);
unset($array);
} // if cache limit is on..
return $artist_id;
} //check_artist
/*!
@function check_album
@disucssion Takes $album and checks if there then return id else insert and return id
@param $album The name of the album
*/
function check_album($album,$album_year=0) {
/* Clean up the album name */
$album = trim($album);
$album = sql_escape($album);
$album_year = intval($album_year);
// Set it once to reduce function calls
$cache_limit = conf('album_cache_limit');
/* Ohh no the album has lost it's mojo */
if (!$album) {
$album = "Unknown (Orphaned)";
}
// Remove the prefix so we can sort it correctly
preg_match("/^(The\s|An\s|A\s)(.*)/i",$album,$matches);
if (count($matches)) {
$album = $matches[2];
$prefix = $matches[1];
}
// Check to see if we've seen this album before
if (isset($this->albums[$album])) {
return $this->albums[$album];
}
/* Setup the Query */
$sql = "SELECT id FROM album WHERE name LIKE '$album'";
if ($album_year) { $sql .= " AND year='$album_year'"; }
$db_results = mysql_query($sql, dbh());
/* If it's found */
if ($r = mysql_fetch_object($db_results)) {
$album_id = $r->id;
} //if found
/* If not found create */
else {
$prefix_txt = 'NULL';
if ($prefix) {
$prefix_txt = "'$prefix'";
}
$sql = "INSERT INTO album (name, prefix,year) VALUES ('$album',$prefix_txt,'$album_year')";
$db_results = mysql_query($sql, dbh());
$album_id = mysql_insert_id(dbh());
if (!$db_results) {
echo "Error Inserting Album:$album
";
flush();
}
} //not found
if ($cache_limit > 0) {
$albums_count = count($this->albums);
if ($albums_count == $cache_limit) {
$this->albums = array_slice($this->albums,1);
}
$array = array($album => $album_id);
$this->albums = array_merge($this->albums,$array);
unset($array);
} // if cache limit is on..
return $album_id;
} //check_album
/*!
@function check_genre
@discussion Finds the Genre_id from the text name
@param $genre The name of the genre
*/
function check_genre($genre) {
/* If a genre isn't specified force one */
if (strlen(trim($genre)) < 1) {
$genre = "Unknown (Orphaned)";
}
if ($this->genres[$genre]) {
return $this->genres[$genre];
}
/* Look in the genre table */
$genre = sql_escape($genre);
$sql = "SELECT id FROM genre WHERE name LIKE '$genre'";
$db_results = mysql_query($sql, dbh());
$results = mysql_fetch_object($db_results);
if (!$results->id) {
$sql = "INSERT INTO genre (name) VALUES ('$genre')";
$db_results = mysql_query($sql, dbh());
$results->id = mysql_insert_id(dbh());
}
$this->genres[$genre] = $results->id;
return $results->id;
} //check_genre
/*!
@function check_title
@discussion this checks to make sure something is
set on the title, if it isn't it looks at the
filename and trys to set the title based on that
*/
function check_title($title,$file=0) {
if (strlen(trim($title)) < 1) {
preg_match("/.+\/(.*)\.....?$/",$file,$matches);
$title = sql_escape($matches[1]);
}
return $title;
} //check_title
/*!
@function insert_local_song
@discussion Insert a song that isn't already in the database this
function is in here so we don't have to create a song object
@param $file The file name we are adding (full path)
@param $file_info The information of the file, size etc taken from stat()
*/
function insert_local_song($file,$file_info) {
/* Create the Audioinfo object and get info */
$audio_info = new Audioinfo();
$song_obj = new Song();
$results = $audio_info->Info($file);
$results['file'] = $file;
$key = get_tag_type($results);
/* Fill Empty info from filename/path */
$results = $song_obj->fill_info($results,$this->sort_pattern . "/" . $this->rename_pattern,$this->id,$key);
/* Clean Up the tags */
$results = clean_tag_info($results,$key,$file);
/* Set the vars here... so we don't have to do the '" . $blah['asd'] . "' */
$title = sql_escape($results['title']);
$artist = $results['artist'];
$album = $results['album'];
$genre = $results['genre'];
$bitrate = $results['bitrate'];
$rate = $results['rate'];
$mode = $results['mode'];
$size = $results['size'];
$song_time = $results['time'];
$track = $results['track'];
$year = $results['year'];
$comment = $results['comment'];
$current_time = time();
/*
* We have the artist/genre/album name need to check it in the tables
* If found then add & return id, else return id
*/
$artist_id = $this->check_artist($artist);
$genre_id = $this->check_genre($genre);
$album_id = $this->check_album($album,$year);
$title = $this->check_title($title,$file);
$add_file = sql_escape($results['file']);
$sql = "INSERT INTO song (file,catalog,album,artist,title,bitrate,rate,mode,size,time,track,genre,addition_time,year,comment)" .
" VALUES ('$add_file','$this->id','$album_id','$artist_id','$title','$bitrate','$rate','$mode','$size','$song_time','$track','$genre_id','$current_time','$year','$comment')";
$db_results = mysql_query($sql, dbh());
if (!$db_results) {
if (conf('debug')) { log_event($_SESSION['userdata']['username'],'insert',"Unable to insert $file -- $sql",'ampache-catalog'); }
echo "Error Adding $file
$sql
";
flush();
}
/* Clear Variables */
unset($results,$audio_info,$song_obj);
} // insert_local_song
/*!
@function insert_remote_song
@discussion takes the information gotten from XML-RPC and
inserts it into the local database. The filename
ends up being the url.
*/
function insert_remote_song($song) {
$url = sql_escape($song->file);
$title = $this->check_title($song->title);
$title = sql_escape($title);
$comment = sql_escape($song->comment);
$current_time = time();
$sql = "INSERT INTO song (file,catalog,album,artist,title,bitrate,rate,mode,size,time,track,genre,addition_time,year,comment)" .
" VALUES ('$url','$song->catalog','$song->album','$song->artist','$title','$song->bitrate','$song->rate','$song->mode','$song->size','$song->time','$song->track','$song->genre','$current_time','$song->year','$comment')";
$db_results = mysql_query($sql, dbh());
if (!$db_results) {
if (conf('debug')) { log_event($_SESSION['userdata']['username'],'insert',"Unable to Add Remote $url -- $sql",'ampache-catalog'); }
echo "Error Adding Remote $url
$sql
\n";
flush();
}
} // insert_remote_song
/*!
@function check_remote_song
@discussion checks to see if a remote song exists in the database or not
if it find a song it returns the UID
*/
function check_remote_song($url) {
$url = sql_escape($url);
$sql = "SELECT id FROM song WHERE file='$url'";
$db_results = mysql_query($sql, dbh());
if (mysql_num_rows($db_results)) {
return true;
}
return false;
} // check_remote_song
/*!
@function check_local_mp3
@discussion Checks the song to see if it's there already returns true if found, false if not
@param $full_file The full file name that we are checking
@param $gather_type=0 If we need to check id3 tags or not
*/
function check_local_mp3($full_file, $gather_type='') {
if ($gather_type == 'fast_add') {
$file_date = filemtime($full_file);
if ($file_date < $this->last_add) {
return true;
}
}
$full_file = sql_escape($full_file);
$sql = "SELECT id FROM song WHERE file = '$full_file'";
$db_results = mysql_query($sql, dbh());
//If it's found then return true
if (@mysql_fetch_row($db_results)) {
return true;
}
return false;
} //check_local_mp3
/*!
@function import_m3u
@discussion this takes m3u filename and then attempts
to create a Public Playlist based on the filenames
listed in the m3u
*/
function import_m3u($filename) {
$m3u_handle = @fopen($filename,'r');
$data = @fread($m3u_handle,filesize($filename));
$results = explode("\n",$data);
foreach ($results as $value) {
// Remove extra whitespace
$value = trim($value);
if (preg_match("/\.[A-Za-z0-9]{3,4}$/",$value)) {
$file[0] = str_replace("/","\\",$value);
$file[1] = str_replace("\\","/",$value);
/* Search for this filename, cause it's a audio file */
$sql = "SELECT id FROM song WHERE file LIKE '%" . sql_escape($file[0]) . "' OR file LIKE '%" . sql_escape($file[1]) . "'";
$db_results = mysql_query($sql, dbh());
$song_id = mysql_result($db_results,'id');
if ($song_id) { $songs[] = $song_id; }
} // if it's a file
} // end foreach line
if (conf('debug')) { log_event($GLOBALS['user']->username,'m3u_parse',"Parsing $filename - Found: " . count($songs) . " Songs"); }
if (count($songs)) {
$playlist = new Playlist();
$playlist_name = "M3U - " . basename($filename);
$playlist->create($playlist_name,'public');
$playlist->add_songs($songs);
return true;
}
return false;
} // import_m3u
/*!
@function merge_stats
@discussion merge stats entries
@param $type the object_type row in object_count to use
@param $oldid the old object_id
@param $newid the new object_id to merge to
@return the number of stats changed
@todo move this to the right file
*/
function merge_stats ($type,$oldid,$newid) {
//check data
$accepted_types = array ("artist");
if (!in_array($type,$accepted_types)) { return false; }
//now retrieve all of type and oldid
$stats_qstring = "SELECT id,count,userid," .
"(SELECT id FROM object_count WHERE object_type = '$type' AND object_id = '$newid' AND userid=o.userid) AS existingid " .
"FROM object_count AS o WHERE object_type = '$type' AND object_id = '$oldid'";
$stats_query = mysql_query($stats_qstring,dbh());
$oldstats = array();
//now collect needed data into a array
while ($stats_result = mysql_fetch_assoc($stats_query)) {
$userid = $stats_result['userid'];
$oldstats[$userid]['id'] = $stats_result['id'];
$oldstats[$userid]['count'] = $stats_result['count'];
$oldstats[$userid]['existingid'] = $stats_result['existingid'];
}
//now foreach that array, changeing/updateing object_count and if needed deleting old row
$num_changed = 0;
foreach ($oldstats as $userid => $stats) {
//first check if it is a update or insert
if (is_numeric($stats['existingid'])) {
$stats_count_change_qstring = "UPDATE object_count SET count = count + '" . $stats['count'] . "' WHERE id = '" . $stats['existingid'] . "'";
mysql_query($stats_count_change_qstring,dbh());
//then, delete old row
$old_stats_delete_qstring = "DELETE FROM object_count WHERE id ='" . $stats['id'] . "'";
mysql_query($old_stats_delete_qstring,dbh());
$num_changed++;
} else {
//hasn't yet listened, just change object_id
$stats_artist_change_qstring = "UPDATE object_count SET object_id = '$newid' WHERE id ='" . $stats['id'] . "'";
mysql_query($stats_artist_change_qstring,dbh());
//done!
$num_changed++;
}
}
return $num_changed;
} // merge_stats
/*!
@function delete_catalog
@discussion Deletes the catalog and everything assoicated with it
assumes $this
*/
function delete_catalog() {
// Do some crazyness to delete all the songs in this catalog
// from playlists...
$sql = "SELECT playlist_data.song FROM song,playlist_data,catalog WHERE catalog.id=song.catalog AND playlist_data.song=song.id AND catalog.id='$this->id'";
$db_results = mysql_query($sql, dbh());
$results = array();
while ($r = mysql_fetch_object($db_results)) {
$results[] = $r;
}
foreach ($results as $r) {
// Clear Playlist Data
$sql = "DELETE FROM playlist_data WHERE song='$r->song'";
$db_results = mysql_query($sql, dbh());
} // End Foreach
// First remove the songs in this catalog
$sql = "DELETE FROM song WHERE catalog = '$this->id'";
$db_results = mysql_query($sql, dbh());
// Next Remove the Catalog Entry it's self
$sql = "DELETE FROM catalog WHERE id = '$this->id'";
$db_results = mysql_query($sql, dbh());
// Run the Aritst/Album Cleaners...
$this->clean_albums();
$this->clean_artists();
$this->clean_stats();
$this->clean_playlists();
$this->clean_flagged();
} // delete_catalog
/*!
@function remove_songs
@discussion removes all songs sent in $songs array from the
database, it doesn't actually delete them...
*/
function remove_songs($songs) {
foreach($songs as $song) {
$sql = "DELETE FROM song WHERE id = '$song'";
$db_results = mysql_query($sql, dbh());
}
} // remove_songs
} //end of catalog class
?>