PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Xmlrpcs.php

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