/************************************************************\
*
* music Randomiser Copyright 2005 Howard Yeend
* www.puremango.co.uk
*
* This file is part of music Randomiser.
*
* music Randomiser 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.
*
* music Randomiser 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 music Randomiser; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
\************************************************************/
$version = "1.00";
/*
Music Randomiser
a PHP script that will play random files from
a specified directory
this script has -only- been tested with the
following configuration:
-Winamp 5.02,
-PHP 5.0.2,
-Apache 1.3.29,
-Win2kPro
it is customisable, and may work with other
media players. (though winamp is the best)
it is designed for internal network use ONLY,
and if placed online, may compromise the security
and/or stability of your webserver.
and yes, you can achieve all of it's functionality
using winamp's internal 'randomise list' or
'shuffle' features. the idea is that you might
not want to load a 40000 track playlist into
winamp every time you want a random track.
visit puremango.co.uk for more amazing scripts.
*/
// CUSTOMISABLE VARS:
// the webserver this file is on
// eg; 127.0.0.1 or www.myhost.com - no http or forward slashes
$hostname = "127.0.0.1";
// the name of this file,
// eg; random_track.php or stuff/random_track.php - no leading slash
$filename = "random_track.php";
// where are your files?
// eg; c:\music - no trailing backslash
$path_to_music = "e:\moosic";
// which type of file are you using?
// eg; mp3 - sorry, multiple types not supported.
// use "*" to search for files of any type.
$filetype = "mp3";
// where is your media player?
// eg; c:\PROGRA~1\Winamp\winamp.exe - must be a DOS compatible path;
// i.e. "Program Files" becomes "progra~1"
$path_to_media_player = "c:\PROGRA~1\Winamp\winamp.exe";
// what's it called?
// eg; Winamp, or Windows Media Player - doesn't really matter what you call it.
$media_player_name = "Winamp";
// what -don't- you want to be played?
// eg; ("\\Theme Tunes\\","Merzbow")
// the example above will not play anything in the folder "Theme Tunes"
// or anything with "Merzbow" in the path or file name
// add as many extra phrases as you need
// to exclude nothing use this line instead of the below: $exclude_list = Array();
$exclude_list = Array("\\TV Themes\\","\\speeches\\","robert miles","Merzbow","\\drop\\","pet shop boys");
// END CUSTOMISABLE VARS
// you shouldn't need to change anything below.
// (but I bet you'll try to)
session_start();
// format some of the vars:
$path_to_media_player = addslashes($path_to_media_player);
$media_player_name = ucfirst($media_player_name);
?>
=$media_player_name?> Randomiser v=$version?>
=$media_player_name?> Randomiser v=$version?> - www.puremango.co.uk
// if this is the first time it's been run, set up the played tracks array
if(!isset($_SESSION['played_tracks']))
{
$_SESSION['played_tracks'] = Array();
}
function my_filesize($file)
{
// give rough size in format x.xxMb
$size_in_bytes = filesize($file);
$size_in_kb = intval($size_in_bytes/1024);
$size_in_mb = substr($size_in_kb,0,strlen($size_in_kb)-3);
$size_in_mb = (empty($size_in_mb)) ? 0 : $size_in_mb;
return $size_in_mb.".".substr($size_in_kb,-3,2)."Mb";
}
function excluded($track)
{
// returns true if excluded, false if not
global $exclude_list;
// don't let us randomly choose the same track more than once.
$exclude_list = array_merge($exclude_list,$_SESSION['played_tracks']);
foreach($exclude_list as $needle)
{
if(stripos($track,$needle)!==false)
{
return true;
}
}
return false;
}
function display_old_tracks()
{
// give list of all played tracks, most recently played first.
$played_tracks = array_reverse($_SESSION['played_tracks']);
?>
// unless we've just stopped, start at 1 to avoid listing the currently playing track.
$start = (isset($_GET['stop'])) ? 0 : 1;
for($i=$start ; $iPlayed: | \"".stripslashes($played_tracks[$i])."\" | ";
}
?>
}
function http_request($url,$file,$extraHeaders="")
{
// sends a HTTP request to http://$url/$file, sending any $extraHeaders required
// returns true on success, false on failure.
// initialise some variables
$headersEnded = 0;
// open connection
$fsock = @fsockopen($url, 80,$errno, $errstr,30);
if(!$fsock)
{
// if you're having problems connecting, uncomment this line:
//echo "
$errstr ($errno)
";
return false;
} else {
// prepare headers to send
$out = "GET $file HTTP/1.0\r\n";
$out .= "Host: $url\r\n";
$out .= "Referer: http://$url\r\n";
// ask server to return data in plain ascii
$out .= "Accept-Encoding: \r\n";
// add anything else (eg cookie):
$out .= $extraHeaders;
$out .= "Connection: close\r\n";
// terminate headers
$out .= "\r\n";
// send headers to server
fputs($fsock, $out);
// this is where we would receive the response - for this application, we don't want to
// in fact, we MUST not.
// close connection
fclose($fsock);
return true;
}
}
// are we supposed to be playing a track?
if(isset($_GET['play']))
{
if(!empty($_GET['playtrack']))
{
// actually run the media player
passthru($path_to_media_player." \"".stripslashes(base64_decode($_GET['playtrack']))."\"");
// PHP hangs until the media player closes, so the exit() here never gets executed - it's just to remind me that after the passthru we may as well have exited as the effect is the same.
exit();
} else {
// get track and call self to play track
if(!isset($_GET['track']))
{
// choose random track and escape path info
// get dir list as an array
$dir_list = `dir /b/s ${path_to_music}\\*.$filetype`;
$dir_list = explode("\n",$dir_list);
do {
// keep choosing random tracks until we find one that is not excluded.
srand((float) microtime() * 10000000);
$key = array_rand($dir_list);
$track = addslashes($dir_list[$key]);
} while(excluded($track));
} else {
// we were sent a specific track to play, from the history list.
$track = base64_decode($_GET['track']);
// tracks sent this way bypass the excluded list; so a user can choose to play the same track twice.
}
// tell me what track it is, and give link to stop playing
echo "Now playing: \"".stripslashes($track)."\" (".my_filesize(stripslashes($track)).")";
echo "
stop ";
// remember track
$_SESSION['played_tracks'][] = $track;
// call self to run the media player
// this is because running the media player causes PHP to hang and we can't output playlist and stop/play list to the user if PHP is waiting for the media player to exit before sending output.
// this problem only exists within windows.
http_request($hostname,"/".$filename."?play&playtrack=".base64_encode($track));
}
} else if(isset($_GET['stop'])){
// doesn't actually stop it, instead, tries to play a non-existant file.
http_request($hostname,"/".$filename."?play&playtrack=foo.bar");
// AFAIK, the process will actually stay alive until apache is re-started.
// (it serves my purposes, that is all.)
echo $media_player_name." stopped
";
}
?>
play
display_old_tracks();
?>