PDA

View Full Version : TS2server Information Class (PHP)


White
17-06-2003, 15:32
Hi all :)
As i needed it myself and wanted to test around with ts2 a little bit i wrote this little class in PHP which connects to a TS2 Server and than can retrieve some information from it. E.g. connected players, open channels, serverinformation and so on.

Because i was testing on a Server I have no Account (neither root ^^) on, there are no features included, which need me to login (like msges to the server).

This will come next days, when ive got my own server to test with.

Oki here we go, first the class itself

<?php
class TS2server
{
var $ip;
var $port;
var $udpport;
var $data;
var $fp;
var $nl;
var $connected;
var $okflag;

function TS2server($pIp, $pPort, $pUDPport = 1122)
{
$this->ip = $pIp;
$this->port = $pPort;
$this->data = array();
$this->nl = "\n";
$this->udpport = $pUDPport;
$this->okflag = 'OK';
$this->connected = $this->connect();
}

function connect()
{
if($this->fp = fsockopen($this->ip, $this->port, $errnr, $errmsg))
{
socket_set_timeout($this->fp, 2);
if(strstr($this->read(), '[TS]'))
{
return $this->selectServer();
}
else
{
echo 'No TS2 Server found on this IP:Port !';
return false;
}
}
else
{
echo 'Could not connect!<br>(' . $errnr . ') ' . $errmsg;
return false;
}
}

function disconnect()
{
$this->send('quit');
fclose($this->fp);
}

function send($pString)
{
fputs($this->fp, $pString . $this->nl);
}

function read($pBytes = 128)
{
return fgets($this->fp, $pBytes);
}

function readString()
{
$str = '';
while($string = $this->read())
{
if($string != '')
{
$str .= $string;
}
}
return $str;
}

function readArray()
{
$string = $this->readString();
return explode($this->nl, $string);
}

function selectServer()
{
$this->send('sel ' . $this->udpport);
if(strstr($this->read(), $this->okflag))
{
return true;
}
else
{
$this->send('sl');
$udpport = trim($this->read());
$this->readString();
if(is_numeric($udpport))
{
$this->udpport = $udpport;
$this->selectServer();
}
else
{
echo 'No Server ID found !';
return false;
}
}
}

function getVersion()
{
$this->send('ver');
$info = $this->readArray();
if(strstr($info[1], $this->okflag))
{
$parts = explode(' ', $info[0]);
$ret['version'] = $parts[0];
$ret['os'] = $parts[1];
return $ret;
}
else
{
echo 'No Versioninformation found !';
return false;
}
}

function getServerInformation()
{
$this->send('si');
$info = $this->readArray();
foreach($info as $val)
{
if(strstr($val, 'server_'))
{
$parts = explode('=', $val);
$ret[str_replace('server_', '', $parts[0])] = $parts[1];
}
else if(strstr($val, $this->okflag))
{
break;
}
}
return $ret;
}

function getPlayers()
{
$this->send('pl');
$info = $this->readArray();

$player = array();
foreach($info as $val)
{
list($id, $channel, $blah1, $blah2, $blah3, $blah4, $blah5, $blah6, $blah7, $blah8, $blah9, $blah10, $blah11, $ip, $nick, $realname, $blah12) = split("\t", $val);
if(is_numeric($id))
{
$player[$id]['channel'] = $channel;
$player[$id]['ip'] = $ip;
$player[$id]['nick'] = $nick;
$player[$id]['realname'] = $realname;
}
}
return $player;
}

function getChannels()
{
$this->send('cl');
$info = $this->readArray();

$channel = array();
foreach($info as $val)
{
if(strstr($val, $this->okflag))
{
break;
}
list($id, $blah1, $blah2, $blah3, $blah4, $name, $blah6, $blah7, $blah8) = split("\t", $val);
if(is_numeric($id))
{
$channel[$id]['name'] = $name;
}
}
return $channel;
}
}


I called this file class.ts2.php.
Now a little example:

require_once 'class.ts2.php';

$TS2 = new TS2server('217.196.72.210', 51234);

echo '<pre>';
echo 'Version:<br>';
print_r($TS2->getVersion());

echo '<br>Infos:<br>';
print_r($TS2->getServerInformation());

echo '<br>Players:';
print_r($TS2->getPlayers());

echo '<br>Channels:<br>';
print_r($TS2->getChannels());
echo '</pre>';

$TS2->disconnect();


As standard UDPport 8767 is used. You can change this by instantiating the class with the 3rd parameter:
$TS2 = new TS2server('217.196.72.210', 51234, 2233);

If no Server is found on the given UDPport, the Scripts takes all Ports which are available and than takes the first !


Oki this should work, but now i have a question to you guys out there. As you see, in getPlayers and getChannels there are much $blahX. This is, because i absolutely dunno what the values are standing for :(

Here is an example line for a Player, perhaps you can help me out

10 11 4402 528009 4138 504300 2 56 1925 0 0 0 0 0.0.0.0 VISH

more
06-10-2005, 22:00
log in to teamspeak via telnet
telnet.exe -> open -> ip 5123 (default port)
type "pl <serverport>" and you'll see what it means

Thomas
06-10-2005, 22:26
Therefor I think you got a mistake in your class! (I'm not really sure, because I am not able to write something in PHP)

But when you call the function GetPlayers, in the Variables of the first Player are the titles of this variables, because the first line, the server is sending when you type "pl", is the title of the table!


MfG
PLuS

Beldar
07-10-2005, 18:45
You might check out the few php scripts that are available both here and at other websites. Inside those scripts you will find out what all the data means.

Brain
07-10-2005, 19:25
Holy thread resurrection, batman!

The data return format in White's post has been superceded since one or two years ago.