Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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 308640 2011-02-24 20:46:30Z sergiosgc $
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
        if ($this->encoding != 'utf-8') throw new XML_RPC2_Exception('XML_RPC2_Backend_Php does not support any encoding other than utf-8, due to a simplexml limitation');
83
    }
84
 
85
    // }}}
86
    // {{{ handleCall()
87
 
88
    /**
89
     * Receive the XML-RPC request, decode the HTTP payload, delegate execution to the call handler, and output the encoded call handler response.
90
     *
91
     */
92
    public function handleCall()
93
    {
94
        if ($this->autoDocument && $this->input->isEmpty()) {
95
            $this->autoDocument();
96
        } else {
97
            $response = $this->getResponse();
98
            header('Content-type: text/xml; charset=' . $this->encoding);
99
            header('Content-length: ' . $this->getContentLength($response));
100
            print $response;
101
        }
102
    }
103
 
104
    // }}}
105
    // {{{ getResponse()
106
 
107
    /**
108
     * Get the XML response of the XMLRPC server
109
     *
110
     * @return string XML response
111
     */
112
    public function getResponse()
113
    {
114
        try {
115
            set_error_handler(array('XML_RPC2_Backend_Php_Server', 'errorToException'));
116
            $request = @simplexml_load_string($this->input->readRequest());
117
            // TODO : do not use exception but a XMLRPC error !
118
            if (!is_object($request)) throw new XML_RPC2_FaultException('Unable to parse request XML', 0);
119
            $request = XML_RPC2_Backend_Php_Request::createFromDecode($request);
120
            $methodName = $request->getMethodName();
121
            $arguments = $request->getParameters();
122
            if ($this->signatureChecking) {
123
                $method = $this->callHandler->getMethod($methodName);
124
                if (!($method)) {
125
                    // see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php for standard error codes
126
                    return (XML_RPC2_Backend_Php_Response::encodeFault(-32601, 'server error. requested method not found'));
127
                }
128
                if (!($method->matchesSignature($methodName, $arguments))) {
129
                    return (XML_RPC2_Backend_Php_Response::encodeFault(-32602, 'server error. invalid method parameters'));
130
                }
131
            }
132
            restore_error_handler();
133
            return (XML_RPC2_Backend_Php_Response::encode(call_user_func_array(array($this->callHandler, $methodName), $arguments), $this->encoding));
134
        } catch (XML_RPC2_FaultException $e) {
135
            return (XML_RPC2_Backend_Php_Response::encodeFault($e->getFaultCode(), $e->getMessage(), $this->encoding));
136
        } catch (Exception $e) {
137
            return (XML_RPC2_Backend_Php_Response::encodeFault(1, 'Unhandled ' . get_class($e) . ' exception:' . $e->getMessage() . $e->getTraceAsString(), $this->encoding));
138
        }
139
    }
140
 
141
}
142
?>