PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Remote.php

Documentation is available at Remote.php

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Stig Bakken <ssb@php.net>                                    |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Remote.php,v 1.50 2004/06/08 18:03:46 cellog Exp $
  20.  
  21. require_once 'PEAR.php';
  22. require_once 'PEAR/Config.php';
  23.  
  24. /**
  25.  * This is a class for doing remote operations against the central
  26.  * PEAR database.
  27.  *
  28.  * @nodep XML_RPC_Value
  29.  * @nodep XML_RPC_Message
  30.  * @nodep XML_RPC_Client
  31.  */
  32. class PEAR_Remote extends PEAR
  33. {
  34.     // {{{ properties
  35.  
  36.     var $config = null;
  37.     var $cache  = null;
  38.  
  39.     // }}}
  40.  
  41.     // {{{ PEAR_Remote(config_object)
  42.  
  43.     function PEAR_Remote(&$config)
  44.     {
  45.         $this->PEAR();
  46.         $this->config &$config;
  47.     }
  48.  
  49.     // }}}
  50.  
  51.     // {{{ getCache()
  52.  
  53.  
  54.     function getCache($args)
  55.     {
  56.         $id       md5(serialize($args));
  57.         $cachedir $this->config->get('cache_dir');
  58.         $filename $cachedir . DIRECTORY_SEPARATOR . 'xmlrpc_cache_' $id;
  59.         if (!file_exists($filename)) {
  60.             return null;
  61.         };
  62.  
  63.         $fp fopen($filename'rb');
  64.         if (!$fp{
  65.             return null;
  66.         }
  67.         $content  fread($fpfilesize($filename));
  68.         fclose($fp);
  69.         $result   = array(
  70.             'age'        => time(filemtime($filename),
  71.             'lastChange' => filemtime($filename),
  72.             'content'    => unserialize($content),
  73.             );
  74.         return $result;
  75.     }
  76.  
  77.     // }}}
  78.  
  79.     // {{{ saveCache()
  80.  
  81.     function saveCache($args$data)
  82.     {
  83.         $id       md5(serialize($args));
  84.         $cachedir $this->config->get('cache_dir');
  85.         if (!file_exists($cachedir)) {
  86.             System::mkdir(array('-p'$cachedir));
  87.         }
  88.         $filename $cachedir.'/xmlrpc_cache_'.$id;
  89.  
  90.         $fp @fopen($filename"wb");
  91.         if ($fp{
  92.             fwrite($fpserialize($data));
  93.             fclose($fp);
  94.         };
  95.     }
  96.  
  97.     // }}}
  98.  
  99.     // {{{ call(method, [args...])
  100.  
  101.     function call($method)
  102.     {
  103.         $_args $args func_get_args();
  104.  
  105.         $this->cache $this->getCache($args);
  106.         $cachettl $this->config->get('cache_ttl');
  107.         // If cache is newer than $cachettl seconds, we use the cache!
  108.         if ($this->cache !== null && $this->cache['age'$cachettl{
  109.             return $this->cache['content'];
  110.         };
  111.  
  112.         if (extension_loaded("xmlrpc")) {
  113.             $result call_user_func_array(array(&$this'call_epi')$args);
  114.             if (!PEAR::isError($result)) {
  115.                 $this->saveCache($_args$result);
  116.             };
  117.             return $result;
  118.         }
  119.         if (!@include_once("XML/RPC.php")) {
  120.             return $this->raiseError("For this remote PEAR operation you need to install the XML_RPC package");
  121.         }
  122.         array_shift($args);
  123.         $server_host $this->config->get('master_server');
  124.         $username $this->config->get('username');
  125.         $password $this->config->get('password');
  126.         $eargs = array();
  127.         foreach($args as $arg$eargs[$this->_encode($arg);
  128.         $f = new XML_RPC_Message($method$eargs);
  129.         if ($this->cache !== null{
  130.             $maxAge '?maxAge='.$this->cache['lastChange'];
  131.         else {
  132.             $maxAge '';
  133.         };
  134.         $proxy_host $proxy_port $proxy_user $proxy_pass '';
  135.         if ($proxy parse_url($this->config->get('http_proxy'))) {
  136.             $proxy_host @$proxy['host'];
  137.             $proxy_port @$proxy['port'];
  138.             $proxy_user @urldecode(@$proxy['user']);
  139.             $proxy_pass @urldecode(@$proxy['pass']);
  140.         }
  141.         $c = new XML_RPC_Client('/xmlrpc.php'.$maxAge$server_host80$proxy_host$proxy_port$proxy_user$proxy_pass);
  142.         if ($username && $password{
  143.             $c->setCredentials($username$password);
  144.         }
  145.         if ($this->config->get('verbose'>= 3{
  146.             $c->setDebug(1);
  147.         }
  148.         $r $c->send($f);
  149.         if (!$r{
  150.             return $this->raiseError("XML_RPC send failed");
  151.         }
  152.         $v $r->value();
  153.         if ($e $r->faultCode()) {
  154.             if ($e == $GLOBALS['XML_RPC_err']['http_error'&& strstr($r->faultString()'304 Not Modified'!== false{
  155.                 return $this->cache['content'];
  156.             }
  157.             return $this->raiseError($r->faultString()$e);
  158.         }
  159.  
  160.         $result = XML_RPC_decode($v);
  161.         $this->saveCache($_args$result);
  162.         return $result;
  163.     }
  164.  
  165.     // }}}
  166.  
  167.     // {{{ call_epi(method, [args...])
  168.  
  169.     function call_epi($method)
  170.     {
  171.         do {
  172.             if (extension_loaded("xmlrpc")) {
  173.                 break;
  174.             }
  175.             if (OS_WINDOWS{
  176.                 $ext 'dll';
  177.             elseif (PHP_OS == 'HP-UX'{
  178.                 $ext 'sl';
  179.             elseif (PHP_OS == 'AIX'{
  180.                 $ext 'a';
  181.             else {
  182.                 $ext 'so';
  183.             }
  184.             $ext OS_WINDOWS ? 'dll' 'so';
  185.             @dl("xmlrpc-epi.$ext");
  186.             if (extension_loaded("xmlrpc")) {
  187.                 break;
  188.             }
  189.             @dl("xmlrpc.$ext");
  190.             if (extension_loaded("xmlrpc")) {
  191.                 break;
  192.             }
  193.             return $this->raiseError("unable to load xmlrpc extension");
  194.         while (false);
  195.         $params func_get_args();
  196.         array_shift($params);
  197.         $method str_replace("_""."$method);
  198.         $request = xmlrpc_encode_request($method$params);
  199.         $server_host $this->config->get("master_server");
  200.         if (empty($server_host)) {
  201.             return $this->raiseError("PEAR_Remote::call: no master_server configured");
  202.         }
  203.         $server_port = 80;
  204.         if ($http_proxy $this->config->get('http_proxy')) {
  205.             $proxy parse_url($http_proxy);
  206.             $proxy_host $proxy_port $proxy_user $proxy_pass '';
  207.             $proxy_host @$proxy['host'];
  208.             $proxy_port @$proxy['port'];
  209.             $proxy_user @urldecode(@$proxy['user']);
  210.             $proxy_pass @urldecode(@$proxy['pass']);
  211.             $fp @fsockopen($proxy_host$proxy_port);
  212.             $use_proxy = true;
  213.         else {
  214.             $use_proxy = false;
  215.             $fp @fsockopen($server_host$server_port);
  216.         }
  217.         if (!$fp && $http_proxy{
  218.             return $this->raiseError("PEAR_Remote::call: fsockopen(`$proxy_host', $proxy_port) failed");
  219.         elseif (!$fp{
  220.             return $this->raiseError("PEAR_Remote::call: fsockopen(`$server_host', $server_port) failed");
  221.         }
  222.         $len strlen($request);
  223.         $req_headers = "Host: $server_host:$server_port\r\n" .
  224.              "Content-type: text/xml\r\n" .
  225.              "Content-length: $len\r\n";
  226.         $username $this->config->get('username');
  227.         $password $this->config->get('password');
  228.         if ($username && $password{
  229.             $req_headers .= "Cookie: PEAR_USER=$username; PEAR_PW=$password\r\n";
  230.             $tmp base64_encode("$username:$password");
  231.             $req_headers .= "Authorization: Basic $tmp\r\n";
  232.         }
  233.         if ($this->cache !== null{
  234.             $maxAge '?maxAge='.$this->cache['lastChange'];
  235.         else {
  236.             $maxAge '';
  237.         };
  238.  
  239.         if ($use_proxy && $proxy_host != '' && $proxy_user != ''{
  240.             $req_headers .= 'Proxy-Authorization: Basic '
  241.                 .base64_encode($proxy_user.':'.$proxy_pass)
  242.                 ."\r\n";
  243.         }
  244.  
  245.         if ($this->config->get('verbose'> 3{
  246.             print "XMLRPC REQUEST HEADERS:\n";
  247.             var_dump($req_headers);
  248.             print "XMLRPC REQUEST BODY:\n";
  249.             var_dump($request);
  250.         }
  251.  
  252.         if ($use_proxy && $proxy_host != ''{
  253.             $post_string "POST http://".$server_host;
  254.             if ($proxy_port ''{
  255.                 $post_string .= ':'.$server_port;
  256.             }
  257.         else {
  258.             $post_string "POST ";
  259.         }
  260.  
  261.         fwrite($fp($post_string."/xmlrpc.php$maxAge HTTP/1.0\r\n$req_headers\r\n$request"));
  262.         $response '';
  263.         $line1 fgets($fp2048);
  264.         if (!preg_match('!^HTTP/[0-9\.]+ (\d+) (.*)!'$line1$matches)) {
  265.             return $this->raiseError("PEAR_Remote: invalid HTTP response from XML-RPC server");
  266.         }
  267.         switch ($matches[1]{
  268.             case "200"// OK
  269.                 break;
  270.             case "304"// Not Modified
  271.                 return $this->cache['content'];
  272.             case "401"// Unauthorized
  273.                 if ($username && $password{
  274.                     return $this->raiseError("PEAR_Remote: authorization failed"401);
  275.                 else {
  276.                     return $this->raiseError("PEAR_Remote: authorization required, please log in first"401);
  277.                 }
  278.             default:
  279.                 return $this->raiseError("PEAR_Remote: unexpected HTTP response"(int)$matches[1]nullnull"$matches[1] $matches[2]");
  280.         }
  281.         while (trim(fgets($fp2048)) != '')// skip rest of headers
  282.         while ($chunk fread($fp10240)) {
  283.             $response .= $chunk;
  284.         }
  285.         fclose($fp);
  286.         if ($this->config->get('verbose'> 3{
  287.             print "XMLRPC RESPONSE:\n";
  288.             var_dump($response);
  289.         }
  290.         $ret = xmlrpc_decode($response);
  291.         if (is_array($ret&& isset($ret['__PEAR_TYPE__'])) {
  292.             if ($ret['__PEAR_TYPE__'== 'error'{
  293.                 if (isset($ret['__PEAR_CLASS__'])) {
  294.                     $class $ret['__PEAR_CLASS__'];
  295.                 else {
  296.                     $class "PEAR_Error";
  297.                 }
  298.                 if ($ret['code']     === ''$ret['code']     = null;
  299.                 if ($ret['message']  === ''$ret['message']  = null;
  300.                 if ($ret['userinfo'=== ''$ret['userinfo'= null;
  301.                 if (strtolower($class== 'db_error'{
  302.                     $ret $this->raiseError(PEAR::errorMessage($ret['code']),
  303.                                              $ret['code']nullnull,
  304.                                              $ret['userinfo']);
  305.                 else {
  306.                     $ret $this->raiseError($ret['message']$ret['code'],
  307.                                              nullnull$ret['userinfo']);
  308.                 }
  309.             }
  310.         elseif (is_array($ret&& sizeof($ret== 1 && isset($ret[0])
  311.                   && is_array($ret[0]&&
  312.                   !empty($ret[0]['faultString']&&
  313.                   !empty($ret[0]['faultCode'])) {
  314.             extract($ret[0]);
  315.             $faultString "XML-RPC Server Fault: " .
  316.                  str_replace("\n"" "$faultString);
  317.             return $this->raiseError($faultString$faultCode);
  318.         }
  319.         return $ret;
  320.     }
  321.  
  322.     // }}}
  323.  
  324.     // {{{ _encode
  325.  
  326.     // a slightly extended version of XML_RPC_encode
  327.     function _encode($php_val)
  328.     {
  329.         global $XML_RPC_Boolean$XML_RPC_Int$XML_RPC_Double;
  330.         global $XML_RPC_String$XML_RPC_Array$XML_RPC_Struct;
  331.  
  332.         $type gettype($php_val);
  333.         $xmlrpcval = new XML_RPC_Value;
  334.  
  335.         switch($type{
  336.             case "array":
  337.                 reset($php_val);
  338.                 $firstkey key($php_val);
  339.                 end($php_val);
  340.                 $lastkey key($php_val);
  341.                 if ($firstkey === 0 && is_int($lastkey&&
  342.                     ($lastkey + 1== count($php_val)) {
  343.                     $is_continuous = true;
  344.                     reset($php_val);
  345.                     $size count($php_val);
  346.                     for ($expect = 0; $expect $size$expect++next($php_val)) {
  347.                         if (key($php_val!== $expect{
  348.                             $is_continuous = false;
  349.                             break;
  350.                         }
  351.                     }
  352.                     if ($is_continuous{
  353.                         reset($php_val);
  354.                         $arr = array();
  355.                         while (list($k$veach($php_val)) {
  356.                             $arr[$k$this->_encode($v);
  357.                         }
  358.                         $xmlrpcval->addArray($arr);
  359.                         break;
  360.                     }
  361.                 }
  362.                 // fall though if not numerical and continuous
  363.             case "object":
  364.                 $arr = array();
  365.                 while (list($k$veach($php_val)) {
  366.                     $arr[$k$this->_encode($v);
  367.                 }
  368.                 $xmlrpcval->addStruct($arr);
  369.                 break;
  370.             case "integer":
  371.                 $xmlrpcval->addScalar($php_val$XML_RPC_Int);
  372.                 break;
  373.             case "double":
  374.                 $xmlrpcval->addScalar($php_val$XML_RPC_Double);
  375.                 break;
  376.             case "string":
  377.             case "NULL":
  378.                 $xmlrpcval->addScalar($php_val$XML_RPC_String);
  379.                 break;
  380.             case "boolean":
  381.                 $xmlrpcval->addScalar($php_val$XML_RPC_Boolean);
  382.                 break;
  383.             case "unknown type":
  384.             default:
  385.                 return null;
  386.         }
  387.         return $xmlrpcval;
  388.     }
  389.  
  390.     // }}}
  391.  
  392. }
  393.  
  394. ?>

Documentation generated on Mon, 11 Mar 2019 14:24:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.