<?php
error_reporting(0); 

$input = $_SERVER['QUERY_STRING'];
//$input = KXGL_FM;

$a = file_get_contents("http://www.streamaudio.com/player/player.aspx?optin=no&station=$input");
//$a = file_get_contents("http://www.streamaudio.com/Player/Player.aspx?Station=$input&filename=");
//$a = file_get_contents("http://www.streamaudio.com/Player/Player.aspx?Station=KSYN_FM&filename=");

function find_between($a, $str1, $str2, $ignore_case = false) {
    $func = $ignore_case ? stripos : strpos;
    $start = $func($a, $str1);
    if ($start === false) {
	return '';
    }

    $start += strlen($str1);
    $end = $func($a, $str2, $start); 
    if ($end === false) {
	return '';
    }

    return substr($a, $start, $end - $start);
}

// echo find_between($a, 'g_mmsURL = "', '";'), "\n";
$c = find_between($a, 'g_mmsURL = "', '";');

//$b = stristr($a, 'SRC="http://www.streamaudio.com/stations/player/pages/newplayer/auth/GenerateASX1.asp?');
//$c = file_get_contents(substr($b, 25, strlen("http://www.streamaudio.com/stations/player/pages/newplayer/auth/GenerateASX1.asp?") + 150));


$handle = @fopen($c,'r');
if($handle !== false){
$c = $c;
//echo $c;
//exit;
}

else {
$x = file_get_contents("http://www.streamaudio.com/player/player.aspx?optin=no&station=$input");
//$y = stristr($x, 'g_ArchiveUrl = ');
//$y1 = substr($y, 16, 45);
$c = find_between($x, 'g_ArchiveUrl = "', '";');
$c .= ".m3u";
}
?>

<html>
<head>
<script type="text/javascript">
<!--
window.location = "<?php echo "$c" ?>"
setTimeout("self.close();",2000) 
//-->
</script>
</head>
<body></body>
</html>