<? 
// DirCaster v0.4: Automatic iPodder RSS feed maker
// Written by Ryan King 			www.shadydentist.com
// id3 class from frummel
// No license or warrantee expressed, implied, enclosed, etc.

// Drop this script in a directory and it will generate an RSS feed suitable for
// iPodder, etc based off the MP3 files in that directory. To 'cast a new
// file, simply upload it to the directory containing this script.

// Feed information is based on id3 tags, requires PHP4 or better. 
//
// Note: Avoid spaces in your MP3 filenames to avoid ugly filenames coming out
// of iPodder

// Instructions
// 1. Place this file on a server with supports PHP4 or greater.
// 2. Place MP3 files to feed in the same directory, add new files at will
// 3. Point iPodder to the URL of this file.
// ex. http://www.mysite.com/podcast/dircaster.php
// 4. Set the following varibles to match your setup if you wish

// Maximum number of mp3s to offer in a feed
$maxFeed = 100;

//Title of feed/site	Used by iPodder for download subdirectory name
$titleTAG="Marcos Gedanken";

//iPodder 1.0 seems to ignore everything below
//URL of site feed applies too
$linkTAG="http://pintman.blogspot.com";
//Description
$descriptionTAG="Marcos Gedanken, Irrungen und Wirrungen";
//Feed language en-us = english USA
$languageTAG="de-de";
//Copyright for feed
$copyrightTAG="Copyright Marco Bakera";
//Your email address
$webMasterTAG="pintman@gmx.de";
//Name of feed generator
$generatorTAG="dirCastv0.4";
// Time to live
$ttlTAG=60;

///////////////
// You should not need to edit anything below this point.
/////////////
// Main Code
///////////
header('Content-type: text/xml', true);
$rootMP3URL = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$rootMP3URL =  substr($rootMP3URL, 0, strrpos ($rootMP3URL, "/")); // Trim off script name itself

print "<?xml version=\"1.0\"?>\n";
print "<rss version=\"2.0\">\n";
print"	<channel>\n";
print"		<title>$titleTAG</title>\n";
print"		<link>$linkTAG</link>\n";
print"		<description>$descriptionTAG</description>\n";
print"		<lastBuildDate>" . date("r") ."</lastBuildDate>\n";
print"		<language>$languageTAG</language>\n";
print"		<copyright>$copyrightTAG</copyright>\n";
print"		<generator>$generatorTAG</generator>\n";
print"		<webMaster>$webMasterTAG</webMaster>\n";
print"		<ttl>$ttlTAG</ttl>\n\n";

$dirArray = getDir(".");	// Get a list of the current directory
while (list($filename, $filedate) = each($dirArray)AND $maxFeed > 0) {
	$mp3file = new CMP3File;
	$mp3file->getid3 ($filename);
	print "<item>\n";
	echo ("<title>$mp3file->title</title>\n");
	echo ("<link>$rootMP3URL/". htmlentities(str_replace(" ", "%20", $filename)) ."</link>\n");
	echo ("<description>$mp3file->title - $mp3file->album - $mp3file->artist</description>\n");
	echo ("<pubDate>".date("r",$filedate)."</pubDate>\n");
	echo ("<enclosure url=\"".htmlentities($rootMP3URL)."/". htmlentities(str_replace(" ", "%20", $filename)) ."\" length=\"");
	echo filesize($filename);
	echo ("\" type=\"audio/mpeg\"/>\n");	// Training slash for XML
	print "</item>\n\n";
	$maxFeed--;
}

print "</channel>\n</rss>\n";

// Functions and Classes
function stripJunk ($text) {
// Strip non-text characters
	for ($c=0; $c<strlen($text); $c++) {
		if (ord($text[$c]) >= 32 AND ord($text[$c]) <= 122)
			$outText.=$text[$c];
	}
	return $outText;
}

class CMP3File {
    //properties
    var $title;
    var $artist;
    var $album;
    var $year;
    var $comment;
    var $genre;
    
    function getid3 ($file)
    { // read the ID3 or ID3v2 tag from an MP3 file
        if (file_exists($file))
        { //after verifying the file exists,
            $id_start = filesize($file) - 128;
            
            $fp = fopen($file, "r");
            fseek($fp, $id_start);
            $tag = fread($fp,3);
            if ($tag == "TAG")
            {
                $this->title    = stripJunk(trim(fread($fp, 30)));
                $this->artist    = stripJunk(trim(fread($fp, 30)));
                $this->album    = stripJunk(trim(fread($fp, 30)));
                $this->year        = stripJunk(trim(fread($fp, 4)));
                $this->comment    = stripJunk(trim(fread($fp, 30)));
                $this->genre    = stripJunk(trim(fread($fp, 1)));
                fclose($fp);
                return true;
            }
            else
            { // No ID3 tag
            	  fclose($fp);
                return false;
            }
        } else //the file doesn't exist
            return false;
    }
}

function getDir($mp3Dir) {	
// Returns directory as array[file]=date in newest to oldest order
	$dirArray = array();
	$diskdir = "./$mp3Dir/";
	if (is_dir($diskdir)) {
		$dh = opendir($diskdir);
		while (($file = readdir($dh)) != false ) {
			if (filetype($diskdir . $file) == "file" && $file[0]  != ".") {
				if (strrchr(strtolower($file), ".") == ".mp3") {
					$ftime = filemtime($mp3Dir."/".$file); 
					$dirArray[$file] = $ftime;
				}
			}
		}
		closedir($dh);
	}
	asort($dirArray);
	$dirArray = array_reverse($dirArray);
	return $dirArray;
}

?>
