Xmlrpcs.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. <?php
  2. /**
  3. * CodeIgniter
  4. *
  5. * An open source application development framework for PHP
  6. *
  7. * This content is released under the MIT License (MIT)
  8. *
  9. * Copyright (c) 2014 - 2019, British Columbia Institute of Technology
  10. *
  11. * 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:
  17. *
  18. * 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
  31. * @copyright Copyright (c) 2008 - 2014, EllisLab, Inc. (https://ellislab.com/)
  32. * @copyright Copyright (c) 2014 - 2019, British Columbia Institute of Technology (https://bcit.ca/)
  33. * @license https://opensource.org/licenses/MIT MIT License
  34. * @link https://codeigniter.com
  35. * @since Version 1.0.0
  36. * @filesource
  37. */
  38. defined('BASEPATH') OR exit('No direct script access allowed');
  39. if ( ! function_exists('xml_parser_create'))
  40. {
  41. show_error('Your PHP installation does not support XML');
  42. }
  43. if ( ! class_exists('CI_Xmlrpc', FALSE))
  44. {
  45. show_error('You must load the Xmlrpc class before loading the Xmlrpcs class in order to create a server.');
  46. }
  47. // ------------------------------------------------------------------------
  48. /**
  49. * XML-RPC server class
  50. *
  51. * @package CodeIgniter
  52. * @subpackage Libraries
  53. * @category XML-RPC
  54. * @author EllisLab Dev Team
  55. * @link https://codeigniter.com/user_guide/libraries/xmlrpc.html
  56. */
  57. class CI_Xmlrpcs extends CI_Xmlrpc {
  58. /**
  59. * Array of methods mapped to function names and signatures
  60. *
  61. * @var array
  62. */
  63. public $methods = array();
  64. /**
  65. * Debug Message
  66. *
  67. * @var string
  68. */
  69. public $debug_msg = '';
  70. /**
  71. * XML RPC Server methods
  72. *
  73. * @var array
  74. */
  75. public $system_methods = array();
  76. /**
  77. * Configuration object
  78. *
  79. * @var object
  80. */
  81. public $object = FALSE;
  82. /**
  83. * Initialize XMLRPC class
  84. *
  85. * @param array $config
  86. * @return void
  87. */
  88. public function __construct($config = array())
  89. {
  90. parent::__construct();
  91. $this->set_system_methods();
  92. if (isset($config['functions']) && is_array($config['functions']))
  93. {
  94. $this->methods = array_merge($this->methods, $config['functions']);
  95. }
  96. log_message('info', 'XML-RPC Server Class Initialized');
  97. }
  98. // --------------------------------------------------------------------
  99. /**
  100. * Initialize Prefs and Serve
  101. *
  102. * @param mixed
  103. * @return void
  104. */
  105. public function initialize($config = array())
  106. {
  107. if (isset($config['functions']) && is_array($config['functions']))
  108. {
  109. $this->methods = array_merge($this->methods, $config['functions']);
  110. }
  111. if (isset($config['debug']))
  112. {
  113. $this->debug = $config['debug'];
  114. }
  115. if (isset($config['object']) && is_object($config['object']))
  116. {
  117. $this->object = $config['object'];
  118. }
  119. if (isset($config['xss_clean']))
  120. {
  121. $this->xss_clean = $config['xss_clean'];
  122. }
  123. }
  124. // --------------------------------------------------------------------
  125. /**
  126. * Setting of System Methods
  127. *
  128. * @return void
  129. */
  130. public function set_system_methods()
  131. {
  132. $this->methods = array(
  133. 'system.listMethods' => array(
  134. 'function' => 'this.listMethods',
  135. 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString), array($this->xmlrpcArray)),
  136. 'docstring' => 'Returns an array of available methods on this server'),
  137. 'system.methodHelp' => array(
  138. 'function' => 'this.methodHelp',
  139. 'signature' => array(array($this->xmlrpcString, $this->xmlrpcString)),
  140. 'docstring' => 'Returns a documentation string for the specified method'),
  141. 'system.methodSignature' => array(
  142. 'function' => 'this.methodSignature',
  143. 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcString)),
  144. 'docstring' => 'Returns an array describing the return type and required parameters of a method'),
  145. 'system.multicall' => array(
  146. 'function' => 'this.multicall',
  147. 'signature' => array(array($this->xmlrpcArray, $this->xmlrpcArray)),
  148. 'docstring' => 'Combine multiple RPC calls in one request. See http://www.xmlrpc.com/discuss/msgReader$1208 for details')
  149. );
  150. }
  151. // --------------------------------------------------------------------
  152. /**
  153. * Main Server Function
  154. *
  155. * @return void
  156. */
  157. public function serve()
  158. {
  159. $r = $this->parseRequest();
  160. $payload = '<?xml version="1.0" encoding="'.$this->xmlrpc_defencoding.'"?'.'>'."\n".$this->debug_msg.$r->prepare_response();
  161. header('Content-Type: text/xml');
  162. header('Content-Length: '.strlen($payload));
  163. exit($payload);
  164. }
  165. // --------------------------------------------------------------------
  166. /**
  167. * Add Method to Class
  168. *
  169. * @param string method name
  170. * @param string function
  171. * @param string signature
  172. * @param string docstring
  173. * @return void
  174. */
  175. public function add_to_map($methodname, $function, $sig, $doc)
  176. {
  177. $this->methods[$methodname] = array(
  178. 'function' => $function,
  179. 'signature' => $sig,
  180. 'docstring' => $doc
  181. );
  182. }
  183. // --------------------------------------------------------------------
  184. /**
  185. * Parse Server Request
  186. *
  187. * @param string data
  188. * @return object xmlrpc response
  189. */
  190. public function parseRequest($data = '')
  191. {
  192. //-------------------------------------
  193. // Get Data
  194. //-------------------------------------
  195. if ($data === '')
  196. {
  197. $CI =& get_instance();
  198. if ($CI->input->method() === 'post')
  199. {
  200. $data = $CI->input->raw_input_stream;
  201. }
  202. }
  203. //-------------------------------------
  204. // Set up XML Parser
  205. //-------------------------------------
  206. $parser = xml_parser_create($this->xmlrpc_defencoding);
  207. $parser_object = new XML_RPC_Message('filler');
  208. $pname = (string) $parser;
  209. $parser_object->xh[$pname] = array(
  210. 'isf' => 0,
  211. 'isf_reason' => '',
  212. 'params' => array(),
  213. 'stack' => array(),
  214. 'valuestack' => array(),
  215. 'method' => ''
  216. );
  217. xml_set_object($parser, $parser_object);
  218. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, TRUE);
  219. xml_set_element_handler($parser, 'open_tag', 'closing_tag');
  220. xml_set_character_data_handler($parser, 'character_data');
  221. //xml_set_default_handler($parser, 'default_handler');
  222. //-------------------------------------
  223. // PARSE + PROCESS XML DATA
  224. //-------------------------------------
  225. if ( ! xml_parse($parser, $data, 1))
  226. {
  227. // Return XML error as a faultCode
  228. $r = new XML_RPC_Response(0,
  229. $this->xmlrpcerrxml + xml_get_error_code($parser),
  230. sprintf('XML error: %s at line %d',
  231. xml_error_string(xml_get_error_code($parser)),
  232. xml_get_current_line_number($parser)));
  233. xml_parser_free($parser);
  234. }
  235. elseif ($parser_object->xh[$pname]['isf'])
  236. {
  237. return new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
  238. }
  239. else
  240. {
  241. xml_parser_free($parser);
  242. $m = new XML_RPC_Message($parser_object->xh[$pname]['method']);
  243. $plist = '';
  244. for ($i = 0, $c = count($parser_object->xh[$pname]['params']); $i < $c; $i++)
  245. {
  246. if ($this->debug === TRUE)
  247. {
  248. $plist .= $i.' - '.print_r(get_object_vars($parser_object->xh[$pname]['params'][$i]), TRUE).";\n";
  249. }
  250. $m->addParam($parser_object->xh[$pname]['params'][$i]);
  251. }
  252. if ($this->debug === TRUE)
  253. {
  254. echo "<pre>---PLIST---\n".$plist."\n---PLIST END---\n\n</pre>";
  255. }
  256. $r = $this->_execute($m);
  257. }
  258. //-------------------------------------
  259. // SET DEBUGGING MESSAGE
  260. //-------------------------------------
  261. if ($this->debug === TRUE)
  262. {
  263. $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
  264. }
  265. return $r;
  266. }
  267. // --------------------------------------------------------------------
  268. /**
  269. * Executes the Method
  270. *
  271. * @param object
  272. * @return mixed
  273. */
  274. protected function _execute($m)
  275. {
  276. $methName = $m->method_name;
  277. // Check to see if it is a system call
  278. $system_call = (strpos($methName, 'system') === 0);
  279. if ($this->xss_clean === FALSE)
  280. {
  281. $m->xss_clean = FALSE;
  282. }
  283. //-------------------------------------
  284. // Valid Method
  285. //-------------------------------------
  286. if ( ! isset($this->methods[$methName]['function']))
  287. {
  288. return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
  289. }
  290. //-------------------------------------
  291. // Check for Method (and Object)
  292. //-------------------------------------
  293. $method_parts = explode('.', $this->methods[$methName]['function']);
  294. $objectCall = ! empty($method_parts[1]);
  295. if ($system_call === TRUE)
  296. {
  297. if ( ! is_callable(array($this, $method_parts[1])))
  298. {
  299. return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
  300. }
  301. }
  302. elseif (($objectCall && ! is_callable(array($method_parts[0], $method_parts[1])))
  303. OR ( ! $objectCall && ! is_callable($this->methods[$methName]['function']))
  304. )
  305. {
  306. return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
  307. }
  308. //-------------------------------------
  309. // Checking Methods Signature
  310. //-------------------------------------
  311. if (isset($this->methods[$methName]['signature']))
  312. {
  313. $sig = $this->methods[$methName]['signature'];
  314. for ($i = 0, $c = count($sig); $i < $c; $i++)
  315. {
  316. $current_sig = $sig[$i];
  317. if (count($current_sig) === count($m->params)+1)
  318. {
  319. for ($n = 0, $mc = count($m->params); $n < $mc; $n++)
  320. {
  321. $p = $m->params[$n];
  322. $pt = ($p->kindOf() === 'scalar') ? $p->scalarval() : $p->kindOf();
  323. if ($pt !== $current_sig[$n+1])
  324. {
  325. $pno = $n+1;
  326. $wanted = $current_sig[$n+1];
  327. return new XML_RPC_Response(0,
  328. $this->xmlrpcerr['incorrect_params'],
  329. $this->xmlrpcstr['incorrect_params'] .
  330. ': Wanted '.$wanted.', got '.$pt.' at param '.$pno.')');
  331. }
  332. }
  333. }
  334. }
  335. }
  336. //-------------------------------------
  337. // Calls the Function
  338. //-------------------------------------
  339. if ($objectCall === TRUE)
  340. {
  341. if ($method_parts[0] === 'this' && $system_call === TRUE)
  342. {
  343. return call_user_func(array($this, $method_parts[1]), $m);
  344. }
  345. elseif ($this->object === FALSE)
  346. {
  347. return get_instance()->{$method_parts[1]}($m);
  348. }
  349. return $this->object->{$method_parts[1]}($m);
  350. }
  351. return call_user_func($this->methods[$methName]['function'], $m);
  352. }
  353. // --------------------------------------------------------------------
  354. /**
  355. * Server Function: List Methods
  356. *
  357. * @param mixed
  358. * @return object
  359. */
  360. public function listMethods($m)
  361. {
  362. $v = new XML_RPC_Values();
  363. $output = array();
  364. foreach ($this->methods as $key => $value)
  365. {
  366. $output[] = new XML_RPC_Values($key, 'string');
  367. }
  368. foreach ($this->system_methods as $key => $value)
  369. {
  370. $output[] = new XML_RPC_Values($key, 'string');
  371. }
  372. $v->addArray($output);
  373. return new XML_RPC_Response($v);
  374. }
  375. // --------------------------------------------------------------------
  376. /**
  377. * Server Function: Return Signature for Method
  378. *
  379. * @param mixed
  380. * @return object
  381. */
  382. public function methodSignature($m)
  383. {
  384. $parameters = $m->output_parameters();
  385. $method_name = $parameters[0];
  386. if (isset($this->methods[$method_name]))
  387. {
  388. if ($this->methods[$method_name]['signature'])
  389. {
  390. $sigs = array();
  391. $signature = $this->methods[$method_name]['signature'];
  392. for ($i = 0, $c = count($signature); $i < $c; $i++)
  393. {
  394. $cursig = array();
  395. $inSig = $signature[$i];
  396. for ($j = 0, $jc = count($inSig); $j < $jc; $j++)
  397. {
  398. $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
  399. }
  400. $sigs[] = new XML_RPC_Values($cursig, 'array');
  401. }
  402. return new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
  403. }
  404. return new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
  405. }
  406. return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
  407. }
  408. // --------------------------------------------------------------------
  409. /**
  410. * Server Function: Doc String for Method
  411. *
  412. * @param mixed
  413. * @return object
  414. */
  415. public function methodHelp($m)
  416. {
  417. $parameters = $m->output_parameters();
  418. $method_name = $parameters[0];
  419. if (isset($this->methods[$method_name]))
  420. {
  421. $docstring = isset($this->methods[$method_name]['docstring']) ? $this->methods[$method_name]['docstring'] : '';
  422. return new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
  423. }
  424. return new XML_RPC_Response(0, $this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
  425. }
  426. // --------------------------------------------------------------------
  427. /**
  428. * Server Function: Multi-call
  429. *
  430. * @param mixed
  431. * @return object
  432. */
  433. public function multicall($m)
  434. {
  435. // Disabled
  436. return new XML_RPC_Response(0, $this->xmlrpcerr['unknown_method'], $this->xmlrpcstr['unknown_method']);
  437. $parameters = $m->output_parameters();
  438. $calls = $parameters[0];
  439. $result = array();
  440. foreach ($calls as $value)
  441. {
  442. $m = new XML_RPC_Message($value[0]);
  443. $plist = '';
  444. for ($i = 0, $c = count($value[1]); $i < $c; $i++)
  445. {
  446. $m->addParam(new XML_RPC_Values($value[1][$i], 'string'));
  447. }
  448. $attempt = $this->_execute($m);
  449. if ($attempt->faultCode() !== 0)
  450. {
  451. return $attempt;
  452. }
  453. $result[] = new XML_RPC_Values(array($attempt->value()), 'array');
  454. }
  455. return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
  456. }
  457. // --------------------------------------------------------------------
  458. /**
  459. * Multi-call Function: Error Handling
  460. *
  461. * @param mixed
  462. * @return object
  463. */
  464. public function multicall_error($err)
  465. {
  466. $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
  467. $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
  468. $struct['faultCode'] = new XML_RPC_Values($code, 'int');
  469. $struct['faultString'] = new XML_RPC_Values($str, 'string');
  470. return new XML_RPC_Values($struct, 'struct');
  471. }
  472. // --------------------------------------------------------------------
  473. /**
  474. * Multi-call Function: Processes method
  475. *
  476. * @param mixed
  477. * @return object
  478. */
  479. public function do_multicall($call)
  480. {
  481. if ($call->kindOf() !== 'struct')
  482. {
  483. return $this->multicall_error('notstruct');
  484. }
  485. elseif ( ! $methName = $call->me['struct']['methodName'])
  486. {
  487. return $this->multicall_error('nomethod');
  488. }
  489. list($scalar_value, $scalar_type) = array(reset($methName->me), key($methName->me));
  490. $scalar_type = $scalar_type === $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
  491. if ($methName->kindOf() !== 'scalar' OR $scalar_type !== 'string')
  492. {
  493. return $this->multicall_error('notstring');
  494. }
  495. elseif ($scalar_value === 'system.multicall')
  496. {
  497. return $this->multicall_error('recursion');
  498. }
  499. elseif ( ! $params = $call->me['struct']['params'])
  500. {
  501. return $this->multicall_error('noparams');
  502. }
  503. elseif ($params->kindOf() !== 'array')
  504. {
  505. return $this->multicall_error('notarray');
  506. }
  507. list($b, $a) = array(reset($params->me), key($params->me));
  508. $msg = new XML_RPC_Message($scalar_value);
  509. for ($i = 0, $numParams = count($b); $i < $numParams; $i++)
  510. {
  511. $msg->params[] = $params->me['array'][$i];
  512. }
  513. $result = $this->_execute($msg);
  514. if ($result->faultCode() !== 0)
  515. {
  516. return $this->multicall_error($result);
  517. }
  518. return new XML_RPC_Values(array($result->value()), 'array');
  519. }
  520. }