<?php
// dircaster 0.4e_jp(UTF-8)
// Warren Stone氏の許可を取って日本語環境に変更＋設定ファイルの外部化 http://www.ipodcasting.jp/
// ライセンスはWarren Stone氏に準じます。
// dircaster 0.4e
// improved the handling of all five esc characters required for xml
// dircaster 0.4c/d
// added in the audio path property to allow music files to be stored
// BELOW the directory containing dircaster.
// Copy and paste this information ABOVE the main code section of dircaster.
// replacing all of the default settings in dircaster.

// dircasterの設定読み込み
require_once 'dc_config.php';


/* Do not edit anything below this line! **********************/
/* Main Code **************************************************/
// dirCaster 0.4e_jp released 11/12/2005. by Ken Nishikawa (http://www.ipodcasting.jp/)
// translate dirCaster 0.4e into Japanese. (+ sepalate config.ini) 
// dirCaster 0.4e, released 07/23/2005. Open source code but please
// leave all references to prior work intact when making changes.
// based on dircaster 0.4 by Ryan King (http://www.shadydentist.com)
// ID3v2.x tag support added by Warren Stone <fasttr@gmail.com> and
// utilizing getid3 library by James Heinrich <info@getid3.org>, 
// http://www.getid3.org. iTunes specific tag support by Warren Stone
//
// need to include the code to read id3 tags
// also reads lots of different formats
// we'll implement mp3, mpa (quicktime), asf (wma) and riff (wav/avi)
// alter the path to your getid3 directory location
require_once($id3LibPath);


// lets make all of the user set variables xml compliant just in case
$titleTAG = escChars ($titleTAG);
$descriptionTAG = escChars ($descriptionTAG);
$copyrightTAG = escChars ($copyrightTAG);
$languageTAG = escChars ($languageTAG);
$webMasterTAG = escChars ($webMasterTAG);
$generatorTAG = escChars ($generatorTAG);;
$rssImageTitleTAG = escChars ($rssImageTitleTAG);
$summaryTAG = escChars ($summaryTAG);
$authorTAG = escChars ($authorTAG);
$ownerNameTAG = escChars ($ownerNameTAG);
$ownerEmailTAG = escChars ($ownerEmailTAG);
$topCategoryTAG = escChars ($topCategoryTAG);
$subCategoryTAG = escChars ($subCategoryTAG);
$keywordTAG = escChars ($keywordTAG);
$imageTitleTAG = escChars ($imageTitleTAG);


// header("Content-Type: application/xml");
header('Content-type: text/xml', true);
// header("Content-Type: text/html");
// path manipulations
$rootMP3URL = "http://" . $_SERVER[HTTP_HOST] . $_SERVER[REQUEST_URI];
$rootMP3URL =  substr($rootMP3URL, 0, strrpos ($rootMP3URL, "/")); // Trim off script name itself
// add a slash to the audio path if it's not set to the current directory
if ($audioPath != "./") $audioPath = $audioPath."/";
// let's remove the "dot" and any double slashes that might occur
$filePath = str_replace("//","/", str_replace(".","", $audioPath));

print "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
print "<rss xmlns:itunes=\"$nameSpaceTAG\" version=\"2.0\">\n";
//print"<html><head></head><body>\n";
print"<channel>\n";
// original dircaster channel info
print"<path>$filePath</path>\n";
print"<audiopath>$audioPath</audiopath>\n";
print"<title>$titleTAG</title>\n";
print"<link>$linkTAG</link>\n";
print"<description>$descriptionTAG</description>\n";
print"<category>$topCategoryTAG</category>\n";
print"<pubDate>" . date("r") ."</pubDate>\n";
print"<lastBuildDate>" . date("r") ."</lastBuildDate>\n";
print"<language>$languageTAG</language>\n";
print"<copyright>$copyrightTAG</copyright>\n";
print"<generator>$generatorTAG</generator>\n";
print"<managingEditor>$ownerEmailTAG ($ownerNameTAG)</managingEditor>\n";
print"<webMaster>$webMasterTAG</webMaster>\n";
print"<ttl>$ttlTAG</ttl>\n\n";
// new itunes channel stuff
// itunes author tag
print"<itunes:author>$authorTAG</itunes:author>\n";
// itune subtitle tag
print"<itunes:subtitle>$descriptionTAG</itunes:subtitle>\n";
// itunes category tags
print"<itunes:category text=\"".$topCategoryTAG."\">\n";
print"<itunes:category text=\"".$subCategoryTAG."\"/>\n";
print"</itunes:category>\n";
// itunes summary tag
print"<itunes:summary>$summaryTAG</itunes:summary>\n";
// itunes owner tags
print"<itunes:owner>\n";
print"<itunes:name>$ownerNameTAG</itunes:name>\n";
print"<itunes:email>$ownerEmailTAG</itunes:email>\n";
print"</itunes:owner>\n";
// itunes explicit tag
print"<itunes:explicit>$explicitTAG</itunes:explicit>\n";
// image tags
print"<image>\n";
print"<url>".$rssImageUrlTAG."</url>\n";
print"<title>".$rssImageTitleTAG."</title>\n";
print"<link>".$rssImageLinkTAG."</link>\n";
print"</image>\n";
// itunes image link
print"<itunes:image href=\"$imageUrlTAG\" />\n";

// item listing
$dirArray = getDir($audioPath, $sftypes);	// Get a list of the current directory
 while (list($filename, $filedate) = each($dirArray)AND $maxFeeds > 0) {
	$mp3file = new CMP3File;
	$mp3file->getid3 ($filename);
	echo "<item>\n";
	// title tag
	$mp3title = str_replace("_", " ", $mp3file->title);
	if ($mp3title == "") $mp3title = str_replace("/","",strrchr($filename,"/"));
	echo ("<title>".$mp3title."</title>\n"); // title string
	// note filename returns with the audio path as a prefix
	$url = $rootMP3URL."/". htmlentities(str_replace(" ", "%20", $filename));
	echo ("<link>".$linkTAG."</link>\n"); //link string
	// artist

	$mp3artist = $mp3file->artist;
	$mp3album  = $mp3file->album;
	$mp3mime_type = $mp3file->mime_type;
	$mp3comment = $mp3file->comment;

	echo ("<author>$ownerEmailTAG</author>\n");
	echo ("<itunes:album>$mp3album</itunes:album>\n");
	echo ("<description>$title - $mp3album - ".$mp3artist."</description>\n");
	echo ("<pubDate>".date("r",$filedate)."</pubDate>\n");
	echo ("<enclosure url=\"".$url."\" length=\"".filesize($filename)."\" type=\"$mp3mime_type\"/>\n");
	echo ("<itunes:author>$mp3artist</itunes:author>\n");
	echo ("<itunes:subtitle>$mp3comment</itunes:subtitle>\n");
	echo ("<itunes:keywords>$keywordTAG</itunes:keywords>\n");
// check to see if we want image info for each item
	if ($imageItemTAG == "yes") {
	echo ("<itunes:image href=\"$imageUrlTAG\" />\n");
		}
	$fix_duration = $mp3file->duration;	
	if (strlen($fix_duration) < 3) {
		$fix_duration = "00:00:".$fix_duration;
		}
	if (strlen($fix_duration) < 6) {
		$fix_duration = "00:".$fix_duration;
		}
	
	echo ("<itunes:duration>$fix_duration</itunes:duration>\n");
		
	print "</item>\n\n";
	$maxFeed--;
}

print "</channel>\n</rss>\n";

function escChars ($text) {
// Strip non-text characters
	$fixed = str_replace("&","&amp;", $text);   // ampersand
	$fixed = str_replace("<","&lt;",$fixed);	// less than
	$fixed = str_replace("©","&#169;",$fixed);	// copyright
	$fixed = str_replace("'","&apos;",$fixed);	// apostrophe
	$fixed = str_replace("\"","&quot;",$fixed);	// double quotes
	$outText = str_replace(">","&gt;",$fixed);	// greater than
	return $outText;
}

class CMP3File {
    //properties
    var $title;
    var $artist;
    var $album;
    var $year;
    var $comment;
    var $genre;
	var $composer;
	var $copyright;
	var $mime_type;
	var $duration;
	
// need to enter this with the audio folder prefix already attached to the filename
function getid3 ($file) {
// Initialize getID3 engine
	if (file_exists($file))
	{ //after verifying the file exists,
		$getID3 = new getID3;

		// Analyze file and store returned data in $ThisFileInfo
		$ThisFileInfo = $getID3->analyze($file);

		// Optional: copies data from all subarrays of [tags] into [comments] so
		// metadata is all available in one location for all tag formats
		// metainformation is always available under [tags] even if this is not called
		getid3_lib::CopyTagsToComments($ThisFileInfo);

		// Output desired information in whatever format you want
		// Note: all entries in [comments] or [tags] are arrays of strings
		// See structure.txt for information on what information is available where
		// or check out the output of /demos/demo.browse.php for a particular file
		// to see the full detail of what information is returned where in the array
		//echo @$ThisFileInfo['comments']['artist'][0]; // artist from any/all available tag formats

	$mim = @$ThisFileInfo['mime_type']; // artist from any/all available tag formats
	$dur = @$ThisFileInfo['playtime_string']; // play duration from any/all available tag formats
	switch (strrchr(strtolower($file), "."))
		{
		case ".mp3";
			$tit = @$ThisFileInfo['comments_html']['title'][0];
			$alb = @$ThisFileInfo['comments_html']['album'][0];
			$art = @$ThisFileInfo['comments_html']['artist'][0];
			$com = @$ThisFileInfo['comments_html']['comment'][0];
			$cmp = @$ThisFileInfo['comments_html']['composer'][0];
			$gen = @$ThisFileInfo['comments_html']['genre'][0];
			break;
		case ".m4a";
			$tit = @$ThisFileInfo['quicktime']['comments']['title'][0]; // artist from any/all available tag formats
			$alb = @$ThisFileInfo['quicktime']['comments']['album'][0]; // artist from any/all available tag formats
			$art = @$ThisFileInfo['quicktime']['comments']['artist'][0]; // artist from any/all available tag formats
			$com = @$ThisFileInfo['quicktime']['comments']['comment'][0]; // artist from any/all available tag formats
			$cmp = @$ThisFileInfo['quicktime']['comments']['writer'][0]; // artist from any/all available tag formats
			//	$gen = @$ThisFileInfo['quicktime']['comments']['genre'][0]; // artist from any/all available tag formats
			break;
		case ".m4b";
			$tit = @$ThisFileInfo['quicktime']['comments']['title'][0]; // artist from any/all available tag formats
			$alb = @$ThisFileInfo['quicktime']['comments']['album'][0]; // artist from any/all available tag formats
			$art = @$ThisFileInfo['quicktime']['comments']['artist'][0]; // artist from any/all available tag formats
			$com = @$ThisFileInfo['quicktime']['comments']['comment'][0]; // artist from any/all available tag formats
			$cmp = @$ThisFileInfo['quicktime']['comments']['writer'][0]; // artist from any/all available tag formats
			//	$gen = @$ThisFileInfo['quicktime']['comments']['genre'][0]; // artist from any/all available tag formats
			break;
		case ".mov";
			$tit = @$ThisFileInfo['quicktime']['comments']['title'][0]; // artist from any/all available tag formats
			$alb = @$ThisFileInfo['quicktime']['comments']['album'][0]; // artist from any/all available tag formats
			$art = @$ThisFileInfo['quicktime']['comments']['artist'][0]; // artist from any/all available tag formats
			$com = @$ThisFileInfo['quicktime']['comments']['comment'][0]; // artist from any/all available tag formats
			$cmp = @$ThisFileInfo['quicktime']['comments']['director'][0]; // artist from any/all available tag formats
			//	$gen = @$ThisFileInfo['quicktime']['comments']['genre'][0]; // artist from any/all available tag formats
			break;
		case ".asf";
			$tit = @$ThisFileInfo['asf']['comments']['title'][0]; // artist from any/all available tag formats
			$alb = @$ThisFileInfo['asf']['comments']['album'][0]; // artist from any/all available tag formats
			$art = @$ThisFileInfo['asf']['comments']['artist'][0]; // artist from any/all available tag formats
			$com = @$ThisFileInfo['asf']['comments']['comment'][0]; // artist from any/all available tag formats
			$cmp = @$ThisFileInfo['asf']['comments']['composer'][0]; // artist from any/all available tag formats
			$gen = @$ThisFileInfo['asf']['comments']['genre'][0]; // artist from any/all available tag formats
			break;
		case ".wma";
			$tit = @$ThisFileInfo['asf']['comments']['title'][0]; // artist from any/all available tag formats
			$alb = @$ThisFileInfo['asf']['comments']['album'][0]; // artist from any/all available tag formats
			$art = @$ThisFileInfo['asf']['comments']['artist'][0]; // artist from any/all available tag formats
			$com = @$ThisFileInfo['asf']['comments']['comment'][0]; // artist from any/all available tag formats
			$cmp = @$ThisFileInfo['asf']['comments']['composer'][0]; // artist from any/all available tag formats
			$gen = @$ThisFileInfo['asf']['comments']['genre'][0]; // artist from any/all available tag formats
			break;
		default;
			$tit = $file; // artist from any/all available tag formats
		}

	$tit = mb_decode_numericentity($tit, array(0, 0xffff, 0, 0xffff) ,"UTF-8");
	$alb = mb_decode_numericentity($alb, array(0, 0xffff, 0, 0xffff) ,"UTF-8");
	$art = mb_decode_numericentity($art, array(0, 0xffff, 0, 0xffff) ,"UTF-8");
	$com = mb_decode_numericentity($com, array(0, 0xffff, 0, 0xffff) ,"UTF-8");
	$cmp = mb_decode_numericentity($cmp, array(0, 0xffff, 0, 0xffff) ,"UTF-8");
	$gen = mb_decode_numericentity($gen, array(0, 0xffff, 0, 0xffff) ,"UTF-8");

	$this->title 		= escChars ($tit);
	$this->composer 	= escChars ($cmp);
	$this->album 		= escChars ($alb);
	$this->comment 		= escChars ($com);
	$this->copyright 	= escChars ($cmp);
	$this->artist 		= escChars ($art);
	$this->mime_type 	= escChars ($mim);
	$this->duration 	= escChars ($dur);

	return true;
	} else {
	return false; // file doesn't exist
	}
}
}
// mp3Dir can be any directory BELOW the directory where dircaster.php resides
function getDir($mp3Dir, $supported_file_types) {
// 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]  != ".") {
	$fext = strrchr(strtolower($file), ".");
				if (strpos ($supported_file_types, $fext) > 0) {
					$ftime = filemtime($mp3Dir.$file); 
					$dirArray[str_replace("./","",$mp3Dir).$file] = $ftime;
				}
			}
		}
		closedir($dh);
	}
	asort($dirArray);
	$dirArray = array_reverse($dirArray);
	return $dirArray;
}
/* Initial iTunes category listings
/********************************************
Arts & Entertainment
Sub-Categories for Arts & Entertainment:  
Architecture
Books
Design
Entertainment
Games
Performing Arts
Photography
Poetry
Science Fiction
Audio Blogs
Business
Sub-Categories for Business:  
Careers
Finance
Investing
Management
Marketing
Comedy
Education
Sub-Categories for Education:  
K-12
Higher Education
Food
Health
Sub-Categories for Health:  
Diet & Nutrition
Fitness
Relationships
Self-Help
Sexuality
International
Sub-Categories for International:  
Australian
Belgian
Brazilian
Canadian
Chinese
Dutch
French
German
Hebrew
Italian
Japanese
Norwegian
Polish
Portuguese
Spanish
Swedish
Movies & Television
Music
News
Politics
Public Radio
Religion & Spirituality
Sub-Categories for Religion & Spirituality:  
Buddhism
Christianity
Islam
Judaism
New Age
Philosophy
Spirituality
Science
Sports
Talk Radio
Technology
Sub-Categories for Technology:  
Computers
Developers
Gadgets
Information Technology
News
Operating Systems
Podcasting
Smart Phones
Text/Speech
Travel
/********************************************/

?>
