<?php

// homepage www.samscripts.com/scripts/voxinfo

class voxinfo{

	function voxinfo( $servers = array(),$serverurl = "http://info.voxtreme.com/index.xml"){
		$this->serverswanted = $servers;
		$this->url = $serverurl;
	}

	function doit($template){
		if( $this->getinfo() ){
			$this->displayresults($template);
			return true;
		}else{
			return false;
		}
	}

	function getinfo(){
		$url = $this->url;
		$iwant = "";
		$xml = @file($url);
		if( $xml ){
			$xml = join("", $xml);
			$this->parse($xml);
			return true;
		}else{
			return false;
		}
	}

	function parse(&$xml){
		$parser = xml_parser_create();
		xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0);
		xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1);
		xml_parse_into_struct($parser,$xml,$values,$tags);
		xml_parser_free($parser);
		$this->announcements = array();
		$this->servers = array();
		foreach($values as $v){
			switch( $v["tag"]){
				case "announcement":
					if( count($this->serverswanted) == 0 || preg_match("/(".join("|", $this->serverswanted).")/is", $v["attributes"]["text"]) )
						$this->announcements[$v["attributes"]["date"]] = $v["attributes"]["text"];
					break;
				case "server":
					if( count($this->serverswanted) == 0 || preg_match("/(".join("|", $this->serverswanted).")/is", $v["attributes"]["name"]) )
						$this->servers[$v["attributes"]["name"]] = $v["attributes"];
					break;
			}
		}
		$this->numservers = count($this->servers);
		$this->numannouncements = count($this->announcements);
	}

	function displayresults($template){
		if( preg_match("/<announcements>(.*?)<\/announcements>/is", $template, $matches) ){
			$tpla = $matches[1];
		}else{
			$tpla = "";
		}
		if( preg_match("/<servers>(.*?)<\/servers>/is", $template, $matches) ){
			$tpls = $matches[1];
		}else{
			$tpls = "";
		}
		ob_start();
		$this->displayannouncements($tpla);
		$a = ob_get_contents();
		ob_end_clean();
		ob_start();
		$this->displayservers($tpls);
		$s = ob_get_contents();
		ob_end_clean();
		echo preg_replace(array("/<announcements>.*?<\/announcements>/is", "/<servers>.*?<\/servers>/is"),
					array($a, $s), $template);
	}

	function displayannouncements($template){
		foreach($this->announcements as $date => $text){
			echo str_replace(array("{date}", "{announcement}"), array($date, $text), $template);
		}
	}

	function displayservers($template){
		$search = array("{name}", "{uptime}", "{ftp}", "{http}", "{ip}", "{smtp}", "{pop3}", "{MySql}");
		foreach( $this->servers as $name=>$values){
			echo str_replace($search, array($values["name"], $values["uptime"], $values["ftp"], $values["http"], $values["ip"],
					$values["smtp"], $values["pop3"], $values["MySql"]), $template);
		}
	}

}

// uncomment for example

/*
$tpl = '
<table>
<tr><td colspan="7" align="center"><b>Announcements</b></td></tr>
<announcements><tr><td colspan="7">{date} : <i>{announcement}</i></td></tr></announcements>
<tr><td colspan="7"><br><br></td></tr>
<tr><td><b><u>Server</u></b></td>
<td><b><u>ftp</u></b></td>
<td><b><u>http</u></b></td>
<td><b><u>smtp</u></b></td>
<td><b><u>pop3</u></b></td>
<td><b><u>mysql</u></b></td>
<td><b><u>uptime</u></b></td></tr>
<servers>
<tr>
<td><b>{name}</b><br><i><ip></i></td>
<td>{ftp}</td>
<td>{http}</td>
<td>{smtp}</td>
<td>{pop3}</td>
<td>{MySql}</td>
<td>{uptime}</td>
</tr>
</servers>
</table>
';

// $vi = new voxinfo(array("swordfish", "kraken")); // just get info for swordfish and kraken
// $vi = new voxinfo(array("barracuda")); // just for barracuda ... etc

$vi = new voxinfo();

if( !$vi->doit($tpl) ) echo "S**t, an error occurred!";
*/
?>