Cfxplayers.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\Exception\Protocol as Exception;
  20. use GameQ\Protocols\Http;
  21. /**
  22. * GTA Five M Protocol Class
  23. *
  24. * Server base can be found at https://fivem.net/
  25. *
  26. * Based on code found at https://github.com/LiquidObsidian/fivereborn-query
  27. *
  28. * @author Austin Bischoff <[email protected]>
  29. *
  30. * Adding FiveM Player List by
  31. * @author Jesse Lukas <[email protected]>
  32. */
  33. class CFXPlayers extends Http
  34. {
  35. /**
  36. * Holds the real ip so we can overwrite it back
  37. *
  38. * @var string
  39. */
  40. protected $realIp = null;
  41. /**
  42. * Holds the real port so we can overwrite it back
  43. *
  44. * @var int
  45. */
  46. protected $realPortQuery = null;
  47. /**
  48. * Packets to send
  49. *
  50. * @var array
  51. */
  52. protected $packets = [
  53. self::PACKET_STATUS => "GET /players.json HTTP/1.0\r\nAccept: */*\r\n\r\n", // Player List
  54. ];
  55. /**
  56. * The protocol being used
  57. *
  58. * @var string
  59. */
  60. protected $protocol = 'cfxplayers';
  61. /**
  62. * String name of this protocol class
  63. *
  64. * @var string
  65. */
  66. protected $name = 'cfxplayers';
  67. /**
  68. * Longer string name of this protocol class
  69. *
  70. * @var string
  71. */
  72. protected $name_long = "cfxplayers";
  73. /**
  74. * Process the response
  75. *
  76. * @return array
  77. * @throws Exception
  78. */
  79. public function processResponse()
  80. {
  81. // Make sure we have any players
  82. if (empty($this->packets_response)) {
  83. return [];
  84. }
  85. // Implode and rip out the JSON
  86. preg_match('/\{(.*)\}/ms', implode('', $this->packets_response), $matches);
  87. // Return should be JSON, let's validate
  88. if (!isset($matches[0]) || ($json = json_decode($matches[0], true)) === null) {
  89. throw new Exception(__METHOD__ . " JSON response from Stationeers protocol is invalid.");
  90. }
  91. // Return json as it should already be well formed
  92. return [
  93. 'players' => $json,
  94. ];
  95. }
  96. }