Advertisement
Guest User

TS3 PHP Framework Example Code

a guest
Oct 3rd, 2019
378
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.97 KB | None | 0 0
  1. <?php
  2.  
  3.     //SQL Connection
  4.     $servername = "localhost";
  5.     $username = "root";
  6.     $password = "";
  7.     $database = "C3RP_ts3monitor";
  8.  
  9.     // Create connection
  10.     $conn = new mysqli($servername, $username, $password, $database);
  11.  
  12.     // Check connection
  13.     if ($conn->connect_error) {
  14.         die("Database Connection failed: " . $conn->connect_error);
  15.     }
  16.  
  17.     // load framework files
  18.     require_once("ts3lib/TeamSpeak3.php");
  19.     require_once("ts3lib/Exception.php");
  20.    
  21.     //Read tracked fields
  22.     $activity_fields = explode("\n", str_replace("\r", "", file_get_contents("activity.log")));
  23.     $channel_fields = explode("\n", str_replace("\r", "", file_get_contents("channels.log")));
  24.  
  25.     // IPv4 connection URI
  26.     $conf = array(
  27.         "tsip" => "ts.alzlper.com",
  28.         "tsport" => "9987",
  29.         "ts_query_admin" => "tsbot",
  30.         "ts_query_password" => "------",
  31.         "ts_query_port" => "10011",
  32.         "ts_query_user_nick" => "Teamspeak-Baron"
  33.     );
  34.     $uri = "serverquery://".$conf["ts_query_admin"].":".$conf["ts_query_password"]."@".$conf["tsip"].":".$conf["ts_query_port"]."/?server_port=".$conf["tsport"]."&nickname=".$conf["ts_query_user_nick"];
  35.  
  36.     $ts3_coon;
  37.     try {
  38.         TeamSpeak3::init();
  39.         $ts3_coon = TeamSpeak3::factory($uri);
  40.     } catch(Exception $e) {
  41.         echo "Es konnte keine Verbindung zum TS3 Server hergestellt werden! ErrorID: <b>". $e->getCode() ."</b>; Error Message: <b>". $e->getMessage() ."</b>;";
  42.         exit;
  43.     }
  44.    
  45.     //Select the virtual server
  46.     $ts3 = $ts3_coon->serverGetById(1);
  47.  
  48.     while(true) {
  49.         updateActivity($activity_fields, $conn, $conf, $ts3);
  50.         updateChannels($channel_fields, $conn, $conf, $ts3);
  51.         sleep(10);
  52.     }
  53.  
  54.     function updateTs3FieldData($list, $fields, $conn, $conf, $ts3, $table) {
  55.         $sql_cache = "";
  56.         foreach($list as $node_obj) {
  57.             if($table == "activity" && $node_obj["client_nickname"] == $conf["ts_query_user_nick"]) continue;
  58.  
  59.             $sql_fields = [];
  60.             foreach ($fields as $field) {
  61.                 $sql_fields["fields"][] = "`$field`";
  62.                 $sql_fields["data"][] = "'$node_obj[$field]'";
  63.             }
  64.             echo count($sql_fields["data"])."\n";
  65.             $sql_cache .= "INSERT INTO `$table` (".implode(",",$sql_fields["fields"]).") VALUES (".implode(",",$sql_fields["data"]).");";
  66.         }
  67.         $conn->query($sql_cache);
  68.         return $sql_cache;
  69.     }
  70.  
  71.     function updateActivity($activity_fields, $conn, $conf, $ts3) {
  72.         updateTs3FieldData(
  73.             $ts3->clientList(),
  74.             $activity_fields,
  75.             $conn,
  76.             $conf,
  77.             $ts3,
  78.             "activity"
  79.         );
  80.     }
  81.  
  82.     function updateChannels($channel_fields, $conn, $conf, $ts3) {
  83.         updateTs3FieldData(
  84.             $ts3->channelList(),
  85.             $channel_fields,
  86.             $conn,
  87.             $conf,
  88.             $ts3,
  89.             "channel"
  90.         );
  91.     }
  92.  
  93. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement