1: <?php
2:
3: namespace Budabot\User\Modules;
4:
5: use Exception;
6:
7: 8: 9:
10: class Teamspeak3 {
11: private $username;
12: private $password;
13: private $address;
14: private $port;
15: private $serverId;
16:
17: public function __construct($username, $password, $address = '127.0.0.1', $port = 10011, $serverId = 1) {
18: $this->username = $username;
19: $this->password = $password;
20: $this->address = $address;
21: $this->port = $port;
22: $this->serverId = $serverId;
23: }
24:
25:
26: public function exec($data) {
27: $this->connect();
28: fputs($this->stream, $data . "\n");
29: fputs($this->stream, "quit\n");
30: while (!feof($this->stream)) {
31: $data = explode('|', fgets($this->stream));
32: if (substr($data[0], 1, 8) != 'error id' && substr($data[0], 1, 7) != 'Welcome' && substr($data[0], 1, 2) != 'S3') {
33: forEach ($data as $outputVar) {
34: $outputLine = explode(' ', $outputVar);
35: unset($outputVar);
36: forEach ($outputLine as $line) {
37: $fragment = explode('=', $line);
38: @$outputVar[trim($fragment[0])] = trim($fragment[1]);
39: }
40: $finalData[] = $outputVar;
41: }
42: }
43: }
44: fclose($this->stream);
45: if (@$finalData) {
46: array_pop($finalData);
47: return $finalData;
48: } else {
49: throw new Exception('Invalid TS3 query.');
50: }
51: }
52:
53:
54: public function connect() {
55: $stream = fsockopen($this->address, $this->port, $errno, $errstr, 5);
56: $this->stream = $stream;
57: if ($this->stream) {
58: if ($this->username) {
59: fputs($this->stream, "login client_login_name=". $this->username ." client_login_password=". $this->password ."\n");
60: }
61: fputs($this->stream, "use ". $this->serverId ."\n");
62: } else {
63: throw new Exception('Unable to connect to the TS3 server.');
64: }
65: }
66:
67:
68: public function __destruct() {
69: fclose($this->stream);
70: }
71: }
72: