English   German
  #1  
Old 17-06-2003, 16:32
White
Guest
 
Posts: n/a
TS2server Information Class (PHP)

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 Code:
<?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->fp2);
            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:
PHP Code:
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
Code:
10	11	4402	528009	4138	504300	2	56	1925	0	0	0	0	0.0.0.0	VISH

Last edited by White; 17-06-2003 at 16:44.
  #2  
Old 06-10-2005, 23:00
more more is offline
-= TeamSpeak User =-
 
Join Date: Oct 2005
Location: Germany
Posts: 1
more is on a distinguished road
log in to teamspeak via telnet
telnet.exe -> open -> ip 5123 (default port)
type "pl <serverport>" and you'll see what it means
  #3  
Old 06-10-2005, 23:26
Thomas's Avatar
Thomas Thomas is offline
-= TeamSpeak Support =-
 
Join Date: Sep 2005
Location: Germany / Dortmund
Posts: 1,556
Thomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond reputeThomas has a reputation beyond repute
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
  #4  
Old 07-10-2005, 19:45
Beldar Beldar is offline
-= TeamSpeak Lover =-
 
Join Date: Sep 2005
Location: Dundee, OR
Posts: 33
Beldar will become famous soon enough
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.
  #5  
Old 07-10-2005, 20:25
Brain's Avatar
Brain Brain is offline
-= TeamSpeak Support =-
 
Join Date: Jan 2003
Location: Germany
Posts: 4,145
Brain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond reputeBrain has a reputation beyond repute
Holy thread resurrection, batman!

The data return format in White's post has been superceded since one or two years ago.
__________________
1f y0u c4n r34d 7h15 y0u r3411y n33d 70 g37 l41d
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 08:14.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Add to Bookmarks   |   Printview   |   Contact Us   |   Legal Notices