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 / Php / Server.php < prev    next >
Encoding:
PHP Script  |  2008-07-02  |  6.3 KB  |  142 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.11 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 PHP-only 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_Php_Server extends XML_RPC2_Server
  65. {
  66.  
  67.     // {{{ constructor
  68.     
  69.     /**
  70.      * Create a new XML-RPC Server. 
  71.      *
  72.      * The constructor receives a mandatory parameter: the Call Handler. The call handler executes the actual
  73.      * method call. XML_RPC2 server acts as a protocol decoder/encoder between the call handler and the client
  74.      *
  75.      * @param object $callHandler
  76.      * @param array $options associative array of options
  77.      * @access public
  78.      */
  79.     function __construct($callHandler, $options = array())
  80.     {
  81.         parent::__construct($callHandler, $options);
  82.     }
  83.     
  84.     // }}}
  85.     // {{{ handleCall()
  86.     
  87.     /**
  88.      * Receive the XML-RPC request, decode the HTTP payload, delegate execution to the call handler, and output the encoded call handler response.
  89.      *
  90.      */
  91.     public function handleCall()
  92.     {
  93.         if ((!($this->autoDocument)) or ((isset($GLOBALS['HTTP_RAW_POST_DATA'])) && (strlen($GLOBALS['HTTP_RAW_POST_DATA'])>0))) {
  94.             $response = $this->getResponse();
  95.             header('Content-type: text/xml; charset=' . $this->encoding);
  96.             header('Content-length: '.strlen($response));
  97.             print $response;
  98.         } else {
  99.             $this->autoDocument();
  100.         }
  101.     }
  102.     
  103.     // }}}
  104.     // {{{ getResponse()
  105.     
  106.     /**
  107.      * Get the XML response of the XMLRPC server
  108.      * 
  109.      * @return string XML response
  110.      */
  111.     public function getResponse()
  112.     {
  113.         try {
  114.             set_error_handler(array('XML_RPC2_Backend_Php_Server', 'errorToException'));
  115.             $request = @simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA']);
  116.             // TODO : do not use exception but a XMLRPC error !
  117.             if (!is_object($request)) throw new XML_RPC2_FaultException('Unable to parse request XML', 0);
  118.             $request = XML_RPC2_Backend_Php_Request::createFromDecode($request);  
  119.             $methodName = $request->getMethodName();
  120.             $arguments = $request->getParameters();
  121.             if ($this->signatureChecking) {
  122.                 $method = $this->callHandler->getMethod($methodName);
  123.                 if (!($method)) {
  124.                     // see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php for standard error codes 
  125.                     return (XML_RPC2_Backend_Php_Response::encodeFault(-32601, 'server error. requested method not found'));
  126.                 }
  127.                 if (!($method->matchesSignature($methodName, $arguments))) {
  128.                     return (XML_RPC2_Backend_Php_Response::encodeFault(-32602, 'server error. invalid method parameters'));        
  129.                 }
  130.             }
  131.             restore_error_handler();
  132.             return (XML_RPC2_Backend_Php_Response::encode(call_user_func_array(array($this->callHandler, $methodName), $arguments)));              
  133.         } catch (XML_RPC2_FaultException $e) {
  134.             return (XML_RPC2_Backend_Php_Response::encodeFault($e->getFaultCode(), $e->getMessage()));
  135.         } catch (Exception $e) {
  136.             return (XML_RPC2_Backend_Php_Response::encodeFault(1, 'Unhandled ' . get_class($e) . ' exception:' . $e->getMessage()));
  137.         }        
  138.     }
  139.     
  140. }
  141. ?>
  142.