<HTML><HEAD>
<TITLE>Dave's MP3 Server</TITLE>
</HEAD>
<BODY>
<H1>Play Music!</H1>
<?
# This PHP script is Copyright (c) 2001 David L. Sifry
#
#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.
#
#For a copy of the license, please go to: http://www.gnu.org/copyleft/gpl.html
#
$version = '1.2';
$mp3dir = "/home/music/pub";
$dir = stripslashes($dir);
#echo "Dir=$dir<BR>\n";
if (!ereg("^/?(([-_\'\"a-zA-Z0-9 ]+)/?)+$",$dir)) {
# echo "ouch.<BR>\n";
$dir = "";
}
$fulldir = $mp3dir . $dir;
if (!is_dir($fulldir)) {
$fulldir = $mp3dir;
}
musicmenu();
switch ($op) {
case 'play':
playsong($fulldir,$file);
break;
case 'playdir':
playdir($fulldir,$dir);
break;
case 'stop':
stopmusic();
break;
case 'mute':
mutemusic();
break;
case 'unmute':
unmutemusic();
break;
default:
displaydir($mp3dir,$dir,$fulldir);
$displayed=1;
break;
}
if ($displayed==0) {
displaydir($mp3dir,$dir,$fulldir);
}
function displaydir($mp3dir,$dir,$fulldir)
{
$d = dir($fulldir);
#echo "Handle: ".$d->handle."<br>\n";
#echo "Path: ".$d->path."<br>\n";
$i=0;
while($entry=$d->read()) {
$file[$i++] = $entry;
}
$d->close();
sort($file);
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode("$dir") ."&op=playdir'>Play All Songs In Directory</A><BR>\n";
for ($j=0;$j<$i;$j++) {
$entry = $file[$j];
$fullpath = $fulldir . '/' . $entry;
if (is_dir($fullpath)) {
if ($entry == '.')
continue;
if ($entry == '..') {
$updir = dirname($dir);
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode("$updir") ."'>Go up</A><P>\n";
}
else
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode("$dir/$entry") ."'>$entry</A><BR>\n";
}
else {
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode($dir) . "&file=" . urlencode($entry) ."&op=play'>$entry</A><BR>\n";
}
}
}
function playdir($fulldir)
{
$d = dir($fulldir);
#echo "Handle: ".$d->handle."<br>\n";
#echo "Path: ".$d->path."<br>\n";
$i=0;
while($entry=$d->read()) {
$file[$i++] = $entry;
}
$d->close();
sort($file);
echo "Playing $dir...<BR>\n";
$cmdstring = "";
for ($j=0;$j<$i;$j++) {
$fullpath = $fulldir . "/" . $file[$j];
if (is_file($fullpath)) {
$s = addslashes($fullpath);
# $s = escapeshellarg($s);
$s = ereg_replace(' ','\ ',$s);
$s = preg_replace('/\(/','\(',$s);
$s = preg_replace('/\)/','\)',$s);
$cmdstring .= $s . ' ';
}
}
# echo "Playing $cmdstring...<BR>\n";
# system("export HOME=/home/httpd; /usr/bin/mpg123 $cmdstring 2>&1 &");
system("export HOME=/home/httpd; /usr/bin/mpg123 $cmdstring > /dev/null &");
}
function playsong($fulldir,$file)
{
$file = stripslashes($file);
$fullpath = $fulldir . "/" . $file;
if (is_file($fullpath)) {
echo "Playing $file...<BR>\n";
$s = addslashes($fullpath);
# $s = escapeshellarg($s);
$s = ereg_replace(' ','\ ',$s);
$s = preg_replace('/\(/','\(',$s);
$s = preg_replace('/\)/','\)',$s);
# echo "Playing $s...<BR>\n";
# system("export HOME=/home/httpd; /usr/bin/mpg123 $s 2>&1 &");
system("export HOME=/home/httpd; /usr/bin/mpg123 $s > /dev/null &");
}
}
function musicmenu()
{
global $dir;
echo "<P>\n";
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode($dir) ."&op=stop'>Stop</A><BR>\n";
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode($dir) ."&op=mute'>Mute</A><BR>\n";
echo "<A HREF='$SCRIPT_NAME?dir=". urlencode($dir) ."&op=unmute'>Unmute</A><BR>\n";
}
function stopmusic()
{
system("/usr/bin/killall mpg123");
echo "Music Stopped.<BR>\n";
}
function mutemusic()
{
system("/usr/bin/aumix -v 0");
echo "Muted.<BR>\n";
}
function unmutemusic()
{
system("/usr/bin/aumix -v 100");
echo "Unmuted.<BR>\n";
}
echo "<P><A HREF='http://www.sifry.com/mp3servercontrol/mp3servercontrol.phps'>Dave's MP3 Server</A> V$version\n";
?>
</BODY></HTML>