Advertisement
Guest User

Untitled

a guest
Apr 30th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.92 KB | None | 0 0
  1. <?php
  2.    
  3.     class ts3server {
  4.         private $teamspeakhandle;
  5.         function __construct($ip, $port, $name, $pass, $virtualServer) {
  6.             $this->teamspeakhandle=fsockopen($ip,$port);
  7.             stream_set_timeout($this->teamspeakhandle,0,100000);
  8.             if ($this->teamspeakhandle) {
  9.                 $this->_login($name, $pass, $virtualServer);
  10.                 //echo $this->_tsread(256); //Welcome to the TeamSpeak 3 ServerQuery interface, type "help" for a list of commands and "help " for information on a specific command. error id=0 msg=ok
  11.             }else {
  12.                 echo "Socket connection failed!";
  13.             }
  14.         }
  15.         private function _login($name, $pass, $virtualServer) {
  16.             fputs($this->teamspeakhandle,"login ".$name." ".$pass."\n");
  17.             $return = $this->_tsread(256);
  18.             if(strpos($return, 'msg=ok') !== FALSE) {
  19.                 $this->_command('use', array($virtualServer));
  20.                 //echo "Login successful\n";
  21.             }
  22.         }
  23.        
  24.         private function _command($command, $params = array()) {
  25.             $command .= " ";
  26.             if($params) {
  27.                 foreach($params as $param) {
  28.                     $command .= $param." ";
  29.                 }
  30.             }
  31.            
  32.             fputs($this->teamspeakhandle,$command."\n");
  33.             return $this->_tsread(256);
  34.         }
  35.         public function clientList() {
  36.             $return = $this->_command('clientlist -ip');
  37.             $return = explode("|", $return);
  38.             foreach($return as $key => $ret) {
  39.                 foreach(explode(" ", $ret) as $value) {
  40.                     $tmp = explode("=", $value);
  41.                     if (!isset($tmp[1])) $tmp[1] = NULL;
  42.                     $clients[$key][$tmp[0]] = str_replace("\s", " ",$tmp[1]);
  43.                     if(isset($clients[$key]["client_type"]) && $clients[$key]["client_type"] == 1) {
  44.                         unset($clients[$key]);
  45.                     }
  46.                 }
  47.             }
  48.             return $clients;
  49.         }
  50.         private function _tsread($size,$timeout=1){
  51.             $start=microtime(true);
  52.             do{
  53.                 usleep($size*100);
  54.                 $data=fread($this->teamspeakhandle,1);
  55.                 $info=stream_get_meta_data($this->teamspeakhandle);
  56.             }
  57.             while ((microtime(true)-$start)<$timeout and $info['timed_out']);
  58.             do{
  59.                 usleep($size*10);
  60.                 if ($info['unread_bytes']>$size){
  61.                     $data.=fread($this->teamspeakhandle,$size);
  62.                 }
  63.                 else {
  64.                     $data.=fread($this->teamspeakhandle,$info['unread_bytes']);
  65.                 }            
  66.                 $info=stream_get_meta_data($this->teamspeakhandle);
  67.             }
  68.             while ($info['unread_bytes']>0);
  69.             return $data;
  70.         }
  71.     };
  72.     $ts3 = new ts3server('127.0.0.1', 10011, "serveradmin","xxx","4");
  73.  
  74. while(1)
  75. {  
  76.     sleep(1);
  77.    
  78.     foreach($ts3->clientList() as $client) {
  79.         //echo "|".$client['connection_client_ip']."|\n";
  80.         //include("mysql.php");
  81.     //print_r($client);
  82.     if (!isset($client['connection_client_ip'])) $client['connection_client_ip'] = NULL;
  83.     $client['connection_client_ip'] = explode("error", $client['connection_client_ip']);
  84.     foreach($client['connection_client_ip'] as $clientip) {
  85.         if ($clientip != NULL)
  86.         if ($clientip != "")
  87.        
  88.         {
  89.            
  90.             echo "</br>IP: ".$client['connection_client_ip'][0];
  91.             echo "</br>ID: ".$client['clid'];
  92.             echo "</br>DBID: ".$client['client_database_id'];
  93.             echo "</br>NickName: ".$client['client_nickname'];
  94.             echo "</br>Type: ".$client['client_type'];
  95.             echo "</br>";
  96.            
  97.             /*
  98.             xZ6mER4fGuAq8ufY
  99.             nowybot
  100.            
  101.             */
  102.            
  103.                 $servername = "localhost";
  104.                 $username = "newbot";
  105.                 $password = "xZ6mER4fGuAq8ufY";
  106.                 $dbname = "newbot";
  107.  
  108.                 // Create connection
  109.                 $conn = new mysqli($servername, $username, $password, $dbname);
  110.                 // Check connection
  111.                 if ($conn->connect_error) {
  112.                     die("Connection failed: " . $conn->connect_error);
  113.                 }
  114.  
  115.                 $sql = "INSERT INTO online_users (DBID, ID, IP, NickName, Type)
  116.                 VALUES ('John', 'Doe', 'john@example.com')";
  117.  
  118.                 if ($conn->query($sql) === TRUE) {
  119.                     echo "New record created successfully";
  120.                 } else {
  121.                     echo "Error: " . $sql . "<br>" . $conn->error;
  122.                 }
  123.                
  124.                 $conn->close();
  125.            
  126.         }
  127.            
  128.             /*
  129.             Array
  130.             (
  131.                 [clid] => 4
  132.                 [cid] => 236
  133.                 [client_database_id] => 7943
  134.                 [client_nickname] => bocik
  135.                 [client_type] => 0
  136.             )
  137.             */
  138.         }
  139.     }
  140. }
  141.    
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement