blob: c2768445e32e50cd4645a5787d255e0a5f941ef5 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02005 * An open source application development framework for PHP
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +02007 * This content is released under the MIT License (MIT)
Andrey Andreev56e32142011-12-25 18:36:31 +02008 *
Andrey Andreevfe9309d2015-01-09 17:48:58 +02009 * Copyright (c) 2014 - 2015, British Columbia Institute of Technology
Andrey Andreev56e32142011-12-25 18:36:31 +020010 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020011 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
Derek Jonesf4a4bd82011-10-20 12:18:42 -050017 *
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020018 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * THE SOFTWARE.
28 *
29 * @package CodeIgniter
30 * @author EllisLab Dev Team
darwinel871754a2014-02-11 17:34:57 +010031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (http://ellislab.com/)
Andrey Andreevfe9309d2015-01-09 17:48:58 +020032 * @copyright Copyright (c) 2014 - 2015, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
34 * @link http://codeigniter.com
35 * @since Version 1.0.0
Derek Allard2067d1a2008-11-13 22:59:24 +000036 * @filesource
37 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020038defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000039
40if ( ! function_exists('xml_parser_create'))
Barry Mienydd671972010-10-04 16:33:58 +020041{
Derek Allard2067d1a2008-11-13 22:59:24 +000042 show_error('Your PHP installation does not support XML');
43}
44
Andrey Andreev49e68de2013-02-21 16:30:55 +020045if ( ! class_exists('CI_Xmlrpc', FALSE))
Derek Allard2067d1a2008-11-13 22:59:24 +000046{
47 show_error('You must load the Xmlrpc class before loading the Xmlrpcs class in order to create a server.');
48}
49
50// ------------------------------------------------------------------------
51
52/**
53 * XML-RPC server class
54 *
55 * @package CodeIgniter
56 * @subpackage Libraries
57 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -050058 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000059 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
60 */
vkeranov52de3792012-11-23 23:48:35 +020061class CI_Xmlrpcs extends CI_Xmlrpc {
62
Timothy Warren0688ac92012-04-20 10:25:04 -040063 /**
vkeranov52de3792012-11-23 23:48:35 +020064 * Array of methods mapped to function names and signatures
Timothy Warren0688ac92012-04-20 10:25:04 -040065 *
66 * @var array
67 */
68 public $methods = array();
Andrey Andreev56454792012-05-17 14:32:19 +030069
Timothy Warren0688ac92012-04-20 10:25:04 -040070 /**
71 * Debug Message
72 *
73 * @var string
74 */
75 public $debug_msg = '';
Andrey Andreev56454792012-05-17 14:32:19 +030076
Timothy Warren0688ac92012-04-20 10:25:04 -040077 /**
78 * XML RPC Server methods
79 *
80 * @var array
81 */
82 public $system_methods = array();
Andrey Andreev56454792012-05-17 14:32:19 +030083
Timothy Warren0688ac92012-04-20 10:25:04 -040084 /**
85 * Configuration object
86 *
87 * @var object
88 */
89 public $object = FALSE;
Derek Jonesc4c34ee2010-03-02 23:00:28 -060090
Timothy Warren0688ac92012-04-20 10:25:04 -040091 /**
92 * Initialize XMLRPC class
93 *
Andrey Andreev56454792012-05-17 14:32:19 +030094 * @param array $config
95 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040096 */
Andrey Andreev52278372012-03-26 16:02:30 +030097 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020098 {
Greg Akera9263282010-11-10 15:26:43 -060099 parent::__construct();
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 $this->set_system_methods();
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 if (isset($config['functions']) && is_array($config['functions']))
103 {
104 $this->methods = array_merge($this->methods, $config['functions']);
105 }
Barry Mienydd671972010-10-04 16:33:58 +0200106
Andrey Andreev90726b82015-01-20 12:39:22 +0200107 log_message('info', 'XML-RPC Server Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +0000108 }
Barry Mienydd671972010-10-04 16:33:58 +0200109
Pascal Kriete68d29872011-02-14 13:39:06 -0500110 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200111
Pascal Kriete68d29872011-02-14 13:39:06 -0500112 /**
113 * Initialize Prefs and Serve
114 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500115 * @param mixed
116 * @return void
117 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200118 public function initialize($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200119 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000120 if (isset($config['functions']) && is_array($config['functions']))
121 {
122 $this->methods = array_merge($this->methods, $config['functions']);
123 }
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 if (isset($config['debug']))
126 {
127 $this->debug = $config['debug'];
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Derek Allard2067d1a2008-11-13 22:59:24 +0000130 if (isset($config['object']) && is_object($config['object']))
131 {
132 $this->object = $config['object'];
133 }
Barry Mienydd671972010-10-04 16:33:58 +0200134
Robin Sowell66a3fc02010-03-18 09:44:55 -0400135 if (isset($config['xss_clean']))
136 {
137 $this->xss_clean = $config['xss_clean'];
138 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 }
Barry Mienydd671972010-10-04 16:33:58 +0200140
Pascal Kriete68d29872011-02-14 13:39:06 -0500141 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200142
Pascal Kriete68d29872011-02-14 13:39:06 -0500143 /**
144 * Setting of System Methods
145 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500146 * @return void
147 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200148 public function set_system_methods()
Derek Allard2067d1a2008-11-13 22:59:24 +0000149 {
150 $this->methods = array(
151 'system.listMethods' => array(
Andrey Andreev56e32142011-12-25 18:36:31 +0200152 'function' => 'this.listMethods',
153 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
154 'docstring' => 'Returns an array of available methods on this server'),
155 'system.methodHelp' => array(
156 'function' => 'this.methodHelp',
157 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
158 'docstring' => 'Returns a documentation string for the specified method'),
Derek Allard2067d1a2008-11-13 22:59:24 +0000159 'system.methodSignature' => array(
Andrey Andreev56e32142011-12-25 18:36:31 +0200160 'function' => 'this.methodSignature',
161 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
162 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
163 'system.multicall' => array(
164 'function' => 'this.multicall',
165 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
166 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
167 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000168 }
169
Pascal Kriete68d29872011-02-14 13:39:06 -0500170 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000171
Pascal Kriete68d29872011-02-14 13:39:06 -0500172 /**
173 * Main Server Function
174 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500175 * @return void
176 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200177 public function serve()
Derek Allard2067d1a2008-11-13 22:59:24 +0000178 {
179 $r = $this->parseRequest();
Andrey Andreev56e32142011-12-25 18:36:31 +0200180 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n".$this->debug_msg.$r->prepare_response();
Barry Mienydd671972010-10-04 16:33:58 +0200181
Andrey Andreev52278372012-03-26 16:02:30 +0300182 header('Content-Type: text/xml');
183 header('Content-Length: '.strlen($payload));
Derek Jonesb8d3c3d2009-06-24 15:27:01 +0000184 exit($payload);
Derek Allard2067d1a2008-11-13 22:59:24 +0000185 }
186
Pascal Kriete68d29872011-02-14 13:39:06 -0500187 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200188
Pascal Kriete68d29872011-02-14 13:39:06 -0500189 /**
190 * Add Method to Class
191 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500192 * @param string method name
193 * @param string function
194 * @param string signature
195 * @param string docstring
196 * @return void
197 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200198 public function add_to_map($methodname, $function, $sig, $doc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 {
200 $this->methods[$methodname] = array(
vkeranov52de3792012-11-23 23:48:35 +0200201 'function' => $function,
202 'signature' => $sig,
203 'docstring' => $doc
Derek Allard2067d1a2008-11-13 22:59:24 +0000204 );
205 }
206
Pascal Kriete68d29872011-02-14 13:39:06 -0500207 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000208
Pascal Kriete68d29872011-02-14 13:39:06 -0500209 /**
210 * Parse Server Request
211 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500212 * @param string data
213 * @return object xmlrpc response
214 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200215 public function parseRequest($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000216 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000217 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500218 // Get Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 //-------------------------------------
220
Alex Bilbied261b1e2012-06-02 11:12:16 +0100221 if ($data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000222 {
Andrey Andreev20e802e2014-02-24 12:16:48 +0200223 $CI =& get_instance();
224 if ($CI->input->method() === 'post')
225 {
226 $data = http_build_query($CI->input->input_stream(NULL, FALSE));
227 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000228 }
229
230 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500231 // Set up XML Parser
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200233
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 $parser = xml_parser_create($this->xmlrpc_defencoding);
Andrey Andreev52278372012-03-26 16:02:30 +0300235 $parser_object = new XML_RPC_Message('filler');
Andrey Andreev998608e2015-03-26 13:01:56 +0200236 $pname = (string) $parser;
Barry Mienydd671972010-10-04 16:33:58 +0200237
Andrey Andreev998608e2015-03-26 13:01:56 +0200238 $parser_object->xh[$pname] = array(
239 'isf' => 0,
240 'isf_reason' => '',
241 'params' => array(),
242 'stack' => array(),
243 'valuestack' => array(),
244 'method' => ''
Andrey Andreev20e802e2014-02-24 12:16:48 +0200245 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000246
247 xml_set_object($parser, $parser_object);
Alex Bilbieafee2262012-07-15 18:59:01 +0100248 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
250 xml_set_character_data_handler($parser, 'character_data');
251 //xml_set_default_handler($parser, 'default_handler');
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200254 // PARSE + PROCESS XML DATA
Barry Mienydd671972010-10-04 16:33:58 +0200255 //-------------------------------------
256
Derek Allard2067d1a2008-11-13 22:59:24 +0000257 if ( ! xml_parse($parser, $data, 1))
258 {
vkeranov52de3792012-11-23 23:48:35 +0200259 // Return XML error as a faultCode
Derek Allard2067d1a2008-11-13 22:59:24 +0000260 $r = new XML_RPC_Response(0,
vkeranov52de3792012-11-23 23:48:35 +0200261 $this->xmlrpcerrxml + xml_get_error_code($parser),
262 sprintf('XML error: %s at line %d',
Derek Allard2067d1a2008-11-13 22:59:24 +0000263 xml_error_string(xml_get_error_code($parser)),
264 xml_get_current_line_number($parser)));
265 xml_parser_free($parser);
266 }
Andrey Andreev998608e2015-03-26 13:01:56 +0200267 elseif ($parser_object->xh[$pname]['isf'])
Derek Allard2067d1a2008-11-13 22:59:24 +0000268 {
269 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
270 }
271 else
272 {
273 xml_parser_free($parser);
Barry Mienydd671972010-10-04 16:33:58 +0200274
Andrey Andreev998608e2015-03-26 13:01:56 +0200275 $m = new XML_RPC_Message($parser_object->xh[$pname]['method']);
vkeranov52de3792012-11-23 23:48:35 +0200276 $plist = '';
Barry Mienydd671972010-10-04 16:33:58 +0200277
Andrey Andreev998608e2015-03-26 13:01:56 +0200278 for ($i = 0, $c = count($parser_object->xh[$pname]['params']); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 {
280 if ($this->debug === TRUE)
281 {
Andrey Andreev998608e2015-03-26 13:01:56 +0200282 $plist .= $i.' - '.print_r(get_object_vars($parser_object->xh[$pname]['params'][$i]), TRUE).";\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000283 }
Barry Mienydd671972010-10-04 16:33:58 +0200284
Andrey Andreev998608e2015-03-26 13:01:56 +0200285 $m->addParam($parser_object->xh[$pname]['params'][$i]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 }
Barry Mienydd671972010-10-04 16:33:58 +0200287
Derek Allard2067d1a2008-11-13 22:59:24 +0000288 if ($this->debug === TRUE)
289 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200290 echo "<pre>---PLIST---\n".$plist."\n---PLIST END---\n\n</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 }
Barry Mienydd671972010-10-04 16:33:58 +0200292
Derek Allard2067d1a2008-11-13 22:59:24 +0000293 $r = $this->_execute($m);
294 }
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200297 // SET DEBUGGING MESSAGE
Barry Mienydd671972010-10-04 16:33:58 +0200298 //-------------------------------------
299
Derek Allard2067d1a2008-11-13 22:59:24 +0000300 if ($this->debug === TRUE)
301 {
302 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
303 }
Barry Mienydd671972010-10-04 16:33:58 +0200304
Derek Allard2067d1a2008-11-13 22:59:24 +0000305 return $r;
306 }
307
Pascal Kriete68d29872011-02-14 13:39:06 -0500308 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200309
Pascal Kriete68d29872011-02-14 13:39:06 -0500310 /**
311 * Executes the Method
312 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500313 * @param object
314 * @return mixed
315 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200316 protected function _execute($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000317 {
318 $methName = $m->method_name;
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 // Check to see if it is a system call
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300321 $system_call = (strpos($methName, 'system') === 0);
Barry Mienydd671972010-10-04 16:33:58 +0200322
Alex Bilbied261b1e2012-06-02 11:12:16 +0100323 if ($this->xss_clean === FALSE)
Robin Sowell66a3fc02010-03-18 09:44:55 -0400324 {
325 $m->xss_clean = FALSE;
326 }
327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200329 // Valid Method
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 if ( ! isset($this->methods[$methName]['function']))
333 {
334 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
335 }
Barry Mienydd671972010-10-04 16:33:58 +0200336
Derek Allard2067d1a2008-11-13 22:59:24 +0000337 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200338 // Check for Method (and Object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200340
Andrey Andreev52278372012-03-26 16:02:30 +0300341 $method_parts = explode('.', $this->methods[$methName]['function']);
Alex Bilbied261b1e2012-06-02 11:12:16 +0100342 $objectCall = (isset($method_parts[1]) && $method_parts[1] !== '');
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 if ($system_call === TRUE)
345 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200346 if ( ! is_callable(array($this,$method_parts[1])))
Derek Allard2067d1a2008-11-13 22:59:24 +0000347 {
348 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
349 }
350 }
Andrey Andreev52278372012-03-26 16:02:30 +0300351 elseif (($objectCall && ! is_callable(array($method_parts[0], $method_parts[1])))
352 OR ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
353 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000354 {
Andrey Andreev52278372012-03-26 16:02:30 +0300355 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 }
Barry Mienydd671972010-10-04 16:33:58 +0200357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200359 // Checking Methods Signature
Derek Allard2067d1a2008-11-13 22:59:24 +0000360 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200361
Derek Allard2067d1a2008-11-13 22:59:24 +0000362 if (isset($this->methods[$methName]['signature']))
363 {
364 $sig = $this->methods[$methName]['signature'];
Andrey Andreev56e32142011-12-25 18:36:31 +0200365 for ($i = 0, $c = count($sig); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000366 {
367 $current_sig = $sig[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200368
Andrey Andreev56e32142011-12-25 18:36:31 +0200369 if (count($current_sig) === count($m->params)+1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000370 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200371 for ($n = 0, $mc = count($m->params); $n < $mc; $n++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 {
373 $p = $m->params[$n];
Alex Bilbied261b1e2012-06-02 11:12:16 +0100374 $pt = ($p->kindOf() === 'scalar') ? $p->scalarval() : $p->kindOf();
Barry Mienydd671972010-10-04 16:33:58 +0200375
Alex Bilbied261b1e2012-06-02 11:12:16 +0100376 if ($pt !== $current_sig[$n+1])
Derek Allard2067d1a2008-11-13 22:59:24 +0000377 {
378 $pno = $n+1;
379 $wanted = $current_sig[$n+1];
Barry Mienydd671972010-10-04 16:33:58 +0200380
Derek Allard2067d1a2008-11-13 22:59:24 +0000381 return new XML_RPC_Response(0,
382 $this->xmlrpcerr['incorrect_params'],
383 $this->xmlrpcstr['incorrect_params'] .
Andrey Andreev52278372012-03-26 16:02:30 +0300384 ': Wanted '.$wanted.', got '.$pt.' at param '.$pno.')');
Derek Allard2067d1a2008-11-13 22:59:24 +0000385 }
386 }
387 }
388 }
389 }
390
391 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200392 // Calls the Function
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 //-------------------------------------
394
395 if ($objectCall === TRUE)
396 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200397 if ($method_parts[0] === 'this' && $system_call === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 {
399 return call_user_func(array($this, $method_parts[1]), $m);
400 }
Andrey Andreev119d8a72014-01-08 15:27:53 +0200401 elseif ($this->object === FALSE)
402 {
403 return get_instance()->$method_parts[1]($m);
404 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000405 else
406 {
Andrey Andreev119d8a72014-01-08 15:27:53 +0200407 return $this->object->$method_parts[1]($m);
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 }
409 }
410 else
411 {
412 return call_user_func($this->methods[$methName]['function'], $m);
413 }
414 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200415
Pascal Kriete68d29872011-02-14 13:39:06 -0500416 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200417
Pascal Kriete68d29872011-02-14 13:39:06 -0500418 /**
vkeranov52de3792012-11-23 23:48:35 +0200419 * Server Function: List Methods
Pascal Kriete68d29872011-02-14 13:39:06 -0500420 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500421 * @param mixed
422 * @return object
423 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200424 public function listMethods($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
426 $v = new XML_RPC_Values();
427 $output = array();
Barry Mienydd671972010-10-04 16:33:58 +0200428
Pascal Kriete68d29872011-02-14 13:39:06 -0500429 foreach ($this->methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000430 {
431 $output[] = new XML_RPC_Values($key, 'string');
432 }
Barry Mienydd671972010-10-04 16:33:58 +0200433
Pascal Kriete68d29872011-02-14 13:39:06 -0500434 foreach ($this->system_methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000435 {
Andrey Andreev52278372012-03-26 16:02:30 +0300436 $output[] = new XML_RPC_Values($key, 'string');
Derek Allard2067d1a2008-11-13 22:59:24 +0000437 }
438
439 $v->addArray($output);
440 return new XML_RPC_Response($v);
441 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200442
Pascal Kriete68d29872011-02-14 13:39:06 -0500443 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200444
Pascal Kriete68d29872011-02-14 13:39:06 -0500445 /**
vkeranov52de3792012-11-23 23:48:35 +0200446 * Server Function: Return Signature for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500447 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500448 * @param mixed
449 * @return object
450 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200451 public function methodSignature($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 {
453 $parameters = $m->output_parameters();
454 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200455
Derek Allard2067d1a2008-11-13 22:59:24 +0000456 if (isset($this->methods[$method_name]))
457 {
458 if ($this->methods[$method_name]['signature'])
459 {
460 $sigs = array();
461 $signature = $this->methods[$method_name]['signature'];
Barry Mienydd671972010-10-04 16:33:58 +0200462
Andrey Andreev56e32142011-12-25 18:36:31 +0200463 for ($i = 0, $c = count($signature); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $cursig = array();
466 $inSig = $signature[$i];
Andrey Andreev56e32142011-12-25 18:36:31 +0200467 for ($j = 0, $jc = count($inSig); $j < $jc; $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 {
469 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
470 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200471 $sigs[] = new XML_RPC_Values($cursig, 'array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Andrey Andreev52278372012-03-26 16:02:30 +0300473
474 return new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000475 }
Andrey Andreev52278372012-03-26 16:02:30 +0300476
477 return new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000478 }
Andrey Andreev52278372012-03-26 16:02:30 +0300479
vkeranov52de3792012-11-23 23:48:35 +0200480 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000481 }
Barry Mienydd671972010-10-04 16:33:58 +0200482
Pascal Kriete68d29872011-02-14 13:39:06 -0500483 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200484
Pascal Kriete68d29872011-02-14 13:39:06 -0500485 /**
vkeranov52de3792012-11-23 23:48:35 +0200486 * Server Function: Doc String for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500487 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500488 * @param mixed
489 * @return object
490 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200491 public function methodHelp($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 {
493 $parameters = $m->output_parameters();
494 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 if (isset($this->methods[$method_name]))
497 {
498 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
Barry Mienydd671972010-10-04 16:33:58 +0200499
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
501 }
502 else
503 {
504 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
505 }
506 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200507
Pascal Kriete68d29872011-02-14 13:39:06 -0500508 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000509
Pascal Kriete68d29872011-02-14 13:39:06 -0500510 /**
vkeranov52de3792012-11-23 23:48:35 +0200511 * Server Function: Multi-call
Pascal Kriete68d29872011-02-14 13:39:06 -0500512 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500513 * @param mixed
514 * @return object
515 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200516 public function multicall($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000517 {
518 // Disabled
519 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $parameters = $m->output_parameters();
522 $calls = $parameters[0];
523
524 $result = array();
525
526 foreach ($calls as $value)
527 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000528 $m = new XML_RPC_Message($value[0]);
vkeranov52de3792012-11-23 23:48:35 +0200529 $plist = '';
Barry Mienydd671972010-10-04 16:33:58 +0200530
Andrey Andreev56e32142011-12-25 18:36:31 +0200531 for ($i = 0, $c = count($value[1]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
534 }
Barry Mienydd671972010-10-04 16:33:58 +0200535
Derek Allard2067d1a2008-11-13 22:59:24 +0000536 $attempt = $this->_execute($m);
537
Alex Bilbied261b1e2012-06-02 11:12:16 +0100538 if ($attempt->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000539 {
540 return $attempt;
541 }
542
543 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
544 }
545
546 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
547 }
Barry Mienydd671972010-10-04 16:33:58 +0200548
Pascal Kriete68d29872011-02-14 13:39:06 -0500549 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200550
Pascal Kriete68d29872011-02-14 13:39:06 -0500551 /**
vkeranov52de3792012-11-23 23:48:35 +0200552 * Multi-call Function: Error Handling
Pascal Kriete68d29872011-02-14 13:39:06 -0500553 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500554 * @param mixed
555 * @return object
556 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200557 public function multicall_error($err)
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 {
vkeranov52de3792012-11-23 23:48:35 +0200559 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
Derek Allard2067d1a2008-11-13 22:59:24 +0000560 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
Barry Mienydd671972010-10-04 16:33:58 +0200561
Derek Allard2067d1a2008-11-13 22:59:24 +0000562 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
563 $struct['faultString'] = new XML_RPC_Values($str, 'string');
Barry Mienydd671972010-10-04 16:33:58 +0200564
Derek Allard2067d1a2008-11-13 22:59:24 +0000565 return new XML_RPC_Values($struct, 'struct');
566 }
Barry Mienydd671972010-10-04 16:33:58 +0200567
Pascal Kriete68d29872011-02-14 13:39:06 -0500568 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200569
Pascal Kriete68d29872011-02-14 13:39:06 -0500570 /**
vkeranov52de3792012-11-23 23:48:35 +0200571 * Multi-call Function: Processes method
Pascal Kriete68d29872011-02-14 13:39:06 -0500572 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500573 * @param mixed
574 * @return object
575 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200576 public function do_multicall($call)
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100578 if ($call->kindOf() !== 'struct')
Pascal Kriete68d29872011-02-14 13:39:06 -0500579 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 return $this->multicall_error('notstruct');
Pascal Kriete68d29872011-02-14 13:39:06 -0500581 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 elseif ( ! $methName = $call->me['struct']['methodName'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500583 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000584 return $this->multicall_error('nomethod');
Pascal Kriete68d29872011-02-14 13:39:06 -0500585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
vkeranov52de3792012-11-23 23:48:35 +0200587 list($scalar_type, $scalar_value) = each($methName->me);
Alex Bilbied261b1e2012-06-02 11:12:16 +0100588 $scalar_type = $scalar_type === $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
Barry Mienydd671972010-10-04 16:33:58 +0200589
Alex Bilbied261b1e2012-06-02 11:12:16 +0100590 if ($methName->kindOf() !== 'scalar' OR $scalar_type !== 'string')
Pascal Kriete68d29872011-02-14 13:39:06 -0500591 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 return $this->multicall_error('notstring');
Pascal Kriete68d29872011-02-14 13:39:06 -0500593 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100594 elseif ($scalar_value === 'system.multicall')
Pascal Kriete68d29872011-02-14 13:39:06 -0500595 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000596 return $this->multicall_error('recursion');
Pascal Kriete68d29872011-02-14 13:39:06 -0500597 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 elseif ( ! $params = $call->me['struct']['params'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500599 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000600 return $this->multicall_error('noparams');
Pascal Kriete68d29872011-02-14 13:39:06 -0500601 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100602 elseif ($params->kindOf() !== 'array')
Pascal Kriete68d29872011-02-14 13:39:06 -0500603 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 return $this->multicall_error('notarray');
Pascal Kriete68d29872011-02-14 13:39:06 -0500605 }
Barry Mienydd671972010-10-04 16:33:58 +0200606
vkeranov52de3792012-11-23 23:48:35 +0200607 list($a, $b) = each($params->me);
Derek Allard2067d1a2008-11-13 22:59:24 +0000608
609 $msg = new XML_RPC_Message($scalar_value);
Andrey Andreev56e32142011-12-25 18:36:31 +0200610 for ($i = 0, $numParams = count($b); $i < $numParams; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 $msg->params[] = $params->me['array'][$i];
613 }
614
615 $result = $this->_execute($msg);
616
Alex Bilbied261b1e2012-06-02 11:12:16 +0100617 if ($result->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000618 {
619 return $this->multicall_error($result);
620 }
621
622 return new XML_RPC_Values(array($result->value()), 'array');
Barry Mienydd671972010-10-04 16:33:58 +0200623 }
624
Derek Allard2067d1a2008-11-13 22:59:24 +0000625}