blob: 4680bbfa47de12d4de9afb310cc437745bae81e1 [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 Andreevcce6bd12018-01-09 11:32:02 +02009 * Copyright (c) 2014 - 2018, 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
Andrey Andreev1924e872016-01-11 12:55:34 +020031 * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
Andrey Andreevcce6bd12018-01-09 11:32:02 +020032 * @copyright Copyright (c) 2014 - 2018, British Columbia Institute of Technology (http://bcit.ca/)
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020033 * @license http://opensource.org/licenses/MIT MIT License
Andrey Andreevbd202c92016-01-11 12:50:18 +020034 * @link https://codeigniter.com
Andrey Andreevbdb96ca2014-10-28 00:13:31 +020035 * @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
Andrey Andreevbd202c92016-01-11 12:50:18 +020059 * @link https://codeigniter.com/user_guide/libraries/xmlrpc.html
Derek Allard2067d1a2008-11-13 22:59:24 +000060 */
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 {
Andrey Andreev7abc08a2015-03-26 21:10:49 +0200226 $data = $CI->input->raw_input_stream;
Andrey Andreev20e802e2014-02-24 12:16:48 +0200227 }
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']);
Andrey Andreev14a6c2e2016-10-31 10:04:17 +0200342 $objectCall = ! empty($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 Andreev14a6c2e2016-10-31 10:04:17 +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 {
Andrey Andreev14a6c2e2016-10-31 10:04:17 +0200403 return get_instance()->{$method_parts[1]}($m);
Andrey Andreev119d8a72014-01-08 15:27:53 +0200404 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200405
406 return $this->object->{$method_parts[1]}($m);
Derek Allard2067d1a2008-11-13 22:59:24 +0000407 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200408
409 return call_user_func($this->methods[$methName]['function'], $m);
Derek Allard2067d1a2008-11-13 22:59:24 +0000410 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200411
Pascal Kriete68d29872011-02-14 13:39:06 -0500412 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200413
Pascal Kriete68d29872011-02-14 13:39:06 -0500414 /**
vkeranov52de3792012-11-23 23:48:35 +0200415 * Server Function: List Methods
Pascal Kriete68d29872011-02-14 13:39:06 -0500416 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500417 * @param mixed
418 * @return object
419 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200420 public function listMethods($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $v = new XML_RPC_Values();
423 $output = array();
Barry Mienydd671972010-10-04 16:33:58 +0200424
Pascal Kriete68d29872011-02-14 13:39:06 -0500425 foreach ($this->methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
427 $output[] = new XML_RPC_Values($key, 'string');
428 }
Barry Mienydd671972010-10-04 16:33:58 +0200429
Pascal Kriete68d29872011-02-14 13:39:06 -0500430 foreach ($this->system_methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000431 {
Andrey Andreev52278372012-03-26 16:02:30 +0300432 $output[] = new XML_RPC_Values($key, 'string');
Derek Allard2067d1a2008-11-13 22:59:24 +0000433 }
434
435 $v->addArray($output);
436 return new XML_RPC_Response($v);
437 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200438
Pascal Kriete68d29872011-02-14 13:39:06 -0500439 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200440
Pascal Kriete68d29872011-02-14 13:39:06 -0500441 /**
vkeranov52de3792012-11-23 23:48:35 +0200442 * Server Function: Return Signature for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500443 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500444 * @param mixed
445 * @return object
446 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200447 public function methodSignature($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000448 {
449 $parameters = $m->output_parameters();
450 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200451
Derek Allard2067d1a2008-11-13 22:59:24 +0000452 if (isset($this->methods[$method_name]))
453 {
454 if ($this->methods[$method_name]['signature'])
455 {
456 $sigs = array();
457 $signature = $this->methods[$method_name]['signature'];
Barry Mienydd671972010-10-04 16:33:58 +0200458
Andrey Andreev56e32142011-12-25 18:36:31 +0200459 for ($i = 0, $c = count($signature); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000460 {
461 $cursig = array();
462 $inSig = $signature[$i];
Andrey Andreev56e32142011-12-25 18:36:31 +0200463 for ($j = 0, $jc = count($inSig); $j < $jc; $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000464 {
465 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
466 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200467 $sigs[] = new XML_RPC_Values($cursig, 'array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
Andrey Andreev52278372012-03-26 16:02:30 +0300469
470 return new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Andrey Andreev52278372012-03-26 16:02:30 +0300472
473 return new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 }
Andrey Andreev52278372012-03-26 16:02:30 +0300475
vkeranov52de3792012-11-23 23:48:35 +0200476 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000477 }
Barry Mienydd671972010-10-04 16:33:58 +0200478
Pascal Kriete68d29872011-02-14 13:39:06 -0500479 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200480
Pascal Kriete68d29872011-02-14 13:39:06 -0500481 /**
vkeranov52de3792012-11-23 23:48:35 +0200482 * Server Function: Doc String for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500483 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500484 * @param mixed
485 * @return object
486 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200487 public function methodHelp($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000488 {
489 $parameters = $m->output_parameters();
490 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200491
Derek Allard2067d1a2008-11-13 22:59:24 +0000492 if (isset($this->methods[$method_name]))
493 {
494 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
Barry Mienydd671972010-10-04 16:33:58 +0200495
Derek Allard2067d1a2008-11-13 22:59:24 +0000496 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
497 }
Andrey Andreevfbe4d792017-12-27 19:49:03 +0200498
499 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000500 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200501
Pascal Kriete68d29872011-02-14 13:39:06 -0500502 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000503
Pascal Kriete68d29872011-02-14 13:39:06 -0500504 /**
vkeranov52de3792012-11-23 23:48:35 +0200505 * Server Function: Multi-call
Pascal Kriete68d29872011-02-14 13:39:06 -0500506 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500507 * @param mixed
508 * @return object
509 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200510 public function multicall($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 {
512 // Disabled
513 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Barry Mienydd671972010-10-04 16:33:58 +0200514
Derek Allard2067d1a2008-11-13 22:59:24 +0000515 $parameters = $m->output_parameters();
516 $calls = $parameters[0];
517
518 $result = array();
519
520 foreach ($calls as $value)
521 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 $m = new XML_RPC_Message($value[0]);
vkeranov52de3792012-11-23 23:48:35 +0200523 $plist = '';
Barry Mienydd671972010-10-04 16:33:58 +0200524
Andrey Andreev56e32142011-12-25 18:36:31 +0200525 for ($i = 0, $c = count($value[1]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 {
527 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
528 }
Barry Mienydd671972010-10-04 16:33:58 +0200529
Derek Allard2067d1a2008-11-13 22:59:24 +0000530 $attempt = $this->_execute($m);
531
Alex Bilbied261b1e2012-06-02 11:12:16 +0100532 if ($attempt->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 {
534 return $attempt;
535 }
536
537 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
538 }
539
540 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Pascal Kriete68d29872011-02-14 13:39:06 -0500543 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200544
Pascal Kriete68d29872011-02-14 13:39:06 -0500545 /**
vkeranov52de3792012-11-23 23:48:35 +0200546 * Multi-call Function: Error Handling
Pascal Kriete68d29872011-02-14 13:39:06 -0500547 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500548 * @param mixed
549 * @return object
550 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200551 public function multicall_error($err)
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 {
vkeranov52de3792012-11-23 23:48:35 +0200553 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
Derek Allard2067d1a2008-11-13 22:59:24 +0000554 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
Barry Mienydd671972010-10-04 16:33:58 +0200555
Derek Allard2067d1a2008-11-13 22:59:24 +0000556 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
557 $struct['faultString'] = new XML_RPC_Values($str, 'string');
Barry Mienydd671972010-10-04 16:33:58 +0200558
Derek Allard2067d1a2008-11-13 22:59:24 +0000559 return new XML_RPC_Values($struct, 'struct');
560 }
Barry Mienydd671972010-10-04 16:33:58 +0200561
Pascal Kriete68d29872011-02-14 13:39:06 -0500562 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200563
Pascal Kriete68d29872011-02-14 13:39:06 -0500564 /**
vkeranov52de3792012-11-23 23:48:35 +0200565 * Multi-call Function: Processes method
Pascal Kriete68d29872011-02-14 13:39:06 -0500566 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500567 * @param mixed
568 * @return object
569 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200570 public function do_multicall($call)
Derek Allard2067d1a2008-11-13 22:59:24 +0000571 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100572 if ($call->kindOf() !== 'struct')
Pascal Kriete68d29872011-02-14 13:39:06 -0500573 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 return $this->multicall_error('notstruct');
Pascal Kriete68d29872011-02-14 13:39:06 -0500575 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 elseif ( ! $methName = $call->me['struct']['methodName'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500577 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000578 return $this->multicall_error('nomethod');
Pascal Kriete68d29872011-02-14 13:39:06 -0500579 }
Barry Mienydd671972010-10-04 16:33:58 +0200580
Andrey Andreev4316a152017-01-20 15:46:17 +0200581 list($scalar_value, $scalar_type) = array(reset($methName->me), key($methName->me));
Alex Bilbied261b1e2012-06-02 11:12:16 +0100582 $scalar_type = $scalar_type === $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
Barry Mienydd671972010-10-04 16:33:58 +0200583
Alex Bilbied261b1e2012-06-02 11:12:16 +0100584 if ($methName->kindOf() !== 'scalar' OR $scalar_type !== 'string')
Pascal Kriete68d29872011-02-14 13:39:06 -0500585 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 return $this->multicall_error('notstring');
Pascal Kriete68d29872011-02-14 13:39:06 -0500587 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100588 elseif ($scalar_value === 'system.multicall')
Pascal Kriete68d29872011-02-14 13:39:06 -0500589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 return $this->multicall_error('recursion');
Pascal Kriete68d29872011-02-14 13:39:06 -0500591 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000592 elseif ( ! $params = $call->me['struct']['params'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500593 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 return $this->multicall_error('noparams');
Pascal Kriete68d29872011-02-14 13:39:06 -0500595 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100596 elseif ($params->kindOf() !== 'array')
Pascal Kriete68d29872011-02-14 13:39:06 -0500597 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000598 return $this->multicall_error('notarray');
Pascal Kriete68d29872011-02-14 13:39:06 -0500599 }
Barry Mienydd671972010-10-04 16:33:58 +0200600
Andrey Andreev4316a152017-01-20 15:46:17 +0200601 list($b, $a) = array(reset($params->me), key($params->me));
Derek Allard2067d1a2008-11-13 22:59:24 +0000602
603 $msg = new XML_RPC_Message($scalar_value);
Andrey Andreev56e32142011-12-25 18:36:31 +0200604 for ($i = 0, $numParams = count($b); $i < $numParams; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000605 {
606 $msg->params[] = $params->me['array'][$i];
607 }
608
609 $result = $this->_execute($msg);
610
Alex Bilbied261b1e2012-06-02 11:12:16 +0100611 if ($result->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000612 {
613 return $this->multicall_error($result);
614 }
615
616 return new XML_RPC_Values(array($result->value()), 'array');
Barry Mienydd671972010-10-04 16:33:58 +0200617 }
618
Derek Allard2067d1a2008-11-13 22:59:24 +0000619}