PageRenderTime 57ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Xmlrpcs.php

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