1 |
7ae92444
|
Colin Smith
|
<?php
|
2 |
605938e5
|
Bill Marquette
|
|
3 |
7ae92444
|
Colin Smith
|
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
|
4 |
|
|
|
5 |
|
|
/**
|
6 |
605938e5
|
Bill Marquette
|
* Server commands for our PHP implementation of the XML-RPC protocol
|
7 |
7ae92444
|
Colin Smith
|
*
|
8 |
|
|
* This is a PEAR-ified version of Useful inc's XML-RPC for PHP.
|
9 |
|
|
* It has support for HTTP transport, proxies and authentication.
|
10 |
|
|
*
|
11 |
|
|
* PHP versions 4 and 5
|
12 |
|
|
*
|
13 |
|
|
* LICENSE: License is granted to use or modify this software
|
14 |
|
|
* ("XML-RPC for PHP") for commercial or non-commercial use provided the
|
15 |
|
|
* copyright of the author is preserved in any distributed or derivative work.
|
16 |
|
|
*
|
17 |
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESSED OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
19 |
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
20 |
|
|
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
22 |
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
23 |
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
24 |
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
25 |
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
26 |
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
*
|
28 |
|
|
* @category Web Services
|
29 |
|
|
* @package XML_RPC
|
30 |
|
|
* @author Edd Dumbill <edd@usefulinc.com>
|
31 |
|
|
* @author Stig Bakken <stig@php.net>
|
32 |
|
|
* @author Martin Jansen <mj@php.net>
|
33 |
605938e5
|
Bill Marquette
|
* @author Daniel Convissor <danielc@php.net>
|
34 |
9c22a703
|
Seth Mos
|
* @copyright 1999-2001 Edd Dumbill, 2001-2006 The PHP Group
|
35 |
7ae92444
|
Colin Smith
|
* @version CVS: $Id$
|
36 |
|
|
* @link http://pear.php.net/package/XML_RPC
|
37 |
|
|
*/
|
38 |
|
|
|
39 |
523855b0
|
Scott Ullrich
|
/*
|
40 |
|
|
pfSense_MODULE: utils
|
41 |
|
|
*/
|
42 |
7ae92444
|
Colin Smith
|
|
43 |
|
|
/**
|
44 |
|
|
* Pull in the XML_RPC class
|
45 |
|
|
*/
|
46 |
b60f7708
|
Colin Smith
|
require_once 'xmlrpc_client.inc';
|
47 |
7ae92444
|
Colin Smith
|
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
605938e5
|
Bill Marquette
|
* signature for system.listMethods: return = array,
|
51 |
|
|
* parameters = a string or nothing
|
52 |
7ae92444
|
Colin Smith
|
* @global array $GLOBALS['XML_RPC_Server_listMethods_sig']
|
53 |
|
|
*/
|
54 |
|
|
$GLOBALS['XML_RPC_Server_listMethods_sig'] = array(
|
55 |
|
|
array($GLOBALS['XML_RPC_Array'],
|
56 |
|
|
$GLOBALS['XML_RPC_String']
|
57 |
|
|
),
|
58 |
|
|
array($GLOBALS['XML_RPC_Array'])
|
59 |
|
|
);
|
60 |
|
|
|
61 |
|
|
/**
|
62 |
605938e5
|
Bill Marquette
|
* docstring for system.listMethods
|
63 |
7ae92444
|
Colin Smith
|
* @global string $GLOBALS['XML_RPC_Server_listMethods_doc']
|
64 |
|
|
*/
|
65 |
|
|
$GLOBALS['XML_RPC_Server_listMethods_doc'] = 'This method lists all the'
|
66 |
|
|
. ' methods that the XML-RPC server knows how to dispatch';
|
67 |
|
|
|
68 |
|
|
/**
|
69 |
605938e5
|
Bill Marquette
|
* signature for system.methodSignature: return = array,
|
70 |
|
|
* parameters = string
|
71 |
7ae92444
|
Colin Smith
|
* @global array $GLOBALS['XML_RPC_Server_methodSignature_sig']
|
72 |
|
|
*/
|
73 |
|
|
$GLOBALS['XML_RPC_Server_methodSignature_sig'] = array(
|
74 |
|
|
array($GLOBALS['XML_RPC_Array'],
|
75 |
|
|
$GLOBALS['XML_RPC_String']
|
76 |
|
|
)
|
77 |
|
|
);
|
78 |
|
|
|
79 |
|
|
/**
|
80 |
605938e5
|
Bill Marquette
|
* docstring for system.methodSignature
|
81 |
7ae92444
|
Colin Smith
|
* @global string $GLOBALS['XML_RPC_Server_methodSignature_doc']
|
82 |
|
|
*/
|
83 |
|
|
$GLOBALS['XML_RPC_Server_methodSignature_doc'] = 'Returns an array of known'
|
84 |
|
|
. ' signatures (an array of arrays) for the method name passed. If'
|
85 |
|
|
. ' no signatures are known, returns a none-array (test for type !='
|
86 |
|
|
. ' array to detect missing signature)';
|
87 |
|
|
|
88 |
|
|
/**
|
89 |
605938e5
|
Bill Marquette
|
* signature for system.methodHelp: return = string,
|
90 |
|
|
* parameters = string
|
91 |
7ae92444
|
Colin Smith
|
* @global array $GLOBALS['XML_RPC_Server_methodHelp_sig']
|
92 |
|
|
*/
|
93 |
|
|
$GLOBALS['XML_RPC_Server_methodHelp_sig'] = array(
|
94 |
|
|
array($GLOBALS['XML_RPC_String'],
|
95 |
|
|
$GLOBALS['XML_RPC_String']
|
96 |
|
|
)
|
97 |
|
|
);
|
98 |
|
|
|
99 |
|
|
/**
|
100 |
605938e5
|
Bill Marquette
|
* docstring for methodHelp
|
101 |
7ae92444
|
Colin Smith
|
* @global string $GLOBALS['XML_RPC_Server_methodHelp_doc']
|
102 |
|
|
*/
|
103 |
|
|
$GLOBALS['XML_RPC_Server_methodHelp_doc'] = 'Returns help text if defined'
|
104 |
|
|
. ' for the method passed, otherwise returns an empty string';
|
105 |
|
|
|
106 |
|
|
/**
|
107 |
605938e5
|
Bill Marquette
|
* dispatch map for the automatically declared XML-RPC methods.
|
108 |
7ae92444
|
Colin Smith
|
* @global array $GLOBALS['XML_RPC_Server_dmap']
|
109 |
|
|
*/
|
110 |
|
|
$GLOBALS['XML_RPC_Server_dmap'] = array(
|
111 |
|
|
'system.listMethods' => array(
|
112 |
|
|
'function' => 'XML_RPC_Server_listMethods',
|
113 |
|
|
'signature' => $GLOBALS['XML_RPC_Server_listMethods_sig'],
|
114 |
|
|
'docstring' => $GLOBALS['XML_RPC_Server_listMethods_doc']
|
115 |
|
|
),
|
116 |
|
|
'system.methodHelp' => array(
|
117 |
|
|
'function' => 'XML_RPC_Server_methodHelp',
|
118 |
|
|
'signature' => $GLOBALS['XML_RPC_Server_methodHelp_sig'],
|
119 |
|
|
'docstring' => $GLOBALS['XML_RPC_Server_methodHelp_doc']
|
120 |
|
|
),
|
121 |
|
|
'system.methodSignature' => array(
|
122 |
|
|
'function' => 'XML_RPC_Server_methodSignature',
|
123 |
|
|
'signature' => $GLOBALS['XML_RPC_Server_methodSignature_sig'],
|
124 |
|
|
'docstring' => $GLOBALS['XML_RPC_Server_methodSignature_doc']
|
125 |
|
|
)
|
126 |
|
|
);
|
127 |
|
|
|
128 |
|
|
/**
|
129 |
|
|
* @global string $GLOBALS['XML_RPC_Server_debuginfo']
|
130 |
|
|
*/
|
131 |
|
|
$GLOBALS['XML_RPC_Server_debuginfo'] = '';
|
132 |
|
|
|
133 |
|
|
|
134 |
|
|
/**
|
135 |
|
|
* Lists all the methods that the XML-RPC server knows how to dispatch
|
136 |
|
|
*
|
137 |
|
|
* @return object a new XML_RPC_Response object
|
138 |
|
|
*/
|
139 |
|
|
function XML_RPC_Server_listMethods($server, $m)
|
140 |
|
|
{
|
141 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap;
|
142 |
|
|
|
143 |
|
|
$v = new XML_RPC_Value();
|
144 |
|
|
$outAr = array();
|
145 |
605938e5
|
Bill Marquette
|
foreach ($server->dmap as $key => $val) {
|
146 |
7ae92444
|
Colin Smith
|
$outAr[] = new XML_RPC_Value($key, 'string');
|
147 |
|
|
}
|
148 |
605938e5
|
Bill Marquette
|
foreach ($XML_RPC_Server_dmap as $key => $val) {
|
149 |
7ae92444
|
Colin Smith
|
$outAr[] = new XML_RPC_Value($key, 'string');
|
150 |
|
|
}
|
151 |
|
|
$v->addArray($outAr);
|
152 |
|
|
return new XML_RPC_Response($v);
|
153 |
|
|
}
|
154 |
|
|
|
155 |
|
|
/**
|
156 |
|
|
* Returns an array of known signatures (an array of arrays)
|
157 |
|
|
* for the given method
|
158 |
|
|
*
|
159 |
|
|
* If no signatures are known, returns a none-array
|
160 |
|
|
* (test for type != array to detect missing signature)
|
161 |
|
|
*
|
162 |
|
|
* @return object a new XML_RPC_Response object
|
163 |
|
|
*/
|
164 |
|
|
function XML_RPC_Server_methodSignature($server, $m)
|
165 |
|
|
{
|
166 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap;
|
167 |
|
|
|
168 |
|
|
$methName = $m->getParam(0);
|
169 |
|
|
$methName = $methName->scalarval();
|
170 |
|
|
if (strpos($methName, 'system.') === 0) {
|
171 |
|
|
$dmap = $XML_RPC_Server_dmap;
|
172 |
|
|
$sysCall = 1;
|
173 |
|
|
} else {
|
174 |
|
|
$dmap = $server->dmap;
|
175 |
|
|
$sysCall = 0;
|
176 |
|
|
}
|
177 |
|
|
// print "<!-- ${methName} -->\n";
|
178 |
|
|
if (isset($dmap[$methName])) {
|
179 |
|
|
if ($dmap[$methName]['signature']) {
|
180 |
|
|
$sigs = array();
|
181 |
|
|
$thesigs = $dmap[$methName]['signature'];
|
182 |
|
|
for ($i = 0; $i < sizeof($thesigs); $i++) {
|
183 |
|
|
$cursig = array();
|
184 |
|
|
$inSig = $thesigs[$i];
|
185 |
|
|
for ($j = 0; $j < sizeof($inSig); $j++) {
|
186 |
|
|
$cursig[] = new XML_RPC_Value($inSig[$j], 'string');
|
187 |
|
|
}
|
188 |
|
|
$sigs[] = new XML_RPC_Value($cursig, 'array');
|
189 |
|
|
}
|
190 |
|
|
$r = new XML_RPC_Response(new XML_RPC_Value($sigs, 'array'));
|
191 |
|
|
} else {
|
192 |
|
|
$r = new XML_RPC_Response(new XML_RPC_Value('undef', 'string'));
|
193 |
|
|
}
|
194 |
|
|
} else {
|
195 |
|
|
$r = new XML_RPC_Response(0, $XML_RPC_err['introspect_unknown'],
|
196 |
|
|
$XML_RPC_str['introspect_unknown']);
|
197 |
|
|
}
|
198 |
|
|
return $r;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
/**
|
202 |
|
|
* Returns help text if defined for the method passed, otherwise returns
|
203 |
|
|
* an empty string
|
204 |
|
|
*
|
205 |
|
|
* @return object a new XML_RPC_Response object
|
206 |
|
|
*/
|
207 |
|
|
function XML_RPC_Server_methodHelp($server, $m)
|
208 |
|
|
{
|
209 |
|
|
global $XML_RPC_err, $XML_RPC_str, $XML_RPC_Server_dmap;
|
210 |
|
|
|
211 |
|
|
$methName = $m->getParam(0);
|
212 |
|
|
$methName = $methName->scalarval();
|
213 |
|
|
if (strpos($methName, 'system.') === 0) {
|
214 |
|
|
$dmap = $XML_RPC_Server_dmap;
|
215 |
|
|
$sysCall = 1;
|
216 |
|
|
} else {
|
217 |
|
|
$dmap = $server->dmap;
|
218 |
|
|
$sysCall = 0;
|
219 |
|
|
}
|
220 |
012b6983
|
Colin Smith
|
|
221 |
7ae92444
|
Colin Smith
|
if (isset($dmap[$methName])) {
|
222 |
|
|
if ($dmap[$methName]['docstring']) {
|
223 |
|
|
$r = new XML_RPC_Response(new XML_RPC_Value($dmap[$methName]['docstring']),
|
224 |
|
|
'string');
|
225 |
|
|
} else {
|
226 |
|
|
$r = new XML_RPC_Response(new XML_RPC_Value('', 'string'));
|
227 |
|
|
}
|
228 |
|
|
} else {
|
229 |
|
|
$r = new XML_RPC_Response(0, $XML_RPC_err['introspect_unknown'],
|
230 |
|
|
$XML_RPC_str['introspect_unknown']);
|
231 |
|
|
}
|
232 |
|
|
return $r;
|
233 |
|
|
}
|
234 |
|
|
|
235 |
|
|
/**
|
236 |
|
|
* @return void
|
237 |
|
|
*/
|
238 |
|
|
function XML_RPC_Server_debugmsg($m)
|
239 |
|
|
{
|
240 |
|
|
global $XML_RPC_Server_debuginfo;
|
241 |
|
|
$XML_RPC_Server_debuginfo = $XML_RPC_Server_debuginfo . $m . "\n";
|
242 |
|
|
}
|
243 |
|
|
|
244 |
|
|
|
245 |
|
|
/**
|
246 |
605938e5
|
Bill Marquette
|
* A server for receiving and replying to XML RPC requests
|
247 |
7ae92444
|
Colin Smith
|
*
|
248 |
605938e5
|
Bill Marquette
|
* <code>
|
249 |
|
|
* $server = new XML_RPC_Server(
|
250 |
|
|
* array(
|
251 |
|
|
* 'isan8' =>
|
252 |
|
|
* array(
|
253 |
|
|
* 'function' => 'is_8',
|
254 |
|
|
* 'signature' =>
|
255 |
|
|
* array(
|
256 |
|
|
* array('boolean', 'int'),
|
257 |
|
|
* array('boolean', 'int', 'boolean'),
|
258 |
|
|
* array('boolean', 'string'),
|
259 |
|
|
* array('boolean', 'string', 'boolean'),
|
260 |
|
|
* ),
|
261 |
|
|
* 'docstring' => 'Is the value an 8?'
|
262 |
|
|
* ),
|
263 |
|
|
* ),
|
264 |
|
|
* 1,
|
265 |
|
|
* 0
|
266 |
|
|
* );
|
267 |
|
|
* </code>
|
268 |
7ae92444
|
Colin Smith
|
*
|
269 |
|
|
* @category Web Services
|
270 |
|
|
* @package XML_RPC
|
271 |
|
|
* @author Edd Dumbill <edd@usefulinc.com>
|
272 |
|
|
* @author Stig Bakken <stig@php.net>
|
273 |
|
|
* @author Martin Jansen <mj@php.net>
|
274 |
605938e5
|
Bill Marquette
|
* @author Daniel Convissor <danielc@php.net>
|
275 |
9c22a703
|
Seth Mos
|
* @copyright 1999-2001 Edd Dumbill, 2001-2006 The PHP Group
|
276 |
|
|
* @version Release: 1.5.1
|
277 |
7ae92444
|
Colin Smith
|
* @link http://pear.php.net/package/XML_RPC
|
278 |
|
|
*/
|
279 |
|
|
class XML_RPC_Server
|
280 |
|
|
{
|
281 |
9c22a703
|
Seth Mos
|
/**
|
282 |
|
|
* Should the payload's content be passed through mb_convert_encoding()?
|
283 |
|
|
*
|
284 |
|
|
* @see XML_RPC_Server::setConvertPayloadEncoding()
|
285 |
|
|
* @since Property available since Release 1.5.1
|
286 |
|
|
* @var boolean
|
287 |
|
|
*/
|
288 |
|
|
var $convert_payload_encoding = false;
|
289 |
|
|
|
290 |
605938e5
|
Bill Marquette
|
/**
|
291 |
|
|
* The dispatch map, listing the methods this server provides.
|
292 |
|
|
* @var array
|
293 |
|
|
*/
|
294 |
7ae92444
|
Colin Smith
|
var $dmap = array();
|
295 |
605938e5
|
Bill Marquette
|
|
296 |
|
|
/**
|
297 |
|
|
* The present response's encoding
|
298 |
|
|
* @var string
|
299 |
|
|
* @see XML_RPC_Message::getEncoding()
|
300 |
|
|
*/
|
301 |
7ae92444
|
Colin Smith
|
var $encoding = '';
|
302 |
605938e5
|
Bill Marquette
|
|
303 |
|
|
/**
|
304 |
|
|
* Debug mode (0 = off, 1 = on)
|
305 |
|
|
* @var integer
|
306 |
|
|
*/
|
307 |
7ae92444
|
Colin Smith
|
var $debug = 0;
|
308 |
|
|
|
309 |
|
|
/**
|
310 |
605938e5
|
Bill Marquette
|
* The response's HTTP headers
|
311 |
|
|
* @var string
|
312 |
|
|
*/
|
313 |
|
|
var $server_headers = '';
|
314 |
|
|
|
315 |
|
|
/**
|
316 |
|
|
* The response's XML payload
|
317 |
|
|
* @var string
|
318 |
|
|
*/
|
319 |
|
|
var $server_payload = '';
|
320 |
|
|
|
321 |
|
|
|
322 |
|
|
/**
|
323 |
|
|
* Constructor for the XML_RPC_Server class
|
324 |
|
|
*
|
325 |
|
|
* @param array $dispMap the dispatch map. An associative array
|
326 |
|
|
* explaining each function. The keys of the main
|
327 |
|
|
* array are the procedure names used by the
|
328 |
|
|
* clients. The value is another associative array
|
329 |
|
|
* that contains up to three elements:
|
330 |
|
|
* + The 'function' element's value is the name
|
331 |
|
|
* of the function or method that gets called.
|
332 |
|
|
* To define a class' method: 'class::method'.
|
333 |
|
|
* + The 'signature' element (optional) is an
|
334 |
|
|
* array describing the return values and
|
335 |
|
|
* parameters
|
336 |
|
|
* + The 'docstring' element (optional) is a
|
337 |
|
|
* string describing what the method does
|
338 |
|
|
* @param int $serviceNow should the HTTP response be sent now?
|
339 |
|
|
* (1 = yes, 0 = no)
|
340 |
|
|
* @param int $debug should debug output be displayed?
|
341 |
|
|
* (1 = yes, 0 = no)
|
342 |
|
|
*
|
343 |
7ae92444
|
Colin Smith
|
* @return void
|
344 |
|
|
*/
|
345 |
|
|
function XML_RPC_Server($dispMap, $serviceNow = 1, $debug = 0)
|
346 |
|
|
{
|
347 |
|
|
global $HTTP_RAW_POST_DATA;
|
348 |
|
|
|
349 |
|
|
if ($debug) {
|
350 |
|
|
$this->debug = 1;
|
351 |
|
|
} else {
|
352 |
|
|
$this->debug = 0;
|
353 |
|
|
}
|
354 |
|
|
|
355 |
|
|
$this->dmap = $dispMap;
|
356 |
605938e5
|
Bill Marquette
|
|
357 |
7ae92444
|
Colin Smith
|
if ($serviceNow) {
|
358 |
|
|
$this->service();
|
359 |
605938e5
|
Bill Marquette
|
} else {
|
360 |
|
|
$this->createServerPayload();
|
361 |
|
|
$this->createServerHeaders();
|
362 |
7ae92444
|
Colin Smith
|
}
|
363 |
|
|
}
|
364 |
|
|
|
365 |
|
|
/**
|
366 |
|
|
* @return string the debug information if debug debug mode is on
|
367 |
|
|
*/
|
368 |
|
|
function serializeDebug()
|
369 |
|
|
{
|
370 |
|
|
global $XML_RPC_Server_debuginfo, $HTTP_RAW_POST_DATA;
|
371 |
|
|
|
372 |
|
|
if ($this->debug) {
|
373 |
|
|
XML_RPC_Server_debugmsg('vvv POST DATA RECEIVED BY SERVER vvv' . "\n"
|
374 |
|
|
. $HTTP_RAW_POST_DATA
|
375 |
|
|
. "\n" . '^^^ END POST DATA ^^^');
|
376 |
|
|
}
|
377 |
|
|
|
378 |
|
|
if ($XML_RPC_Server_debuginfo != '') {
|
379 |
|
|
return "<!-- PEAR XML_RPC SERVER DEBUG INFO:\n\n"
|
380 |
9c22a703
|
Seth Mos
|
. $GLOBALS['XML_RPC_func_ereg_replace']('--', '- - ', $XML_RPC_Server_debuginfo)
|
381 |
7ae92444
|
Colin Smith
|
. "-->\n";
|
382 |
|
|
} else {
|
383 |
|
|
return '';
|
384 |
|
|
}
|
385 |
|
|
}
|
386 |
|
|
|
387 |
9c22a703
|
Seth Mos
|
/**
|
388 |
|
|
* Sets whether the payload's content gets passed through
|
389 |
|
|
* mb_convert_encoding()
|
390 |
|
|
*
|
391 |
|
|
* Returns PEAR_ERROR object if mb_convert_encoding() isn't available.
|
392 |
|
|
*
|
393 |
|
|
* @param int $in where 1 = on, 0 = off
|
394 |
|
|
*
|
395 |
|
|
* @return void
|
396 |
|
|
*
|
397 |
|
|
* @see XML_RPC_Message::getEncoding()
|
398 |
|
|
* @since Method available since Release 1.5.1
|
399 |
|
|
*/
|
400 |
|
|
function setConvertPayloadEncoding($in)
|
401 |
|
|
{
|
402 |
|
|
if ($in && !function_exists('mb_convert_encoding')) {
|
403 |
|
|
return $this->raiseError('mb_convert_encoding() is not available',
|
404 |
|
|
XML_RPC_ERROR_PROGRAMMING);
|
405 |
|
|
}
|
406 |
|
|
$this->convert_payload_encoding = $in;
|
407 |
|
|
}
|
408 |
|
|
|
409 |
7ae92444
|
Colin Smith
|
/**
|
410 |
605938e5
|
Bill Marquette
|
* Sends the response
|
411 |
7ae92444
|
Colin Smith
|
*
|
412 |
|
|
* The encoding and content-type are determined by
|
413 |
|
|
* XML_RPC_Message::getEncoding()
|
414 |
|
|
*
|
415 |
|
|
* @return void
|
416 |
|
|
*
|
417 |
605938e5
|
Bill Marquette
|
* @uses XML_RPC_Server::createServerPayload(),
|
418 |
|
|
* XML_RPC_Server::createServerHeaders()
|
419 |
7ae92444
|
Colin Smith
|
*/
|
420 |
|
|
function service()
|
421 |
605938e5
|
Bill Marquette
|
{
|
422 |
012b6983
|
Colin Smith
|
if (!$this->server_payload) {
|
423 |
|
|
$this->createServerPayload();
|
424 |
|
|
}
|
425 |
|
|
if (!$this->server_headers) {
|
426 |
|
|
$this->createServerHeaders();
|
427 |
|
|
}
|
428 |
b755666f
|
Scott Ullrich
|
|
429 |
|
|
/*
|
430 |
|
|
* $server_headers needs to remain a string for compatibility with
|
431 |
|
|
* old scripts using this package, but PHP 4.4.2 no longer allows
|
432 |
|
|
* line breaks in header() calls. So, we split each header into
|
433 |
|
|
* an individual call. The initial replace handles the off chance
|
434 |
|
|
* that someone composed a single header with multiple lines, which
|
435 |
|
|
* the RFCs allow.
|
436 |
|
|
*/
|
437 |
9c22a703
|
Seth Mos
|
$this->server_headers = $GLOBALS['XML_RPC_func_ereg_replace']("[\r\n]+[ \t]+",
|
438 |
|
|
' ', trim($this->server_headers));
|
439 |
|
|
$headers = $GLOBALS['XML_RPC_func_split']("[\r\n]+", $this->server_headers);
|
440 |
b755666f
|
Scott Ullrich
|
foreach ($headers as $header)
|
441 |
|
|
{
|
442 |
|
|
header($header);
|
443 |
|
|
}
|
444 |
|
|
|
445 |
605938e5
|
Bill Marquette
|
print $this->server_payload;
|
446 |
|
|
}
|
447 |
|
|
|
448 |
|
|
/**
|
449 |
|
|
* Generates the payload and puts it in the $server_payload property
|
450 |
|
|
*
|
451 |
9c22a703
|
Seth Mos
|
* If XML_RPC_Server::setConvertPayloadEncoding() was set to true,
|
452 |
|
|
* the payload gets passed through mb_convert_encoding()
|
453 |
|
|
* to ensure the payload matches the encoding set in the
|
454 |
|
|
* XML declaration. The encoding type can be manually set via
|
455 |
|
|
* XML_RPC_Message::setSendEncoding().
|
456 |
|
|
*
|
457 |
605938e5
|
Bill Marquette
|
* @return void
|
458 |
|
|
*
|
459 |
|
|
* @uses XML_RPC_Server::parseRequest(), XML_RPC_Server::$encoding,
|
460 |
|
|
* XML_RPC_Response::serialize(), XML_RPC_Server::serializeDebug()
|
461 |
9c22a703
|
Seth Mos
|
* @see XML_RPC_Server::setConvertPayloadEncoding()
|
462 |
605938e5
|
Bill Marquette
|
*/
|
463 |
|
|
function createServerPayload()
|
464 |
7ae92444
|
Colin Smith
|
{
|
465 |
|
|
$r = $this->parseRequest();
|
466 |
605938e5
|
Bill Marquette
|
$this->server_payload = '<?xml version="1.0" encoding="'
|
467 |
|
|
. $this->encoding . '"?>' . "\n"
|
468 |
|
|
. $this->serializeDebug()
|
469 |
|
|
. $r->serialize();
|
470 |
9c22a703
|
Seth Mos
|
if ($this->convert_payload_encoding) {
|
471 |
|
|
$this->server_payload = mb_convert_encoding($this->server_payload,
|
472 |
|
|
$this->encoding);
|
473 |
|
|
}
|
474 |
605938e5
|
Bill Marquette
|
}
|
475 |
|
|
|
476 |
|
|
/**
|
477 |
|
|
* Determines the HTTP headers and puts them in the $server_headers
|
478 |
|
|
* property
|
479 |
|
|
*
|
480 |
|
|
* @return boolean TRUE if okay, FALSE if $server_payload isn't set.
|
481 |
|
|
*
|
482 |
|
|
* @uses XML_RPC_Server::createServerPayload(),
|
483 |
|
|
* XML_RPC_Server::$server_headers
|
484 |
|
|
*/
|
485 |
|
|
function createServerHeaders()
|
486 |
|
|
{
|
487 |
|
|
if (!$this->server_payload) {
|
488 |
|
|
return false;
|
489 |
|
|
}
|
490 |
|
|
$this->server_headers = 'Content-Length: '
|
491 |
|
|
. strlen($this->server_payload) . "\r\n"
|
492 |
|
|
. 'Content-Type: text/xml;'
|
493 |
|
|
. ' charset=' . $this->encoding;
|
494 |
|
|
return true;
|
495 |
7ae92444
|
Colin Smith
|
}
|
496 |
|
|
|
497 |
|
|
/**
|
498 |
|
|
* @return array
|
499 |
|
|
*/
|
500 |
|
|
function verifySignature($in, $sig)
|
501 |
|
|
{
|
502 |
|
|
for ($i = 0; $i < sizeof($sig); $i++) {
|
503 |
|
|
// check each possible signature in turn
|
504 |
|
|
$cursig = $sig[$i];
|
505 |
|
|
if (sizeof($cursig) == $in->getNumParams() + 1) {
|
506 |
|
|
$itsOK = 1;
|
507 |
|
|
for ($n = 0; $n < $in->getNumParams(); $n++) {
|
508 |
|
|
$p = $in->getParam($n);
|
509 |
|
|
// print "<!-- $p -->\n";
|
510 |
|
|
if ($p->kindOf() == 'scalar') {
|
511 |
|
|
$pt = $p->scalartyp();
|
512 |
|
|
} else {
|
513 |
|
|
$pt = $p->kindOf();
|
514 |
|
|
}
|
515 |
|
|
// $n+1 as first type of sig is return type
|
516 |
605938e5
|
Bill Marquette
|
if ($pt != $cursig[$n+1]) {
|
517 |
7ae92444
|
Colin Smith
|
$itsOK = 0;
|
518 |
|
|
$pno = $n+1;
|
519 |
|
|
$wanted = $cursig[$n+1];
|
520 |
|
|
$got = $pt;
|
521 |
|
|
break;
|
522 |
|
|
}
|
523 |
|
|
}
|
524 |
|
|
if ($itsOK) {
|
525 |
|
|
return array(1);
|
526 |
|
|
}
|
527 |
|
|
}
|
528 |
|
|
}
|
529 |
605938e5
|
Bill Marquette
|
if (isset($wanted)) {
|
530 |
|
|
return array(0, "Wanted ${wanted}, got ${got} at param ${pno}");
|
531 |
|
|
} else {
|
532 |
|
|
$allowed = array();
|
533 |
|
|
foreach ($sig as $val) {
|
534 |
|
|
end($val);
|
535 |
|
|
$allowed[] = key($val);
|
536 |
|
|
}
|
537 |
|
|
$allowed = array_unique($allowed);
|
538 |
|
|
$last = count($allowed) - 1;
|
539 |
|
|
if ($last > 0) {
|
540 |
|
|
$allowed[$last] = 'or ' . $allowed[$last];
|
541 |
|
|
}
|
542 |
|
|
return array(0,
|
543 |
|
|
'Signature permits ' . implode(', ', $allowed)
|
544 |
|
|
. ' parameters but the request had '
|
545 |
|
|
. $in->getNumParams());
|
546 |
|
|
}
|
547 |
7ae92444
|
Colin Smith
|
}
|
548 |
|
|
|
549 |
|
|
/**
|
550 |
|
|
* @return object a new XML_RPC_Response object
|
551 |
605938e5
|
Bill Marquette
|
*
|
552 |
|
|
* @uses XML_RPC_Message::getEncoding(), XML_RPC_Server::$encoding
|
553 |
7ae92444
|
Colin Smith
|
*/
|
554 |
|
|
function parseRequest($data = '')
|
555 |
|
|
{
|
556 |
|
|
global $XML_RPC_xh, $HTTP_RAW_POST_DATA,
|
557 |
|
|
$XML_RPC_err, $XML_RPC_str, $XML_RPC_errxml,
|
558 |
|
|
$XML_RPC_defencoding, $XML_RPC_Server_dmap;
|
559 |
|
|
|
560 |
|
|
if ($data == '') {
|
561 |
|
|
$data = $HTTP_RAW_POST_DATA;
|
562 |
|
|
}
|
563 |
|
|
|
564 |
|
|
$this->encoding = XML_RPC_Message::getEncoding($data);
|
565 |
|
|
$parser_resource = xml_parser_create($this->encoding);
|
566 |
|
|
$parser = (int) $parser_resource;
|
567 |
|
|
|
568 |
|
|
$XML_RPC_xh[$parser] = array();
|
569 |
|
|
$XML_RPC_xh[$parser]['cm'] = 0;
|
570 |
|
|
$XML_RPC_xh[$parser]['isf'] = 0;
|
571 |
|
|
$XML_RPC_xh[$parser]['params'] = array();
|
572 |
|
|
$XML_RPC_xh[$parser]['method'] = '';
|
573 |
012b6983
|
Colin Smith
|
$XML_RPC_xh[$parser]['stack'] = array();
|
574 |
|
|
$XML_RPC_xh[$parser]['valuestack'] = array();
|
575 |
7ae92444
|
Colin Smith
|
|
576 |
|
|
$plist = '';
|
577 |
|
|
|
578 |
|
|
// decompose incoming XML into request structure
|
579 |
|
|
|
580 |
|
|
xml_parser_set_option($parser_resource, XML_OPTION_CASE_FOLDING, true);
|
581 |
|
|
xml_set_element_handler($parser_resource, 'XML_RPC_se', 'XML_RPC_ee');
|
582 |
|
|
xml_set_character_data_handler($parser_resource, 'XML_RPC_cd');
|
583 |
|
|
if (!xml_parse($parser_resource, $data, 1)) {
|
584 |
|
|
// return XML error as a faultCode
|
585 |
|
|
$r = new XML_RPC_Response(0,
|
586 |
|
|
$XML_RPC_errxml+xml_get_error_code($parser_resource),
|
587 |
|
|
sprintf('XML error: %s at line %d',
|
588 |
|
|
xml_error_string(xml_get_error_code($parser_resource)),
|
589 |
|
|
xml_get_current_line_number($parser_resource)));
|
590 |
|
|
xml_parser_free($parser_resource);
|
591 |
012b6983
|
Colin Smith
|
} elseif ($XML_RPC_xh[$parser]['isf']>1) {
|
592 |
|
|
$r = new XML_RPC_Response(0,
|
593 |
|
|
$XML_RPC_err['invalid_request'],
|
594 |
|
|
$XML_RPC_str['invalid_request']
|
595 |
|
|
. ': '
|
596 |
|
|
. $XML_RPC_xh[$parser]['isf_reason']);
|
597 |
|
|
xml_parser_free($parser_resource);
|
598 |
7ae92444
|
Colin Smith
|
} else {
|
599 |
|
|
xml_parser_free($parser_resource);
|
600 |
|
|
$m = new XML_RPC_Message($XML_RPC_xh[$parser]['method']);
|
601 |
|
|
// now add parameters in
|
602 |
|
|
for ($i = 0; $i < sizeof($XML_RPC_xh[$parser]['params']); $i++) {
|
603 |
|
|
// print '<!-- ' . $XML_RPC_xh[$parser]['params'][$i]. "-->\n";
|
604 |
012b6983
|
Colin Smith
|
$plist .= "$i - " . var_export($XML_RPC_xh[$parser]['params'][$i], true) . " \n";
|
605 |
|
|
$m->addParam($XML_RPC_xh[$parser]['params'][$i]);
|
606 |
|
|
}
|
607 |
|
|
|
608 |
|
|
if ($this->debug) {
|
609 |
|
|
XML_RPC_Server_debugmsg($plist);
|
610 |
7ae92444
|
Colin Smith
|
}
|
611 |
|
|
|
612 |
|
|
// now to deal with the method
|
613 |
|
|
$methName = $XML_RPC_xh[$parser]['method'];
|
614 |
|
|
if (strpos($methName, 'system.') === 0) {
|
615 |
|
|
$dmap = $XML_RPC_Server_dmap;
|
616 |
|
|
$sysCall = 1;
|
617 |
|
|
} else {
|
618 |
|
|
$dmap = $this->dmap;
|
619 |
|
|
$sysCall = 0;
|
620 |
|
|
}
|
621 |
|
|
|
622 |
|
|
if (isset($dmap[$methName]['function'])
|
623 |
|
|
&& is_string($dmap[$methName]['function'])
|
624 |
|
|
&& strpos($dmap[$methName]['function'], '::') !== false)
|
625 |
|
|
{
|
626 |
|
|
$dmap[$methName]['function'] =
|
627 |
|
|
explode('::', $dmap[$methName]['function']);
|
628 |
|
|
}
|
629 |
|
|
|
630 |
|
|
if (isset($dmap[$methName]['function'])
|
631 |
|
|
&& is_callable($dmap[$methName]['function']))
|
632 |
|
|
{
|
633 |
|
|
// dispatch if exists
|
634 |
|
|
if (isset($dmap[$methName]['signature'])) {
|
635 |
|
|
$sr = $this->verifySignature($m,
|
636 |
|
|
$dmap[$methName]['signature'] );
|
637 |
|
|
}
|
638 |
605938e5
|
Bill Marquette
|
if (!isset($dmap[$methName]['signature']) || $sr[0]) {
|
639 |
7ae92444
|
Colin Smith
|
// if no signature or correct signature
|
640 |
|
|
if ($sysCall) {
|
641 |
|
|
$r = call_user_func($dmap[$methName]['function'], $this, $m);
|
642 |
|
|
} else {
|
643 |
|
|
$r = call_user_func($dmap[$methName]['function'], $m);
|
644 |
|
|
}
|
645 |
605938e5
|
Bill Marquette
|
if (!is_a($r, 'XML_RPC_Response')) {
|
646 |
|
|
$r = new XML_RPC_Response(0, $XML_RPC_err['not_response_object'],
|
647 |
|
|
$XML_RPC_str['not_response_object']);
|
648 |
|
|
}
|
649 |
7ae92444
|
Colin Smith
|
} else {
|
650 |
|
|
$r = new XML_RPC_Response(0, $XML_RPC_err['incorrect_params'],
|
651 |
|
|
$XML_RPC_str['incorrect_params']
|
652 |
|
|
. ': ' . $sr[1]);
|
653 |
|
|
}
|
654 |
|
|
} else {
|
655 |
|
|
// else prepare error response
|
656 |
|
|
$r = new XML_RPC_Response(0, $XML_RPC_err['unknown_method'],
|
657 |
|
|
$XML_RPC_str['unknown_method']);
|
658 |
|
|
}
|
659 |
|
|
}
|
660 |
|
|
return $r;
|
661 |
|
|
}
|
662 |
|
|
|
663 |
|
|
/**
|
664 |
|
|
* Echos back the input packet as a string value
|
665 |
|
|
*
|
666 |
|
|
* @return void
|
667 |
|
|
*
|
668 |
|
|
* Useful for debugging.
|
669 |
|
|
*/
|
670 |
605938e5
|
Bill Marquette
|
function echoInput()
|
671 |
|
|
{
|
672 |
7ae92444
|
Colin Smith
|
global $HTTP_RAW_POST_DATA;
|
673 |
|
|
|
674 |
|
|
$r = new XML_RPC_Response(0);
|
675 |
|
|
$r->xv = new XML_RPC_Value("'Aha said I: '" . $HTTP_RAW_POST_DATA, 'string');
|
676 |
|
|
print $r->serialize();
|
677 |
|
|
}
|
678 |
|
|
}
|
679 |
|
|
|
680 |
|
|
/*
|
681 |
|
|
* Local variables:
|
682 |
|
|
* tab-width: 4
|
683 |
|
|
* c-basic-offset: 4
|
684 |
|
|
* c-hanging-comment-ender-p: nil
|
685 |
|
|
* End:
|
686 |
|
|
*/
|
687 |
|
|
|
688 |
cbe2ebe0
|
Scott Ullrich
|
?>
|