diff options
-rwxr-xr-x | docs/CHANGELOG | 4 | ||||
-rw-r--r-- | lib/class/song.class.php | 21 | ||||
-rw-r--r-- | lib/general.lib.php | 79 | ||||
-rw-r--r-- | play/index.php | 25 |
4 files changed, 111 insertions, 18 deletions
diff --git a/docs/CHANGELOG b/docs/CHANGELOG index 92fa5783..be4b9bf2 100755 --- a/docs/CHANGELOG +++ b/docs/CHANGELOG @@ -13,6 +13,10 @@ - Updated Now Playing to account for new db structure - Fixed Now Playing so that songs played by the MPD file method actually show up (Thx sigger) + - Fixed Now Playing so that if Windows Media Player is detected + it handles it correctly, and doesn't remove the row after + the script execution has finished, and depends upon the + GC to catch it -------------------------------------------------------------------------- diff --git a/lib/class/song.class.php b/lib/class/song.class.php index 8349208d..9240a3c0 100644 --- a/lib/class/song.class.php +++ b/lib/class/song.class.php @@ -235,6 +235,27 @@ class Song { return $results['name']; } // get_genre_name + + /** + * set_played + * this checks to see if the current object has been played + * if not then it sets it to played + * @package Song + * @catagory Class + */ + function set_played() { + + if ($song->played) { + return true; + } + + /* If it hasn't been played, set it! */ + $song->update_played('1'); + + return true; + + } // set_played + /*! @function compare_song_information @discussion this compares the new ID3 tags of a file against diff --git a/lib/general.lib.php b/lib/general.lib.php index d5a0c330..8395393d 100644 --- a/lib/general.lib.php +++ b/lib/general.lib.php @@ -742,4 +742,83 @@ function tbl_name($table) { } // tbl_name + +/** + * insert_now_playing + * This function takes care of inserting the now playing data + * we use this function because we need to do thing differently + * depending upon which play is actually streaming + * @package General + * @catagory Now Playing + */ +function insert_now_playing($song_id,$uid,$song_length) { + + $user_agent = $_SERVER['HTTP_USER_AGENT']; + $time = time(); + + /* Windows Media Player is evil and it makes multiple requests per song */ + if (stristr($user_agent,"NSPlayer")) { return false; } + + /* Set the Expire Time */ + + // If they are using Windows media player + if (stristr($user_agent,"Windows-Media-Player")) { + // WMP does keep the session open so we need to cheat a little here + $expire = $time + $song_length; + } + else { + $expire = $time; + } + + $sql = "INSERT INTO now_playing (`song_id`, `user`, `start_time`)" . + " VALUES ('$song_id', '$uid', '$expire')"; + + $db_result = mysql_query($sql, dbh()); + + $insert_id = mysql_insert_id(dbh()); + + return $insert_id; + +} // insert_now_playing + +/** + * delete_now_playing + * This function checks to see if we should delete the last now playing entry now that it's + * finished streaming the song. Basicly this is an exception for WMP10 + * @package General + * @catagory Now Playing + */ +function delete_now_playing($insert_id) { + + $user_agent = $_SERVER['HTTP_USER_AGENT']; + + if (stristr($user_agent,"Windows-Media-Player")) { + // Do Nothing! + return true; + } + + + // Remove the song from the now_playing table + $sql = "DELETE FROM now_playing WHERE id = '$insert_id'"; + $db_result = mysql_query($sql, dbh()); + +} // delete_now_playing + +/** + * gc_now_playing + * this is a garbage collection function for now playing this is called every time something + * is streamed + * @package General + * @catagory Now Playing + */ +function gc_now_playing() { + + $time = time(); + $expire = $time - 1800; // 86400 seconds = 1 day + + $sql = "DELETE FROM now_playing WHERE start_time < $expire"; + $db_result = mysql_query($sql, dbh()); + +} // gc_now_playing + ?> diff --git a/play/index.php b/play/index.php index c3870e4f..874de187 100644 --- a/play/index.php +++ b/play/index.php @@ -146,11 +146,8 @@ else { // Update the users last seen $user->update_last_seen(); - // Garbage collection for stale entries in the now_playing table - $time = time(); - $expire = $time - 900; // 86400 seconds = 1 day - $sql = "DELETE FROM now_playing WHERE start_time < $expire"; - $db_result = mysql_query($sql, $dbh); + /* Run Garbage Collection on Now Playing */ + gc_now_playing(); // If we are running in Legalize mode, don't play songs already playing if (conf('lock_songs') == 'true') { @@ -166,11 +163,7 @@ else { } // Put this song in the now_playing table - $end_time = time() - $song->time; - $sql = "INSERT INTO now_playing (`song_id`, `user`, `start_time`)" . - " VALUES ('$song_id', '$uid', '$end_time')"; - $db_result = mysql_query($sql, $dbh); - $lastid = mysql_insert_id($dbh); + $lastid = insert_now_playing($song->id,$uid,$song->time); // make fread binary safe set_magic_quotes_runtime(0); @@ -282,15 +275,11 @@ else { $user->update_stats($song_id); } - // If the played flag isn't set, set it - if (!$song->played) { - $sql = "UPDATE song SET played='1' WHERE id='$song->id'"; - $db_results = mysql_query($sql, $dbh); - } + /* Set the Song as Played if it isn't already */ + $song->update_played(); - // Remove the song from the now_playing table - $sql = "DELETE FROM now_playing WHERE id = '$lastid'"; - $db_result = mysql_query($sql, $dbh); + /* Delete the Now Playing Entry */ + delete_now_playing($lastid); /* Clean up any open ends */ if ($ds) { |