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

### index.php ###
// displays playlists, and the contents of an mp3 directory
include("global.php");
include("function.php");

$dir_list=array(); // array of directories in a directory
$playlist_list=array(); // array of playlist names located in user cookies
$audio_list=array(); // array of audio files in a directory
$video_list=array(); // array of video files in a directory
$image_list=array(); // array of picture files found in a 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");
  }

// generates a list of playlists from the cookies
read_playlists();

// Creates the webpage
echo "<head>\n";
// Creates a "select all" function in the form
echo "<SCRIPT Language=Javascript>\n";
echo "  function SelectAll() {\n";
echo "    for(var i=0;i<document.songs.elements.length;i++) {\n";
echo "      if(document.songs.elements[i].type == \"checkbox\") {\n";
echo "        if(document.songs.elements[i].name != \"add\" && \n";
echo "           document.songs.elements[i].name != \"shuffle\") {\n";
echo "          document.songs.elements[i].checked = true;\n";
echo "        }\n      }\n    }\n  }\n</SCRIPT>\n";
// Creates the "deselect all" function in the form
echo "<SCRIPT Language=Javascript>\n";
echo "  function DeselectAll() {\n";
echo "    for(var i=0;i<document.songs.elements.length;i++) {\n";
echo "      if(document.songs.elements[i].type == \"checkbox\") {\n";
echo "        if(document.songs.elements[i].name != \"add\" && \n";
echo "           document.songs.elements[i].name != \"shuffle\") {\n";
echo "          document.songs.elements[i].checked = false;\n";
echo "        }\n      }\n    }\n  }\n</SCRIPT>\n";

// Back to the html
echo "<title>Brunhilde mp3 Miniserver</title>\n";
echo "</head>";

// imports the logo and standard links at the top of the page
include("header.php");

// if playlists are found, loops through $playlist_list and displays contents
if(sizeof($playlist_list) > 0) {
  echo "<p>";
  echo "<table border=0 cellpadding=0 cellspacing=2 width=100% $BORDER>\n";
  echo "<tr><td>\n";
  echo "<table border=0 cellpadding=1 cellspacing=1 width=100% $BODY>\n";
  echo "<tr $TITLE><th>Playlists:</th></tr>\n";
  echo "<tr><td>\n";
 
  for($i=0; $i<sizeof($playlist_list); $i++) {
    echo "<table border=0 width=100% cellpadding=0 cellspacing=0>\n";
    echo "<tr>";
    echo "<td width=\"1%\">";
    $playlist = stripslashes(rawurldecode($playlist_list[$i])); // decoded playlist name
    echo "<a href=edit.php?playlist=$playlist_list[$i]><img src=$EDIT_ICON alt=\"Edit Playlist\"></a>&nbsp;</td>";
    echo "<td width=\"99%\">";
    echo "<a href=m3u.php?m3u=1&playlist=$playlist_list[$i]>$playlist</a><br>";
    echo "</table>";
    }
  echo "</td></tr></table></td></tr></table>\n";
  echo "</p>\n";
  }

// if multiple source directories were entered, Brunhilde
// displays a list of them, otherwise, it automatically 
// enters the only one.
if(!isset($base) && (sizeof($MEDIA_DIR) > 1)) {
  DISPLAY_ROOT();
  }

// displays the contents of a folder
else { DISPLAY_CONTENTS(); }


// displays the footer at the bottom
include("footer.php");

echo "</body></html>";

exit;


// Displays a list of media source directories
function DISPLAY_ROOT() {
global $MEDIA_DIR;
global $BORDER;
global $BODY;
global $TITLE;
global $SUBDIR_ICON;
global $HTTP_SERVER_VARS;
global $cdir;
global $cdir_url;

// Lets the user know that they are in the Root Directory
echo "
 <TABLE width=100% border=0 cellpadding=0 cellspacing=2>
  <TBODY>
  <TR>
   <TD width=100% align=center>
    <font size=3>
    <B>Current Directory:&nbsp;&nbsp;&nbsp;'&nbsp;Root Directory&nbsp;'
    </font>
   </TD>
  </TR>
  </TBODY>
 </TABLE>
";

  echo "<p>";
  echo "<table border=0 cellpadding=0 cellspacing=2 width=100% $BORDER>\n";
  echo "<tr><td>\n";
  echo "<table border=0 cellpadding=1 cellspacing=1 width=100% $BODY>\n";
  echo "<tr $TITLE><th>Media Directories:</th></tr>\n";
  echo "<tr><td>\n";

  for($i=0; $i < count($MEDIA_DIR); $i++) {
    echo "<table border=0 cellspacing=0 cellpadding=0 width=100%>\n";
    echo "<tr>";
    echo "<td width=\"1%\">";
    echo "<a href=\"$HTTP_SERVER_VARS[SCRIPT_NAME]?base=$i\"><img src=$SUBDIR_ICON></a>&nbsp;</td>";
    echo "<td width=\"99%\">";
    echo "<a href=\"$HTTP_SERVER_VARS[SCRIPT_NAME]?base=$i\">".$MEDIA_DIR[$i][0]."</a></td></tr>\n";
    echo "</table>\n";
    }
  echo "</td></tr></table></td></tr></table>\n";
  echo "</p>\n";
  }


// Looks in the directory and displays its contents
function DISPLAY_CONTENTS() {
global $AUDIO_TYPE;
global $VIDEO_TYPE;
global $base;
global $cdir;
global $cdir_url;
global $MEDIA_DIR;
global $DOWNLOAD;
global $dir_list;
global $IMAGE_TYPE;
global $playlist_list;
global $image_list;
global $audio_list;
global $video_list;
global $TITLE;
global $BORDER;
global $BODY;
global $SUBDIR_ICON;
global $ID3_ICON;
global $DOWNLOAD_ICON;
global $HTTP_SERVER_VARS;
global $SCRIPT_URL;

// defaults to the first (and only) source directory
if(!isset($base)) { $base = 0; }

// looks at the files in the directory
read_dir($MEDIA_DIR[$base][1].$cdir);

// NAV BAR
// Displays the path of the current directory with links
// to each parent directory in the path
echo "
 <TABLE width=100% border=0 cellpadding=0 cellspacing=2>
  <TBODY>
  <TR>
   <TD width=100% align=center>
    <font size=3><B>Current Directory:&nbsp;&nbsp;&nbsp;'&nbsp;
  ";
// if multiple Media Directories exist, displays the name of
// the current media directory tree at the beginning of the path
if(sizeof($MEDIA_DIR) > 1) {
  echo "<A HREF=\"index.php?base=$base\"><B>".$MEDIA_DIR[$base][0]."</B></A>&nbsp;/&nbsp;";
  }
$cdir_array = explode("/", $cdir);
$cdir_tempurl = "index.php?base=$base&cdir=";
// loops through each member of the directory heirarchy displaying their links
for($i = 0; $i < sizeof($cdir_array) - 1; $i++) {
  // url for parent directory
  $cdir_tempurl = "$cdir_tempurl$cdir_array[$i]/";
  // keeps the current directory from being a link
  if ($i == sizeof($cdir_array) - 2) { echo "<B>$cdir_array[$i]</B>&nbsp;/&nbsp;"; }
  else { echo "<a href=\"$cdir_tempurl\"><B>$cdir_array[$i]</B></a>&nbsp;/&nbsp;"; }
  }

echo "
    '</font>
   </TD>
  </TR>
  </TBODY>
 </TABLE>
";

// if pictures exist in a folder, neatly displays images.
// NOTE: will only display images if option is enabled and a filter is chosen
if(isset($IMAGE_TYPE)) {
  if(count($image_list) > 0) {
    echo "<P ALIGN=CENTER>\n";
    echo "<TABLE BORDER=0 CELLPADDING=0 CELLSPACING=2 WIDTH=95%>\n";
    echo "<TBODY><br><TR>\n";
    echo "<TD ALIGN=CENTER>\n";
  
    // loops through images resizing them and displaying them in rows of upt to 3
    for($i=0, $wrap=1; $i < count($image_list); $i++, $wrap++) {
      $image = $image_list[$i][0];
      $image_url = rawurlencode($image);
      echo "<A HREF=\"image.php?base=$base&cdir=$cdir_url&image=$image_url\">\n";
      echo "<IMG SRC=\"image.php?base=$base&cdir=$cdir_url&image=$image_url\" ALT=\"$image\" WIDTH=170 HEIGHT=155 BORDER=0>\n";
      echo "</A>&nbsp;&nbsp;\n";

      // adds a new row to the table if their are already 
      // 3 images in the current one
      if($wrap == 3) {
        echo "</TD>\n</TR>\n<TR>\n<TD ALIGN=CENTER>\n";
	$wrap = 0;
	}
      }

    //closes out the table
    echo "</TD>\n</TR>\n</TBODY>\n</TABLE>\n</P>\n";
    }
  }

// if subdirectories exist, loops through $dir_list displaying folders
if(count($dir_list) > 0) {
  echo "<p>";
  echo "<table border=0 cellpadding=0 cellspacing=2 width=100% $BORDER>\n";
  echo "<tr><td>\n";
  echo "<table border=0 cellpadding=1 cellspacing=1 width=100% $BODY>\n";
  echo "<tr $TITLE><th>Subdirectories:</th></tr>\n";
  echo "<tr><td>\n";

  for($i=0; $i < count($dir_list); $i++) {
    echo "<table border=0 cellspacing=0 cellpadding=0 width=100%>\n";
    $next_cdir_url = rawurlencode("$cdir$dir_list[$i]");
    echo "<tr>";
    echo "<td width=\"1%\">";
    echo "<a href=\"$HTTP_SERVER_VARS[SCRIPT_NAME]?base=$base&cdir=$next_cdir_url\"><img src=$SUBDIR_ICON></a>&nbsp;</td>";
    echo "<td width=\"99%\">";
    echo "<a href=\"$HTTP_SERVER_VARS[SCRIPT_NAME]?base=$base&cdir=$next_cdir_url\">$dir_list[$i]</a></td></tr>\n";
    echo "</table>\n";
    }
  echo "</td></tr></table></td></tr></table>\n";
  echo "</p>\n";
  }


// if video files exist, loops through $audio_list displaying the files
if(count($video_list) > 0 && isset($VIDEO_TYPE)) {
  
  // builds the file listing
  echo "<p>";
  echo "<table border=0 cellpadding=0 cellspacing=2 width=100% $BORDER>\n";
  echo "<tr><td>\n";
  echo "<table border=0 cellpadding=1 cellspacing=1 width=100% $BODY>\n";
  echo "<tr $TITLE><th>Songs:</th></tr>\n";
  echo "<tr><td>\n";

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

  for($i=0,$number = 1; $i<count($video_list); $i++,$number++) {
    $file_url = $cdir_url.$video_list[$i][0];
    $file_url = rawurlencode(stripslashes($file_url));
    $filename = stripslashes($video_list[$i][0]);
    // retrieves the filetype (mpg, mov, etc... )
    $type = $video_list[$i][1];
    // determines which script will build the appropriate playlist file
    $playlist_script = $VIDEO_TYPE[$type][3];
    echo "<tr>";
    echo "<td width=1% align=right>&nbsp;$number.&nbsp;</td>";
    echo "<td width=\"1%\">";
    echo "<a href=\"id3.php?m3u=$file_url&base=$base\">";
    echo "<img src=$ID3_ICON alt=\"View File Info\" align=top></a>&nbsp;</td>";
    echo "<td width=\"97%\">";
    echo "<a href=\"$playlist_script?play=$file_url&base=$base\">";
    echo stripslashes($filename)."</a>";

    // optionally adds a download button to each file
    if($DOWNLOAD == 1) {
      echo "&nbsp;&nbsp;&nbsp;";
      echo "<a href=$SCRIPT_URL/download.php?play=$file_url&base=$base>";
      echo "<img src=$DOWNLOAD_ICON></a>";
      }

    echo "</td>\n</tr>\n";
    }

  // closes out the file table
  echo "</TBODY></TABLE></td></tr></table></td></tr></table>\n";
  }

  

// if audio files exist, loops through $audio_list displaying the files
if(count($audio_list) > 0 && isset($AUDIO_TYPE)) {
  
  // initiates the form for building playlists
  echo "<form name=songs method=post action=\"m3u.php?base=$base&cdir=$cdir_url\">";

  // builds the file listing
  echo "<p>";
  echo "<table border=0 cellpadding=0 cellspacing=2 width=100% $BORDER>\n";
  echo "<tr><td>\n";
  echo "<table border=0 cellpadding=1 cellspacing=1 width=100% $BODY>\n";
  echo "<tr $TITLE><th>Songs:</th></tr>\n";
  echo "<tr><td>\n";

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

  for($i=0,$number = 1; $i<count($audio_list); $i++,$number++) {
    $file_url = $cdir_url.$audio_list[$i][0];
    $file_url = rawurlencode(stripslashes($file_url));
    $filename = stripslashes($audio_list[$i][0]);
    // retrieves filetype (mp3, ogg, etc... )
    $type = $audio_list[$i][1];
    // determines which script will build the appropriate playlist file
    $playlist_script = $AUDIO_TYPE[$type][3];
    echo "<tr>";
    echo "<td width=\"1%\">";
    echo "<input type=checkbox name=selected[] value=\"$i\">&nbsp;</td>";
    echo "<td width=1% align=right>&nbsp;$number.&nbsp;</td>";
    // the id3 info icon/link
    echo "<td width=\"1%\">";
    echo "<a href=\"id3.php?m3u=$file_url&base=$base\">";
    echo "<img src=$ID3_ICON alt=\"View File Info\"></a>&nbsp;</td>";
    // the file name/link
    echo "<td width=\"97%\">";
    echo "<a href=\"$playlist_script?play=$file_url&base=$base\">";
    echo stripslashes($filename)."</a>";

    // optionally adds a download button to each song
    if($DOWNLOAD == 1) {
      echo "&nbsp;&nbsp;&nbsp;";
      echo "<a href=$SCRIPT_URL/download.php?play=$file_url&base=$base>";
      echo "<img src=$DOWNLOAD_ICON></a>";
      }

    echo "</td>";
    echo "</input>";
    echo "</tr>\n";
    }

  echo "</TBODY></TABLE></td></tr></table></td></tr></table>\n";

  // "select all" and "deselect all" button
  echo "<input type=button value=\"Select All\" onclick=SelectAll();>&nbsp;";
  echo "<input type=button value=\"Deselect All\" onclick=DeselectAll();><br>";
  echo "</p>\n";

  // displays the 'recurse all' option
  echo "<TABLE><TBODY><TR>";
  echo "<TD width=1% align=left><a href=recurse.php?cdir=$cdir_url&base=$base>Play&nbsp;All&nbsp;Songs&nbsp;Recursively&nbsp;&nbsp;</a></TD>";
  echo "<TD width=1% align=left><a href=recurse.php?cdir=$cdir_url&base=$base&shuffle=1>Shuffle&nbsp;All&nbsp;Songs&nbsp;Recursively&nbsp;&nbsp;</a></TD>";
  echo "<TD width=1% align=left><a href=recurse_add.php?cdir=$cdir_url&base=$base>Add&nbsp;All&nbsp;Songs&nbsp;To&nbsp;Playlist&nbsp;Recursively</a></TD>";
  echo "</TR></TBODY></TABLE>";


  // Form submission elements
  echo "<br><input type=checkbox name=shuffle> Shuffle<br>\n";

  // playlist creation options
  echo "<br><input type=checkbox name=add> Add To Playlist\n";
  // 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><br>\n";
  echo "<br><input type=submit value=\"Submit List\"><br>\n";
  echo "</form>\n";
  echo "</table>\n";
  }
else {
  // displays the 'recurse all' option if there were no immediate audio
  // files in the directory
  echo "<TABLE><TBODY><TR>";
  echo "<TD width=1% align=left><a href=recurse.php?cdir=$cdir_url&base=$base>Play&nbsp;All&nbsp;Songs&nbsp;Recursively&nbsp;&nbsp;</a></TD>";
  echo "<TD width=1% align=left><a href=recurse.php?cdir=$cdir_url&base=$base&shuffle=1>Shuffle&nbsp;All&nbsp;Songs&nbsp;Recursively&nbsp;&nbsp;</a></TD>";
  echo "<TD width=1% align=left><a href=recurse_add.php?cdir=$cdir_url&base=$base>Add&nbsp;All&nbsp;Songs&nbsp;To&nbsp;Playlist&nbsp;Recursively</a></TD>";
  echo "</TR></TBODY></TABLE>";
  }
}

?>
Return current item: Brunhilde Media Server