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