<?php

	ob_start();
	include 'inc/config.php';

	// Generate Playlist and send it as attachment.
	session_write_close();

	header("Content-Description: File Transfer");
	header("Content-Transfer-Encoding: binary");
	header("Pragma: public");
	header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

	switch($_GET['name']) {

		case "listen.asx":

			header("Content-Type: video/x-ms-asf");
			header("Content-Disposition: attachment; filename=\"Listen.asx\"");

			echo "<asx version=\"3.0\">\r\n";

			foreach ($config['streams'] as $name => $link) {
				if ($name == 'stats') { continue; }
				echo "<title>ILHM Radio</title>\r\n<entry>\r\n<title>{$name}</title>\r\n<ref href=\"" . preg_replace("/;.*$/", '', $link['mp3']) . "\"/>\r\n</entry>\r\n";
			}

			echo '</asx>';

			break;

		case "listen.m3u":

			header("Content-Type: audio/mpeg-url");
			header("Content-Disposition: attachment; filename=\"Listen.m3u\"");

			$i=0;
			echo "#EXTM3U\r\n";
			foreach ($config['streams'] as $name => $link) {
				if ($name == 'stats') { continue; }
				echo "#EXTINF:{$i},{$name}\r\n" . preg_replace("/;.*$/", '', $link['mp3']) . "\r\n";
				$i++;
				echo "#EXTINF:{$i},{$name}\r\n" . preg_replace("/;.*$/", '', $link['oga']) . "\r\n";
				$i++;
			}

			break;

		default:
			header("Content-Type: audio/x-scpls");
			header("Content-Disposition: attachment; filename=\"Listen.pls\"");

			$i=0;
			echo "[playlist]\r\n";
			foreach ($config['streams'] as $name => $link) {
				if ($name == 'stats') { continue; }
				$i++;
				echo "File{$i}=" . preg_replace("/;.*$/", '', $link['mp3']) . "\r\nTitle{$i}={$name}\r\nLength{$i}=0\r\n\r\n";
			}

			echo "NumberOfEntries={$i}\r\n\r\nVersion=2";

			break;
	}


?>