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

### recurse_add.php ###
// function to recursively add all songs in a directory to a playlist

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

$cdir = stripslashes($cdir);
$cdir_url = rawurlencode($cdir);
$playlist_list = array();
$song_list = recurse_songs("$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");
  }

// adds the songs to the specified playlist
if(isset($pladd)) {
  
  // if the songs are made into a new playlist
  if($pladd == "new") {
    // finds an untaken default name for the playlist
    for($i = 1;;$i++) {
      $pladd = "Playlist $i";
      if(isset($Brunhilde[$playlist])) { continue; }
      else {
        $pladd = "Playlist $i";
        break;
        }
      }
    $pladd_url = rawurlencode($pladd);
    for($i = 0; $i < sizeof($song_list); $i++) {
      $playlist = $playlist.stripslashes(rawurldecode($song_list[$i]))."\n&base=$base";
      // keeps \n from being added to the last element in the playlist
      if($i != sizeof($song_list) - 1) { $playlist = $playlist."\n"; }
      }
    }
  
  // if new songs are added to an existing playlist
  else { 
    $pladd_url = $pladd;
    $pladd = stripslashes(rawurldecode($pladd));
    $playlist = $Brunhilde[$pladd];
    
    // fixes bug if playlist was present but empty
    if($playlist == "") {
      $playlist = stripslashes(rawurldecode($song_list[0]));
      }
    else {
      $playlist = $playlist."\n".stripslashes(rawurldecode($song_list[0]))."\n&base=$base";
      }
    
    for($i = 1; $i < sizeof($song_list); $i++) {
      $playlist = $playlist."\n".stripslashes(rawurldecode($song_list[$i]))."\n&base=$base";
      }
    }

  // 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[$pladd_url]", $playlist, $cookie_time);
  
  // sends the browser to the playlist edit screen
  header("Content-Type: text/html");
  header("location: edit.php?playlist=$pladd_url");
  exit;
  
  }

// creates a form for users to specify a playlist to add the songs to
else {

  read_playlists();  
  
  echo "<html><head><title>Add to Playlist Recursively</title></head>\n";

  include("header.php");

  echo "<br><br>\n";
  echo "<TABLE $BORDER width=100% cellspacing=0 cellpadding=0><TBODY><TR><TD>\n";
  echo "<TABLE width=100% border=0 cellspacing=1 cellpadding=1>\n<TBODY>\n";
  echo"<TR $TITLE><TD width=100% align=center><font size=3><B>Please choose a playlist to add the specified songs to.</B></font><br></TD></TR>\n";
  echo "<TR $BODY><TD align=center width=100%>";
  echo "<form name=pl method=post action=recurse_add.php?cdir=$cdir_url&base=$base>";
  // drop menu with playlist names
  echo "<select name=pladd>\n";
  echo "\t<option value=new>New</option>\n";
  // generates a list of playlists for the menu
  for($i=0; $i<sizeof($playlist_list); $i++) {
    $real_name = rawurldecode($playlist_list[$i]);
    echo "\t<option value=$playlist_list[$i]>$real_name</option>\n";
    }
  echo "</select>\n";
  echo "&nbsp;&nbsp;";
  echo "<input type=submit value=\"submit\"><br>\n";
  echo "</TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>\n";
  
  // displays a list of all songs to be added to the playlist
  echo "<br>\n";
  echo "<TABLE $BORDER width=100% cellspacing=0 cellpadding=0><TBODY><TR><TD>\n";
  echo "<TABLE width=100% border=0 cellspacing=1 cellpadding=1>\n<TBODY>\n";
  echo"<TR $TITLE><TD width=100% align=center><font size=3><B>Songs:</B></font><br></TD></TR>\n";
  echo "<TR $BODY><TD align=left width=100%>";


  echo "<table border=0 width=100% cellpadding=1 cellspacing=0>\n";
  echo "<TBODY>";

  // loops through all of the songs, displaying their names and links
  for($i=0,$number = 1; $i<count($song_list); $i++,$number++) {
    $m3u_url = rawurlencode(stripslashes($song_list[$i]));
    $song_name = stripslashes(rawurldecode($song_list[$i]));
    $song_name_array = explode("/", $song_name);
    $song_name = $song_name_array[sizeof($song_name_array) - 1];
    echo "<tr>";
    echo "<td width=4% align=right>&nbsp;$number.&nbsp;</td>";
    echo "<td width=\"1%\">";
    echo "<a href=\"id3.php?m3u=$m3u_url&base=$base\">";
    echo "<img src=$ID3_ICON alt=\"View File Info\" align=top></a></td>";
    echo "<td width=\"95%\">";
    echo "<a href=\"m3u.php?play=$m3u_url&base=$base\">";
    echo "$song_name</a></td>";
    echo "</input>";
    echo "</tr>\n";
    }

  
  echo "</TBODY></TABLE></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>\n";
  echo "<BR>&nbsp;<BR>";

  include("footer.php");
  
  }

?>
Return current item: Brunhilde Media Server