Ignore:
Timestamp:
Jun 27, 2009, 9:09:12 PM (15 years ago)
Author:
george
Message:
  • Přidáno: Knihovna pro převod kódování znaků.
Location:
trunk/inc/teamspeakdisplay
Files:
1 deleted
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/inc/teamspeakdisplay/teamspeakdisplay.php

    r405 r607  
    1818
    1919class teamspeakDisplayClass {
    20        
    21         // Removes subsequent end of line charachter from the right part of a string
    22         function _stripEOL($evalString) {
    23                 $newLen = strlen($evalString);
    24                 while (((substr($evalString, $newLen - 1, 1) == "\r")) || ((substr($evalString, $newLen - 1, 1) == "\n"))) {
    25                         $newLen--;
    26                 }
    27                 return substr($evalString, 0, $newLen);
    28         }
    29        
    30         // Opens a connection to the teamspeak server
    31         function _openConnection(&$socket, $host, $port, $timeout) {
    32                 @$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
    33                 if ($socket and ($this->_stripEOL(fgets($socket, 4096)) == "[TS]")) {
    34                         return true;
    35                 } else {
    36                         return false;
    37                 }
    38         }
    39        
    40         // Closes the connection to the Teamspeak server
    41         function _closeConnection($socket) {
    42                 fputs($socket, "quit\n");
    43                 fclose($socket);
    44         }
    45         // odstrannie diakritiky
    46         function Kodovani ($string)
    47         {
    48     $st_new = strtr($string,
    49         "\xe1\xe4\xe8\xef\xe9\xec\xed\xbe\xe5\xf2\xf3\xf6\xf5\xf4\xf8\xe0\x9a\x9d\xfa\xf9\xfc\xfb\xfd\x9e\xc1\xc4\xc8\xcf\xc9\xcc\xcd\xbc\xc5\xd2\xd3\xd6\xd5\xd4\xd8\xc0\x8a\x8d\xda\xd9\xdc\xdb\xdd\x8e",
    50         "aacdeeillnoooorrstuuuuyzAACDEEILLNOOOORRSTUUUUYZ"
    51     );
     20 
     21  // Removes subsequent end of line charachter from the right part of a string
     22  function _stripEOL($evalString) {
     23    $newLen = strlen($evalString);
     24    while (((substr($evalString, $newLen - 1, 1) == "\r")) || ((substr($evalString, $newLen - 1, 1) == "\n"))) {
     25      $newLen--;
     26    }
     27    return substr($evalString, 0, $newLen);
     28  }
     29 
     30  // Opens a connection to the teamspeak server
     31  function _openConnection(&$socket, $host, $port, $timeout) {
     32    @$socket = fsockopen($host, $port, $errno, $errstr, $timeout);
     33    if ($socket and ($this->_stripEOL(fgets($socket, 4096)) == "[TS]")) {
     34      return true;
     35    } else {
     36      return false;
     37    }
     38  }
     39 
     40  // Closes the connection to the Teamspeak server
     41  function _closeConnection($socket) {
     42    fputs($socket, "quit\n");
     43    fclose($socket);
     44  }
     45  // odstrannie diakritiky
     46  function Kodovani ($string)
     47  {
     48    $st_new = ToUTF8($string);
     49//    $st_new = strtr($string,       "\xe1\xe4\xe8\xef\xe9\xec\xed\xbe\xe5\xf2\xf3\xf6\xf5\xf4\xf8\xe0\x9a\x9d\xfa\xf9\xfc\xfb\xfd\x9e\xc1\xc4\xc8\xcf\xc9\xcc\xcd\xbc\xc5\xd2\xd3\xd6\xd5\xd4\xd8\xc0\x8a\x8d\xda\xd9\xdc\xdb\xdd\x8e","aacdeeillnoooorrstuuuuyzAACDEEILLNOOOORRSTUUUUYZ" );
    5250    $st_new = htmlspecialchars($st_new);
    5351    return $st_new;
    5452  }
    55        
    56         // Returns the part of evalString until a tab (or the end of a string) and deletes the
    57         // returned part from evalString (including the possible tab that follows)
    58         function _stripPartFromString(&$evalString) {
    59                 $pos = strpos($evalString, "\t");
    60                 if(is_integer($pos)) {
    61                         $result = substr($evalString, 0, $pos);
    62                         $evalString = substr($evalString, $pos + 1);
    63                 } else {
    64                         $result = $evalString;
    65                         $evalString = "";
    66                 }
    67                 return $result;
    68         }
    69        
    70         // Removes the surrounding quotes from evalString and returns the result
    71         function _stripQuotes($evalString) {
    72                 if(strpos($evalString, '"') == 0) $evalString = substr($evalString, 1, strlen($evalString) - 1);
    73                 if(strrpos($evalString, '"') == strlen($evalString) - 1) $evalString = substr($evalString, 0, strlen($evalString) - 1);
    74                 return $evalString;
    75         }
    76        
    77         // Request, read and parse the server info:
    78         function _getServerInfo($socket) {
    79                 fputs($socket, "si\n");
    80                 $result = array();
    81                 do {
    82                         $buffer = $this->_stripEOL(fgets($socket, 4096));
    83                         if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
    84                                 $pos = strpos($buffer, '=');
    85                                 if ($pos !== False) {
    86                                         $result[substr($buffer, 0, $pos)] = substr($buffer, $pos + 1);
    87                                 }
    88                         }
    89                 } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
    90                 return $result;
    91         }
    92        
    93         function _setPlayerDisplayImage(&$playerInfo) {
    94                 // Determine the right userpicture:
    95                 if (($playerInfo["attribute"] & 8) == 8) { $playerImage = "away"; }
    96                 else if (($playerInfo["attribute"] & 32) == 32) { $playerImage = "mutespeakers"; }
    97                 else if (($playerInfo["attribute"] & 16) == 16) { $playerImage = "mutemicrophone"; }
    98                 else if (($playerInfo["attribute"] & 1) == 1) { $playerImage = "channelcommander"; }
    99                 else { $playerImage = "normal"; }
    100                 $playerInfo["displayimage"] = $playerImage;
    101         }
    102        
    103         function _setPlayerDisplayName(&$playerInfo) {
    104                 // Determine the player status (U = Unregistered, R = Registered, SA = Server Admin,
    105                 // CA = Channel Admin, AO = Auto-Operator, AV = Auto-Voice, O = Operator, V = Voice)
    106                 if (($playerInfo["userstatus"] & 4) == 4) { $playerstatus = "R"; } else { $playerstatus = 'U'; }
    107                 if (($playerInfo["userstatus"] & 1) == 1) { $playerstatus .= " SA"; }
    108                 if (($playerInfo["privileg"] & 1) == 1) { $playerstatus .= " CA"; }
    109                 if (($playerInfo["privileg"] & 8) == 8) { $playerstatus .= " AO"; }
    110                 if (($playerInfo["privileg"] & 16) == 16) { $playerstatus .= " AV"; }
    111                 if (($playerInfo["privileg"] & 2) == 2) { $playerstatus .= " O"; }
    112                 if (($playerInfo["privileg"] & 4) == 4) { $playerstatus .= " V"; }
    113                 if (($playerInfo["attribute"] & 64) == 64) { $playerstatus .= " Rec"; }
    114                
    115                 // Determine the player attributes to be listed behind the player status (WV = Want Voice)
    116                 if (($playerInfo["attribute"] & 2) == 2) { $playerattributes = ' WV'; } else { $playerattributes = ''; }
    117                
    118                 $playerInfo["displayname"] = $playerInfo["playername"] . " (" . $playerstatus . ")" . $playerattributes;
    119         }
    120        
    121         function _getPlayerList($socket) {
    122                 // Request, read and parse the player list
    123                 fputs($socket, "pl\n");
    124                 $buffer = $this->_stripEOL(fgets($socket, 4096));
    125                 $result = array();
    126                 if (strtoupper(substr($buffer, 0, 5)) == "ERROR") { return $result; }
    127                 do {
    128                         $buffer = $this->_stripEOL(fgets($socket, 4096));
    129                         if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
    130                                 $playerid = $this->_stripPartFromString($buffer);
    131                                 $result[$playerid] = array(
    132                                         "playerid" => $playerid,
    133                                         "channelid" => $this->_stripPartFromString($buffer),
    134                                         "receivedpackets" => $this->_stripPartFromString($buffer),
    135                                         "receivedbytes" => $this->_stripPartFromString($buffer),
    136                                         "sentpackets" => $this->_stripPartFromString($buffer),
    137                                         "sentbytes" => $this->_stripPartFromString($buffer),
    138                                         "paketlost" => $this->_stripPartFromString($buffer) / 100,
    139                                         "pingtime" => $this->_stripPartFromString($buffer),
    140                                         "totaltime" => $this->_stripPartFromString($buffer),
    141                                         "idletime" => $this->_stripPartFromString($buffer),
    142                                         "privileg" => $this->_stripPartFromString($buffer),
    143                                         "userstatus" => $this->_stripPartFromString($buffer),
    144                                         "attribute" => $this->_stripPartFromString($buffer),
    145                                         "ip" => $this->_stripPartFromString($buffer),
    146                                         "playername" => $this->_stripQuotes($this->_stripPartFromString($buffer)),
    147                                         "loginname" => $this->_stripQuotes($this->_stripPartFromString($buffer))
    148                                 );
    149                                 $this->_setPlayerDisplayImage($result[$playerid]);
    150                                 $this->_setPlayerDisplayName($result[$playerid]);
    151                         }
    152                 } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
    153                 return $result;
    154         }
    155        
    156         function _getLimitedPlayerList($socket, $channelList) {
    157                 $playerList = $this->_getPlayerList($socket);
    158                 $result = array();
    159                 foreach($playerList as $playerInfo) {
    160                         foreach($channelList as $channelInfo) {
    161                                 if ($playerInfo["channelid"] == $channelInfo["channelid"]) {
    162                                         $result[$playerInfo["playerid"]] = $playerInfo;
    163                                 }
    164                         }
    165                 }
    166                 return $result;
    167         }
    168        
    169         function _setChannelDisplayName(&$channelInfo) {
    170                 if ($channelInfo["parent"] != -1) {
    171                         $channelInfo["displayname"] = $channelInfo["channelname"];
    172                 } else {
    173                         // Determine the channel status (U = Unregisterd, R = Registered, M = Moderated,
    174                         // P = Passworded, S = Sub-channels, D = Default).
    175                         if (($channelInfo["flags"] & 1) == 1) { $channelstatus = 'U'; } else { $channelstatus = 'R'; }
    176                         if (($channelInfo["flags"] & 2) == 2) { $channelstatus .= 'M'; }
    177                         if (($channelInfo["flags"] & 4) == 4) { $channelstatus .= 'P'; }
    178                         if (($channelInfo["flags"] & 8) == 8) { $channelstatus .= 'S'; }
    179                         if (($channelInfo["flags"] & 16) == 16) { $channelstatus .= 'D'; }
    180                         $channelInfo["displayname"] = $channelInfo["channelname"] . " (" . $channelstatus . ")";
    181                 }
    182         }
    183        
    184         function _getChannelList($socket) {
    185                 // Request, read and parse the channel list
    186                 fputs($socket, "cl\n");
    187                 $buffer = $this->_stripEOL(fgets($socket, 4096));
    188                 $result = array();
    189                 if (strtoupper(substr($buffer, 0, 5)) == "ERROR") { return $result; }
    190                 do {
    191                         $buffer = $this->_stripEOL(fgets($socket, 4096));
    192                         if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
    193                                 $channelid = $this->_stripPartFromString($buffer);
    194                                 $result[$channelid] = array(
    195                                         "channelid" => $channelid,
    196                                         "codec" => $this->_stripPartFromString($buffer),
    197                                         "parent" => $this->_stripPartFromString($buffer),
    198                                         "order" => $this->_stripPartFromString($buffer),
    199                                         "maxplayers" => $this->_stripPartFromString($buffer),
    200                                         "channelname" => $this->_stripQuotes($this->_stripPartFromString($buffer)),
    201                                         "flags" => $this->_stripPartFromString($buffer),
    202                                         "password" => $this->_stripPartFromString($buffer),
    203                                         "topic" => $this->_stripQuotes($this->_stripPartFromString($buffer))
    204                                 );
    205                                 $this->_setChannelDisplayName($result[$channelid]);
    206                         }
    207                 } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
    208                 return $result;
    209         }
    210        
    211         function _getLimitedChannelList($socket, $limitChannel) {
    212                 $channelList = $this->_getChannelList($socket);
    213                 $result = array();
    214                 foreach($channelList as $channelInfo) {
    215                         if ($channelInfo["parent"] == -1) {
    216                                 if ($channelInfo["channelname"] == $limitChannel) {
    217                                         $result[$channelInfo["channelid"]] = $channelInfo;
    218                                         foreach($channelList as $subChannelInfo) {
    219                                                 if ($subChannelInfo["parent"] == $channelInfo["channelid"]) {
    220                                                         $result[$subChannelInfo["channelid"]] = $subChannelInfo;
    221                                                 }
    222                                         }
    223                                 }
    224                         }
    225                 }
    226                 return $result;
    227         }
    228        
    229         function _selectServer($socket, $port) {
    230                 // Request the server to select the server which is hosted on  the port set in serverUDPPort
    231                 fputs($socket, "sel ".$port . "\n");
    232                
    233                 // Read server response on request to select a server
    234                 return ($this->_stripEOL(fgets($socket, 4096)) == "OK");
    235         }
    236        
    237         // Queries the Teamspeak server
    238         function queryTeamspeakServerEx($settings) {
    239                 $result = array();
    240                
    241                 // Try to establish a connection to the teamspeak server
    242                 if (! $this->_openConnection($socket, $settings["serveraddress"], $settings["serverqueryport"], 0.3)) {
    243                         $result["queryerror"] = 1;
    244                 } else if (! $this->_selectServer($socket, $settings["serverudpport"])) {
    245                         $result["queryerror"] = 2;
    246                         $this->_closeConnection($socket);
    247                 } else {
    248                         $result["queryerror"] = 0;
    249                         $result["serverinfo"] = $this->_getServerInfo($socket);
    250                         $result["channellist"] = ($settings["limitchannel"] == "") ? $this->_getChannelList($socket) : $this->_getLimitedChannelList($socket, $settings["limitchannel"]);
    251                         $result["playerlist"] = ($settings["limitchannel"] == "") ? $this->_getPlayerList($socket) : $this->_getLimitedPlayerList($socket, $result["channellist"]);
    252                         $this->_closeConnection($socket);
    253                 }
    254                 return $result;
    255         }
    256        
    257         function queryTeamspeakServer($serverAddress, $serverUDPPort, $serverQueryPort) {
    258                 $settings = $this->getDefaultSettings();
    259                 $settings["serveraddress"] = $serverAddress;
    260                 $settings["serverudpport"] = $serverUDPPort;
    261                 $settings["serverqueryport"] = $serverQueryPort;
    262                 return $this->queryTeamspeakServerEx($settings);
    263         }
    264        
    265         function _orderAlphaGetString($string) {
    266                 $lowerstring = strtolower($string);
    267                 $result = "";
    268                 for ($i = 0; $i < strlen($lowerstring); $i++) {
    269                         if (strpos("0123456789abcdefghijklmnopqrstuvwxyz", substr($lowerstring, $i, 1)) !== false) {
    270                                 $result .= substr($lowerstring, $i, 1);
    271                         }
    272                 }
    273                 return $result;
    274         }
    275        
    276         function _orderAlpha($str1, $str2) {
    277                 return strcmp($this->_orderAlphaGetString($str1), $this->_orderAlphaGetString($str2));
    278         }
    279        
    280         function _compareChannel($a, $b) {
    281                 if ($a["order"] != $b["order"]) { return ($a["order"] < $b["order"]) ? -1 : 1; }
    282                 else { return $this->_orderAlpha($a["displayname"], $b["displayname"]); }
    283         }
    284        
    285         function _comparePlayer($a, $b) {
    286                 // Determine userlevel (0 = Not server admin, 1 = Server admin)
    287                 $userlevela = $a["userstatus"] & 1;
    288                 $userlevelb = $b["userstatus"] & 1;
    289                 if ($userlevela != $userlevelb) { return ($userlevela < $userlevelb) ? 1 : -1; }
    290                 else { return $this->_orderAlpha($a["displayname"], $b["displayname"]); }
    291         }
    292        
    293         function sortServerInfo(&$serverInfo) {
    294                 usort($serverInfo["channellist"], array($this, "_compareChannel"));
    295                 usort($serverInfo["playerlist"], array($this, "_comparePlayer"));
    296         }
    297        
    298         function _formatTime($totaltime) {
    299                 $hours = floor($totaltime / 3600);
    300                 $minutes = floor(($totaltime % 3600) / 60);
    301                 return (($hours < 10) ? "0" : "") . $hours . ":" . (($minutes < 10) ? "0" : "") . $minutes;
    302         }
    303        
    304         // Returns the codec name
    305         function _getCodecName($codec) {
    306                 if ($codec == 0) { return "CELP 5.1 Kbit"; }
    307                 else if ($codec == 1) { return "CELP 6.3 Kbit"; }
    308                 else if ($codec == 2) { return "GSM 14.8 Kbit"; }
    309                 else if ($codec == 3) { return "GSM 16.4 Kbit"; }
    310                 else if ($codec == 4) { return "CELP Windows 5.2 Kbit"; }
    311                 else if ($codec == 5) { return "Speex 3.4 Kbit"; }
    312                 else if ($codec == 6) { return "Speex 5.2 Kbit"; }
    313                 else if ($codec == 7) { return "Speex 7.2 Kbit"; }
    314                 else if ($codec == 8) { return "Speex 9.3 Kbit"; }
    315                 else if ($codec == 9) { return "Speex 12.3 Kbit"; }
    316                 else if ($codec == 10) { return "Speex 16.3 Kbit"; }
    317                 else if ($codec == 11) { return "Speex 19.5 Kbit"; }
    318                 else if ($codec == 12) { return "Speex 25.9 Kbit"; }
    319                 else { return "Unknown (" . $codec . ")"; }
    320         }
    321        
    322         function getDefaultSettings() {
    323                 $result = array();
    324                 $result["serveraddress"] = "";
    325                 $result["serverudpport"] = 8767;
    326                 $result["serverqueryport"] = 51234;
    327                 $result["limitchannel"] = "";
    328                 $result["forbiddennicknamechars"] = "()[]{}";
    329                 return $result;
    330         }
    331        
    332         // Main function (queries, sorts and displays the teamspeak serverinfo). Its code is not
    333         // very readable... well what shall I say about it... it was hard to write so it should
    334         // be hard to read >:)
    335         function displayTeamspeakEx($settings) {
    336                 $serverInfo = $this->queryTeamspeakServerEx($settings);
    337                
    338                 echo("<div id=\"teamspeakdisplay\">\n");
    339                 if ($serverInfo["queryerror"] != 0) {
    340                         $popupInfo = "Server address: " . $settings["serveraddress"] . (($settings["serverudpport"] != 8767) ? (":" . $settings["serverudpport"]): "");
    341                         if ($serverInfo["queryerror"] == 1) {
    342                                 $popupInfo .= ", Error: could not connect to query port";
    343                         } else {
    344                                 $popupInfo .= ", Error: no server running on port " . $settings["serverudpport"];
    345                         }
    346                         echo("<table><tr><td>");
    347                         echo("<img src=\"inc/teamspeakdisplay/teamspeak_offline.png\" alt=\"\" title=\"" . $popupInfo . "\">");
    348                         echo("</td><td class=\"teamspeakserver\" title=\"" . $popupInfo . "\">");
    349                         echo("Server offline");
    350                         echo("</td></tr></table>\n");
    351                 } else {
    352                         $this->sortServerInfo($serverInfo);
    353                        
    354                         // Generate javascript for teamspeak hyperlinks
    355                         $jsTeamspeakId = md5($settings["serveraddress"] . ":" . $settings["serverudpport"]);
    356                         echo("<script type=\"text/javascript\"><!--\n");
    357                         echo("function stringOk_" . $jsTeamspeakId . "(string, forbiddenChars) {\n");
    358                         echo("  for(var i = 0; i < string.length; i++) {\n");
    359                         echo("          if (forbiddenChars.indexOf(string.charAt(i)) > -1) {\n");
    360                         echo("                  return false;\n");
    361                         echo("          }\n");
    362                         echo("  }\n");
    363                         echo("  return true;\n");
    364                         echo("}\n");
    365                         echo("function enterServer_" . $jsTeamspeakId . "() {\n");
    366                         echo("  enterSubChannel_" . $jsTeamspeakId . "(null, false, null);\n");
    367                         echo("}\n");
    368                         echo("function enterChannel_" . $jsTeamspeakId . "(channelName, channelPassworded) {\n");
    369                         echo("  enterSubChannel_" . $jsTeamspeakId . "(channelName, channelPassworded, null);\n");
    370                         echo("}\n");
    371                         echo("function enterSubChannel_" . $jsTeamspeakId . "(channelName, channelPassworded, subChannelName) {\n");
    372                         echo("  var serveraddress = 'teamspeak://" . $settings["serveraddress"] . ":" . $settings["serverudpport"] . "';\n");
    373                         echo("  var nickname=window.prompt('Enter your nickname', '');\n");
    374                         echo("  if (nickname == null) {\n");
    375                         echo("          return;\n");
    376                         echo("  } else if (! stringOk_" . $jsTeamspeakId . "(nickname, '" . str_replace("'", "\\'", $settings["forbiddennicknamechars"]) . "')) {\n");
    377                         echo("          window.alert('Could not enter the teamspeak server because the nickname you entered contains one or more of these forbidden characters: " . str_replace("'", "\\'", $settings["forbiddennicknamechars"]) . "');\n");
    378                         echo("          return;\n");
    379                         echo("  } else if (nickname == \"\") {\n");
    380                         echo("          window.alert('Could not enter the teamspeak server because you did not enter your nickname');\n");
    381                         echo("          return;\n");
    382                         echo("  }\n");
    383                         echo("  serveraddress = serveraddress + \"/nickname=\" + escape(nickname);\n");
    384                         if ($serverInfo["serverinfo"]["server_password"] == "1") {
    385                                 echo("  var password=window.prompt('Enter the teamspeak server password for " . $serverInfo["serverinfo"]["server_name"] . "', '');\n");
    386                                 echo("  if (password == null) {\n");
    387                                 echo("          return;\n");
    388                                 echo("  } else if (password == \"\") {\n");
    389                                 echo("          window.alert('Could not enter the teamspeak server because you did not enter a server password');\n");
    390                                 echo("          return;\n");
    391                                 echo("  }\n");
    392                                 echo("  serveraddress = serveraddress + \"?password=\" + escape(password);\n");
    393                         }
    394                         echo("  if (channelName != null) { serveraddress = serveraddress + \"?channel=\" + escape(channelName); }\n");
    395                         echo("  if (channelPassworded) {\n");
    396                         echo("          var channelpassword=window.prompt('Enter the channel password for channel ' + channelName, '');\n");
    397                         echo("          if (channelpassword == null) {\n");
    398                         echo("                  return;\n");
    399                         echo("          } else if (channelpassword == \"\") {\n");
    400                         echo("                  window.alert('Could not enter the teamspeak server because you did not enter a channel password');\n");
    401                         echo("                  return;\n");
    402                         echo("          }\n");
    403                         echo("          serveraddress = serveraddress + \"?channelpassword=\" + escape(channelpassword);\n");
    404                         echo("  }\n");
    405                         echo("  if (subChannelName != null) { serveraddress = serveraddress + \"?subchannel=\" + escape(subChannelName); }\n");
    406                         echo("  window.location=serveraddress;\n");
    407                         echo("}\n");
    408                         echo("//--></script>\n");
    409                        
    410                         $popupInfo = "Server address: " . $settings["serveraddress"] . (($settings["serverudpport"] != 8767) ? (":" . $settings["serverudpport"]): "") . ", Max players: " . $serverInfo["serverinfo"]["server_maxusers"] . ", Uptime: " . $this->_formatTime($serverInfo["serverinfo"]["server_uptime"]);
    411                        
    412                         // Print the topmost element of the teamspeak tree
    413                         echo("<table><tr><td>");
    414                         echo("<img src=\"inc/teamspeakdisplay/teamspeak_online.png\" alt=\"\" title=\"" . $popupInfo . "\">");
    415                         echo("</td><td class=\"teamspeakserver\" title=\"" . $popupInfo . "\">");
    416                         echo("<a class=\"teamspeakserver\" href=\"javascript:enterServer_" . $jsTeamspeakId . "();\">");
    417                         echo(str_replace(" ", "&nbsp;", $this -> Kodovani($serverInfo["serverinfo"]["server_name"])));
    418                         echo("</a>");
    419                         echo("</td></tr></table>\n");
    420                        
    421                         // Count the number of channels to be listed:
    422                         $currentchannels = 0;
    423                         foreach($serverInfo["channellist"] as $channelInfo) {
    424                                 if ($channelInfo["parent"] == -1) {
    425                                         $currentchannels++;
    426                                 }
    427                         }
    428                        
    429                         // Initialize the channelcounter to zero
    430                         $counter = 0;
    431                        
    432                         // Loop through all channels:
    433                         foreach($serverInfo["channellist"] as $channelInfo) { if ($channelInfo["parent"] == -1) {
    434                                
    435                                 // determine number of players in channel
    436                                 $currentplayers = 0;
    437                                 foreach($serverInfo["playerlist"] as $playerInfo) {
    438                                         if($playerInfo["channelid"] == $channelInfo["channelid"]) $currentplayers++;
    439                                 }
    440                                
    441                                 // Count the number of channels to be listed:
    442                                 $currentplayersandsubchannels = $currentplayers;
    443                                 foreach($serverInfo["channellist"] as $subchannelInfo) {
    444                                         if ($subchannelInfo["parent"] == $channelInfo["channelid"]) {
    445                                                 $currentplayersandsubchannels++;
    446                                         }
    447                                 }
    448                                
    449                                 $popupInfo = "Max players: " . $channelInfo["maxplayers"] . ", Codec: " . $this->_getCodecName($channelInfo["codec"]);
    450                                 if ($channelInfo["topic"] != "") { $popupInfo = $popupInfo . ", Topic: " . $channelInfo["topic"]; }
    451                                
    452                                 // Display channel:
    453                                 echo("<table><tr><td>");
    454                                 echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "3" : "2") . ".png\" alt=\"\">");
    455                                 echo("<img src=\"inc/teamspeakdisplay/channel.png\" alt=\"\" title=\"" . $popupInfo . "\">");
    456                                 echo("</td><td class=\"teamspeakchannel\" title=\"" . $popupInfo . "\">");
    457                                 echo("<a class=\"teamspeakchannel\" href=\"javascript:enterChannel_" . $jsTeamspeakId . "('" . str_replace("'", "\'", $channelInfo["channelname"]) . "', " . (($channelInfo["password"]) == "1" ? "true" : "false") . ");\">");
    458                                 echo(str_replace(" ", "&nbsp;", $this -> Kodovani($channelInfo["displayname"])));
    459                                 echo("</a>");
    460                                 echo("</td></tr></table>\n");
    461                                
    462                                 // Initialize the playercounter for this channel to zero
    463                                 $counter_playerandsubchannels = 0;
    464                                
    465                                 // Loop through all players in the current channel:
    466                                 foreach($serverInfo["playerlist"] as $playerInfo) {
    467                                        
    468                                         // Is the current player in the current channel?
    469                                         if ($playerInfo["channelid"] == $channelInfo["channelid"]) {
    470                                                
    471                                                 $popupInfo = "Time online: " . $this->_formatTime($playerInfo["totaltime"]) . ", Time idle: " . $this->_formatTime($playerInfo["idletime"]) . ", Ping: " . $playerInfo["pingtime"] . "ms";
    472                                                
    473                                                 // Display player:
    474                                                 echo("<table><tr><td>");
    475                                                 echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
    476                                                 echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "3" : "2") . ".png\" alt=\"\">");
    477                                                 echo("<img src=\"inc/teamspeakdisplay/player_" . $playerInfo["displayimage"] . ".png\" alt=\"" . $playerInfo["displayimage"] . "\" title=\"" . $popupInfo . "\">");
    478                                                 echo("</td><td class=\"teamspeakplayer\" title=\"" . $popupInfo . "\">");
    479                                                 echo(str_replace(" ", "&nbsp;", $this -> Kodovani($playerInfo["displayname"])));
    480                                                 echo("</td></tr></table>\n");
    481                                                
    482                                                 // Increase the player counter:
    483                                                 $counter_playerandsubchannels++;
    484                                         }
    485                                 }
    486                                
    487                                 // Loop through all channels:
    488                                 foreach($serverInfo["channellist"] as $subchannelInfo) { if ($subchannelInfo["parent"] == $channelInfo["channelid"]) {
    489                                         // determine number of players in channel
    490                                         $currentplayers = 0;
    491                                         foreach($serverInfo["playerlist"] as $playerInfo) {
    492                                                 if($playerInfo["channelid"] == $subchannelInfo["channelid"]) $currentplayers++;
    493                                         }
    494                                        
    495                                         $popupInfo = "Max players: " . $subchannelInfo["maxplayers"] . ", Codec: " . $this->_getCodecName($subchannelInfo["codec"]);
    496                                         if ($subchannelInfo["topic"] != "") { $popupInfo = $popupInfo . ", Topic: " . $subchannelInfo["topic"]; }
    497                                        
    498                                         // Display channel:
    499                                         echo("<table><tr><td>");
    500                                         echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
    501                                         echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "3" : "2") . ".png\" alt=\"\">");
    502                                         echo("<img src=\"inc/teamspeakdisplay/channel.png\" alt=\"\" title=\"" . $popupInfo . "\">");
    503                                         echo("</td><td class=\"teamspeaksubchannel\" title=\"" . $popupInfo . "\">");
    504                                         echo("<a class=\"teamspeaksubchannel\" href=\"javascript:enterSubChannel_" . $jsTeamspeakId . "('" . str_replace("'", "\'", $channelInfo["channelname"]) . "', " . (($channelInfo["password"]) == "1" ? "true" : "false") . ", '" . str_replace("'", "\'", $subchannelInfo["channelname"]) . "');\">");
    505                                         echo(str_replace(" ", "&nbsp;", $this -> Kodovani($subchannelInfo["displayname"])));
    506                                         echo("</a>");
    507                                         echo("</td></tr></table>\n");
    508                                        
    509                                         // Initialize the playercounter for this channel to zero
    510                                         $counter_player = 0;
    511                                        
    512                                         // Loop through all players in the current channel:
    513                                         foreach($serverInfo["playerlist"] as $playerInfo) {
    514                                                
    515                                                 // Is the current player in the current channel?
    516                                                 if ($playerInfo["channelid"] == $subchannelInfo["channelid"]) {
    517                                                        
    518                                                         $popupInfo = "Time online: " . $this->_formatTime($playerInfo["totaltime"]) . ", Time idle: " . $this->_formatTime($playerInfo["idletime"]) . ", Ping: " . $playerInfo["pingtime"] . "ms";
    519                                                        
    520                                                         // Display player:
    521                                                         echo("<table><tr><td>");
    522                                                         echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
    523                                                         echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "4" : "1") . ".png\" alt=\"\">");
    524                                                         echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_player + 1) == $currentplayers) ? "3" : "2") . ".png\" alt=\"\">");
    525                                                         echo("<img src=\"inc/teamspeakdisplay/player_" . $playerInfo["displayimage"] . ".png\" alt=\"" . $playerInfo["displayimage"] . "\" title=\"" . $popupInfo . "\">");
    526                                                         echo("</td><td class=\"teamspeakplayer\" title=\"" . $popupInfo . "\">");
    527                                                         echo(str_replace(" ", "&nbsp;", $this -> Kodovani($playerInfo["displayname"])));
    528                                                         echo("</td></tr></table>\n");
    529                                                        
    530                                                         // Increase the player counter:
    531                                                         $counter_player++;
    532                                                 }
    533                                         }
    534                                        
    535                                         // Increase the channelcounter
    536                                         $counter_playerandsubchannels++;
    537                                 } }
    538                                
    539                                 // Increase the channelcounter
    540                                 $counter++;
    541                         } }
    542                 }
    543                 echo("</div>\n");
    544         }
    545        
    546         function displayTeamspeak($serverAddress, $serverUDPPort=8767, $serverQueryPort=51234) {
    547                 $settings = $this->getDefaultSettings();
    548                 $settings["serveraddress"] = $serverAddress;
    549                 $settings["serverudpport"] = $serverUDPPort;
    550                 $settings["serverqueryport"] = $serverQueryPort;
    551                 $this->displayTeamspeakEx($settings);
    552         }
    553        
     53 
     54  // Returns the part of evalString until a tab (or the end of a string) and deletes the
     55  // returned part from evalString (including the possible tab that follows)
     56  function _stripPartFromString(&$evalString) {
     57    $pos = strpos($evalString, "\t");
     58    if(is_integer($pos)) {
     59      $result = substr($evalString, 0, $pos);
     60      $evalString = substr($evalString, $pos + 1);
     61    } else {
     62      $result = $evalString;
     63      $evalString = "";
     64    }
     65    return $result;
     66  }
     67 
     68  // Removes the surrounding quotes from evalString and returns the result
     69  function _stripQuotes($evalString) {
     70    if(strpos($evalString, '"') == 0) $evalString = substr($evalString, 1, strlen($evalString) - 1);
     71    if(strrpos($evalString, '"') == strlen($evalString) - 1) $evalString = substr($evalString, 0, strlen($evalString) - 1);
     72    return $evalString;
     73  }
     74 
     75  // Request, read and parse the server info:
     76  function _getServerInfo($socket) {
     77    fputs($socket, "si\n");
     78    $result = array();
     79    do {
     80      $buffer = $this->_stripEOL(fgets($socket, 4096));
     81      if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
     82        $pos = strpos($buffer, '=');
     83        if ($pos !== False) {
     84          $result[substr($buffer, 0, $pos)] = substr($buffer, $pos + 1);
     85        }
     86      }
     87    } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
     88    return $result;
     89  }
     90 
     91  function _setPlayerDisplayImage(&$playerInfo) {
     92    // Determine the right userpicture:
     93    if (($playerInfo["attribute"] & 8) == 8) { $playerImage = "away"; }
     94    else if (($playerInfo["attribute"] & 32) == 32) { $playerImage = "mutespeakers"; }
     95    else if (($playerInfo["attribute"] & 16) == 16) { $playerImage = "mutemicrophone"; }
     96    else if (($playerInfo["attribute"] & 1) == 1) { $playerImage = "channelcommander"; }
     97    else { $playerImage = "normal"; }
     98    $playerInfo["displayimage"] = $playerImage;
     99  }
     100 
     101  function _setPlayerDisplayName(&$playerInfo) {
     102    // Determine the player status (U = Unregistered, R = Registered, SA = Server Admin,
     103    // CA = Channel Admin, AO = Auto-Operator, AV = Auto-Voice, O = Operator, V = Voice)
     104    if (($playerInfo["userstatus"] & 4) == 4) { $playerstatus = "R"; } else { $playerstatus = 'U'; }
     105    if (($playerInfo["userstatus"] & 1) == 1) { $playerstatus .= " SA"; }
     106    if (($playerInfo["privileg"] & 1) == 1) { $playerstatus .= " CA"; }
     107    if (($playerInfo["privileg"] & 8) == 8) { $playerstatus .= " AO"; }
     108    if (($playerInfo["privileg"] & 16) == 16) { $playerstatus .= " AV"; }
     109    if (($playerInfo["privileg"] & 2) == 2) { $playerstatus .= " O"; }
     110    if (($playerInfo["privileg"] & 4) == 4) { $playerstatus .= " V"; }
     111    if (($playerInfo["attribute"] & 64) == 64) { $playerstatus .= " Rec"; }
     112   
     113    // Determine the player attributes to be listed behind the player status (WV = Want Voice)
     114    if (($playerInfo["attribute"] & 2) == 2) { $playerattributes = ' WV'; } else { $playerattributes = ''; }
     115   
     116    $playerInfo["displayname"] = $playerInfo["playername"] . " (" . $playerstatus . ")" . $playerattributes;
     117  }
     118 
     119  function _getPlayerList($socket) {
     120    // Request, read and parse the player list
     121    fputs($socket, "pl\n");
     122    $buffer = $this->_stripEOL(fgets($socket, 4096));
     123    $result = array();
     124    if (strtoupper(substr($buffer, 0, 5)) == "ERROR") { return $result; }
     125    do {
     126      $buffer = $this->_stripEOL(fgets($socket, 4096));
     127      if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
     128        $playerid = $this->_stripPartFromString($buffer);
     129        $result[$playerid] = array(
     130          "playerid" => $playerid,
     131          "channelid" => $this->_stripPartFromString($buffer),
     132          "receivedpackets" => $this->_stripPartFromString($buffer),
     133          "receivedbytes" => $this->_stripPartFromString($buffer),
     134          "sentpackets" => $this->_stripPartFromString($buffer),
     135          "sentbytes" => $this->_stripPartFromString($buffer),
     136          "paketlost" => $this->_stripPartFromString($buffer) / 100,
     137          "pingtime" => $this->_stripPartFromString($buffer),
     138          "totaltime" => $this->_stripPartFromString($buffer),
     139          "idletime" => $this->_stripPartFromString($buffer),
     140          "privileg" => $this->_stripPartFromString($buffer),
     141          "userstatus" => $this->_stripPartFromString($buffer),
     142          "attribute" => $this->_stripPartFromString($buffer),
     143          "ip" => $this->_stripPartFromString($buffer),
     144          "playername" => $this->_stripQuotes($this->_stripPartFromString($buffer)),
     145          "loginname" => $this->_stripQuotes($this->_stripPartFromString($buffer))
     146        );
     147        $this->_setPlayerDisplayImage($result[$playerid]);
     148        $this->_setPlayerDisplayName($result[$playerid]);
     149      }
     150    } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
     151    return $result;
     152  }
     153 
     154  function _getLimitedPlayerList($socket, $channelList) {
     155    $playerList = $this->_getPlayerList($socket);
     156    $result = array();
     157    foreach($playerList as $playerInfo) {
     158      foreach($channelList as $channelInfo) {
     159        if ($playerInfo["channelid"] == $channelInfo["channelid"]) {
     160          $result[$playerInfo["playerid"]] = $playerInfo;
     161        }
     162      }
     163    }
     164    return $result;
     165  }
     166 
     167  function _setChannelDisplayName(&$channelInfo) {
     168    if ($channelInfo["parent"] != -1) {
     169      $channelInfo["displayname"] = $channelInfo["channelname"];
     170    } else {
     171      // Determine the channel status (U = Unregisterd, R = Registered, M = Moderated,
     172      // P = Passworded, S = Sub-channels, D = Default).
     173      if (($channelInfo["flags"] & 1) == 1) { $channelstatus = 'U'; } else { $channelstatus = 'R'; }
     174      if (($channelInfo["flags"] & 2) == 2) { $channelstatus .= 'M'; }
     175      if (($channelInfo["flags"] & 4) == 4) { $channelstatus .= 'P'; }
     176      if (($channelInfo["flags"] & 8) == 8) { $channelstatus .= 'S'; }
     177      if (($channelInfo["flags"] & 16) == 16) { $channelstatus .= 'D'; }
     178      $channelInfo["displayname"] = $channelInfo["channelname"] . " (" . $channelstatus . ")";
     179    }
     180  }
     181 
     182  function _getChannelList($socket) {
     183    // Request, read and parse the channel list
     184    fputs($socket, "cl\n");
     185    $buffer = $this->_stripEOL(fgets($socket, 4096));
     186    $result = array();
     187    if (strtoupper(substr($buffer, 0, 5)) == "ERROR") { return $result; }
     188    do {
     189      $buffer = $this->_stripEOL(fgets($socket, 4096));
     190      if (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR")) {
     191        $channelid = $this->_stripPartFromString($buffer);
     192        $result[$channelid] = array(
     193          "channelid" => $channelid,
     194          "codec" => $this->_stripPartFromString($buffer),
     195          "parent" => $this->_stripPartFromString($buffer),
     196          "order" => $this->_stripPartFromString($buffer),
     197          "maxplayers" => $this->_stripPartFromString($buffer),
     198          "channelname" => $this->_stripQuotes($this->_stripPartFromString($buffer)),
     199          "flags" => $this->_stripPartFromString($buffer),
     200          "password" => $this->_stripPartFromString($buffer),
     201          "topic" => $this->_stripQuotes($this->_stripPartFromString($buffer))
     202        );
     203        $this->_setChannelDisplayName($result[$channelid]);
     204      }
     205    } while (($buffer != "OK") && (strtoupper(substr($buffer, 0, 5)) != "ERROR") && (!feof($socket)));
     206    return $result;
     207  }
     208 
     209  function _getLimitedChannelList($socket, $limitChannel) {
     210    $channelList = $this->_getChannelList($socket);
     211    $result = array();
     212    foreach($channelList as $channelInfo) {
     213      if ($channelInfo["parent"] == -1) {
     214        if ($channelInfo["channelname"] == $limitChannel) {
     215          $result[$channelInfo["channelid"]] = $channelInfo;
     216          foreach($channelList as $subChannelInfo) {
     217            if ($subChannelInfo["parent"] == $channelInfo["channelid"]) {
     218              $result[$subChannelInfo["channelid"]] = $subChannelInfo;
     219            }
     220          }
     221        }
     222      }
     223    }
     224    return $result;
     225  }
     226 
     227  function _selectServer($socket, $port) {
     228    // Request the server to select the server which is hosted on  the port set in serverUDPPort
     229    fputs($socket, "sel ".$port . "\n");
     230   
     231    // Read server response on request to select a server
     232    return ($this->_stripEOL(fgets($socket, 4096)) == "OK");
     233  }
     234 
     235  // Queries the Teamspeak server
     236  function queryTeamspeakServerEx($settings) {
     237    $result = array();
     238   
     239    // Try to establish a connection to the teamspeak server
     240    if (! $this->_openConnection($socket, $settings["serveraddress"], $settings["serverqueryport"], 0.3)) {
     241      $result["queryerror"] = 1;
     242    } else if (! $this->_selectServer($socket, $settings["serverudpport"])) {
     243      $result["queryerror"] = 2;
     244      $this->_closeConnection($socket);
     245    } else {
     246      $result["queryerror"] = 0;
     247      $result["serverinfo"] = $this->_getServerInfo($socket);
     248      $result["channellist"] = ($settings["limitchannel"] == "") ? $this->_getChannelList($socket) : $this->_getLimitedChannelList($socket, $settings["limitchannel"]);
     249      $result["playerlist"] = ($settings["limitchannel"] == "") ? $this->_getPlayerList($socket) : $this->_getLimitedPlayerList($socket, $result["channellist"]);
     250      $this->_closeConnection($socket);
     251    }
     252    return $result;
     253  }
     254 
     255  function queryTeamspeakServer($serverAddress, $serverUDPPort, $serverQueryPort) {
     256    $settings = $this->getDefaultSettings();
     257    $settings["serveraddress"] = $serverAddress;
     258    $settings["serverudpport"] = $serverUDPPort;
     259    $settings["serverqueryport"] = $serverQueryPort;
     260    return $this->queryTeamspeakServerEx($settings);
     261  }
     262 
     263  function _orderAlphaGetString($string) {
     264    $lowerstring = strtolower($string);
     265    $result = "";
     266    for ($i = 0; $i < strlen($lowerstring); $i++) {
     267      if (strpos("0123456789abcdefghijklmnopqrstuvwxyz", substr($lowerstring, $i, 1)) !== false) {
     268        $result .= substr($lowerstring, $i, 1);
     269      }
     270    }
     271    return $result;
     272  }
     273 
     274  function _orderAlpha($str1, $str2) {
     275    return strcmp($this->_orderAlphaGetString($str1), $this->_orderAlphaGetString($str2));
     276  }
     277 
     278  function _compareChannel($a, $b) {
     279    if ($a["order"] != $b["order"]) { return ($a["order"] < $b["order"]) ? -1 : 1; }
     280    else { return $this->_orderAlpha($a["displayname"], $b["displayname"]); }
     281  }
     282 
     283  function _comparePlayer($a, $b) {
     284    // Determine userlevel (0 = Not server admin, 1 = Server admin)
     285    $userlevela = $a["userstatus"] & 1;
     286    $userlevelb = $b["userstatus"] & 1;
     287    if ($userlevela != $userlevelb) { return ($userlevela < $userlevelb) ? 1 : -1; }
     288    else { return $this->_orderAlpha($a["displayname"], $b["displayname"]); }
     289  }
     290 
     291  function sortServerInfo(&$serverInfo) {
     292    usort($serverInfo["channellist"], array($this, "_compareChannel"));
     293    usort($serverInfo["playerlist"], array($this, "_comparePlayer"));
     294  }
     295 
     296  function _formatTime($totaltime) {
     297    $hours = floor($totaltime / 3600);
     298    $minutes = floor(($totaltime % 3600) / 60);
     299    return (($hours < 10) ? "0" : "") . $hours . ":" . (($minutes < 10) ? "0" : "") . $minutes;
     300  }
     301 
     302  // Returns the codec name
     303  function _getCodecName($codec) {
     304    if ($codec == 0) { return "CELP 5.1 Kbit"; }
     305    else if ($codec == 1) { return "CELP 6.3 Kbit"; }
     306    else if ($codec == 2) { return "GSM 14.8 Kbit"; }
     307    else if ($codec == 3) { return "GSM 16.4 Kbit"; }
     308    else if ($codec == 4) { return "CELP Windows 5.2 Kbit"; }
     309    else if ($codec == 5) { return "Speex 3.4 Kbit"; }
     310    else if ($codec == 6) { return "Speex 5.2 Kbit"; }
     311    else if ($codec == 7) { return "Speex 7.2 Kbit"; }
     312    else if ($codec == 8) { return "Speex 9.3 Kbit"; }
     313    else if ($codec == 9) { return "Speex 12.3 Kbit"; }
     314    else if ($codec == 10) { return "Speex 16.3 Kbit"; }
     315    else if ($codec == 11) { return "Speex 19.5 Kbit"; }
     316    else if ($codec == 12) { return "Speex 25.9 Kbit"; }
     317    else { return "Unknown (" . $codec . ")"; }
     318  }
     319 
     320  function getDefaultSettings() {
     321    $result = array();
     322    $result["serveraddress"] = "";
     323    $result["serverudpport"] = 8767;
     324    $result["serverqueryport"] = 51234;
     325    $result["limitchannel"] = "";
     326    $result["forbiddennicknamechars"] = "()[]{}";
     327    return $result;
     328  }
     329 
     330  // Main function (queries, sorts and displays the teamspeak serverinfo). Its code is not
     331  // very readable... well what shall I say about it... it was hard to write so it should
     332  // be hard to read >:)
     333  function displayTeamspeakEx($settings) {
     334    $serverInfo = $this->queryTeamspeakServerEx($settings);
     335   
     336    echo("<div id=\"teamspeakdisplay\">\n");
     337    if ($serverInfo["queryerror"] != 0) {
     338      $popupInfo = "Server address: " . $settings["serveraddress"] . (($settings["serverudpport"] != 8767) ? (":" . $settings["serverudpport"]): "");
     339      if ($serverInfo["queryerror"] == 1) {
     340        $popupInfo .= ", Error: could not connect to query port";
     341      } else {
     342        $popupInfo .= ", Error: no server running on port " . $settings["serverudpport"];
     343      }
     344      echo("<table><tr><td>");
     345      echo("<img src=\"inc/teamspeakdisplay/teamspeak_offline.png\" alt=\"\" title=\"" . $popupInfo . "\">");
     346      echo("</td><td class=\"teamspeakserver\" title=\"" . $popupInfo . "\">");
     347      echo("Server offline");
     348      echo("</td></tr></table>\n");
     349    } else {
     350      $this->sortServerInfo($serverInfo);
     351     
     352      // Generate javascript for teamspeak hyperlinks
     353      $jsTeamspeakId = md5($settings["serveraddress"] . ":" . $settings["serverudpport"]);
     354      echo("<script type=\"text/javascript\"><!--\n");
     355      echo("function stringOk_" . $jsTeamspeakId . "(string, forbiddenChars) {\n");
     356      echo("  for(var i = 0; i < string.length; i++) {\n");
     357      echo("    if (forbiddenChars.indexOf(string.charAt(i)) > -1) {\n");
     358      echo("      return false;\n");
     359      echo("    }\n");
     360      echo("  }\n");
     361      echo("  return true;\n");
     362      echo("}\n");
     363      echo("function enterServer_" . $jsTeamspeakId . "() {\n");
     364      echo("  enterSubChannel_" . $jsTeamspeakId . "(null, false, null);\n");
     365      echo("}\n");
     366      echo("function enterChannel_" . $jsTeamspeakId . "(channelName, channelPassworded) {\n");
     367      echo("  enterSubChannel_" . $jsTeamspeakId . "(channelName, channelPassworded, null);\n");
     368      echo("}\n");
     369      echo("function enterSubChannel_" . $jsTeamspeakId . "(channelName, channelPassworded, subChannelName) {\n");
     370      echo("  var serveraddress = 'teamspeak://" . $settings["serveraddress"] . ":" . $settings["serverudpport"] . "';\n");
     371      echo("  var nickname=window.prompt('Enter your nickname', '');\n");
     372      echo("  if (nickname == null) {\n");
     373      echo("    return;\n");
     374      echo("  } else if (! stringOk_" . $jsTeamspeakId . "(nickname, '" . str_replace("'", "\\'", $settings["forbiddennicknamechars"]) . "')) {\n");
     375      echo("    window.alert('Could not enter the teamspeak server because the nickname you entered contains one or more of these forbidden characters: " . str_replace("'", "\\'", $settings["forbiddennicknamechars"]) . "');\n");
     376      echo("    return;\n");
     377      echo("  } else if (nickname == \"\") {\n");
     378      echo("    window.alert('Could not enter the teamspeak server because you did not enter your nickname');\n");
     379      echo("    return;\n");
     380      echo("  }\n");
     381      echo("  serveraddress = serveraddress + \"/nickname=\" + escape(nickname);\n");
     382      if ($serverInfo["serverinfo"]["server_password"] == "1") {
     383        echo("  var password=window.prompt('Enter the teamspeak server password for " . $serverInfo["serverinfo"]["server_name"] . "', '');\n");
     384        echo("  if (password == null) {\n");
     385        echo("    return;\n");
     386        echo("  } else if (password == \"\") {\n");
     387        echo("    window.alert('Could not enter the teamspeak server because you did not enter a server password');\n");
     388        echo("    return;\n");
     389        echo("  }\n");
     390        echo("  serveraddress = serveraddress + \"?password=\" + escape(password);\n");
     391      }
     392      echo("  if (channelName != null) { serveraddress = serveraddress + \"?channel=\" + escape(channelName); }\n");
     393      echo("  if (channelPassworded) {\n");
     394      echo("    var channelpassword=window.prompt('Enter the channel password for channel ' + channelName, '');\n");
     395      echo("    if (channelpassword == null) {\n");
     396      echo("      return;\n");
     397      echo("    } else if (channelpassword == \"\") {\n");
     398      echo("      window.alert('Could not enter the teamspeak server because you did not enter a channel password');\n");
     399      echo("      return;\n");
     400      echo("    }\n");
     401      echo("    serveraddress = serveraddress + \"?channelpassword=\" + escape(channelpassword);\n");
     402      echo("  }\n");
     403      echo("  if (subChannelName != null) { serveraddress = serveraddress + \"?subchannel=\" + escape(subChannelName); }\n");
     404      echo("  window.location=serveraddress;\n");
     405      echo("}\n");
     406      echo("//--></script>\n");
     407     
     408      $popupInfo = "Server address: " . $settings["serveraddress"] . (($settings["serverudpport"] != 8767) ? (":" . $settings["serverudpport"]): "") . ", Max players: " . $serverInfo["serverinfo"]["server_maxusers"] . ", Uptime: " . $this->_formatTime($serverInfo["serverinfo"]["server_uptime"]);
     409     
     410      // Print the topmost element of the teamspeak tree
     411      echo("<table><tr><td>");
     412      echo("<img src=\"inc/teamspeakdisplay/teamspeak_online.png\" alt=\"\" title=\"" . $popupInfo . "\">");
     413      echo("</td><td class=\"teamspeakserver\" title=\"" . $popupInfo . "\">");
     414      echo("<a class=\"teamspeakserver\" href=\"javascript:enterServer_" . $jsTeamspeakId . "();\">");
     415      echo(str_replace(" ", "&nbsp;", $this -> Kodovani($serverInfo["serverinfo"]["server_name"])));
     416      echo("</a>");
     417      echo("</td></tr></table>\n");
     418     
     419      // Count the number of channels to be listed:
     420      $currentchannels = 0;
     421      foreach($serverInfo["channellist"] as $channelInfo) {
     422        if ($channelInfo["parent"] == -1) {
     423          $currentchannels++;
     424        }
     425      }
     426     
     427      // Initialize the channelcounter to zero
     428      $counter = 0;
     429     
     430      // Loop through all channels:
     431      foreach($serverInfo["channellist"] as $channelInfo) { if ($channelInfo["parent"] == -1) {
     432       
     433        // determine number of players in channel
     434        $currentplayers = 0;
     435        foreach($serverInfo["playerlist"] as $playerInfo) {
     436          if($playerInfo["channelid"] == $channelInfo["channelid"]) $currentplayers++;
     437        }
     438       
     439        // Count the number of channels to be listed:
     440        $currentplayersandsubchannels = $currentplayers;
     441        foreach($serverInfo["channellist"] as $subchannelInfo) {
     442          if ($subchannelInfo["parent"] == $channelInfo["channelid"]) {
     443            $currentplayersandsubchannels++;
     444          }
     445        }
     446       
     447        $popupInfo = "Max players: " . $channelInfo["maxplayers"] . ", Codec: " . $this->_getCodecName($channelInfo["codec"]);
     448        if ($channelInfo["topic"] != "") { $popupInfo = $popupInfo . ", Topic: " . $channelInfo["topic"]; }
     449       
     450        // Display channel:
     451        echo("<table><tr><td>");
     452        echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "3" : "2") . ".png\" alt=\"\">");
     453        echo("<img src=\"inc/teamspeakdisplay/channel.png\" alt=\"\" title=\"" . $popupInfo . "\">");
     454        echo("</td><td class=\"teamspeakchannel\" title=\"" . $popupInfo . "\">");
     455        echo("<a class=\"teamspeakchannel\" href=\"javascript:enterChannel_" . $jsTeamspeakId . "('" . str_replace("'", "\'", $channelInfo["channelname"]) . "', " . (($channelInfo["password"]) == "1" ? "true" : "false") . ");\">");
     456        echo(str_replace(" ", "&nbsp;", $this -> Kodovani($channelInfo["displayname"])));
     457        echo("</a>");
     458        echo("</td></tr></table>\n");
     459       
     460        // Initialize the playercounter for this channel to zero
     461        $counter_playerandsubchannels = 0;
     462       
     463        // Loop through all players in the current channel:
     464        foreach($serverInfo["playerlist"] as $playerInfo) {
     465         
     466          // Is the current player in the current channel?
     467          if ($playerInfo["channelid"] == $channelInfo["channelid"]) {
     468           
     469            $popupInfo = "Time online: " . $this->_formatTime($playerInfo["totaltime"]) . ", Time idle: " . $this->_formatTime($playerInfo["idletime"]) . ", Ping: " . $playerInfo["pingtime"] . "ms";
     470           
     471            // Display player:
     472            echo("<table><tr><td>");
     473            echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
     474            echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "3" : "2") . ".png\" alt=\"\">");
     475            echo("<img src=\"inc/teamspeakdisplay/player_" . $playerInfo["displayimage"] . ".png\" alt=\"" . $playerInfo["displayimage"] . "\" title=\"" . $popupInfo . "\">");
     476            echo("</td><td class=\"teamspeakplayer\" title=\"" . $popupInfo . "\">");
     477            echo(str_replace(" ", "&nbsp;", $this -> Kodovani($playerInfo["displayname"])));
     478            echo("</td></tr></table>\n");
     479           
     480            // Increase the player counter:
     481            $counter_playerandsubchannels++;
     482          }
     483        }
     484       
     485        // Loop through all channels:
     486        foreach($serverInfo["channellist"] as $subchannelInfo) { if ($subchannelInfo["parent"] == $channelInfo["channelid"]) {
     487          // determine number of players in channel
     488          $currentplayers = 0;
     489          foreach($serverInfo["playerlist"] as $playerInfo) {
     490            if($playerInfo["channelid"] == $subchannelInfo["channelid"]) $currentplayers++;
     491          }
     492         
     493          $popupInfo = "Max players: " . $subchannelInfo["maxplayers"] . ", Codec: " . $this->_getCodecName($subchannelInfo["codec"]);
     494          if ($subchannelInfo["topic"] != "") { $popupInfo = $popupInfo . ", Topic: " . $subchannelInfo["topic"]; }
     495         
     496          // Display channel:
     497          echo("<table><tr><td>");
     498          echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
     499          echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "3" : "2") . ".png\" alt=\"\">");
     500          echo("<img src=\"inc/teamspeakdisplay/channel.png\" alt=\"\" title=\"" . $popupInfo . "\">");
     501          echo("</td><td class=\"teamspeaksubchannel\" title=\"" . $popupInfo . "\">");
     502          echo("<a class=\"teamspeaksubchannel\" href=\"javascript:enterSubChannel_" . $jsTeamspeakId . "('" . str_replace("'", "\'", $channelInfo["channelname"]) . "', " . (($channelInfo["password"]) == "1" ? "true" : "false") . ", '" . str_replace("'", "\'", $subchannelInfo["channelname"]) . "');\">");
     503          echo(str_replace(" ", "&nbsp;", $this -> Kodovani($subchannelInfo["displayname"])));
     504          echo("</a>");
     505          echo("</td></tr></table>\n");
     506         
     507          // Initialize the playercounter for this channel to zero
     508          $counter_player = 0;
     509         
     510          // Loop through all players in the current channel:
     511          foreach($serverInfo["playerlist"] as $playerInfo) {
     512           
     513            // Is the current player in the current channel?
     514            if ($playerInfo["channelid"] == $subchannelInfo["channelid"]) {
     515             
     516              $popupInfo = "Time online: " . $this->_formatTime($playerInfo["totaltime"]) . ", Time idle: " . $this->_formatTime($playerInfo["idletime"]) . ", Ping: " . $playerInfo["pingtime"] . "ms";
     517             
     518              // Display player:
     519              echo("<table><tr><td>");
     520              echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter + 1) == $currentchannels) ? "4" : "1") . ".png\" alt=\"\">");
     521              echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_playerandsubchannels + 1) == $currentplayersandsubchannels) ? "4" : "1") . ".png\" alt=\"\">");
     522              echo("<img src=\"inc/teamspeakdisplay/treeimage" . ((($counter_player + 1) == $currentplayers) ? "3" : "2") . ".png\" alt=\"\">");
     523              echo("<img src=\"inc/teamspeakdisplay/player_" . $playerInfo["displayimage"] . ".png\" alt=\"" . $playerInfo["displayimage"] . "\" title=\"" . $popupInfo . "\">");
     524              echo("</td><td class=\"teamspeakplayer\" title=\"" . $popupInfo . "\">");
     525              echo(str_replace(" ", "&nbsp;", $this -> Kodovani($playerInfo["displayname"])));
     526              echo("</td></tr></table>\n");
     527             
     528              // Increase the player counter:
     529              $counter_player++;
     530            }
     531          }
     532         
     533          // Increase the channelcounter
     534          $counter_playerandsubchannels++;
     535        } }
     536       
     537        // Increase the channelcounter
     538        $counter++;
     539      } }
     540    }
     541    echo("</div>\n");
     542  }
     543 
     544  function displayTeamspeak($serverAddress, $serverUDPPort=8767, $serverQueryPort=51234) {
     545    $settings = $this->getDefaultSettings();
     546    $settings["serveraddress"] = $serverAddress;
     547    $settings["serverudpport"] = $serverUDPPort;
     548    $settings["serverqueryport"] = $serverQueryPort;
     549    $this->displayTeamspeakEx($settings);
     550  }
     551 
    554552}
    555553
Note: See TracChangeset for help on using the changeset viewer.