RemoteAdmin:RemoteAdmin Class
From OpenSimulator
(Difference between revisions)
(→RemoteAdmin Class) |
(→RemoteAdmin Class: Fix preg_match) |
||
| (5 intermediate revisions by 2 users not shown) | |||
| Line 5: | Line 5: | ||
'''This is a modified version of Remote Admin Class''' | '''This is a modified version of Remote Admin Class''' | ||
</div> | </div> | ||
| + | Tested with OpenSimulator v0.9.1.0 Dev (2019) and <b>PHP 7.3</b> | ||
| + | <source lang="php"> | ||
| + | <?php | ||
| + | class RemoteAdmin | ||
| + | { | ||
| + | function __construct($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|boolean|i4)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER)) | ||
| + | { | ||
| + | foreach($regs as $key => $val) | ||
| + | { | ||
| + | $result[$val[1]] = $val[3]; | ||
| + | } | ||
| + | } | ||
| + | return $result; | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | ?> | ||
| + | </source> | ||
| + | Tested with OpenSimulator v0.9.1.0 Dev (2018) and <b>PHP 5.6</b> | ||
<source lang="php"> | <source lang="php"> | ||
<?php | <?php | ||
| Line 79: | Line 162: | ||
$result = array(); | $result = array(); | ||
| − | if (preg_match_all('#<name>(.+)</name><value><(string|int)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER)) | + | if (preg_match_all('#<name>(.+)</name><value><(string|int|boolean|i4)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER)) |
{ | { | ||
foreach($regs as $key => $val) | foreach($regs as $key => $val) | ||
| Line 86: | Line 169: | ||
} | } | ||
} | } | ||
| − | return $ | + | return $result; |
} | } | ||
} | } | ||
| Line 92: | Line 175: | ||
?> | ?> | ||
</source> | </source> | ||
| + | |||
[[Category:RemoteAdmin]] | [[Category:RemoteAdmin]] | ||
Latest revision as of 16:48, 19 June 2019
[edit] RemoteAdmin Class
This is a modified version of Remote Admin Class
Tested with OpenSimulator v0.9.1.0 Dev (2019) and PHP 7.3
<?php class RemoteAdmin { function __construct($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|boolean|i4)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER)) { foreach($regs as $key => $val) { $result[$val[1]] = $val[3]; } } return $result; } } } ?>
Tested with OpenSimulator v0.9.1.0 Dev (2018) and PHP 5.6
<?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|boolean|i4)>(.*)</\2></value>#U', $response, $regs, PREG_SET_ORDER)) { foreach($regs as $key => $val) { $result[$val[1]] = $val[3]; } } return $result; } } } ?>