Advertisement
Guest User

Untitled

a guest
Nov 24th, 2012
3,689
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.15 KB | None | 0 0
  1. <?PHP
  2.  
  3. /**
  4.  * @author      Par0noid Solutions <par0noid@gmx.de>
  5.  * @package     ts3admin
  6.  * @version     0.6.6
  7.  * @copyright   Copyright (c) 2009-2012, Stefan Z.
  8.  * @link        http://ts3admin.info
  9.  * @link        http://par0noid.info
  10. **/
  11. class ts3admin {
  12.  
  13.     private $runtime = array('socket' => '', 'selected' => false, 'host' => '', 'queryport' => '10011', 'timeout' => 2, 'debug' => array(), 'fileSocket' => '');
  14.  
  15.     function channelList($params = '') {
  16.         if(!$this->runtime['selected']) { return $this->checkSelected(); }
  17.         if(!empty($params)) { $params = ' '.$params; }
  18.        
  19.         return $this->getData('multi', 'channellist'.$params);
  20.     }
  21.  
  22.     function clientInfo($clid) {
  23.         if(!$this->runtime['selected']) { return $this->checkSelected(); }
  24.         return $this->getData('array', 'clientinfo clid='.$clid);
  25.     }
  26.  
  27.  
  28.     function clientList($params = '') {
  29.         if(!$this->runtime['selected']) { return $this->checkSelected(); }
  30.        
  31.         if(!empty($params)) { $params = ' '.$params; }
  32.        
  33.         return $this->getData('multi', 'clientlist'.$params);
  34.     }
  35.  
  36.     function selectServer($value, $type = 'port', $virtual = false) {
  37.         if(in_array($type, array('port', 'serverId'))) {
  38.             if($type == 'port') {
  39.                 if($virtual) { $virtual = ' -virtual'; }else{ $virtual = ''; }
  40.                 $res = $this->getData('boolean', 'use port='.$value.$virtual);
  41.                 if($res['success']) {
  42.                     $this->runtime['selected'] = true;
  43.                 }
  44.                 return $res;
  45.             }else{
  46.                 $res = $this->getData('boolean', 'use sid='.$value);
  47.                 if($res['success']) {
  48.                     $this->runtime['selected'] = true;
  49.                 }
  50.                 return $res;
  51.             }
  52.         }else{
  53.             return $this->generateOutput(false, array('Error: wrong value type'), false);
  54.         }
  55.     }
  56.  
  57.     function serverInfo() {
  58.         if(!$this->runtime['selected']) { return $this->checkSelected(); }
  59.         return $this->getData('array', 'serverinfo');
  60.     }
  61.  
  62.     function __construct($host, $queryport, $timeout = 2) {
  63.         if($queryport >= 1 and $queryport <= 65536) {
  64.             if($timeout >= 1) {
  65.                 $this->runtime['host'] = $host;
  66.                 $this->runtime['queryport'] = $queryport;
  67.                 $this->runtime['timeout'] = $timeout;
  68.             }else{
  69.             }
  70.         }else{
  71.         }
  72.     }
  73.  
  74.     private function isConnected() {
  75.         if(empty($this->runtime['socket'])) {
  76.             return false;
  77.         }else{
  78.             return true;
  79.         }
  80.     }
  81.  
  82.     private function generateOutput($success, $errors, $data) {
  83.         return array('success' => $success, 'errors' => $errors, 'data' => $data);
  84.     }
  85.  
  86.     private function unEscapeText($text) {
  87.         $escapedChars = array("\t", "\v", "\r", "\n", "\f", "\s", "\p", "\/");
  88.         $unEscapedChars = array('', '', '', '', '', ' ', '|', '/');
  89.         $text = str_replace($escapedChars, $unEscapedChars, $text);
  90.         return $text;
  91.     }
  92.  
  93.     public function connect() {
  94.         if($this->isConnected()) {
  95.             return $this->generateOutput(false, array('Error: the script is already connected!'), false);
  96.         }
  97.         $socket = @fsockopen($this->runtime['host'], $this->runtime['queryport'], $errnum, $errstr, $this->runtime['timeout']);
  98.  
  99.         if(!$socket) {
  100.             return $this->generateOutput(false, array('Error: connection failed!', 'Server returns: '.$errstr), false);
  101.         }else{
  102.             if(strpos(fgets($socket), 'TS3') !== false) {
  103.                 $tmpVar = fgets($socket);
  104.                 $this->runtime['socket'] = $socket;
  105.                 return $this->generateOutput(true, array(), true);
  106.             }else{
  107.                 return $this->generateOutput(false, array('Error: host isn\'t a ts3 instance!'), false);
  108.             }
  109.         }
  110.     }
  111.  
  112.     private function executeCommand($command, $tracert) {
  113.         if(!$this->isConnected()) {
  114.             return $this->generateOutput(false, array('Error: script isn\'t connected to server'), false);
  115.         }
  116.        
  117.         $data = '';
  118.  
  119.        
  120.         $splittedCommand = str_split($command, 1024);
  121.        
  122.         $splittedCommand[(count($splittedCommand) - 1)] .= "\n";
  123.        
  124.         foreach($splittedCommand as $commandPart) {
  125.             fputs($this->runtime['socket'], $commandPart);
  126.         }
  127.  
  128.         do {
  129.             $data .= fgets($this->runtime['socket'], 4096);
  130.            
  131.             if(strpos($data, 'error id=3329 msg=connection') !== false) {
  132.                 $this->runtime['socket'] = '';
  133.                 return $this->generateOutput(false, array('You got banned from server. Connection closed.'), false);
  134.             }
  135.            
  136.         } while(strpos($data, 'msg=') === false or strpos($data, 'error id=') === false);
  137.  
  138.         if(strpos($data, 'error id=0 msg=ok') === false) {
  139.             $splittedResponse = explode('error id=', $data);
  140.             $chooseEnd = count($splittedResponse) - 1;
  141.            
  142.             $cutIdAndMsg = explode(' msg=', $splittedResponse[$chooseEnd]);
  143.            
  144.            
  145.             return $this->generateOutput(false, array('ErrorID: '.$cutIdAndMsg[0].' | Message: '.$this->unEscapeText($cutIdAndMsg[1])), false);
  146.         }else{
  147.             return $this->generateOutput(true, array(), $data);
  148.         }
  149.     }
  150.  
  151.     private function getData($mode, $command) {
  152.    
  153.         $validModes = array('boolean', 'array', 'multi', 'plain');
  154.    
  155.         if(!in_array($mode, $validModes)) {
  156.             return $this->generateOutput(false, array('Error: '.$mode.' is an invalid mode'), false);
  157.         }
  158.        
  159.         if(empty($command)) {
  160.             return $this->generateOutput(false, array('Error: you have to enter a command'), false);
  161.         }
  162.        
  163.         $fetchData = $this->executeCommand($command, debug_backtrace());
  164.        
  165.        
  166.         $fetchData['data'] = str_replace(array('error id=0 msg=ok', chr('01')), '', $fetchData['data']);
  167.        
  168.        
  169.         if($fetchData['success']) {
  170.             if($mode == 'boolean') {
  171.                 return $this->generateOutput(true, array(), true);
  172.             }
  173.            
  174.             if($mode == 'array') {
  175.                 if(empty($fetchData['data'])) { return $this->generateOutput(true, array(), array()); }
  176.                 $datasets = explode(' ', $fetchData['data']);
  177.                
  178.                 $output = array();
  179.                
  180.                 foreach($datasets as $dataset) {
  181.                     $dataset = explode('=', $dataset);
  182.                    
  183.                     if(count($dataset) > 2) {
  184.                         for($i = 2; $i < count($dataset); $i++) {
  185.                             $dataset[1] .= '='.$dataset[$i];
  186.                         }
  187.                         $output[$this->unEscapeText($dataset[0])] = $this->unEscapeText($dataset[1]);
  188.                     }else{
  189.                         if(count($dataset) == 1) {
  190.                             $output[$this->unEscapeText($dataset[0])] = '';
  191.                         }else{
  192.                             $output[$this->unEscapeText($dataset[0])] = $this->unEscapeText($dataset[1]);
  193.                         }
  194.                        
  195.                     }
  196.                 }
  197.                 return $this->generateOutput(true, array(), $output);
  198.             }
  199.             if($mode == 'multi') {
  200.                 if(empty($fetchData['data'])) { return $this->generateOutput(true, array(), array()); }
  201.                 $datasets = explode('|', $fetchData['data']);
  202.                
  203.                 $output = array();
  204.                
  205.                 foreach($datasets as $datablock) {
  206.                     $datablock = explode(' ', $datablock);
  207.                    
  208.                     $tmpArray = array();
  209.                    
  210.                     foreach($datablock as $dataset) {
  211.                         $dataset = explode('=', $dataset);
  212.                         if(count($dataset) > 2) {
  213.                             for($i = 2; $i < count($dataset); $i++) {
  214.                                 $dataset[1] .= '='.$dataset[$i];
  215.                             }
  216.                             $tmpArray[$this->unEscapeText($dataset[0])] = $this->unEscapeText($dataset[1]);
  217.                         }else{
  218.                             if(count($dataset) == 1) {
  219.                                 $tmpArray[$this->unEscapeText($dataset[0])] = '';
  220.                             }else{
  221.                                 $tmpArray[$this->unEscapeText($dataset[0])] = $this->unEscapeText($dataset[1]);
  222.                             }
  223.                         }                  
  224.                     }
  225.                     $output[] = $tmpArray;
  226.                 }
  227.                 return $this->generateOutput(true, array(), $output);
  228.             }
  229.             if($mode == 'plain') {
  230.                 return $fetchData;
  231.             }
  232.         }else{
  233.             return $this->generateOutput(false, $fetchData['errors'], false);
  234.         }
  235.     }
  236. }
  237.  
  238. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement