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 / Backend / Xmlrpcext / Server.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  7.5 KB  |  178 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 Gonalves 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: Server.php,v 1.10 2006/12/02 18:09:49 sergiosgc Exp $
  37. * @link       http://pear.php.net/package/XML_RPC2
  38. */
  39.  
  40. // }}}
  41.  
  42. // dependencies {{{
  43. require_once 'XML/RPC2/Backend/Php/Request.php';
  44. require_once 'XML/RPC2/Backend/Php/Response.php';
  45. require_once 'XML/RPC2/Exception.php';
  46. // }}}
  47.  
  48. /**
  49.  * XML_RPC server class XMLRPCext extension-based backend
  50.  * 
  51.  * The XML_RPC2_Server does the work of decoding and encoding xml-rpc request and response. The actual
  52.  * method execution is delegated to the call handler instance.
  53.  *
  54.  * The XML_RPC server is responsible for decoding the request and calling the appropriate method in the
  55.  * call handler class. It then encodes the result into an XML-RPC response and returns it to the client.
  56.  *
  57.  * @category   XML
  58.  * @package    XML_RPC2
  59.  * @author     Sergio Carvalho <sergio.carvalho@portugalmail.com>  
  60.  * @copyright  2004-2006 Sergio Carvalho
  61.  * @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
  62.  * @link       http://pear.php.net/package/XML_RPC2
  63.  */
  64. class XML_RPC2_Backend_Xmlrpcext_Server extends XML_RPC2_Server
  65. {
  66.     
  67.     // {{{ properties
  68.     
  69.     /** 
  70.      * xmlrpcext server
  71.      *
  72.      * @var resource
  73.      */
  74.     private $_xmlrpcextServer;
  75.        
  76.     // }}}
  77.     // {{{ constructor
  78.     
  79.     /**
  80.      * Create a new XML-RPC Server. 
  81.      *
  82.      * The constructor receives a mandatory parameter: the Call Handler. The call handler executes the actual
  83.      * method call. XML_RPC2 server acts as a protocol decoder/encoder between the call handler and the client
  84.      *
  85.      * @param object $callHandler
  86.      * @param array $options associative array of options
  87.      */
  88.     function __construct($callHandler, $options = array())
  89.     {
  90.         parent::__construct($callHandler, $options);
  91.         $this->_xmlrpcextServer = xmlrpc_server_create();
  92.         foreach ($callHandler->getMethods() as $method) {
  93.             if (xmlrpc_server_register_method($this->_xmlrpcextServer, 
  94.                                               $method->getName(), 
  95.                                               array($this, 'epiFunctionHandlerAdapter')) !== true) {
  96.                 throw new XML_RPC2_Exception('Unable to setup XMLRPCext server. xmlrpc_server_register_method returned non-true.');
  97.             }
  98.         }
  99.     }
  100.     
  101.     // }}}
  102.     // {{{ epiFunctionHandlerAdapter()
  103.     
  104.     /**
  105.      * This is an adapter between XML_RPC2_CallHandler::__call and xmlrpc_server_register_method callback interface
  106.      * 
  107.      * @param string Method name
  108.      * @param array Parameters
  109.      * @param array Application data (ignored)
  110.      */
  111.     protected function epiFunctionHandlerAdapter($method_name, $params, $app_data) {
  112.         return @call_user_func_array(array($this->callHandler, $method_name), $params);
  113.     }
  114.     
  115.     // }}}
  116.     // {{{ handleCall()
  117.     
  118.     /**
  119.      * Respond to the XML-RPC request.
  120.      *
  121.      * handleCall reads the XML-RPC request from the raw HTTP body and decodes it. It then calls the 
  122.      * corresponding method in the call handler class, returning the encoded result to the client.
  123.      */
  124.     public function handleCall()
  125.     {
  126.         if ((!($this->autoDocument)) or ((isset($GLOBALS['HTTP_RAW_POST_DATA'])) && (strlen($GLOBALS['HTTP_RAW_POST_DATA'])>0))) {
  127.             $response = $this->getResponse();
  128.             header('Content-type: text/xml; charset=' . $this->encoding);
  129.             header('Content-length: '.strlen($response));
  130.             print $response;
  131.         } else {
  132.             $this->autoDocument();
  133.         }
  134.     }
  135.     
  136.     // }}}
  137.     // {{{ getResponse()
  138.     
  139.     /**
  140.      * get the XML response of the XMLRPC server
  141.      *
  142.      * @return string the XML response
  143.      */
  144.     public function getResponse()
  145.     {
  146.         try {
  147.             if ($this->signatureChecking) {
  148.                 $tmp = xmlrpc_parse_method_descriptions($GLOBALS['HTTP_RAW_POST_DATA']);
  149.                 $methodName = $tmp['methodName'];
  150.                 $parameters = xmlrpc_decode($GLOBALS['HTTP_RAW_POST_DATA'], $this->encoding);
  151.                 $method = $this->callHandler->getMethod($methodName);
  152.                 if (!($method)) {
  153.                     // see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php for standard error codes 
  154.                     return (XML_RPC2_Backend_Php_Response::encodeFault(-32601, 'server error. requested method not found'));
  155.                 }
  156.                 if (!($method->matchesSignature($methodName, $parameters))) {
  157.                     return (XML_RPC2_Backend_Php_Response::encodeFault(-32602, 'server error. invalid method parameters'));        
  158.                 }
  159.             }
  160.             set_error_handler(array('XML_RPC2_Backend_Xmlrpcext_Server', 'errorToException'));
  161.             $response = @xmlrpc_server_call_method($this->_xmlrpcextServer, 
  162.                                                   $GLOBALS['HTTP_RAW_POST_DATA'],
  163.                                                   null,
  164.                                                   array('output_type' => 'xml', 'encoding' => $this->encoding));
  165.             restore_error_handler();
  166.             return $response;
  167.         } catch (XML_RPC2_FaultException $e) {
  168.             return (XML_RPC2_Backend_Php_Response::encodeFault($e->getFaultCode(), $e->getMessage()));
  169.         } catch (Exception $e) {
  170.             return (XML_RPC2_Backend_Php_Response::encodeFault(1, 'Unhandled ' . get_class($e) . ' exception:' . $e->getMessage()));
  171.         }
  172.     }
  173.      
  174.     
  175. }
  176.  
  177. ?>
  178.