Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace wcf\data\TeamSpeak3;
- use wcf\system\exception\TeamSpeak3SyncException;
- /**
- * @author Tobias Hübner
- * @copyright 2014 Tobias Hübner
- * @package de.atrox-dev.teamspeakSynchronisation
- * @license Atrox Development License <http://atrox-dev.de/license.html>
- */
- class TeamSpeak3API
- {
- private $ipAddress = '';
- private $queryPort = '';
- private $serverPort = '';
- private $username = '';
- private $password = '';
- private $socket = null;
- public function __construct($ipAddress, $queryPort, $serverPort, $username, $password)
- {
- $this->ipAddress = $ipAddress;
- $this->queryPort = $queryPort;
- $this->serverPort = $serverPort;
- $this->username = $username;
- $this->password = $password;
- $socket = @fsockopen($this->ipAddress, $this->queryPort, $errnum, $errstr, 3);
- if(!$socket) {
- throw new TeamSpeak3SyncException('Unable to connect to server. (error code: '.$errnum.') message: '.$errstr);
- }
- else
- {
- if(strpos(fgets($socket), 'TS3') !== false)
- {
- $tmpVar = fgets($socket);
- $this->socket = $socket;
- $this->execute('login '.$this->username.' '.$this->password);
- $this->execute('use port='.$this->serverPort);
- if(strlen(TEAMSPEAKSYNCHRONISATION_NICKNAME) > 0)
- $this->execute('clientupdate client_nickname='.$this->escapeString(TEAMSPEAKSYNCHRONISATION_NICKNAME));
- }
- }
- }
- public function __destruct()
- {
- $this->execute('logout');
- @fputs($this->socket, "quit\n");
- @fclose($this->socket);
- }
- private function execute($command)
- {
- $data = '';
- $splittedCommand = str_split($command, 1024);
- $splittedCommand[(sizeof($splittedCommand) - 1)] .= "\n";
- foreach($splittedCommand as $commandPart) {
- fputs($this->socket, $commandPart);
- }
- do {
- $data .= fgets($this->socket, 4096);
- if(strpos($data, 'error id=1033') !== false) {
- throw new TeamSpeak3SyncException('Invalid server port.');
- }
- if(strpos($data, 'error id=3329 msg=connection') !== false) {
- $ipAddress = 'IP-Address unknown';
- if(isset($_SERVER['SERVER_ADDR']))
- $ipAddress = $_SERVER['SERVER_ADDR'];
- 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.');
- return '';
- }
- if(strpos($data, 'error id=2568') !== false) {
- throw new TeamSpeak3SyncException('Insufficient server query permissions. Error message: '.$data);
- return '';
- }
- } while(strpos($data, 'msg=') === false or strpos($data, 'error id=') === false);
- if(strpos($data, 'error id=0 msg=ok') === false)
- return '';
- return $data;
- }
- public function clientFindDb($pattern, $uid = false)
- {
- return $this->fetchServerAnswer('multi', 'clientdbfind pattern='.$pattern.($uid ? ' -uid' : ''));
- }
- public function message($target, $message)
- {
- $targetmode = 0x01;//clientID
- return $this->fetchServerAnswer('boolean', 'sendtextmessage targetmode='.$targetmode.' target='.$target.' msg='.$this->escapeString($message));
- }
- public function serverGroupClientDelete($sgid, $cldbid)
- {
- return $this->fetchServerAnswer('boolean', 'servergroupdelclient sgid='.$sgid.' cldbid='.$cldbid);
- }
- public function serverGroupClientAdd($sgid, $cldbid)
- {
- return $this->fetchServerAnswer('boolean', 'servergroupaddclient sgid='.$sgid.' cldbid='.$cldbid);
- }
- public function channelGroupClientSet($cgid, $cid, $cldbid)
- {
- return $this->fetchServerAnswer('boolean', 'setclientchannelgroup cgid='.$cgid.' cid='.$cid.' cldbid='.$cldbid);
- }
- public function channelGroupClientList($cgid, $cid, $cldbid)
- {
- return $this->fetchServerAnswer('multi', 'channelgroupclientlist cgid='.$cgid.' cid='.$cid.' cldbid='.$cldbid);
- }
- public function serverGroupList()
- {
- return $this->fetchServerAnswer('multi', 'servergrouplist');
- }
- public function serverChannelGroupList()
- {
- return $this->fetchServerAnswer('multi', 'channelgrouplist');
- }
- public function serverChannelList()
- {
- return $this->fetchServerAnswer('multi', 'channellist');
- }
- public function clientGetByUid($cluid)
- {
- return $this->fetchServerAnswer('multi', 'clientgetids cluid='.$cluid);
- }
- public function serverGroupGetById($sgid)
- {
- $groupList = $this->fetchServerAnswer('multi', 'servergrouplist');
- foreach ($groupList as $group)
- {
- if($group['sgid'] == $sgid)
- {
- return $group;
- }
- }
- return array();
- }
- public function banList()
- {
- return $this->fetchServerAnswer('multi', 'banlist');
- }
- public function banDelete($banID)
- {
- return $this->fetchServerAnswer('boolean', 'bandel banid='.$banID);
- }
- public function banAddByUid($uid, $time, $banReason = NULL)
- {
- if(!empty($banReason))
- $msg = ' banreason='.$this->escapeString($banReason);
- else
- $msg = NULL;
- return $this->fetchServerAnswer('array', 'banadd uid='.$uid.' time='.$time.$msg);
- }
- public function clientEdit($cldbid, $data)
- {
- $dataString = '';
- foreach($data as $key => $value) {
- $dataString .= ' '.$key.'='.$this->escapeString($value);
- }
- return $this->fetchServerAnswer('boolean', 'clientdbedit cldbid='.$cldbid.$dataString);
- }
- private function fetchServerAnswer($mode, $command)
- {
- if(empty($command)) return '';
- $fetchData = $this->execute($command);
- $fetchData = str_replace(array('error id=0 msg=ok', chr('01')), '', $fetchData);
- if($mode == 'boolean')
- {
- if(empty($fetchData)) return false;
- return true;
- }
- else if($mode == 'array')
- {
- if(empty($fetchData)) return array();
- $output = array();
- $dataComp = explode(' ', $fetchData);
- foreach($dataComp as $data)
- {
- $data = explode('=', $data);
- if(count($data) > 2)
- {
- for($i = 2; $i < count($data); $i++)
- {
- $data[1] .= '='.$data[$i];
- }
- $output[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
- }
- else
- {
- if(count($data) == 1)
- $output[$this->unEscapeString($data[0])] = '';
- else
- $output[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
- }
- }
- return $output;
- }
- else if($mode == 'multi')
- {
- if(empty($fetchData)) return array();
- $output = array();
- $dataComp = explode('|', $fetchData);
- foreach($dataComp as $dataBlock)
- {
- $dataBlock = explode(' ', $dataBlock);
- $tmp = array();
- foreach($dataBlock as $data)
- {
- $data = explode('=', $data);
- if(count($data) > 2)
- {
- for($i = 2; $i < count($data); $i++)
- {
- $data[1] .= '='.$data[$i];
- }
- $tmp[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
- }
- else
- {
- if(count($data) == 1)
- $tmp[$this->unEscapeString($data[0])] = '';
- else
- $tmp[$this->unEscapeString($data[0])] = $this->unEscapeString($data[1]);
- }
- }
- $output[] = $tmp;
- }
- return $output;
- }
- else
- {
- return $fetchData;
- }
- }
- private function unEscapeString($text) {
- $escapedChars = array("\t", "\v", "\r", "\n", "\f", "\s", "\p", "\/");
- $unEscapedChars = array('', '', '', '', '', ' ', '|', '/');
- $text = str_replace($escapedChars, $unEscapedChars, $text);
- return $text;
- }
- private function escapeString($text) {
- $text = str_replace("\t", '\t', $text);
- $text = str_replace("\v", '\v', $text);
- $text = str_replace("\r", '\r', $text);
- $text = str_replace("\n", '\n', $text);
- $text = str_replace("\f", '\f', $text);
- $text = str_replace(' ', '\s', $text);
- $text = str_replace('|', '\p', $text);
- $text = str_replace('/', '\/', $text);
- return $text;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement