Teamspeak3.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * This file is part of GameQ.
  4. *
  5. * GameQ is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU Lesser General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GameQ is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. namespace GameQ\Protocols;
  19. use GameQ\Protocol;
  20. use GameQ\Buffer;
  21. use GameQ\Result;
  22. use GameQ\Server;
  23. use GameQ\Exception\Protocol as Exception;
  24. /**
  25. * Teamspeak 3 Protocol Class
  26. *
  27. * All values are utf8 encoded upon processing
  28. *
  29. * This code ported from GameQ v1/v2. Credit to original author(s) as I just updated it to
  30. * work within this new system.
  31. *
  32. * @author Austin Bischoff <[email protected]>
  33. */
  34. class Teamspeak3 extends Protocol
  35. {
  36. /**
  37. * Array of packets we want to look up.
  38. * Each key should correspond to a defined method in this or a parent class
  39. *
  40. * @type array
  41. */
  42. protected $packets = [
  43. self::PACKET_DETAILS => "use port=%d\x0Aserverinfo\x0A",
  44. self::PACKET_PLAYERS => "use port=%d\x0Aclientlist\x0A",
  45. self::PACKET_CHANNELS => "use port=%d\x0Achannellist -topic\x0A",
  46. ];
  47. /**
  48. * The transport mode for this protocol is TCP
  49. *
  50. * @type string
  51. */
  52. protected $transport = self::TRANSPORT_TCP;
  53. /**
  54. * The query protocol used to make the call
  55. *
  56. * @type string
  57. */
  58. protected $protocol = 'teamspeak3';
  59. /**
  60. * String name of this protocol class
  61. *
  62. * @type string
  63. */
  64. protected $name = 'teamspeak3';
  65. /**
  66. * Longer string name of this protocol class
  67. *
  68. * @type string
  69. */
  70. protected $name_long = "Teamspeak 3";
  71. /**
  72. * The client join link
  73. *
  74. * @type string
  75. */
  76. protected $join_link = "ts3server://%s?port=%d";
  77. /**
  78. * Normalize settings for this protocol
  79. *
  80. * @type array
  81. */
  82. protected $normalize = [
  83. // General
  84. 'general' => [
  85. 'dedicated' => 'dedicated',
  86. 'hostname' => 'virtualserver_name',
  87. 'password' => 'virtualserver_flag_password',
  88. 'numplayers' => 'numplayers',
  89. 'maxplayers' => 'virtualserver_maxclients',
  90. ],
  91. // Player
  92. 'player' => [
  93. 'id' => 'clid',
  94. 'team' => 'cid',
  95. 'name' => 'client_nickname',
  96. ],
  97. // Team
  98. 'team' => [
  99. 'id' => 'cid',
  100. 'name' => 'channel_name',
  101. ],
  102. ];
  103. /**
  104. * Before we send off the queries we need to update the packets
  105. *
  106. * @param \GameQ\Server $server
  107. *
  108. * @throws \GameQ\Exception\Protocol
  109. */
  110. public function beforeSend(Server $server)
  111. {
  112. // Check to make sure we have a query_port because it is required
  113. if (!isset($this->options[Server::SERVER_OPTIONS_QUERY_PORT])
  114. || empty($this->options[Server::SERVER_OPTIONS_QUERY_PORT])
  115. ) {
  116. throw new Exception(__METHOD__ . " Missing required setting '" . Server::SERVER_OPTIONS_QUERY_PORT . "'.");
  117. }
  118. // Let's loop the packets and set the proper pieces
  119. foreach ($this->packets as $packet_type => $packet) {
  120. // Update with the client port for the server
  121. $this->packets[$packet_type] = sprintf($packet, $server->portClient());
  122. }
  123. }
  124. /**
  125. * Process the response
  126. *
  127. * @return array
  128. * @throws \GameQ\Exception\Protocol
  129. */
  130. public function processResponse()
  131. {
  132. // Make a new buffer out of all of the packets
  133. $buffer = new Buffer(implode('', $this->packets_response));
  134. // Check the header TS3
  135. if (($header = trim($buffer->readString("\n"))) !== 'TS3') {
  136. throw new Exception(__METHOD__ . " Expected header '{$header}' does not match expected 'TS3'.");
  137. }
  138. // Convert all the escaped characters
  139. $raw = str_replace(
  140. [
  141. '\\\\', // Translate escaped \
  142. '\\/', // Translate escaped /
  143. ],
  144. [
  145. '\\',
  146. '/',
  147. ],
  148. $buffer->getBuffer()
  149. );
  150. // Explode the sections and filter to remove empty, junk ones
  151. $sections = array_filter(explode("\n", $raw), function ($value) {
  152. $value = trim($value);
  153. // Not empty string or a message response for "error id=\d"
  154. return !empty($value) && substr($value, 0, 5) !== 'error';
  155. });
  156. // Trim up the values to remove extra whitespace
  157. $sections = array_map('trim', $sections);
  158. // Set the result to a new result instance
  159. $result = new Result();
  160. // Iterate over the sections and offload the parsing
  161. foreach ($sections as $section) {
  162. // Grab a snip of the data so we can figure out what it is
  163. $check = substr(trim($section), 0, 4);
  164. // Use the first part of the response to figure out where we need to go
  165. if ($check == 'virt') {
  166. // Server info
  167. $this->processDetails($section, $result);
  168. } elseif ($check == 'cid=') {
  169. // Channels
  170. $this->processChannels($section, $result);
  171. } elseif ($check == 'clid') {
  172. // Clients (players)
  173. $this->processPlayers($section, $result);
  174. }
  175. }
  176. unset($buffer, $sections, $section, $check);
  177. return $result->fetch();
  178. }
  179. /*
  180. * Internal methods
  181. */
  182. /**
  183. * Process the properties of the data.
  184. *
  185. * Takes data in "key1=value1 key2=value2 ..." and processes it into a usable format
  186. *
  187. * @param $data
  188. *
  189. * @return array
  190. */
  191. protected function processProperties($data)
  192. {
  193. // Will hold the properties we are sending back
  194. $properties = [];
  195. // All of these are split on space
  196. $items = explode(' ', $data);
  197. // Iterate over the items
  198. foreach ($items as $item) {
  199. // Explode and make sure we always have 2 items in the array
  200. list($key, $value) = array_pad(explode('=', $item, 2), 2, '');
  201. // Convert spaces and other character changes
  202. $properties[$key] = utf8_encode(str_replace(
  203. [
  204. '\\s', // Translate spaces
  205. ],
  206. [
  207. ' ',
  208. ],
  209. $value
  210. ));
  211. }
  212. return $properties;
  213. }
  214. /**
  215. * Handles processing the details data into a usable format
  216. *
  217. * @param string $data
  218. * @param \GameQ\Result $result
  219. */
  220. protected function processDetails($data, Result &$result)
  221. {
  222. // Offload the parsing for these values
  223. $properties = $this->processProperties($data);
  224. // Always dedicated
  225. $result->add('dedicated', 1);
  226. // Iterate over the properties
  227. foreach ($properties as $key => $value) {
  228. $result->add($key, $value);
  229. }
  230. // We need to manually figure out the number of players
  231. $result->add(
  232. 'numplayers',
  233. ($properties['virtualserver_clientsonline'] - $properties['virtualserver_queryclientsonline'])
  234. );
  235. unset($data, $properties, $key, $value);
  236. }
  237. /**
  238. * Process the channel listing
  239. *
  240. * @param string $data
  241. * @param \GameQ\Result $result
  242. */
  243. protected function processChannels($data, Result &$result)
  244. {
  245. // We need to split the data at the pipe
  246. $channels = explode('|', $data);
  247. // Iterate over the channels
  248. foreach ($channels as $channel) {
  249. // Offload the parsing for these values
  250. $properties = $this->processProperties($channel);
  251. // Iterate over the properties
  252. foreach ($properties as $key => $value) {
  253. $result->addTeam($key, $value);
  254. }
  255. }
  256. unset($data, $channel, $channels, $properties, $key, $value);
  257. }
  258. /**
  259. * Process the user listing
  260. *
  261. * @param string $data
  262. * @param \GameQ\Result $result
  263. */
  264. protected function processPlayers($data, Result &$result)
  265. {
  266. // We need to split the data at the pipe
  267. $players = explode('|', $data);
  268. // Iterate over the channels
  269. foreach ($players as $player) {
  270. // Offload the parsing for these values
  271. $properties = $this->processProperties($player);
  272. // Iterate over the properties
  273. foreach ($properties as $key => $value) {
  274. $result->addPlayer($key, $value);
  275. }
  276. }
  277. unset($data, $player, $players, $properties, $key, $value);
  278. }
  279. }