<?php

function wordpress_diraudio($cat=false) {
	include(dirname(__FILE__).'/cache'.($cat?'cat':'').'.php');
	if($_GET['refresh'] != 'true' && $aDate > (time() - 24 * 60 * 60)) {
		echo $aCache;
		return;
	}

	// 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/www/files/diraudio/Yuttadhammo';

	

	$countno = 0;

	

	// read through the directory and filter files to an array

	$handle = opendir($directory);

	while (FALSE !== ($entry = readdir($handle)))

	{

		if($entry != '.' && $entry != '..')

		{

			$path = $directory.'/'.$entry;



			if(is_dir($path) && !is_link($path)) 

			{

				$handle2 = opendir($path);

				while (FALSE !== ($entry2 = readdir($handle2)))

				{

					$ps = strpos(strtolower($entry2), $filter);

					if (!($ps === false)) {

                        if(!$cat) $album = '20'.substr($entry2,0,2);
						else $album = $entry;
						
						$items[$album][] = $entry2; 

						$albums[$entry2] = $entry; 

					} 

				}

				closedir($handle2);

				$itemsno[$entry] = $countno; 

				$countno++;

			}

		}

	}

	closedir($handle);


	// include getID3() library (can be in a different directory if full path is specified)

	require_once('/yuttadhammo/siri/www/files/getid3/getid3.php');


    $count = 0;

    $count2 = 0;

    $onehid = 0;

$form = '<script>
	function togglePlayer(id,track) {
		var play = document.getElementById(\'player\'+id);
		if (play.innerHTML == \'\') 
			play.innerHTML=\'<object data="http://flash-mp3-player.net/medias/player_mp3_maxi.swf" width="400" height="20"><param name="movie" value="http://flash-mp3-player.net/medias/player_mp3_maxi.swf" /><param name="bgcolor" value="#ffffff" /><param name="FlashVars" value="mp3=http://www.sirimangalo.org/files/diraudio/Yuttadhammo/\'+track+\'&width=300&height=15&autoplay=1&showstop=1&showinfo=1&showvolume=1&buttonwidth=20&volumewidth=40&bgcolor1=00aa00&bgcolor2=007700&slidercolor2=aaaaaa&sliderovercolor=ffff52" /></object>\';  
		else play.innerHTML = \'\';
		
	}
</script>
<ul>';

	if($cat) ksort($items);
	else krsort($items);

	foreach($items as $album => $track_array) {

                $form .= '<li><b><a href="javascript:void(0)" onclick="jQuery(\'#mp3'.$album.'\').toggle()">'.$album.'</a></b><ul style="display:none" id="mp3'.$album.'">';

		natsort($track_array);

		if(!$cat) $track_array = array_reverse($track_array, true);

		foreach($track_array as $track) {

            $album = $albums[$track];

			$trackloc = $directory.'/'.$album.'/'.$track;



		// Initialize getID3 engine

			$getID3 = new getID3;
			$ThisFileInfo = $getID3->analyze($trackloc);



			$myFileSize=filesize($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;


                        $form .= '<li><a href="javascript:void(0)" onclick="togglePlayer('.$onehid.',\''.$album.'/'.$track.'\')">'.$trackname.'</a> (<a href="http://yuttadhammo.sirimangalo.org/mp3-'.$album.'/'.substr($track,0,-4).'" target="yd_audio" title="permalink to this audio file">#</a>)<div id="player'.$onehid.'"></div></li>';


			$onehid++;

		}

		$form .= '</ul></li>';



	}

	$form .= '</ul>';
	$nCache = '<?php $aDate='.time().';$aCache=\''.str_replace('\'','\\\'',$form).'\';';
	file_put_contents(dirname(__FILE__).'/cache'.($cat?'cat':'').'.php',$nCache);
	echo $form;
}
