Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $user->add_lang('mods/tsviewer');
- $data = tsv("217.26.212.10",51234,8767);
- $playerlist = "";
- for($i = 1; $i <= count($data['playerlist']); $i++)
- {
- $playerlist .= "<b>".$data['playerlist'][$i]['playername']."</b>, ";
- }
- $firstw = "i";
- $secondw = "i";
- $number = count($data['playerlist']);
- if($number == 1)
- {
- $firstw = "e";
- $secondw = "o";
- }
- $template->assign_vars(array(
- 'TSV_USER_TOTAL' => sprintf($user->lang['TSV_USER_TOTAL'], $number,$firstw),
- 'TSV_CONNECTED' => sprintf($user->lang['TSV_CONNECTED'], $secondw),
- 'TSV_PLAYER_LIST' => sprintf($user->lang['TSV_PLAYER_LIST'], $playerlist),
- ));
- // constructor queries the teamspeak server
- function tsv($address,$queport,$udpport)
- {
- $result = array();
- if(!function_exists('fsockopen'))
- {
- $result["errorno"] = 3;
- }
- else if(!tsv_connect($socket,$address,$queport,0.3))
- {
- $result["errorno"] = 1;
- }
- else if(!tsv_request_server($socket,$udpport))
- {
- $result["errorno"] = 2;
- tsv_disconnect($socket);
- }
- else
- {
- $result["errorno"] = 0;
- $result["serverinfo"] = tsv_server_info($socket);
- $result["playerlist"] = tsv_player_list($socket);
- tsv_disconnect($socket);
- }
- return $result;
- }
- //tsv_connects to the teamspeak server
- function tsv_connect(&$socket,$host,$port,$timeout)
- {
- @$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
- if($socket and (tsv_sanitize(fgets($socket,4096)) == "[TS]"))
- return true;
- else
- return false;
- }
- // distsv_connect from the teamspeak server
- function tsv_disconnect($socket)
- {
- fputs($socket, "quit\n");
- fclose($socket);
- }
- // request the server data
- function tsv_server_info($socket)
- {
- fputs($socket, "si\n");
- $result = array();
- do
- {
- $buffer = tsv_sanitize(fgets($socket,4096));
- if (($buffer != "OK") && (strtoupper(substr($buffer,0,5)) != "ERROR"))
- {
- $pos = strpos($buffer,'=');
- if ($pos !== false)
- {
- $result[substr($buffer,0,$pos)] = substr($buffer,$pos+1);
- }
- }
- }
- while (($buffer != "OK") && (strtoupper(substr($buffer,0,5)) != "ERROR") && (!feof($socket)));
- return $result;
- }
- // request the player list and their data
- function tsv_player_list($socket)
- {
- fputs($socket,"pl\n");
- $buffer = tsv_sanitize(fgets($socket,4096));
- $result = array();
- if(strtoupper(substr($buffer,0,5)) == "ERROR")
- return $result;
- do
- {
- $buffer = tsv_sanitize(fgets($socket,4096));
- if(($buffer != "OK") && (strtoupper(substr($buffer,0,5)) != "ERROR"))
- {
- $playerid = tsv_remove_tabs($buffer);
- $result[$playerid] = array(
- "playerid" => $playerid,
- "channelid" => tsv_remove_tabs($buffer),
- "receivedpackets" => tsv_remove_tabs($buffer),
- "receivedbytes" => tsv_remove_tabs($buffer),
- "sentpackets" => tsv_remove_tabs($buffer),
- "sentbytes" => tsv_remove_tabs($buffer),
- "paketlost" => tsv_remove_tabs($buffer)/100,
- "pingtime" => tsv_remove_tabs($buffer),
- "totaltime" => tsv_remove_tabs($buffer),
- "idletime" => tsv_remove_tabs($buffer),
- "privileg" => tsv_remove_tabs($buffer),
- "userstatus" => tsv_remove_tabs($buffer),
- "attribute" => tsv_remove_tabs($buffer),
- "ip" => tsv_remove_tabs($buffer),
- "playername" => tsv_remove_quotes(tsv_remove_tabs($buffer)),
- "loginname" => tsv_remove_quotes(tsv_remove_tabs($buffer))
- );
- }
- }
- while(($buffer != "OK") && (strtoupper(substr($buffer,0,5)) != "ERROR") && (!feof($socket)));
- return $result;
- }
- // request the server behind the udpport
- function tsv_request_server($socket, $port)
- {
- fputs($socket,"sel ".$port."\n");
- if(tsv_sanitize(fgets($socket,4096)) == "OK")
- return true;
- else
- return false;
- }
- // remove the endstring characters
- function tsv_sanitize($string)
- {
- $lenght = strlen($string);
- while(((substr($string,$lenght-1,1) == "\r")) || ((substr($string,$lenght-1,1) == "\n")))
- $lenght--;
- return substr($string, 0, $lenght);
- }
- // returns a string before a tab and truncates the string
- function tsv_remove_tabs(&$string)
- {
- $pos = strpos($string, "\t");
- if(is_integer($pos))
- {
- $result = substr($string,0,$pos);
- $string = substr($string,$pos+1);
- }
- else
- {
- $result = $string;
- $string = "";
- }
- return $result;
- }
- // remove the quotes from a string
- function tsv_remove_quotes($string)
- {
- if(strpos($string,'"') == 0)
- $string = substr($string,1,strlen($string)-1);
- if(strrpos($string,'"') == strlen($string)-1)
- $string = substr($string,0,strlen($string)-1);
- return $string;
- }
- ?>
Add Comment
Please, Sign In to add comment