Location: PHPKode > projects > Brunhilde Media Server > brunhilde/m3u.php
<?php

include("global.php");
include("function.php");

### m3u.php ###
// handles all functions associated with building, manipulating, playing m3u files

$dir_list=array(); // array of directories in the current directory
$audio_list=array(); // array of audio files in the current directory
$video_list=array(); // array of video files in the current directory
$image_list=array(); // array of pictures files found in the current directory

// sets url encoded and decoded variables for the rest of the script
if(!isset($cdir)) { $cdir = ""; }
$cdir_url = rawurlencode(stripslashes($cdir));
$cdir = stripslashes(rawurldecode($cdir));

// These lines are ESSENTIAL for the brunhilde server remain secure
// They keep a client from entering "../" to gain access to files
// outside of your web tree
if((substr_count($cdir, "../") > 0) || (substr_count($cdir, "..\\") > 0)) {
  include("security.php");
  }

if(isset($playlist)) {
  $playlist_url = rawurlencode(stripslashes($playlist));
  $playlist = stripslashes(rawurldecode($playlist));
  }

// this is where the script decides what to do

// plays an m3u from the user's cookie
if(isset($playlist)) { play_playlist(); }

// initiates a playlist creation or edit mode
else if(isset($add)) {
  if($pladd == "new") { // new playlist creation
    create_playlist();
    }
  else { // playlist edit mode
    append_playlist();
    }
  }

// if the user submitted multiple songs without a playlist
else if(isset($selected)) { quick_playlist(); }

// if a single song is chosen by clicking on an immediate link
else { single_playlist(); }

exit;

// grabs the playlist contents from the users cookie and sends it in m3u form
function play_playlist() {
  global $Brunhilde;
  global $playlist;
  global $PLAY_DIR;
  global $DOMAIN;
  global $SCRIPT_URL;
  
  // creates an array of playlist entries
  $playlist_contents = explode("\n", $Brunhilde[$playlist]);
  
  // sends header information
  header("Content-Type: audio/x-mpegurl");
  header("Content-Disposition: filename=\"Brunhilde.m3u\"");
  
  // loops through the contents of the playlist
  for($i = 0; $i < sizeof($playlist_contents); $i++) {

    // If the user has defined a different play.php location
    if($PLAY_DIR != "") { $entry = $DOMAIN.$PLAY_DIR."play.php?play="; }
    else { $entry = $SCRIPT_URL."/play.php?play="; }

    $entry = $entry.rawurlencode(stripslashes($playlist_contents[$i]));

    // grabs the base info
    $i++;
    $entry = $entry.$playlist_contents[$i];

    // adds a "\n" character after every playlist entry except the last one
    if($i < sizeof($playlist_contents) - 1) { $entry = $entry."\n"; }

    // outputs the entry
    echo $entry;
    
    }
  }


// writes a new playlist in the users cookie
function create_playlist() {
  global $Brunhilde;
  global $selected;
  global $shuffle;
  global $cdir;
  global $base;
  global $MEDIA_DIR;
  global $audio_list;
  global $SCRIPT_URL;
  $list_contents = ""; // holds the contents to be written to a playlist
  
  // finds an untaken default name for the playlist
  for($i = 1;;$i++) {
    $playlist = "Playlist $i";
    if(isset($Brunhilde[$playlist])) { continue; }
    else {
      $playlist_name = "Playlist $i";
      break;
      }
    }

  read_dir($MEDIA_DIR[$base][1].$cdir);
  
  // shuffles selection if the "shuffle" feature is selected
  if(isset($shuffle)) { $selected = shuffle_array($selected); }
  
  for($i=0; $i<sizeof($selected); $i++) {
    $index=$selected[$i];
    $content = stripslashes("$cdir".$audio_list[$index][0]);
    // adds the base=, variable to the file entry
    // uses another \n as a separator because this part of the entry
    // must not be rawurlencoded for the server to parse correctly
    $content = $content."\n&base=$base";
    // keeps \n from being added to the last element in the playlist
    if($i == sizeof($selected) - 1) {
      $list_contents = $list_contents.stripslashes($content);
      }
    else {
      $list_contents = $list_contents.stripslashes($content)."\n";
      }
    }

  $playlist_name = rawurlencode($playlist_name);
  
  // makes the cookie last until this script is obsolete
  $cookie_time = mktime(0, 0, 0, 12, 30, 2032);
  // writes the playlists contents to the cookie
  setcookie("Brunhilde[$playlist_name]", $list_contents, $cookie_time);

  header("Content-Type: text/html");
  header("location: edit.php?playlist=$playlist_name");
  }

// adds new selections to the playlist
function append_playlist() {
  global $MEDIA_DIR;
  global $audio_list;
  global $Brunhilde;
  global $selected;
  global $base;
  global $cdir;
  global $pladd;
  global $shuffle;
  $playlist = rawurldecode($pladd);
  $playlist_url = $pladd;

  if(sizeof($selected) > 0) {
    $list_contents = stripslashes($Brunhilde[$playlist])."\n";
    read_dir($MEDIA_DIR[$base][1].$cdir);
    
    // shuffles the order of the new songs if the "shuffle" feature is selected
    if(isset($shuffle)) { $selected = shuffle_array($selected); }
    }
  
  for($i=0; $i<sizeof($selected); $i++) {
    $index=$selected[$i];
    $content = $cdir.$audio_list[$index][0];
    // adds the base variable to the playlist entery.  Separated by \n
    $content = $content."\n&base=$base";
    // keeps \n from being added to the last element in the playlist
    if($i == sizeof($selected) - 1) {
      $list_contents = $list_contents.stripslashes($content);
      }
    else {
      $list_contents = $list_contents.stripslashes($content)."\n";
      }
    }
  
  // makes the cookie last until this script is obsolete
  $cookie_time = mktime(0, 0, 0, 12, 30, 2032);
  // writes the new playlist's contents to the cookie
  setcookie("Brunhilde[$playlist_url]", $list_contents, $cookie_time);

  // sends the browser to the playlist's edit screen
  header("Content-Type: text/html");
  header("location: edit.php?playlist=$playlist_url");
  }


// Creates aplaylist on the fly if multiple songs were selected upon submission
function quick_playlist() {
  global $MEDIA_DIR;
  global $audio_list;
  global $base;
  global $cdir;
  global $selected;
  global $shuffle;
  global $PLAY_DIR;
  global $SCRIPT_URL;
  global $DOMAIN;

  // shuffles the mp3 selections if the shuffle option was selected
  if(isset($shuffle)) {
    $selected = shuffle_array($selected);
    }
    
  read_dir($MEDIA_DIR[$base][1].$cdir);
  header("Content-Type: audio/x-mpegurl");
  header("Content-Disposition: filename=\"Brunhilde.m3u\"");
  
  // loops through the selected songs
  for($i=0; $i<sizeof($selected); $i++) {
    $index=$selected[$i];
    
    // stripslashes fixes a bug related to extra "\"s from rawurlencoding.
    $contents = rawurlencode(stripslashes($cdir.$audio_list[$index][0]));
    
    // keeps "\n" from being added to the last entry
    if($i == sizeof($selected) - 1) {
      if($PLAY_DIR != "") { echo "$DOMAIN$PLAY_DIR"."play.php?play=$contents&base=$base"; }
      else { echo "$SCRIPT_URL/play.php?play=$contents&base=$base"; }
      }
    else {
      if($PLAY_DIR != "") { echo "$DOMAIN$PLAY_DIR"."play.php?play=$contents&base=$base\n"; }
      else { echo "$SCRIPT_URL/play.php?type=$type&play=$contents&base=$base\n"; }
      }

    }
  }


// creates a playlist with a single entry
function single_playlist() {
  global $play;
  global $base;
  global $type;
  global $DOMAIN;
  global $PLAY_DIR;
  global $SCRIPT_URL;

  $play = rawurlencode(stripslashes("$play"));
  header("Content-Type: audio/x-mpegurl");
  header("Content-Disposition: filename=\"Brunhilde.m3u\"");
  if($PLAY_DIR != "") {
    echo "$DOMAIN$PLAY_DIR"."play.php?play=$play&base=$base";
    }
  else {
    echo "$SCRIPT_URL/play.php?play=$play&base=$base";
    }
  }

?>
Return current item: Brunhilde Media Server