1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 5.1.6 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 if ( ! function_exists('xml_parser_create'))
18 show_error('Your PHP installation does not support XML');
22 // ------------------------------------------------------------------------
25 * XML-RPC request handler class
27 * @package CodeIgniter
28 * @subpackage Libraries
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
35 var $debug = FALSE; // Debugging on or off
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';
46 var $xmlrpcTypes = array();
47 var $valid_parents = array();
48 var $xmlrpcerr = array(); // Response numbers
49 var $xmlrpcstr = array(); // Response strings
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
62 var $error = ''; // Error string for request
64 var $response = array(); // Response from remote server
66 var $xss_clean = TRUE;
68 //-------------------------------------
69 // VALUES THAT MULTIPLE CLASSES NEED
70 //-------------------------------------
72 public function __construct($config = array())
74 $this->xmlrpcName = $this->xmlrpcName;
75 $this->xmlrpc_backslash = chr(92).chr(92);
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'
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')
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 received 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.';
125 $this->initialize($config);
127 log_message('debug', "XML-RPC Class Initialized");
131 //-------------------------------------
133 //-------------------------------------
135 function initialize($config = array())
137 if (count($config) > 0)
139 foreach ($config as $key => $val)
141 if (isset($this->$key))
150 //-------------------------------------
151 // Take URL and parse it
152 //-------------------------------------
154 function server($url, $port=80)
156 if (substr($url, 0, 4) != "http")
158 $url = "http://".$url;
161 $parts = parse_url($url);
163 $path = ( ! isset($parts['path'])) ? '/' : $parts['path'];
165 if (isset($parts['query']) && $parts['query'] != '')
167 $path .= '?'.$parts['query'];
170 $this->client = new XML_RPC_Client($path, $parts['host'], $port);
174 //-------------------------------------
176 //-------------------------------------
178 function timeout($seconds=5)
180 if ( ! is_null($this->client) && is_int($seconds))
182 $this->client->timeout = $seconds;
187 //-------------------------------------
189 //-------------------------------------
191 function method($function)
193 $this->method = $function;
197 //-------------------------------------
198 // Take Array of Data and Create Objects
199 //-------------------------------------
201 function request($incoming)
203 if ( ! is_array($incoming))
208 $this->data = array();
210 foreach ($incoming as $key => $value)
212 $this->data[$key] = $this->values_parsing($value);
218 //-------------------------------------
220 //-------------------------------------
222 function set_debug($flag = TRUE)
224 $this->debug = ($flag == TRUE) ? TRUE : FALSE;
227 //-------------------------------------
229 //-------------------------------------
231 function values_parsing($value, $return = FALSE)
233 if (is_array($value) && array_key_exists(0, $value))
235 if ( ! isset($value['1']) OR ( ! isset($this->xmlrpcTypes[$value['1']])))
237 if (is_array($value[0]))
239 $temp = new XML_RPC_Values($value['0'], 'array');
243 $temp = new XML_RPC_Values($value['0'], 'string');
246 elseif (is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array'))
248 while (list($k) = each($value['0']))
250 $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE);
253 $temp = new XML_RPC_Values($value['0'], $value['1']);
257 $temp = new XML_RPC_Values($value['0'], $value['1']);
262 $temp = new XML_RPC_Values($value, 'string');
270 //-------------------------------------
271 // Sends XML-RPC Request
272 //-------------------------------------
274 function send_request()
276 $this->message = new XML_RPC_Message($this->method,$this->data);
277 $this->message->debug = $this->debug;
279 if ( ! $this->result = $this->client->send($this->message))
281 $this->error = $this->result->errstr;
284 elseif ( ! is_object($this->result->val))
286 $this->error = $this->result->errstr;
290 $this->response = $this->result->decode();
296 //-------------------------------------
298 //-------------------------------------
300 function display_error()
306 //-------------------------------------
307 // Returns Remote Server Response
308 //-------------------------------------
310 function display_response()
312 return $this->response;
316 //-------------------------------------
317 // Sends an Error Message for Server Request
318 //-------------------------------------
320 function send_error_message($number, $message)
322 return new XML_RPC_Response('0',$number, $message);
327 //-------------------------------------
328 // Send Response for Server Request
329 //-------------------------------------
331 function send_response($response)
333 // $response should be array of values, which will be parsed
334 // based on their data and type into a valid group of XML-RPC values
336 $response = $this->values_parsing($response);
338 return new XML_RPC_Response($response);
342 } // END XML_RPC Class
347 * XML-RPC Client class
350 * @author ExpressionEngine Dev Team
351 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
353 class XML_RPC_Client extends CI_Xmlrpc
361 var $no_multicall = FALSE;
363 public function __construct($path, $server, $port=80)
365 parent::__construct();
368 $this->server = $server;
376 // Multi-call disabled
377 $r = new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'],$this->xmlrpcstr['multicall_recursion']);
381 return $this->sendPayload($msg);
384 function sendPayload($msg)
386 $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout);
388 if ( ! is_resource($fp))
390 error_log($this->xmlrpcstr['http_error']);
391 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'],$this->xmlrpcstr['http_error']);
395 if (empty($msg->payload))
397 // $msg = XML_RPC_Messages
398 $msg->createPayload();
402 $op = "POST {$this->path} HTTP/1.0$r";
403 $op .= "Host: {$this->server}$r";
404 $op .= "Content-Type: text/xml$r";
405 $op .= "User-Agent: {$this->xmlrpcName}$r";
406 $op .= "Content-Length: ".strlen($msg->payload). "$r$r";
407 $op .= $msg->payload;
410 if ( ! fputs($fp, $op, strlen($op)))
412 error_log($this->xmlrpcstr['http_error']);
413 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
416 $resp = $msg->parseResponse($fp);
421 } // end class XML_RPC_Client
425 * XML-RPC Response class
428 * @author ExpressionEngine Dev Team
429 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
431 class XML_RPC_Response
436 var $headers = array();
437 var $xss_clean = TRUE;
439 public function __construct($val, $code = 0, $fstr = '')
444 $this->errno = $code;
445 $this->errstr = htmlentities($fstr);
447 else if ( ! is_object($val))
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();
464 function faultString()
466 return $this->errstr;
474 function prepare_response()
476 $result = "<methodResponse>\n";
483 <name>faultCode</name>
484 <value><int>' . $this->errno . '</int></value>
487 <name>faultString</name>
488 <value><string>' . $this->errstr . '</string></value>
496 $result .= "<params>\n<param>\n" .
497 $this->val->serialize_class() .
498 "</param>\n</params>";
500 $result .= "\n</methodResponse>";
504 function decode($array=FALSE)
506 $CI =& get_instance();
508 if ($array !== FALSE && is_array($array))
510 while (list($key) = each($array))
512 if (is_array($array[$key]))
514 $array[$key] = $this->decode($array[$key]);
518 $array[$key] = ($this->xss_clean) ? $CI->security->xss_clean($array[$key]) : $array[$key];
526 $result = $this->xmlrpc_decoder($this->val);
528 if (is_array($result))
530 $result = $this->decode($result);
534 $result = ($this->xss_clean) ? $CI->security->xss_clean($result) : $result;
543 //-------------------------------------
544 // XML-RPC Object to PHP Types
545 //-------------------------------------
547 function xmlrpc_decoder($xmlrpc_val)
549 $kind = $xmlrpc_val->kindOf();
551 if ($kind == 'scalar')
553 return $xmlrpc_val->scalarval();
555 elseif ($kind == 'array')
557 reset($xmlrpc_val->me);
558 list($a,$b) = each($xmlrpc_val->me);
563 for ($i = 0; $i < $size; $i++)
565 $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]);
569 elseif ($kind == 'struct')
571 reset($xmlrpc_val->me['struct']);
574 while (list($key,$value) = each($xmlrpc_val->me['struct']))
576 $arr[$key] = $this->xmlrpc_decoder($value);
583 //-------------------------------------
584 // ISO-8601 time to server or UTC time
585 //-------------------------------------
587 function iso8601_decode($time, $utc=0)
589 // return a timet in the localtime, or UTC
591 if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $time, $regs))
593 $fnc = ($utc == 1) ? 'gmmktime' : 'mktime';
594 $t = $fnc($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
599 } // End Response Class
604 * XML-RPC Message class
607 * @author ExpressionEngine Dev Team
608 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
610 class XML_RPC_Message extends CI_Xmlrpc
614 var $params = array();
617 public function __construct($method, $pars=0)
619 parent::__construct();
621 $this->method_name = $method;
622 if (is_array($pars) && count($pars) > 0)
624 for ($i=0; $i<count($pars); $i++)
626 // $pars[$i] = XML_RPC_Values
627 $this->params[] = $pars[$i];
632 //-------------------------------------
633 // Create Payload to Send
634 //-------------------------------------
636 function createPayload()
638 $this->payload = "<?xml version=\"1.0\"?".">\r\n<methodCall>\r\n";
639 $this->payload .= '<methodName>' . $this->method_name . "</methodName>\r\n";
640 $this->payload .= "<params>\r\n";
642 for ($i=0; $i<count($this->params); $i++)
644 // $p = XML_RPC_Values
645 $p = $this->params[$i];
646 $this->payload .= "<param>\r\n".$p->serialize_class()."</param>\r\n";
649 $this->payload .= "</params>\r\n</methodCall>\r\n";
652 //-------------------------------------
653 // Parse External XML-RPC Server's Response
654 //-------------------------------------
656 function parseResponse($fp)
660 while ($datum = fread($fp, 4096))
665 //-------------------------------------
666 // DISPLAY HTTP CONTENT for DEBUGGING
667 //-------------------------------------
669 if ($this->debug === TRUE)
672 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
676 //-------------------------------------
678 //-------------------------------------
682 error_log($this->xmlrpcstr['no_data']);
683 $r = new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']);
688 //-------------------------------------
689 // Check for HTTP 200 Response
690 //-------------------------------------
692 if (strncmp($data, 'HTTP', 4) == 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data))
694 $errstr= substr($data, 0, strpos($data, "\n")-1);
695 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']. ' (' . $errstr . ')');
699 //-------------------------------------
700 // Create and Set Up XML Parser
701 //-------------------------------------
703 $parser = xml_parser_create($this->xmlrpc_defencoding);
705 $this->xh[$parser] = array();
706 $this->xh[$parser]['isf'] = 0;
707 $this->xh[$parser]['ac'] = '';
708 $this->xh[$parser]['headers'] = array();
709 $this->xh[$parser]['stack'] = array();
710 $this->xh[$parser]['valuestack'] = array();
711 $this->xh[$parser]['isf_reason'] = 0;
713 xml_set_object($parser, $this);
714 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
715 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
716 xml_set_character_data_handler($parser, 'character_data');
717 //xml_set_default_handler($parser, 'default_handler');
720 //-------------------------------------
722 //-------------------------------------
724 $lines = explode("\r\n", $data);
725 while (($line = array_shift($lines)))
727 if (strlen($line) < 1)
731 $this->xh[$parser]['headers'][] = $line;
733 $data = implode("\r\n", $lines);
736 //-------------------------------------
738 //-------------------------------------
740 if ( ! xml_parse($parser, $data, count($data)))
742 $errstr = sprintf('XML error: %s at line %d',
743 xml_error_string(xml_get_error_code($parser)),
744 xml_get_current_line_number($parser));
745 //error_log($errstr);
746 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
747 xml_parser_free($parser);
750 xml_parser_free($parser);
752 // ---------------------------------------
753 // Got Ourselves Some Badness, It Seems
754 // ---------------------------------------
756 if ($this->xh[$parser]['isf'] > 1)
758 if ($this->debug === TRUE)
760 echo "---Invalid Return---\n";
761 echo $this->xh[$parser]['isf_reason'];
762 echo "---Invalid Return---\n\n";
765 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
768 elseif ( ! is_object($this->xh[$parser]['value']))
770 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
774 //-------------------------------------
775 // DISPLAY XML CONTENT for DEBUGGING
776 //-------------------------------------
778 if ($this->debug === TRUE)
782 if (count($this->xh[$parser]['headers'] > 0))
784 echo "---HEADERS---\n";
785 foreach ($this->xh[$parser]['headers'] as $header)
789 echo "---END HEADERS---\n\n";
792 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
794 echo "---PARSED---\n" ;
795 var_dump($this->xh[$parser]['value']);
796 echo "\n---END PARSED---</pre>";
799 //-------------------------------------
801 //-------------------------------------
803 $v = $this->xh[$parser]['value'];
805 if ($this->xh[$parser]['isf'])
807 $errno_v = $v->me['struct']['faultCode'];
808 $errstr_v = $v->me['struct']['faultString'];
809 $errno = $errno_v->scalarval();
813 // FAULT returned, errno needs to reflect that
817 $r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval());
821 $r = new XML_RPC_Response($v);
824 $r->headers = $this->xh[$parser]['headers'];
828 // ------------------------------------
829 // Begin Return Message Parsing section
830 // ------------------------------------
832 // quick explanation of components:
833 // ac - used to accumulate values
834 // isf - used to indicate a fault
835 // lv - used to indicate "looking for a value": implements
836 // the logic to allow values with no types to be strings
837 // params - used to store parameters in method calls
838 // method - used to store method name
839 // stack - array with parent tree of the xml element,
840 // used to validate the nesting of elements
842 //-------------------------------------
843 // Start Element Handler
844 //-------------------------------------
846 function open_tag($the_parser, $name, $attrs)
848 // If invalid nesting, then return
849 if ($this->xh[$the_parser]['isf'] > 1) return;
851 // Evaluate and check for correct nesting of XML elements
853 if (count($this->xh[$the_parser]['stack']) == 0)
855 if ($name != 'METHODRESPONSE' && $name != 'METHODCALL')
857 $this->xh[$the_parser]['isf'] = 2;
858 $this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing';
864 // not top level element: see if parent is OK
865 if ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE))
867 $this->xh[$the_parser]['isf'] = 2;
868 $this->xh[$the_parser]['isf_reason'] = "XML-RPC element $name cannot be child of ".$this->xh[$the_parser]['stack'][0];
877 // Creates array for child elements
879 $cur_val = array('value' => array(),
882 array_unshift($this->xh[$the_parser]['valuestack'], $cur_val);
886 $this->xh[$the_parser]['ac'] = '';
889 $this->xh[$the_parser]['isf'] = 1;
892 $this->xh[$the_parser]['value'] = NULL;
895 $this->xh[$the_parser]['vt'] = 'value';
896 $this->xh[$the_parser]['ac'] = '';
897 $this->xh[$the_parser]['lv'] = 1;
904 case 'DATETIME.ISO8601':
906 if ($this->xh[$the_parser]['vt'] != 'value')
908 //two data elements inside a value: an error occurred!
909 $this->xh[$the_parser]['isf'] = 2;
910 $this->xh[$the_parser]['isf_reason'] = "'Twas a $name element following a ".$this->xh[$the_parser]['vt']." element inside a single value";
914 $this->xh[$the_parser]['ac'] = '';
917 // Set name of <member> to nothing to prevent errors later if no <name> is found
918 $this->xh[$the_parser]['valuestack'][0]['name'] = '';
920 // Set NULL value to check to see if value passed for this param/member
921 $this->xh[$the_parser]['value'] = NULL;
925 case 'METHODRESPONSE':
927 // valid elements that add little to processing
930 /// An Invalid Element is Found, so we have trouble
931 $this->xh[$the_parser]['isf'] = 2;
932 $this->xh[$the_parser]['isf_reason'] = "Invalid XML-RPC element found: $name";
936 // Add current element name to stack, to allow validation of nesting
937 array_unshift($this->xh[$the_parser]['stack'], $name);
939 if ($name != 'VALUE') $this->xh[$the_parser]['lv'] = 0;
944 //-------------------------------------
945 // End Element Handler
946 //-------------------------------------
948 function closing_tag($the_parser, $name)
950 if ($this->xh[$the_parser]['isf'] > 1) return;
952 // Remove current element from stack and set variable
953 // NOTE: If the XML validates, then we do not have to worry about
954 // the opening and closing of elements. Nesting is checked on the opening
955 // tag so we be safe there as well.
957 $curr_elem = array_shift($this->xh[$the_parser]['stack']);
963 $cur_val = array_shift($this->xh[$the_parser]['valuestack']);
964 $this->xh[$the_parser]['value'] = ( ! isset($cur_val['values'])) ? array() : $cur_val['values'];
965 $this->xh[$the_parser]['vt'] = strtolower($name);
968 $this->xh[$the_parser]['valuestack'][0]['name'] = $this->xh[$the_parser]['ac'];
975 case 'DATETIME.ISO8601':
977 $this->xh[$the_parser]['vt'] = strtolower($name);
979 if ($name == 'STRING')
981 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
983 elseif ($name=='DATETIME.ISO8601')
985 $this->xh[$the_parser]['vt'] = $this->xmlrpcDateTime;
986 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
988 elseif ($name=='BASE64')
990 $this->xh[$the_parser]['value'] = base64_decode($this->xh[$the_parser]['ac']);
992 elseif ($name=='BOOLEAN')
994 // Translated BOOLEAN values to TRUE AND FALSE
995 if ($this->xh[$the_parser]['ac'] == '1')
997 $this->xh[$the_parser]['value'] = TRUE;
1001 $this->xh[$the_parser]['value'] = FALSE;
1004 elseif ($name=='DOUBLE')
1007 // we must check that only 0123456789-.<space> are characters here
1008 if ( ! preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh[$the_parser]['ac']))
1010 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1014 $this->xh[$the_parser]['value'] = (double)$this->xh[$the_parser]['ac'];
1019 // we have an I4/INT
1020 // we must check that only 0123456789-<space> are characters here
1021 if ( ! preg_match('/^[+-]?[0-9\t ]+$/', $this->xh[$the_parser]['ac']))
1023 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1027 $this->xh[$the_parser]['value'] = (int)$this->xh[$the_parser]['ac'];
1030 $this->xh[$the_parser]['ac'] = '';
1031 $this->xh[$the_parser]['lv'] = 3; // indicate we've found a value
1034 // This if() detects if no scalar was inside <VALUE></VALUE>
1035 if ($this->xh[$the_parser]['vt']=='value')
1037 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1038 $this->xh[$the_parser]['vt'] = $this->xmlrpcString;
1041 // build the XML-RPC value out of the data received, and substitute it
1042 $temp = new XML_RPC_Values($this->xh[$the_parser]['value'], $this->xh[$the_parser]['vt']);
1044 if (count($this->xh[$the_parser]['valuestack']) && $this->xh[$the_parser]['valuestack'][0]['type'] == 'ARRAY')
1047 $this->xh[$the_parser]['valuestack'][0]['values'][] = $temp;
1052 $this->xh[$the_parser]['value'] = $temp;
1056 $this->xh[$the_parser]['ac']='';
1058 // If value add to array in the stack for the last element built
1059 if ($this->xh[$the_parser]['value'])
1061 $this->xh[$the_parser]['valuestack'][0]['values'][$this->xh[$the_parser]['valuestack'][0]['name']] = $this->xh[$the_parser]['value'];
1065 $this->xh[$the_parser]['ac']='';
1068 if ($this->xh[$the_parser]['value'])
1070 $this->xh[$the_parser]['params'][] = $this->xh[$the_parser]['value'];
1074 $this->xh[$the_parser]['method'] = ltrim($this->xh[$the_parser]['ac']);
1079 case 'METHORESPONSE':
1080 // We're all good kids with nuthin' to do
1083 // End of an Invalid Element. Taken care of during the opening tag though
1088 //-------------------------------------
1089 // Parses Character Data
1090 //-------------------------------------
1092 function character_data($the_parser, $data)
1094 if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already
1096 // If a value has not been found
1097 if ($this->xh[$the_parser]['lv'] != 3)
1099 if ($this->xh[$the_parser]['lv'] == 1)
1101 $this->xh[$the_parser]['lv'] = 2; // Found a value
1104 if ( ! @isset($this->xh[$the_parser]['ac']))
1106 $this->xh[$the_parser]['ac'] = '';
1109 $this->xh[$the_parser]['ac'] .= $data;
1114 function addParam($par) { $this->params[]=$par; }
1116 function output_parameters($array=FALSE)
1118 $CI =& get_instance();
1120 if ($array !== FALSE && is_array($array))
1122 while (list($key) = each($array))
1124 if (is_array($array[$key]))
1126 $array[$key] = $this->output_parameters($array[$key]);
1130 // 'bits' is for the MetaWeblog API image bits
1131 // @todo - this needs to be made more general purpose
1132 $array[$key] = ($key == 'bits' OR $this->xss_clean == FALSE) ? $array[$key] : $CI->security->xss_clean($array[$key]);
1136 $parameters = $array;
1140 $parameters = array();
1142 for ($i = 0; $i < count($this->params); $i++)
1144 $a_param = $this->decode_message($this->params[$i]);
1146 if (is_array($a_param))
1148 $parameters[] = $this->output_parameters($a_param);
1152 $parameters[] = ($this->xss_clean) ? $CI->security->xss_clean($a_param) : $a_param;
1161 function decode_message($param)
1163 $kind = $param->kindOf();
1165 if ($kind == 'scalar')
1167 return $param->scalarval();
1169 elseif ($kind == 'array')
1172 list($a,$b) = each($param->me);
1176 for($i = 0; $i < count($b); $i++)
1178 $arr[] = $this->decode_message($param->me['array'][$i]);
1183 elseif ($kind == 'struct')
1185 reset($param->me['struct']);
1189 while (list($key,$value) = each($param->me['struct']))
1191 $arr[$key] = $this->decode_message($value);
1198 } // End XML_RPC_Messages class
1203 * XML-RPC Values class
1206 * @author ExpressionEngine Dev Team
1207 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
1209 class XML_RPC_Values extends CI_Xmlrpc
1214 public function __construct($val=-1, $type='')
1216 parent::__construct();
1218 if ($val != -1 OR $type != '')
1220 $type = $type == '' ? 'string' : $type;
1222 if ($this->xmlrpcTypes[$type] == 1)
1224 $this->addScalar($val,$type);
1226 elseif ($this->xmlrpcTypes[$type] == 2)
1228 $this->addArray($val);
1230 elseif ($this->xmlrpcTypes[$type] == 3)
1232 $this->addStruct($val);
1237 function addScalar($val, $type='string')
1239 $typeof = $this->xmlrpcTypes[$type];
1241 if ($this->mytype==1)
1243 echo '<strong>XML_RPC_Values</strong>: scalar can have only one value<br />';
1249 echo '<strong>XML_RPC_Values</strong>: not a scalar type (${typeof})<br />';
1253 if ($type == $this->xmlrpcBoolean)
1255 if (strcasecmp($val,'true')==0 OR $val==1 OR ($val==true && strcasecmp($val,'false')))
1265 if ($this->mytype == 2)
1267 // adding to an array here
1268 $ar = $this->me['array'];
1269 $ar[] = new XML_RPC_Values($val, $type);
1270 $this->me['array'] = $ar;
1274 // a scalar, so set the value and remember we're scalar
1275 $this->me[$type] = $val;
1276 $this->mytype = $typeof;
1281 function addArray($vals)
1283 if ($this->mytype != 0)
1285 echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
1289 $this->mytype = $this->xmlrpcTypes['array'];
1290 $this->me['array'] = $vals;
1294 function addStruct($vals)
1296 if ($this->mytype != 0)
1298 echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
1301 $this->mytype = $this->xmlrpcTypes['struct'];
1302 $this->me['struct'] = $vals;
1308 switch($this->mytype)
1324 function serializedata($typ, $val)
1328 switch($this->xmlrpcTypes[$typ])
1332 $rs .= "<struct>\n";
1334 while (list($key2, $val2) = each($val))
1336 $rs .= "<member>\n<name>{$key2}</name>\n";
1337 $rs .= $this->serializeval($val2);
1338 $rs .= "</member>\n";
1344 $rs .= "<array>\n<data>\n";
1345 for($i=0; $i < count($val); $i++)
1347 $rs .= $this->serializeval($val[$i]);
1349 $rs.="</data>\n</array>\n";
1355 case $this->xmlrpcBase64:
1356 $rs .= "<{$typ}>" . base64_encode((string)$val) . "</{$typ}>\n";
1358 case $this->xmlrpcBoolean:
1359 $rs .= "<{$typ}>" . ((bool)$val ? '1' : '0') . "</{$typ}>\n";
1361 case $this->xmlrpcString:
1362 $rs .= "<{$typ}>" . htmlspecialchars((string)$val). "</{$typ}>\n";
1365 $rs .= "<{$typ}>{$val}</{$typ}>\n";
1374 function serialize_class()
1376 return $this->serializeval($this);
1379 function serializeval($o)
1384 list($typ, $val) = each($ar);
1385 $rs = "<value>\n".$this->serializedata($typ, $val)."</value>\n";
1389 function scalarval()
1392 list($a,$b) = each($this->me);
1397 //-------------------------------------
1398 // Encode time in ISO-8601 form.
1399 //-------------------------------------
1401 // Useful for sending time in XML-RPC
1403 function iso8601_encode($time, $utc=0)
1407 $t = strftime("%Y%m%dT%H:%M:%S", $time);
1411 if (function_exists('gmstrftime'))
1412 $t = gmstrftime("%Y%m%dT%H:%M:%S", $time);
1414 $t = strftime("%Y%m%dT%H:%M:%S", $time - date('Z'));
1420 // END XML_RPC_Values Class
1422 /* End of file Xmlrpc.php */
1423 /* Location: ./system/libraries/Xmlrpc.php */