All pastes #980586 Raw Edit

Stuff

public php v1 · immutable
·published 2008-04-11 03:15 UTC
rendered paste body
<?phpclass rcon {	public $challenge;	public $server_ip;	public $server_port;	public $server_rcon;		public $socket;	public $errno;	public $errstr;	function __construct($ip, $port=27015, $rcon=NULL, $persistent=FALSE) {		$this->server_ip = $ip;		$this->server_port = $port;		$this->server_rcon = $rcon;		$this->persistent = $persistent;		if(!$this->connect()) return FALSE;	}	function connect() {		$fp = ($this->persistent) ? pfsockopen("udp://{$this->server_ip}", $this->server_port, $this->errno, $this->errstr) : fsockopen("udp://{$this->server_ip}", $this->server_port, $this->errno, $this->errstr);		if(feof($fp)) return FALSE;		$this->socket =& $fp;		return TRUE;	}	function disconnect() {		fclose($this->socket);		return TRUE;	}	function isConnected() {		if(!$this->socket || feof($this->socket)) {			$this->disconnect();			if(!$this->connect()) return FALSE;		} else return TRUE;	}	function checkReceive($data) {		if(is_null($data) || empty($data)) {			$this->disconnect();			if(!$this->connect()) return FALSE;		} else return TRUE;	}	function send($data) {		if(!$this->isConnected()) return FALSE;				if(!is_null($data) && !empty($data)) fwrite($this->socket, $data); else return FALSE;	}	function receive() {		if(!$this->isConnected()) return FALSE;		$buffer = stream_get_contents($this->socket);		if(!$this->checkReceive($buffer)) return FALSE;		if(substr($buffer, 0, 4) == "\xfe\xff\xff\xff") {			$buffer2 = fread ($this->socket, 1);			$status = socket_get_status($this->socket);			$buffer2 .= fread($this->socket, $status["unread_bytes"]);			if(strlen($buffer) > strlen($buffer2)) $buffer = substr($buffer, 14) . substr($buffer2, 9);			else $buffer = substr($buffer2, 14) . substr($buffer, 9);		} else $buffer = substr($buffer, 5);		return $buffer;	}	function rcon($command, $pagenumber = 0, $single = true) {		if(!$this->isConnected()) return FALSE;		if(empty($this->challenge)) {			$challenge = "\xff\xff\xff\xffchallenge rcon\n";			$this->send($challenge);			$buffer = $this->receive();			if(!$this->checkReceive($buffer)) return FALSE;			$buffer = explode(" ", $buffer);			$this->challenge = trim($buffer[2]);		}		$command = "\xff\xff\xff\xffrcon $this->challenge \"$this->server_rcon\" $command";		$result = '';		$buffer = '';		while($pagenumber >= 0) {			$this->send($command);			$buffer .= $this->receive();			if($single) $result = $buffer; else $result .= $buffer;			$command = '';			$pagenumber--;		}		return trim($result);	}	function info($type=0) {		if(!$this->isConnected()) return FALSE;		switch($type) {		case 0:			$command = "\xff\xff\xff\xffinfo\x00";			$this->send($command);			$buffer = $this->receive();			if(!$this->checkReceive($buffer)) return FALSE;			$buffer = explode("\x00", $buffer);			$result["ip"] = substr($buffer[0], 5);			$result["name"] = $buffer[1];			$result["map"] = $buffer[2];			$result["mod"] = $buffer[3];			$result["game"] = $buffer[4];			$result["activeplayers"] = (strlen($buffer[5]) > 1)?ord($buffer[5][0]):"0";			$result["maxplayers"] = (strlen($buffer[5]) > 1)?ord($buffer[5][1]):"0";			break;		case 1:			$status = rcon('status');			if(!$status || trim($status) == "Bad rcon_password.") {				$result = $status;				break;			}			$line = explode("\n", $status);			$map = substr($line[3], strpos($line[3], ":") + 1);			$players = trim(substr($line[4], strpos($line[4], ":") + 1));			$active = explode(" ", $players);			$result["ip"] = trim(substr($line[2], strpos($line[2], ":") + 1));			$result["name"] = trim(substr($line[0], strpos($line[0], ":") + 1));			$result["map"] = trim(substr($map, 0, strpos($map, "at:")));			$result["mod"] = "Counterstrike " . trim(substr($line[1], strpos($line[1], ":") + 1));			$result["game"] = "Halflife";			$result["activeplayers"] = $active[0];			$result["maxplayers"] = substr($active[2], 1);			for($i = 1; $i <= $result["activeplayers"]; $i++) {				$tmp = $line[$i + 6];				if(substr_count($tmp, "#") <= 0) break;				$begin = strpos($tmp, "\"") + 1;				$end = strrpos($tmp, "\"");				$result[$i]["name"] = substr($tmp, $begin, $end - $begin);				$tmp = trim(substr($tmp, $end + 1));				$end = strpos($tmp, " ");				$result[$i]["id"] = substr($tmp, 0, $end);				$tmp = trim(substr($tmp, $end));				$end = strpos($tmp, " ");				$result[$i]["steamid"] = substr($tmp, 0, $end);				$tmp = trim(substr($tmp, $end));				$end = strpos($tmp, " ");				$result[$i]["frag"] = substr($tmp, 0, $end);				$tmp = trim(substr($tmp, $end));				$end = strpos($tmp, " ");				$result[$i]["time"] = substr($tmp, 0, $end);				$tmp = trim(substr($tmp, $end));				$end = strpos($tmp, " ");				$result[$i]["ping"] = substr($tmp, 0, $end);				$tmp = trim(substr($tmp, $end));				$tmp = trim(substr($tmp, $end));				$result[$i]["adress"] = $tmp;			}			break;		case 2:			$command = "\xff\xff\xff\xffplayers\x00";			$this->send($command);			$buffer = $this->receive();			if(!$this->checkReceive($buffer)) return FALSE;			$buffer = substr($buffer, 1);			for($i = 1; strlen($buffer) > 0; $i++) {				$tmp = strpos($buffer, "\x00");				$result[$i]["name"] = substr($buffer, 1, $tmp);				$result[$i]["frag"] = ord($buffer[$tmp + 1]) +					(ord($buffer[$tmp + 2]) << 8) +					(ord($buffer[$tmp + 3]) << 16) +					(ord($buffer[$tmp + 4]) << 24);				$tmptime = @unpack('ftime', substr($buffer, $tmp + 5, 4));				$result[$i]["time"] = date('i:s', round($tmptime['time'], 0) + 82800);				$buffer = substr($buffer, $tmp + 9);			}			break;		case 3:			$command = "\xff\xff\xff\xffrules\x00";			$this->send($command);			$buffer = $this->receive();			if(!$this->checkReceive($buffer)) return FALSE;			$buffer = substr($buffer, 2);			$buffer = explode("\x00", $buffer);			$buffer_count = floor(sizeof($buffer) / 2);			for($i = 0; $i < $buffer_count; $i++) $result[$buffer[2 * $i]] = $buffer[2 * $i + 1];			ksort($result);			break;		default:			return FALSE;			break;		}		return $result;	}	function maps($prefix='*', $pagenumber = 0) {		if(!$this->isConnected()) return FALSE;		$maps = $this->rcon("maps $prefix", $pagenumber);		if(!$maps || trim($maps) == "Bad rcon_password.") return $maps;		$line = explode("\n", $maps);		$count = sizeof($line) - 4;		for($i = 0; $i <= $count; $i++) {			$text = $line[$i];			if(strstr($text, "Dir:")) {				$mapcount = 0;				$directory = strstr($text, " ");			} else if(strstr($text, "(fs)")) {				$mappath = strstr($text, " ");				if(!($tmpmap = strrchr($mappath, "/"))) $tmpmap = $mappath;				$result[$directory][$i] = substr($tmpmap, 1, strpos($tmpmap, ".") - 1);			}		}		return $result;	}}?>