Commit db24b197 authored by Gabor Hudiczius's avatar Gabor Hudiczius
Browse files

rtwi-0.3.4-090726

parent ac81d6ab
<?php
class xmlrpc_handler {
var $conntype;
var $host;
var $port;
var $timeout;
var $request;
var $response;
var $calls;
var $mcalls;
var $times;
var $mtimes;
var $user;
var $password;
var $conntype = "";
var $host = "";
var $port = 0;
var $timeout = 10;
var $request = null;
var $response = null;
var $calls = 0;
var $mcalls = 0;
var $times = 0;
var $mtimes = 0;
var $user = null;
var $password = null;
var $errors = array();
function __construct( $address, $timeout ) {
$this->conntype = "";
$this->host = "";
$this->port = 0;
if ( preg_match( "|^unix://(.+)$|", $address, $match ) ) {
$this->conntype = "socket";
$this->host = $match[1];
$this->port = 0;
if ( ( $perms = @fileperms( $this->host ) ) === false ) {
$this->conntype = "";
$this->host = "";
if ( empty( $match[1] ) ) {
$this->errors[] = "xmlrpc_socket_empty";
} elseif ( !file_exists( $match[1] ) ) {
$this->errors[] = "xmlrpc_socket_noexist";
} elseif ( ( $perms = @fileperms( $match[1] ) ) === false ) {
$this->errors[] = "xmlrpc_socket_noperms";
} elseif ( ( $perms & 0xC000 ) !== 0xC000 ) {
$this->errors[] = "xmlrpc_socket_nosocket";
} elseif ( !is_writable( $match[1] ) ) {
$this->errors[] = "xmlrpc_socket_nowrite";
} elseif ( !is_readable( $match[1] ) ) {
$this->errors[] = "xmlrpc_socket_noread";
} else {
if ( ( fileperms( $this->host ) & 0xC000 ) !== 0xC000 ) {
$this->conntype = "";
$this->host = "";
} elseif ( !is_writable( $this->host ) ) {
$this->conntype = "";
$this->host = "";
} else {
$this->host = $match[0];
}
$this->conntype = "socket";
$this->host = $match[0];
}
} elseif ( preg_match( "<^(http|https)://(.*)$>", $address, $match ) ) {
$this->conntype = "http";
$this->host = $match[0];
$this->port = 0;
if ( empty( $match[2] ) ) {
$this->errors[] = "xmlrpc_url_empty";
} else {
$this->conntype = "http";
$this->host = $match[0];
}
} elseif ( preg_match( "|^:(\d{1,5})$|", $address, $match ) ) {
$this->conntype = "localport";
$this->port = $match[1];
settype( $this->port, "int" );
if ( $this->port < 1 || $this->port > 65535 ) {
$this->conntype = "";
$this->host = "";
$this->port = 0;
$this->errors[] = "xmlrpc_localport_outofrange";
} else {
$this->host = "127.0.0.1";
}
......@@ -54,30 +58,18 @@ class xmlrpc_handler {
$this->port = $match[5];
settype( $this->port, "int" );
if ( ip2long( $this->host ) === false ) {
$this->conntype = "";
$this->host = "";
$this->port = 0;
$this->errors[] = "xmlrpc_port_invalidip";
} elseif ( $this->host != long2ip( ip2long( $this->host ) ) ) {
$this->conntype = "";
$this->host = "";
$this->port = 0;
$this->errors[] = "xmlrpc_port_invalidipplus";
}
if ( $this->port < 1 || $this->port > 65535 ) {
$this->conntype = "";
$this->host = "";
$this->port = 0;
$this->errors[] = "xmlrpc_port_outofrange";
}
} else {
$this->conntype = "";
$this->host = "";
$this->port = 0;
$this->errors[] = "xmlrpc_nodestination";
}
$this->timeout = $timeout;
$this->request = null;
$this->response = null;
$this->calls = 0;
$this->mcalls = 0;
}
function set_type( &$value, $xmlrpc_type ) {
......@@ -113,28 +105,47 @@ class xmlrpc_handler {
case "struct" : {
$mnode = $inode->firstChild;
while ( $mnode != NULL ) {
if ( ( $mnode->firstChild->nodeValue == "faultCode" ) && ( $mnode->nextSibling->firstChild->nodeValue == "faultString" ) ) {
$this->errors[] = "{$mnode->nextSibling->lastChild->nodeValue} ({$mnode->lastChild->nodeValue})";
$this->errors[] = "xmlrpc_fault";
return false;
}
$nname = $mnode->firstChild->nodeValue;
$response[$nname] = $this->decode_rec( $mnode->lastChild->firstChild );
$dec = $this->decode_rec( $mnode->lastChild->firstChild );
if ( $dec === false ) {
return false;
}
$response[$nname] = $dec;
$mnode = $mnode->nextSibling;
}
break;
}
case "array" : {
$vnode = $inode->firstChild->firstChild;
while ( $vnode != NULL ) {
$response[] = $this->decode_rec( $vnode->firstChild );
$dec = $this->decode_rec( $vnode->firstChild );
$response[] = $dec;
if ( $dec === false ) {
return false;
}
$vnode = $vnode->nextSibling;
}
break;
}
}
return $response;
return ( isset( $response ) ? $response : "" );
}
private function decode( $str ) {
$str = preg_replace( "%<(/{0,1})(i8|ex.i8)>%", "<\\1string>", $str );
$str = preg_replace( "%<(/{0,1})(i8|ex\.i8)>%", "<\\1string>", $str );
$str = preg_replace( "/>\s*?</s", "><", $str );
$xml = new DOMDocument( "1.0", "utf-8" );
$xml->formatOutput = true;
$xml->loadXML( $str );
if ( !@$xml->loadXML( $str ) ) {
$this->errors[] = "xmlrpc_xml_noload";
return false;
}
$root = $xml->firstChild;
$root->nodeName;
......@@ -142,6 +153,9 @@ class xmlrpc_handler {
if ( $root->firstChild->nodeName == "fault" ) {
$response[faultCode] = (int)$root->firstChild->firstChild->firstChild->firstChild->lastChild->firstChild->nodeValue;
$response[faultString] = $root->firstChild->firstChild->firstChild->lastChild->lastChild->firstChild->nodeValue;
$this->errors[] = "{$response[faultString]} ({$response[faultCode]})";
$this->errors[] = "xmlrpc_fault";
return false;
} else {
$vnode = $root->firstChild->firstChild->firstChild;
$inode = $vnode->firstChild;
......@@ -161,21 +175,29 @@ class xmlrpc_handler {
$mnode = $inode->firstChild;
while ( $mnode != NULL ) {
$nname = $mnode->firstChild->nodeValue;
$response[$nname] = $this->decode_rec( $mnode->lastChild->firstChild );
$dec = $this->decode_rec( $mnode->lastChild->firstChild );
if ( $dec === false ) {
return false;
}
$response[$nname] = $dec;
$mnode = $mnode->nextSibling;
}
}
case "array" : {
$vnode = $inode->firstChild->firstChild;
while ( $vnode != NULL ) {
$response[] = $this->decode_rec( $vnode->firstChild );
$dec = $this->decode_rec( $vnode->firstChild );
if ( $dec === false ) {
return false;
}
$response[] = $dec;
$vnode = $vnode->nextSibling;
}
}
}
}
return $response;
return ( isset( $response ) ? $response : array() );
}
private function encode_request_rec( $method, $param, &$xml, $pnode ) {
......@@ -205,13 +227,17 @@ class xmlrpc_handler {
foreach ( $param as $key => $val ) {
$mnode = $anode->appendChild( $xml->createElement( "member" ) );
$nnode = $mnode->appendChild( $xml->createElement( "name", $key ) );
$this->encode_request_rec( $method, $val, $xml, $mnode );
if ( $this->encode_request_rec( $method, $val, $xml, $mnode ) === false ) {
return false;
}
}
} else {
$anode = $vnode->appendChild( $xml->createElement( "array" ) );
$dnode = $anode->appendChild( $xml->createElement( "data" ) );
foreach ( $param as $key => $val ) {
$this->encode_request_rec( $method, $val, $xml, $dnode );
if ( $this->encode_request_rec( $method, $val, $xml, $dnode ) === false ) {
return false;
}
}
}
break;
......@@ -230,8 +256,9 @@ class xmlrpc_handler {
break;
}
default : {
print "Error: ".gettype( $param );
exit;
$this->errors[] = gettype( $param );
$this->errors[] = "xmlrpc_request_invalidtype";
return false;
break;
}
}
......@@ -239,7 +266,7 @@ class xmlrpc_handler {
private function encode_request( $method, $params ) {
$xml = new DOMDocument( "1.0", "utf-8" );
$xml->formatOutput = false;
$xml->formatOutput = true;
$root = $xml->appendChild( $xml->createElement( "methodCall" ) );
$mnode = $root->appendChild( $xml->createElement( "methodName", $method ) );
......@@ -259,17 +286,23 @@ class xmlrpc_handler {
foreach ( $params as $key => $val ) {
$mnode = $anode->appendChild( $xml->createElement( "member" ) );
$nnode = $mnode->appendChild( $xml->createElement( "name", $key ) );
$this->encode_request_rec( $method, $val, $xml, $mnode );
if ( $this->encode_request_rec( $method, $val, $xml, $mnode ) === false ) {
return false;
}
}
} else {
foreach ( $params as $key => $val ) {
$pnode = $psnode->appendChild( $xml->createElement( "param" ) );
$this->encode_request_rec( $method, $val, $xml, $pnode );
if ( $this->encode_request_rec( $method, $val, $xml, $pnode ) === false ) {
return false;
}
}
}
} else {
$pnode = $psnode->appendChild( $xml->createElement( "param" ) );
$this->encode_request_rec( $method, $params, $xml, $pnode );
if ( $this->encode_request_rec( $method, $params, $xml, $pnode ) === false ) {
return false;
}
}
return $xml->saveXML();
......@@ -290,6 +323,11 @@ class xmlrpc_handler {
$this->request[] = array( "methodName" => $methodval, "params" => $params );
}
$this->request = $this->encode_request( "system.multicall", array( $this->request ) );
if ( $this->request === false ) {
return false;
} else {
return true;
}
}
function setumrequest( $methods, $params ) {
......@@ -299,10 +337,20 @@ class xmlrpc_handler {
$this->request[] = array( "methodName" => $methods[$i], "params" => $params[$i] );
}
$this->request = $this->encode_request( "system.multicall", array( $this->request ) );
if ( $this->request === false ) {
return false;
} else {
return true;
}
}
function setrequest( $method, $attributes ) {
$this->request = $this->encode_request( $method, $attributes );
if ( $this->request === false ) {
return false;
} else {
return true;
}
}
function scgi_call() {
......@@ -314,32 +362,43 @@ class xmlrpc_handler {
$len = strlen( $headers );
$out = "{$len}:{$headers},{$this->request}";
$fp = fsockopen( $this->host, $this->port, $errno, $errstr, $this->timeout );
if ( $fp ) {
$fp = @fsockopen( $this->host, $this->port, $errno, $errstr, $this->timeout );
if ( $fp === false ) {
$this->errors[] = "{$error} ({$errno})";
$this->errors[] = "xmlrpc_scgi_connectfailed";
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
return false;
} else {
fwrite( $fp, $out );
do {
$line = fgets( $fp );
} while ( trim( $line ) != "" );
if ( ( $this->response = stream_get_contents( $fp ) ) !== false ) {
$this->response = stream_get_contents( $fp );
if ( $this->response === false ) {
$this->errors[] = "xmlrpc_scgi_readfailed";
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
return true;
} else {
return false;
} elseif ( $this->response === "" ) {
$this->errors[] = "xmlrpc_scgi_emptystring";
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
return false;
}
} else {
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
} else {
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
return false;
return true;
}
}
}
......@@ -358,7 +417,21 @@ class xmlrpc_handler {
curl_setopt( $ch, CURLOPT_POST, true );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $this->request );
curl_setopt( $ch, CURLOPT_USERPWD, "{$this->user}:{$this->password}" );
curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false );
$this->response = curl_exec( $ch );
if ( $this->response === false ) {
$error = curl_error( $ch );
$errno = curl_errno( $ch );
$this->errors[] = "{$error} ({$errno})";
$this->errors[] = "xmlrpc_gw_connectfailed";
return false;
} elseif ( $this->response === "" ) {
$error = curl_error( $ch );
$errno = curl_errno( $ch );
$this->errors[] = "{$error} ({$errno})";
$this->errors[] = "xmlrpc_gw_empttstring";
return false;
}
$tt = getmicrotime();
$this->calls++;
$this->times = $this->times + $tt - $st;
......@@ -373,7 +446,8 @@ class xmlrpc_handler {
break;
}
case "http" : {
case "http" :
case "https" : {
return $this->gw_call();
break;
......@@ -394,7 +468,8 @@ class xmlrpc_handler {
}
function parse() {
if ( ( $this->response = $this->decode( $this->response ) ) !== false ) {
$this->response = $this->decode( $this->response );
if ( $this->response !== false ) {
return true;
} else {
return false;
......@@ -420,16 +495,12 @@ class xmlrpc_handler {
}
$r++;
if ( isset( $this->response[$r]["faultCode"] ) && isset( $this->response[$r]["faultString"] ) ) {
print "(Error: {$response[$r]["faultCode"]} {$this->response[$r]["faultString"]})<br />\r\n";
} else {
if ( $methodval == "dht_statistics" ) {
foreach ( $this->response[$r][0] as $dkey => $dval ) {
$responses["{$methodval}_{$dkey}"] = $dval;
}
} else {
$responses[$methodval] = $this->response[$r][0];
if ( $methodval == "dht_statistics" ) {
foreach ( $this->response[$r][0] as $dkey => $dval ) {
$responses["{$methodval}_{$dkey}"] = $dval;
}
} else {
$responses[$methodval] = $this->response[$r][0];
}
}
......@@ -440,6 +511,22 @@ class xmlrpc_handler {
return $this->response;
}
function clearerrors() {
$this->errors = array();
}
function geterrorsnum() {
return count( $this->errors );
}
function geterrors() {
return $this->errors;
}
function getlasterror() {
return array_pop( $this->errors );
}
}
?>
......@@ -132,6 +132,27 @@ $message = array(
"ip_0000" => "Not set",
"proxy_address_0000" => "Not set",
"xmlrpc_socket_empty" => "Malformed unix socket path",
"xmlrpc_socket_noexist" => "The socket does not exist",
"xmlrpc_socket_noperms" => "Cannot read permissions for the socket",
"xmlrpc_socket_nosocket" => "The socket seems not to be a unix socket",
"xmlrpc_socket_nowrite" => "Cannot write socket",
"xmlrpc_socket_noread" => "Cannot read socket",
"xmlrpc_url_empty" => "The URL is empty",
"xmlrpc_localport_outofrange" => "Port is out of range",
"xmlrpc_port_invalidip" => "Invalid IP address",
"xmlrpc_port_invalidipplus" => "Invalid IP address (this message should never appear - if it does, open a ticker in the rTWi trac, and attach your users.conf)",
"xmlrpc_port_outofrange" => "Port is out of range",
"xmlrpc_nodestination" => "Nowhere to connect (malformed address)",
"xmlrpc_scgi_connectfailed" => "Connecting to rTorrent has failed",
"xmlrpc_scgi_readfailed" => "Receiving answer from rTorrent has failed",
"xmlrpc_scgi_emptystring" => "rTorrent has returnde nothing (SCGI)",
"xmlrpc_gw_connectfailed" => "Connecting to rTorrent (using SCGI gateway) has failed",
"xmlrpc_gw_emptystring" => "rTorrent has returnde nothing (GW)",
"xmlrpc_xml_noload" => "Malformed XML (propably a HTTP error, or redirect)",
"xmlrpc_request_invalidtype" => "Invalid parameter type",
"xmlrpc_fault" => "rTorrent returned an XMLRPC error",
"unknown" => "Unknown",
);
......@@ -139,6 +160,7 @@ $viewtypes = array(
"viewtype_main" => "主要",
"viewtype_default" => "預設",
"viewtype_name" => "名稱",
"viewtype_active" => "Active",
"viewtype_started" => "已開始",
"viewtype_stopped" => "已停止",
"viewtype_complete" => "已完成",
......
......@@ -132,6 +132,27 @@ $message = array(
"ip_0000" => "Nicht gesetzt",
"proxy_address_0000" => "Nicht gesetzt",
"xmlrpc_socket_empty" => "Malformed unix socket path",
"xmlrpc_socket_noexist" => "The socket does not exist",
"xmlrpc_socket_noperms" => "Cannot read permissions for the socket",
"xmlrpc_socket_nosocket" => "The socket seems not to be a unix socket",
"xmlrpc_socket_nowrite" => "Cannot write socket",
"xmlrpc_socket_noread" => "Cannot read socket",
"xmlrpc_url_empty" => "The URL is empty",
"xmlrpc_localport_outofrange" => "Port is out of range",
"xmlrpc_port_invalidip" => "Invalid IP address",
"xmlrpc_port_invalidipplus" => "Invalid IP address (this message should never appear - if it does, open a ticker in the rTWi trac, and attach your users.conf)",
"xmlrpc_port_outofrange" => "Port is out of range",
"xmlrpc_nodestination" => "Nowhere to connect (malformed address)",
"xmlrpc_scgi_connectfailed" => "Connecting to rTorrent has failed",
"xmlrpc_scgi_readfailed" => "Receiving answer from rTorrent has failed",
"xmlrpc_scgi_emptystring" => "rTorrent has returnde nothing (SCGI)",
"xmlrpc_gw_connectfailed" => "Connecting to rTorrent (using SCGI gateway) has failed",
"xmlrpc_gw_emptystring" => "rTorrent has returnde nothing (GW)",
"xmlrpc_xml_noload" => "Malformed XML (propably a HTTP error, or redirect)",
"xmlrpc_request_invalidtype" => "Invalid parameter type",
"xmlrpc_fault" => "rTorrent returned an XMLRPC error",
"unknown" => "Unbekannt",
);
......@@ -139,6 +160,7 @@ $viewtypes = array(
"viewtype_main" => "Main",
"viewtype_default" => "Standard",
"viewtype_name" => "Name",
"viewtype_active" => "Active",
"viewtype_started" => "Gestartet",
"viewtype_stopped" => "Gestoppt",
"viewtype_complete" => "Fertig",
......
......@@ -132,6 +132,27 @@ $message = array(
"ip_0000" => "Not set",
"proxy_address_0000" => "Not set",
"xmlrpc_socket_empty" => "Malformed unix socket path",
"xmlrpc_socket_noexist" => "The socket does not exist",
"xmlrpc_socket_noperms" => "Cannot read permissions for the socket",
"xmlrpc_socket_nosocket" => "The socket seems not to be a unix socket",
"xmlrpc_socket_nowrite" => "Cannot write socket",
"xmlrpc_socket_noread" => "Cannot read socket",
"xmlrpc_url_empty" => "The URL is empty",
"xmlrpc_localport_outofrange" => "Port is out of range",
"xmlrpc_port_invalidip" => "Invalid IP address",
"xmlrpc_port_invalidipplus" => "Invalid IP address (this message should never appear - if it does, open a ticker in the rTWi trac, and attach your users.conf)",
"xmlrpc_port_outofrange" => "Port is out of range",
"xmlrpc_nodestination" => "Nowhere to connect (malformed address)",
"xmlrpc_scgi_connectfailed" => "Connecting to rTorrent has failed",
"xmlrpc_scgi_readfailed" => "Receiving answer from rTorrent has failed",
"xmlrpc_scgi_emptystring" => "rTorrent has returnde nothing (SCGI)",
"xmlrpc_gw_connectfailed" => "Connecting to rTorrent (using SCGI gateway) has failed",
"xmlrpc_gw_emptystring" => "rTorrent has returnde nothing (GW)",
"xmlrpc_xml_noload" => "Malformed XML (propably a HTTP error, or redirect)",
"xmlrpc_request_invalidtype" => "Invalid parameter type",
"xmlrpc_fault" => "rTorrent returned an XMLRPC error",
"unknown" => "Unknown",
);
......@@ -139,6 +160,7 @@ $viewtypes = array(
"viewtype_main" => "Main",
"viewtype_default" => "Default",
"viewtype_name" => "Name",
"viewtype_active" => "Active",
"viewtype_started" => "Started",
"viewtype_stopped" => "Stopped",
"viewtype_complete" => "Complete",
......
......@@ -132,6 +132,27 @@ $message = array(
"ip_0000" => "Nincs beállítva",
"proxy_address_0000" => "Nincs beállítva",
"xmlrpc_socket_empty" => "A unix socket útvonalának formátuma hiás",
"xmlrpc_socket_noexist" => "A socket nem létezik",
"xmlrpc_socket_noperms" => "Nem lehet kiolvasni a socket jogosultságait",
"xmlrpc_socket_nosocket" => "A socket nem unix socket",
"xmlrpc_socket_nowrite" => "A socket nem írható",
"xmlrpc_socket_noread" => "A socket nem olvasható",
"xmlrpc_url_empty" => "Az URL üres",
"xmlrpc_localport_outofrange" => "A port 1 és 65535 között kell, hogy legyen",
"xmlrpc_port_invalidip" => "Hibás IP cím",
"xmlrpc_port_invalidipplus" => "Hibás IP cím (ez a hiba elvileg nem fordulhat elő, ellenkező esetben nyiss egy ticket-et a trac-ban és csatold a users.conf fájlt)",
"xmlrpc_port_outofrange" => "A port 1 és 65535 között kell, hogy legyen",
"xmlrpc_nodestination" => "Ninc hová csatlakozni (hibás cím)",
"xmlrpc_scgi_connectfailed" => "Nem sikerült kapcsolódni az rTorrent-hez",
"xmlrpc_scgi_readfailed" => "Nem sikerült választ kapni az rTorrent-től",
"xmlrpc_scgi_emptystring" => "Az rTorrent semmit nem adott vissza (SCGI)",
"xmlrpc_gw_connectfailed" => "Nem siker csatlakozni az rTorrent-hez (SCGI gateway haszálatával)",
"xmlrpc_gw_emptystring" => "Az rTorrent semmit nem adott vissza (GW)",
"xmlrpc_xml_noload" => "Hibás XML (valószínqleg HTTP hibaoldal, vagy átirányítás)",
"xmlrpc_request_invalidtype" => "Hibás paraméter típus",
"xmlrpc_fault" => "Az rTorrent XMLRPC hibával tért vissza",
"unknown" => "Ismeretlen",
);
......@@ -139,6 +160,7 @@ $viewtypes = array(
"viewtype_main" => "Fő nézet",
"viewtype_default" => "Alapértelmezett",
"viewtype_name" => "Név szerint",
"viewtype_active" => "Aktív",
"viewtype_started" => "Elindított torrentek",
"viewtype_stopped" => "Leállított torrentek",