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

### revise.php ###
// makes specified changes to playlist

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

$playlist_url = rawurlencode(stripslashes($playlist));
$playlist = stripslashes(rawurldecode($playlist));

//echo "<del is ".$del.">\n";

if(isset($del) || $rename != "" || isset($shuffle) || isset($sort)) {

  // deletes elements of the playlist that were selected
  if( isset($del) && (sizeof($del) > 0)) {

    // gets an array of songs in the playlist
    $song_list = explode("\n", ($Brunhilde[$playlist]));

    // holds the contents of the new playlist
    $new_song_list = array();

    // loops through grabbing only elements that weren't selected
    for($i=0, $n=0; $i<sizeof($song_list); $i++) {
      // if the song is to be deleted, does nothing
      if(($n < sizeof($del)) && ($i == $del[$n])) {
        $n++;
	// passes the following $base variable after the entry
	$i++;
	}
      // if the song was to be kept, it is added to the new playlist
      else { 
        array_push($new_song_list, $song_list[$i]);
	// grabs the $base variable after the playlist entry
	$i++;
	array_push($new_song_list, $song_list[$i]);
	}
      }

    // single string representation of new cookie contents
    $list_contents = "";

    // pulls the contents out of the array and concatinates them as a string
    for($i = 0; $i < sizeof($new_song_list); $i++) {
      $list_contents = $list_contents.stripslashes($new_song_list[$i])."\n";
      // handles the following $base variable
      $i++;
      // keeps "\n" from being added to the last entry in the playlist
      if($i == sizeof($new_song_list) - 1) {
        $list_contents = $list_contents.stripslashes($new_song_list[$i]);
        }
      // makes sure that the "\n" is added to separate playlist entries
      else {
        $list_contents = $list_contents.stripslashes($new_song_list[$i])."\n";
        }
      }
    }
  else { $list_contents = stripslashes($Brunhilde[$playlist]); }

  // possibly deletes the old playlist and creates name for the new one
  if($rename != "") {
    $rename_url = rawurlencode(stripslashes($rename));
    setcookie("Brunhilde[$playlist_url]", "", time() -3600);
    }
  else {
    $rename_url = $playlist_url;
    }

  
  // if the user has chosen the "shuffle playlist" option from the playlist edit
  if(isset($shuffle)) {
    $tmp_contents = explode("\n", $list_contents);
    // arranges the playlist into a 2 dimensional array (to shuffle)
    $tmp_2d_array = array();
    for($i=0; $i < sizeof($tmp_contents); $i++) {
      // the song
      $song = $tmp_contents[$i];
      // the $base variable
      $i++;
      $base_var = $tmp_contents[$i];
      array_push($tmp_2d_array, array($song, $base_var));
      }
    
    // shuffles the array
    $tmp_2d_array = shuffle_array($tmp_2d_array);
    // string will hold the contents
    $list_contents = "";
    // pulls the contents out of the array and concatinates them as a string
    for($i=0; $i < sizeof($tmp_2d_array); $i++) {
      if($i == sizeof($tmp_2d_array) - 1) {
        $list_contents = $list_contents.stripslashes($tmp_2d_array[$i][0])."\n".$tmp_2d_array[$i][1];
        }
      else {
        $list_contents = $list_contents.stripslashes($tmp_2d_array[$i][0])."\n".$tmp_2d_array[$i][1]."\n";
        }
      }
    }

  // if the user has chosen the "sort playlist" option from the playlist edit
  else if(isset($sort)) {
    $tmp_contents = explode("\n", $list_contents);
    $sorted_contents = array(); // pairs entries with their $base values
    for($i=0; $i < count($tmp_contents); $i++) {
      $entry = $tmp_contents[$i];
      // handles the $base variable
      $i++;
      $base_var = $tmp_contents[$i];
      array_push($sorted_contents, array($entry, $base_var));
      }
    sort($sorted_contents);
    $list_contents = "";
    // pulls the contents out of the array and concatinates them as a string
    for($i = 0; $i < sizeof($sorted_contents); $i++) {
      $list_contents = $list_contents.stripslashes($sorted_contents[$i][0]);
      if($i == sizeof($sorted_contents) - 1) {
        $list_contents = $list_contents."\n".$sorted_contents[$i][1];
        }
      else {
        $list_contents = $list_contents."\n".$sorted_contents[$i][1]."\n";
        }
      }
    }
 

  // makes the cookie last until this script is obsolete
  $cookie_time = mktime(0, 0, 0, 12, 30, 2032);
  // writes the new cookie contents and/or cookie name
  setcookie("Brunhilde[$rename_url]", $list_contents, $cookie_time);
  }
  
// redirects the playlist screen back to itself if nothing was done
else { $rename_url = rawurlencode(stripslashes($playlist)); }

// sends the user back to the playlist edit form, showing changes
header("Content-Type: text/html");
header("location: edit.php?edit=1&playlist=$rename_url");

?>
Return current item: Brunhilde Media Server