PageRenderTime 60ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/system/libraries/Xmlrpcs.php

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