<?php
//------============== DEDICATED SERVER SCRIPT v2.1 =======------------------
//------============ By: OPTIMO of UBERTECHNIK.com  =======------------------
//---------------------------------------------------------------------------
//Modified by AB156 1/2/06 - Added loop for multiple servers.
//Modified by DarrenM 15/07/06 to show more detail and to search on name and ip

// Put your server name or ip here. It does a partial match, so it will 
// find all servers that contain this string in their name or ip.
$server = "SRP-CLASSICOS/91.121.14.164";

// Optionally dislay car Class next to the drivers name. TRUE/FALSE
$showClass = TRUE;
// Optionally dislay car name next to the drivers name. TRUE/FALSE
$showCar = TRUE;
// Optionally dislay drivers count i.e. "Drivers Online (6,13)". TRUE/FALSE
$showDriverCount = TRUE;
// Optionally dislay Join and Watch racecast links
$showLinks = TRUE;

// Get a file into an array.  In this example we'll go through HTTP to get 
// the HTML source of a URL.
// URL to Racecast XML Feed
$feedurl = "http://racecast.rfactor.net/rssfeed/RaceCastRSSFeed.xml";
// Tests if feed is up
if (@fclose(@fopen("$feedurl", "r"))) 
{
	$racecastfeed = file_get_contents($feedurl);
	// Searches for an indentifying part of your server name
	$pos = strpos($racecastfeed, $server); 
	if ($pos === false) 
	{ 
	   echo "Server/s - OFFLINE."; 
	} 
	else 
	{ 
		$lines = file($feedurl);
		$serverCount = 0;

		// Loop through our array, show HTML source as HTML source; and line numbers too.
		foreach ($lines as $line_num => $line)
		{
			if (strpos($lines[$line_num], "<server>") > 0 &&
			    (strpos($lines[$line_num+1], $server) > 0 ||
			     strpos($lines[$line_num+3], $server) > 0))
			{
				if ($serverCount > 0)
					echo "<br>";
				$serverCount++;
				// Details
				$serverName = trim(strip_tags($lines[$line_num+1]));
				$raceID = trim(strip_tags($lines[$line_num+2]));
				$serverAddr = trim(strip_tags($lines[$line_num+3]));
				$serverPort = trim(strip_tags($lines[$line_num+4]));
				$event = trim(strip_tags($lines[$line_num+5]));
				$series = trim(strip_tags($lines[$line_num+6]));
				$sessionName = trim(strip_tags($lines[$line_num+7]));

				//Server Name
				echo "<b>".$serverName."</b><br>";
				//Series
				echo $series."<br>";
				//Track
				echo $event."<br>";
                                //Session
                                $session = $lines[$line_num+7];
                                strtok($session, '"');
                                $sessionID = strtok('"');
                                strtok('"');
                                $prevSessionID = strtok('"');
                                strtok('"');
                                $sessionType = strtok('"');
                                strtok('"');
                                $time = round(strtok('"') / 60,0);
                                strtok('"');
                                $laps = strtok('"');
				if ($sessionType == "time")
					echo $sessionName." ". $time. " Mins<br>";
				else
					echo $sessionName." ". $laps. " Laps<br>";

				if ($showLinks)
				{
					echo "<table style=\"width: 70px \"><tr>";
					if ($sessionName != "Race")
					{
						echo "<td style=\"text-align: center; width: 50%;\">";
						echo "<a href=\"rfactor://".$serverAddr.":".$serverPort." +password\"><img src=\"images/serverlisting_join.gif\"></a>";
						echo "</td >";
					}
					else
					{
						echo "<td style=\"text-align: center; width: 50%;\">";
						echo "<img src=\"images/serverlisting_no-join.gif\">";
						echo "</td >";
					}
					echo "<td style=\"text-align: center; width: 50%;\">";
					echo "<a target=_new href=\"http://racecast.rfactor.net/?go=racedetails&raceID=".$raceID."&sessiontype=".$sessionName."\"><img src=\"images/serverlisting_watch.gif\"></a><br>";
					echo "</td>";
					echo "</tr></table>";
				}

				//Drivers
				$drivers = $lines[$line_num+8];
				strtok($drivers,'"');
				$driverCount = strtok('"');
				strtok('"');
				$maxDrivers = strtok('"');
				if ($showDriverCount)
					echo "<b>Drivers Online ($driverCount,$maxDrivers):</b><br>";
				else
					echo "<b>Drivers Online:</b><br>";
				$line_num += 8;

				//Driver names
				WHILE (strpos($lines[$line_num], "</players>") == 0 )
				{
					if (strpos($lines[$line_num], "<name>") > 0 )
					{	echo trim(strip_tags($lines[$line_num]));
						if ($showCar)
							echo ", ".trim(strip_tags($lines[$line_num+1]));
						if ($showClass)
							echo ", ".trim(strip_tags($lines[$line_num+2]));
						echo "<br>";
					}
					$line_num += 1;
				}
			}
		}
	}
}
else 
{ 
	echo "Racecast RSS Feed is OFFLINE.";
} 
?>