Skip to content
This repository has been archived by the owner on Oct 14, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from toxiicdev/patch-1
Browse files Browse the repository at this point in the history
Remote socket closed handler
  • Loading branch information
Stefan committed Oct 20, 2016
2 parents 3bb59da + 347557c commit 51beb6f
Showing 1 changed file with 31 additions and 18 deletions.
49 changes: 31 additions & 18 deletions lib/ts3admin.class.php
Expand Up @@ -4135,11 +4135,7 @@ function __call($name, $args) {
* @return boolean connected
*/
private function isConnected() {
if(empty($this->runtime['socket'])) {
return false;
}else{
return true;
}
return !empty($this->runtime['socket']);
}

/**
Expand Down Expand Up @@ -4228,17 +4224,24 @@ function connect() {
$this->addDebugLog('Error: you are already connected!');
return $this->generateOutput(false, array('Error: the script is already connected!'), false);
}

$socket = @fsockopen($this->runtime['host'], $this->runtime['queryport'], $errnum, $errstr, $this->runtime['timeout']);

if(!$socket) {
if(!$socket)
{
$this->addDebugLog('Error: connection failed!');
return $this->generateOutput(false, array('Error: connection failed!', 'Server returns: '.$errstr), false);
}else{
if(strpos(fgets($socket), 'TS3') !== false) {
}
else
{
if(strpos(fgets($socket), 'TS3') !== false)
{
$tmpVar = fgets($socket);
$this->runtime['socket'] = $socket;
return $this->generateOutput(true, array(), true);
}else{
}
else
{
$this->addDebugLog('host isn\'t a ts3 instance!');
return $this->generateOutput(false, array('Error: host isn\'t a ts3 instance!'), false);
}
Expand All @@ -4262,20 +4265,30 @@ private function executeCommand($command, $tracert) {
}

$data = '';


$splittedCommand = str_split($command, 1024);

$splittedCommand[(count($splittedCommand) - 1)] .= "\n";

foreach($splittedCommand as $commandPart) {
fputs($this->runtime['socket'], $commandPart);
foreach($splittedCommand as $commandPart)
{
if(!(@fputs($this->runtime['socket'], $commandPart)))
{
$this->runtime['socket'] = '';
$this->addDebugLog('Socket closed.', $tracert[1]['function'], $tracert[0]['line']);
return $this->generateOutput(false, array('Socket closed.'), false);
}
}

do {
$data .= fgets($this->runtime['socket'], 4096);
$data .= @fgets($this->runtime['socket'], 4096);

if(strpos($data, 'error id=3329 msg=connection') !== false) {
if(empty($data))
{
$this->runtime['socket'] = '';
$this->addDebugLog('Socket closed.', $tracert[1]['function'], $tracert[0]['line']);
return $this->generateOutput(false, array('Socket closed.'), false);
}
else if(strpos($data, 'error id=3329 msg=connection') !== false) {
$this->runtime['socket'] = '';
$this->addDebugLog('You got banned from server. Socket closed.', $tracert[1]['function'], $tracert[0]['line']);
return $this->generateOutput(false, array('You got banned from server. Connection closed.'), false);
Expand Down Expand Up @@ -4407,7 +4420,7 @@ private function getData($mode, $command) {
* @return none
*/
private function ftSendKey($key, $additional = NULL) {
fputs($this->runtime['fileSocket'], $key.$additional);
@fputs($this->runtime['fileSocket'], $key.$additional);
}

/**
Expand All @@ -4422,7 +4435,7 @@ private function ftSendKey($key, $additional = NULL) {
private function ftSendData($data) {
$data = str_split($data, 4096);
foreach($data as $dat) {
fputs($this->runtime['fileSocket'], $dat);
@fputs($this->runtime['fileSocket'], $dat);
}
}

Expand Down Expand Up @@ -4497,4 +4510,4 @@ private function addDebugLog($text, $methodName = '', $line = '') {
*
* <a href="http://ts3admin.info" />http://ts3admin.info</a>
*/
?>
?>

0 comments on commit 51beb6f

Please sign in to comment.