[
  {
    "path": ".gitignore",
    "content": "composer.phar\n/vendor\n"
  },
  {
    "path": "LICENSE",
    "content": "The MIT License (MIT)\n\nCopyright (c) 2013 Chris Churchwell\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of\nthis software and associated documentation files (the \"Software\"), to deal in\nthe Software without restriction, including without limitation the rights to\nuse, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of\nthe Software, and to permit persons to whom the Software is furnished to do so,\nsubject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all\ncopies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\nIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS\nFOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR\nCOPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER\nIN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\nCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n"
  },
  {
    "path": "README.md",
    "content": "PHP-Minecraft-Rcon\n==================\n[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/thedudeguy/PHP-Minecraft-Rcon/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/thedudeguy/PHP-Minecraft-Rcon/?branch=master)\n\nSimple Rcon class for php.\n\n## Installation\n### Using Composer\nThis Rcon library may be installed by issuing the following command:\n```bash\n$ composer require thedudeguy/rcon\n```\n### Not Using Composer\nIf not using Composer, just place the Rcon.php file in your project and include it in your PHP script:\n```php\nrequire_once('Rcon.php');\n```\n\n## Example\nFor this script to work, rcon must be enabled on the server, by setting `enable-rcon=true` in the server's `server.properties` file. A password must also be set, and provided in the script.\n\n```php\n$host = 'some.minecraftserver.com'; // Server host name or IP\n$port = 25575;                      // Port rcon is listening on\n$password = 'server-rcon-password'; // rcon.password setting set in server.properties\n$timeout = 3;                       // How long to timeout.\n\nuse Thedudeguy\\Rcon;\n\n$rcon = new Rcon($host, $port, $password, $timeout);\n\nif ($rcon->connect())\n{\n  $rcon->sendCommand(\"say Hello World!\");\n}\n```\n"
  },
  {
    "path": "composer.json",
    "content": "{\n\t\"name\": \"thedudeguy/rcon\",\n\t\"description\": \"Simple Rcon class for php.\",\n\t\"keywords\": [\"rcon\", \"minecraft\"],\n\t\"homepage\": \"https://github.com/thedudeguy/PHP-Minecraft-Rcon\",\n\t\"license\": \"MIT\",\n\t\"authors\": [\n\t\t{\n\t\t\t\"name\": \"Chris Churchwell\",\n\t\t\t\"homepage\": \"http://chrischurchwell.com\",\n\t\t\t\"email\": \"chris@chrischurchwell.com\"\n\t\t}\n\t],\n\t\"support\": {\n\t\t\"issues\": \"https://github.com/thedudeguy/PHP-Minecraft-Rcon/issues\",\n\t\t\"source\": \"https://github.com/thedudeguy/PHP-Minecraft-Rcon/tree/master\"\n\t},\n\t\"autoload\": {\n\t\t\"psr-4\": { \n\t\t\t\"Thedudeguy\\\\\": \"src/\"\n\t\t}\n    } \n}\n"
  },
  {
    "path": "src/Rcon.php",
    "content": "<?php\n/**\n * See https://developer.valvesoftware.com/wiki/Source_RCON_Protocol for\n * more information about Source RCON Packets\n *\n * PHP Version 7\n *\n * @copyright 2013-2017 Chris Churchwell\n * @author thedudeguy\n * @link https://github.com/thedudeguy/PHP-Minecraft-Rcon\n */\n\nnamespace Thedudeguy;\n\nclass Rcon\n{\n    private $host;\n    private $port;\n    private $password;\n    private $timeout;\n\n    private $socket;\n\n    private $authorized = false;\n    private $lastResponse = '';\n\n    const PACKET_AUTHORIZE = 5;\n    const PACKET_COMMAND = 6;\n\n    const SERVERDATA_AUTH = 3;\n    const SERVERDATA_AUTH_RESPONSE = 2;\n    const SERVERDATA_EXECCOMMAND = 2;\n    const SERVERDATA_RESPONSE_VALUE = 0;\n\n    /**\n     * Create a new instance of the Rcon class.\n     *\n     * @param string $host\n     * @param integer $port\n     * @param string $password\n     * @param integer $timeout\n     */\n    public function __construct($host, $port, $password, $timeout)\n    {\n        $this->host = $host;\n        $this->port = $port;\n        $this->password = $password;\n        $this->timeout = $timeout;\n    }\n\n    /**\n     * Get the latest response from the server.\n     *\n     * @return string\n     */\n    public function getResponse()\n    {\n        return $this->lastResponse;\n    }\n\n    /**\n     * Connect to a server.\n     *\n     * @return boolean\n     */\n    public function connect()\n    {\n        $this->socket = fsockopen($this->host, $this->port, $errno, $errstr, $this->timeout);\n\n        if (!$this->socket) {\n            $this->lastResponse = $errstr;\n            return false;\n        }\n\n        //set timeout\n        stream_set_timeout($this->socket, 3, 0);\n\n        // check authorization\n        return $this->authorize();\n    }\n\n    /**\n     * Disconnect from server.\n     *\n     * @return void\n     */\n    public function disconnect()\n    {\n        if ($this->socket) {\n                    fclose($this->socket);\n        }\n    }\n\n    /**\n     * True if socket is connected and authorized.\n     *\n     * @return boolean\n     */\n    public function isConnected()\n    {\n        return $this->authorized;\n    }\n\n    /**\n     * Send a command to the connected server.\n     *\n     * @param string $command\n     *\n     * @return boolean|mixed\n     */\n    public function sendCommand($command)\n    {\n        if (!$this->isConnected()) {\n                    return false;\n        }\n\n        // send command packet\n        $this->writePacket(self::PACKET_COMMAND, self::SERVERDATA_EXECCOMMAND, $command);\n\n        // get response\n        $response_packet = $this->readPacket();\n        if ($response_packet['id'] == self::PACKET_COMMAND) {\n            if ($response_packet['type'] == self::SERVERDATA_RESPONSE_VALUE) {\n                $this->lastResponse = $response_packet['body'];\n\n                return $response_packet['body'];\n            }\n        }\n\n        return false;\n    }\n\n    /**\n     * Log into the server with the given credentials.\n     *\n     * @return boolean\n     */\n    private function authorize()\n    {\n        $this->writePacket(self::PACKET_AUTHORIZE, self::SERVERDATA_AUTH, $this->password);\n        $response_packet = $this->readPacket();\n\n        if ($response_packet['type'] == self::SERVERDATA_AUTH_RESPONSE) {\n            if ($response_packet['id'] == self::PACKET_AUTHORIZE) {\n                $this->authorized = true;\n\n                return true;\n            }\n        }\n\n        $this->disconnect();\n        return false;\n    }\n\n    /**\n     * Writes a packet to the socket stream.\n     *\n     * @param $packetId\n     * @param $packetType\n     * @param string $packetBody\n     *\n     * @return void\n     */\n    private function writePacket($packetId, $packetType, $packetBody)\n    {\n        /*\n\t\tSize\t\t\t32-bit little-endian Signed Integer\t \tVaries, see below.\n\t\tID\t\t\t\t32-bit little-endian Signed Integer\t\tVaries, see below.\n\t\tType\t        32-bit little-endian Signed Integer\t\tVaries, see below.\n\t\tBody\t\t    Null-terminated ASCII String\t\t\tVaries, see below.\n\t\tEmpty String    Null-terminated ASCII String\t\t\t0x00\n\t\t*/\n\n        //create packet\n        $packet = pack('VV', $packetId, $packetType);\n        $packet = $packet.$packetBody.\"\\x00\";\n        $packet = $packet.\"\\x00\";\n\n        // get packet size.\n        $packet_size = strlen($packet);\n\n        // attach size to packet.\n        $packet = pack('V', $packet_size).$packet;\n\n        // write packet.\n        fwrite($this->socket, $packet, strlen($packet));\n    }\n\n    /**\n     * Read a packet from the socket stream.\n     *\n     * @return array\n     */\n    private function readPacket()\n    {\n        //get packet size.\n        $size_data = fread($this->socket, 4);\n        $size_pack = unpack('V1size', $size_data);\n        $size = $size_pack['size'];\n\n        // if size is > 4096, the response will be in multiple packets.\n        // this needs to be address. get more info about multi-packet responses\n        // from the RCON protocol specification at\n        // https://developer.valvesoftware.com/wiki/Source_RCON_Protocol\n        // currently, this script does not support multi-packet responses.\n\n        $packet_data = fread($this->socket, $size);\n        $packet_pack = unpack('V1id/V1type/a*body', $packet_data);\n\n        return $packet_pack;\n    }\n}\n"
  }
]