From: Tomas V.V.Cox Date: Sat, 12 Jul 2003 14:25:06 +0000 (+0000) Subject: Added proxy support for remote commands using the xmlrcp C ext X-Git-Tag: BEFORE_ARG_INFO~272 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4e268d9270f25a36690cbb182b223c35b4726cc3;p=php Added proxy support for remote commands using the xmlrcp C ext (contribution by Adam Ashley ) --- diff --git a/pear/PEAR/Remote.php b/pear/PEAR/Remote.php index b7e34911e3..21399db849 100644 --- a/pear/PEAR/Remote.php +++ b/pear/PEAR/Remote.php @@ -50,7 +50,7 @@ class PEAR_Remote extends PEAR // {{{ getCache() - + function getCache($args) { $id = md5(serialize($args)); @@ -62,7 +62,7 @@ class PEAR_Remote extends PEAR if (!file_exists($filename)) { return null; }; - + $fp = fopen($filename, "rb"); if ($fp === null) { return null; @@ -78,7 +78,7 @@ class PEAR_Remote extends PEAR } // }}} - + // {{{ saveCache() function saveCache($args, $data) @@ -89,7 +89,7 @@ class PEAR_Remote extends PEAR System::mkdir('-p '.$cachedir); } $filename = $cachedir.'/xmlrpc_cache_'.$id; - + $fp = @fopen($filename, "wb"); if ($fp !== null) { fwrite($fp, serialize($data)); @@ -98,20 +98,20 @@ class PEAR_Remote extends PEAR } // }}} - + // {{{ call(method, [args...]) function call($method) { $_args = $args = func_get_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 (extension_loaded("xmlrpc")) { $result = call_user_func_array(array(&$this, 'call_epi'), $args); if (!PEAR::isError($result)) { @@ -140,7 +140,7 @@ class PEAR_Remote extends PEAR $proxy_port = @$proxy['port']; $proxy_user = @$proxy['user']; $proxy_pass = @$proxy['pass']; - } + } $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); @@ -204,8 +204,21 @@ class PEAR_Remote extends PEAR return $this->raiseError("PEAR_Remote::call: no master_server configured"); } $server_port = 80; - $fp = @fsockopen($server_host, $server_port); - if (!$fp) { + $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']; + $proxy_user = @$proxy['user']; + $proxy_pass = @$proxy['pass']; + } + if ($proxy != '') { + $fp = @fsockopen($proxy_host, $proxy_port); + } else { + $fp = @fsockopen($server_host, $server_port); + } + if (!$fp && $proxy_host != '') { + return $this->raiseError("PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed"); + } elseif (!$fp) { return $this->raiseError("PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed"); } $len = strlen($request); @@ -224,15 +237,30 @@ class PEAR_Remote extends PEAR } else { $maxAge = ''; }; - + + if ($proxy_host != '' && $proxy_user != '') { + $req_headers .= 'Proxy-Authorization: Basic ' + .base64_encode($proxy_user.':'.$proxy_pass) + ."\r\n"; + } + if ($this->config->get('verbose') > 3) { print "XMLRPC REQUEST HEADERS:\n"; var_dump($req_headers); print "XMLRPC REQUEST BODY:\n"; var_dump($request); } - - fwrite($fp, ("POST /xmlrpc.php$maxAge HTTP/1.0\r\n$req_headers\r\n$request")); + + if ($proxy_host != '') { + $post_string = "POST http://".$server_host; + if ($proxy_port > '') { + $post_string .= ':'.$server_port; + } + } else { + $post_string = "POST "; + } + + fwrite($fp, ($post_string."/xmlrpc.php$maxAge HTTP/1.0\r\n$req_headers\r\n$request")); $response = ''; $line1 = fgets($fp, 2048); if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!', $line1, $matches)) {