<?php

/**
 * play.php RSS feed to HTML webpage script
 * brian kane 2006 
 * modification of rss2html  
 * Complete documentation and samples of rss2html are at:
 * http://www.feedforall.com/php-documentation.htm
 */


// ==========================================================================
// Configuration options
// ==========================================================================
//
// Set the following variable useFopenURL to one if you want/need to use 
// fopen() instead of CURL
$useFopenURL = 1;

//
// If XLMFILE is passed as part of the REQUEST_URI, then it will be used
// otherwise the the file below is used.
$xmlfilename = "eric/eric.xml";

//
// If template is passed as part of the REQUEST_URI, then it will be used
// otherwise the the file below is used.
$templatefilename = "radio";


// initialize a counter - used for counting the number of items in the rss file
$counter = 1;

//
// date() function documented http://www.php.net/manual/en/function.date.php
$LongDateFormat = "F jS, Y";    // ie, "Jan 21st, 2004"
$ShortDateFormat = "m/d/Y";     // ie, "1/21/2004"
$LongTimeFormat = "H:i:s T O";  // ie, "13:24:30 EDT -0400"
$ShortTimeFormat = "h:i A";     // ie, "1:24 PM"

// ==========================================================================
// Below this point of the file there are no user editable options.  Your
// are welcome to make any modifications that you wish to any of the code
// below, but that is not necessary for normal use.
// ==========================================================================

// init item 2007-04-09 pbull
$item = '';

if ($useFopenURL) {
  ini_set("allow_url_fopen", "1");
  ini_set("user_agent", 'rss2html.php');
}


if (function_exists("rss2html_readFile") === FALSE) {
  Function rss2html_readFile($filename, $useFopenURL) {
    
    $GLOBALS["ERRORSTRING"] = "";
    $result = "";
    if (stristr($filename, "://")) {
      if ($useFopenURL) {
        if (($fd = @fopen($filename, "rb")) === FALSE) {
          return FALSE;
        }
        while (($data = fread($fd, 4096)) != "") {
          $result .= $data;
        }
        fclose($fd);
      } else {
        // This is a URL so use CURL
        $curlHandle = curl_init();
        curl_setopt($curlHandle, CURLOPT_URL, $filename);
        curl_setopt($curlHandle, CURLOPT_HEADER, 0);
        curl_setopt($curlHandle, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curlHandle, CURLOPT_USERAGENT, "rss2html.php");
        //    curl_setopt($curlHandle, CURLOPT_AUTOREFERER, 1);
        curl_setopt($curlHandle, CURLOPT_REFERER, $filename);
        curl_setopt($curlHandle, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($curlHandle, CURLOPT_MAXREDIRS, 10);
        $result = curl_exec($curlHandle);
        if (curl_errno($curlHandle)) {
          $GLOBALS["ERRORSTRING"] = curl_error($curlHandle);
          curl_close($curlHandle);
          return FALSE;
        }
        curl_close($curlHandle);
      }
    } else {
      // This is a local file, so use fopen
      if (($fd = @fopen($filename, "rb")) === FALSE) {
        return FALSE;
      }
      while (($data = fread($fd, 4096)) != "") {
        $result .= $data;
      }
      fclose($fd);
    }
    return $result;
  }
}

if (!isset($_REQUEST["buildURL"])) {
  if (isset($_REQUEST["xml"])) {
    if (stristr($_REQUEST["xml"], "file://")) {
      // Not allowed
      ;
    }
    elseif (stristr($_REQUEST["xml"], "://")) {
      // URL files are allowed
      $xmlfilename = $_REQUEST["xml"];
    } else {
      // It is local and must be in the same directory
      //$xmlfilename = basename($_REQUEST["xml"]);
      $xmlfilename = $_REQUEST["xml"];
    }
  }



/*
// create a podcast pcast or itpc url based on the xml filename
//  inserted bk
if (  ereg("http://", $xmlfilename))   {
  // use pcast
  //$pcasturl = str_replace("http", "pcast", $xmlfilename);
  // use itpc
  $pcasturl = str_replace("http", "itpc", $xmlfilename);
}
else {
  //use pcast
  //$pcasturl = "pcast://streams.wgbh.org/online/" .  $xmlfilename;
  // use itpc
  $pcasturl = "pcast://streams.wgbh.org/online/" .  $xmlfilename;
}
*/




/*
  if (isset($_REQUEST["template"])) {
    if (stristr($_REQUEST["template"], "file://")) {
      // Not allowed
      ;
    }
    elseif (stristr($_REQUEST["template"], "://")) {
      // URL files are allowed
      $templatefilename = $_REQUEST["template"];
    } else {
      // It is local and must be in the same directory
      // $templatefilename = basename($_REQUEST["template"]);
      $templatefilename = $_REQUEST["template"];
    }
  }

// Custom setting for clas/ directory
// use template clas by default
if (!(isset($_REQUEST["template"]))) {
	if (  ereg("clas/", $xmlfilename))   {
	$templatefilename = "clas"; 
	}
}
*/
/**
 * 2008-09-25 pbull
 * get template file
 */
if (isset($_REQUEST["template"])) {
  $templatefilename = $_REQUEST["template"];
}
else {
  $templatefilename = "radio";
}


/**
 * 2008-02-04 pbull: temp solution to remove image from live cape player
 */
if ($xmlfilename == "cainan.xml") {
  $templatefilename = "cape_audio_blank";
}




/**
 * 2009-02-19 pbull: redirect, as appropriate, to mediaplayer.wgbh.org
 */
$mediaplayer_redirect = TRUE;
if (strpos($xmlfilename, 'cape/') !== false) {
  $mediaplayer_redirect = FALSE;
}
if (strpos($xmlfilename, 'cape2/') !== false) {
  $mediaplayer_redirect = FALSE;
}
if (strpos($xmlfilename, 'war/') !== false) {
  $mediaplayer_redirect = FALSE;
}
if (strpos($xmlfilename, 'creativeservices/') !== false) {
  $mediaplayer_redirect = FALSE;
}
if (strpos($xmlfilename, 'science/radiolab') !== false) {
  $mediaplayer_redirect = FALSE;
}
$referer = $_SERVER["HTTP_REFERER"];
if (strpos($referer, "cainan") || strpos($referer, "wnan_theirs")) {
  $mediaplayer_redirect = FALSE;
}
if ($mediaplayer_redirect) {
  header("HTTP/1.1 301 Moved Permanently");
  header("Location: http://mediaplayer.wgbh.org/?xml=$xmlfilename&resize=1");
  exit();
}





/**
 * 2008-11-04 pbull: iphone redirection
 */
if ($xmlfilename == "897.xml" || $xmlfilename == "classicalhd.xml" || $xmlfilename == "cainan.xml") {
  if (strpos($_SERVER['HTTP_USER_AGENT'], "iPod") || strpos($_SERVER['HTTP_USER_AGENT'], "iPhone")) {
    header("Location: http://streams.wgbh.org/online/iphone/");
    exit();
  }
}




/**
 * DEV
 * live stream player, check for "player" in querystring
 */
if (isset($_REQUEST["player"])) {
  $player = $_REQUEST["player"];
  $player_js = 'var player="'. $player .'"; ';
}
/**
 * DEV
 * live stream player, check for xml source
 */
if (!stristr($_REQUEST["xml"], "/")) {
  $live_stream = TRUE;
  
  /*
  if ($xmlfilename == "cainan.xml") {
    $templatefilename = "cape_mediaplayer";
  }
  else {
    $templatefilename = "radio_mediaplayer";
  }
  */
  
  
  $basename = basename($xmlfilename, ".xml");
  $templatefilename = ($xmlfilename == "cainan.xml") ? "cape_audio_blank" : "radio";
  //"cape_mediaplayer" : "radio_mediaplayer";
  switch ($basename) {
    case "cainan":        $port = "8002"; break;
    case "classicalhd":   $port = "8006"; break;
    case "celt":          $port = "8012"; break;
    case "897":
    default:              $port = "8000"; break;
  }
  $html_head = '<script type="text/javascript">'. $player_js .'var basename="'. $basename .'"; var player_xml="'. $_REQUEST["xml"] .'"; var port="'. $port .'"</script>' . "\n  "
    . '<script type="text/javascript" src="/online/mediaplayer/jquery-1.2.6.min.js"></script>' . "\n  "
    . '<script type="text/javascript" src="/online/mediaplayer/jmedia-wgbh.js"></script>' . "\n  "
    . '<script type="text/javascript">$(document).ready(function() { $("#player-container").html(player_markup); });</script>';
}






  //
  // Maximum number of items to be displayed
  //

  // deafult max items to 1
  $FeedMaxItems = 1;
  if (isset($_REQUEST["max"])) {
    $FeedMaxItems = $_REQUEST["max"];
  }
  $NoFutureItems = FALSE;
  if (isset($_REQUEST["NOFUTUREITEMS"])) {
    $NoFutureItems = TRUE;
  }


//  Modification to accept command line arguments - useful for debugging ...
foreach ( $argv as $key => $value ) {
//   fwrite(STDOUT,"$key => $value\n");

if ($key == "1") {
$xmlfilename = "$value";
//print "$xmlfilename\n";
}

if ($key == "2") {
$templatefilename = "$value";
//print "$templatefilename\n";
}

if ($key == "3") {
$FeedMaxItems = "$value";
//print "$FeedMaxItems\n";
}

}





  //
  // As much as I hate globals, they are needed due to the
  // recusive nature of the parser
  $insidechannel = FALSE;
  $level_channel = 0;
  $insidechannelimage = FALSE;
  $level_channelimage = 0;
  $insideitunesowner = FALSE;
  $level_itunesowner = 0;
  $insideitem = FALSE;
  $level_item = 0;

  if (function_exists("rss2html_getRFDdate") === FALSE) {
    Function rss2html_getRFDdate($datestring) {
      $year = substr($datestring, 0, 4);
      $month = substr($datestring, 5, 2);
      $day = substr($datestring, 8, 2);
      $hour = substr($datestring, 11, 2);
      $minute = substr($datestring, 14, 2);
      $second = substr($datestring, 17, 2);
      if (substr($datestring, 19, 1) == "Z") {
        $offset_hour = 0;
        $offset_minute = 0;
      } else {
        if (substr($datestring, 19, 1) == "+") {
          $offset_hour = substr($datestring, 20, 2);
          $offset_minute = substr($datestring, 23, 2);
        } else {
          $offset_hour = -1*substr($datestring, 20, 2);
          $offset_minute = -1*substr($datestring, 23, 2);
        }
      }
      return gmmktime($hour+$offset_hour, $minute+$offset_minute, $second, $month, $day, $year);
    }

    class rss2html_RSSParser {
      var $gotROOT = 0;
      var $feedTYPE = "RSS";
      var $level = 0;
      var $tag = "";
      var $title = "";
      var $description = "";
      var $contentEncoded = "";
      var $link = "";
      var $guid = "";
      var $enclosureURL = "";
      var $enclosureType = "";
      var $enclosureLength = "";
      var $pubdate = "";
      var $pubdateDC = "";
      var $fimageURL = "";
      var $fimageTitle = "";
      var $fimageLink = "";
      var $author = "";
      var $DcCreator = "";
      var $ItunesOwnerEmail = "";
      var $ItunesOwnerName = "";
      var $ItunesLinkHref = "";
      var $ItunesImageHref = "";
      var $ItunesLink = "";

      var $FeedTitle = "";
      var $FeedDescription = "";
      var $FeedContentEncoded = "";
      var $FeedLink = "";
      var $FeedPubDate = "";
      var $FeedPubDateDC = "";
      var $FeedPubDate_t = "";
      var $FeedLastBuildDate = "";
      var $FeedImageURL = "";
      var $FeedImageTitle = "";
      var $FeedImageLink = "";
      var $FeedItunesOwnerEmail = "";
      var $FeedItunesOwnerName = "";
      var $FeedItunesAuthor = "";
      var $FeedItunesSubtitle = "";
      var $FeedItunesSummary = "";
      var $FeedItunesExplicit = "";
      var $FeedItunesLinkHref = "";
      var $FeedItunesImageHref = "";
      var $FeedItunesLink = "";
      var $FeedCopyright = "";

 
      // When adding new Item elements, be sure to update the sort below
      var $ItemTitle = "";
      var $ItemDescription = "";
      var $ItemContentEncoded = "";
      var $ItemLink = "";
      var $ItemGuid = "";
      var $ItemPubDate = "";
      var $ItemPubDate_t = "";
      var $ItemEnclosureURL = "";
      var $ItemEnclosureType = "";
      var $ItemEnclosureLength = "";
      var $ItemAuthor = "";
      var $ItemItunesSummary = "";
      var $ItemItunesSubtitle = "";
      var $ItemItunesDuration = "";

      function startElement($parser, $tagName, $attrs) {
        GLOBAL $insidechannel;
        GLOBAL $level_channel;
        GLOBAL $insidechannelimage;
        GLOBAL $level_channelimage;
        GLOBAL $insideitunesowner;
        GLOBAL $level_itunesowner;
        GLOBAL $insideitem;
        GLOBAL $level_item;
        

        $this->level++;
        $this->tag = $tagName;
        if ($this->gotROOT == 0) {
          $this->gotROOT = 1;
          if (strstr($tagName, "RSS")) {
            $this->feedTYPE = "RSS";
          }
          elseif (strstr($tagName, "RDF")) {
            $this->feedTYPE = "RDF";
          }
          elseif (strstr($tagName, "FEE")) {
            $this->feedTYPE = "FEE";
            $insidechannel = TRUE;
            $level_channel = 1;
          }
        }
        elseif ((($tagName == "ITEM") && ($this->feedTYPE != "FEE")) || (($tagName == "ENTRY") && ($this->feedTYPE == "FEE"))) {
          $insideitem = TRUE;
          $level_item = $this->level;
        }
        elseif (($insideitem) && ($tagName == "ENCLOSURE")) {
          $this->enclosureURL = $attrs["URL"];
          $this->enclosureType = $attrs["TYPE"];
          $this->enclosureLength = $attrs["LENGTH"];
        }        

        elseif (($tagName == "LINK") && ($this->feedTYPE == "FEE")) {
          $this->link = $attrs["HREF"];
        }
        elseif ($tagName == "CHANNEL") {
          $insidechannel = TRUE;
          $level_channel = $this->level;
        }
        elseif (($tagName == "IMAGE") && ($insidechannel = TRUE)) {
          $insidechannelimage = TRUE;
          $level_channelimage = $this->level;
        }
        
        elseif (($tagName == "ITUNES:OWNER") && ($insidechannel = TRUE)) {
          $insideitunesowner = TRUE;
          $level_itunesowner = $this->level;
        }

        elseif (($tagName == "ITUNES:LINK") && ($insidechannel = TRUE)) {
        $this->FeedItunesLinkHref = $attrs["HREF"];
        }
        
        elseif (($tagName == "ITUNES:IMAGE") && ($insidechannel = TRUE)) {
        $this->FeedItunesImageHref = $attrs["HREF"];
        }

        
        if ($parser);
      }

      function endElement($parser, $tagName) {
        GLOBAL $insidechannel;
        GLOBAL $level_channel;
        GLOBAL $insidechannelimage;
        GLOBAL $level_channelimage;
        GLOBAL $insideitunesowner;
        GLOBAL $level_itunesowner;
        GLOBAL $insideitem;
        GLOBAL $level_item;
        GLOBAL $NoFutureItems;

        $this->level--;
        if ((($tagName == "ITEM") && ($this->feedTYPE != "FEE")) || (($tagName == "ENTRY") && ($this->feedTYPE == "FEE"))) {
          $UseItem = TRUE;

          if ($NoFutureItems) {
            $noon = strtotime("today at 12:00");
            if (trim($this->pubdate) != "") {
              $ItemPubDate = strtotime($this->pubdate);
            }
            else if (trim($this->pubdateDC) != "") {
              $ItemPubDate = rss2html_getRFDdate($this->pubdateDC);
            } else {
              $ItemPubDate = time();
            }
            if (($ItemPubDate - $noon) > 43200) {
              $UseItem = FALSE;
            }
          }

          if ($UseItem) {
            $this->ItemTitle[] = trim($this->title);
            $this->ItemDescription[] = trim($this->description);
            $this->ItemContentEncoded[] = trim($this->contentEncoded);
            if (trim($this->contentEncoded) == "") {
              $this->ItemContentEncoded[] = $this->description;
            }
            $this->ItemLink[] = trim($this->link);
            
            // Get the pubDate from pubDate first and then dc:date
            if (trim($this->pubdate) != "") {
              $this->ItemPubDate[] = trim($this->pubdate);
              $this->ItemPubDate_t[] = strtotime($this->pubdate);
            }
            else if (trim($this->pubdateDC) != "") {
              $this->ItemPubDate[] = trim($this->pubdateDC);
              $this->ItemPubDate_t[] = rss2html_getRFDdate($this->pubdateDC);
            } else {
              $this->ItemPubDate[] = date("D, d M Y H:i:s +0000");
              $this->ItemPubDate_t[] = time();
            }
            $this->ItemGuid[] = trim($this->guid);
            $this->ItemEnclosureURL[] = trim($this->enclosureURL);
            $this->ItemEnclosureType[] = trim($this->enclosureType);
            $this->ItemEnclosureLength[] = trim($this->enclosureLength);                        
            $this->ItemItunesSummary[] = trim($this->ItunesSummary);
            $this->ItemItunesSubtitle[] = trim($this->ItunesSubtitle);
	    $this->ItemItunesDuration[] = trim($this->ItunesDuration);
            if ($this->author == "") {
              $this->ItemAuthor[] = $this->DcCreator;
            } else {
              $this->ItemAuthor[] = $this->author;
            }
          }
          $this->title = "";
          $this->description = "";
          $this->contentEncoded = "";
          $this->link = "";
          $this->pubdate = "";
          $this->pubdateDC = "";
          $this->guid = "";
          $this->enclosureURL = "";
          $this->enclosureType = "";
          $this->enclosureLength = "";
          $this->author = "";
          $this->DcCreator = "";

// dont forget to add items here
        	$this->ItunesDuration = "";
		$this->ItunesSubtitle = "";
		$this->ItunesSummary = "";

          $insideitem = FALSE;
          $level_item = 0;
        }
        elseif (($tagName == "IMAGE") && ($insidechannelimage)) {
          $this->FeedImageURL = trim($this->fimageURL);
          $this->FeedImageTitle = trim($this->fimageTitle);
          $this->FeedImageLink = trim($this->fimageLink);
          $this->fimageURL = "";
          $this->fimageTitle = "";
          $this->fimageLink = "";
          $insidechannelimage = FALSE;
          $level_channelimage = 0;
        }
        
        elseif (($tagName == "ITUNES:OWNER") && ($insideitunesowner)) {
        
          $this->FeedItunesOwnerEmail = trim($this->ItunesOwnerEmail);
          $this->FeedItunesOwnerName = trim($this->ItunesOwnerName);
          $this->ItunesEmail = "";
          $this->ItunesName = "";
          $this->fimageLink = "";
          $insideitunesowner = FALSE;
          $level_itunesowner = 0;

        }
        

        
        elseif ($tagName == "CHANNEL") {
          
          // Get the pubDate from pubDate first and then dc:date
          if (trim($this->FeedPubDate) != "") {
            $this->FeedPubDate_t = strtotime($this->FeedPubDate);
          }
          else if (trim($this->FeedPubDateDC) != "") {
            $this->FeedPubDate_t = rss2html_getRFDdate($this->FeedPubDateDC);
          }
          else if (trim($this->FeedLastBuildDate) != "") {
            $this->FeedPubDate_t = strtotime($this->FeedLastBuildDate);
          } else {
            $this->FeedPubDate = date("D, d M Y H:i:s +0000");
            $this->FeedPubDate_t = time();
          }
          $insidechannel = FALSE;
          $level_channel = 0;
        }
        elseif ($this->level == $level_channel) {
          if ($tagName == "TITLE") {
            $this->FeedTitle = trim($this->title);
            $this->title = "";
          }
          elseif (($tagName == "DESCRIPTION") || ($tagName == "TAGLINE")) {
            $this->FeedDescription = trim($this->description);
            $this->description = "";
          }
          elseif ($tagName == "CONTENT:ENCODED") {
            $this->FeedContentEncoded = trim($this->contentEncoded);
            $this->contentEncoded = "";
          }
          elseif ($tagName == "ITUNES:EMAIL") {
            $this->FeedItunesOwnerEmail = trim($this->ItunesOwnerEmail);
            $this->ItunesOwnerEmail = "";
          }
          elseif ($tagName == "LINK") {
            $this->FeedLink = trim($this->link);
            $this->link = "";
          }
          elseif ($tagName == "ITUNES:AUTHOR") {
            $this->FeedItunesAuthor = trim($this->ItunesAuthor);
            $this->ItunesAuthor = "";
          }
                  
          elseif ($tagName == "ITUNES:SUBTITLE") {
            $this->FeedItunesSubtitle = trim($this->ItunesSubtitle);
            $this->ItunesSubtitle = "";
          }
          
          elseif ($tagName == "ITUNES:SUMMARY") {
            $this->FeedItunesSummary = trim($this->ItunesSummary);
            $this->ItunesSummary = "";
          }
          
          elseif ($tagName == "ITUNES:EXPLICIT") {
            $this->FeedItunesExplicit = trim($this->ItunesExplicit);
            $this->ItunesExplicit = "";
          }
          
          elseif ($tagName == "ITUNES:LINK") {
            $this->FeedItunesLink = trim($this->ItunesLink);
            $this->ItunesLink = "";     
          }

          elseif ($tagName == "COPYRIGHT") {
            $this->FeedCopyright = trim($this->Copyright);
            $this->Copyright = "";     
          }
          
          
        }
        if ($parser);
      }

      function characterData($parser, $data) {
        GLOBAL $insidechannel;
        GLOBAL $level_channel;
        GLOBAL $insidechannelimage;
        GLOBAL $level_channelimage;
        GLOBAL $insideitunesowner;
        GLOBAL $level_itunesowner;
        GLOBAL $insideitem;
        GLOBAL $level_item;
        

        if (($data == "") || ($data == NULL)) {
        } else {
          if (($insideitem) && ($this->level == $level_item+1)) {
            switch ($this->tag) {
              case "TITLE":
              $this->title .= $data;
              break;

              case "DESCRIPTION":
              $this->description .= $data;
              break;

              case "CONTENT:ENCODED":
              $this->contentEncoded .= $data;
              break;

              case "SUMMARY":
              $this->description .= $data;
              break;

              case "LINK":
              $this->link .= $data;
              break;

              case "PUBDATE":
              $this->pubdate .= $data;
              break;

              case "DC:DATE":
              $this->pubdateDC .= $data;
              break;
              
              case "ITUNES:SUMMARY":
              $this->ItunesSummary .= $data;
              break; 

              case "ITUNES:SUBTITLE":
              $this->ItunesSubtitle .= $data;
              break;

              case "ITUNES:DURATION":
              $this->ItunesDuration .= $data;
              break; 

              case "MODIFIED":
              $this->pubdateDC .= $data;
              break;

              case "GUID":
              $this->guid .= $data;
              break;

              case "AUTHOR":
              $this->author .= $data;
              break;

              case "DC:CREATOR":
              $this->DcCreator .= $data;
              break;
            }
          }
          elseif ($insidechannelimage) {
            switch ($this->tag) {
              case "TITLE":
              $this->fimageTitle .= $data;
              break;

              case "URL":
              $this->fimageURL .= $data;
              break;

              case "LINK":
              $this->fimageLink .= $data;
              break;
            }
           
          }  
          elseif ($insideitunesowner) {
            switch ($this->tag) {
              case "ITUNES:EMAIL":
              $this->ItunesOwnerEmail .= $data;
              break;
              
              case "ITUNES:NAME":
              $this->ItunesOwnerName .= $data;
              break;

            }  
                       
             
          }
          elseif (($insidechannel) && ($this->level == $level_channel+1)) {
            switch ($this->tag) {
              case "TITLE":
              $this->title .= $data;
              break;

              case "DESCRIPTION":
              $this->description .= $data;
              break;

              case "CONTENT:ENCODED":
              $this->contentEncoded .= $data;
              break;
              
	      case "ITUNES:AUTHOR":
              $this->ItunesAuthor .= $data;
              break;
              
              case "ITUNES:SUBTITLE":
              $this->ItunesSubtitle .= $data;
              break;
              
              case "ITUNES:SUMMARY":
              $this->ItunesSummary .= $data;
              break;
              
              case "ITUNES:EXPLICIT":
              $this->ItunesExplicit .= $data;
              break;

              case "COPYRIGHT":
              $this->Copyright .= $data;
              break;
              
              case "ITUNES:LINK":
              $this->ItunesLink .= $data;
              break;

              case "TAGLINE":
              $this->description .= $data;
              break;

              case "LINK":
              $this->link .= $data;
              break;

              case "PUBDATE":
              $this->FeedPubDate .= $data;
              break;

              case "DC:DATE":
              $this->FeedPubDateDC .= $data;
              break;

              case "MODIFIED":
              $this->FeedPubDateDC .= $data;
              break;

              case "LASTBUILDDATE":
              $this->FeedLastBuildDate .= $data;
              break;
            }
          }
        }
        if ($parser);
      }
    }
  }

//  prepend template name with templates directory - bk added
$templatefilename = "templates/" . $templatefilename;

  if (($template = rss2html_readFile($templatefilename, $useFopenURL)) === FALSE) {
    if ($GLOBALS["ERRORSTRING"] == "") {
      echo "Unable to open template $templatefilename, exiting\n";
    } else {
      echo "Unable to open template $templatefilename with error <b>$GLOBALS[ERRORSTRING]</b>, exiting\n";
    }
    exit -1;
  }

  if (strstr($template, "~~~NoFutureItems~~~")) {
    $NoFutureItems = TRUE;
  }

  $xml_parser = xml_parser_create('');
  $rss_parser = new rss2html_RSSParser();
  xml_set_object($xml_parser,$rss_parser);
  xml_set_element_handler($xml_parser, "startElement", "endElement");
  xml_set_character_data_handler($xml_parser, "characterData");
  xml_parser_set_option($xml_parser,XML_OPTION_CASE_FOLDING,1);
  if (($xml = rss2html_readFile($xmlfilename, $useFopenURL )) === FALSE) {
    if ($GLOBALS["ERRORSTRING"] == "") {
      echo "Unable to open RSS Feed $xmlfilename, exiting\n";
    } else {
      echo "Unable to open RSS Feed $xmlfilename with error <b>$GLOBALS[ERRORSTRING]</b>, exiting\n";
    }
    exit -1;
  }
  xml_parse($xml_parser, $xml);
  xml_parser_free($xml_parser);


/**
 * 2007-12-03 pbull
 * modified script to handle new xml from web media tool
 */
if (strtolower($xmlfilename) == 'eric/eric.xml') {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
//if (strtolower($xmlfilename) == 'folk/folk.xml') {
if (strpos($xmlfilename, 'folk/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strtolower($xmlfilename) == 'blue/blue.xml') {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}

if (strpos($xmlfilename, 'carroll/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
/******
if (strtolower($xmlfilename) == 'carroll/carroll.xml') {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'carr200712') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'carr2008') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
******/


if (strpos($xmlfilename, 'celt/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'clas/') !== false) {
  //$rss_parser->FeedDescription = $rss_parser->FeedTitle;
  $rss_parser->FeedDescription = $rss_parser->ItemTitle[0];
  if ($s = strrpos($rss_parser->FeedDescription, "- WGBH Classical Performance")) {
    $rss_parser->FeedDescription = substr($rss_parser->FeedDescription, 0, $s);
  }
  if ($s = strrpos($rss_parser->ItemDescription[0], "***")) {
    $rss_parser->ItemDescription[0] = substr($rss_parser->ItemDescription[0], 0, $s);
  }
}
if ((strpos($xmlfilename, 'birdnews/') !== false) || (strpos($xmlfilename, 'finch/') !== false) || (strpos($xmlfilename, 'thepoint/') !== false)) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'jfs4/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'archives/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'play/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'connections/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
if (strpos($xmlfilename, 'backstage/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}

// 2008-05-13 pbull: adding Cape generic container
if (strpos($xmlfilename, 'cape2/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
  //$rss_parser->FeedDescription = $rss_parser->ItemTitle[0];
}

// 2008-10-31 pbull: morning stories
if (strpos($xmlfilename, 'morn/') !== false) {
  $rss_parser->FeedDescription = $rss_parser->ItemTitle[0];
  $rss_parser->ItemDescription[0] .= 'Listen to <a href="http://www.wgbh.org/article?item_id=2020890" '.
    'target="wgbh_main">other Morning Stories</a>. ' .
    'Support WGBH. <a href="http://www.wgbh.org/support/pledge" target="wgbh_main">Pledge now!</a>';
}

// 2008-12-10 pbull: jazz conversations
if (strpos($xmlfilename, 'specials/jazz_conversations') !== false) {
  $rss_parser->FeedDescription = $rss_parser->FeedTitle;
}
/**
 * end xml fixes
 */





/**
 * 2008-02-04 pbull: swap header links within template, object is to 
 * reduce the number of required template files
 */ 
  $links = array();
  
  // classical (clas)
  if ((strpos($xmlfilename, 'clas/') === 0) || (strpos($xmlfilename, 'connections/') === 0)) {
    $links[] = array('text' => 'WGBH Classical', 'url' => 'http://www.wgbh.org/classical/');
  }
  // jazz (eric, jfs4)
  elseif ((strpos($xmlfilename, 'eric/') === 0) || (strpos($xmlfilename, 'jfs4/') === 0)) {
    $links[] = array('text' => 'WGBH Jazz', 'url' => 'http://www.wgbh.org/jazz/');
  }
  else {
    $links[] = array('text' => 'Multimedia', 'url' => 'http://www.wgbh.org/web');
  }

  $links_html = '';
  foreach ($links as $link) {
    $links_html .= '<a href="' . $link['url'] . '" title="' . $link['text'] . '" target="wgbh_main">' . $link['text'] . '</a> | ';
  }
/**
 * end swap header links
 */



  // make sure the channel contentEncoded is not blank
  if ($rss_parser->FeedContentEncoded == "") {
    $rss_parser->FeedContentEncoded = $rss_parser->FeedDescription;
  }
  
  // 2008-09-25 pbull: insert script into header
  $template = str_replace("~~~HtmlHead~~~", $html_head, $template);

  // 2008-02-04 pbull: swap header links
  $template = str_replace("~~~MultimediaLinks~~~", $links_html, $template);

  $template = str_replace("~~~FeedTitle~~~", $rss_parser->FeedTitle, $template);
  $template = str_replace("~~~FeedDescription~~~", $rss_parser->FeedDescription, $template);
  $template = str_replace("~~~FeedContentEncoded~~~", $rss_parser->FeedContentEncoded, $template);
  $template = str_replace("~~~FeedLink~~~", $rss_parser->FeedLink, $template);
  $template = str_replace("~~~FeedPubDate~~~", $rss_parser->FeedPubDate, $template);
  $template = str_replace("~~~FeedPubLongDate~~~", date($LongDateFormat, $rss_parser->FeedPubDate_t), $template);
  $template = str_replace("~~~FeedPubShortDate~~~", date($ShortDateFormat, $rss_parser->FeedPubDate_t), $template);
  $template = str_replace("~~~FeedPubLongTime~~~", date($LongTimeFormat, $rss_parser->FeedPubDate_t), $template);
  $template = str_replace("~~~FeedPubShortTime~~~", date($ShortTimeFormat, $rss_parser->FeedPubDate_t), $template);
  $template = str_replace("~~~FeedImageUrl~~~", $rss_parser->FeedImageURL, $template);
  $template = str_replace("~~~FeedImageTitle~~~", $rss_parser->FeedImageTitle, $template);
  $template = str_replace("~~~FeedImageLink~~~", $rss_parser->FeedImageLink, $template);
  $template = str_replace("~~~FeedItunesOwnerEmail~~~", $rss_parser->FeedItunesOwnerEmail, $template);
  $template = str_replace("~~~FeedItunesOwnerName~~~", $rss_parser->FeedItunesOwnerName, $template);
  $template = str_replace("~~~FeedItunesAuthor~~~", $rss_parser->FeedItunesAuthor, $template);
  $template = str_replace("~~~FeedItunesSubtitle~~~", $rss_parser->FeedItunesSubtitle, $template);
  $template = str_replace("~~~FeedItunesSummary~~~", $rss_parser->FeedItunesSummary, $template);
  $template = str_replace("~~~FeedItunesExplicit~~~", $rss_parser->FeedItunesExplicit, $template);
  $template = str_replace("~~~FeedItunesLink~~~", $rss_parser->FeedItunesLink, $template);
  $template = str_replace("~~~FeedItunesLinkHref~~~", $rss_parser->FeedItunesLinkHref, $template);
  $template = str_replace("~~~FeedItunesImageHref~~~", $rss_parser->FeedItunesImageHref, $template);
  $template = str_replace("~~~FeedCopyright~~~", $rss_parser->FeedCopyright, $template);
  $template = str_replace("~~~FeedXMLFile~~~", $xmlfilename, $template);
 
// modification to allow for starting at a certain item number - bk    
    $StartItem = "1";
    if (isset($_REQUEST["item"])) {
    $StartItem = $_REQUEST["item"] ;
  }
    $template = str_replace("~~~StartTrack~~~", $StartItem, $template);

  //$template = str_replace("~~~FeedPcastUrl~~~", $pcasturl, $template);
  $match = NULL;

  $template = str_replace("~~~NoFutureItems~~~", "", $template);

  // Sort by PubDate if requested
  if (strstr($template, "~~~SortByPubDate~~~")) {
    $template = str_replace("~~~SortByPubDate~~~", "", $template);

    for ($x = 0; $x < count($rss_parser->ItemTitle)-1; $x++)
    {
      for ($y = $x+1; $y < count($rss_parser->ItemTitle); $y++)
      {
        if ($rss_parser->ItemPubDate_t[$x] < $rss_parser->ItemPubDate_t[$y])
        {
          // Swap them
          $swapTemp = $rss_parser->ItemTitle[$x]; $rss_parser->ItemTitle[$x] = $rss_parser->ItemTitle[$y]; $rss_parser->ItemTitle[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemDescription[$x]; $rss_parser->ItemDescription[$x] = $rss_parser->ItemDescription[$y]; $rss_parser->ItemDescription[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemContentEncoded[$x]; $rss_parser->ItemContentEncoded[$x] = $rss_parser->ItemContentEncoded[$y]; $rss_parser->ItemContentEncoded[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemLink[$x]; $rss_parser->ItemLink[$x] = $rss_parser->ItemLink[$y]; $rss_parser->ItemLink[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemGuid[$x]; $rss_parser->ItemGuid[$x] = $rss_parser->ItemGuid[$y]; $rss_parser->ItemGuid[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemPubDate[$x]; $rss_parser->ItemPubDate[$x] = $rss_parser->ItemPubDate[$y]; $rss_parser->ItemPubDate[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemPubDate_t[$x]; $rss_parser->ItemPubDate_t[$x] = $rss_parser->ItemPubDate_t[$y]; $rss_parser->ItemPubDate_t[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemEnclosureURL[$x]; $rss_parser->ItemEnclosureURL[$x] = $rss_parser->ItemEnclosureURL[$y]; $rss_parser->ItemEnclosureURL[$y] = $swapTemp;
          $swapTemp = $rss_parser->ItemAuthor[$x]; $rss_parser->ItemAuthor[$x] = $rss_parser->ItemAuthor[$y]; $rss_parser->ItemAuthor[$y] = $swapTemp;
        }
      }
    }
  }

  // The maximum items requested
  if (strstr($template, "~~~FeedMaxItems=")) {
    // Limit the maximun number of items displayed
    if (preg_match("/~~~FeedMaxItems=([0-9-]*)~~~/", $template, $match) !== FALSE) {
      if (($match[0] != "") && ($match[1] != "")) {
        $FeedMaxItems = $match[1];
        $template = str_replace("~~~FeedMaxItems=$match[1]~~~", "", $template);
        if (abs($FeedMaxItems) > count($rss_parser->ItemTitle)) {
          if ($FeedMaxItems > 0) {
            $FeedMaxItems = count($rss_parser->ItemTitle);
          } else {
            $FeedMaxItems = -count($rss_parser->ItemTitle);
          }
        }
      }
    }
  }

  //
  // Find the string, if it exists, between the ~~~EndItemsRecord~~~ and ~~~BeginItemsRecord~~~
  //
  while ((strstr($template, "~~~BeginItemsRecord~~~")) !== FALSE) {
    $match = NULL;
    $allitems = NULL;
    $loop_limit = min(abs($FeedMaxItems), count($rss_parser->ItemTitle));
    if (($parts = split("~~~BeginItemsRecord~~~", $template)) !== FALSE) {
      if (($parts = split("~~~EndItemsRecord~~~", $parts[1])) !== FALSE) {
        $WholeBlock = $parts[0];
        //
        // Check for ~~~BeginAlternateItemsRecord~~~
        //
        if (strstr($WholeBlock, "~~~BeginAlternateItemsRecord~~~")) {
          $parts = split("~~~BeginAlternateItemsRecord~~~", $WholeBlock);
          $block1 = $parts[0];
          $block2 = $parts[1];
        } else {
          $block1 = $WholeBlock;
          $block2 = $WholeBlock;
        }
        if ($FeedMaxItems < 0) {
          for ($x = count($rss_parser->ItemTitle)-1; $x >= count($rss_parser->ItemTitle) + $FeedMaxItems; $x--) {
            $item = str_replace("~~~ItemTitle~~~", $rss_parser->ItemTitle[$x], $block1);
            $item = str_replace("~~~ItemDescription~~~", $rss_parser->ItemDescription[$x], $item);
            $item = str_replace("~~~ItemContentEncoded~~~", $rss_parser->ItemContentEncoded[$x], $item);
            $item = str_replace("~~~ItemLink~~~", $rss_parser->ItemLink[$x], $item);
            $item = str_replace("~~~ItemPubDate~~~", $rss_parser->ItemPubDate[$x], $item);
            $item = str_replace("~~~ItemGuid~~~", $rss_parser->ItemGuid[$x], $item);
            $item = str_replace("~~~ItemPubLongDate~~~", date($LongDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubShortDate~~~", date($ShortDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubLongTime~~~", date($LongTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubShortTime~~~", date($ShortTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemEnclosureUrl~~~", $rss_parser->ItemEnclosureURL[$x], $item);
			$item = str_replace("~~~ItemEnclosureType~~~", $rss_parser->ItemEnclosureType[$x], $item);
			$item = str_replace("~~~ItemEnclosureLength~~~", $rss_parser->ItemEnclosureLength[$x], $item);
            $item = str_replace("~~~ItemAuthor~~~", $rss_parser->ItemAuthor[$x], $item);
            $item = str_replace("~~~ItemItunesSummary~~~", $rss_parser->ItemItunesSummary[$x], $item);
            $item = str_replace("~~~ItemItunesSubtitle~~~", $rss_parser->ItemItunesSubtitle[$x], $item);
	        $item = str_replace("~~~ItemItunesDuration~~~", $rss_parser->ItemItunesDuration[$x], $item);
	      $item = str_replace("~~~ItemCount~~~", $counter, $item);
	      $counter++; 
            $allitems .= $item;
            $x--;
            if ($x >= count($rss_parser->ItemTitle) + $FeedMaxItems) {
              //
              // This is at least one more item so use the Alternate definition
              //
              $item = str_replace("~~~ItemTitle~~~", $rss_parser->ItemTitle[$x], $block2);
              $item = str_replace("~~~ItemDescription~~~", $rss_parser->ItemDescription[$x], $item);
              $item = str_replace("~~~ItemContentEncoded~~~", $rss_parser->ItemContentEncoded[$x], $item);
              $item = str_replace("~~~ItemLink~~~", $rss_parser->ItemLink[$x], $item);
              $item = str_replace("~~~ItemPubDate~~~", $rss_parser->ItemPubDate[$x], $item);
              $item = str_replace("~~~ItemGuid~~~", $rss_parser->ItemGuid[$x], $item);
              $item = str_replace("~~~ItemPubLongDate~~~", date($LongDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubShortDate~~~", date($ShortDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubLongTime~~~", date($LongTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubShortTime~~~", date($ShortTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemEnclosureUrl~~~", $rss_parser->ItemEnclosureURL[$x], $item);
				$item = str_replace("~~~ItemEnclosureType~~~", $rss_parser->ItemEnclosureType[$x], $item);
				$item = str_replace("~~~ItemEnclosureLength~~~", $rss_parser->ItemEnclosureLength[$x], $item);
              $item = str_replace("~~~ItemAuthor~~~", $rss_parser->ItemAuthor[$x], $item);
              $item = str_replace("~~~ItemItunesSummary~~~", $rss_parser->ItemItunesSummary[$x], $item);
              $item = str_replace("~~~ItemItunesSubtitle~~~", $rss_parser->ItemItunesSubtitle[$x], $item);
	      $item = str_replace("~~~ItemItunesDuration~~~", $rss_parser->ItemItunesDuration[$x], $item);
	      $item = str_replace("~~~ItemCount~~~", $counter, $item);
	      $counter++; 
              $allitems .= $item;
            }
          }
        } else {


// find a specific item - set x and loop limit - bk 
    $x = 0;
    if (isset($_REQUEST["item"])) {
    $x = $_REQUEST["item"] - 1 ;
    $loop_limit = $x + 1;
        if (isset($_REQUEST["max"])) {
        $loop_limit = $x + $_REQUEST["max"] ;
        }
  }


          for ($x ; $x < $loop_limit; $x++) {


	    $item = str_replace("~~~StartTrack~~~", $loop_limit, $item);
            $item = str_replace("~~~ItemTitle~~~", $rss_parser->ItemTitle[$x], $block1);
            $item = str_replace("~~~ItemDescription~~~", $rss_parser->ItemDescription[$x], $item);
            $item = str_replace("~~~ItemContentEncoded~~~", $rss_parser->ItemContentEncoded[$x], $item);
            $item = str_replace("~~~ItemLink~~~", $rss_parser->ItemLink[$x], $item);
            $item = str_replace("~~~ItemPubDate~~~", $rss_parser->ItemPubDate[$x], $item);
            $item = str_replace("~~~ItemGuid~~~", $rss_parser->ItemGuid[$x], $item);
            $item = str_replace("~~~ItemPubLongDate~~~", date($LongDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubShortDate~~~", date($ShortDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubLongTime~~~", date($LongTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemPubShortTime~~~", date($ShortTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
            $item = str_replace("~~~ItemEnclosureUrl~~~", $rss_parser->ItemEnclosureURL[$x], $item);
			$item = str_replace("~~~ItemEnclosureType~~~", $rss_parser->ItemEnclosureType[$x], $item);
			$item = str_replace("~~~ItemEnclosureLength~~~", $rss_parser->ItemEnclosureLength[$x], $item);
            $item = str_replace("~~~ItemAuthor~~~", $rss_parser->ItemAuthor[$x], $item);
            $item = str_replace("~~~ItemItunesSummary~~~", $rss_parser->ItemItunesSummary[$x], $item);
            $item = str_replace("~~~ItemItunesSubtitle~~~", $rss_parser->ItemItunesSubtitle[$x], $item);
	        $item = str_replace("~~~ItemItunesDuration~~~", $rss_parser->ItemItunesDuration[$x], $item);
	      $item = str_replace("~~~ItemCount~~~", $counter, $item);
	      $counter++; 
            $allitems .= $item;
		$x++;
            if ($x < $loop_limit) {
              //
              // This is at least one more item so use the Alternate definition
              //
	      $item = str_replace("~~~StartTrack~~~", $loop_limit, $item);
              $item = str_replace("~~~ItemTitle~~~", $rss_parser->ItemTitle[$x], $block2);
              $item = str_replace("~~~ItemDescription~~~", $rss_parser->ItemDescription[$x], $item);
              $item = str_replace("~~~ItemContentEncoded~~~", $rss_parser->ItemContentEncoded[$x], $item);
              $item = str_replace("~~~ItemLink~~~", $rss_parser->ItemLink[$x], $item);
              $item = str_replace("~~~ItemPubDate~~~", $rss_parser->ItemPubDate[$x], $item);
              $item = str_replace("~~~ItemGuid~~~", $rss_parser->ItemGuid[$x], $item);
              $item = str_replace("~~~ItemPubLongDate~~~", date($LongDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubShortDate~~~", date($ShortDateFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubLongTime~~~", date($LongTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemPubShortTime~~~", date($ShortTimeFormat, $rss_parser->ItemPubDate_t[$x]), $item);
              $item = str_replace("~~~ItemEnclosureUrl~~~", $rss_parser->ItemEnclosureURL[$x], $item);
	     		$item = str_replace("~~~ItemEnclosureType~~~", $rss_parser->ItemEnclosureType[$x], $item);
		    	$item = str_replace("~~~ItemEnclosureLength~~~", $rss_parser->ItemEnclosureLength[$x], $item);              
              $item = str_replace("~~~ItemAuthor~~~", $rss_parser->ItemAuthor[$x], $item);
              $item = str_replace("~~~ItemItunesSummary~~~", $rss_parser->ItemItunesSummary[$x], $item); 
              $item = str_replace("~~~ItemItunesSubtitle~~~", $rss_parser->ItemItunesSubtitle[$x], $item);
	      $item = str_replace("~~~ItemItunesDuration~~~", $rss_parser->ItemItunesDuration[$x], $item);
	      $item = str_replace("~~~ItemCount~~~", $counter, $item);
	      $counter++; 
		$allitems .= $item;              
            }
          }
        }
        $template = str_replace("~~~BeginItemsRecord~~~".$WholeBlock."~~~EndItemsRecord~~~", $allitems, $template);
      }
    }
  }

  // Since &apos; is not HTML, but is xml convert.
  $template = str_replace("&apos;", "'", $template);
  $template = str_replace("~~~FeedItemsCount~~~", $counter - 1, $template);
  
  
  // set a variety of header content types based upon the requested template - bk 
  if ($_REQUEST["template"] == "smil") {
  header("Content-type: application/smil");
  header("Content-Disposition: inline; filename=\"playlist.smil\"");
  }
  
  if ($_REQUEST["template"] == "pls") {
  header("Content-type: audio/x-scpls");
  header("Content-Disposition: inline; filename=\"playlist.pls\"");
  }
  
  if ($_REQUEST["template"] == "m3u") {
  header("Content-type: audio/x-scpls");
  header("Content-Disposition: inline; filename=\"playlist.m3u\"");
  }
  
  if ($_REQUEST["template"] == "xspf") {
  header("Content-type: application/xspf+xml");
  header("Content-Disposition: inline; filename=\"playlist.xml\"");
  }
  
  // real audio mimetype, added 2007-01-26 pbull
  if ($_REQUEST["template"] == 'ram') {
    header('Content-Type: audio/x-pn-realaudio');
    header("Content-Disposition: inline; filename=\"playlist.ram\"");
  }
  
  // windows media mimetype, added 2007-01-26 pbull
  if ($_REQUEST["template"] == 'asx') {
    header('Content-Type: video/x-ms-asf');
    header("Content-Disposition: inline; filename=\"playlist.asx\"");
  }
  
  
  // filter out some bad characters - bk 08/28/06
  $template = str_replace("\xA9", "&copy;", $template);
  $template = str_replace("\r", "", $template);
  $template = str_replace("\xC0", "", $template);
  $template = str_replace("\xC1", "", $template);
  $template = str_replace("\xC2", "", $template);
  $template = str_replace("\xC3", "", $template);
  $template = str_replace("\xC4", "", $template);
  $template = str_replace("\xC5", "", $template);
  $template = str_replace("\xE4", "a", $template);
  $template = str_replace("\xE1", "a", $template);
  $template = str_replace("\a", "a", $template);
  $template = str_replace("%E1", "a", $template);
  
  function corrige_acent($texto){
    //$acentu_var = array("A#", "AS", "A(c)", "Ao", "Aa", "A", "A!", "A", "Au", "a");
    $acentu_var = array("Ì£", "Ì¤", "Ì©", "Ì¼", "Ì»", "ÌÁ", "Ìµ");
    $corrig_var = array("a", "c", "e", "u", "e", "o", "i", "a");
    $novo_texto = str_replace($acentu_var, $corrig_var, $texto);
    return $novo_texto;
  }
  $template = corrige_acent($template);
  
  // output completed html
  echo $template;
}
