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