<?php
/*class pvpgnBot {
	private $fp ;
	public $stream;
	private $patterns=array(
		'/1018 INFO /',
		'*"*',
		#'/*\n/',
		'/There are /',
		'/ users online./',
		'/ games online./',
		'/ channels online./',
		'/Your latency         /',
	);


	public function __construct($server,$port,$login,$password) {

		$this->fp = @fsockopen($server, $port, $errno, $errstr,5);
		if (!$this->fp) {
			die ("ERROR: Can't connect to $server:$port. $errno - $errstr"); //сервер выключен
		}
		$this->sendLine("\x03\x04{$login}\x0d{$password}\n//nu\n/p\n/exit\n");
		//echo "\x03\x04{$login}\x0d{$password}\n//nu\n/p\n/exit\n";
		fclose($this->fp);
	}
	private function sendLine($str) {
		fwrite ($this->fp, $str); 
		while (!feof($this->fp))
		{
			$this->stream[]= fgets($this->fp, 4086)."\n";
			echo current($this->stream);
		}
	}
	

	public function getUsers() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-4]);
	}
	public function getGames() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-3]);
	}
	public function getChannels() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-2]);
	}
	public function getLatency() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-1]);
	}

}

$servera=array(
"games.podolsk.ru",
"games.utech.ru",
"netcraft.info.pl",
"bnetd.fishbattle.net",
"playground.ru",

);
$server="games.podolsk.ru";
//$server=;

$port="6112";
$login='zmd';
$password='111';

$bot= new pvpgnBot($server,$port,$login,$password);
print_r( $bot->stream);

echo "u".$bot->getUsers()."|g";
echo $bot->getGames()."|c";
echo $bot->getChannels()."|l";
echo $bot->getLatency()."";*/


// Bot login for PvPGN example
// (c) 2009 HarpyWar

// Bot login for PvPGN example
// (c) 2009 HarpyWar


// set script execution timeout
set_time_limit(10);
register_shutdown_function('handle_timeout');

// exec when execution time exceed or exception occured
function handle_timeout()
{ 
    $error = error_get_last();

    if ($error['type'] === E_ERROR) {
		// do something to handle it
		echo "error catched!";
    }
}


class pvpgnBot 
{
	private $fp ;
	public $stream;
	private $patterns = array(
		'/1018 INFO /',
		'*"*',
		#'/*\n/',
		'/There are /',
		'/ users online./',
		'/ games online./',
		'/ channels online./',
		'/Your latency /',
	);
	
	// sequence of chat commands to be executed after login
	public $commands = array(
		"//nu",
		"/p",
		"/exit"
	);

	public function __construct($server,$port,$login,$password)
	{
		$this->fp = @fsockopen($server, $port, $errno, $errstr, 5);
		if (!$this->fp) {
			die ("ERROR: Can't connect to $server:$port. $errno - $errstr"); //na?aa? auee??ai
		}
		// login to server
		$cmdline = "\x03\x04{$login}\x0d{$password}\n";
		// append commands
		foreach ($this->commands as $cmd)
			$cmdline .= $cmd . "\n";

		$this->sendLine($cmdline);
		fclose($this->fp);
	}
	
	private function sendLine($str)
	{
		fwrite ($this->fp, $str); 
		while (!feof($this->fp))
		{
			$data = trim( fgets($this->fp, 4086) );
			if ( !empty($data) )
				$this->stream[] = $data;
		}
		//print_r($this->stream); # print buffer output
	}
	

	public function getUsers() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-5]);
	}
	public function getGames() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-4]);
	}
	public function getChannels() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-3]);
	}
	public function getLatency() {
		return preg_replace($this->patterns, "", $this->stream[count($this->stream)-2]);
	}

}

$server="server.pvpgn.pro";

$port="6112";
$login='monitor';
$password='11532';

$bot = new pvpgnBot($server,$port,$login,$password);

echo "<b>$server</b><br><br>";

echo $bot->getUsers()." users<br>";
echo $bot->getGames()." games<br>";
echo $bot->getChannels()." channels<br>";
echo $bot->getLatency()." latency<br>";
