Advertisement
Guest User

Untitled

a guest
Nov 27th, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.82 KB | None | 0 0
  1. <?php
  2. namespace wcf\data\TeamSpeak3;
  3.  
  4. use wcf\system\exception\TeamSpeak3SyncException;
  5.  
  6. /**
  7.  * @author Tobias Hübner
  8.  * @copyright 2014 Tobias Hübner
  9.  * @package de.atrox-dev.teamspeakSynchronisation
  10.  * @license Atrox Development License <http://atrox-dev.de/license.html>
  11.  */
  12.  
  13. class TeamSpeak3API
  14. {
  15.     private $ipAddress = '';
  16.     private $queryPort = '';
  17.     private $serverPort = '';
  18.     private $username = '';
  19.     private $password = '';
  20.     private $socket = null;
  21.    
  22.     public function __construct($ipAddress, $queryPort, $serverPort, $username, $password)
  23.     {
  24.         $this->ipAddress = $ipAddress;
  25.         $this->queryPort = $queryPort;
  26.         $this->serverPort = $serverPort;
  27.         $this->username = $username;
  28.         $this->password = $password;
  29.        
  30.         $socket = @fsockopen($this->ipAddress, $this->queryPort, $errnum, $errstr, 3);
  31.  
  32.         if(!$socket) {
  33.             throw new TeamSpeak3SyncException('Unable to connect to server. (error code: '.$errnum.') message: '.$errstr);
  34.         }
  35.         else
  36.         {
  37.             if(strpos(fgets($socket), 'TS3') !== false)
  38.             {
  39.                 $tmpVar = fgets($socket);
  40.                 $this->socket = $socket;
  41.                
  42.                 $this->execute('login '.$this->username.' '.$this->password);
  43.                
  44.                 $this->execute('use port='.$this->serverPort);
  45.                
  46.                 if(strlen(TEAMSPEAKSYNCHRONISATION_NICKNAME) > 0)
  47.                     $this->execute('clientupdate client_nickname='.$this->escapeString(TEAMSPEAKSYNCHRONISATION_NICKNAME));
  48.             }
  49.         }
  50.     }
  51.    
  52.     public function __destruct()
  53.     {
  54.         $this->execute('logout');
  55.         @fputs($this->socket, "quit\n");
  56.         @fclose($this->socket);
  57.     }
  58.    
  59.     private function execute($command)
  60.     {
  61.         $data = '';
  62.        
  63.         $splittedCommand = str_split($command, 1024);
  64.        
  65.         $splittedCommand[(sizeof($splittedCommand) - 1)] .= "\n";
  66.        
  67.         foreach($splittedCommand as $commandPart) {
  68.             fputs($this->socket, $commandPart);
  69.         }
  70.        
  71.         do {
  72.             $data .= fgets($this->socket, 4096);
  73.             if(strpos($data, 'error id=1033') !== false) {
  74.                 throw new TeamSpeak3SyncException('Invalid server port.');
  75.             }
  76.             if(strpos($data, 'error id=3329 msg=connection') !== false) {
  77.                 $ipAddress = 'IP-Address unknown';
  78.                 if(isset($_SERVER['SERVER_ADDR']))
  79.                     $ipAddress = $_SERVER['SERVER_ADDR'];
  80.                 throw new TeamSpeak3SyncException('You got banned from server. Add '.$ipAddress.' to your query_ip_whitelist.txt. Otherwise try to set your serverinstance_serverquery_flood_time to 0 or increase your serverinstance_serverquery_flood_commands limit.');
  81.                 return '';
  82.             }
  83.             if(strpos($data, 'error id=2568') !== false) {
  84.                 throw new TeamSpeak3SyncException('Insufficient server query permissions. Error message: '.$data);
  85.                 return '';
  86.             }
  87.            
  88.         } while(strpos($data, 'msg=') === false or strpos($data, 'error id=') === false);
  89.  
  90.         if(strpos($data, 'error id=0 msg=ok') === false)
  91.             return '';
  92.            
  93.         return $data;
  94.     }
  95.    
  96.     public function clientFindDb($pattern, $uid = false)
  97.     {
  98.         return $this->fetchServerAnswer('multi', 'clientdbfind pattern='.$pattern.($uid ? ' -uid' : ''));
  99.     }
  100.    
  101.     public function message($target, $message)
  102.     {
  103.         $targetmode = 0x01;//clientID
  104.         return $this->fetchServerAnswer('boolean', 'sendtextmessage targetmode='.$targetmode.' target='.$target.' msg='.$this->escapeString($message));
  105.     }
  106.    
  107.     public function serverGroupClientDelete($sgid, $cldbid)
  108.     {
  109.         return $this->fetchServerAnswer('boolean', 'servergroupdelclient sgid='.$sgid.' cldbid='.$cldbid);
  110.     }
  111.    
  112.     public function serverGroupClientAdd($sgid, $cldbid)
  113.     {
  114.         return $this->fetchServerAnswer('boolean', 'servergroupaddclient sgid='.$sgid.' cldbid='.$cldbid);
  115.     }
  116.    
  117.     public function channelGroupClientSet($cgid, $cid, $cldbid)
  118.     {
  119.         return $this->fetchServerAnswer('boolean', 'setclientchannelgroup cgid='.$cgid.' cid='.$cid.' cldbid='.$cldbid);
  120.     }
  121.    
  122.     public function channelGroupClientList($cgid, $cid, $cldbid)
  123.     {
  124.         return $this->fetchServerAnswer('multi', 'channelgroupclientlist cgid='.$cgid.' cid='.$cid.' cldbid='.$cldbid);
  125.     }
  126.    
  127.     public function serverGroupList()
  128.     {
  129.         return $this->fetchServerAnswer('multi', 'servergrouplist');
  130.     }
  131.    
  132.     public function serverChannelGroupList()
  133.     {
  134.         return $this->fetchServerAnswer('multi', 'channelgrouplist');
  135.     }
  136.    
  137.     public function serverChannelList()
  138.     {
  139.         return $this->fetchServerAnswer('multi', 'channellist');
  140.     }
  141.    
  142.     public function clientGetByUid($cluid)
  143.     {
  144.         return $this->fetchServerAnswer('multi', 'clientgetids cluid='.$cluid);
  145.     }
  146.    
  147.     public function serverGroupGetById($sgid)
  148.     {
  149.         $groupList = $this->fetchServerAnswer('multi', 'servergrouplist');
  150.        
  151.         foreach ($groupList as $group)
  152.         {
  153.             if($group['sgid'] == $sgid)
  154.             {
  155.                 return $group;
  156.             }
  157.         }
  158.         return array();
  159.     }
  160.    
  161.     public function banList()
  162.     {
  163.         return $this->fetchServerAnswer('multi', 'banlist');
  164.     }
  165.    
  166.     public function banDelete($banID)
  167.     {
  168.         return $this->fetchServerAnswer('boolean', 'bandel banid='.$banID);
  169.     }
  170.    
  171.     public function banAddByUid($uid, $time, $banReason = NULL)
  172.     {
  173.         if(!empty($banReason))
  174.             $msg = ' banreason='.$this->escapeString($banReason);
  175.         else
  176.             $msg = NULL;
  177.        
  178.         return $this->fetchServerAnswer('array', 'banadd uid='.$uid.' time='.$time.$msg);
  179.     }
  180.    
  181.     public function clientEdit($cldbid, $data)
  182.     {      
  183.         $dataString = '';
  184.        
  185.         foreach($data as $key => $value) {
  186.             $dataString .= ' '.$key.'='.$this->escapeString($value);
  187.         }
  188.         return $this->fetchServerAnswer('boolean', 'clientdbedit cldbid='.$cldbid.$dataString);
  189.     }
  190.    
  191.     private function fetchServerAnswer($mode, $command)
  192.     {
  193.         if(empty($command)) return '';
  194.        
  195.         $fetchData = $this->execute($command);             
  196.         $fetchData = str_replace(array('error id=0 msg=ok', chr('01')), '', $fetchData);       
  197.        
  198.         if($mode == 'boolean')
  199.         {
  200.             if(empty($fetchData)) return false;
  201.             return true;
  202.         }
  203.         else if($mode == 'array')
  204.         {
  205.             if(empty($fetchData)) return array();
  206.            
  207.             $output = array();         
  208.             $dataComp = explode(' ', $fetchData);          
  209.            
  210.             foreach($dataComp as $data)
  211.             {
  212.                 $data = explode('=', $data);
  213.                
  214.                 if(count($data) > 2)
  215.                 {
  216.                     for($i = 2; $i < count($data); $i++)
  217.                     {
  218.                         $data[1] .= '='.$data[$i];
  219.                     }
  220.                     $output[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
  221.                 }
  222.                 else
  223.                 {
  224.                     if(count($data) == 1)
  225.                         $output[$this->unEscapeString($data[0])] = '';
  226.                     else
  227.                         $output[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
  228.                 }
  229.             }
  230.             return $output;
  231.         }
  232.         else if($mode == 'multi')
  233.         {
  234.             if(empty($fetchData)) return array();
  235.            
  236.             $output = array();
  237.             $dataComp = explode('|', $fetchData);          
  238.            
  239.             foreach($dataComp as $dataBlock)
  240.             {
  241.                 $dataBlock = explode(' ', $dataBlock);
  242.                
  243.                 $tmp = array();
  244.                
  245.                 foreach($dataBlock as $data)
  246.                 {
  247.                     $data = explode('=', $data);
  248.                    
  249.                     if(count($data) > 2)
  250.                     {
  251.                         for($i = 2; $i < count($data); $i++)
  252.                         {
  253.                             $data[1] .= '='.$data[$i];
  254.                         }
  255.                         $tmp[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
  256.                     }
  257.                     else
  258.                     {
  259.                         if(count($data) == 1)
  260.                             $tmp[$this->unEscapeString($data[0])] = '';
  261.                         else
  262.                             $tmp[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
  263.                     }                  
  264.                 }              
  265.                 $output[] = $tmp;
  266.             }
  267.             return $output;
  268.         }
  269.         else
  270.         {
  271.             return $fetchData;
  272.         }
  273.     }
  274.    
  275.     private function unEscapeString($text) {
  276.         $escapedChars = array("\t", "\v", "\r", "\n", "\f", "\s", "\p", "\/");
  277.         $unEscapedChars = array('', '', '', '', '', ' ', '|', '/');
  278.         $text = str_replace($escapedChars, $unEscapedChars, $text);
  279.         return $text;
  280.     }
  281.    
  282.     private function escapeString($text) {
  283.         $text = str_replace("\t", '\t', $text);
  284.         $text = str_replace("\v", '\v', $text);
  285.         $text = str_replace("\r", '\r', $text);
  286.         $text = str_replace("\n", '\n', $text);
  287.         $text = str_replace("\f", '\f', $text);
  288.         $text = str_replace(' ', '\s', $text);
  289.         $text = str_replace('|', '\p', $text);
  290.         $text = str_replace('/', '\/', $text);
  291.         return $text;
  292.     }
  293. }
  294. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement