PageRenderTime 28ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/Modifications Since 08/www/events/system/libraries/Xmlrpcs.php

https://github.com/holsinger/openfloor
PHP | 503 lines | 321 code | 90 blank | 92 comment | 49 complexity | 1438bd018725a18761965fb3bead2a52 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 Rick Ellis, Paul Burdick
  9. * @copyright Copyright (c) 2006, EllisLab, Inc.
  10. * @license http://www.codeignitor.com/user_guide/license.html
  11. * @link http://www.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 Paul Burdick
  31. * @link http://www.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 = $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 = $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. $system_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,
  160. $this->xmlrpcerr['invalid_return'],
  161. $this->xmlrpcstr['invalid_return']);
  162. }
  163. else
  164. {
  165. xml_parser_free($parser);
  166. $m = new XML_RPC_Message($parser_object->xh[$parser]['method']);
  167. $plist='';
  168. for($i=0; $i < sizeof($parser_object->xh[$parser]['params']); $i++)
  169. {
  170. $plist .= "$i - " . print_r(get_object_vars($parser_object->xh[$parser]['params'][$i]), TRUE). ";\n";
  171. $m->addParam($parser_object->xh[$parser]['params'][$i]);
  172. }
  173. if ($this->debug === TRUE)
  174. {
  175. echo "<pre>";
  176. echo "---PLIST---\n" . $plist . "\n---PLIST END---\n\n";
  177. echo "</pre>";
  178. }
  179. $r = $this->_execute($m);
  180. }
  181. //-------------------------------------
  182. // SET DEBUGGING MESSAGE
  183. //-------------------------------------
  184. if ($this->debug === TRUE)
  185. {
  186. $this->debug_msg = "<!-- DEBUG INFO:\n\n".$plist."\n END DEBUG-->\n";
  187. }
  188. return $r;
  189. }
  190. //-------------------------------------
  191. // Executes the Method
  192. //-------------------------------------
  193. function _execute($m)
  194. {
  195. $methName = $m->method_name;
  196. // Check to see if it is a system call
  197. // If so, load the system_methods
  198. $sysCall = ereg("^system\.", $methName);
  199. $methods = $sysCall ? $this->system_methods : $this->methods;
  200. //-------------------------------------
  201. // Check for Function
  202. //-------------------------------------
  203. if (!isset($methods[$methName]['function']))
  204. {
  205. return new XML_RPC_Response(0,
  206. $this->xmlrpcerr['unknown_method'],
  207. $this->xmlrpcstr['unknown_method']);
  208. }
  209. else
  210. {
  211. // See if we are calling function in an object
  212. $method_parts = explode(".",$methods[$methName]['function']);
  213. $objectCall = (isset($method_parts['1']) && $method_parts['1'] != "") ? true : false;
  214. if ($objectCall && !is_callable(array($method_parts['0'],$method_parts['1'])))
  215. {
  216. return new XML_RPC_Response(0,
  217. $this->xmlrpcerr['unknown_method'],
  218. $this->xmlrpcstr['unknown_method']);
  219. }
  220. elseif (!$objectCall && !is_callable($methods[$methName]['function']))
  221. {
  222. return new XML_RPC_Response(0,
  223. $this->xmlrpcerr['unknown_method'],
  224. $this->xmlrpcstr['unknown_method']);
  225. }
  226. }
  227. //-------------------------------------
  228. // Checking Methods Signature
  229. //-------------------------------------
  230. if (isset($methods[$methName]['signature']))
  231. {
  232. $sig = $methods[$methName]['signature'];
  233. for($i=0; $i<sizeof($sig); $i++)
  234. {
  235. $current_sig = $sig[$i];
  236. if (sizeof($current_sig) == sizeof($m->params)+1)
  237. {
  238. for($n=0; $n < sizeof($m->params); $n++)
  239. {
  240. $p = $m->params[$n];
  241. $pt = ($p->kindOf() == 'scalar') ? $p->scalartyp() : $p->kindOf();
  242. if ($pt != $current_sig[$n+1])
  243. {
  244. $pno = $n+1;
  245. $wanted = $current_sig[$n+1];
  246. return new XML_RPC_Response(0,
  247. $this->xmlrpcerr['incorrect_params'],
  248. $this->xmlrpcstr['incorrect_params'] .
  249. ": Wanted {$wanted}, got {$pt} at param {$pno})");
  250. }
  251. }
  252. }
  253. }
  254. }
  255. //-------------------------------------
  256. // Calls the Function
  257. //-------------------------------------
  258. if ($objectCall)
  259. {
  260. if ($method_parts['1'] == "this")
  261. {
  262. return call_user_func(array($this, $method_parts['0']), $m);
  263. }
  264. else
  265. {
  266. $CI =& get_instance();
  267. return $CI->$method_parts['1']($m);
  268. //$class = new $method_parts['0'];
  269. //return $class->$method_parts['1']($m);
  270. //return call_user_func(array(&$method_parts['0'],$method_parts['1']), $m);
  271. }
  272. }
  273. else
  274. {
  275. return call_user_func($methods[$methName]['function'], $m);
  276. }
  277. }
  278. //-------------------------------------
  279. // Server Function: List Methods
  280. //-------------------------------------
  281. function listMethods($m)
  282. {
  283. $v = new XML_RPC_Values();
  284. $output = array();
  285. foreach($this->$methods as $key => $value)
  286. {
  287. $output[] = new XML_RPC_Values($key, 'string');
  288. }
  289. foreach($this->system_methods as $key => $value)
  290. {
  291. $output[]= new XML_RPC_Values($key, 'string');
  292. }
  293. $v->addArray($output);
  294. return new XML_RPC_Response($v);
  295. }
  296. //-------------------------------------
  297. // Server Function: Return Signature for Method
  298. //-------------------------------------
  299. function methodSignature($m)
  300. {
  301. $methName = $m->getParam(0);
  302. $method_name = $methName->scalarval();
  303. $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
  304. if (isset($methods[$method_name]))
  305. {
  306. if ($methods[$method_name]['signature'])
  307. {
  308. $sigs = array();
  309. $signature = $methods[$method_name]['signature'];
  310. for($i=0; $i < sizeof($signature); $i++)
  311. {
  312. $cursig = array();
  313. $inSig = $signature[$i];
  314. for($j=0; $j<sizeof($inSig); $j++)
  315. {
  316. $cursig[]= new XML_RPC_Values($inSig[$j], 'string');
  317. }
  318. $sigs[]= new XML_RPC_Values($cursig, 'array');
  319. }
  320. $r = new XML_RPC_Response(new XML_RPC_Values($sigs, 'array'));
  321. }
  322. else
  323. {
  324. $r = new XML_RPC_Response(new XML_RPC_Values('undef', 'string'));
  325. }
  326. }
  327. else
  328. {
  329. $r = new XML_RPC_Response(0,$this->xmlrpcerr['introspect_unknown'], $this->xmlrpcstr['introspect_unknown']);
  330. }
  331. return $r;
  332. }
  333. //-------------------------------------
  334. // Server Function: Doc String for Method
  335. //-------------------------------------
  336. function methodHelp($m)
  337. {
  338. $methName = $m->getParam(0);
  339. $method_name = $methName->scalarval();
  340. $methods = ereg("^system\.", $method_name) ? $this->system_methods : $this->methods;
  341. if (isset($methods[$methName]))
  342. {
  343. $docstring = isset($methods[$method_name]['docstring']) ? $methods[$method_name]['docstring'] : '';
  344. $r = new XML_RPC_Response(new XML_RPC_Values($docstring, 'string'));
  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: Multi-call
  354. //-------------------------------------
  355. function multicall($m)
  356. {
  357. $calls = $m->getParam(0);
  358. list($a,$b)=each($calls->me);
  359. $result = array();
  360. for ($i = 0; $i < sizeof($b); $i++)
  361. {
  362. $call = $calls->me['array'][$i];
  363. $result[$i] = $this->do_multicall($call);
  364. }
  365. return new XML_RPC_Response(new XML_RPC_Values($result, 'array'));
  366. }
  367. //-------------------------------------
  368. // Multi-call Function: Error Handling
  369. //-------------------------------------
  370. function multicall_error($err)
  371. {
  372. $str = is_string($err) ? $this->xmlrpcstr["multicall_${err}"] : $err->faultString();
  373. $code = is_string($err) ? $this->xmlrpcerr["multicall_${err}"] : $err->faultCode();
  374. $struct['faultCode'] = new XML_RPC_Values($code, 'int');
  375. $struct['faultString'] = new XML_RPC_Values($str, 'string');
  376. return new XML_RPC_Values($struct, 'struct');
  377. }
  378. //-------------------------------------
  379. // Multi-call Function: Processes method
  380. //-------------------------------------
  381. function do_multicall($call)
  382. {
  383. if ($call->kindOf() != 'struct')
  384. return $this->multicall_error('notstruct');
  385. elseif (!$methName = $call->me['struct']['methodName'])
  386. return $this->multicall_error('nomethod');
  387. list($scalar_type,$scalar_value)=each($methName->me);
  388. $scalar_type = $scalar_type == $this->xmlrpcI4 ? $this->xmlrpcInt : $scalar_type;
  389. if ($methName->kindOf() != 'scalar' || $scalar_type != 'string')
  390. return $this->multicall_error('notstring');
  391. elseif ($scalar_value == 'system.multicall')
  392. return $this->multicall_error('recursion');
  393. elseif (!$params = $call->me['struct']['params'])
  394. return $this->multicall_error('noparams');
  395. elseif ($params->kindOf() != 'array')
  396. return $this->multicall_error('notarray');
  397. list($a,$b)=each($params->me);
  398. $numParams = sizeof($b);
  399. $msg = new XML_RPC_Message($scalar_value);
  400. for ($i = 0; $i < $numParams; $i++)
  401. {
  402. $msg->params[] = $params->me['array'][$i];
  403. }
  404. $result = $this->_execute($msg);
  405. if ($result->faultCode() != 0)
  406. {
  407. return $this->multicall_error($result);
  408. }
  409. return new XML_RPC_Values(array($result->value()), 'array');
  410. }
  411. }
  412. // END XML_RPC_Server class
  413. ?>