Advertisement
Guest User

Untitled

a guest
Aug 31st, 2013
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.79 KB | None | 0 0
  1. <?php
  2.     /*  WebSend Class Pulled from source
  3.      *      http://dev.bukkit.org/bukkit-plugins/websend/
  4.      *      Version 0.5
  5.      */
  6.     class Minecraft{
  7.         var $stream;
  8.         public $host = "127.0.0.1";/* Minecraft server IP or domain, May be internal LAN ip */
  9.         public $password = "password";/* Password in Websend config */
  10.         public $port = 4445;/* Port in Websend config */
  11.         public $timeout = 3600;/* Connect() timeout */
  12.        
  13.         /**
  14.         * Connects to a Websend server.
  15.         */
  16.         public function connect(){
  17.             $this->stream = fsockopen($this->host, $this->port,$errno,$errstr,$this->timeout);
  18.             if($this->stream){
  19.                 $this->writeRawByte(21);
  20.                 $this->writeString($this->password);
  21.                 return true;
  22.             }else{
  23.                 return false;
  24.             }
  25.            
  26.         }
  27.  
  28.         /**
  29.         * Sends a disconnect signal to the currently connected Websend server.
  30.         */
  31.         public function disconnect(){
  32.             $this->writeRawByte(20);
  33.         }
  34.  
  35.         //NETWORK IO
  36.         private function writeRawInt( $i ){
  37.             fwrite( $this->stream, pack( "N", $i ), 4 );
  38.         }
  39.    
  40.         private function writeRawDouble( $d ){
  41.             fwrite( $this->stream, strrev( pack( "d", $d ) ) );
  42.         }
  43.  
  44.         private function writeRawByte( $b ){
  45.             fwrite( $this->stream, strrev( pack( "C", $b ) ) );
  46.         }
  47.  
  48.         private function writeChar( $char ){
  49.             $v = ord($char);
  50.             $this->writeRawByte((0xff & ($v >> 8)));
  51.             $this->writeRawByte((0xff & $v));
  52.         }
  53.  
  54.         private function writeChars( $string ){
  55.             $array = str_split($string);
  56.             foreach($array as &$cur){
  57.                 $v = ord($cur);
  58.                 $this->writeRawByte((0xff & ($v >> 8)));
  59.                 $this->writeRawByte((0xff & $v));
  60.             }
  61.         }
  62.  
  63.         private function writeString( $string ){
  64.             $array = str_split($string);
  65.             $this->writeRawInt(count($array));
  66.             foreach($array as &$cur){
  67.                 $v = ord($cur);
  68.                 $this->writeRawByte((0xff & ($v >> 8)));
  69.                 $this->writeRawByte((0xff & $v));
  70.             }
  71.         }
  72.  
  73.         private function readRawInt(){
  74.             $a = $this->readRawByte();
  75.             $b = $this->readRawByte();
  76.             $c = $this->readRawByte();
  77.             $d = $this->readRawByte();
  78.             $i = ((($a & 0xff) << 24) | (($b & 0xff) << 16) | (($c & 0xff) << 8) | ($d & 0xff));
  79.             return $i;
  80.         }
  81.         private function readRawDouble(){
  82.             $up = unpack( "di", strrev( fread( $this->stream, 8 ) ) );
  83.             $d = $up["i"];
  84.             return $d;
  85.         }
  86.         private function readRawByte(){
  87.             $up = unpack( "Ci", fread( $this->stream, 1 ) );
  88.             $b = $up["i"];
  89.             return $b;
  90.         }
  91.         private function readChar(){
  92.             $byte1 = $this->readRawByte();
  93.             $byte2 = $this->readRawByte();
  94.             $charValue = chr(utf8_decode((($byte1 << 8) | ($byte2 & 0xff))));
  95.             return $charValue;
  96.         }
  97.         private function readChars($len){
  98.             $buf = "";
  99.             for($i = 0;$i<$len;$i++){
  100.                 $byte1 = $this->readRawByte();
  101.                 $byte2 = $this->readRawByte();
  102.                 $buf = $buf.chr(utf8_decode((($byte1 << 8) | ($byte2 & 0xff))));
  103.             }
  104.             return $buf;
  105.         }
  106.  
  107.         //WEBSEND SPECIFIC
  108.  
  109.         /**
  110.         * Run a command as if the specified player typed it into the chat.
  111.         *
  112.         * @param string $cmmd Command and arguments to run.
  113.         * @param string $playerName Exact name of the player to run it as.
  114.         * @return true if the command and player were found, else false
  115.         */
  116.         public function doCommandAsPlayer($cmmd, $playerName){
  117.             $this->writeRawByte(1);
  118.             $this->writeString($cmmd);
  119.             if(isset($playerName)){
  120.                 $this->writeString($playerName);
  121.             }else{
  122.                 $this->writeString("null");
  123.             }
  124.  
  125.             if($this->readRawInt() == 1){
  126.                 return true;
  127.             }else{
  128.                 return false;
  129.             }
  130.         }
  131.  
  132.         /**
  133.         * Run a command as if it were typed into the console.
  134.         *
  135.         * @param string $cmmd Command and arguments to run.
  136.         * @return true if the command was found, else false
  137.         */
  138.         public function doCommandAsConsole($cmmd){
  139.             $this->writeRawByte(2);
  140.             $this->writeString($cmmd);
  141.  
  142.             if($this->readRawInt() == 1){
  143.                 return true;
  144.             }else{
  145.                 return false;
  146.             }
  147.         }
  148.  
  149.         /**
  150.         * Run a script.
  151.         * The script has to be in the Websend scripts directory and has to be compiled and loaded before this is runned.
  152.         *
  153.         * @param string $scriptName Name of the script.
  154.         */
  155.         public function doScript($scriptName){
  156.             $this->writeRawByte(3);
  157.             $this->writeString($scriptName);
  158.         }
  159.        
  160.         /**
  161.         * Start plugin output capture
  162.         *
  163.         * @param string $pluginName Name of the plugin.
  164.         */
  165.         public function startPluginOutputListening($pluginName){
  166.             $this->writeRawByte(4);
  167.             $this->writeString($pluginName);
  168.         }
  169.        
  170.         /**
  171.         * Stop plugin output capture
  172.         *
  173.         * @param string $pluginName Name of the plugin.
  174.         * @return array of strings that contains output.
  175.         */
  176.         public function stopPluginOutputListening($pluginName){
  177.             $this->writeRawByte(5);
  178.             $this->writeString($pluginName);
  179.             $size = $this->readRawInt();
  180.             $data = array();
  181.             for($i = 0; $i<$size;$i++){
  182.                 $messageSize = $this->readRawInt();
  183.                 $data[$i] = $this->readChars($messageSize);
  184.             }
  185.             return $data;
  186.         }
  187.        
  188.         /**
  189.         * Print output to the console window. Invisible to players.
  190.         */
  191.         public function writeOutputToConsole($message){
  192.             $this->writeRawByte(10);
  193.             $this->writeString($message);
  194.         }
  195.  
  196.         /**
  197.         * Prints output to specified player.
  198.         *
  199.         * @param string $message Message to be shown.
  200.         * @param string $playerName Exact name of the player to print the message to.
  201.         * @return true if the player was found, else false
  202.         */
  203.         public function writeOutputToPlayer($message, $playerName){
  204.             $this->writeRawByte(11);
  205.             $this->writeString($message);
  206.             if(isset($playerName)){
  207.                 $this->writeString($playerName);
  208.             }else{
  209.                 $this->writeString("null");
  210.             }
  211.  
  212.             if($this->readRawInt() == 1){
  213.                 return true;
  214.             }else{
  215.                 return false;
  216.             }
  217.         }
  218.  
  219.         /**
  220.         * Prints a message to all players and the console.
  221.         *
  222.         * @param string $message Message to be shown.
  223.         */
  224.         public function broadcast($message){
  225.             $this->writeRawByte(12);
  226.             $this->writeString($message);
  227.         }
  228.     }
  229. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement