Source for file Remote.php
Documentation is available at Remote.php
// +----------------------------------------------------------------------+
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2004 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 3.0 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available through the world-wide-web at the following url: |
// | http://www.php.net/license/3_0.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Author: Stig Bakken <ssb@php.net> |
// +----------------------------------------------------------------------+
// $Id: Remote.php,v 1.50 2004/06/08 18:03:46 cellog Exp $
require_once 'PEAR/Config.php';
* This is a class for doing remote operations against the central
class PEAR_Remote extends PEAR
// {{{ PEAR_Remote(config_object)
function PEAR_Remote (&$config)
$this->config = &$config;
$cachedir = $this->config->get ('cache_dir');
$filename = $cachedir . DIRECTORY_SEPARATOR . 'xmlrpc_cache_' . $id;
$fp = fopen($filename, 'rb');
function saveCache ($args, $data)
$cachedir = $this->config->get ('cache_dir');
System::mkdir (array ('-p', $cachedir));
$filename = $cachedir. '/xmlrpc_cache_'. $id;
$fp = @fopen($filename, "wb");
// {{{ call(method, [args...])
$this->cache = $this->getCache ($args);
$cachettl = $this->config->get ('cache_ttl');
// If cache is newer than $cachettl seconds, we use the cache!
if ($this->cache !== null && $this->cache['age'] < $cachettl) {
return $this->cache['content'];
if (!PEAR ::isError ($result)) {
$this->saveCache ($_args, $result);
if (!@include_once("XML/RPC.php")) {
return $this->raiseError ("For this remote PEAR operation you need to install the XML_RPC package");
$server_host = $this->config->get ('master_server');
$username = $this->config->get ('username');
$password = $this->config->get ('password');
foreach($args as $arg) $eargs[] = $this->_encode ($arg);
$f = new XML_RPC_Message ($method, $eargs);
if ($this->cache !== null ) {
$maxAge = '?maxAge='. $this->cache['lastChange'];
$proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
if ($proxy = parse_url($this->config->get ('http_proxy'))) {
$proxy_host = @$proxy['host'];
$proxy_port = @$proxy['port'];
$c = new XML_RPC_Client ('/xmlrpc.php'. $maxAge, $server_host, 80 , $proxy_host, $proxy_port, $proxy_user, $proxy_pass);
if ($username && $password) {
$c->setCredentials ($username, $password);
if ($this->config->get ('verbose') >= 3 ) {
return $this->raiseError ("XML_RPC send failed");
if ($e = $r->faultCode ()) {
if ($e == $GLOBALS['XML_RPC_err']['http_error'] && strstr($r->faultString (), '304 Not Modified') !== false ) {
return $this->cache['content'];
return $this->raiseError ($r->faultString (), $e);
$result = XML_RPC_decode ($v);
$this->saveCache ($_args, $result);
// {{{ call_epi(method, [args...])
function call_epi ($method)
} elseif (PHP_OS == 'HP-UX') {
} elseif (PHP_OS == 'AIX') {
return $this->raiseError ("unable to load xmlrpc extension");
$request = xmlrpc_encode_request ($method, $params);
$server_host = $this->config->get ("master_server");
if (empty ($server_host)) {
return $this->raiseError ("PEAR_Remote::call: no master_server configured");
if ($http_proxy = $this->config->get ('http_proxy')) {
$proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
$proxy_host = @$proxy['host'];
$proxy_port = @$proxy['port'];
$fp = @fsockopen($server_host, $server_port);
if (!$fp && $http_proxy) {
return $this->raiseError (" PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed" );
return $this->raiseError (" PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed" );
$req_headers = " Host: $server_host:$server_port\r\n" .
"Content-type: text/xml\r\n" .
" Content-length: $len\r\n";
$username = $this->config->get ('username');
$password = $this->config->get ('password');
if ($username && $password) {
$req_headers .= " Cookie: PEAR_USER=$username; PEAR_PW=$password\r\n";
$req_headers .= " Authorization: Basic $tmp\r\n";
if ($this->cache !== null ) {
$maxAge = '?maxAge='. $this->cache['lastChange'];
if ($use_proxy && $proxy_host != '' && $proxy_user != '') {
$req_headers .= 'Proxy-Authorization: Basic '
if ($this->config->get ('verbose') > 3 ) {
print "XMLRPC REQUEST HEADERS:\n";
print "XMLRPC REQUEST BODY:\n";
if ($use_proxy && $proxy_host != '') {
$post_string = "POST http://". $server_host;
$post_string .= ':'. $server_port;
fwrite($fp, ($post_string." /xmlrpc.php$maxAge HTTP/1.0\r\n$req_headers\r\n$request" ));
$line1 = fgets($fp, 2048 );
if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!', $line1, $matches)) {
return $this->raiseError ("PEAR_Remote: invalid HTTP response from XML-RPC server");
case "304": // Not Modified
return $this->cache['content'];
case "401": // Unauthorized
if ($username && $password) {
return $this->raiseError ("PEAR_Remote: authorization failed", 401 );
return $this->raiseError ("PEAR_Remote: authorization required, please log in first", 401 );
return $this->raiseError ("PEAR_Remote: unexpected HTTP response", (int) $matches[1 ], null , null , " $matches[1] $matches[2]" );
while (trim(fgets($fp, 2048 )) != ''); // skip rest of headers
while ($chunk = fread($fp, 10240 )) {
if ($this->config->get ('verbose') > 3 ) {
print "XMLRPC RESPONSE:\n";
$ret = xmlrpc_decode ($response);
if (is_array($ret) && isset ($ret['__PEAR_TYPE__'])) {
if ($ret['__PEAR_TYPE__'] == 'error') {
if (isset ($ret['__PEAR_CLASS__'])) {
$class = $ret['__PEAR_CLASS__'];
if ($ret['code'] === '') $ret['code'] = null;
if ($ret['message'] === '') $ret['message'] = null;
if ($ret['userinfo'] === '') $ret['userinfo'] = null;
$ret = $this->raiseError (PEAR ::errorMessage ($ret['code']),
$ret['code'], null , null ,
$ret = $this->raiseError ($ret['message'], $ret['code'],
null , null , $ret['userinfo']);
!empty ($ret[0 ]['faultString']) &&
!empty ($ret[0 ]['faultCode'])) {
$faultString = "XML-RPC Server Fault: " .
return $this->raiseError ($faultString, $faultCode);
// a slightly extended version of XML_RPC_encode
function _encode ($php_val)
global $XML_RPC_Boolean, $XML_RPC_Int, $XML_RPC_Double;
global $XML_RPC_String, $XML_RPC_Array, $XML_RPC_Struct;
$xmlrpcval = new XML_RPC_Value;
$firstkey = key($php_val);
$lastkey = key($php_val);
if ($firstkey === 0 && is_int($lastkey) &&
($lastkey + 1 ) == count($php_val)) {
for ($expect = 0; $expect < $size; $expect++ , next($php_val)) {
if (key($php_val) !== $expect) {
while (list ($k, $v) = each($php_val)) {
$arr[$k] = $this->_encode ($v);
$xmlrpcval->addArray ($arr);
// fall though if not numerical and continuous
while (list ($k, $v) = each($php_val)) {
$arr[$k] = $this->_encode ($v);
$xmlrpcval->addStruct ($arr);
$xmlrpcval->addScalar ($php_val, $XML_RPC_Int);
$xmlrpcval->addScalar ($php_val, $XML_RPC_Double);
$xmlrpcval->addScalar ($php_val, $XML_RPC_String);
$xmlrpcval->addScalar ($php_val, $XML_RPC_Boolean);
Documentation generated on Mon, 11 Mar 2019 14:24:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.
|