/halogy/libraries/Xmlrpc.php

https://bitbucket.org/haloweb/halogy-1.0/ · PHP · 1421 lines · 1020 code · 202 blank · 199 comment · 124 complexity · 79927efd3d180191c616234f8644885e 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. // ------------------------------------------------------------------------
  20. /**
  21. * XML-RPC request handler class
  22. *
  23. * @package CodeIgniter
  24. * @subpackage Libraries
  25. * @category XML-RPC
  26. * @author ExpressionEngine Dev Team
  27. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  28. */
  29. class CI_Xmlrpc {
  30. var $debug = FALSE; // Debugging on or off
  31. var $xmlrpcI4 = 'i4';
  32. var $xmlrpcInt = 'int';
  33. var $xmlrpcBoolean = 'boolean';
  34. var $xmlrpcDouble = 'double';
  35. var $xmlrpcString = 'string';
  36. var $xmlrpcDateTime = 'dateTime.iso8601';
  37. var $xmlrpcBase64 = 'base64';
  38. var $xmlrpcArray = 'array';
  39. var $xmlrpcStruct = 'struct';
  40. var $xmlrpcTypes = array();
  41. var $valid_parents = array();
  42. var $xmlrpcerr = array(); // Response numbers
  43. var $xmlrpcstr = array(); // Response strings
  44. var $xmlrpc_defencoding = 'UTF-8';
  45. var $xmlrpcName = 'XML-RPC for CodeIgniter';
  46. var $xmlrpcVersion = '1.1';
  47. var $xmlrpcerruser = 800; // Start of user errors
  48. var $xmlrpcerrxml = 100; // Start of XML Parse errors
  49. var $xmlrpc_backslash = ''; // formulate backslashes for escaping regexp
  50. var $client;
  51. var $method;
  52. var $data;
  53. var $message = '';
  54. var $error = ''; // Error string for request
  55. var $result;
  56. var $response = array(); // Response from remote server
  57. //-------------------------------------
  58. // VALUES THAT MULTIPLE CLASSES NEED
  59. //-------------------------------------
  60. function CI_Xmlrpc ($config = array())
  61. {
  62. $this->xmlrpcName = $this->xmlrpcName;
  63. $this->xmlrpc_backslash = chr(92).chr(92);
  64. // Types for info sent back and forth
  65. $this->xmlrpcTypes = array(
  66. $this->xmlrpcI4 => '1',
  67. $this->xmlrpcInt => '1',
  68. $this->xmlrpcBoolean => '1',
  69. $this->xmlrpcString => '1',
  70. $this->xmlrpcDouble => '1',
  71. $this->xmlrpcDateTime => '1',
  72. $this->xmlrpcBase64 => '1',
  73. $this->xmlrpcArray => '2',
  74. $this->xmlrpcStruct => '3'
  75. );
  76. // Array of Valid Parents for Various XML-RPC elements
  77. $this->valid_parents = array('BOOLEAN' => array('VALUE'),
  78. 'I4' => array('VALUE'),
  79. 'INT' => array('VALUE'),
  80. 'STRING' => array('VALUE'),
  81. 'DOUBLE' => array('VALUE'),
  82. 'DATETIME.ISO8601' => array('VALUE'),
  83. 'BASE64' => array('VALUE'),
  84. 'ARRAY' => array('VALUE'),
  85. 'STRUCT' => array('VALUE'),
  86. 'PARAM' => array('PARAMS'),
  87. 'METHODNAME' => array('METHODCALL'),
  88. 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
  89. 'MEMBER' => array('STRUCT'),
  90. 'NAME' => array('MEMBER'),
  91. 'DATA' => array('ARRAY'),
  92. 'FAULT' => array('METHODRESPONSE'),
  93. 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT')
  94. );
  95. // XML-RPC Responses
  96. $this->xmlrpcerr['unknown_method'] = '1';
  97. $this->xmlrpcstr['unknown_method'] = 'This is not a known method for this XML-RPC Server';
  98. $this->xmlrpcerr['invalid_return'] = '2';
  99. $this->xmlrpcstr['invalid_return'] = 'The XML data receieved was either invalid or not in the correct form for XML-RPC. Turn on debugging to examine the XML data further.';
  100. $this->xmlrpcerr['incorrect_params'] = '3';
  101. $this->xmlrpcstr['incorrect_params'] = 'Incorrect parameters were passed to method';
  102. $this->xmlrpcerr['introspect_unknown'] = '4';
  103. $this->xmlrpcstr['introspect_unknown'] = "Cannot inspect signature for request: method unknown";
  104. $this->xmlrpcerr['http_error'] = '5';
  105. $this->xmlrpcstr['http_error'] = "Did not receive a '200 OK' response from remote server.";
  106. $this->xmlrpcerr['no_data'] = '6';
  107. $this->xmlrpcstr['no_data'] ='No data received from server.';
  108. $this->initialize($config);
  109. log_message('debug', "XML-RPC Class Initialized");
  110. }
  111. //-------------------------------------
  112. // Initialize Prefs
  113. //-------------------------------------
  114. function initialize($config = array())
  115. {
  116. if (count($config) > 0)
  117. {
  118. foreach ($config as $key => $val)
  119. {
  120. if (isset($this->$key))
  121. {
  122. $this->$key = $val;
  123. }
  124. }
  125. }
  126. }
  127. // END
  128. //-------------------------------------
  129. // Take URL and parse it
  130. //-------------------------------------
  131. function server($url, $port=80)
  132. {
  133. if (substr($url, 0, 4) != "http")
  134. {
  135. $url = "http://".$url;
  136. }
  137. $parts = parse_url($url);
  138. $path = ( ! isset($parts['path'])) ? '/' : $parts['path'];
  139. if (isset($parts['query']) && $parts['query'] != '')
  140. {
  141. $path .= '?'.$parts['query'];
  142. }
  143. $this->client = new XML_RPC_Client($path, $parts['host'], $port);
  144. }
  145. // END
  146. //-------------------------------------
  147. // Set Timeout
  148. //-------------------------------------
  149. function timeout($seconds=5)
  150. {
  151. if ( ! is_null($this->client) && is_int($seconds))
  152. {
  153. $this->client->timeout = $seconds;
  154. }
  155. }
  156. // END
  157. //-------------------------------------
  158. // Set Methods
  159. //-------------------------------------
  160. function method($function)
  161. {
  162. $this->method = $function;
  163. }
  164. // END
  165. //-------------------------------------
  166. // Take Array of Data and Create Objects
  167. //-------------------------------------
  168. function request($incoming)
  169. {
  170. if ( ! is_array($incoming))
  171. {
  172. // Send Error
  173. }
  174. $this->data = array();
  175. foreach($incoming as $key => $value)
  176. {
  177. $this->data[$key] = $this->values_parsing($value);
  178. }
  179. }
  180. // END
  181. //-------------------------------------
  182. // Set Debug
  183. //-------------------------------------
  184. function set_debug($flag = TRUE)
  185. {
  186. $this->debug = ($flag == TRUE) ? TRUE : FALSE;
  187. }
  188. //-------------------------------------
  189. // Values Parsing
  190. //-------------------------------------
  191. function values_parsing($value, $return = FALSE)
  192. {
  193. if (is_array($value) && isset($value['0']))
  194. {
  195. if ( ! isset($value['1']) OR (! isset($this->xmlrpcTypes[$value['1']])))
  196. {
  197. if (is_array($value[0]))
  198. {
  199. $temp = new XML_RPC_Values($value['0'], 'array');
  200. }
  201. else
  202. {
  203. $temp = new XML_RPC_Values($value['0'], 'string');
  204. }
  205. }
  206. elseif(is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array'))
  207. {
  208. while (list($k) = each($value['0']))
  209. {
  210. $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE);
  211. }
  212. $temp = new XML_RPC_Values($value['0'], $value['1']);
  213. }
  214. else
  215. {
  216. $temp = new XML_RPC_Values($value['0'], $value['1']);
  217. }
  218. }
  219. else
  220. {
  221. $temp = new XML_RPC_Values($value, 'string');
  222. }
  223. return $temp;
  224. }
  225. // END
  226. //-------------------------------------
  227. // Sends XML-RPC Request
  228. //-------------------------------------
  229. function send_request()
  230. {
  231. $this->message = new XML_RPC_Message($this->method,$this->data);
  232. $this->message->debug = $this->debug;
  233. if ( ! $this->result = $this->client->send($this->message))
  234. {
  235. $this->error = $this->result->errstr;
  236. return FALSE;
  237. }
  238. elseif( ! is_object($this->result->val))
  239. {
  240. $this->error = $this->result->errstr;
  241. return FALSE;
  242. }
  243. $this->response = $this->result->decode();
  244. return TRUE;
  245. }
  246. // END
  247. //-------------------------------------
  248. // Returns Error
  249. //-------------------------------------
  250. function display_error()
  251. {
  252. return $this->error;
  253. }
  254. // END
  255. //-------------------------------------
  256. // Returns Remote Server Response
  257. //-------------------------------------
  258. function display_response()
  259. {
  260. return $this->response;
  261. }
  262. // END
  263. //-------------------------------------
  264. // Sends an Error Message for Server Request
  265. //-------------------------------------
  266. function send_error_message($number, $message)
  267. {
  268. return new XML_RPC_Response('0',$number, $message);
  269. }
  270. // END
  271. //-------------------------------------
  272. // Send Response for Server Request
  273. //-------------------------------------
  274. function send_response($response)
  275. {
  276. // $response should be array of values, which will be parsed
  277. // based on their data and type into a valid group of XML-RPC values
  278. $response = $this->values_parsing($response);
  279. return new XML_RPC_Response($response);
  280. }
  281. // END
  282. } // END XML_RPC Class
  283. /**
  284. * XML-RPC Client class
  285. *
  286. * @category XML-RPC
  287. * @author ExpressionEngine Dev Team
  288. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  289. */
  290. class XML_RPC_Client extends CI_Xmlrpc
  291. {
  292. var $path = '';
  293. var $server = '';
  294. var $port = 80;
  295. var $errno = '';
  296. var $errstring = '';
  297. var $timeout = 5;
  298. var $no_multicall = false;
  299. function XML_RPC_Client($path, $server, $port=80)
  300. {
  301. parent::CI_Xmlrpc();
  302. $this->port = $port;
  303. $this->server = $server;
  304. $this->path = $path;
  305. }
  306. function send($msg)
  307. {
  308. if (is_array($msg))
  309. {
  310. // Multi-call disabled
  311. $r = new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'],$this->xmlrpcstr['multicall_recursion']);
  312. return $r;
  313. }
  314. return $this->sendPayload($msg);
  315. }
  316. function sendPayload($msg)
  317. {
  318. $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout);
  319. if ( ! is_resource($fp))
  320. {
  321. error_log($this->xmlrpcstr['http_error']);
  322. $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'],$this->xmlrpcstr['http_error']);
  323. return $r;
  324. }
  325. if(empty($msg->payload))
  326. {
  327. // $msg = XML_RPC_Messages
  328. $msg->createPayload();
  329. }
  330. $r = "\r\n";
  331. $op = "POST {$this->path} HTTP/1.0$r";
  332. $op .= "Host: {$this->server}$r";
  333. $op .= "Content-Type: text/xml$r";
  334. $op .= "User-Agent: {$this->xmlrpcName}$r";
  335. $op .= "Content-Length: ".strlen($msg->payload). "$r$r";
  336. $op .= $msg->payload;
  337. if ( ! fputs($fp, $op, strlen($op)))
  338. {
  339. error_log($this->xmlrpcstr['http_error']);
  340. $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
  341. return $r;
  342. }
  343. $resp = $msg->parseResponse($fp);
  344. fclose($fp);
  345. return $resp;
  346. }
  347. } // end class XML_RPC_Client
  348. /**
  349. * XML-RPC Response class
  350. *
  351. * @category XML-RPC
  352. * @author ExpressionEngine Dev Team
  353. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  354. */
  355. class XML_RPC_Response
  356. {
  357. var $val = 0;
  358. var $errno = 0;
  359. var $errstr = '';
  360. var $headers = array();
  361. function XML_RPC_Response($val, $code = 0, $fstr = '')
  362. {
  363. if ($code != 0)
  364. {
  365. // error
  366. $this->errno = $code;
  367. $this->errstr = htmlentities($fstr);
  368. }
  369. else if ( ! is_object($val))
  370. {
  371. // programmer error, not an object
  372. error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to XML_RPC_Response. Defaulting to empty value.");
  373. $this->val = new XML_RPC_Values();
  374. }
  375. else
  376. {
  377. $this->val = $val;
  378. }
  379. }
  380. function faultCode()
  381. {
  382. return $this->errno;
  383. }
  384. function faultString()
  385. {
  386. return $this->errstr;
  387. }
  388. function value()
  389. {
  390. return $this->val;
  391. }
  392. function prepare_response()
  393. {
  394. $result = "<methodResponse>\n";
  395. if ($this->errno)
  396. {
  397. $result .= '<fault>
  398. <value>
  399. <struct>
  400. <member>
  401. <name>faultCode</name>
  402. <value><int>' . $this->errno . '</int></value>
  403. </member>
  404. <member>
  405. <name>faultString</name>
  406. <value><string>' . $this->errstr . '</string></value>
  407. </member>
  408. </struct>
  409. </value>
  410. </fault>';
  411. }
  412. else
  413. {
  414. $result .= "<params>\n<param>\n" .
  415. $this->val->serialize_class() .
  416. "</param>\n</params>";
  417. }
  418. $result .= "\n</methodResponse>";
  419. return $result;
  420. }
  421. function decode($array=FALSE)
  422. {
  423. $CI =& get_instance();
  424. if ($array !== FALSE && is_array($array))
  425. {
  426. while (list($key) = each($array))
  427. {
  428. if (is_array($array[$key]))
  429. {
  430. $array[$key] = $this->decode($array[$key]);
  431. }
  432. else
  433. {
  434. $array[$key] = $CI->input->xss_clean($array[$key]);
  435. }
  436. }
  437. $result = $array;
  438. }
  439. else
  440. {
  441. $result = $this->xmlrpc_decoder($this->val);
  442. if (is_array($result))
  443. {
  444. $result = $this->decode($result);
  445. }
  446. else
  447. {
  448. $result = $CI->input->xss_clean($result);
  449. }
  450. }
  451. return $result;
  452. }
  453. //-------------------------------------
  454. // XML-RPC Object to PHP Types
  455. //-------------------------------------
  456. function xmlrpc_decoder($xmlrpc_val)
  457. {
  458. $kind = $xmlrpc_val->kindOf();
  459. if($kind == 'scalar')
  460. {
  461. return $xmlrpc_val->scalarval();
  462. }
  463. elseif($kind == 'array')
  464. {
  465. reset($xmlrpc_val->me);
  466. list($a,$b) = each($xmlrpc_val->me);
  467. $size = count($b);
  468. $arr = array();
  469. for($i = 0; $i < $size; $i++)
  470. {
  471. $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]);
  472. }
  473. return $arr;
  474. }
  475. elseif($kind == 'struct')
  476. {
  477. reset($xmlrpc_val->me['struct']);
  478. $arr = array();
  479. while(list($key,$value) = each($xmlrpc_val->me['struct']))
  480. {
  481. $arr[$key] = $this->xmlrpc_decoder($value);
  482. }
  483. return $arr;
  484. }
  485. }
  486. //-------------------------------------
  487. // ISO-8601 time to server or UTC time
  488. //-------------------------------------
  489. function iso8601_decode($time, $utc=0)
  490. {
  491. // return a timet in the localtime, or UTC
  492. $t = 0;
  493. if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $time, $regs))
  494. {
  495. if ($utc == 1)
  496. $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  497. else
  498. $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
  499. }
  500. return $t;
  501. }
  502. } // End Response Class
  503. /**
  504. * XML-RPC Message class
  505. *
  506. * @category XML-RPC
  507. * @author ExpressionEngine Dev Team
  508. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  509. */
  510. class XML_RPC_Message extends CI_Xmlrpc
  511. {
  512. var $payload;
  513. var $method_name;
  514. var $params = array();
  515. var $xh = array();
  516. function XML_RPC_Message($method, $pars=0)
  517. {
  518. parent::CI_Xmlrpc();
  519. $this->method_name = $method;
  520. if (is_array($pars) && count($pars) > 0)
  521. {
  522. for($i=0; $i<count($pars); $i++)
  523. {
  524. // $pars[$i] = XML_RPC_Values
  525. $this->params[] = $pars[$i];
  526. }
  527. }
  528. }
  529. //-------------------------------------
  530. // Create Payload to Send
  531. //-------------------------------------
  532. function createPayload()
  533. {
  534. $this->payload = "<?xml version=\"1.0\"?".">\r\n<methodCall>\r\n";
  535. $this->payload .= '<methodName>' . $this->method_name . "</methodName>\r\n";
  536. $this->payload .= "<params>\r\n";
  537. for($i=0; $i<count($this->params); $i++)
  538. {
  539. // $p = XML_RPC_Values
  540. $p = $this->params[$i];
  541. $this->payload .= "<param>\r\n".$p->serialize_class()."</param>\r\n";
  542. }
  543. $this->payload .= "</params>\r\n</methodCall>\r\n";
  544. }
  545. //-------------------------------------
  546. // Parse External XML-RPC Server's Response
  547. //-------------------------------------
  548. function parseResponse($fp)
  549. {
  550. $data = '';
  551. while($datum = fread($fp, 4096))
  552. {
  553. $data .= $datum;
  554. }
  555. //-------------------------------------
  556. // DISPLAY HTTP CONTENT for DEBUGGING
  557. //-------------------------------------
  558. if ($this->debug === TRUE)
  559. {
  560. echo "<pre>";
  561. echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
  562. echo "</pre>";
  563. }
  564. //-------------------------------------
  565. // Check for data
  566. //-------------------------------------
  567. if($data == "")
  568. {
  569. error_log($this->xmlrpcstr['no_data']);
  570. $r = new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']);
  571. return $r;
  572. }
  573. //-------------------------------------
  574. // Check for HTTP 200 Response
  575. //-------------------------------------
  576. if (strncmp($data, 'HTTP', 4) == 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data))
  577. {
  578. $errstr= substr($data, 0, strpos($data, "\n")-1);
  579. $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']. ' (' . $errstr . ')');
  580. return $r;
  581. }
  582. //-------------------------------------
  583. // Create and Set Up XML Parser
  584. //-------------------------------------
  585. $parser = xml_parser_create($this->xmlrpc_defencoding);
  586. $this->xh[$parser] = array();
  587. $this->xh[$parser]['isf'] = 0;
  588. $this->xh[$parser]['ac'] = '';
  589. $this->xh[$parser]['headers'] = array();
  590. $this->xh[$parser]['stack'] = array();
  591. $this->xh[$parser]['valuestack'] = array();
  592. $this->xh[$parser]['isf_reason'] = 0;
  593. xml_set_object($parser, $this);
  594. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
  595. xml_set_element_handler($parser, 'open_tag', 'closing_tag');
  596. xml_set_character_data_handler($parser, 'character_data');
  597. //xml_set_default_handler($parser, 'default_handler');
  598. //-------------------------------------
  599. // GET HEADERS
  600. //-------------------------------------
  601. $lines = explode("\r\n", $data);
  602. while (($line = array_shift($lines)))
  603. {
  604. if (strlen($line) < 1)
  605. {
  606. break;
  607. }
  608. $this->xh[$parser]['headers'][] = $line;
  609. }
  610. $data = implode("\r\n", $lines);
  611. //-------------------------------------
  612. // PARSE XML DATA
  613. //-------------------------------------
  614. if ( ! xml_parse($parser, $data, count($data)))
  615. {
  616. $errstr = sprintf('XML error: %s at line %d',
  617. xml_error_string(xml_get_error_code($parser)),
  618. xml_get_current_line_number($parser));
  619. //error_log($errstr);
  620. $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
  621. xml_parser_free($parser);
  622. return $r;
  623. }
  624. xml_parser_free($parser);
  625. // ---------------------------------------
  626. // Got Ourselves Some Badness, It Seems
  627. // ---------------------------------------
  628. if ($this->xh[$parser]['isf'] > 1)
  629. {
  630. if ($this->debug === TRUE)
  631. {
  632. echo "---Invalid Return---\n";
  633. echo $this->xh[$parser]['isf_reason'];
  634. echo "---Invalid Return---\n\n";
  635. }
  636. $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
  637. return $r;
  638. }
  639. elseif ( ! is_object($this->xh[$parser]['value']))
  640. {
  641. $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
  642. return $r;
  643. }
  644. //-------------------------------------
  645. // DISPLAY XML CONTENT for DEBUGGING
  646. //-------------------------------------
  647. if ($this->debug === TRUE)
  648. {
  649. echo "<pre>";
  650. if (count($this->xh[$parser]['headers'] > 0))
  651. {
  652. echo "---HEADERS---\n";
  653. foreach ($this->xh[$parser]['headers'] as $header)
  654. {
  655. echo "$header\n";
  656. }
  657. echo "---END HEADERS---\n\n";
  658. }
  659. echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
  660. echo "---PARSED---\n" ;
  661. var_dump($this->xh[$parser]['value']);
  662. echo "\n---END PARSED---</pre>";
  663. }
  664. //-------------------------------------
  665. // SEND RESPONSE
  666. //-------------------------------------
  667. $v = $this->xh[$parser]['value'];
  668. if ($this->xh[$parser]['isf'])
  669. {
  670. $errno_v = $v->me['struct']['faultCode'];
  671. $errstr_v = $v->me['struct']['faultString'];
  672. $errno = $errno_v->scalarval();
  673. if ($errno == 0)
  674. {
  675. // FAULT returned, errno needs to reflect that
  676. $errno = -1;
  677. }
  678. $r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval());
  679. }
  680. else
  681. {
  682. $r = new XML_RPC_Response($v);
  683. }
  684. $r->headers = $this->xh[$parser]['headers'];
  685. return $r;
  686. }
  687. // ------------------------------------
  688. // Begin Return Message Parsing section
  689. // ------------------------------------
  690. // quick explanation of components:
  691. // ac - used to accumulate values
  692. // isf - used to indicate a fault
  693. // lv - used to indicate "looking for a value": implements
  694. // the logic to allow values with no types to be strings
  695. // params - used to store parameters in method calls
  696. // method - used to store method name
  697. // stack - array with parent tree of the xml element,
  698. // used to validate the nesting of elements
  699. //-------------------------------------
  700. // Start Element Handler
  701. //-------------------------------------
  702. function open_tag($the_parser, $name, $attrs)
  703. {
  704. // If invalid nesting, then return
  705. if ($this->xh[$the_parser]['isf'] > 1) return;
  706. // Evaluate and check for correct nesting of XML elements
  707. if (count($this->xh[$the_parser]['stack']) == 0)
  708. {
  709. if ($name != 'METHODRESPONSE' && $name != 'METHODCALL')
  710. {
  711. $this->xh[$the_parser]['isf'] = 2;
  712. $this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing';
  713. return;
  714. }
  715. }
  716. else
  717. {
  718. // not top level element: see if parent is OK
  719. if ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE))
  720. {
  721. $this->xh[$the_parser]['isf'] = 2;
  722. $this->xh[$the_parser]['isf_reason'] = "XML-RPC element $name cannot be child of ".$this->xh[$the_parser]['stack'][0];
  723. return;
  724. }
  725. }
  726. switch($name)
  727. {
  728. case 'STRUCT':
  729. case 'ARRAY':
  730. // Creates array for child elements
  731. $cur_val = array('value' => array(),
  732. 'type' => $name);
  733. array_unshift($this->xh[$the_parser]['valuestack'], $cur_val);
  734. break;
  735. case 'METHODNAME':
  736. case 'NAME':
  737. $this->xh[$the_parser]['ac'] = '';
  738. break;
  739. case 'FAULT':
  740. $this->xh[$the_parser]['isf'] = 1;
  741. break;
  742. case 'PARAM':
  743. $this->xh[$the_parser]['value'] = null;
  744. break;
  745. case 'VALUE':
  746. $this->xh[$the_parser]['vt'] = 'value';
  747. $this->xh[$the_parser]['ac'] = '';
  748. $this->xh[$the_parser]['lv'] = 1;
  749. break;
  750. case 'I4':
  751. case 'INT':
  752. case 'STRING':
  753. case 'BOOLEAN':
  754. case 'DOUBLE':
  755. case 'DATETIME.ISO8601':
  756. case 'BASE64':
  757. if ($this->xh[$the_parser]['vt'] != 'value')
  758. {
  759. //two data elements inside a value: an error occurred!
  760. $this->xh[$the_parser]['isf'] = 2;
  761. $this->xh[$the_parser]['isf_reason'] = "'Twas a $name element following a ".$this->xh[$the_parser]['vt']." element inside a single value";
  762. return;
  763. }
  764. $this->xh[$the_parser]['ac'] = '';
  765. break;
  766. case 'MEMBER':
  767. // Set name of <member> to nothing to prevent errors later if no <name> is found
  768. $this->xh[$the_parser]['valuestack'][0]['name'] = '';
  769. // Set NULL value to check to see if value passed for this param/member
  770. $this->xh[$the_parser]['value'] = null;
  771. break;
  772. case 'DATA':
  773. case 'METHODCALL':
  774. case 'METHODRESPONSE':
  775. case 'PARAMS':
  776. // valid elements that add little to processing
  777. break;
  778. default:
  779. /// An Invalid Element is Found, so we have trouble
  780. $this->xh[$the_parser]['isf'] = 2;
  781. $this->xh[$the_parser]['isf_reason'] = "Invalid XML-RPC element found: $name";
  782. break;
  783. }
  784. // Add current element name to stack, to allow validation of nesting
  785. array_unshift($this->xh[$the_parser]['stack'], $name);
  786. if ($name != 'VALUE') $this->xh[$the_parser]['lv'] = 0;
  787. }
  788. // END
  789. //-------------------------------------
  790. // End Element Handler
  791. //-------------------------------------
  792. function closing_tag($the_parser, $name)
  793. {
  794. if ($this->xh[$the_parser]['isf'] > 1) return;
  795. // Remove current element from stack and set variable
  796. // NOTE: If the XML validates, then we do not have to worry about
  797. // the opening and closing of elements. Nesting is checked on the opening
  798. // tag so we be safe there as well.
  799. $curr_elem = array_shift($this->xh[$the_parser]['stack']);
  800. switch($name)
  801. {
  802. case 'STRUCT':
  803. case 'ARRAY':
  804. $cur_val = array_shift($this->xh[$the_parser]['valuestack']);
  805. $this->xh[$the_parser]['value'] = ( ! isset($cur_val['values'])) ? array() : $cur_val['values'];
  806. $this->xh[$the_parser]['vt'] = strtolower($name);
  807. break;
  808. case 'NAME':
  809. $this->xh[$the_parser]['valuestack'][0]['name'] = $this->xh[$the_parser]['ac'];
  810. break;
  811. case 'BOOLEAN':
  812. case 'I4':
  813. case 'INT':
  814. case 'STRING':
  815. case 'DOUBLE':
  816. case 'DATETIME.ISO8601':
  817. case 'BASE64':
  818. $this->xh[$the_parser]['vt'] = strtolower($name);
  819. if ($name == 'STRING')
  820. {
  821. $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
  822. }
  823. elseif ($name=='DATETIME.ISO8601')
  824. {
  825. $this->xh[$the_parser]['vt'] = $this->xmlrpcDateTime;
  826. $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
  827. }
  828. elseif ($name=='BASE64')
  829. {
  830. $this->xh[$the_parser]['value'] = base64_decode($this->xh[$the_parser]['ac']);
  831. }
  832. elseif ($name=='BOOLEAN')
  833. {
  834. // Translated BOOLEAN values to TRUE AND FALSE
  835. if ($this->xh[$the_parser]['ac'] == '1')
  836. {
  837. $this->xh[$the_parser]['value'] = TRUE;
  838. }
  839. else
  840. {
  841. $this->xh[$the_parser]['value'] = FALSE;
  842. }
  843. }
  844. elseif ($name=='DOUBLE')
  845. {
  846. // we have a DOUBLE
  847. // we must check that only 0123456789-.<space> are characters here
  848. if ( ! preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh[$the_parser]['ac']))
  849. {
  850. $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
  851. }
  852. else
  853. {
  854. $this->xh[$the_parser]['value'] = (double)$this->xh[$the_parser]['ac'];
  855. }
  856. }
  857. else
  858. {
  859. // we have an I4/INT
  860. // we must check that only 0123456789-<space> are characters here
  861. if ( ! preg_match('/^[+-]?[0-9\t ]+$/', $this->xh[$the_parser]['ac']))
  862. {
  863. $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
  864. }
  865. else
  866. {
  867. $this->xh[$the_parser]['value'] = (int)$this->xh[$the_parser]['ac'];
  868. }
  869. }
  870. $this->xh[$the_parser]['ac'] = '';
  871. $this->xh[$the_parser]['lv'] = 3; // indicate we've found a value
  872. break;
  873. case 'VALUE':
  874. // This if() detects if no scalar was inside <VALUE></VALUE>
  875. if ($this->xh[$the_parser]['vt']=='value')
  876. {
  877. $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
  878. $this->xh[$the_parser]['vt'] = $this->xmlrpcString;
  879. }
  880. // build the XML-RPC value out of the data received, and substitute it
  881. $temp = new XML_RPC_Values($this->xh[$the_parser]['value'], $this->xh[$the_parser]['vt']);
  882. if (count($this->xh[$the_parser]['valuestack']) && $this->xh[$the_parser]['valuestack'][0]['type'] == 'ARRAY')
  883. {
  884. // Array
  885. $this->xh[$the_parser]['valuestack'][0]['values'][] = $temp;
  886. }
  887. else
  888. {
  889. // Struct
  890. $this->xh[$the_parser]['value'] = $temp;
  891. }
  892. break;
  893. case 'MEMBER':
  894. $this->xh[$the_parser]['ac']='';
  895. // If value add to array in the stack for the last element built
  896. if ($this->xh[$the_parser]['value'])
  897. {
  898. $this->xh[$the_parser]['valuestack'][0]['values'][$this->xh[$the_parser]['valuestack'][0]['name']] = $this->xh[$the_parser]['value'];
  899. }
  900. break;
  901. case 'DATA':
  902. $this->xh[$the_parser]['ac']='';
  903. break;
  904. case 'PARAM':
  905. if ($this->xh[$the_parser]['value'])
  906. {
  907. $this->xh[$the_parser]['params'][] = $this->xh[$the_parser]['value'];
  908. }
  909. break;
  910. case 'METHODNAME':
  911. $this->xh[$the_parser]['method'] = ltrim($this->xh[$the_parser]['ac']);
  912. break;
  913. case 'PARAMS':
  914. case 'FAULT':
  915. case 'METHODCALL':
  916. case 'METHORESPONSE':
  917. // We're all good kids with nuthin' to do
  918. break;
  919. default:
  920. // End of an Invalid Element. Taken care of during the opening tag though
  921. break;
  922. }
  923. }
  924. //-------------------------------------
  925. // Parses Character Data
  926. //-------------------------------------
  927. function character_data($the_parser, $data)
  928. {
  929. if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already
  930. // If a value has not been found
  931. if ($this->xh[$the_parser]['lv'] != 3)
  932. {
  933. if ($this->xh[$the_parser]['lv'] == 1)
  934. {
  935. $this->xh[$the_parser]['lv'] = 2; // Found a value
  936. }
  937. if( ! @isset($this->xh[$the_parser]['ac']))
  938. {
  939. $this->xh[$the_parser]['ac'] = '';
  940. }
  941. $this->xh[$the_parser]['ac'] .= $data;
  942. }
  943. }
  944. function addParam($par) { $this->params[]=$par; }
  945. function output_parameters($array=FALSE)
  946. {
  947. $CI =& get_instance();
  948. if ($array !== FALSE && is_array($array))
  949. {
  950. while (list($key) = each($array))
  951. {
  952. if (is_array($array[$key]))
  953. {
  954. $array[$key] = $this->output_parameters($array[$key]);
  955. }
  956. else
  957. {
  958. $array[$key] = $CI->input->xss_clean($array[$key]);
  959. }
  960. }
  961. $parameters = $array;
  962. }
  963. else
  964. {
  965. $parameters = array();
  966. for ($i = 0; $i < count($this->params); $i++)
  967. {
  968. $a_param = $this->decode_message($this->params[$i]);
  969. if (is_array($a_param))
  970. {
  971. $parameters[] = $this->output_parameters($a_param);
  972. }
  973. else
  974. {
  975. $parameters[] = $CI->input->xss_clean($a_param);
  976. }
  977. }
  978. }
  979. return $parameters;
  980. }
  981. function decode_message($param)
  982. {
  983. $kind = $param->kindOf();
  984. if($kind == 'scalar')
  985. {
  986. return $param->scalarval();
  987. }
  988. elseif($kind == 'array')
  989. {
  990. reset($param->me);
  991. list($a,$b) = each($param->me);
  992. $arr = array();
  993. for($i = 0; $i < count($b); $i++)
  994. {
  995. $arr[] = $this->decode_message($param->me['array'][$i]);
  996. }
  997. return $arr;
  998. }
  999. elseif($kind == 'struct')
  1000. {
  1001. reset($param->me['struct']);
  1002. $arr = array();
  1003. while(list($key,$value) = each($param->me['struct']))
  1004. {
  1005. $arr[$key] = $this->decode_message($value);
  1006. }
  1007. return $arr;
  1008. }
  1009. }
  1010. } // End XML_RPC_Messages class
  1011. /**
  1012. * XML-RPC Values class
  1013. *
  1014. * @category XML-RPC
  1015. * @author ExpressionEngine Dev Team
  1016. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  1017. */
  1018. class XML_RPC_Values extends CI_Xmlrpc
  1019. {
  1020. var $me = array();
  1021. var $mytype = 0;
  1022. function XML_RPC_Values($val=-1, $type='')
  1023. {
  1024. parent::CI_Xmlrpc();
  1025. if ($val != -1 OR $type != '')
  1026. {
  1027. $type = $type == '' ? 'string' : $type;
  1028. if ($this->xmlrpcTypes[$type] == 1)
  1029. {
  1030. $this->addScalar($val,$type);
  1031. }
  1032. elseif ($this->xmlrpcTypes[$type] == 2)
  1033. {
  1034. $this->addArray($val);
  1035. }
  1036. elseif ($this->xmlrpcTypes[$type] == 3)
  1037. {
  1038. $this->addStruct($val);
  1039. }
  1040. }
  1041. }
  1042. function addScalar($val, $type='string')
  1043. {
  1044. $typeof = $this->xmlrpcTypes[$type];
  1045. if ($this->mytype==1)
  1046. {
  1047. echo '<strong>XML_RPC_Values</strong>: scalar can have only one value<br />';
  1048. return 0;
  1049. }
  1050. if ($typeof != 1)
  1051. {
  1052. echo '<strong>XML_RPC_Values</strong>: not a scalar type (${typeof})<br />';
  1053. return 0;
  1054. }
  1055. if ($type == $this->xmlrpcBoolean)
  1056. {
  1057. if (strcasecmp($val,'true')==0 OR $val==1 OR ($val==true && strcasecmp($val,'false')))
  1058. {
  1059. $val = 1;
  1060. }
  1061. else
  1062. {
  1063. $val=0;
  1064. }
  1065. }
  1066. if ($this->mytype == 2)
  1067. {
  1068. // adding to an array here
  1069. $ar = $this->me['array'];
  1070. $ar[] = new XML_RPC_Values($val, $type);
  1071. $this->me['array'] = $ar;
  1072. }
  1073. else
  1074. {
  1075. // a scalar, so set the value and remember we're scalar
  1076. $this->me[$type] = $val;
  1077. $this->mytype = $typeof;
  1078. }
  1079. return 1;
  1080. }
  1081. function addArray($vals)
  1082. {
  1083. if ($this->mytype != 0)
  1084. {
  1085. echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
  1086. return 0;
  1087. }
  1088. $this->mytype = $this->xmlrpcTypes['array'];
  1089. $this->me['array'] = $vals;
  1090. return 1;
  1091. }
  1092. function addStruct($vals)
  1093. {
  1094. if ($this->mytype != 0)
  1095. {
  1096. echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
  1097. return 0;
  1098. }
  1099. $this->mytype = $this->xmlrpcTypes['struct'];
  1100. $this->me['struct'] = $vals;
  1101. return 1;
  1102. }
  1103. function kindOf()
  1104. {
  1105. switch($this->mytype)
  1106. {
  1107. case 3:
  1108. return 'struct';
  1109. break;
  1110. case 2:
  1111. return 'array';
  1112. break;
  1113. case 1:
  1114. return 'scalar';
  1115. break;
  1116. default:
  1117. return 'undef';
  1118. }
  1119. }
  1120. function serializedata($typ, $val)
  1121. {
  1122. $rs = '';
  1123. switch($this->xmlrpcTypes[$typ])
  1124. {
  1125. case 3:
  1126. // struct
  1127. $rs .= "<struct>\n";
  1128. reset($val);
  1129. while(list($key2, $val2) = each($val))
  1130. {
  1131. $rs .= "<member>\n<name>{$key2}</name>\n";
  1132. $rs .= $this->serializeval($val2);
  1133. $rs .= "</member>\n";
  1134. }
  1135. $rs .= '</struct>';
  1136. break;
  1137. case 2:
  1138. // array
  1139. $rs .= "<array>\n<data>\n";
  1140. for($i=0; $i < count($val); $i++)
  1141. {
  1142. $rs .= $this->serializeval($val[$i]);
  1143. }
  1144. $rs.="</data>\n</array>\n";
  1145. break;
  1146. case 1:
  1147. // others
  1148. switch ($typ)
  1149. {
  1150. case $this->xmlrpcBase64:
  1151. $rs .= "<{$typ}>" . base64_encode((string)$val) . "</{$typ}>\n";
  1152. break;
  1153. case $this->xmlrpcBoolean:
  1154. $rs .= "<{$typ}>" . ((bool)$val ? '1' : '0') . "</{$typ}>\n";
  1155. break;
  1156. case $this->xmlrpcString:
  1157. $rs .= "<{$typ}>" . htmlspecialchars((string)$val). "</{$typ}>\n";
  1158. break;
  1159. default:
  1160. $rs .= "<{$typ}>{$val}</{$typ}>\n";
  1161. break;
  1162. }
  1163. default:
  1164. break;
  1165. }
  1166. return $rs;
  1167. }
  1168. function serialize_class()
  1169. {
  1170. return $this->serializeval($this);
  1171. }
  1172. function serializeval($o)
  1173. {
  1174. $ar = $o->me;
  1175. reset($ar);
  1176. list($typ, $val) = each($ar);
  1177. $rs = "<value>\n".$this->serializedata($typ, $val)."</value>\n";
  1178. return $rs;
  1179. }
  1180. function scalarval()
  1181. {
  1182. reset($this->me);
  1183. list($a,$b) = each($this->me);
  1184. return $b;
  1185. }
  1186. //-------------------------------------
  1187. // Encode time in ISO-8601 form.
  1188. //-------------------------------------
  1189. // Useful for sending time in XML-RPC
  1190. function iso8601_encode($time, $utc=0)
  1191. {
  1192. if ($utc == 1)
  1193. {
  1194. $t = strftime("%Y%m%dT%H:%M:%S", $time);
  1195. }
  1196. else
  1197. {
  1198. if (function_exists('gmstrftime'))
  1199. $t = gmstrftime("%Y%m%dT%H:%M:%S", $time);
  1200. else
  1201. $t = strftime("%Y%m%dT%H:%M:%S", $time - date('Z'));
  1202. }
  1203. return $t;
  1204. }
  1205. }
  1206. // END XML_RPC_Values Class
  1207. /* End of file Xmlrpc.php */
  1208. /* Location: ./system/libraries/Xmlrpc.php */