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 308615 2011-02-23 21:44:05Z 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 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
    // {{{ properties
67
 
68
    /**
69
     * xmlrpcext server
70
     *
71
     * @var resource
72
     */
73
    private $_xmlrpcextServer;
74
 
75
    // }}}
76
    // {{{ constructor
77
 
78
    /**
79
     * Create a new XML-RPC Server.
80
     *
81
     * The constructor receives a mandatory parameter: the Call Handler. The call handler executes the actual
82
     * method call. XML_RPC2 server acts as a protocol decoder/encoder between the call handler and the client
83
     *
84
     * @param object $callHandler
85
     * @param array $options associative array of options
86
     */
87
    function __construct($callHandler, $options = array())
88
    {
89
        parent::__construct($callHandler, $options);
90
        $this->_xmlrpcextServer = xmlrpc_server_create();
91
        foreach ($callHandler->getMethods() as $method) {
92
            if (xmlrpc_server_register_method($this->_xmlrpcextServer,
93
                                              $method->getName(),
94
                                              array($this, 'epiFunctionHandlerAdapter')) !== true) {
95
                throw new XML_RPC2_Exception('Unable to setup XMLRPCext server. xmlrpc_server_register_method returned non-true.');
96
            }
97
        }
98
    }
99
 
100
    // }}}
101
    // {{{ epiFunctionHandlerAdapter()
102
 
103
    /**
104
     * This is an adapter between XML_RPC2_CallHandler::__call and xmlrpc_server_register_method callback interface
105
     *
106
     * @param string Method name
107
     * @param array Parameters
108
     * @param array Application data (ignored)
109
     */
110
    protected function epiFunctionHandlerAdapter($method_name, $params, $app_data) {
111
        return @call_user_func_array(array($this->callHandler, $method_name), $params);
112
    }
113
 
114
    // }}}
115
    // {{{ handleCall()
116
 
117
    /**
118
     * Respond to the XML-RPC request.
119
     *
120
     * handleCall reads the XML-RPC request from the raw HTTP body and decodes it. It then calls the
121
     * corresponding method in the call handler class, returning the encoded result to the client.
122
     */
123
    public function handleCall()
124
    {
125
        if ($this->autoDocument && $this->input->isEmpty()) {
126
            $this->autoDocument();
127
        } else {
128
            $response = $this->getResponse();
129
            header('Content-type: text/xml; charset=' . $this->encoding);
130
            header('Content-length: ' . $this->getContentLength($response));
131
            print $response;
132
        }
133
    }
134
 
135
    // }}}
136
    // {{{ getResponse()
137
 
138
    /**
139
     * get the XML response of the XMLRPC server
140
     *
141
     * @return string the XML response
142
     */
143
    public function getResponse()
144
    {
145
        try {
146
            if ($this->signatureChecking) {
147
                $tmp = xmlrpc_parse_method_descriptions($this->input->readRequest());
148
                $methodName = $tmp['methodName'];
149
                $parameters = xmlrpc_decode($this->input->readRequest(), $this->encoding);
150
                $method = $this->callHandler->getMethod($methodName);
151
                if (!($method)) {
152
                    // see http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php for standard error codes
153
                    return (XML_RPC2_Backend_Php_Response::encodeFault(-32601, 'server error. requested method not found'));
154
                }
155
                if (!($method->matchesSignature($methodName, $parameters))) {
156
                    return (XML_RPC2_Backend_Php_Response::encodeFault(-32602, 'server error. invalid method parameters'));
157
                }
158
            }
159
            set_error_handler(array('XML_RPC2_Backend_Xmlrpcext_Server', 'errorToException'));
160
            $response = @xmlrpc_server_call_method($this->_xmlrpcextServer,
161
                                                  $this->input->readRequest(),
162
                                                  null,
163
                                                  array('output_type' => 'xml', 'encoding' => $this->encoding));
164
            restore_error_handler();
165
            return $response;
166
        } catch (XML_RPC2_FaultException $e) {
167
            return (XML_RPC2_Backend_Php_Response::encodeFault($e->getFaultCode(), $e->getMessage()));
168
        } catch (Exception $e) {
169
            return (XML_RPC2_Backend_Php_Response::encodeFault(1, 'Unhandled ' . get_class($e) . ' exception:' . $e->getMessage()));
170
        }
171
    }
172
    // }}}
173
}
174
 
175
?>