blob: d6c3416c94d8de580801e308056aca5f3363ce02 [file] [log] [blame]
Andrey Andreevc5536aa2012-11-01 17:33:58 +02001<?php
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Phil Sturgeon07c1ac82012-03-09 17:03:37 +00005 * An open source application development framework for PHP 5.2.4 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
Andrey Andreev56e32142011-12-25 18:36:31 +02008 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05009 * Licensed under the Open Software License version 3.0
Andrey Andreev56e32142011-12-25 18:36:31 +020010 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -050011 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
Greg Aker0defe5d2012-01-01 18:46:41 -060021 * @copyright Copyright (c) 2008 - 2012, EllisLab, Inc. (http://ellislab.com/)
Derek Jonesf4a4bd82011-10-20 12:18:42 -050022 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
Andrey Andreevc5536aa2012-11-01 17:33:58 +020027defined('BASEPATH') OR exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +000028
29if ( ! function_exists('xml_parser_create'))
Barry Mienydd671972010-10-04 16:33:58 +020030{
Derek Allard2067d1a2008-11-13 22:59:24 +000031 show_error('Your PHP installation does not support XML');
32}
33
34if ( ! class_exists('CI_Xmlrpc'))
35{
36 show_error('You must load the Xmlrpc class before loading the Xmlrpcs class in order to create a server.');
37}
38
39// ------------------------------------------------------------------------
40
41/**
42 * XML-RPC server class
43 *
44 * @package CodeIgniter
45 * @subpackage Libraries
46 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -050047 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000048 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
49 */
vkeranov52de3792012-11-23 23:48:35 +020050class CI_Xmlrpcs extends CI_Xmlrpc {
51
Timothy Warren0688ac92012-04-20 10:25:04 -040052 /**
vkeranov52de3792012-11-23 23:48:35 +020053 * Array of methods mapped to function names and signatures
Timothy Warren0688ac92012-04-20 10:25:04 -040054 *
55 * @var array
56 */
57 public $methods = array();
Andrey Andreev56454792012-05-17 14:32:19 +030058
Timothy Warren0688ac92012-04-20 10:25:04 -040059 /**
60 * Debug Message
61 *
62 * @var string
63 */
64 public $debug_msg = '';
Andrey Andreev56454792012-05-17 14:32:19 +030065
Timothy Warren0688ac92012-04-20 10:25:04 -040066 /**
67 * XML RPC Server methods
68 *
69 * @var array
70 */
71 public $system_methods = array();
Andrey Andreev56454792012-05-17 14:32:19 +030072
Timothy Warren0688ac92012-04-20 10:25:04 -040073 /**
74 * Configuration object
75 *
76 * @var object
77 */
78 public $object = FALSE;
Derek Jonesc4c34ee2010-03-02 23:00:28 -060079
Timothy Warren0688ac92012-04-20 10:25:04 -040080 /**
81 * Initialize XMLRPC class
82 *
Andrey Andreev56454792012-05-17 14:32:19 +030083 * @param array $config
84 * @return void
Timothy Warren0688ac92012-04-20 10:25:04 -040085 */
Andrey Andreev52278372012-03-26 16:02:30 +030086 public function __construct($config = array())
Barry Mienydd671972010-10-04 16:33:58 +020087 {
Greg Akera9263282010-11-10 15:26:43 -060088 parent::__construct();
Derek Allard2067d1a2008-11-13 22:59:24 +000089 $this->set_system_methods();
Barry Mienydd671972010-10-04 16:33:58 +020090
Derek Allard2067d1a2008-11-13 22:59:24 +000091 if (isset($config['functions']) && is_array($config['functions']))
92 {
93 $this->methods = array_merge($this->methods, $config['functions']);
94 }
Barry Mienydd671972010-10-04 16:33:58 +020095
Andrey Andreev52278372012-03-26 16:02:30 +030096 log_message('debug', 'XML-RPC Server Class Initialized');
Derek Allard2067d1a2008-11-13 22:59:24 +000097 }
Barry Mienydd671972010-10-04 16:33:58 +020098
Pascal Kriete68d29872011-02-14 13:39:06 -050099 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200100
Pascal Kriete68d29872011-02-14 13:39:06 -0500101 /**
102 * Initialize Prefs and Serve
103 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500104 * @param mixed
105 * @return void
106 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200107 public function initialize($config = array())
Barry Mienydd671972010-10-04 16:33:58 +0200108 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000109 if (isset($config['functions']) && is_array($config['functions']))
110 {
111 $this->methods = array_merge($this->methods, $config['functions']);
112 }
Barry Mienydd671972010-10-04 16:33:58 +0200113
Derek Allard2067d1a2008-11-13 22:59:24 +0000114 if (isset($config['debug']))
115 {
116 $this->debug = $config['debug'];
117 }
Barry Mienydd671972010-10-04 16:33:58 +0200118
Derek Allard2067d1a2008-11-13 22:59:24 +0000119 if (isset($config['object']) && is_object($config['object']))
120 {
121 $this->object = $config['object'];
122 }
Barry Mienydd671972010-10-04 16:33:58 +0200123
Robin Sowell66a3fc02010-03-18 09:44:55 -0400124 if (isset($config['xss_clean']))
125 {
126 $this->xss_clean = $config['xss_clean'];
127 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
Pascal Kriete68d29872011-02-14 13:39:06 -0500130 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200131
Pascal Kriete68d29872011-02-14 13:39:06 -0500132 /**
133 * Setting of System Methods
134 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500135 * @return void
136 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200137 public function set_system_methods()
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 $this->methods = array(
140 'system.listMethods' => array(
Andrey Andreev56e32142011-12-25 18:36:31 +0200141 'function' => 'this.listMethods',
142 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
143 'docstring' => 'Returns an array of available methods on this server'),
144 'system.methodHelp' => array(
145 'function' => 'this.methodHelp',
146 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
147 'docstring' => 'Returns a documentation string for the specified method'),
Derek Allard2067d1a2008-11-13 22:59:24 +0000148 'system.methodSignature' => array(
Andrey Andreev56e32142011-12-25 18:36:31 +0200149 'function' => 'this.methodSignature',
150 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
151 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
152 'system.multicall' => array(
153 'function' => 'this.multicall',
154 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
155 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
156 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000157 }
158
Pascal Kriete68d29872011-02-14 13:39:06 -0500159 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000160
Pascal Kriete68d29872011-02-14 13:39:06 -0500161 /**
162 * Main Server Function
163 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500164 * @return void
165 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200166 public function serve()
Derek Allard2067d1a2008-11-13 22:59:24 +0000167 {
168 $r = $this->parseRequest();
Andrey Andreev56e32142011-12-25 18:36:31 +0200169 $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n".$this->debug_msg.$r->prepare_response();
Barry Mienydd671972010-10-04 16:33:58 +0200170
Andrey Andreev52278372012-03-26 16:02:30 +0300171 header('Content-Type: text/xml');
172 header('Content-Length: '.strlen($payload));
Derek Jonesb8d3c3d2009-06-24 15:27:01 +0000173 exit($payload);
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 }
175
Pascal Kriete68d29872011-02-14 13:39:06 -0500176 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200177
Pascal Kriete68d29872011-02-14 13:39:06 -0500178 /**
179 * Add Method to Class
180 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500181 * @param string method name
182 * @param string function
183 * @param string signature
184 * @param string docstring
185 * @return void
186 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200187 public function add_to_map($methodname, $function, $sig, $doc)
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 {
189 $this->methods[$methodname] = array(
vkeranov52de3792012-11-23 23:48:35 +0200190 'function' => $function,
191 'signature' => $sig,
192 'docstring' => $doc
Derek Allard2067d1a2008-11-13 22:59:24 +0000193 );
194 }
195
Pascal Kriete68d29872011-02-14 13:39:06 -0500196 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000197
Pascal Kriete68d29872011-02-14 13:39:06 -0500198 /**
199 * Parse Server Request
200 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500201 * @param string data
202 * @return object xmlrpc response
203 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200204 public function parseRequest($data = '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000205 {
206 global $HTTP_RAW_POST_DATA;
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500209 // Get Data
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 //-------------------------------------
211
Alex Bilbied261b1e2012-06-02 11:12:16 +0100212 if ($data === '')
Derek Allard2067d1a2008-11-13 22:59:24 +0000213 {
214 $data = $HTTP_RAW_POST_DATA;
215 }
216
217 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500218 // Set up XML Parser
Derek Allard2067d1a2008-11-13 22:59:24 +0000219 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200220
Derek Allard2067d1a2008-11-13 22:59:24 +0000221 $parser = xml_parser_create($this->xmlrpc_defencoding);
Andrey Andreev52278372012-03-26 16:02:30 +0300222 $parser_object = new XML_RPC_Message('filler');
Barry Mienydd671972010-10-04 16:33:58 +0200223
Andrey Andreev56e32142011-12-25 18:36:31 +0200224 $parser_object->xh[$parser] = array(
225 'isf' => 0,
226 'isf_reason' => '',
227 'params' => array(),
228 'stack' => array(),
229 'valuestack' => array(),
230 'method' => ''
231 );
Derek Allard2067d1a2008-11-13 22:59:24 +0000232
233 xml_set_object($parser, $parser_object);
Alex Bilbieafee2262012-07-15 18:59:01 +0100234 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, TRUE);
Derek Allard2067d1a2008-11-13 22:59:24 +0000235 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
236 xml_set_character_data_handler($parser, 'character_data');
237 //xml_set_default_handler($parser, 'default_handler');
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200240 // PARSE + PROCESS XML DATA
Barry Mienydd671972010-10-04 16:33:58 +0200241 //-------------------------------------
242
Derek Allard2067d1a2008-11-13 22:59:24 +0000243 if ( ! xml_parse($parser, $data, 1))
244 {
vkeranov52de3792012-11-23 23:48:35 +0200245 // Return XML error as a faultCode
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 $r = new XML_RPC_Response(0,
vkeranov52de3792012-11-23 23:48:35 +0200247 $this->xmlrpcerrxml + xml_get_error_code($parser),
248 sprintf('XML error: %s at line %d',
Derek Allard2067d1a2008-11-13 22:59:24 +0000249 xml_error_string(xml_get_error_code($parser)),
250 xml_get_current_line_number($parser)));
251 xml_parser_free($parser);
252 }
Pascal Kriete68d29872011-02-14 13:39:06 -0500253 elseif ($parser_object->xh[$parser]['isf'])
Derek Allard2067d1a2008-11-13 22:59:24 +0000254 {
255 return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
256 }
257 else
258 {
259 xml_parser_free($parser);
Barry Mienydd671972010-10-04 16:33:58 +0200260
Derek Allard2067d1a2008-11-13 22:59:24 +0000261 $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
vkeranov52de3792012-11-23 23:48:35 +0200262 $plist = '';
Barry Mienydd671972010-10-04 16:33:58 +0200263
Andrey Andreev56e32142011-12-25 18:36:31 +0200264 for ($i = 0, $c = count($parser_object->xh[$parser]['params']); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 {
266 if ($this->debug === TRUE)
267 {
Andrey Andreev52278372012-03-26 16:02:30 +0300268 $plist .= $i.' - '.print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE).";\n";
Derek Allard2067d1a2008-11-13 22:59:24 +0000269 }
Barry Mienydd671972010-10-04 16:33:58 +0200270
Derek Allard2067d1a2008-11-13 22:59:24 +0000271 $m->addParam($parser_object->xh[$parser]['params'][$i]);
272 }
Barry Mienydd671972010-10-04 16:33:58 +0200273
Derek Allard2067d1a2008-11-13 22:59:24 +0000274 if ($this->debug === TRUE)
275 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200276 echo "<pre>---PLIST---\n".$plist."\n---PLIST END---\n\n</pre>";
Derek Allard2067d1a2008-11-13 22:59:24 +0000277 }
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 $r = $this->_execute($m);
280 }
Barry Mienydd671972010-10-04 16:33:58 +0200281
Derek Allard2067d1a2008-11-13 22:59:24 +0000282 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200283 // SET DEBUGGING MESSAGE
Barry Mienydd671972010-10-04 16:33:58 +0200284 //-------------------------------------
285
Derek Allard2067d1a2008-11-13 22:59:24 +0000286 if ($this->debug === TRUE)
287 {
288 $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
289 }
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 return $r;
292 }
293
Pascal Kriete68d29872011-02-14 13:39:06 -0500294 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200295
Pascal Kriete68d29872011-02-14 13:39:06 -0500296 /**
297 * Executes the Method
298 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500299 * @param object
300 * @return mixed
301 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200302 protected function _execute($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000303 {
304 $methName = $m->method_name;
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 // Check to see if it is a system call
Andrey Andreev3dad2e72012-06-14 15:10:56 +0300307 $system_call = (strpos($methName, 'system') === 0);
Barry Mienydd671972010-10-04 16:33:58 +0200308
Alex Bilbied261b1e2012-06-02 11:12:16 +0100309 if ($this->xss_clean === FALSE)
Robin Sowell66a3fc02010-03-18 09:44:55 -0400310 {
311 $m->xss_clean = FALSE;
312 }
313
Derek Allard2067d1a2008-11-13 22:59:24 +0000314 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200315 // Valid Method
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 if ( ! isset($this->methods[$methName]['function']))
319 {
320 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
321 }
Barry Mienydd671972010-10-04 16:33:58 +0200322
Derek Allard2067d1a2008-11-13 22:59:24 +0000323 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200324 // Check for Method (and Object)
Derek Allard2067d1a2008-11-13 22:59:24 +0000325 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200326
Andrey Andreev52278372012-03-26 16:02:30 +0300327 $method_parts = explode('.', $this->methods[$methName]['function']);
Alex Bilbied261b1e2012-06-02 11:12:16 +0100328 $objectCall = (isset($method_parts[1]) && $method_parts[1] !== '');
Barry Mienydd671972010-10-04 16:33:58 +0200329
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 if ($system_call === TRUE)
331 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200332 if ( ! is_callable(array($this,$method_parts[1])))
Derek Allard2067d1a2008-11-13 22:59:24 +0000333 {
334 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
335 }
336 }
Andrey Andreev52278372012-03-26 16:02:30 +0300337 elseif (($objectCall && ! is_callable(array($method_parts[0], $method_parts[1])))
338 OR ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
339 )
Derek Allard2067d1a2008-11-13 22:59:24 +0000340 {
Andrey Andreev52278372012-03-26 16:02:30 +0300341 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000342 }
Barry Mienydd671972010-10-04 16:33:58 +0200343
Derek Allard2067d1a2008-11-13 22:59:24 +0000344 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200345 // Checking Methods Signature
Derek Allard2067d1a2008-11-13 22:59:24 +0000346 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 if (isset($this->methods[$methName]['signature']))
349 {
350 $sig = $this->methods[$methName]['signature'];
Andrey Andreev56e32142011-12-25 18:36:31 +0200351 for ($i = 0, $c = count($sig); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000352 {
353 $current_sig = $sig[$i];
Barry Mienydd671972010-10-04 16:33:58 +0200354
Andrey Andreev56e32142011-12-25 18:36:31 +0200355 if (count($current_sig) === count($m->params)+1)
Derek Allard2067d1a2008-11-13 22:59:24 +0000356 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200357 for ($n = 0, $mc = count($m->params); $n < $mc; $n++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000358 {
359 $p = $m->params[$n];
Alex Bilbied261b1e2012-06-02 11:12:16 +0100360 $pt = ($p->kindOf() === 'scalar') ? $p->scalarval() : $p->kindOf();
Barry Mienydd671972010-10-04 16:33:58 +0200361
Alex Bilbied261b1e2012-06-02 11:12:16 +0100362 if ($pt !== $current_sig[$n+1])
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 {
364 $pno = $n+1;
365 $wanted = $current_sig[$n+1];
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 return new XML_RPC_Response(0,
368 $this->xmlrpcerr['incorrect_params'],
369 $this->xmlrpcstr['incorrect_params'] .
Andrey Andreev52278372012-03-26 16:02:30 +0300370 ': Wanted '.$wanted.', got '.$pt.' at param '.$pno.')');
Derek Allard2067d1a2008-11-13 22:59:24 +0000371 }
372 }
373 }
374 }
375 }
376
377 //-------------------------------------
vkeranov52de3792012-11-23 23:48:35 +0200378 // Calls the Function
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 //-------------------------------------
380
381 if ($objectCall === TRUE)
382 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200383 if ($method_parts[0] === 'this' && $system_call === TRUE)
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 {
385 return call_user_func(array($this, $method_parts[1]), $m);
386 }
387 else
388 {
389 if ($this->object === FALSE)
390 {
391 $CI =& get_instance();
Andrey Andreev56e32142011-12-25 18:36:31 +0200392 return $CI->$method_parts[1]($m);
Derek Allard2067d1a2008-11-13 22:59:24 +0000393 }
394 else
395 {
Andrey Andreev56e32142011-12-25 18:36:31 +0200396 return $this->object->$method_parts[1]($m);
Derek Allard2067d1a2008-11-13 22:59:24 +0000397 }
398 }
399 }
400 else
401 {
402 return call_user_func($this->methods[$methName]['function'], $m);
403 }
404 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200405
Pascal Kriete68d29872011-02-14 13:39:06 -0500406 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200407
Pascal Kriete68d29872011-02-14 13:39:06 -0500408 /**
vkeranov52de3792012-11-23 23:48:35 +0200409 * Server Function: List Methods
Pascal Kriete68d29872011-02-14 13:39:06 -0500410 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500411 * @param mixed
412 * @return object
413 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200414 public function listMethods($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 {
416 $v = new XML_RPC_Values();
417 $output = array();
Barry Mienydd671972010-10-04 16:33:58 +0200418
Pascal Kriete68d29872011-02-14 13:39:06 -0500419 foreach ($this->methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000420 {
421 $output[] = new XML_RPC_Values($key, 'string');
422 }
Barry Mienydd671972010-10-04 16:33:58 +0200423
Pascal Kriete68d29872011-02-14 13:39:06 -0500424 foreach ($this->system_methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000425 {
Andrey Andreev52278372012-03-26 16:02:30 +0300426 $output[] = new XML_RPC_Values($key, 'string');
Derek Allard2067d1a2008-11-13 22:59:24 +0000427 }
428
429 $v->addArray($output);
430 return new XML_RPC_Response($v);
431 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200432
Pascal Kriete68d29872011-02-14 13:39:06 -0500433 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200434
Pascal Kriete68d29872011-02-14 13:39:06 -0500435 /**
vkeranov52de3792012-11-23 23:48:35 +0200436 * Server Function: Return Signature for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500437 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500438 * @param mixed
439 * @return object
440 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200441 public function methodSignature($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000442 {
443 $parameters = $m->output_parameters();
444 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200445
Derek Allard2067d1a2008-11-13 22:59:24 +0000446 if (isset($this->methods[$method_name]))
447 {
448 if ($this->methods[$method_name]['signature'])
449 {
450 $sigs = array();
451 $signature = $this->methods[$method_name]['signature'];
Barry Mienydd671972010-10-04 16:33:58 +0200452
Andrey Andreev56e32142011-12-25 18:36:31 +0200453 for ($i = 0, $c = count($signature); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000454 {
455 $cursig = array();
456 $inSig = $signature[$i];
Andrey Andreev56e32142011-12-25 18:36:31 +0200457 for ($j = 0, $jc = count($inSig); $j < $jc; $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000458 {
459 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
460 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200461 $sigs[] = new XML_RPC_Values($cursig, 'array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000462 }
Andrey Andreev52278372012-03-26 16:02:30 +0300463
464 return new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000465 }
Andrey Andreev52278372012-03-26 16:02:30 +0300466
467 return new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000468 }
Andrey Andreev52278372012-03-26 16:02:30 +0300469
vkeranov52de3792012-11-23 23:48:35 +0200470 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000471 }
Barry Mienydd671972010-10-04 16:33:58 +0200472
Pascal Kriete68d29872011-02-14 13:39:06 -0500473 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200474
Pascal Kriete68d29872011-02-14 13:39:06 -0500475 /**
vkeranov52de3792012-11-23 23:48:35 +0200476 * Server Function: Doc String for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500477 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500478 * @param mixed
479 * @return object
480 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200481 public function methodHelp($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000482 {
483 $parameters = $m->output_parameters();
484 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 if (isset($this->methods[$method_name]))
487 {
488 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
Barry Mienydd671972010-10-04 16:33:58 +0200489
Derek Allard2067d1a2008-11-13 22:59:24 +0000490 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
491 }
492 else
493 {
494 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
495 }
496 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200497
Pascal Kriete68d29872011-02-14 13:39:06 -0500498 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000499
Pascal Kriete68d29872011-02-14 13:39:06 -0500500 /**
vkeranov52de3792012-11-23 23:48:35 +0200501 * Server Function: Multi-call
Pascal Kriete68d29872011-02-14 13:39:06 -0500502 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500503 * @param mixed
504 * @return object
505 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200506 public function multicall($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000507 {
508 // Disabled
509 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Barry Mienydd671972010-10-04 16:33:58 +0200510
Derek Allard2067d1a2008-11-13 22:59:24 +0000511 $parameters = $m->output_parameters();
512 $calls = $parameters[0];
513
514 $result = array();
515
516 foreach ($calls as $value)
517 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000518 $m = new XML_RPC_Message($value[0]);
vkeranov52de3792012-11-23 23:48:35 +0200519 $plist = '';
Barry Mienydd671972010-10-04 16:33:58 +0200520
Andrey Andreev56e32142011-12-25 18:36:31 +0200521 for ($i = 0, $c = count($value[1]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000522 {
523 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
524 }
Barry Mienydd671972010-10-04 16:33:58 +0200525
Derek Allard2067d1a2008-11-13 22:59:24 +0000526 $attempt = $this->_execute($m);
527
Alex Bilbied261b1e2012-06-02 11:12:16 +0100528 if ($attempt->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 {
530 return $attempt;
531 }
532
533 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
534 }
535
536 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
537 }
Barry Mienydd671972010-10-04 16:33:58 +0200538
Pascal Kriete68d29872011-02-14 13:39:06 -0500539 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200540
Pascal Kriete68d29872011-02-14 13:39:06 -0500541 /**
vkeranov52de3792012-11-23 23:48:35 +0200542 * Multi-call Function: Error Handling
Pascal Kriete68d29872011-02-14 13:39:06 -0500543 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500544 * @param mixed
545 * @return object
546 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200547 public function multicall_error($err)
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 {
vkeranov52de3792012-11-23 23:48:35 +0200549 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
Barry Mienydd671972010-10-04 16:33:58 +0200551
Derek Allard2067d1a2008-11-13 22:59:24 +0000552 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
553 $struct['faultString'] = new XML_RPC_Values($str, 'string');
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 return new XML_RPC_Values($struct, 'struct');
556 }
Barry Mienydd671972010-10-04 16:33:58 +0200557
Pascal Kriete68d29872011-02-14 13:39:06 -0500558 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200559
Pascal Kriete68d29872011-02-14 13:39:06 -0500560 /**
vkeranov52de3792012-11-23 23:48:35 +0200561 * Multi-call Function: Processes method
Pascal Kriete68d29872011-02-14 13:39:06 -0500562 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500563 * @param mixed
564 * @return object
565 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200566 public function do_multicall($call)
Derek Allard2067d1a2008-11-13 22:59:24 +0000567 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100568 if ($call->kindOf() !== 'struct')
Pascal Kriete68d29872011-02-14 13:39:06 -0500569 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 return $this->multicall_error('notstruct');
Pascal Kriete68d29872011-02-14 13:39:06 -0500571 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000572 elseif ( ! $methName = $call->me['struct']['methodName'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500573 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000574 return $this->multicall_error('nomethod');
Pascal Kriete68d29872011-02-14 13:39:06 -0500575 }
Barry Mienydd671972010-10-04 16:33:58 +0200576
vkeranov52de3792012-11-23 23:48:35 +0200577 list($scalar_type, $scalar_value) = each($methName->me);
Alex Bilbied261b1e2012-06-02 11:12:16 +0100578 $scalar_type = $scalar_type === $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
Barry Mienydd671972010-10-04 16:33:58 +0200579
Alex Bilbied261b1e2012-06-02 11:12:16 +0100580 if ($methName->kindOf() !== 'scalar' OR $scalar_type !== 'string')
Pascal Kriete68d29872011-02-14 13:39:06 -0500581 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 return $this->multicall_error('notstring');
Pascal Kriete68d29872011-02-14 13:39:06 -0500583 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100584 elseif ($scalar_value === 'system.multicall')
Pascal Kriete68d29872011-02-14 13:39:06 -0500585 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000586 return $this->multicall_error('recursion');
Pascal Kriete68d29872011-02-14 13:39:06 -0500587 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 elseif ( ! $params = $call->me['struct']['params'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500589 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000590 return $this->multicall_error('noparams');
Pascal Kriete68d29872011-02-14 13:39:06 -0500591 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100592 elseif ($params->kindOf() !== 'array')
Pascal Kriete68d29872011-02-14 13:39:06 -0500593 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000594 return $this->multicall_error('notarray');
Pascal Kriete68d29872011-02-14 13:39:06 -0500595 }
Barry Mienydd671972010-10-04 16:33:58 +0200596
vkeranov52de3792012-11-23 23:48:35 +0200597 list($a, $b) = each($params->me);
Derek Allard2067d1a2008-11-13 22:59:24 +0000598
599 $msg = new XML_RPC_Message($scalar_value);
Andrey Andreev56e32142011-12-25 18:36:31 +0200600 for ($i = 0, $numParams = count($b); $i < $numParams; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000601 {
602 $msg->params[] = $params->me['array'][$i];
603 }
604
605 $result = $this->_execute($msg);
606
Alex Bilbied261b1e2012-06-02 11:12:16 +0100607 if ($result->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000608 {
609 return $this->multicall_error($result);
610 }
611
612 return new XML_RPC_Values(array($result->value()), 'array');
Barry Mienydd671972010-10-04 16:33:58 +0200613 }
614
Derek Allard2067d1a2008-11-13 22:59:24 +0000615}
Derek Allard2067d1a2008-11-13 22:59:24 +0000616
Derek Allard2067d1a2008-11-13 22:59:24 +0000617/* End of file Xmlrpcs.php */
Andrey Andreev52278372012-03-26 16:02:30 +0300618/* Location: ./system/libraries/Xmlrpcs.php */