diff options
Diffstat (limited to 'play')
-rw-r--r-- | play/index.php | 282 | ||||
-rw-r--r-- | play/pupload.php | 227 |
2 files changed, 509 insertions, 0 deletions
diff --git a/play/index.php b/play/index.php new file mode 100644 index 00000000..0091db10 --- /dev/null +++ b/play/index.php @@ -0,0 +1,282 @@ +<?php +/* + + Copyright (c) 2001 - 2005 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. + +*/ +/* + + This is the wrapper for opening music streams from this server. This script + will play the local version or redirect to the remote server if that be + the case. Also this will update local statistics for songs as well. + +*/ + +$no_session = true; +require_once('../modules/init.php'); +require_once(conf('prefix') . '/lib/Browser.php'); + + +/* These parameters has better come on the url. */ +$uid = htmlspecialchars($_REQUEST['uid']); +$song_id = htmlspecialchars($_REQUEST['song']); +$sid = htmlspecialchars($_REQUEST['sid']); + +/* Misc Housework */ +$dbh = dbh(); +$user = new User(0,$uid); + +if (conf('require_session') && !conf('xml_rpc')) { + if(!session_exists($sid)) { + die(_("Session Expired: please log in again at") . " " . conf('web_path') . "/login.php"); + } + + // Now that we've confirmed the session is valid + // extend it + extend_session($sid); +} + +/* If we are in demo mode.. die here */ +if (conf('demo_mode') || !$user->has_access('25')) { + if (conf('debug')) { + log_event($user->usrename,' access_denied ', "Streaming Access Denied, " . conf('demo_mode') . "is the value of demo_mode. Current user level is $user->access"); + } +// access_denied(); +} + +/* + If they are using access lists let's make sure + that they have enough access to play this mojo +*/ +if (conf('access_control')) { + + $access = new Access(0); + if (!$access->check("25", $_SERVER['REMOTE_ADDR'])) { + if (conf('debug')) { + log_event($user->username,' access_denied ', "Streaming Access Denied, " . $_SERVER['REMOTE_ADDR'] . " does not have stream level access"); + } + access_denied(); + } + +} // access_control is enabled + + +// require a uid and valid song +if ( isset( $uid ) ) { + $song = new Song($song_id); + $song->format_song(); + + // Create the user object if possible + + if (!$song->file OR ( !is_readable($song->file) AND $catalog->catalog_type != 'remote' ) ) { + if (conf('debug')) { + log_event($user->username,' file_not_found ',"Error song ($song->title) does not have a valid filename specified"); + } + echo "Error: No Song"; + exit; + } + if ($song->status === 'disabled') { + exit; + } + if ($user->access === 'disabled') { + if (conf('debug')) { + log_event($user->username,' user_disabled ',"Error $user->username is currently disabled, stream access denied"); + } + echo "Error: User Disabled"; + exit; + } + if (!$user->id && !$user->is_xmlrpc()) { + if (conf('debug')) { + log_event($user->username,' user_not_found ',"Error $user->id not found, stream access denied"); + } + echo "Error: No User Found"; + exit; + } + +} +else { + if (conf('debug')) { + log_event("Unknown", ' user_not_found ',"Error no UID passed with URL, stream access denied"); + } + echo "Error: No UID specified"; + exit; +} + +$catalog = new Catalog($song->catalog); + +if ( $catalog->catalog_type == 'remote' ) { + // redirect to the remote host's play path + header("Location: $song->file"); +} +else { + if ($user->prefs['play_type'] == 'downsample') { + $ds = $user->prefs['sample_rate']; + } + + // 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); + + // If we are running in Legalize mode, don't play songs already playing + if (conf('lock_songs') == 'true') { + $sql = "SELECT COUNT(*) FROM now_playing" . + " WHERE song_id = '$song_id'"; + $db_result = mysql_query($sql, $dbh); + while ($r = mysql_fetch_row($db_result)) { + if ($r[0] == 1) { + // Song is already playing, so exit without returning song + exit; + } + } + } + + // Put this song in the now_playing table + $end_time = time() - $song->time; + $sql = "INSERT INTO now_playing (`song_id`, `user_id`, `start_time`)" . + " VALUES ('$song_id', '$uid', '$end_time')"; + $db_result = mysql_query($sql, $dbh); + $lastid = mysql_insert_id($dbh); + + // make fread binary safe + set_magic_quotes_runtime(0); + + // don't abort the script if user skips this song because we need to update now_playing + ignore_user_abort(TRUE); + + /* Format the Song Name */ + if (conf('stream_name_format')) { + $song_name = conf('stream_name_format'); + $song_name = str_replace("%basename",basename($song->file),$song_name); + $song_name = str_replace("%filename",$song->file,$song_name); + $song_name = str_replace("%type",$song->type,$song_name); + $song_name = str_replace("%catalog",$catalog->name,$song_name); + $song_name = str_replace("%A",$song->f_album_full,$song_name); // this and next could be truncated version + $song_name = str_replace("%a",$song->f_artist_full,$song_name); + $song_name = str_replace("%C",$catalog->path,$song_name); + $song_name = str_replace("%c",$song->comment,$song_name); + $song_name = str_replace("%g",$song->f_genre,$song_name); + $song_name = str_replace("%T",$song->track,$song_name); + $song_name = str_replace("%t",$song->title,$song_name); + $song_name = str_replace("%y",$song->year,$song_name); + } + else { + $song_name = $song->f_artist_full . " - " . $song->title . "." . $song->type; + } + + + + // Send file, possible at a byte offset + $fp = @fopen($song->file, 'r'); + + if (!is_resource($fp)) { + if (conf('debug')) { + log_event($user->username,' file_read_error ',"Error: Unable to open $song->file for reading"); + } + cleanup_and_exit($lastid); + } + + $startArray = sscanf( $_SERVER[ "HTTP_RANGE" ], "bytes=%d-" ); + $start = $startArray[0]; + + // Generate browser class for sending headers + $browser = new Browser(); + header("Accept-Ranges: bytes" ); + header("Content-Length: " . $song->size); + + // Prevent the script from timing out + set_time_limit(0); + + if ($ds) { + $ds = $user->prefs['sample_rate']; + $dsratio = $ds/$song->bitrate*1024; + $browser->downloadHeaders($song_name, $song->mime, false,$dsratio*$song->size); + + /* Get Offset */ + $offset = ( $start*$song->time )/( $dsratio*$song->size ); + $offsetmm = floor($offset/60); + $offsetss = floor($offset-$offsetmm*60); + $offset = sprintf("%02d.%02d",$offsetmm,$offsetss); + + /* Get EOF */ + $eofmm = floor($song->time/60); + $eofss = floor($song->time-$eofmm*60); + $eof = sprintf("%02d.%02d",$eofmm,$eofss); + + /* Replace Variables */ + $downsample_command = conf('downsample_cmd'); + $downsample_command = str_replace("%FILE%",$song->file,$downsample_command); + $downsample_command = str_replace("%OFFSET%",$offset,$downsample_command); + $downsample_command = str_replace("%EOF%",$eof,$downsample_command); + $downsample_command = str_replace("%SAMPLE%",$ds,$downsample_command); + + // If we are debugging log this event + if (conf('debug')) { + $message = "Exec: $downsample_command"; + log_event($user->username,'downsample',$message); + } // if debug + + $fp = @popen($downsample_command, 'r'); + } // if downsampling + + elseif ($start) { + $browser->downloadHeaders($song_name, $song->mime, false, $song->size); + fseek( $fp, $start ); + $range = $start ."-". $song->size . "/" . $song->size; + header("HTTP/1.1 206 Partial Content"); + header("Content-Range: bytes=$range"); + } + /* Last but not least pump em out */ + else { + $browser->downloadHeaders($song_name, $song->mime, false, $song->size); + } + + + while (!feof($fp) && (connection_status() == 0)) { + print(fread($fp, 8192)); + } + + if ( ! $start ) { + $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); + } + + // Remove the song from the now_playing table + $sql = "DELETE FROM now_playing WHERE id = '$lastid'"; + $db_result = mysql_query($sql, $dbh); + + /* Clean up any open ends */ + if ($ds) { + @pclose($fp); + } + else { + @fclose($fp); + } + +} + +?> diff --git a/play/pupload.php b/play/pupload.php new file mode 100644 index 00000000..2cc73f10 --- /dev/null +++ b/play/pupload.php @@ -0,0 +1,227 @@ +<?php
+/*
+
+ Copyright (c) 2001 - 2005 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.
+
+*/
+/*
+
+ This is the wrapper for opening music streams from this server. This script
+ will play the local version or redirect to the remote server if that be
+ the case. Also this will update local statistics for songs as well.
+
+*/
+
+$no_session = true;
+require_once('../modules/init.php');
+require_once(conf('prefix') . '/lib/Browser.php');
+
+$browser = new Browser();
+
+$dbh = setup_sess_db("song",
+ libglue_param('mysql_host'),
+ libglue_param('mysql_db'),
+ libglue_param('mysql_user'),
+ libglue_param('mysql_pass')
+ );
+
+/* These parameters has better come on the url. */
+$song = htmlspecialchars($_REQUEST['song']);
+$song_nm = htmlspecialchars($_REQUEST['song']);
+$action = htmlspecialchars($_REQUEST['action']);
+$uid = htmlspecialchars($_REQUEST['uid']);
+$web_path = conf('web_path');
+
+
+/* If we are in demo mode.. die here */
+if (conf('demo_mode')) {
+ exit();
+}
+
+/*
+ If they are using access lists let's make sure
+ that they have enough access to play this mojo
+*/
+if (conf('access_control') == "true") {
+
+ $access = new Access(0);
+ if (!$access->check("25", $_SERVER['REMOTE_ADDR'])) {
+ echo "Error: Access Denied, Invalid Source ADDR";
+ exit();
+ }
+
+} // access_control is enabled
+
+// Get site preferences
+$site = new User(0);
+$site->get_preferences();
+
+
+// require a uid and valid song
+if ( isset( $uid ) ) {
+ // Create the user object if possible
+ $user = new User(0,$uid);
+
+ $song = $site->prefs['upload_dir'] . $song;
+
+ if (!file_exists ( $song )) { echo "Error: No Song"; exit; }
+ if ($user->access === 'disabled') { echo "Error: User Disabled"; exit; }
+ if (!$user->id && !$user->is_xmlrpc()) { echo "Error: No User Found"; exit; }
+
+}
+else {
+ exit;
+}
+
+/* Get file info */
+$audio_info = new Audioinfo();
+$results = $audio_info->Info($song);
+
+$order = conf('id3tag_order');
+
+// set the $key to the first found tag style (according to their prefs)
+foreach($order as $key) {
+ if ($results[$key]) { break; }
+}
+
+// Fetch Song Info
+$artist_name = addslashes($results[$key]['artist']);
+$album_name = addslashes($results[$key]['album']);
+$title = addslashes($results[$key]['title']);
+$song_time = intval($results['playing_time']);
+$size = filesize($song);
+preg_match('/\.([A-Za-z0-9]+)$/', $song,$results);
+
+$type = $results[1];
+
+switch ($type) {
+ case "ogg":
+ $mime = "application/x-ogg";
+ break;
+ case "mp3":
+ case "mpeg3":
+ $mime = "audio/mpeg";
+ case "rm":
+ $mime = "audio/x-realaudio";
+ break;
+}
+
+
+if ( $_REQUEST['action'] == 'm3u' ) {
+
+ if($temp_user->prefs['play_type'] == 'local_play') {
+ // Play the song locally using local play configuration
+ $song_name = $artist . " - " . $title . "." . $type;;
+ $sess = $_COOKIE[libglue_param('sess_name')];
+ //echo "Song Name: $song_name<BR>\n";
+ $url = escapeshellarg("$web_path/play/pupload.php?song=$song_nm&uid=$user->id&sid=$sess");
+ $localplay_add = conf('localplay_add');
+ $localplay_add = str_replace("%URL%", $url, $localplay_add);
+ //echo "Executing: $localplay_add<BR>";
+ exec($localplay_add);
+ header("Location: $web_path");
+
+ }
+ else
+ {
+
+ if (conf('force_http_play')) {
+ $http_port = conf('http_port');
+ $web_path = preg_replace("/https/","http",$web_path);
+ $web_path = preg_replace("/:\d+/",":$http_port",$web_path);
+ }
+
+ // 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";
+
+ $song_name = $song . " - " . $title . "." . $type;
+ $song_name = $artist . " - " . $title . "." . $song->type;;
+ echo "#EXTINF:$song_time,$title\n";
+ $sess = $_COOKIE[libglue_param('sess_name')];
+ if($temp_user->prefs['down-sample'] == 'true')
+ $ds = $temp_user->prefs['sample_rate'];
+ echo "$web_path/play/pupload.php?song=" . rawurlencode($song_nm) . "&uid=$user->id&sid=$sess";
+
+ }
+ exit;
+}
+else
+{
+ // Check to see if we should be downsampling
+ $user->get_preferences();
+
+ if ($user->prefs['play_type'] == 'downsample') {
+ $ds = $user->prefs['sample_rate'];
+ }
+
+ // make fread binary safe
+ set_magic_quotes_runtime(0);
+
+ // don't abort the script if user skips this song because we need to update now_playing
+ ignore_user_abort(TRUE);
+
+ // Build the Song Name
+ $song_name = $artist_name . " - " . $title . "." . $type;
+
+ // Send headers
+ $browser->downloadHeaders($song_name, $mime, false, $size );
+ header("Accept-Ranges: bytes" );
+
+ // Send file, possible at a byte offset
+ $fp = fopen($song, 'r');
+
+ $startArray = sscanf( $_SERVER[ "HTTP_RANGE" ], "bytes=%d-" );
+ $start = $startArray[0];
+
+ // Prevent the script from timing out
+ set_time_limit(0);
+
+ if ($ds) {
+ // FIXME:: $dsratio needs to be set
+ $ds = $user->prefs['sample_rate']; //ds hack
+ $dsratio = $ds/$song->bitrate*1000; //resample hack
+ $browser->downloadHeaders($song_name, $mime, false,$dsratio*$size);
+ $offset = ( $start*$song_time )/( $dsratio*$size );
+ $offsetmm = floor($offset/60);
+ $offsetss = floor($offset-$offsetmm*60);
+ $offset = sprintf("%02d.%02d",$offsetmm,$offsetss);
+ $cmd = "mp3splt -qnf \"$song->file\" $offset EOF -o - | lame --mp3input -q 3 -b $ds -S - -";
+ if (!$handle = fopen('/tmp/ampache.log', 'a'));
+ fwrite($handle,"$offset |");
+ fclose($handle);
+ $fp = popen($cmd, 'r');
+ $close='pclose';
+ } // if downsampling
+
+ elseif ($start) {
+ fseek( $fp, $start );
+ header("Content-Range: bytes=$start-$size/$song");
+ }
+
+ while (!feof($fp) && (connection_status() == 0)) {
+ print(fread($fp, 8192));
+ }
+
+ fclose($fp);
+
+}
+
+?>
|