Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * IDK RCON Class (UDP Client)
- * for sending remote server commands
- *
- * @author Steve Winfield
- **/
- namespace IDK;
- class RconInstance {
- const RECV_WAIT_TIMEOUT_SECONDS = 3;
- private $ip;
- private $port;
- public function __construct($ip, $port) {
- $this->ip = $ip;
- $this->port = $port;
- }
- public function sendData($data) {
- $data = (string) $data;
- if(!($socket = socket_create(AF_INET, SOCK_DGRAM, 0))) {
- $errorcode = socket_last_error();
- $errormsg = socket_strerror($errorcode);
- throw new \Exception("Couldn't create socket: [$errorcode] $errormsg");
- }
- if(!socket_sendto($socket, $data, strlen($data), 0, $this->ip, $this->port)) {
- $errorcode = socket_last_error();
- $errormsg = socket_strerror($errorcode);
- throw new \Exception("Could not send data: [$errorcode] $errormsg");
- }
- socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => self::RECV_WAIT_TIMEOUT_SECONDS, 'usec'=>0));
- socket_recv($socket, $reply, 1024, MSG_WAITALL);
- socket_close($socket);
- return $reply;
- }
- }
- class RconWriter {
- private $data;
- public function __construct($opCode) {
- $this->data = RconEncryption::encodeB64($opCode);
- }
- public function pushUTF($string) {
- $this->data .= RconEncryption::encodeB64(strlen($string)) . $string;
- return $this;
- }
- public function pushBoolean($boolean) {
- $this->data .= $boolean ? "I" : "H";
- }
- public function pushInteger($i) {
- $this->data .= RconEncryption::encodeVL64($i);
- }
- public function __toString() {
- return RconEncryption::encodeB64(strlen($this->data), 3) . $this->data;
- }
- }
- class RconEncryption {
- /**
- * @author BurakDev
- **/
- public static function encodeVL64($i) {
- $wf = array();
- $pos = 0;
- $spos = 0;
- $bytes = 1;
- $negativeMask = $i>=0?0:4;
- $i = abs($i);
- $wf[$pos++] = 64 + ($i & 3);
- for ($i >>= 2; $i != 0; $i >>= 6){
- $bytes++;
- $wf[$pos++] = 64 + ($i & 0x3f);
- }
- $wf[$spos] = $wf[$spos] | $bytes << 3 | $negativeMask;
- $str = "";
- foreach($wf as $tmp) {
- $str .= chr($tmp);
- }
- return str_replace("\0","",$str);
- }
- /**
- * @author BurakDev
- **/
- public static function encodeB64($i, $length = 2) {
- if(is_numeric($i)) {
- try {
- $s = "";
- for($x = 1; $x <= $length; $x++) {
- $s.= chr( (64 + ($i >> 6 * ( $length - $x) & 0x3f)) );
- }
- return $s;
- } catch(Exception $e) {
- return $e->getMessage();
- }
- } else {
- return false;
- }
- }
- }
- class InfobusQuestion extends RconWriter {
- public function __construct($roomId, $question, $selections) {
- parent::__construct(2);
- parent::pushInteger($roomId);
- parent::pushUTF($question);
- parent::pushInteger(count($selections));
- foreach ($selections as $selection) {
- parent::pushUTF($selection);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement