/******
* You may use and/or modify this script as long as you:
* 1. Keep my name & webpage mentioned
* 2. Don't use it for commercial purposes
*
* If you want to use this script without complying to the rules above, please contact me first at: marty@excudo.net
* 
* Author: Martijn Korse
* Website: http://devshed.excudo.net
*
* Date:  08-05-2006
***/
class Radio
{
	var $fields;
	var $very_first_str;
	var $domain, $port;
	var $errno, $errstr;
	var $trackLists = array();
	function Radio($domain, $port)
	{
		$this->domain = $domain;
		$this->port = $port;
		$this->setOffset("Current Stream Information");
		$this->setTableStart("
");
	}
	function setFields($array=array())
	{
		$this->fields = $array;
	}
	function setOffset($string)
	{
		$this->very_first_str = $string;
	}
	function setTableStart($string)
	{
		$this->tableStart = $string;
	}
	function setTableEnd($string)
	{
		$this->tableEnd = $string;
	}
	function getHTML($page)
	{
		$contents = "";
		$domain = (substr($this->domain, 0, 7) == "http://") ? substr($this->domain, 7) : $this->domain;
		if ($fp = fsockopen($domain, $this->port, $this->errno, $this->errstr, 2))
		{
			fputs($fp, "GET ".$page." HTTP/1.1\r\n".
				"User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n".
				"Host: ".$domain."\r\n\r\n");
			while (!feof($fp))
				$contents .= fgets($fp, 256);
			fclose ($fp);
			return $contents;
		}
		else
		{
			return False;
		}
	}
	function getServerInfo($page="/", $display_array=null, $very_first_str=null)
	{
		if (!isset($display_array))
			$display_array = $this->fields;
		if (!isset($very_first_str))
			$very_first_str = $this->very_first_str;
		if ($html = $this->getHTML($page))
		{
			 // parsing the contents
			foreach ($display_array AS $key => $item)
			{
				$very_first_pos	= stripos($html, $very_first_str);
				$first_pos	= stripos($html, $item, $very_first_pos);
				$line_start	= strpos($html, "", $first_pos);
				$line_end	= strpos($html, "", $line_start) + 4;
				$difference	= $line_end - $line_start;
				$line		= substr($html, $line_start, $difference);
				$data[$key]	= strip_tags($line);
			}
			return $data;
		}
		else
		{
			return $this->errstr." (".$this->errno.")";
		}
	}
	function createHistoryArray($page)
	{
		if (!in_array($page, $this->trackLists))
		{
			$this->trackLists[] = $page;
			if ($html = $this->getHTML($page))
			{
				$fromPos	= stripos($html, $this->tableStart);
				$toPos		= stripos($html, $this->tableEnd);
				$tableData	= substr($html, $fromPos, ($toPos-$fromPos));
				$lines		= explode(" | ", $tableData);
				$tracks = array();
				$c = 0;
				foreach ($lines AS $line)
				{
					$info = explode ("| ", $line);
					$time = trim(strip_tags($info[0]));
					if (substr($time, 0, 9) != "Copyright")
					{
						$this->tracks[$c]['time'] = $time;
						$this->tracks[$c++]['track'] = trim(strip_tags($info[1]));
					}
				}
				unset($this->tracks[0]);
				$this->tracks[1]['track'] = str_replace("Current Song", "", $this->tracks[1]['track']);
			}
			else
			{
				$this->tracks[0] = array("time"=>$this->errno, "track"=>$this->errstr);
			}
		}
	}
	function getHistoryArray($page="/played.html")
	{
		if (!in_array($page, $this->trackLists))
			$this->createHistoryArray($page);
		return $this->tracks;
	}
	function getHistoryTable($page="/played.html", $timeColText=False, $trackColText=False, $class=False)
	{
		if (!in_array($page, $this->trackLists))
			$this->createHistoryArray($page);
		$output = " ";
		if ($timeColText && $trackColText)
			$output .= "\n";
		return $output;
	}
}
// this is needed for those with a php version < 5
// the function is copied from the user comments @ php.net (http://nl3.php.net/stripos)
if (!function_exists("stripos"))
{
	function stripos($haystack, $needle, $offset=0)
	{
		return strpos(strtoupper($haystack), strtoupper($needle), $offset);
	}
}
?>";
		foreach ($this->tracks AS $trackArr)
			$output .= "| ".$timeColText." | ".$trackColText." |  ";
		$output .= "| ".$trackArr['time']." | ".$trackArr['track']." |  |