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