<?php
// This wiki plugin reads the status2.xsl info exmpl http://kiilo.dyndns.org:5000/status2.xsl
// it is displayed in a 'fancy' table
// author kiilo@kiilo.dyndns.org
// TODO: no translation yet
// thx: 
// Author: that dude over there at casterclub.com
// Bugs: post in the forums at casterclub
// bugs ??? security ???

function wikiplugin_icestat_help() {
        return tra("Displays Icecast2 Status info").":<br />~np~{ICESTAT()}".tra("URL Icecast2 server:port")."{ICESTAT}~/np~<br />";
}

function wikiplugin_icestat($ip, $params) {
	extract($params);
	$ip = preg_replace("'<[\/\!]*?[^<>]*?>'si","only URL here! ","$ip"); // striping code injecting
	
	$ice_temp = explode(":",$ip); // extract data
	$ip = $ice_temp[0];
	if (!isset($port)) {
		$port = $ice_temp[1]; }
	$port = trim($port); // stripe white space
	//$ip = "kiilo.dyndns.org";               //your server address in the form of mydomain.com or 192.161.1.1
	//$port = "5000";             		//the port of your server
	//opening socket
	$fp = fsockopen("$ip", $port, &$errno, &$errstr, 30); //open connection

	if(!$fp) {
			$success=2;  //set if no connection

	$asetup = "<table class=\"normal\" >
	<tr>
		<td>
		<b>".$ip.":".$port." is currently offline</b></td>
	</tr>
	</table>";
	}

	if($success!=2){ //if connection
		fputs($fp,"GET /status2.xsl HTTP/1.0\r\nUser-Agent: Icecast2 XSL Parser (Mozilla Compatible)\r\n\r\n"); //get status2.xsl
		while(!feof($fp)) {
			$ice_page .= fgets($fp, 1000);
		}
		fclose($fp); //close connection

		$ice_page = ereg_replace(".*<pre>", "", $ice_page); //extract data
		$ice_page = ereg_replace("</pre>.*", ",", $ice_page); //extract data
		$numbers = explode(",",$ice_page); //bomb it and extract data
		// edit if you want a differnt style here:
		$asetup = "<table class=\"normal\" > 
		<tr>
			<td class=\"heading\">name </td>
			<td class=\"heading\">listen </td>
			<td class=\"heading\">listeners </td>
			<td class=\"heading\">current song </td>
		</tr>\n";
		$row = 0;
		for($x=11; $x<count($numbers)-2; $x=$x+6){
		$numbers[$x] = trim($numbers[$x]);
		$asetup .= "<tr class=";
		if (bcmod($row,2)) { 
			$asetup .= "\"odd\""; }
			else {
			$asetup .= "\"even\""; }
		$asetup .= ">\n";
		$asetup .= "	<td><a href=\"http://".$ip.":".$port.$numbers[$x]."\" class=\"wiki\" >http://".$ip.":".$port.$numbers[$x]."</a></td>";
		$asetup .= "	<td align=\"center\"><a target=\"_blank\" href=\"http://".$ip.":".$port.$numbers[$x].".m3u\" class=\"wiki\">listen</a></td>"."\n"."</td>\n";
		$asetup .= "	<td align=\"center\">".$numbers[$x+3]."</td>\n";
		$asetup .= "	<td>".$numbers[$x+5]."</td>\n";
		$asetup .= "</tr>\n";
		$row++;
		next;
		}
	$asetup .= "</table>\n";
	}

return $asetup;
}

?>

