blob: 3c3337bf2419a25d1f10f8cf9183800fcd8afc03 [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 */
50class CI_Xmlrpcs extends CI_Xmlrpc
51{
Timothy Warren0688ac92012-04-20 10:25:04 -040052 /**
53 * array of methods mapped to function names and signatures
54 *
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(
Derek Jones37f4b9c2011-07-01 17:56:50 -0500190 'function' => $function,
Derek Allard2067d1a2008-11-13 22:59:24 +0000191 'signature' => $sig,
192 'docstring' => $doc
193 );
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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 // 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 {
245 // return XML error as a faultCode
246 $r = new XML_RPC_Response(0,
247 $this->xmlrpcerrxml + xml_get_error_code($parser),
248 sprintf('XML error: %s at line %d',
249 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']);
262 $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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500283 // 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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500315 // 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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500324 // 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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500345 // 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 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500378 // 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 //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
398 }
399 }
400 }
401 else
402 {
403 return call_user_func($this->methods[$methName]['function'], $m);
404 }
405 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200406
Pascal Kriete68d29872011-02-14 13:39:06 -0500407 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200408
Pascal Kriete68d29872011-02-14 13:39:06 -0500409 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500410 * Server Function: List Methods
Pascal Kriete68d29872011-02-14 13:39:06 -0500411 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500412 * @param mixed
413 * @return object
414 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200415 public function listMethods($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000416 {
417 $v = new XML_RPC_Values();
418 $output = array();
Barry Mienydd671972010-10-04 16:33:58 +0200419
Pascal Kriete68d29872011-02-14 13:39:06 -0500420 foreach ($this->methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000421 {
422 $output[] = new XML_RPC_Values($key, 'string');
423 }
Barry Mienydd671972010-10-04 16:33:58 +0200424
Pascal Kriete68d29872011-02-14 13:39:06 -0500425 foreach ($this->system_methods as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000426 {
Andrey Andreev52278372012-03-26 16:02:30 +0300427 $output[] = new XML_RPC_Values($key, 'string');
Derek Allard2067d1a2008-11-13 22:59:24 +0000428 }
429
430 $v->addArray($output);
431 return new XML_RPC_Response($v);
432 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200433
Pascal Kriete68d29872011-02-14 13:39:06 -0500434 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200435
Pascal Kriete68d29872011-02-14 13:39:06 -0500436 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500437 * Server Function: Return Signature for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500438 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500439 * @param mixed
440 * @return object
441 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200442 public function methodSignature($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000443 {
444 $parameters = $m->output_parameters();
445 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200446
Derek Allard2067d1a2008-11-13 22:59:24 +0000447 if (isset($this->methods[$method_name]))
448 {
449 if ($this->methods[$method_name]['signature'])
450 {
451 $sigs = array();
452 $signature = $this->methods[$method_name]['signature'];
Barry Mienydd671972010-10-04 16:33:58 +0200453
Andrey Andreev56e32142011-12-25 18:36:31 +0200454 for ($i = 0, $c = count($signature); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000455 {
456 $cursig = array();
457 $inSig = $signature[$i];
Andrey Andreev56e32142011-12-25 18:36:31 +0200458 for ($j = 0, $jc = count($inSig); $j < $jc; $j++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000459 {
460 $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
461 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200462 $sigs[] = new XML_RPC_Values($cursig, 'array');
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 }
Andrey Andreev52278372012-03-26 16:02:30 +0300464
465 return new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000466 }
Andrey Andreev52278372012-03-26 16:02:30 +0300467
468 return new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
Derek Allard2067d1a2008-11-13 22:59:24 +0000469 }
Andrey Andreev52278372012-03-26 16:02:30 +0300470
471 return new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
Derek Allard2067d1a2008-11-13 22:59:24 +0000472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Pascal Kriete68d29872011-02-14 13:39:06 -0500474 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200475
Pascal Kriete68d29872011-02-14 13:39:06 -0500476 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500477 * Server Function: Doc String for Method
Pascal Kriete68d29872011-02-14 13:39:06 -0500478 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500479 * @param mixed
480 * @return object
481 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200482 public function methodHelp($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000483 {
484 $parameters = $m->output_parameters();
485 $method_name = $parameters[0];
Barry Mienydd671972010-10-04 16:33:58 +0200486
Derek Allard2067d1a2008-11-13 22:59:24 +0000487 if (isset($this->methods[$method_name]))
488 {
489 $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
Barry Mienydd671972010-10-04 16:33:58 +0200490
Derek Allard2067d1a2008-11-13 22:59:24 +0000491 return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
492 }
493 else
494 {
495 return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
496 }
497 }
Andrey Andreev56e32142011-12-25 18:36:31 +0200498
Pascal Kriete68d29872011-02-14 13:39:06 -0500499 // --------------------------------------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000500
Pascal Kriete68d29872011-02-14 13:39:06 -0500501 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500502 * Server Function: Multi-call
Pascal Kriete68d29872011-02-14 13:39:06 -0500503 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500504 * @param mixed
505 * @return object
506 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200507 public function multicall($m)
Derek Allard2067d1a2008-11-13 22:59:24 +0000508 {
509 // Disabled
510 return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
Barry Mienydd671972010-10-04 16:33:58 +0200511
Derek Allard2067d1a2008-11-13 22:59:24 +0000512 $parameters = $m->output_parameters();
513 $calls = $parameters[0];
514
515 $result = array();
516
517 foreach ($calls as $value)
518 {
519 //$attempt = $this->_execute(new XML_RPC_Message($value[0], $value[1]));
Barry Mienydd671972010-10-04 16:33:58 +0200520
Derek Allard2067d1a2008-11-13 22:59:24 +0000521 $m = new XML_RPC_Message($value[0]);
522 $plist='';
Barry Mienydd671972010-10-04 16:33:58 +0200523
Andrey Andreev56e32142011-12-25 18:36:31 +0200524 for ($i = 0, $c = count($value[1]); $i < $c; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000525 {
526 $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
527 }
Barry Mienydd671972010-10-04 16:33:58 +0200528
Derek Allard2067d1a2008-11-13 22:59:24 +0000529 $attempt = $this->_execute($m);
530
Alex Bilbied261b1e2012-06-02 11:12:16 +0100531 if ($attempt->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000532 {
533 return $attempt;
534 }
535
536 $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
537 }
538
539 return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
540 }
Barry Mienydd671972010-10-04 16:33:58 +0200541
Pascal Kriete68d29872011-02-14 13:39:06 -0500542 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200543
Pascal Kriete68d29872011-02-14 13:39:06 -0500544 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500545 * Multi-call Function: Error Handling
Pascal Kriete68d29872011-02-14 13:39:06 -0500546 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500547 * @param mixed
548 * @return object
549 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200550 public function multicall_error($err)
Derek Allard2067d1a2008-11-13 22:59:24 +0000551 {
Derek Jones37f4b9c2011-07-01 17:56:50 -0500552 $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
Derek Allard2067d1a2008-11-13 22:59:24 +0000553 $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
Barry Mienydd671972010-10-04 16:33:58 +0200554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 $struct['faultCode'] = new XML_RPC_Values($code, 'int');
556 $struct['faultString'] = new XML_RPC_Values($str, 'string');
Barry Mienydd671972010-10-04 16:33:58 +0200557
Derek Allard2067d1a2008-11-13 22:59:24 +0000558 return new XML_RPC_Values($struct, 'struct');
559 }
Barry Mienydd671972010-10-04 16:33:58 +0200560
Pascal Kriete68d29872011-02-14 13:39:06 -0500561 // --------------------------------------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200562
Pascal Kriete68d29872011-02-14 13:39:06 -0500563 /**
Derek Jones37f4b9c2011-07-01 17:56:50 -0500564 * Multi-call Function: Processes method
Pascal Kriete68d29872011-02-14 13:39:06 -0500565 *
Pascal Kriete68d29872011-02-14 13:39:06 -0500566 * @param mixed
567 * @return object
568 */
Andrey Andreev56e32142011-12-25 18:36:31 +0200569 public function do_multicall($call)
Derek Allard2067d1a2008-11-13 22:59:24 +0000570 {
Alex Bilbied261b1e2012-06-02 11:12:16 +0100571 if ($call->kindOf() !== 'struct')
Pascal Kriete68d29872011-02-14 13:39:06 -0500572 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 return $this->multicall_error('notstruct');
Pascal Kriete68d29872011-02-14 13:39:06 -0500574 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000575 elseif ( ! $methName = $call->me['struct']['methodName'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500576 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000577 return $this->multicall_error('nomethod');
Pascal Kriete68d29872011-02-14 13:39:06 -0500578 }
Barry Mienydd671972010-10-04 16:33:58 +0200579
Derek Allard2067d1a2008-11-13 22:59:24 +0000580 list($scalar_type,$scalar_value)=each($methName->me);
Alex Bilbied261b1e2012-06-02 11:12:16 +0100581 $scalar_type = $scalar_type === $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
Barry Mienydd671972010-10-04 16:33:58 +0200582
Alex Bilbied261b1e2012-06-02 11:12:16 +0100583 if ($methName->kindOf() !== 'scalar' OR $scalar_type !== 'string')
Pascal Kriete68d29872011-02-14 13:39:06 -0500584 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000585 return $this->multicall_error('notstring');
Pascal Kriete68d29872011-02-14 13:39:06 -0500586 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100587 elseif ($scalar_value === 'system.multicall')
Pascal Kriete68d29872011-02-14 13:39:06 -0500588 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000589 return $this->multicall_error('recursion');
Pascal Kriete68d29872011-02-14 13:39:06 -0500590 }
Derek Allard2067d1a2008-11-13 22:59:24 +0000591 elseif ( ! $params = $call->me['struct']['params'])
Pascal Kriete68d29872011-02-14 13:39:06 -0500592 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000593 return $this->multicall_error('noparams');
Pascal Kriete68d29872011-02-14 13:39:06 -0500594 }
Alex Bilbied261b1e2012-06-02 11:12:16 +0100595 elseif ($params->kindOf() !== 'array')
Pascal Kriete68d29872011-02-14 13:39:06 -0500596 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 return $this->multicall_error('notarray');
Pascal Kriete68d29872011-02-14 13:39:06 -0500598 }
Barry Mienydd671972010-10-04 16:33:58 +0200599
Andrey Andreev56e32142011-12-25 18:36:31 +0200600 list($a,$b) = each($params->me);
Derek Allard2067d1a2008-11-13 22:59:24 +0000601
602 $msg = new XML_RPC_Message($scalar_value);
Andrey Andreev56e32142011-12-25 18:36:31 +0200603 for ($i = 0, $numParams = count($b); $i < $numParams; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000604 {
605 $msg->params[] = $params->me['array'][$i];
606 }
607
608 $result = $this->_execute($msg);
609
Alex Bilbied261b1e2012-06-02 11:12:16 +0100610 if ($result->faultCode() !== 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000611 {
612 return $this->multicall_error($result);
613 }
614
615 return new XML_RPC_Values(array($result->value()), 'array');
Barry Mienydd671972010-10-04 16:33:58 +0200616 }
617
Derek Allard2067d1a2008-11-13 22:59:24 +0000618}
Derek Allard2067d1a2008-11-13 22:59:24 +0000619
Derek Allard2067d1a2008-11-13 22:59:24 +0000620/* End of file Xmlrpcs.php */
Andrey Andreev52278372012-03-26 16:02:30 +0300621/* Location: ./system/libraries/Xmlrpcs.php */