<?php
header("Content-type: text/html");
header("Expires: Sat, 05 Nov 2005 00:00:00 GMT");
header("Last-Modified: ".gmdate("D, d M Y H:i:s")." GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");

if(!function_exists('fsockopen')) { die("allow_url_fopen is disabled in your php.ini! stream data not fetchable"); }

if(file_exists("listen/streamconfig.php")) { include("listen/streamconfig.php"); }
else { die("<small><em>Streamstatus-Einstellungen sind (noch) nicht gesetzt</em></small>"); }

$page = "";
// Daten holen
if(IsSet($sc_server[1]) && $sc_server[1]!='' && IsSet($sc_port[1]) && $sc_port[1]!='' && IsSet($sc_adminpw[1]) && $sc_adminpw[1]!='') {
	$uri  = "/admin.cgi?mode=viewxml".(IsSet($sc_id[1]) && $sc_id[1]>=1 ? "&sid=".$sc_id[1] : "");
	$sock = @fsockopen($sc_server[1], $sc_port[1], $errno, $errstr, 10);
		if($sock && $errno == 0) {			
			fputs ($sock, "GET ".$uri." HTTP/1.1\r\n");
			fputs ($sock, "Host: ".$sc_server[1].":".$sc_port[1]."\r\n");
			fputs ($sock, "User-Agent: XML Getter (Mozilla Compatible)\r\n");
			fputs ($sock, "Authorization: Basic ".base64_encode("admin:".$sc_adminpw[1])."\r\n");
			fputs ($sock, "Connection: close\r\n\r\n");
			while(!feof($sock))
				$page .= fgets($sock, 4096);
			@fclose($sock);
		

			// Daten zerlegen
			preg_match("~<STREAMSTATUS>(.*)</STREAMSTATUS>~", $page, $current_status);
			preg_match("~<SONGTITLE>(.*)</SONGTITLE>~", $page, $current_song);
			preg_match("~<CURRENTLISTENERS>(.*)</CURRENTLISTENERS>~", $page, $current_listeners);
		}
	if(IsSet($current_status[1]) && $current_status[1]==1) {
		$songtitle = $current_song[1];
	}
	// Stream falsch konfiguriert oder nicht erreichbar:
	else { $songtitle = false; }
}
// gar keine oder unvollst. Streamdaten konfiguriert fuer Stream 1
else { $songtitle = false; }

// Weitere Hoerer ermitteln?
$more_listeners = 0;
$page = "";
if($songtitle && IsSet($_GET['hoerer']) && $_GET['hoerer']=='1') {
	for($i=2;$i<=3;$i++) {
    $page = "";$listeners[1]=0;
		if(IsSet($sc_server[$i]) && $sc_server[$i]!='' && IsSet($sc_port[$i]) && $sc_port[$i]!='' && IsSet($sc_adminpw[$i]) && $sc_adminpw[$i]!='') {
			$uri  = "/admin.cgi?mode=viewxml".(IsSet($sc_id[$i]) && $sc_id[$i]>=1 ? "&sid=".$sc_id[$i] : "");
			$sock = @fsockopen($sc_server[$i], $sc_port[$i], $errno, $errstr, 10);
			if($sock && $errno == 0) {				
				fputs ($sock, "GET ".$uri." HTTP/1.1\r\n");
				fputs ($sock, "Host: ".$sc_server[$i].":".$sc_port[$i]."\r\n");
				fputs ($sock, "User-Agent: XML Getter (Mozilla Compatible)\r\n");
				fputs ($sock, "Authorization: Basic ".base64_encode("admin:".$sc_adminpw[$i])."\r\n");
				fputs ($sock, "Connection: close\r\n\r\n");
				while(!feof($sock))
					$page .= fgets($sock, 4096);
				@fclose($sock);

				// Daten zerlegen
				preg_match("~<STREAMSTATUS>(.*)</STREAMSTATUS>~", $page, $current_status);
			}
			if(IsSet($current_status[1]) && $current_status[1]=='1') {
				preg_match("~<CURRENTLISTENERS>(.*)</CURRENTLISTENERS>~", $page, $listeners);
				$more_listeners+=$listeners[1];
			}

		}
	}
}

if($songtitle) {
	echo "<strong>".$songtitle."</strong>\n";
	if(IsSet($_GET['hoerer']) && $_GET['hoerer']=='1') {
		echo "<br />\n<small>(".($current_listeners[1]+$more_listeners)." Zuh&ouml;rer)</small>\n";
	}
}
else {
	echo "<em>- offline -</em>";
}
?>