<?php
function createPlaylists() {
	// search for mp3 files. set this to '.flv' or '.jpg' for the other scripts 
	$filter = ".mp3";
	// path to the directory you want to scan
	$directory = '/yuttadhammo/siri/static/diraudio/Yuttadhammo';
	
	$countno = 0;

	$header = '<?xml version="1.0" encoding="UTF-8"?>
<playlist version="1" xmlns="http://xspf.org/ns/0/">';

	$footer = '
	</trackList>
</playlist>';

	// include getID3() library (can be in a different directory if full path is specified)
	require_once('/yuttadhammo/siri/www/files/getid3/getid3.php');
	
	// read through the directory and filter files to an array
	$handle = opendir($directory);
	while (FALSE !== ($entry = readdir($handle)))
	{
		if($entry != '.' && $entry != '..')
		{
			$path = $directory.'/'.$entry;
			$album = $entry;

			if(is_dir($path) && !is_link($path)) 
			{
				$handle2 = opendir($path);
				while (FALSE !== ($entry2 = readdir($handle2)))
				{
					$ps = strpos(strtolower($entry2), $filter);
					if (!($ps === false)) {
						
						$items[] = $entry2; 
					} 
				}
				
				// create XSPF
				
				$xml = $header;
				
				$xml .= "
	<title>$album</title>
	<creator>Yuttadhammo Bhikkhu</creator>
	<info>http://yuttadhammo.sirimangalo.org/</info>
	<trackList>";
				
				foreach($items as $track) {
					$trackloc = $directory.'/'.$album.'/'.$track;
					$getID3 = new getID3;
					$ThisFileInfo = $getID3->analyze($trackloc);
					
					$mp3_title = @$ThisFileInfo['tags']['id3v2']['title'][0];
					if (!$mp3_title) $mp3_title = @$ThisFileInfo['tags']['id3v1']['title'][0];
					$mp3_comment = @$ThisFileInfo['tags']['id3v2']['comments'][1]; // comment from ID3v2
					$mp3_comment_extra = @$ThisFileInfo['tags']['id3v2']['comments'][0];
					if (!$mp3_comment || (isset($mp3_comment_extra) && strlen($mp3_comment_extra) > strlen($mp3_comment))) $mp3_comment = @$ThisFileInfo['tags']['id3v2']['comments'][0];
					if (!$mp3_comment) $mp3_comment = @$ThisFileInfo['tags']['id3v1']['comment'][0];
					$mp3_comment = utf8_encode(str_replace('&','&amp;',$mp3_comment));
					$mp3_title = utf8_encode(str_replace('&','&amp;',$mp3_title));
					if ($mp3_title) $trackname = $mp3_title;
					else $trackname = substr($track,7,-4);
					if($trackname == 'þ') $trackname = substr($track,7,-4);
					if (!$mp3_comment) $mp3_comment = $trackname;					
					
					$duration = $ThisFileInfo['playtime_string'];
					
					$xml .= "
		<track>
			<location>http://www.sirimangalo.org/files/diraudio/Yuttadhammo/$album/$track</location>
			<creator>Yuttadhammo Bhikkhu</creator>
			<album>$album</album>
			<title>$mp3_title</title>
			<annotation>$mp3_comment</annotation>
			<duration>$duration</duration>
			<image>http://static.sirimangalo.org/images/portrait_square.jpg</image>
			<info>http://example.com</info>
		</track>";
				}
				
				$xml .= $footer;
				error_log($directory.'/'.$album.'/playlist.xspf');		
				file_put_contents($directory.'/'.$album.'/playlist.xspf',$xml);
				
				closedir($handle2);
			}
		}
	}
	closedir($handle);

}
createPlaylists();
