| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | |
| 19: | namespace GameQ\Protocols; |
| 20: | |
| 21: | use GameQ\Buffer; |
| 22: | use GameQ\Exception\Protocol as Exception; |
| 23: | use GameQ\Helpers\Str; |
| 24: | use GameQ\Protocol; |
| 25: | use GameQ\Result; |
| 26: | use GameQ\Server; |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | |
| 36: | |
| 37: | |
| 38: | class Teamspeak3 extends Protocol |
| 39: | { |
| 40: | |
| 41: | |
| 42: | |
| 43: | |
| 44: | |
| 45: | |
| 46: | protected $packets = [ |
| 47: | self::PACKET_DETAILS => "use port=%d\x0Aserverinfo\x0A", |
| 48: | self::PACKET_PLAYERS => "use port=%d\x0Aclientlist\x0A", |
| 49: | self::PACKET_CHANNELS => "use port=%d\x0Achannellist -topic\x0A", |
| 50: | ]; |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | |
| 57: | protected $transport = self::TRANSPORT_TCP; |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | |
| 64: | protected $protocol = 'teamspeak3'; |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | protected $name = 'teamspeak3'; |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | protected $name_long = "Teamspeak 3"; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | protected $join_link = "ts3server://%s?port=%d"; |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | protected $normalize = [ |
| 93: | |
| 94: | 'general' => [ |
| 95: | 'dedicated' => 'dedicated', |
| 96: | 'hostname' => 'virtualserver_name', |
| 97: | 'password' => 'virtualserver_flag_password', |
| 98: | 'numplayers' => 'numplayers', |
| 99: | 'maxplayers' => 'virtualserver_maxclients', |
| 100: | ], |
| 101: | |
| 102: | 'player' => [ |
| 103: | 'id' => 'clid', |
| 104: | 'team' => 'cid', |
| 105: | 'name' => 'client_nickname', |
| 106: | ], |
| 107: | |
| 108: | 'team' => [ |
| 109: | 'id' => 'cid', |
| 110: | 'name' => 'channel_name', |
| 111: | ], |
| 112: | ]; |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | public function beforeSend(Server $server) |
| 122: | { |
| 123: | |
| 124: | if (!isset($this->options[Server::SERVER_OPTIONS_QUERY_PORT]) |
| 125: | || empty($this->options[Server::SERVER_OPTIONS_QUERY_PORT]) |
| 126: | ) { |
| 127: | throw new Exception(__METHOD__ . " Missing required setting '" . Server::SERVER_OPTIONS_QUERY_PORT . "'."); |
| 128: | } |
| 129: | |
| 130: | |
| 131: | foreach ($this->packets as $packet_type => $packet) { |
| 132: | |
| 133: | $this->packets[$packet_type] = sprintf($packet, $server->portClient()); |
| 134: | } |
| 135: | } |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | public function processResponse() |
| 144: | { |
| 145: | |
| 146: | $buffer = new Buffer(implode('', $this->packets_response)); |
| 147: | |
| 148: | |
| 149: | if (($header = trim($buffer->readString("\n"))) !== 'TS3') { |
| 150: | throw new Exception(__METHOD__ . " Expected header '{$header}' does not match expected 'TS3'."); |
| 151: | } |
| 152: | |
| 153: | |
| 154: | $raw = str_replace( |
| 155: | [ |
| 156: | '\\\\', |
| 157: | '\\/', |
| 158: | ], |
| 159: | [ |
| 160: | '\\', |
| 161: | '/', |
| 162: | ], |
| 163: | $buffer->getBuffer() |
| 164: | ); |
| 165: | |
| 166: | |
| 167: | $sections = array_filter(explode("\n", $raw), function ($value) { |
| 168: | $value = trim($value); |
| 169: | |
| 170: | |
| 171: | return !empty($value) && substr($value, 0, 5) !== 'error'; |
| 172: | }); |
| 173: | |
| 174: | |
| 175: | $sections = array_map('trim', $sections); |
| 176: | |
| 177: | |
| 178: | $result = new Result(); |
| 179: | |
| 180: | |
| 181: | foreach ($sections as $section) { |
| 182: | |
| 183: | $check = substr(trim($section), 0, 4); |
| 184: | |
| 185: | |
| 186: | if ($check == 'virt') { |
| 187: | |
| 188: | $this->processDetails($section, $result); |
| 189: | } elseif ($check == 'cid=') { |
| 190: | |
| 191: | $this->processChannels($section, $result); |
| 192: | } elseif ($check == 'clid') { |
| 193: | |
| 194: | $this->processPlayers($section, $result); |
| 195: | } |
| 196: | } |
| 197: | |
| 198: | unset($buffer, $sections, $section, $check); |
| 199: | |
| 200: | return $result->fetch(); |
| 201: | } |
| 202: | |
| 203: | |
| 204: | |
| 205: | |
| 206: | |
| 207: | |
| 208: | |
| 209: | |
| 210: | |
| 211: | |
| 212: | |
| 213: | protected function processProperties($data) |
| 214: | { |
| 215: | |
| 216: | $properties = []; |
| 217: | |
| 218: | |
| 219: | $items = explode(' ', $data); |
| 220: | |
| 221: | |
| 222: | foreach ($items as $item) { |
| 223: | |
| 224: | list($key, $value) = array_pad(explode('=', $item, 2), 2, ''); |
| 225: | |
| 226: | |
| 227: | $properties[$key] = Str::isoToUtf8(str_replace( |
| 228: | [ |
| 229: | '\\s', |
| 230: | ], |
| 231: | [ |
| 232: | ' ', |
| 233: | ], |
| 234: | $value |
| 235: | )); |
| 236: | } |
| 237: | |
| 238: | return $properties; |
| 239: | } |
| 240: | |
| 241: | |
| 242: | |
| 243: | |
| 244: | |
| 245: | |
| 246: | |
| 247: | |
| 248: | protected function processDetails($data, Result &$result) |
| 249: | { |
| 250: | |
| 251: | $properties = $this->processProperties($data); |
| 252: | |
| 253: | |
| 254: | $result->add('dedicated', 1); |
| 255: | |
| 256: | |
| 257: | foreach ($properties as $key => $value) { |
| 258: | $result->add($key, $value); |
| 259: | } |
| 260: | |
| 261: | |
| 262: | $result->add( |
| 263: | 'numplayers', |
| 264: | ($properties['virtualserver_clientsonline'] - $properties['virtualserver_queryclientsonline']) |
| 265: | ); |
| 266: | |
| 267: | unset($data, $properties, $key, $value); |
| 268: | } |
| 269: | |
| 270: | |
| 271: | |
| 272: | |
| 273: | |
| 274: | |
| 275: | |
| 276: | |
| 277: | protected function processChannels($data, Result &$result) |
| 278: | { |
| 279: | |
| 280: | $channels = explode('|', $data); |
| 281: | |
| 282: | |
| 283: | foreach ($channels as $channel) { |
| 284: | |
| 285: | $properties = $this->processProperties($channel); |
| 286: | |
| 287: | |
| 288: | foreach ($properties as $key => $value) { |
| 289: | $result->addTeam($key, $value); |
| 290: | } |
| 291: | } |
| 292: | |
| 293: | unset($data, $channel, $channels, $properties, $key, $value); |
| 294: | } |
| 295: | |
| 296: | |
| 297: | |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | protected function processPlayers($data, Result &$result) |
| 304: | { |
| 305: | |
| 306: | $players = explode('|', $data); |
| 307: | |
| 308: | |
| 309: | foreach ($players as $player) { |
| 310: | |
| 311: | $properties = $this->processProperties($player); |
| 312: | |
| 313: | |
| 314: | foreach ($properties as $key => $value) { |
| 315: | $result->addPlayer($key, $value); |
| 316: | } |
| 317: | } |
| 318: | |
| 319: | unset($data, $player, $players, $properties, $key, $value); |
| 320: | } |
| 321: | } |
| 322: | |