RemoteAdmin:RemoteAdmin Class

From OpenSimulator

(Difference between revisions)
Jump to: navigation, search
Line 93: Line 93:
 
</source>
 
</source>
  
An example of using the RemoteAdmin.class.php
+
An example of using the RemoteAdmin.class.php [[https://github.com/wp2opensim/openSIMULATOR-Steuerung https://github.com/wp2opensim/openSIMULATOR-Steuerung]]
 
+
[[https://github.com/wp2opensim/openSIMULATOR-Steuerung https://github.com/wp2opensim/openSIMULATOR-Steuerung]]
+
 
[[Category:RemoteAdmin]]
 
[[Category:RemoteAdmin]]

Revision as of 12:50, 19 April 2018

RemoteAdmin Class

This is a modified version of Remote Admin Class

Tested with OpenSimulator v0.9.1.0 Dev

<?php
class RemoteAdmin
{
    function RemoteAdmin($sURL, $sPort, $pass)
    {
        $this->simulatorURL = $sURL;
        $this->simulatorPort = $sPort;
        $this->password = $pass;
    }
 
    function SendCommand($command, $params=array())
    {
        $paramsNames = array_keys($params);
        $paramsValues = array_values($params);
 
        $xml = '
        <methodCall>
        <methodName>'.htmlspecialchars($command).'</methodName>
            <params>
                <param>
                    <value>
                        <struct>
                            <member>
                                <name>password</name>
                                <value><string>'.htmlspecialchars($this->password).'</string></value>
                            </member>';
                            if (count($params) != 0)
                            {
                                for ($p = 0; $p < count($params); $p++)
                                {
                                    $xml .= '<member><name>'.htmlspecialchars($paramsNames[$p]).'</name>';
                                    $xml .= is_int($paramsValues[$p]) ? '<value><int>'.$paramsValues[$p].'</int></value></member>' : '<value><string>'.htmlspecialchars($paramsValues[$p]).'</string></value></member>';
                                }
                            }
                        $xml .= '</struct>
                    </value>
                </param>
            </params>
        </methodCall>';
 
        $host = $this->simulatorURL;
        $port = $this->simulatorPort;
        $timeout = 5;
        error_reporting(0);
        $fp = fsockopen($host, $port, $errno, $errstr, $timeout);
 
        if (!$fp)
        {
            return FALSE;
        }
 
        else
        {
            fputs($fp, "POST / HTTP/1.1\r\n");
            fputs($fp, "Host: $host\r\n");
            fputs($fp, "Content-type: text/xml\r\n");
            fputs($fp, "Content-length: ". strlen($xml) ."\r\n");
            fputs($fp, "Connection: close\r\n\r\n");
            fputs($fp, $xml);
            $res = "";
 
            while(!feof($fp))
            {
                $res .= fgets($fp, 128);
            }
 
            fclose($fp);
            $response = substr($res, strpos($res, "\r\n\r\n"));
            $result = array();
 
            if (preg_match_all('#<name>(.+)</name><value><(string|int)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER))
            {
                foreach($regs as $key => $val)
                {
                    $result[$val[1]] = $val[3];
                }
            }
            return $response;
        }
    }
}
?>

An example of using the RemoteAdmin.class.php [https://github.com/wp2opensim/openSIMULATOR-Steuerung]

Personal tools
General
About This Wiki