[PHP]Remote commands [1.3.1]

Status
Not open for further replies.

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
this will work on any websend version AFTER 1.2.5, the current version is 1.2.5 so that's why it's in the title.
Easy as copy 'n paste, edit lines 4-6 on config.php, edit your Websend config (if you haven't already) and this shall work. This originally was going to be a rank change but then I realized that the commands for all the permisison systems where different (but an if statement would easily change that) so I just decided to make a working version of a command sender, this sends commands to the bukkit server and is executed as console.
Download:

config.php:
PHP:
<?php
# By Mace at DevBest.com, please don't re-release without my permission #
 
# Configuration #
$HOST = '127.0.0.1';  # The IP of the Minecraft server, 127.0.0.1 for localhost #
$password = 'password'; # Websend password #
$port = '4445'; # the port Websend uses #
# Don't touch anything below this line! #
$command = $_POST['command'];
 
?>
Index.php
PHP:
<html>
<!-- # By Mace at DevBest.com, please don't re-release without my permission # -->
<head>
<title>Rank change</title>
</head>
<body>
<form name="rank" action="send.php" method="post">
Command: <input type="text" name="command" /><br />
<input type="submit" value="Change rank" />
</form>
</body>
</html>
Send.php
PHP:
<?php
# By Mace at DevBest.com, please don't re-release without my permission #
    include_once 'Websend.php';
    include('config.php');
   
    $ws = new Websend($HOST);
    $ws->connect($password);
    $ws->doCommandAsConsole($command);
    $ws->disconnect();
?>
YOU CANNOT USE THE DEFAULT WEBSEND.PHP WITH THIS, IT WILL NOT WORK AND GIVE YOU ERRORS!
PHP:
<?php
# No need to touch anything in here #
class Websend{
    var $host;
    var $port;
    var $stream;
   
    public function __construct($host, $port = 4445){
        $this->host = $host;
        $this->port = $port;
    }
   
    /**
    * Connects to a Websend server.
    */
    public function connect($password){
        $this->stream = fsockopen($this->host, $this->port);
        $this->writeRawByte(21);
        $this->writeString($password);
    }
   
    /**
    * Sends a disconnect signal to the currently connected Websend server.
    */
    public function disconnect(){
        $this->writeRawByte(20);
    }
   
    //NETWORK IO
   
    private function writeRawInt( $i ) {
        fwrite( $this->stream, pack( "N", $i ), 4 );
    }
    private function writeRawDouble( $d ) {
        fwrite( $this->stream, strrev( pack( "d", $d ) ) );
    }
   
    private function writeRawByte( $b ) {
        fwrite( $this->stream, strrev( pack( "C", $b ) ) );
    }
   
    private function writeChar( $char ){
        $v = ord($char);
        $this->writeRawByte((0xff & ($v >> 8)));
        $this->writeRawByte((0xff & $v));
    }
   
    private function writeChars( $string ){
        $array = str_split($string);
        foreach($array as &$cur){
            $v = ord($cur);
            $this->writeRawByte((0xff & ($v >> 8)));
            $this->writeRawByte((0xff & $v));
        }
    }
   
    private function writeString( $string ){
        $array = str_split($string);
        $this->writeRawInt(count($array));
        foreach($array as &$cur){
            $v = ord($cur);
            $this->writeRawByte((0xff & ($v >> 8)));
            $this->writeRawByte((0xff & $v));
        }
    }
   
    private function readRawInt() {
        $a = $this->readRawByte();
        $b = $this->readRawByte();
        $c = $this->readRawByte();
        $d = $this->readRawByte();
        $i = ((($a & 0xff) << 24) | (($b & 0xff) << 16) | (($c & 0xff) << 8) | ($d & 0xff));
        return $i;
    }
    private function readRawDouble() {
        $up = unpack( "di", strrev( fread( $this->stream, 8 ) ) );
        $d = $up["i"];
        return $d;
    }
    private function readRawByte() {
        $up = unpack( "Ci", fread( $this->stream, 1 ) );
        $b = $up["i"];
        return $b;
    }
    private function readChar() {
        $byte1 = $this->readRawByte();
        $byte2 = $this->readRawByte();
        $charValue = chr(utf8_decode((($byte1 << 8) | ($byte2 & 0xff))));
        return $charValue;
    }
    private function readChars($len) {
        $buf = "";
        for($i = 0;$i<$len;$i++){
            $byte1 = $this->readRawByte();
            $byte2 = $this->readRawByte();
            $buf = $buf.chr(utf8_decode((($byte1 << 8) | ($byte2 & 0xff))));
        }
        return $buf;
    }
   
    //WEBSEND SPECIFIC
   
    /**
    * Run a command as if the specified player typed it into the chat.
    *
    * @param string $cmmd Command and arguments to run.
    * @param string $playerName Exact name of the player to run it as.
    * @return true if the command and player were found, else false
    */
    public function doCommandAsPlayer($cmmd, $playerName){
        $this->writeRawByte(1);
        $this->writeString($cmmd);
        if(isset($playerName)){
            $this->writeString($playerName);
        }else{
            $this->writeString("null");
        }
       
        if($this->readRawInt() == 1){
            return true;
        }else{
            return false;
        }
    }
   
    /**
    * Run a command as if it were typed into the console.
    *
    * @param string $cmmd Command and arguments to run.
    * @return true if the command was found, else false
    */
    public function doCommandAsConsole($cmmd){
        $this->writeRawByte(2);
        $this->writeString($cmmd);
       
        if($this->readRawInt() == 1){
            return true;
        }else{
            return false;
        }
    }
   
    /**
    * Start output capturing of a plugin. REQUIRES MODDED CRAFTBUKKIT.
    *
    * @param string $pluginName Name of the plugin to capture output of.
    */
    public function startOutputCapture($pluginName){
        $this->writeRawByte(4);
        $this->writeString($pluginName);
    }
   
    /**
    * Stop output capturing of a plugin. REQUIRES MODDED CRAFTBUKKIT.
    *
    * @param string $pluginName Name of the plugin to stop the capture output of.
    * @return string[] Array of strings which contain the output, if any output was found.
    */
    public function stopOutputCapture($pluginName){
        $this->writeRawByte(5);
        $this->writeString($pluginName);
       
        $arraySize = $this->readRawInt();
        $array = array();
        for($i = 0;$i<$arraySize;$i++){
            $array[$i] = $this->readChars($this->readRawInt());
        }
        return $array;
    }
   
    /**
    * Run a script.
    * The script has to be in the Websend scripts directory and has to be compiled and loaded before this is runned.
    *
    * @param string $scriptName Name of the script.
    */
    public function doScript($scriptName){
        $this->writeRawByte(3);
        $this->writeString($scriptName);
    }
   
    /**
    * Print output to the console window. Invisible to players.
    */
    public function writeOutputToConsole($message){
        $this->writeRawByte(10);
        $this->writeString($message);
    }
   
    /**
    * Prints output to specified player.
    *
    * @param string $message Message to be shown.
    * @param string $playerName Exact name of the player to print the message to.
    * @return true if the player was found, else false
    */
    public function writeOutputToPlayer($message, $playerName){
        $this->writeRawByte(11);
        $this->writeString($message);
        if(isset($playerName)){
            $this->writeString($playerName);
        }else{
            $this->writeString("null");
        }
       
        if($this->readRawInt() == 1){
            return true;
        }else{
            return false;
        }
    }
   
    /**
    * Prints a message to all players and the console.
    *
    * @param string $message Message to be shown.
    */
    public function broadcast($message){
        $this->writeRawByte(6);
        $this->writeString($message);
    }
}
?>
Credits:
50% - me
50% -
Enjoy.
 

Macemore

Circumcised pineapples
Aug 26, 2011
1,681
819
tested on the new Websend 1.3.1 release, works 100%.

Moderator please update the title of the thread to "[PHP Remote Commands [ 1.3.1]" thank you.


 
Status
Not open for further replies.

Users who are viewing this thread

Top