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

/system/libraries/Xmlrpc.php

https://bitbucket.org/naando_araujo/pagseguro
PHP | 1423 lines | 1020 code | 202 blank | 201 comment | 129 complexity | e84dd7f04027d0a930c1137c577e363a 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 5.1.6 or newer
  6. *
  7. * @package CodeIgniter
  8. * @author ExpressionEngine Dev Team
  9. * @copyright Copyright (c) 2008 - 2011, 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. var $xss_clean = TRUE;
  58. //-------------------------------------
  59. // VALUES THAT MULTIPLE CLASSES NEED
  60. //-------------------------------------
  61. public function __construct($config = array())
  62. {
  63. $this->xmlrpcName = $this->xmlrpcName;
  64. $this->xmlrpc_backslash = chr(92).chr(92);
  65. // Types for info sent back and forth
  66. $this->xmlrpcTypes = array(
  67. $this->xmlrpcI4 => '1',
  68. $this->xmlrpcInt => '1',
  69. $this->xmlrpcBoolean => '1',
  70. $this->xmlrpcString => '1',
  71. $this->xmlrpcDouble => '1',
  72. $this->xmlrpcDateTime => '1',
  73. $this->xmlrpcBase64 => '1',
  74. $this->xmlrpcArray => '2',
  75. $this->xmlrpcStruct => '3'
  76. );
  77. // Array of Valid Parents for Various XML-RPC elements
  78. $this->valid_parents = array('BOOLEAN' => array('VALUE'),
  79. 'I4' => array('VALUE'),
  80. 'INT' => array('VALUE'),
  81. 'STRING' => array('VALUE'),
  82. 'DOUBLE' => array('VALUE'),
  83. 'DATETIME.ISO8601' => array('VALUE'),
  84. 'BASE64' => array('VALUE'),
  85. 'ARRAY' => array('VALUE'),
  86. 'STRUCT' => array('VALUE'),
  87. 'PARAM' => array('PARAMS'),
  88. 'METHODNAME' => array('METHODCALL'),
  89. 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
  90. 'MEMBER' => array('STRUCT'),
  91. 'NAME' => array('MEMBER'),
  92. 'DATA' => array('ARRAY'),
  93. 'FAULT' => array('METHODRESPONSE'),
  94. 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT')
  95. );
  96. // XML-RPC Responses
  97. $this->xmlrpcerr['unknown_method'] = '1';
  98. $this->xmlrpcstr['unknown_method'] = 'This is not a known method for this XML-RPC Server';
  99. $this->xmlrpcerr['invalid_return'] = '2';
  100. $this->xmlrpcstr['invalid_return'] = 'The XML data received was either invalid or not in the correct form for XML-RPC. Turn on debugging to examine the XML data further.';
  101. $this->xmlrpcerr['incorrect_params'] = '3';
  102. $this->xmlrpcstr['incorrect_params'] = 'Incorrect parameters were passed to method';
  103. $this->xmlrpcerr['introspect_unknown'] = '4';
  104. $this->xmlrpcstr['introspect_unknown'] = "Cannot inspect signature for request: method unknown";
  105. $this->xmlrpcerr['http_error'] = '5';
  106. $this->xmlrpcstr['http_error'] = "Did not receive a '200 OK' response from remote server.";
  107. $this->xmlrpcerr['no_data'] = '6';
  108. $this->xmlrpcstr['no_data'] ='No data received from server.';
  109. $this->initialize($config);
  110. log_message('debug', "XML-RPC Class Initialized");
  111. }
  112. //-------------------------------------
  113. // Initialize Prefs
  114. //-------------------------------------
  115. function initialize($config = array())
  116. {
  117. if (count($config) > 0)
  118. {
  119. foreach ($config as $key => $val)
  120. {
  121. if (isset($this->$key))
  122. {
  123. $this->$key = $val;
  124. }
  125. }
  126. }
  127. }
  128. // END
  129. //-------------------------------------
  130. // Take URL and parse it
  131. //-------------------------------------
  132. function server($url, $port=80)
  133. {
  134. if (substr($url, 0, 4) != "http")
  135. {
  136. $url = "http://".$url;
  137. }
  138. $parts = parse_url($url);
  139. $path = ( ! isset($parts['path'])) ? '/' : $parts['path'];
  140. if (isset($parts['query']) && $parts['query'] != '')
  141. {
  142. $path .= '?'.$parts['query'];
  143. }
  144. $this->client = new XML_RPC_Client($path, $parts['host'], $port);
  145. }
  146. // END
  147. //-------------------------------------
  148. // Set Timeout
  149. //-------------------------------------
  150. function timeout($seconds=5)
  151. {
  152. if ( ! is_null($this->client) && is_int($seconds))
  153. {
  154. $this->client->timeout = $seconds;
  155. }
  156. }
  157. // END
  158. //-------------------------------------
  159. // Set Methods
  160. //-------------------------------------
  161. function method($function)
  162. {
  163. $this->method = $function;
  164. }
  165. // END
  166. //-------------------------------------
  167. // Take Array of Data and Create Objects
  168. //-------------------------------------
  169. function request($incoming)
  170. {
  171. if ( ! is_array($incoming))
  172. {
  173. // Send Error
  174. }
  175. $this->data = array();
  176. foreach ($incoming as $key => $value)
  177. {
  178. $this->data[$key] = $this->values_parsing($value);
  179. }
  180. }
  181. // END
  182. //-------------------------------------
  183. // Set Debug
  184. //-------------------------------------
  185. function set_debug($flag = TRUE)
  186. {
  187. $this->debug = ($flag == TRUE) ? TRUE : FALSE;
  188. }
  189. //-------------------------------------
  190. // Values Parsing
  191. //-------------------------------------
  192. function values_parsing($value, $return = FALSE)
  193. {
  194. if (is_array($value) && array_key_exists(0, $value))
  195. {
  196. if ( ! isset($value['1']) OR ( ! isset($this->xmlrpcTypes[$value['1']])))
  197. {
  198. if (is_array($value[0]))
  199. {
  200. $temp = new XML_RPC_Values($value['0'], 'array');
  201. }
  202. else
  203. {
  204. $temp = new XML_RPC_Values($value['0'], 'string');
  205. }
  206. }
  207. elseif (is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array'))
  208. {
  209. while (list($k) = each($value['0']))
  210. {
  211. $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE);
  212. }
  213. $temp = new XML_RPC_Values($value['0'], $value['1']);
  214. }
  215. else
  216. {
  217. $temp = new XML_RPC_Values($value['0'], $value['1']);
  218. }
  219. }
  220. else
  221. {
  222. $temp = new XML_RPC_Values($value, 'string');
  223. }
  224. return $temp;
  225. }
  226. // END
  227. //-------------------------------------
  228. // Sends XML-RPC Request
  229. //-------------------------------------
  230. function send_request()
  231. {
  232. $this->message = new XML_RPC_Message($this->method,$this->data);
  233. $this->message->debug = $this->debug;
  234. if ( ! $this->result = $this->client->send($this->message))
  235. {
  236. $this->error = $this->result->errstr;
  237. return FALSE;
  238. }
  239. elseif ( ! is_object($this->result->val))
  240. {
  241. $this->error = $this->result->errstr;
  242. return FALSE;
  243. }
  244. $this->response = $this->result->decode();
  245. return TRUE;
  246. }
  247. // END
  248. //-------------------------------------
  249. // Returns Error
  250. //-------------------------------------
  251. function display_error()
  252. {
  253. return $this->error;
  254. }
  255. // END
  256. //-------------------------------------
  257. // Returns Remote Server Response
  258. //-------------------------------------
  259. function display_response()
  260. {
  261. return $this->response;
  262. }
  263. // END
  264. //-------------------------------------
  265. // Sends an Error Message for Server Request
  266. //-------------------------------------
  267. function send_error_message($number, $message)
  268. {
  269. return new XML_RPC_Response('0',$number, $message);
  270. }
  271. // END
  272. //-------------------------------------
  273. // Send Response for Server Request
  274. //-------------------------------------
  275. function send_response($response)
  276. {
  277. // $response should be array of values, which will be parsed
  278. // based on their data and type into a valid group of XML-RPC values
  279. $response = $this->values_parsing($response);
  280. return new XML_RPC_Response($response);
  281. }
  282. // END
  283. } // END XML_RPC Class
  284. /**
  285. * XML-RPC Client class
  286. *
  287. * @category XML-RPC
  288. * @author ExpressionEngine Dev Team
  289. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  290. */
  291. class XML_RPC_Client extends CI_Xmlrpc
  292. {
  293. var $path = '';
  294. var $server = '';
  295. var $port = 80;
  296. var $errno = '';
  297. var $errstring = '';
  298. var $timeout = 5;
  299. var $no_multicall = FALSE;
  300. public function __construct($path, $server, $port=80)
  301. {
  302. parent::__construct();
  303. $this->port = $port;
  304. $this->server = $server;
  305. $this->path = $path;
  306. }
  307. function send($msg)
  308. {
  309. if (is_array($msg))
  310. {
  311. // Multi-call disabled
  312. $r = new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'],$this->xmlrpcstr['multicall_recursion']);
  313. return $r;
  314. }
  315. return $this->sendPayload($msg);
  316. }
  317. function sendPayload($msg)
  318. {
  319. $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout);
  320. if ( ! is_resource($fp))
  321. {
  322. error_log($this->xmlrpcstr['http_error']);
  323. $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'],$this->xmlrpcstr['http_error']);
  324. return $r;
  325. }
  326. if (empty($msg->payload))
  327. {
  328. // $msg = XML_RPC_Messages
  329. $msg->createPayload();
  330. }
  331. $r = "\r\n";
  332. $op = "POST {$this->path} HTTP/1.0$r";
  333. $op .= "Host: {$this->server}$r";
  334. $op .= "Content-Type: text/xml$r";
  335. $op .= "User-Agent: {$this->xmlrpcName}$r";
  336. $op .= "Content-Length: ".strlen($msg->payload). "$r$r";
  337. $op .= $msg->payload;
  338. if ( ! fputs($fp, $op, strlen($op)))
  339. {
  340. error_log($this->xmlrpcstr['http_error']);
  341. $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
  342. return $r;
  343. }
  344. $resp = $msg->parseResponse($fp);
  345. fclose($fp);
  346. return $resp;
  347. }
  348. } // end class XML_RPC_Client
  349. /**
  350. * XML-RPC Response class
  351. *
  352. * @category XML-RPC
  353. * @author ExpressionEngine Dev Team
  354. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  355. */
  356. class XML_RPC_Response
  357. {
  358. var $val = 0;
  359. var $errno = 0;
  360. var $errstr = '';
  361. var $headers = array();
  362. var $xss_clean = TRUE;
  363. public function __construct($val, $code = 0, $fstr = '')
  364. {
  365. if ($code != 0)
  366. {
  367. // error
  368. $this->errno = $code;
  369. $this->errstr = htmlentities($fstr);
  370. }
  371. else if ( ! is_object($val))
  372. {
  373. // programmer error, not an object
  374. error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to XML_RPC_Response. Defaulting to empty value.");
  375. $this->val = new XML_RPC_Values();
  376. }
  377. else
  378. {
  379. $this->val = $val;
  380. }
  381. }
  382. function faultCode()
  383. {
  384. return $this->errno;
  385. }
  386. function faultString()
  387. {
  388. return $this->errstr;
  389. }
  390. function value()
  391. {
  392. return $this->val;
  393. }
  394. function prepare_response()
  395. {
  396. $result = "<methodResponse>\n";
  397. if ($this->errno)
  398. {
  399. $result .= '<fault>
  400. <value>
  401. <struct>
  402. <member>
  403. <name>faultCode</name>
  404. <value><int>' . $this->errno . '</int></value>
  405. </member>
  406. <member>
  407. <name>faultString</name>
  408. <value><string>' . $this->errstr . '</string></value>
  409. </member>
  410. </struct>
  411. </value>
  412. </fault>';
  413. }
  414. else
  415. {
  416. $result .= "<params>\n<param>\n" .
  417. $this->val->serialize_class() .
  418. "</param>\n</params>";
  419. }
  420. $result .= "\n</methodResponse>";
  421. return $result;
  422. }
  423. function decode($array=FALSE)
  424. {
  425. $CI =& get_instance();
  426. if ($array !== FALSE && is_array($array))
  427. {
  428. while (list($key) = each($array))
  429. {
  430. if (is_array($array[$key]))
  431. {
  432. $array[$key] = $this->decode($array[$key]);
  433. }
  434. else
  435. {
  436. $array[$key] = ($this->xss_clean) ? $CI->security->xss_clean($array[$key]) : $array[$key];
  437. }
  438. }
  439. $result = $array;
  440. }
  441. else
  442. {
  443. $result = $this->xmlrpc_decoder($this->val);
  444. if (is_array($result))
  445. {
  446. $result = $this->decode($result);
  447. }
  448. else
  449. {
  450. $result = ($this->xss_clean) ? $CI->security->xss_clean($result) : $result;
  451. }
  452. }
  453. return $result;
  454. }
  455. //-------------------------------------
  456. // XML-RPC Object to PHP Types
  457. //-------------------------------------
  458. function xmlrpc_decoder($xmlrpc_val)
  459. {
  460. $kind = $xmlrpc_val->kindOf();
  461. if ($kind == 'scalar')
  462. {
  463. return $xmlrpc_val->scalarval();
  464. }
  465. elseif ($kind == 'array')
  466. {
  467. reset($xmlrpc_val->me);
  468. list($a,$b) = each($xmlrpc_val->me);
  469. $size = count($b);
  470. $arr = array();
  471. for ($i = 0; $i < $size; $i++)
  472. {
  473. $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]);
  474. }
  475. return $arr;
  476. }
  477. elseif ($kind == 'struct')
  478. {
  479. reset($xmlrpc_val->me['struct']);
  480. $arr = array();
  481. while (list($key,$value) = each($xmlrpc_val->me['struct']))
  482. {
  483. $arr[$key] = $this->xmlrpc_decoder($value);
  484. }
  485. return $arr;
  486. }
  487. }
  488. //-------------------------------------
  489. // ISO-8601 time to server or UTC time
  490. //-------------------------------------
  491. function iso8601_decode($time, $utc=0)
  492. {
  493. // return a timet in the localtime, or UTC
  494. $t = 0;
  495. if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $time, $regs))
  496. {
  497. $fnc = ($utc == 1) ? 'gmmktime' : 'mktime';
  498. $t = $fnc($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. public function __construct($method, $pars=0)
  517. {
  518. parent::__construct();
  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. // 'bits' is for the MetaWeblog API image bits
  959. // @todo - this needs to be made more general purpose
  960. $array[$key] = ($key == 'bits' OR $this->xss_clean == FALSE) ? $array[$key] : $CI->security->xss_clean($array[$key]);
  961. }
  962. }
  963. $parameters = $array;
  964. }
  965. else
  966. {
  967. $parameters = array();
  968. for ($i = 0; $i < count($this->params); $i++)
  969. {
  970. $a_param = $this->decode_message($this->params[$i]);
  971. if (is_array($a_param))
  972. {
  973. $parameters[] = $this->output_parameters($a_param);
  974. }
  975. else
  976. {
  977. $parameters[] = ($this->xss_clean) ? $CI->security->xss_clean($a_param) : $a_param;
  978. }
  979. }
  980. }
  981. return $parameters;
  982. }
  983. function decode_message($param)
  984. {
  985. $kind = $param->kindOf();
  986. if ($kind == 'scalar')
  987. {
  988. return $param->scalarval();
  989. }
  990. elseif ($kind == 'array')
  991. {
  992. reset($param->me);
  993. list($a,$b) = each($param->me);
  994. $arr = array();
  995. for($i = 0; $i < count($b); $i++)
  996. {
  997. $arr[] = $this->decode_message($param->me['array'][$i]);
  998. }
  999. return $arr;
  1000. }
  1001. elseif ($kind == 'struct')
  1002. {
  1003. reset($param->me['struct']);
  1004. $arr = array();
  1005. while (list($key,$value) = each($param->me['struct']))
  1006. {
  1007. $arr[$key] = $this->decode_message($value);
  1008. }
  1009. return $arr;
  1010. }
  1011. }
  1012. } // End XML_RPC_Messages class
  1013. /**
  1014. * XML-RPC Values class
  1015. *
  1016. * @category XML-RPC
  1017. * @author ExpressionEngine Dev Team
  1018. * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
  1019. */
  1020. class XML_RPC_Values extends CI_Xmlrpc
  1021. {
  1022. var $me = array();
  1023. var $mytype = 0;
  1024. public function __construct($val=-1, $type='')
  1025. {
  1026. parent::__construct();
  1027. if ($val != -1 OR $type != '')
  1028. {
  1029. $type = $type == '' ? 'string' : $type;
  1030. if ($this->xmlrpcTypes[$type] == 1)
  1031. {
  1032. $this->addScalar($val,$type);
  1033. }
  1034. elseif ($this->xmlrpcTypes[$type] == 2)
  1035. {
  1036. $this->addArray($val);
  1037. }
  1038. elseif ($this->xmlrpcTypes[$type] == 3)
  1039. {
  1040. $this->addStruct($val);
  1041. }
  1042. }
  1043. }
  1044. function addScalar($val, $type='string')
  1045. {
  1046. $typeof = $this->xmlrpcTypes[$type];
  1047. if ($this->mytype==1)
  1048. {
  1049. echo '<strong>XML_RPC_Values</strong>: scalar can have only one value<br />';
  1050. return 0;
  1051. }
  1052. if ($typeof != 1)
  1053. {
  1054. echo '<strong>XML_RPC_Values</strong>: not a scalar type (${typeof})<br />';
  1055. return 0;
  1056. }
  1057. if ($type == $this->xmlrpcBoolean)
  1058. {
  1059. if (strcasecmp($val,'true')==0 OR $val==1 OR ($val==true && strcasecmp($val,'false')))
  1060. {
  1061. $val = 1;
  1062. }
  1063. else
  1064. {
  1065. $val=0;
  1066. }
  1067. }
  1068. if ($this->mytype == 2)
  1069. {
  1070. // adding to an array here
  1071. $ar = $this->me['array'];
  1072. $ar[] = new XML_RPC_Values($val, $type);
  1073. $this->me['array'] = $ar;
  1074. }
  1075. else
  1076. {
  1077. // a scalar, so set the value and remember we're scalar
  1078. $this->me[$type] = $val;
  1079. $this->mytype = $typeof;
  1080. }
  1081. return 1;
  1082. }
  1083. function addArray($vals)
  1084. {
  1085. if ($this->mytype != 0)
  1086. {
  1087. echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
  1088. return 0;
  1089. }
  1090. $this->mytype = $this->xmlrpcTypes['array'];
  1091. $this->me['array'] = $vals;
  1092. return 1;
  1093. }
  1094. function addStruct($vals)
  1095. {
  1096. if ($this->mytype != 0)
  1097. {
  1098. echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
  1099. return 0;
  1100. }
  1101. $this->mytype = $this->xmlrpcTypes['struct'];
  1102. $this->me['struct'] = $vals;
  1103. return 1;
  1104. }
  1105. function kindOf()
  1106. {
  1107. switch($this->mytype)
  1108. {
  1109. case 3:
  1110. return 'struct';
  1111. break;
  1112. case 2:
  1113. return 'array';
  1114. break;
  1115. case 1:
  1116. return 'scalar';
  1117. break;
  1118. default:
  1119. return 'undef';
  1120. }
  1121. }
  1122. function serializedata($typ, $val)
  1123. {
  1124. $rs = '';
  1125. switch($this->xmlrpcTypes[$typ])
  1126. {
  1127. case 3:
  1128. // struct
  1129. $rs .= "<struct>\n";
  1130. reset($val);
  1131. while (list($key2, $val2) = each($val))
  1132. {
  1133. $rs .= "<member>\n<name>{$key2}</name>\n";
  1134. $rs .= $this->serializeval($val2);
  1135. $rs .= "</member>\n";
  1136. }
  1137. $rs .= '</struct>';
  1138. break;
  1139. case 2:
  1140. // array
  1141. $rs .= "<array>\n<data>\n";
  1142. for($i=0; $i < count($val); $i++)
  1143. {
  1144. $rs .= $this->serializeval($val[$i]);
  1145. }
  1146. $rs.="</data>\n</array>\n";
  1147. break;
  1148. case 1:
  1149. // others
  1150. switch ($typ)
  1151. {
  1152. case $this->xmlrpcBase64:
  1153. $rs .= "<{$typ}>" . base64_encode((string)$val) . "</{$typ}>\n";
  1154. break;
  1155. case $this->xmlrpcBoolean:
  1156. $rs .= "<{$typ}>" . ((bool)$val ? '1' : '0') . "</{$typ}>\n";
  1157. break;
  1158. case $this->xmlrpcString:
  1159. $rs .= "<{$typ}>" . htmlspecialchars((string)$val). "</{$typ}>\n";
  1160. break;
  1161. default:
  1162. $rs .= "<{$typ}>{$val}</{$typ}>\n";
  1163. break;
  1164. }
  1165. default:
  1166. break;
  1167. }
  1168. return $rs;
  1169. }
  1170. function serialize_class()
  1171. {
  1172. return $this->serializeval($this);
  1173. }
  1174. function serializeval($o)
  1175. {
  1176. $ar = $o->me;
  1177. reset($ar);
  1178. list($typ, $val) = each($ar);
  1179. $rs = "<value>\n".$this->serializedata($typ, $val)."</value>\n";
  1180. return $rs;
  1181. }
  1182. function scalarval()
  1183. {
  1184. reset($this->me);
  1185. list($a,$b) = each($this->me);
  1186. return $b;
  1187. }
  1188. //-------------------------------------
  1189. // Encode time in ISO-8601 form.
  1190. //-------------------------------------
  1191. // Useful for sending time in XML-RPC
  1192. function iso8601_encode($time, $utc=0)
  1193. {
  1194. if ($utc == 1)
  1195. {
  1196. $t = strftime("%Y%m%dT%H:%M:%S", $time);
  1197. }
  1198. else
  1199. {
  1200. if (function_exists('gmstrftime'))
  1201. $t = gmstrftime("%Y%m%dT%H:%M:%S", $time);
  1202. else
  1203. $t = strftime("%Y%m%dT%H:%M:%S", $time - date('Z'));
  1204. }
  1205. return $t;
  1206. }
  1207. }
  1208. // END XML_RPC_Values Class
  1209. /* End of file Xmlrpc.php */
  1210. /* Location: ./system/libraries/Xmlrpc.php */