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