/************************************************************************************* * Copyright (c) 2006-2009, Jean Gourd * * 2006-03-17 * * Last updated: 2007-02-23 * * * * MP3Stream v0.9rc1 * * Allows a directory of MP3s to be streamed over the Internet. * * Also has several basic playlist options. * * * * Changelog: * * --------- * * v0.9rc1 Initial release * * * * >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