Created
September 28, 2010 15:11
-
-
Save nylen/601164 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function do_xmlrpc($request) { | |
global $scgi_host, $scgi_port, $scgi_timeout; | |
if($response = scgi_send($scgi_host, $scgi_port, $request, $scgi_timeout)) { | |
$response = parse_http_response($response); | |
$content = str_replace('i8', 'double', $response[1]); | |
return xmlrpc_decode(utf8_encode($content)); | |
} else { | |
die('<h1>ERROR: Cannot connect to rtorrent</h1>'); | |
} | |
} | |
// ... snip other functions ... | |
function parse_http_response($string) { | |
$headers = array(); | |
$arr = explode("\n", $string); | |
for($i = 0; $i < count($arr); $i++) { | |
$str = trim($arr[$i]); | |
if($str === '') { | |
return array($headers, implode("\n", array_slice($arr, $i + 1))); | |
} else { | |
list($name, $val) = explode(':', $str, 2); | |
$name = strtolower($name); | |
$val = ltrim($val); | |
if(is_array($headers[$name])) { | |
$headers[$name][] = $val; | |
} else if(isset($headers[$name])) { | |
$headers[$name] = array($headers[$name], $val); | |
} else { | |
$headers[$name] = $val; | |
} | |
} | |
} | |
return array($headers, ''); | |
} | |
// from source code of rutorrent | |
function scgi_send($host, $port, $data, $timeout=5) { | |
$result = ''; | |
$contentlength = strlen($data); | |
if($contentlength > 0) { | |
$socket = @fsockopen($host, $port, $errno, $errstr, $timeout); | |
if($socket) { | |
$reqheader = "CONTENT_LENGTH\x00$contentlength\x00SCGI\x001\x00"; | |
$tosend = strlen($reqheader) . ":$reqheader,$data"; | |
@fputs($socket, $tosend); | |
while(!feof($socket)) { | |
$result .= @fread($socket, 4096); | |
} | |
fclose($socket); | |
} | |
} | |
return $result; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Full file is located at http://github.com/nylen/rtgui/blob/master/functions.php