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

### function.php ###
// stores functions that are used by more than one script

// adds a directory's contents to the appropriate global array
function read_dir($cdir) {
  global $dir_list;
  global $image_list;
  global $audio_list;
  global $video_list;
  global $FILTER;

  $d = opendir($cdir);
  while($contents = readdir($d)) { //loops through all of the directories contents
    if($contents == "." || $contents == "..") { continue; }
    // if a folder is found
    if(is_dir("$cdir$contents")) {
      array_push($dir_list, $contents."/");
      }
    // if a file is found, possibly uses filters
    else {
      $filter_result = apply_filter($contents, "$cdir$contents");

      // adds an array representing audio files (filename, type), to $audio_type
      if($filter_result[0] == 1) {
        array_push($audio_list, array($contents, $filter_result[1]));
	}

      // adds an array representing video files (filename, type) to $video_list	
      else if($filter_result[0] == 2) {
        array_push($video_list, array($contents, $filter_result[1]));
	}

      // adds an array representing image files (filename, type) to $image_list
      else if($filter_result[0] == 3) {
        array_push($image_list, array($contents, $filter_result[1]));
	}
      }
    }
  closedir($d);

  sort($dir_list);
  sort($audio_list);
  sort($image_list);
  sort($video_list);
  }

// if the filetype is matched (according to the filter) this function returns an
// array: The first number is the media class (1=audio, 2=video, 3=image). The
// Second number in the array is type info in the types array
// Returns a 0 if it is an invalid file type
// takes a file name and a file path
function apply_filter($filename, $file_path) {
  global $FILTER;
  global $AUDIO_TYPE;
  global $VIDEO_TYPE;
  global $IMAGE_TYPE;

  // checks the file extension
  if($FILTER == 1) {
    // sets which part of the type array to look at for comparison
    $ident = 0;
    // uses the file extension as the identifier
    $identifier = get_extension($filename);
    }

  // case to look at the file identifier at the beginning of a file
  else if($FILTER == 2) {
    // sets which part of the type array to look at for comparison
    $ident = 1;
    // uses the file identifier to determine filetype
    $identifier = get_identifier($file_path);
    }
  
  // case if no filter was chosen (will assume everything is an mp3)
  else  {
    return 1;
    exit;
    }


  // loops through each registered filetype to determine each file's type.  
  
  // case for audio types
  if(isset($AUDIO_TYPE)) { // checks to see if audio types are enabled
//    for($i=0; $i < sizeof($AUDIO_TYPE) && $exit == 0; $i++) {
    for($i=0; $i < sizeof($AUDIO_TYPE); $i++) {
      if(eregi($AUDIO_TYPE[$i][$ident], $identifier)) {
        return array(1, $i);
        exit;
	}
      }
    }

  // case for video types  
  if(isset($VIDEO_TYPE)) { // checks to see if video types are enabled
//    for($i=0; $i < sizeof($VIDEO_TYPE) && $exit == 0; $i++) {
    for($i=0; $i < sizeof($VIDEO_TYPE); $i++) {
      if(eregi($VIDEO_TYPE[$i][$ident], $identifier)) {
        return array(2, $i);
        exit;
	}
      }
    }


  // case for image types  
  if(isset($IMAGE_TYPE)) { // checks to see if images are enabled
//    for($i=0; $i < sizeof($IMAGE_TYPE) && $exit == 0; $i++) {
    for($i=0; $i < sizeof($IMAGE_TYPE); $i++) {
      if(eregi($IMAGE_TYPE[$i][$ident], $identifier)) {
        return array(3, $i);
        exit;
	}
      }
    }

  // case if file's type could not be matched
  return 0; 

  }


// adds a users playlists to the global $playlist_list array
function read_playlists() {
  global $Brunhilde;
  global $playlist_list;

  if(isset($Brunhilde)) {
    while(list($name) = each($Brunhilde)) {
      array_push($playlist_list, rawurlencode(stripslashes($name)));
      }
    }
  sort($playlist_list);
  }

// returns an array of a recursed list of songs with their pathnames
function recurse_songs($dir) {
  global $MEDIA_DIR;
  global $FILTER;
  global $base;
  global $shuffle;
  $audio_list = array();
  $dir_list = array();

  // opens the directory and looks at it's contents
  $d = opendir($MEDIA_DIR[$base][1].$dir);
  // loops through all of the directory's contents
  while($contents = readdir($d)) {

    if($contents == "." || $contents == "..") { continue; }
    // if a folder is found
    if(is_dir($MEDIA_DIR[$base][1].$dir.$contents)) {
      array_push($dir_list, $contents."/");
      }
    // if a file is found, determines type
    else {
      $filter_result = apply_filter($contents, $dir.$contents);
      if($filter_result[0] == 1) { array_push($audio_list, $dir.$contents); }
      }
    }
  closedir($d);

  // recurses through all subdirectories
  if(sizeof($dir_list) > 0) {
    for($i = 0; $i < sizeof($dir_list); $i++) {
      $recursive_list = recurse_songs("$dir$dir_list[$i]");
      if(sizeof($recursive_list) > 0) { 
        $audio_list = array_merge($audio_list, $recursive_list);
	}
      }
    }
  if($shuffle == 1) { $audio_list = shuffle_array($audio_list); }
  else { sort($audio_list); }
  return $audio_list;
  }

// returns the extension from a filename
function get_extension($string) {
  $period_array = explode(".", $string);
  $extension = $period_array[count($period_array) -1];
  return $extension;
  }

// gets a 3 character identifier at the beginning of a file
function get_identifier($filepath) {
  $file = fopen($filepath, 'r');
  $identifier = "";
  for($i = 0; $i < 3; $i++) { $identifier = $identifier.fgetc($file); }
  fclose($file);
  return "$identifier";
  }

function shuffle_array($array) {
  srand ((double)microtime()*1000000);
  shuffle($array);
  return $array;
  }

?>
Return current item: Brunhilde Media Server