Forum


Notice to all users

We are migrating towards a new forum system located at community.teamspeak.com, as such this forum will become read-only on January 29, 2020

Results 1 to 2 of 2

Threaded View

  1. #1
    Join Date
    March 2010
    Location
    Germany - NRW
    Posts
    28

    PHP Telnet function

    I thought I should share my PHP Functions for Teamspeak3 Telnet Query.
    It returns an Array of the Output. For some Functions, like help, you have to set the second parameter "true", because you don't want to format this output. Below you see an example of the "clientlist" Function being called:

    PHP Code:
    Array
    (
        [
    status] => Array
            (
                [
    id] => 0
                
    [msg] => ok

            
    )

        [
    result] => Array
            (
                [
    0] => Array
                    (
                        [
    clid] => 2
                        
    [cid] => 8
                        
    [client_database_id] => 1
                        
    [client_nickname] => serveradmin from XXX
                        
    [client_type] => 1
                    
    )

                [
    1] => Array
                    (
                        [
    clid] => 4
                        
    [cid] => 8
                        
    [client_database_id] => 1
                        
    [client_nickname] => serveradmin from XXX
                        
    [client_type] => 1
                    
    )

                [
    2] => Array
                    (
                        [
    clid] => 5
                        
    [cid] => 8
                        
    [client_database_id] => 1
                        
    [client_nickname] => serveradmin from XXX
                        
    [client_type] => 1

                    
    )
            )


    PHP Code:
        $teamspeakhandle=false;
        function 
    tsconnect($ip,$port,$user,$password)
        {
            global 
    $teamspeakhandle;
            
    $teamspeakhandle=fsockopen($ip,$port);
            
    stream_set_timeout($teamspeakhandle,0,100000);
            if (
    $teamspeakhandle)
            {
                
    tscmd("",true);
                
    $result=tscmd("login ".$user." ".$password."");
                if (
    $result["status"]["id"]=="0")
                {
                    return 
    "OK";
                }
                else
                {
                    return 
    $result["status"]["msg"];
                }
            }
            else
            {
                return 
    "Socket connection failed!";
            }
        }
        function 
    tsunescape($text)
        {
            
    $text=str_replace(chr(92).chr(92) ,chr(92) ,$text);
            
    $text=str_replace(chr(92).chr(47) ,chr(47) ,$text);
            
    $text=str_replace(chr(92).chr(115),chr(32) ,$text);
            
    $text=str_replace(chr(92).chr(112),chr(124),$text);
            
    $text=str_replace(chr(92).chr(97) ,chr(7)  ,$text);
            
    $text=str_replace(chr(92).chr(98) ,chr(8)  ,$text);
            
    $text=str_replace(chr(92).chr(102),chr(12) ,$text);
            
    $text=str_replace(chr(92).chr(110),chr(10) ,$text);
            
    $text=str_replace(chr(92).chr(114),chr(13) ,$text);
            
    $text=str_replace(chr(92).chr(116),chr(9)  ,$text);
            
    $text=str_replace(chr(92).chr(118),chr(11) ,$text);
            
    $text=utf8_decode($text);
            return 
    $text;
        }
        function 
    tsescape($text)
        {
            
    $text=str_replace(chr(92) ,chr(92).chr(92) ,$text);
            
    $text=str_replace(chr(47) ,chr(92).chr(47) ,$text);
            
    $text=str_replace(chr(32) ,chr(92).chr(115),$text);
            
    $text=str_replace(chr(124),chr(92).chr(112),$text);
            
    $text=str_replace(chr(7)  ,chr(92).chr(97) ,$text);
            
    $text=str_replace(chr(8)  ,chr(92).chr(98) ,$text);
            
    $text=str_replace(chr(12) ,chr(92).chr(102),$text);
            
    $text=str_replace(chr(10) ,chr(92).chr(110),$text);
            
    $text=str_replace(chr(13) ,chr(92).chr(114),$text);
            
    $text=str_replace(chr(9)  ,chr(92).chr(116),$text);
            
    $text=str_replace(chr(11) ,chr(92).chr(118),$text);
            
    $text=utf8_encode($text);
            return 
    $text;
        }
        function 
    tsread($size,$timeout=1)
        {
            global 
    $teamspeakhandle;
            
    $start=microtime(true);
            do
            {
                
    usleep($size*10);
                
    $data=fread($teamspeakhandle,1);
                
    $info=stream_get_meta_data($teamspeakhandle);
            }
            while ((
    microtime(true)-$start)<$timeout and $info['timed_out']);
            do
            {
                
    usleep($size*10);
                if (
    $info['unread_bytes']>$size)
                {
                    
    $data.=fread($teamspeakhandle,$size);
                }
                else
                {
                    
    $data.=fread($teamspeakhandle,$info['unread_bytes']);
                }            
                
    $info=stream_get_meta_data($teamspeakhandle);
            }
            while (
    $info['unread_bytes']>0);
            return 
    $data;
        }
        function 
    tscmd($command="",$raw=false)
        {
            global 
    $teamspeakhandle;
            
    // Send Command, if specified
            
    if ($command!="")
            {
                
    fputs($teamspeakhandle,$command."\n");
            }
            
    // Format Output
            
    if ($raw)
            {
                
    $content=tsread(256);
                return 
    $content;
            }
            else
            {
                
    // Read Commandreturn
                
    $content=tsread(256);
                
    // If Return is empty
                
    if (strlen($content)==0)
                {
                    
    $content.= "error id=9876 msg=No/sData/srecieved";
                }
                else
                {
                    
    // Read Statusmessage if not already sent
                    
    if (strpos($content,"error id=")===false and $command!="")
                    {
                        
    $content.=tsread(256);
                        if (
    strpos($content,"error id=")===false)
                        {
                            
    $content.=" error id=9876 msg=No/sStatusmessage/srecieved";
                        }
                    }
                    else
                    {
                        if (
    $command=="" and strpos($content,"error id=")===false)
                        {
                            
    $content.=" error id=-1 msg=Server/sresponse/swithout/sCommand";
                        }
                    }
                }
                
    $result=array();
                
    $status=array();
                
    $resultstring=substr($content,0,strpos($content,"error id="));
                
    $statusstring=substr($content,strpos($content,"error id=")+6);
                
    $parameters=explode(" ",$statusstring);
                foreach (
    $parameters as $parameter)
                {
                    list(
    $name,$value)=explode("=",$parameter);
                    
    $error[trim($name)]=tsunescape(trim($value));
                }
                if (
    strpos($resultstring,"|")!==false)
                {
                    
    $lines=explode("|",$resultstring);
                }
                else
                {
                    
    $lines=explode("\n",$resultstring);
                }
                foreach (
    $lines as $count=>$line)
                {
                    if (
    $line!="")
                    {
                        
    $parameters=explode(" ",$line);
                        foreach (
    $parameters as $parameter)
                        {
                            list(
    $name,$value)=explode("=",$parameter);
                            
    $result[$count][trim($name)]=tsunescape(trim($value));
                        }
                    }
                }
                return array(
    "status"=>$error,"result"=>$result);
            }
        }
        
    $status=tsconnect("ip","10011","name","pass");
        if (
    $status!="OK")
        {
            echo 
    "Error: ".$status;
            
    sleep(10);
            die();
        } 
    The Script is able to handle both Userinput via "tscmd", but you can also call the Function with no parameters, to read the Servers Buffer and check for Messages if you registered an Event. There are some custom Error Codes to handle if no Data was recieved, if Data was recieved without a Statusmessage ("error=0 msg=OK"), or if a Message was recieved without sending a Command.
    Last edited by KevinGregull; March 16th, 2010 at 11:50 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •