home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / xampp-win32-1.6.7-installer.exe / php / PEAR / XML / RPC2 / Client.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  10.1 KB  |  298 lines

  1. <?php
  2.  
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
  4.  
  5. // LICENSE AGREEMENT. If folded, press za here to unfold and read license {{{ 
  6.  
  7. /**
  8. * +-----------------------------------------------------------------------------+
  9. * | Copyright (c) 2004-2006 Sergio Goncalves Carvalho                                |
  10. * +-----------------------------------------------------------------------------+
  11. * | This file is part of XML_RPC2.                                              |
  12. * |                                                                             |
  13. * | XML_RPC2 is free software; you can redistribute it and/or modify            |
  14. * | it under the terms of the GNU Lesser General Public License as published by |
  15. * | the Free Software Foundation; either version 2.1 of the License, or         |
  16. * | (at your option) any later version.                                         |
  17. * |                                                                             |
  18. * | XML_RPC2 is distributed in the hope that it will be useful,                 |
  19. * | but WITHOUT ANY WARRANTY; without even the implied warranty of              |
  20. * | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               |
  21. * | GNU Lesser General Public License for more details.                         |
  22. * |                                                                             |
  23. * | You should have received a copy of the GNU Lesser General Public License    |
  24. * | along with XML_RPC2; if not, write to the Free Software                     |
  25. * | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                    |
  26. * | 02111-1307 USA                                                              |
  27. * +-----------------------------------------------------------------------------+
  28. * | Author: Sergio Carvalho <sergio.carvalho@portugalmail.com>                  |
  29. * +-----------------------------------------------------------------------------+
  30. *
  31. * @category   XML
  32. * @package    XML_RPC2
  33. * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  34. * @copyright  2004-2006 Sergio Carvalho
  35. * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  36. * @version    CVS: $Id: Client.php,v 1.13 2007/07/30 08:47:11 sergiosgc Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Exception.php';
  44. require_once 'XML/RPC2/Backend.php';
  45. // }}}
  46.  
  47. /**
  48.  * XML_RPC client class. Use this class to access remote methods.
  49.  * 
  50.  * To use this class, construct it providing the server URL and method prefix. 
  51.  * Then, call remote methods on the new instance as if they were local.
  52.  * 
  53.  * Example:
  54.  * <code>
  55.  *  require_once 'XML_RPC2/Client.php';
  56.  * 
  57.  *  $client = XML_RPC2_Client('http://xmlrpc.example.com/1.0/', 'example.');
  58.  *  $result = $client->hello('Sergio');
  59.  *  print($result);
  60.  * </code>
  61.  * 
  62.  * The above example will call the example.hello method on the xmlrpc.example.com
  63.  * server, under the /1.0/ URI. 
  64.  * 
  65.  * @category   XML
  66.  * @package    XML_RPC2
  67.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  68.  * @copyright  2004-2006 Sergio Carvalho
  69.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  70.  * @link       http://pear.php.net/package/XML_RPC2
  71.  */
  72. abstract class XML_RPC2_Client 
  73. {
  74.  
  75.     // {{{ properties
  76.     
  77.     /**
  78.      * uri Field (holds the uri for the XML_RPC server)
  79.      *
  80.      * @var string
  81.      */
  82.     protected $uri = null;
  83.     
  84.     /**
  85.      * proxy Field (holds the proxy server data)
  86.      *
  87.      * @var string
  88.      */
  89.     protected $proxy = null;
  90.     
  91.     /**
  92.      * Holds the prefix to prepend to method names
  93.      *
  94.      * @var string
  95.      */
  96.     protected $prefix = null;
  97.     
  98.     /** 
  99.      * Holds the debug flag 
  100.      *
  101.      * @var boolean
  102.      */
  103.     protected $debug = false;
  104.     
  105.     /**
  106.      * Hold the encoding of the client request
  107.      *
  108.      * @var string
  109.      */
  110.     protected $encoding = 'iso-8859-1';
  111.     
  112.     /**
  113.      * Hold the SSL verify flag
  114.      *
  115.      * @var boolean
  116.      */
  117.     protected $sslverify = true;
  118.         
  119.     // }}}
  120.     // {{{ remoteCall___()
  121.     
  122.     /**
  123.      * ugly hack flag to avoid http://bugs.php.net/bug.php?id=21949
  124.      * 
  125.      * see XML_RPC2_Backend_Xmlrpcext_Value::createFromNative() from more infos
  126.      */
  127.     protected $uglyStructHack = true;
  128.     
  129.     /**
  130.      * remoteCall executes the XML-RPC call, and returns the result
  131.      * 
  132.      * NB : The '___' at the end of the method name is to avoid collisions with
  133.      * XMLRPC __call() 
  134.      *
  135.      * @param   string      Method name
  136.      * @param   array       Parameters
  137.      */
  138.     public abstract function remoteCall___($methodName, $parameters);
  139.     
  140.     // }}}
  141.     // {{{ constructor
  142.     
  143.     /**
  144.      * Construct a new XML_RPC2_Client.
  145.      *
  146.      * To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/). 
  147.      * Optionally, some options may be set as an associative array. Accepted keys are :
  148.      * 'prefix', 'proxy', 'debug' => see correspondant property to get more informations
  149.      * 
  150.      * @param string URI for the XML-RPC server
  151.      * @param array (optional) Associative array of options
  152.      */
  153.     protected function __construct($uri, $options = array())
  154.     {
  155.         if (!$uriParse = parse_url($uri)) {
  156.             throw new XML_RPC2_InvalidUriException(sprintf('Client URI \'%s\' is not valid', $uri));
  157.         }
  158.         $this->uri = $uri;
  159.         if (isset($options['prefix'])) {
  160.             if (!($this->testMethodName___($options['prefix']))) {
  161.                 throw new XML_RPC2_InvalidPrefixException(sprintf('Prefix \'%s\' is not valid', $options['prefix']));
  162.             }
  163.             $this->prefix = $options['prefix'];
  164.         }
  165.         if (isset($options['proxy'])) {
  166.             if (!$proxyParse = parse_url($options['proxy'])) {
  167.                 throw new XML_RPC2_InvalidProxyException(sprintf('Proxy URI \'%s\' is not valid', $options['proxy']));
  168.             }
  169.             $this->proxy = $options['proxy'];
  170.         }
  171.         if (isset($options['debug'])) {
  172.             if (!(is_bool($options['debug']))) {
  173.                 throw new XML_RPC2_InvalidDebugException(sprintf('Debug \'%s\' is not valid', $options['debug']));
  174.             }
  175.             $this->debug = $options['debug'];
  176.         }
  177.         if (isset($options['encoding'])) {
  178.             // TODO : control & exception
  179.             $this->encoding = $options['encoding'];
  180.         }
  181.         if (isset($options['uglyStructHack'])) {  
  182.             $this->uglyStructHack = $options['uglyStructHack'];
  183.         }
  184.         if (isset($options['sslverify'])) {
  185.             if (!(is_bool($options['sslverify']))) {
  186.                 throw new XML_RPC2_InvalidSslverifyException(sprintf('SSL verify \'%s\' is not valid', $options['sslverify']));
  187.             }
  188.             $this->sslverify = $options['sslverify'];
  189.         }
  190.     }
  191.     
  192.     // }}}
  193.     // {{{ create()
  194.     
  195.     /**
  196.      * Factory method to select, create and return a XML_RPC2_Client backend
  197.      *
  198.      * To create a new XML_RPC2_Client, a URI must be provided (e.g. http://xmlrpc.example.com/1.0/). 
  199.      * 
  200.      * Optionally, some options may be set.
  201.      *
  202.      * @param string URI for the XML-RPC server
  203.      * @param array (optional) associative array of options (see constructor)
  204.      */
  205.     public static function create($uri, $options = array())
  206.     {
  207.         if (isset($options['backend'])) {
  208.             XML_RPC2_Backend::setBackend($options['backend']);
  209.         }
  210.         $backend = XML_RPC2_Backend::getClientClassname();
  211.         return new $backend($uri, $options);
  212.     }
  213.     
  214.     // }}}
  215.     // {{{ __call()
  216.  
  217.     /**
  218.      * __call Catchall. This method catches remote method calls and provides for remote forwarding.
  219.      *
  220.      * If the parameters are native types, this method will use XML_RPC_Value::createFromNative to 
  221.      * convert it into an XML-RPC type. Whenever a parameter is already an instance of XML_RPC_Value
  222.      * it will be used as provided. It follows that, in situations when XML_RPC_Value::createFromNative
  223.      * proves inacurate -- as when encoding DateTime values -- you should present an instance of 
  224.      * XML_RPC_Value in lieu of the native parameter.
  225.      *
  226.      * @param   string      Method name
  227.      * @param   array       Parameters
  228.      * @return  mixed       The call result, already decoded into native types
  229.      */
  230.     public function __call($methodName, $parameters)
  231.     {
  232.         $args = array($methodName, $parameters);
  233.         return @call_user_func_array(array($this, 'remoteCall___'), $args);
  234.     }
  235.    
  236.     // }}}
  237.     // {{{ displayDebugInformations___()
  238.     
  239.     /**
  240.      * Display debug informations
  241.      *
  242.      * NB : The '___' at the end of the method name is to avoid collisions with
  243.      * XMLRPC __call() 
  244.      * 
  245.      * @param string $request XML client request
  246.      * @param string $body XML server response
  247.      */
  248.     protected function displayDebugInformations___($request, $body) 
  249.     {
  250.         print '<pre>';
  251.         print "***** Request *****\n";
  252.         print htmlspecialchars($request);
  253.         print "***** End Of request *****\n\n";
  254.         print "***** Server response *****\n";
  255.         print htmlspecialchars($body);
  256.         print "\n***** End of server response *****\n\n";
  257.     }
  258.     
  259.     // }}}
  260.     // {{{ displayDebugInformations2___()
  261.     
  262.     /**
  263.      * Display debug informations (part 2)
  264.      *
  265.      * NB : The '___' at the end of the method name is to avoid collisions with
  266.      * XMLRPC __call() 
  267.      * 
  268.      * @param mixed $result decoded server response
  269.      */
  270.     protected function displayDebugInformations2___($result)
  271.     {
  272.         print "***** Decoded result *****\n";
  273.         print_r($result);
  274.         print "\n***** End of decoded result *****";
  275.         print '</pre>';
  276.     }
  277.     
  278.     // }}}
  279.     // {{{ testMethodName___()
  280.     
  281.     /**
  282.      * Return true is the given method name is ok with XML/RPC spec. 
  283.      *
  284.      * NB : The '___' at the end of the method name is to avoid collisions with
  285.      * XMLRPC __call() 
  286.      * 
  287.      * @param string $methodName method name
  288.      * @return boolean true if ok
  289.      */
  290.     protected function testMethodName___($methodName)
  291.     {
  292.         return (preg_match('~^[a-zA-Z0-9_.:/]*$~', $methodName)); 
  293.     }
  294.         
  295. }
  296.  
  297. ?>
  298.