Please submit bugs to Jean[dot]Gourd[at]usm[dot]edu * * * * Beta Testers: * * ------------ * * Matthew Gambrell * * Jeremy Kackley * * * * Helpful Sources: * * ------------ * * http://www.spartanicus.utvinternet.ie/streaming.htm (m3u on server side) * * Thanks: http://www.budget-ha.com/audio/simple-mp3-streaming (m3u on client side) * * Thanks: http://www.phpfreaks.com/quickcode/Better-Unique-User-ID/23.php (user id) * * * * Please feel free to use this code. I only ask that you leave my copyright notice * * within your source. Hey, it's not such a huge thing to ask, right? * ************************************************************************************* * This 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 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. * * * * To obtain a copy of the GNU General Public License, write to the Free Software * * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * *************************************************************************************/ /************************ * Configurable Options * ************************/ $MP3STREAM_CHILD_FILENAME = "index.php"; // the filename of the stub MP3Stream file located in subdirectories /******************************* * DO NOT EDIT BELOW THIS LINE * *******************************/ $MP3STREAM_VERSION = "0.9rc1"; // user-defined variables $title = "MP3Stream v$MP3STREAM_VERSION"; error_reporting(E_ALL ^ E_NOTICE); // set GET variables if (sizeof($_GET)) { // set the mode // possible modes: play play a specified MP3 // addToList add a specified MP3 to the playlist // playList play the current playlist // clearList clear the playlist // listList list the playlist contents // addAll adds all songs to the playlist // TODO: addAllRandomized adds all songs to the playlist (randomized) if (isset($_GET["mode"])) $mode = $_GET["mode"]; else $mode = "undefined"; // set the MP3 file if (isset($_GET["mp3"])) $mp3 = $_GET["mp3"]; else $mp3 = "undefined"; // randomly set a user id if none already generated if (!isset($_GET["user"])) $user = md5(uniqid(rand(),1)); else $user = $_GET["user"]; } else // set the user id if not specified as GET parameter $user = md5(uniqid(rand(),1)); // set script and url variables $PHP_SELF = $_SERVER["PHP_SELF"]; $baseUrl = "http://" . $_SERVER['HTTP_HOST'] . makePretty(dirname($PHP_SELF)); $fullUrl = "http://" . $_SERVER['HTTP_HOST'] . $PHP_SELF; // set current path $curpath = makePretty(getcwd()); // remove escape characters in MP3 filename $mp3 = str_replace("\\", "", $mp3); // determine the mode if ($mode == "play") { // play a single song: just create an M3U on-the-fly for the user with the MP3 filename // this does not destroy the user's playlist on the server header("Content-disposition: attachment; filename=$user.m3u"); header("Content-type: audio/x-mpegurl"); echo "$baseUrl/" . encode($mp3) . "\n"; } elseif ($mode == "addToList") { // create/append to the user's unique playlist $file = fopen($curpath . "/" . $user . ".m3u", "a"); if (!$file) { htmlHeader($title); echo " ERROR: Could not create/open playlist file.

\n"; htmlFooter(); die; } else { // add the MP3 to the M3U fputs($file, "$baseUrl/" . encode($mp3) . "\n"); fclose($file); // display status htmlHeader($title); echo " $mp3 added.

\n"; } printFiles($curpath, $user, $fullUrl); } elseif ($mode == "playList") { // make sure the user's playlist exists if (!is_file($user . ".m3u")) { htmlHeader($title); echo " No playlist file exists for you.

\n"; printFiles($curpath, $user, $fullUrl); } else { // play the songs in the list: just create an M3U on-the-fly for the user with the contents of the server M3U // this does not destroy the user's playlist on the server header("Content-disposition: attachment; filename=$user.m3u"); header("Content-type: audio/x-mpegurl"); // read from the user's playlist $file = fopen($curpath . "/" . $user . ".m3u", "r"); if (!$file) { htmlHeader($title); echo " ERROR: Could not open playlist file.

\n"; htmlFooter(); die; } else { // echo each line $lines = file($curpath . "/" . $user . ".m3u"); foreach ($lines as $curline) echo "$curline\n"; fclose($file); } } } elseif ($mode == "clearList") { // make sure the user's playlist exists htmlHeader($title); if (!is_file($curpath . "/" . $user . ".m3u")) echo " No playlist file exists for you.

\n"; else { // delete the playlist file unlink($curpath . "/" . $user . ".m3u"); echo " Playlist file cleared.

\n"; } printFiles($curpath, $user, $fullUrl); } elseif ($mode == "listList") { // make sure the user's playlist exists htmlHeader($title); if (!is_file($curpath . "/" . $user . ".m3u")) echo " No playlist file exists for you.

\n"; else { // read from the user's playlist $file = fopen($curpath . "/" . $user . ".m3u", "r"); if (!$file) { echo " ERROR: Could not open playlist file.

\n"; htmlFooter(); die; } else { // echo each line $lines = file($curpath . "/" . $user . ".m3u"); echo " Playlist contents:

\n"; foreach ($lines as $curline) { // remove the baseUrl and format the MP3 $curline = str_replace("%20", " ", str_replace($baseUrl . "/", "", str_replace("\n", "", $curline))); echo " $curline
\n"; } echo "

\n"; fclose($file); } } printFiles($curpath, $user, $fullUrl); } elseif ($mode == "addAll") { // create/append to the user's unique playlist htmlHeader($title); $file = fopen($curpath . "/" . $user . ".m3u", "a"); if (!$file) { echo " ERROR: Could not create/open playlist file.

\n"; htmlFooter(); die; } else { // add the MP3s to the M3U $files = getFiles($curpath, ".mp3"); foreach ($files as $curfile) fputs($file, "$baseUrl/" . encode($curfile) . "\n"); fclose($file); // display status echo " All MP3s added.

\n"; } printFiles($curpath, $user, $fullUrl); } else { // just display the MP3s in the current directory htmlHeader($title); printFiles($curpath, $user, $fullUrl); } /************* * FUNCTIONS * *************/ // displays the files/directories in the specified directory function printFiles($dir, $user, $fullUrl) { $dirs = getDirs($dir); $files = getFiles($dir, ".mp3"); // TODO: make the file listing/html pretty // TODO: add randomized playlist? // list the files echo <<< END Options:

Refresh
Play playlist
Clear playlist
List playlist
Add all songs to playlist

Subdirectories containing streamed MP3s:

END; foreach ($dirs as $curdir) { $curdir2 = urlencode($curdir); echo " $curdir
\n"; } echo "\n
MP3s in this directory:

\n\n"; foreach ($files as $curfile) { $curfile2 = urlencode(str_replace("&", "&", $curfile)); echo <<< END $curfile | Add to playlist
END; } htmlFooter(); } // returns the MP3 files in the current directory function getFiles($dir, $filter) { $files = array(); $dh = opendir($dir); // get the files while (($filename = readdir($dh)) != false) if (!is_dir($filename)) // make sure we work on the MP3s only if (strpos($filename, $filter) !== false) $files[] = str_replace("&", "&", $filename); sort($files); return $files; } // returns the directories with appropriate streamed MP3s function getDirs($dir) { $dirs = array(); $dh = opendir($dir); while (($dirname = readdir($dh)) != false) if (is_dir($dirname) && ($dirname != "." && $dirname != "..")) if (checkDir($dirname)) $dirs[] = $dirname; sort($dirs); return $dirs; } // check if a directory has the MP3Stream stub function checkDir($dir) { global $MP3STREAM_CHILD_FILENAME; $files = getFiles($dir, $MP3STREAM_CHILD_FILENAME); return (count($files) > 0); } // parses a string and replaces all "\" with "/" function makePretty($string) { return str_replace("\\", "/", $string); } // encodes an MP3 filename function encode($mp3) { return str_replace(" ", "%20", $mp3); } // prints the html header stuff function htmlHeader($title) { header("Content-type: text/html; charset=UTF-8"); echo "\n"; echo " \n"; echo " $title\n"; echo " \n"; echo " \n"; } // prints the html footer stuff function htmlFooter() { echo " \n"; echo "\n"; } // prints several variables (for debug purposes) function printVars($PHP_SELF) { $dirname_file = makePretty(dirname(__FILE__)); $dirname_php_self = makePretty(dirname($PHP_SELF)); if ($dirname_php_self != "/") $dirname_php_self .= "/"; $basename_file = basename(__FILE__); $fullpath = $dirname_file . $dirname_php_self . $basename_file; $fullurl = $_SERVER['HTTP_HOST'] . $PHP_SELF; echo "\$PHP_SELF=$PHP_SELF
\n"; echo "\$_SERVER['HTTP_HOST']=" . $_SERVER['HTTP_HOST'] . "
\n"; echo "\$dirname_file=$dirname_file
\n"; echo "getcwd()=" . makePretty(getcwd()) . "
\n"; echo "\$dirname_php_self=$dirname_php_self
\n"; echo "\$basename_file=$basename_file

\n\n"; echo "\$fullpath=$fullpath
\n"; echo "\$fullurl=$fullurl\n"; } ?>