5 * PHP versions 4 and 5
\r
7 * LICENSE: This source file is subject to version 3.0 of the PHP license
\r
8 * that is available through the world-wide-web at the following URI:
\r
9 * http://www.php.net/license/3_0.txt. If you did not receive a copy of
\r
10 * the PHP License and are unable to obtain it through the web, please
\r
11 * send a note to license@php.net so we can mail you a copy immediately.
\r
15 * @author Stig Bakken <ssb@php.net>
\r
16 * @author Greg Beaver <cellog@php.net>
\r
17 * @copyright 1997-2008 The PHP Group
\r
18 * @license http://www.php.net/license/3_0.txt PHP License 3.0
\r
19 * @version CVS: $Id: Remote.php 7208 2008-01-09 16:07:24Z greg $
\r
20 * @link http://pear.php.net/package/PEAR
\r
21 * @since File available since Release 0.1
\r
25 * needed for PEAR_Error
\r
27 require_once 'PEAR.php';
\r
28 require_once 'PEAR/Config.php';
\r
31 * This is a class for doing remote operations against the central
\r
34 * @nodep XML_RPC_Value
\r
35 * @nodep XML_RPC_Message
\r
36 * @nodep XML_RPC_Client
\r
39 * @author Stig Bakken <ssb@php.net>
\r
40 * @author Greg Beaver <cellog@php.net>
\r
41 * @copyright 1997-2008 The PHP Group
\r
42 * @license http://www.php.net/license/3_0.txt PHP License 3.0
\r
43 * @version Release: 1.4.4
\r
44 * @link http://pear.php.net/package/PEAR
\r
45 * @since Class available since Release 0.1
\r
47 class PEAR_Remote extends PEAR
\r
54 * @var PEAR_Registry
\r
61 // {{{ PEAR_Remote(config_object)
\r
63 function PEAR_Remote(&$config)
\r
66 $this->config = &$config;
\r
67 $this->_registry = &$this->config->getRegistry();
\r
71 // {{{ setRegistry()
\r
73 function setRegistry(&$reg)
\r
75 $this->_registry = &$reg;
\r
81 function getCache($args)
\r
83 $id = md5(serialize($args));
\r
84 $cachedir = $this->config->get('cache_dir');
\r
85 $filename = $cachedir . DIRECTORY_SEPARATOR . 'xmlrpc_cache_' . $id;
\r
86 if (!file_exists($filename)) {
\r
90 $fp = fopen($filename, 'rb');
\r
94 if (function_exists('file_get_contents')) {
\r
96 $content = file_get_contents($filename);
\r
98 $content = fread($fp, filesize($filename));
\r
102 'age' => time() - filemtime($filename),
\r
103 'lastChange' => filemtime($filename),
\r
104 'content' => unserialize($content),
\r
113 function saveCache($args, $data)
\r
115 $id = md5(serialize($args));
\r
116 $cachedir = $this->config->get('cache_dir');
\r
117 if (!file_exists($cachedir)) {
\r
118 System::mkdir(array('-p', $cachedir));
\r
120 $filename = $cachedir.'/xmlrpc_cache_'.$id;
\r
122 $fp = @fopen($filename, "wb");
\r
124 fwrite($fp, serialize($data));
\r
131 // {{{ clearCache()
\r
133 function clearCache($method, $args)
\r
135 array_unshift($args, $method);
\r
136 array_unshift($args, $this->config->get('default_channel')); // cache by channel
\r
137 $id = md5(serialize($args));
\r
138 $cachedir = $this->config->get('cache_dir');
\r
139 $filename = $cachedir.'/xmlrpc_cache_'.$id;
\r
140 if (file_exists($filename)) {
\r
141 @unlink($filename);
\r
146 // {{{ call(method, [args...])
\r
148 function call($method)
\r
150 $_args = $args = func_get_args();
\r
152 $server_channel = $this->config->get('default_channel');
\r
153 $channel = $this->_registry->getChannel($server_channel);
\r
155 $mirror = $this->config->get('preferred_mirror');
\r
156 if ($channel->getMirror($mirror)) {
\r
157 if ($channel->supports('xmlrpc', $method, $mirror)) {
\r
158 $server_channel = $server_host = $mirror; // use the preferred mirror
\r
159 $server_port = $channel->getPort($mirror);
\r
160 } elseif (!$channel->supports('xmlrpc', $method)) {
\r
161 return $this->raiseError("Channel $server_channel does not " .
\r
162 "support xml-rpc method $method");
\r
165 if (!isset($server_host)) {
\r
166 if (!$channel->supports('xmlrpc', $method)) {
\r
167 return $this->raiseError("Channel $server_channel does not support " .
\r
168 "xml-rpc method $method");
\r
170 $server_host = $server_channel;
\r
171 $server_port = $channel->getPort();
\r
175 return $this->raiseError("Unknown channel '$server_channel'");
\r
178 array_unshift($_args, $server_channel); // cache by channel
\r
179 $this->cache = $this->getCache($_args);
\r
180 $cachettl = $this->config->get('cache_ttl');
\r
181 // If cache is newer than $cachettl seconds, we use the cache!
\r
182 if ($this->cache !== null && $this->cache['age'] < $cachettl) {
\r
183 return $this->cache['content'];
\r
185 if (extension_loaded("xmlrpc")) {
\r
186 $result = call_user_func_array(array(&$this, 'call_epi'), $args);
\r
187 if (!PEAR::isError($result)) {
\r
188 $this->saveCache($_args, $result);
\r
191 } elseif (!@include_once 'XML/RPC.php') {
\r
192 return $this->raiseError("For this remote PEAR operation you need to load the xmlrpc extension or install XML_RPC");
\r
195 array_shift($args);
\r
196 $username = $this->config->get('username');
\r
197 $password = $this->config->get('password');
\r
199 foreach($args as $arg) {
\r
200 $eargs[] = $this->_encode($arg);
\r
202 $f = new XML_RPC_Message($method, $eargs);
\r
203 if ($this->cache !== null) {
\r
204 $maxAge = '?maxAge='.$this->cache['lastChange'];
\r
208 $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
\r
209 if ($proxy = parse_url($this->config->get('http_proxy'))) {
\r
210 $proxy_host = @$proxy['host'];
\r
211 if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') {
\r
212 $proxy_host = 'https://' . $proxy_host;
\r
214 $proxy_port = @$proxy['port'];
\r
215 $proxy_user = @urldecode(@$proxy['user']);
\r
216 $proxy_pass = @urldecode(@$proxy['pass']);
\r
218 $shost = $server_host;
\r
219 if ($channel->getSSL()) {
\r
220 $shost = "https://$shost";
\r
222 $c = new XML_RPC_Client('/' . $channel->getPath('xmlrpc')
\r
223 . $maxAge, $shost, $server_port, $proxy_host, $proxy_port,
\r
224 $proxy_user, $proxy_pass);
\r
225 if ($username && $password) {
\r
226 $c->setCredentials($username, $password);
\r
228 if ($this->config->get('verbose') >= 3) {
\r
233 return $this->raiseError("XML_RPC send failed");
\r
236 if ($e = $r->faultCode()) {
\r
237 if ($e == $GLOBALS['XML_RPC_err']['http_error'] && strstr($r->faultString(), '304 Not Modified') !== false) {
\r
238 return $this->cache['content'];
\r
240 return $this->raiseError($r->faultString(), $e);
\r
243 $result = XML_RPC_decode($v);
\r
244 $this->saveCache($_args, $result);
\r
250 // {{{ call_epi(method, [args...])
\r
252 function call_epi($method)
\r
255 if (extension_loaded("xmlrpc")) {
\r
260 } elseif (PHP_OS == 'HP-UX') {
\r
262 } elseif (PHP_OS == 'AIX') {
\r
267 $ext = OS_WINDOWS ? 'dll' : 'so';
\r
268 @dl("xmlrpc-epi.$ext");
\r
269 if (extension_loaded("xmlrpc")) {
\r
272 @dl("xmlrpc.$ext");
\r
273 if (extension_loaded("xmlrpc")) {
\r
276 return $this->raiseError("unable to load xmlrpc extension");
\r
278 $server_channel = $this->config->get('default_channel');
\r
279 $channel = $this->_registry->getChannel($server_channel);
\r
281 $mirror = $this->config->get('preferred_mirror');
\r
282 if ($channel->getMirror($mirror)) {
\r
283 if ($channel->supports('xmlrpc', $method, $mirror)) {
\r
284 $server_channel = $server_host = $mirror; // use the preferred mirror
\r
285 $server_port = $channel->getPort($mirror);
\r
286 } elseif (!$channel->supports('xmlrpc', $method)) {
\r
287 return $this->raiseError("Channel $server_channel does not " .
\r
288 "support xml-rpc method $method");
\r
291 if (!isset($server_host)) {
\r
292 if (!$channel->supports('xmlrpc', $method)) {
\r
293 return $this->raiseError("Channel $server_channel does not support " .
\r
294 "xml-rpc method $method");
\r
296 $server_host = $server_channel;
\r
297 $server_port = $channel->getPort();
\r
301 return $this->raiseError("Unknown channel '$server_channel'");
\r
303 $params = func_get_args();
\r
304 array_shift($params);
\r
305 $method = str_replace("_", ".", $method);
\r
306 $request = xmlrpc_encode_request($method, $params);
\r
307 if ($http_proxy = $this->config->get('http_proxy')) {
\r
308 $proxy = parse_url($http_proxy);
\r
309 $proxy_host = $proxy_port = $proxy_user = $proxy_pass = '';
\r
310 $proxy_host = @$proxy['host'];
\r
311 if (isset($proxy['scheme']) && $proxy['scheme'] == 'https') {
\r
312 $proxy_host = 'ssl://' . $proxy_host;
\r
314 $proxy_port = @$proxy['port'];
\r
315 $proxy_user = @urldecode(@$proxy['user']);
\r
316 $proxy_pass = @urldecode(@$proxy['pass']);
\r
317 $fp = @fsockopen($proxy_host, $proxy_port);
\r
319 if ($channel->getSSL()) {
\r
320 $server_host = "https://$server_host";
\r
323 $use_proxy = false;
\r
324 $ssl = $channel->getSSL();
\r
325 $fp = @fsockopen(($ssl ? 'ssl://' : '') . $server_host, $server_port);
\r
327 $server_host = "$ssl$server_host"; // for error-reporting
\r
330 if (!$fp && $http_proxy) {
\r
331 return $this->raiseError("PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed");
\r
333 return $this->raiseError("PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed");
\r
335 $len = strlen($request);
\r
336 $req_headers = "Host: $server_host:$server_port\r\n" .
\r
337 "Content-type: text/xml\r\n" .
\r
338 "Content-length: $len\r\n";
\r
339 $username = $this->config->get('username');
\r
340 $password = $this->config->get('password');
\r
341 if ($username && $password) {
\r
342 $req_headers .= "Cookie: PEAR_USER=$username; PEAR_PW=$password\r\n";
\r
343 $tmp = base64_encode("$username:$password");
\r
344 $req_headers .= "Authorization: Basic $tmp\r\n";
\r
346 if ($this->cache !== null) {
\r
347 $maxAge = '?maxAge='.$this->cache['lastChange'];
\r
352 if ($use_proxy && $proxy_host != '' && $proxy_user != '') {
\r
353 $req_headers .= 'Proxy-Authorization: Basic '
\r
354 .base64_encode($proxy_user.':'.$proxy_pass)
\r
358 if ($this->config->get('verbose') > 3) {
\r
359 print "XMLRPC REQUEST HEADERS:\n";
\r
360 var_dump($req_headers);
\r
361 print "XMLRPC REQUEST BODY:\n";
\r
362 var_dump($request);
\r
365 if ($use_proxy && $proxy_host != '') {
\r
366 $post_string = "POST http://".$server_host;
\r
367 if ($proxy_port > '') {
\r
368 $post_string .= ':'.$server_port;
\r
371 $post_string = "POST ";
\r
374 $path = '/' . $channel->getPath('xmlrpc');
\r
375 fwrite($fp, ($post_string . $path . "$maxAge HTTP/1.0\r\n$req_headers\r\n$request"));
\r
377 $line1 = fgets($fp, 2048);
\r
378 if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!', $line1, $matches)) {
\r
379 return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server");
\r
381 switch ($matches[1]) {
\r
384 case "304": // Not Modified
\r
385 return $this->cache['content'];
\r
386 case "401": // Unauthorized
\r
387 if ($username && $password) {
\r
388 return $this->raiseError("PEAR_Remote ($server_host:$server_port) " .
\r
389 ": authorization failed", 401);
\r
391 return $this->raiseError("PEAR_Remote ($server_host:$server_port) " .
\r
392 ": authorization required, please log in first", 401);
\r
395 return $this->raiseError("PEAR_Remote ($server_host:$server_port) : " .
\r
396 "unexpected HTTP response", (int)$matches[1], null, null,
\r
397 "$matches[1] $matches[2]");
\r
399 while (trim(fgets($fp, 2048)) != ''); // skip rest of headers
\r
400 while ($chunk = fread($fp, 10240)) {
\r
401 $response .= $chunk;
\r
404 if ($this->config->get('verbose') > 3) {
\r
405 print "XMLRPC RESPONSE:\n";
\r
406 var_dump($response);
\r
408 $ret = xmlrpc_decode($response);
\r
409 if (is_array($ret) && isset($ret['__PEAR_TYPE__'])) {
\r
410 if ($ret['__PEAR_TYPE__'] == 'error') {
\r
411 if (isset($ret['__PEAR_CLASS__'])) {
\r
412 $class = $ret['__PEAR_CLASS__'];
\r
414 $class = "PEAR_Error";
\r
416 if ($ret['code'] === '') $ret['code'] = null;
\r
417 if ($ret['message'] === '') $ret['message'] = null;
\r
418 if ($ret['userinfo'] === '') $ret['userinfo'] = null;
\r
419 if (strtolower($class) == 'db_error') {
\r
420 $ret = $this->raiseError(PEAR::errorMessage($ret['code']),
\r
421 $ret['code'], null, null,
\r
424 $ret = $this->raiseError($ret['message'], $ret['code'],
\r
425 null, null, $ret['userinfo']);
\r
428 } elseif (is_array($ret) && sizeof($ret) == 1 && isset($ret[0])
\r
429 && is_array($ret[0]) &&
\r
430 !empty($ret[0]['faultString']) &&
\r
431 !empty($ret[0]['faultCode'])) {
\r
433 $faultString = "XML-RPC Server Fault: " .
\r
434 str_replace("\n", " ", $faultString);
\r
435 return $this->raiseError($faultString, $faultCode);
\r
436 } elseif (is_array($ret) && sizeof($ret) == 2 && !empty($ret['faultString']) &&
\r
437 !empty($ret['faultCode'])) {
\r
439 $faultString = "XML-RPC Server Fault: " .
\r
440 str_replace("\n", " ", $faultString);
\r
441 return $this->raiseError($faultString, $faultCode);
\r
450 // a slightly extended version of XML_RPC_encode
\r
451 function _encode($php_val)
\r
453 global $XML_RPC_Boolean, $XML_RPC_Int, $XML_RPC_Double;
\r
454 global $XML_RPC_String, $XML_RPC_Array, $XML_RPC_Struct;
\r
456 $type = gettype($php_val);
\r
457 $xmlrpcval = new XML_RPC_Value;
\r
462 $firstkey = key($php_val);
\r
464 $lastkey = key($php_val);
\r
466 if ($firstkey === 0 && is_int($lastkey) &&
\r
467 ($lastkey + 1) == count($php_val)) {
\r
468 $is_continuous = true;
\r
470 $size = count($php_val);
\r
471 for ($expect = 0; $expect < $size; $expect++, next($php_val)) {
\r
472 if (key($php_val) !== $expect) {
\r
473 $is_continuous = false;
\r
477 if ($is_continuous) {
\r
480 while (list($k, $v) = each($php_val)) {
\r
481 $arr[$k] = $this->_encode($v);
\r
483 $xmlrpcval->addArray($arr);
\r
487 // fall though if not numerical and continuous
\r
490 while (list($k, $v) = each($php_val)) {
\r
491 $arr[$k] = $this->_encode($v);
\r
493 $xmlrpcval->addStruct($arr);
\r
496 $xmlrpcval->addScalar($php_val, $XML_RPC_Int);
\r
499 $xmlrpcval->addScalar($php_val, $XML_RPC_Double);
\r
503 $xmlrpcval->addScalar($php_val, $XML_RPC_String);
\r
506 $xmlrpcval->addScalar($php_val, $XML_RPC_Boolean);
\r
508 case "unknown type":
\r