setCredentials('nzbget',$passwd);
$c->setDebug(False);
$r=$c->send($f, $ConnectTimeout);
if(!$r->faultCode())
//Got a valid result, decode into php variables
return php_xmlrpc_decode($r->value());
else {
//Got an error, print description
trigger_error('RPC: method "'.$request.'", error '.$r->faultCode().' - '.$r->faultString());
}
}
function GetMultiRequest_XmlRpc_Lib($host, $port, $passwd, $requestarr) {
global $ConnectTimeout;
$c=new xmlrpc_client('xmlrpc', $host, $port);
$c->setCredentials('nzbget',$passwd);
$c->setDebug(False);
$farr=array();
foreach ($requestarr as $request) {
$f=new xmlrpcmsg($request[0], ParamsLIB($request[1]));
$farr[]=$f;
}
$ra=$c->multicall($farr, $ConnectTimeout);
$rarr=array();
$index = 0;
foreach ($ra as $r) {
if(!$r->faultCode())
//Got a valid result, decode into php variables
$rarr[] = php_xmlrpc_decode($r->value());
else {
if (!strncmp($r->faultString(), 'Connect error: ', 15)) {
return 'ERROR: '.$r->faultString();
}
trigger_error('RPC: method "'.$requestarr[$index][0].'", error '.$r->faultCode().' - '.$r->faultString());
}
$index++;
}
return $rarr;
}
function GetRequest_XmlRpc_Ext($host, $port, $passwd, $method, $params) {
$request = xmlrpc_encode_request($method, $params);
$file = SendRequest($host, $port, '/xmlrpc', 'nzbget', $passwd, $request);
//echo "file=".$file."
";
if (IsConnectError($file)) {
return $file;
}
$response = xmlrpc_decode($file);
//var_dump($response);
if (is_array($response) && xmlrpc_is_fault($response))
trigger_error('RPC: method "'.$method.'", error '.$response['faultCode'].' - '.$response['faultString']);
else
return $response;
}
function GetMultiRequest_XmlRpc_Ext($host, $port, $passwd, $methodarr) {
$multirequest=array();
foreach ($methodarr as $method) {
$multirequest[] = array('methodName' => $method[0], 'params' => $method[1]);
}
$request = xmlrpc_encode_request('system.Multicall', $multirequest);
$file = SendRequest($host, $port, '/xmlrpc', 'nzbget', $passwd, $request);
//echo "file=".$file."
";
if (IsConnectError($file)) {
return $file;
}
$response = xmlrpc_decode($file);
if (is_array($response) && xmlrpc_is_fault($response)) {
trigger_error('RPC: method "system.Multicall", error '.$response['faultCode'].' - '.$response['faultString']);
} else if (is_array($response)) {
$index = 0;
foreach ($response as $r) {
if (is_array($r) && array_key_exists(0, $r) && is_array($r[0]) && array_key_exists('faultCode', $r[0])) {
trigger_error('RPC: method "'.$methodarr[$index][0].'", error '.$r[0]['faultCode']." - ".$r[0]['faultString']);
}
$index++;
}
return $response;
} else {
return 'ERROR: Could not decode xml-data. Multicall-method.';
}
}
function GetRequest_JsonRpc_Ext($host, $port, $passwd, $method, $params) {
$reqarr=array('version' => '1.1', 'method' => $method, 'params' => $params);
$request = json_encode($reqarr);
$file = SendRequest($host, $port, '/jsonrpc', 'nzbget', $passwd, $request);
if (IsConnectError($file)) {
return $file;
}
$response = json_decode($file, true);
if (is_array($response) && isset($response['error']) && isset($response['error']['code']))
trigger_error('RPC: method "'.$method.'", error '.$response['error']['code']." - ".$response['error']['message']);
else if (is_array($response) && isset($response['result']))
return $response['result'];
else
return 'ERROR: Could not decode json-data. Method "'.$method.'".';
}
function GetMultiRequest_JsonRpc_Ext($host, $port, $passwd, $methodarr) {
// There are no native support for multicalls in JSON-RPC, so we emulate it
$response=array();
foreach ($methodarr as $method) {
$methodName=$method[0];
$params=$method[1];
$resp=GetRequest_JsonRpc_Ext($host, $port, $passwd, $methodName, $params);
if (IsConnectError($resp)) {
return $resp;
}
$response[]=$resp;
}
return $response;
}
function GetRequest_JsonRpc_Lib($host, $port, $passwd, $method, $params) {
$reqarr=array('version' => '1.1', 'method' => $method, 'params' => $params);
$json = new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
$request = $json->encode($reqarr);
$file = SendRequest($host, $port, '/jsonrpc', 'nzbget', $passwd, $request);
if (IsConnectError($file)) {
return $file;
}
$response = $json->decode($file);
if (is_array($response) && isset($response['error']) && isset($response['error']['code']))
trigger_error('RPC: method "'.$method.'", error '.$response['error']['code'].' - '.$response['error']['message']);
else if (is_array($response) && isset($response['result']))
return $response['result'];
else
return 'ERROR: Could not decode json-data. Method "'.$method.'".';
}
function GetMultiRequest_JsonRpc_Lib($host, $port, $passwd, $methodarr) {
// There are no native support for multicalls in JSON-RPC, so we emulate it
$response=array();
foreach ($methodarr as $method) {
$methodName=$method[0];
$params=$method[1];
$resp=GetRequest_JsonRpc_Lib($host, $port, $passwd, $methodName, $params);
if (IsConnectError($resp)) {
return $resp;
}
$response[]=$resp;
}
return $response;
}
function GetAvailableApi() {
global $RpcApi;
if (!isset($RpcApi) || ($RpcApi== '') || ($RpcApi== 'auto')) {
if (function_exists('json_decode'))
$RpcApi='json-rpc-ext';
else if (function_exists('xmlrpc_encode_request'))
$RpcApi='xml-rpc-ext';
else if (class_exists('Services_JSON'))
$RpcApi='json-rpc-lib';
else if (class_exists('xmlrpc_client'))
$RpcApi='xml-rpc-lib';
else
$RpcApi = 'none';
}
return $RpcApi;
}
function GetRequest($method, $params) {
global $ServerIp, $ServerPort, $ServerPassword;
$RpcApi= GetAvailableApi();
if ($RpcApi=='json-rpc-ext')
return GetRequest_JsonRpc_Ext($ServerIp, $ServerPort, $ServerPassword, $method, $params);
else if ($RpcApi=='json-rpc-lib')
return GetRequest_JsonRpc_Lib($ServerIp, $ServerPort, $ServerPassword, $method, $params);
else if ($RpcApi=='xml-rpc-ext')
return GetRequest_XmlRpc_Ext($ServerIp, $ServerPort, $ServerPassword, $method, $params);
else if ($RpcApi=='xml-rpc-lib')
return GetRequest_XmlRpc_Lib($ServerIp, $ServerPort, $ServerPassword, $method, $params);
else if ($RpcApi=='none')
return 'ERROR: Could not detect available RPC-API: old PHP version or modules missing. See README for details.';
else
return 'ERROR: Invalid value for option "rpc_api"';
}
function GetMultiRequest($methodarr) {
global $ServerIp, $ServerPort, $ServerPassword;
$RpcApi= GetAvailableApi();
if ($RpcApi=='json-rpc-ext')
return GetMultiRequest_JsonRpc_Ext($ServerIp, $ServerPort, $ServerPassword, $methodarr);
else if ($RpcApi=='json-rpc-lib')
return GetMultiRequest_JsonRpc_Lib($ServerIp, $ServerPort, $ServerPassword, $methodarr);
else if ($RpcApi=='xml-rpc-ext')
return GetMultiRequest_XmlRpc_Ext($ServerIp, $ServerPort, $ServerPassword, $methodarr);
else if ($RpcApi=='xml-rpc-lib')
return GetMultiRequest_XmlRpc_Lib($ServerIp, $ServerPort, $ServerPassword, $methodarr);
else if ($RpcApi=='none')
return 'ERROR: Could not detect available RPC-API: old PHP version or modules missing. See README for details.';
else
return 'ERROR: Invalid value for option "rpc_api"';
}
function IsConnectError($resp) {
return is_string($resp) && !strncmp($resp, 'ERROR:', 6);
}
//
// RPC-Interface to NZBGet-Server - END
//*****************************************************************************
// workaround for a bug on one of test system, where "round" with 0-argument hangs
function round0($arg) {
return $arg==0 ? 0 : round($arg);
}
function round1($arg) {
return $arg < 0.1 ? '0.0' : number_format($arg, 1);
}
function round2($arg) {
return $arg < 0.01 ? '0.00' : number_format($arg, 2);
}
function freediskspace() {
global $CheckSpaceDir;
if (!file_exists($CheckSpaceDir)) {
trigger_error("Directory $CheckSpaceDir does not exist. Check option \"CheckSpaceDir\"");
return 0;
}
return disk_free_space($CheckSpaceDir)/1024/1024;
}
function namereplace($name) {
global $NameReplaceChars;
return strtr($name, $NameReplaceChars, str_pad('', strlen($NameReplaceChars)));
}
function FormatLogText($text) {
$text = str_replace(chr(8), ' ', $text);
global $LogBreakChars;
foreach (str_split($LogBreakChars) as $ch)
{
$text = str_replace($ch, $ch.' ', $text);
}
return $text;
}
function sec2hms($sec) {
$hms = '';
$days = intval(intval($sec) / 86400);
if ($days > 0)
$hms .= $days . 'd ';
$hours = intval((intval($sec) % 86400) / 3600);
$hms .= $hours. ':';
$minutes = intval(($sec / 60) % 60);
$hms .= str_pad($minutes, 2, '0', STR_PAD_LEFT). ':';
$seconds = intval($sec % 60);
$hms .= str_pad($seconds, 2, '0', STR_PAD_LEFT);
return $hms;
}
function formatSizeMB($MB) {
if ($MB > 10240)
return round1($MB / 1024.0) . ' GB';
else if ($MB > 1024)
return round2($MB / 1024.0) . ' GB';
else
return round2($MB) . ' MB';
}
function formatAge($time) {
if ($time > 0) {
$diff = time() - $time;
if ($diff > 60*60*24)
return round0($diff / (60*60*24)) .' d';
else
return round0($diff / (60*60)) .' h';
}
}
function Redirect($url) {
// redirect and exit: headers not always work, so we use additionally META-tag
echo "