blob: 7b1e3fa6e3c13530a05439ef10140e5c50b16e09 [file] [log] [blame]
Derek Jones37f4b9c2011-07-01 17:56:50 -05001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Derek Allard2067d1a2008-11-13 22:59:24 +00002/**
3 * CodeIgniter
4 *
Greg Aker741de1c2010-11-10 14:52:57 -06005 * An open source application development framework for PHP 5.1.6 or newer
Derek Allard2067d1a2008-11-13 22:59:24 +00006 *
Derek Jonesf4a4bd82011-10-20 12:18:42 -05007 * NOTICE OF LICENSE
8 *
9 * Licensed under the Open Software License version 3.0
10 *
11 * This source file is subject to the Open Software License (OSL 3.0) that is
12 * bundled with this package in the files license.txt / license.rst. It is
13 * also available through the world wide web at this URL:
14 * http://opensource.org/licenses/OSL-3.0
15 * If you did not receive a copy of the license and are unable to obtain it
16 * through the world wide web, please send an email to
17 * licensing@ellislab.com so we can send you a copy immediately.
18 *
Derek Allard2067d1a2008-11-13 22:59:24 +000019 * @package CodeIgniter
Derek Jonesf4a4bd82011-10-20 12:18:42 -050020 * @author EllisLab Dev Team
21 * @copyright Copyright (c) 2008 - 2011, EllisLab, Inc. (http://ellislab.com/)
22 * @license http://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0)
Derek Allard2067d1a2008-11-13 22:59:24 +000023 * @link http://codeigniter.com
24 * @since Version 1.0
25 * @filesource
26 */
27
28if ( ! function_exists('xml_parser_create'))
Barry Mienydd671972010-10-04 16:33:58 +020029{
Derek Allard2067d1a2008-11-13 22:59:24 +000030 show_error('Your PHP installation does not support XML');
31}
32
33
34// ------------------------------------------------------------------------
35
36/**
37 * XML-RPC request handler class
38 *
39 * @package CodeIgniter
40 * @subpackage Libraries
41 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -050042 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +000043 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
44 */
45class CI_Xmlrpc {
46
Barry Mienydd671972010-10-04 16:33:58 +020047 var $debug = FALSE; // Debugging on or off
Derek Allard2067d1a2008-11-13 22:59:24 +000048 var $xmlrpcI4 = 'i4';
49 var $xmlrpcInt = 'int';
50 var $xmlrpcBoolean = 'boolean';
Barry Mienydd671972010-10-04 16:33:58 +020051 var $xmlrpcDouble = 'double';
Derek Allard2067d1a2008-11-13 22:59:24 +000052 var $xmlrpcString = 'string';
Derek Jones7e39c0c2009-06-24 16:25:03 +000053 var $xmlrpcDateTime = 'dateTime.iso8601';
Derek Allard2067d1a2008-11-13 22:59:24 +000054 var $xmlrpcBase64 = 'base64';
55 var $xmlrpcArray = 'array';
56 var $xmlrpcStruct = 'struct';
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 var $xmlrpcTypes = array();
59 var $valid_parents = array();
60 var $xmlrpcerr = array(); // Response numbers
Derek Jones37f4b9c2011-07-01 17:56:50 -050061 var $xmlrpcstr = array(); // Response strings
Barry Mienydd671972010-10-04 16:33:58 +020062
Derek Allard2067d1a2008-11-13 22:59:24 +000063 var $xmlrpc_defencoding = 'UTF-8';
64 var $xmlrpcName = 'XML-RPC for CodeIgniter';
65 var $xmlrpcVersion = '1.1';
66 var $xmlrpcerruser = 800; // Start of user errors
67 var $xmlrpcerrxml = 100; // Start of XML Parse errors
68 var $xmlrpc_backslash = ''; // formulate backslashes for escaping regexp
Barry Mienydd671972010-10-04 16:33:58 +020069
Derek Allard2067d1a2008-11-13 22:59:24 +000070 var $client;
71 var $method;
72 var $data;
73 var $message = '';
Barry Mienydd671972010-10-04 16:33:58 +020074 var $error = ''; // Error string for request
Derek Allard2067d1a2008-11-13 22:59:24 +000075 var $result;
Derek Jones37f4b9c2011-07-01 17:56:50 -050076 var $response = array(); // Response from remote server
Derek Allard2067d1a2008-11-13 22:59:24 +000077
Robin Sowell66a3fc02010-03-18 09:44:55 -040078 var $xss_clean = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000079
80 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -050081 // VALUES THAT MULTIPLE CLASSES NEED
Derek Allard2067d1a2008-11-13 22:59:24 +000082 //-------------------------------------
83
Greg Akera9263282010-11-10 15:26:43 -060084 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000085 {
Barry Mienydd671972010-10-04 16:33:58 +020086 $this->xmlrpcName = $this->xmlrpcName;
Derek Allard2067d1a2008-11-13 22:59:24 +000087 $this->xmlrpc_backslash = chr(92).chr(92);
Barry Mienydd671972010-10-04 16:33:58 +020088
Derek Allard2067d1a2008-11-13 22:59:24 +000089 // Types for info sent back and forth
90 $this->xmlrpcTypes = array(
Barry Mienydd671972010-10-04 16:33:58 +020091 $this->xmlrpcI4 => '1',
92 $this->xmlrpcInt => '1',
93 $this->xmlrpcBoolean => '1',
94 $this->xmlrpcString => '1',
95 $this->xmlrpcDouble => '1',
96 $this->xmlrpcDateTime => '1',
97 $this->xmlrpcBase64 => '1',
98 $this->xmlrpcArray => '2',
99 $this->xmlrpcStruct => '3'
Derek Allard2067d1a2008-11-13 22:59:24 +0000100 );
Barry Mienydd671972010-10-04 16:33:58 +0200101
Derek Allard2067d1a2008-11-13 22:59:24 +0000102 // Array of Valid Parents for Various XML-RPC elements
103 $this->valid_parents = array('BOOLEAN' => array('VALUE'),
104 'I4' => array('VALUE'),
105 'INT' => array('VALUE'),
106 'STRING' => array('VALUE'),
107 'DOUBLE' => array('VALUE'),
108 'DATETIME.ISO8601' => array('VALUE'),
109 'BASE64' => array('VALUE'),
110 'ARRAY' => array('VALUE'),
111 'STRUCT' => array('VALUE'),
112 'PARAM' => array('PARAMS'),
113 'METHODNAME' => array('METHODCALL'),
114 'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
115 'MEMBER' => array('STRUCT'),
116 'NAME' => array('MEMBER'),
117 'DATA' => array('ARRAY'),
118 'FAULT' => array('METHODRESPONSE'),
119 'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT')
120 );
Barry Mienydd671972010-10-04 16:33:58 +0200121
122
Derek Allard2067d1a2008-11-13 22:59:24 +0000123 // XML-RPC Responses
124 $this->xmlrpcerr['unknown_method'] = '1';
125 $this->xmlrpcstr['unknown_method'] = 'This is not a known method for this XML-RPC Server';
126 $this->xmlrpcerr['invalid_return'] = '2';
Derek Jones37f4b9c2011-07-01 17:56:50 -0500127 $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.';
Derek Allard2067d1a2008-11-13 22:59:24 +0000128 $this->xmlrpcerr['incorrect_params'] = '3';
129 $this->xmlrpcstr['incorrect_params'] = 'Incorrect parameters were passed to method';
130 $this->xmlrpcerr['introspect_unknown'] = '4';
131 $this->xmlrpcstr['introspect_unknown'] = "Cannot inspect signature for request: method unknown";
132 $this->xmlrpcerr['http_error'] = '5';
133 $this->xmlrpcstr['http_error'] = "Did not receive a '200 OK' response from remote server.";
134 $this->xmlrpcerr['no_data'] = '6';
135 $this->xmlrpcstr['no_data'] ='No data received from server.';
Barry Mienydd671972010-10-04 16:33:58 +0200136
Derek Allard2067d1a2008-11-13 22:59:24 +0000137 $this->initialize($config);
Barry Mienydd671972010-10-04 16:33:58 +0200138
Derek Allard2067d1a2008-11-13 22:59:24 +0000139 log_message('debug', "XML-RPC Class Initialized");
140 }
Barry Mienydd671972010-10-04 16:33:58 +0200141
142
Derek Allard2067d1a2008-11-13 22:59:24 +0000143 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500144 // Initialize Prefs
Derek Allard2067d1a2008-11-13 22:59:24 +0000145 //-------------------------------------
146
147 function initialize($config = array())
148 {
Derek Jones33559102009-02-02 18:50:38 +0000149 if (count($config) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 {
151 foreach ($config as $key => $val)
152 {
153 if (isset($this->$key))
154 {
Barry Mienydd671972010-10-04 16:33:58 +0200155 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000156 }
157 }
158 }
159 }
160 // END
Barry Mienydd671972010-10-04 16:33:58 +0200161
Derek Allard2067d1a2008-11-13 22:59:24 +0000162 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500163 // Take URL and parse it
Derek Allard2067d1a2008-11-13 22:59:24 +0000164 //-------------------------------------
165
166 function server($url, $port=80)
167 {
168 if (substr($url, 0, 4) != "http")
169 {
170 $url = "http://".$url;
171 }
Barry Mienydd671972010-10-04 16:33:58 +0200172
Derek Allard2067d1a2008-11-13 22:59:24 +0000173 $parts = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200174
Derek Allard2067d1a2008-11-13 22:59:24 +0000175 $path = ( ! isset($parts['path'])) ? '/' : $parts['path'];
Barry Mienydd671972010-10-04 16:33:58 +0200176
Derek Allard2067d1a2008-11-13 22:59:24 +0000177 if (isset($parts['query']) && $parts['query'] != '')
178 {
179 $path .= '?'.$parts['query'];
Barry Mienydd671972010-10-04 16:33:58 +0200180 }
181
Derek Allard2067d1a2008-11-13 22:59:24 +0000182 $this->client = new XML_RPC_Client($path, $parts['host'], $port);
183 }
184 // END
Barry Mienydd671972010-10-04 16:33:58 +0200185
Derek Allard2067d1a2008-11-13 22:59:24 +0000186 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500187 // Set Timeout
Derek Allard2067d1a2008-11-13 22:59:24 +0000188 //-------------------------------------
189
190 function timeout($seconds=5)
191 {
192 if ( ! is_null($this->client) && is_int($seconds))
193 {
194 $this->client->timeout = $seconds;
195 }
196 }
197 // END
Barry Mienydd671972010-10-04 16:33:58 +0200198
Derek Allard2067d1a2008-11-13 22:59:24 +0000199 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500200 // Set Methods
Derek Allard2067d1a2008-11-13 22:59:24 +0000201 //-------------------------------------
202
203 function method($function)
204 {
205 $this->method = $function;
206 }
207 // END
Barry Mienydd671972010-10-04 16:33:58 +0200208
Derek Allard2067d1a2008-11-13 22:59:24 +0000209 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500210 // Take Array of Data and Create Objects
Derek Allard2067d1a2008-11-13 22:59:24 +0000211 //-------------------------------------
212
213 function request($incoming)
214 {
215 if ( ! is_array($incoming))
216 {
217 // Send Error
218 }
Barry Mienydd671972010-10-04 16:33:58 +0200219
Derek Allard2067d1a2008-11-13 22:59:24 +0000220 $this->data = array();
Barry Mienydd671972010-10-04 16:33:58 +0200221
Pascal Kriete14287f32011-02-14 13:39:34 -0500222 foreach ($incoming as $key => $value)
Derek Allard2067d1a2008-11-13 22:59:24 +0000223 {
224 $this->data[$key] = $this->values_parsing($value);
225 }
226 }
227 // END
Barry Mienydd671972010-10-04 16:33:58 +0200228
229
Derek Allard2067d1a2008-11-13 22:59:24 +0000230 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500231 // Set Debug
Derek Allard2067d1a2008-11-13 22:59:24 +0000232 //-------------------------------------
233
234 function set_debug($flag = TRUE)
235 {
236 $this->debug = ($flag == TRUE) ? TRUE : FALSE;
237 }
Barry Mienydd671972010-10-04 16:33:58 +0200238
Derek Allard2067d1a2008-11-13 22:59:24 +0000239 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500240 // Values Parsing
Derek Allard2067d1a2008-11-13 22:59:24 +0000241 //-------------------------------------
242
243 function values_parsing($value, $return = FALSE)
244 {
Derek Jones8c4b5e72010-01-06 22:21:41 +0000245 if (is_array($value) && array_key_exists(0, $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000246 {
Pascal Kriete14287f32011-02-14 13:39:34 -0500247 if ( ! isset($value['1']) OR ( ! isset($this->xmlrpcTypes[$value['1']])))
Derek Allard2067d1a2008-11-13 22:59:24 +0000248 {
249 if (is_array($value[0]))
250 {
251 $temp = new XML_RPC_Values($value['0'], 'array');
252 }
253 else
254 {
255 $temp = new XML_RPC_Values($value['0'], 'string');
256 }
257 }
Pascal Kriete14287f32011-02-14 13:39:34 -0500258 elseif (is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array'))
Derek Allard2067d1a2008-11-13 22:59:24 +0000259 {
260 while (list($k) = each($value['0']))
261 {
262 $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE);
263 }
Barry Mienydd671972010-10-04 16:33:58 +0200264
Derek Allard2067d1a2008-11-13 22:59:24 +0000265 $temp = new XML_RPC_Values($value['0'], $value['1']);
266 }
267 else
268 {
269 $temp = new XML_RPC_Values($value['0'], $value['1']);
270 }
271 }
272 else
273 {
274 $temp = new XML_RPC_Values($value, 'string');
275 }
276
277 return $temp;
278 }
279 // END
280
281
282 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500283 // Sends XML-RPC Request
Derek Allard2067d1a2008-11-13 22:59:24 +0000284 //-------------------------------------
285
286 function send_request()
287 {
288 $this->message = new XML_RPC_Message($this->method,$this->data);
289 $this->message->debug = $this->debug;
Barry Mienydd671972010-10-04 16:33:58 +0200290
Derek Allard2067d1a2008-11-13 22:59:24 +0000291 if ( ! $this->result = $this->client->send($this->message))
292 {
293 $this->error = $this->result->errstr;
294 return FALSE;
295 }
Pascal Kriete14287f32011-02-14 13:39:34 -0500296 elseif ( ! is_object($this->result->val))
Derek Allard2067d1a2008-11-13 22:59:24 +0000297 {
298 $this->error = $this->result->errstr;
299 return FALSE;
300 }
Barry Mienydd671972010-10-04 16:33:58 +0200301
Derek Allard2067d1a2008-11-13 22:59:24 +0000302 $this->response = $this->result->decode();
Barry Mienydd671972010-10-04 16:33:58 +0200303
Derek Allard2067d1a2008-11-13 22:59:24 +0000304 return TRUE;
305 }
306 // END
Barry Mienydd671972010-10-04 16:33:58 +0200307
Derek Allard2067d1a2008-11-13 22:59:24 +0000308 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500309 // Returns Error
Derek Allard2067d1a2008-11-13 22:59:24 +0000310 //-------------------------------------
311
312 function display_error()
313 {
314 return $this->error;
315 }
316 // END
Barry Mienydd671972010-10-04 16:33:58 +0200317
Derek Allard2067d1a2008-11-13 22:59:24 +0000318 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500319 // Returns Remote Server Response
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 //-------------------------------------
321
322 function display_response()
323 {
324 return $this->response;
325 }
326 // END
Barry Mienydd671972010-10-04 16:33:58 +0200327
Derek Allard2067d1a2008-11-13 22:59:24 +0000328 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500329 // Sends an Error Message for Server Request
Derek Allard2067d1a2008-11-13 22:59:24 +0000330 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200331
Derek Allard2067d1a2008-11-13 22:59:24 +0000332 function send_error_message($number, $message)
333 {
334 return new XML_RPC_Response('0',$number, $message);
335 }
336 // END
Barry Mienydd671972010-10-04 16:33:58 +0200337
338
Derek Allard2067d1a2008-11-13 22:59:24 +0000339 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500340 // Send Response for Server Request
Derek Allard2067d1a2008-11-13 22:59:24 +0000341 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200342
Derek Allard2067d1a2008-11-13 22:59:24 +0000343 function send_response($response)
344 {
345 // $response should be array of values, which will be parsed
346 // based on their data and type into a valid group of XML-RPC values
Barry Mienydd671972010-10-04 16:33:58 +0200347
Derek Allard2067d1a2008-11-13 22:59:24 +0000348 $response = $this->values_parsing($response);
Barry Mienydd671972010-10-04 16:33:58 +0200349
Derek Allard2067d1a2008-11-13 22:59:24 +0000350 return new XML_RPC_Response($response);
351 }
352 // END
Barry Mienydd671972010-10-04 16:33:58 +0200353
Derek Allard2067d1a2008-11-13 22:59:24 +0000354} // END XML_RPC Class
355
Barry Mienydd671972010-10-04 16:33:58 +0200356
357
Derek Allard2067d1a2008-11-13 22:59:24 +0000358/**
359 * XML-RPC Client class
360 *
361 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -0500362 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +0000363 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
364 */
365class XML_RPC_Client extends CI_Xmlrpc
366{
367 var $path = '';
368 var $server = '';
369 var $port = 80;
370 var $errno = '';
371 var $errstring = '';
372 var $timeout = 5;
Pascal Kriete8761ef52011-02-14 13:13:52 -0500373 var $no_multicall = FALSE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000374
Greg Akera9263282010-11-10 15:26:43 -0600375 public function __construct($path, $server, $port=80)
Derek Allard2067d1a2008-11-13 22:59:24 +0000376 {
Greg Akera9263282010-11-10 15:26:43 -0600377 parent::__construct();
Barry Mienydd671972010-10-04 16:33:58 +0200378
Derek Allard2067d1a2008-11-13 22:59:24 +0000379 $this->port = $port;
380 $this->server = $server;
381 $this->path = $path;
382 }
Barry Mienydd671972010-10-04 16:33:58 +0200383
Derek Allard2067d1a2008-11-13 22:59:24 +0000384 function send($msg)
385 {
386 if (is_array($msg))
387 {
388 // Multi-call disabled
389 $r = new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'],$this->xmlrpcstr['multicall_recursion']);
390 return $r;
391 }
392
393 return $this->sendPayload($msg);
394 }
395
396 function sendPayload($msg)
Barry Mienydd671972010-10-04 16:33:58 +0200397 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000398 $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout);
Barry Mienydd671972010-10-04 16:33:58 +0200399
Derek Allard2067d1a2008-11-13 22:59:24 +0000400 if ( ! is_resource($fp))
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 }
Barry Mienydd671972010-10-04 16:33:58 +0200406
Pascal Kriete14287f32011-02-14 13:39:34 -0500407 if (empty($msg->payload))
Derek Allard2067d1a2008-11-13 22:59:24 +0000408 {
409 // $msg = XML_RPC_Messages
410 $msg->createPayload();
411 }
Barry Mienydd671972010-10-04 16:33:58 +0200412
Derek Allard2067d1a2008-11-13 22:59:24 +0000413 $r = "\r\n";
Derek Jones37f4b9c2011-07-01 17:56:50 -0500414 $op = "POST {$this->path} HTTP/1.0$r";
Derek Allard2067d1a2008-11-13 22:59:24 +0000415 $op .= "Host: {$this->server}$r";
416 $op .= "Content-Type: text/xml$r";
417 $op .= "User-Agent: {$this->xmlrpcName}$r";
418 $op .= "Content-Length: ".strlen($msg->payload). "$r$r";
419 $op .= $msg->payload;
Barry Mienydd671972010-10-04 16:33:58 +0200420
Derek Allard2067d1a2008-11-13 22:59:24 +0000421
422 if ( ! fputs($fp, $op, strlen($op)))
423 {
424 error_log($this->xmlrpcstr['http_error']);
425 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
426 return $r;
427 }
428 $resp = $msg->parseResponse($fp);
429 fclose($fp);
430 return $resp;
431 }
432
433} // end class XML_RPC_Client
434
435
436/**
437 * XML-RPC Response class
438 *
439 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -0500440 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
442 */
443class XML_RPC_Response
444{
445 var $val = 0;
446 var $errno = 0;
447 var $errstr = '';
448 var $headers = array();
Robin Sowellff3ecae2010-04-16 16:13:23 -0400449 var $xss_clean = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000450
Greg Akera9263282010-11-10 15:26:43 -0600451 public function __construct($val, $code = 0, $fstr = '')
Barry Mienydd671972010-10-04 16:33:58 +0200452 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000453 if ($code != 0)
454 {
455 // error
456 $this->errno = $code;
457 $this->errstr = htmlentities($fstr);
458 }
459 else if ( ! is_object($val))
460 {
461 // programmer error, not an object
Derek Jones37f4b9c2011-07-01 17:56:50 -0500462 error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to XML_RPC_Response. Defaulting to empty value.");
Derek Allard2067d1a2008-11-13 22:59:24 +0000463 $this->val = new XML_RPC_Values();
464 }
465 else
466 {
467 $this->val = $val;
468 }
469 }
470
471 function faultCode()
472 {
473 return $this->errno;
474 }
475
476 function faultString()
477 {
478 return $this->errstr;
479 }
480
481 function value()
482 {
483 return $this->val;
484 }
Barry Mienydd671972010-10-04 16:33:58 +0200485
Derek Allard2067d1a2008-11-13 22:59:24 +0000486 function prepare_response()
487 {
488 $result = "<methodResponse>\n";
489 if ($this->errno)
490 {
491 $result .= '<fault>
492 <value>
493 <struct>
494 <member>
495 <name>faultCode</name>
496 <value><int>' . $this->errno . '</int></value>
497 </member>
498 <member>
499 <name>faultString</name>
500 <value><string>' . $this->errstr . '</string></value>
501 </member>
502 </struct>
503 </value>
504</fault>';
505 }
506 else
507 {
508 $result .= "<params>\n<param>\n" .
509 $this->val->serialize_class() .
510 "</param>\n</params>";
511 }
512 $result .= "\n</methodResponse>";
513 return $result;
514 }
Barry Mienydd671972010-10-04 16:33:58 +0200515
Derek Allard2067d1a2008-11-13 22:59:24 +0000516 function decode($array=FALSE)
517 {
518 $CI =& get_instance();
Derek Jones37f4b9c2011-07-01 17:56:50 -0500519
Derek Allard2067d1a2008-11-13 22:59:24 +0000520 if ($array !== FALSE && is_array($array))
521 {
522 while (list($key) = each($array))
523 {
524 if (is_array($array[$key]))
525 {
526 $array[$key] = $this->decode($array[$key]);
527 }
528 else
529 {
Robin Sowell66a3fc02010-03-18 09:44:55 -0400530 $array[$key] = ($this->xss_clean) ? $CI->security->xss_clean($array[$key]) : $array[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000531 }
532 }
Barry Mienydd671972010-10-04 16:33:58 +0200533
Derek Allard2067d1a2008-11-13 22:59:24 +0000534 $result = $array;
535 }
536 else
537 {
538 $result = $this->xmlrpc_decoder($this->val);
Barry Mienydd671972010-10-04 16:33:58 +0200539
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 if (is_array($result))
541 {
542 $result = $this->decode($result);
543 }
544 else
545 {
Robin Sowell66a3fc02010-03-18 09:44:55 -0400546 $result = ($this->xss_clean) ? $CI->security->xss_clean($result) : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000547 }
548 }
Barry Mienydd671972010-10-04 16:33:58 +0200549
Derek Allard2067d1a2008-11-13 22:59:24 +0000550 return $result;
551 }
552
Barry Mienydd671972010-10-04 16:33:58 +0200553
554
Derek Allard2067d1a2008-11-13 22:59:24 +0000555 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500556 // XML-RPC Object to PHP Types
Derek Allard2067d1a2008-11-13 22:59:24 +0000557 //-------------------------------------
558
559 function xmlrpc_decoder($xmlrpc_val)
560 {
561 $kind = $xmlrpc_val->kindOf();
562
Pascal Kriete14287f32011-02-14 13:39:34 -0500563 if ($kind == 'scalar')
Derek Allard2067d1a2008-11-13 22:59:24 +0000564 {
565 return $xmlrpc_val->scalarval();
566 }
Pascal Kriete14287f32011-02-14 13:39:34 -0500567 elseif ($kind == 'array')
Derek Allard2067d1a2008-11-13 22:59:24 +0000568 {
569 reset($xmlrpc_val->me);
570 list($a,$b) = each($xmlrpc_val->me);
Derek Jones33559102009-02-02 18:50:38 +0000571 $size = count($b);
Barry Mienydd671972010-10-04 16:33:58 +0200572
Derek Allard2067d1a2008-11-13 22:59:24 +0000573 $arr = array();
574
Pascal Kriete14287f32011-02-14 13:39:34 -0500575 for ($i = 0; $i < $size; $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000576 {
577 $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]);
578 }
579 return $arr;
580 }
Pascal Kriete14287f32011-02-14 13:39:34 -0500581 elseif ($kind == 'struct')
Derek Allard2067d1a2008-11-13 22:59:24 +0000582 {
583 reset($xmlrpc_val->me['struct']);
584 $arr = array();
585
Pascal Kriete14287f32011-02-14 13:39:34 -0500586 while (list($key,$value) = each($xmlrpc_val->me['struct']))
Derek Allard2067d1a2008-11-13 22:59:24 +0000587 {
588 $arr[$key] = $this->xmlrpc_decoder($value);
589 }
590 return $arr;
591 }
592 }
Barry Mienydd671972010-10-04 16:33:58 +0200593
594
Derek Allard2067d1a2008-11-13 22:59:24 +0000595 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500596 // ISO-8601 time to server or UTC time
Derek Allard2067d1a2008-11-13 22:59:24 +0000597 //-------------------------------------
598
599 function iso8601_decode($time, $utc=0)
600 {
601 // return a timet in the localtime, or UTC
602 $t = 0;
603 if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $time, $regs))
604 {
Pascal Kriete14287f32011-02-14 13:39:34 -0500605 $fnc = ($utc == 1) ? 'gmmktime' : 'mktime';
606 $t = $fnc($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
Derek Allard2067d1a2008-11-13 22:59:24 +0000607 }
608 return $t;
609 }
Barry Mienydd671972010-10-04 16:33:58 +0200610
Derek Allard2067d1a2008-11-13 22:59:24 +0000611} // End Response Class
612
613
614
615/**
616 * XML-RPC Message class
617 *
618 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -0500619 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +0000620 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
621 */
622class XML_RPC_Message extends CI_Xmlrpc
623{
624 var $payload;
625 var $method_name;
626 var $params = array();
Barry Mienydd671972010-10-04 16:33:58 +0200627 var $xh = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000628
Greg Akera9263282010-11-10 15:26:43 -0600629 public function __construct($method, $pars=0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Greg Akera9263282010-11-10 15:26:43 -0600631 parent::__construct();
Barry Mienydd671972010-10-04 16:33:58 +0200632
Derek Allard2067d1a2008-11-13 22:59:24 +0000633 $this->method_name = $method;
Derek Jones33559102009-02-02 18:50:38 +0000634 if (is_array($pars) && count($pars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000635 {
Pascal Kriete14287f32011-02-14 13:39:34 -0500636 for ($i=0; $i<count($pars); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000637 {
638 // $pars[$i] = XML_RPC_Values
639 $this->params[] = $pars[$i];
640 }
641 }
642 }
Barry Mienydd671972010-10-04 16:33:58 +0200643
Derek Allard2067d1a2008-11-13 22:59:24 +0000644 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500645 // Create Payload to Send
Derek Allard2067d1a2008-11-13 22:59:24 +0000646 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200647
Derek Allard2067d1a2008-11-13 22:59:24 +0000648 function createPayload()
649 {
650 $this->payload = "<?xml version=\"1.0\"?".">\r\n<methodCall>\r\n";
651 $this->payload .= '<methodName>' . $this->method_name . "</methodName>\r\n";
652 $this->payload .= "<params>\r\n";
Barry Mienydd671972010-10-04 16:33:58 +0200653
Pascal Kriete14287f32011-02-14 13:39:34 -0500654 for ($i=0; $i<count($this->params); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000655 {
656 // $p = XML_RPC_Values
657 $p = $this->params[$i];
658 $this->payload .= "<param>\r\n".$p->serialize_class()."</param>\r\n";
659 }
Barry Mienydd671972010-10-04 16:33:58 +0200660
Derek Allard2067d1a2008-11-13 22:59:24 +0000661 $this->payload .= "</params>\r\n</methodCall>\r\n";
662 }
Barry Mienydd671972010-10-04 16:33:58 +0200663
Derek Allard2067d1a2008-11-13 22:59:24 +0000664 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500665 // Parse External XML-RPC Server's Response
Derek Allard2067d1a2008-11-13 22:59:24 +0000666 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200667
Derek Allard2067d1a2008-11-13 22:59:24 +0000668 function parseResponse($fp)
669 {
670 $data = '';
Barry Mienydd671972010-10-04 16:33:58 +0200671
Pascal Kriete14287f32011-02-14 13:39:34 -0500672 while ($datum = fread($fp, 4096))
Derek Allard2067d1a2008-11-13 22:59:24 +0000673 {
674 $data .= $datum;
675 }
Barry Mienydd671972010-10-04 16:33:58 +0200676
Derek Allard2067d1a2008-11-13 22:59:24 +0000677 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500678 // DISPLAY HTTP CONTENT for DEBUGGING
Derek Allard2067d1a2008-11-13 22:59:24 +0000679 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200680
Derek Allard2067d1a2008-11-13 22:59:24 +0000681 if ($this->debug === TRUE)
682 {
683 echo "<pre>";
684 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
685 echo "</pre>";
686 }
Barry Mienydd671972010-10-04 16:33:58 +0200687
Derek Allard2067d1a2008-11-13 22:59:24 +0000688 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500689 // Check for data
Derek Allard2067d1a2008-11-13 22:59:24 +0000690 //-------------------------------------
691
Pascal Kriete14287f32011-02-14 13:39:34 -0500692 if ($data == "")
Derek Allard2067d1a2008-11-13 22:59:24 +0000693 {
694 error_log($this->xmlrpcstr['no_data']);
695 $r = new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']);
696 return $r;
697 }
Barry Mienydd671972010-10-04 16:33:58 +0200698
699
Derek Allard2067d1a2008-11-13 22:59:24 +0000700 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500701 // Check for HTTP 200 Response
Derek Allard2067d1a2008-11-13 22:59:24 +0000702 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200703
Derek Allard2067d1a2008-11-13 22:59:24 +0000704 if (strncmp($data, 'HTTP', 4) == 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data))
705 {
706 $errstr= substr($data, 0, strpos($data, "\n")-1);
707 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']. ' (' . $errstr . ')');
708 return $r;
709 }
Barry Mienydd671972010-10-04 16:33:58 +0200710
Derek Allard2067d1a2008-11-13 22:59:24 +0000711 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500712 // Create and Set Up XML Parser
Derek Allard2067d1a2008-11-13 22:59:24 +0000713 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200714
Derek Allard2067d1a2008-11-13 22:59:24 +0000715 $parser = xml_parser_create($this->xmlrpc_defencoding);
716
Barry Mienydd671972010-10-04 16:33:58 +0200717 $this->xh[$parser] = array();
718 $this->xh[$parser]['isf'] = 0;
719 $this->xh[$parser]['ac'] = '';
720 $this->xh[$parser]['headers'] = array();
721 $this->xh[$parser]['stack'] = array();
722 $this->xh[$parser]['valuestack'] = array();
723 $this->xh[$parser]['isf_reason'] = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000724
725 xml_set_object($parser, $this);
726 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
727 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
728 xml_set_character_data_handler($parser, 'character_data');
729 //xml_set_default_handler($parser, 'default_handler');
730
731
732 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500733 // GET HEADERS
Derek Allard2067d1a2008-11-13 22:59:24 +0000734 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200735
Derek Allard2067d1a2008-11-13 22:59:24 +0000736 $lines = explode("\r\n", $data);
737 while (($line = array_shift($lines)))
738 {
739 if (strlen($line) < 1)
740 {
741 break;
742 }
743 $this->xh[$parser]['headers'][] = $line;
744 }
745 $data = implode("\r\n", $lines);
Barry Mienydd671972010-10-04 16:33:58 +0200746
747
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500749 // PARSE XML DATA
Barry Mienydd671972010-10-04 16:33:58 +0200750 //-------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000751
Derek Jones33559102009-02-02 18:50:38 +0000752 if ( ! xml_parse($parser, $data, count($data)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000753 {
754 $errstr = sprintf('XML error: %s at line %d',
755 xml_error_string(xml_get_error_code($parser)),
756 xml_get_current_line_number($parser));
757 //error_log($errstr);
758 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
759 xml_parser_free($parser);
760 return $r;
761 }
762 xml_parser_free($parser);
Barry Mienydd671972010-10-04 16:33:58 +0200763
Derek Allard2067d1a2008-11-13 22:59:24 +0000764 // ---------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500765 // Got Ourselves Some Badness, It Seems
Derek Allard2067d1a2008-11-13 22:59:24 +0000766 // ---------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200767
Derek Allard2067d1a2008-11-13 22:59:24 +0000768 if ($this->xh[$parser]['isf'] > 1)
769 {
770 if ($this->debug === TRUE)
771 {
772 echo "---Invalid Return---\n";
773 echo $this->xh[$parser]['isf_reason'];
774 echo "---Invalid Return---\n\n";
775 }
Barry Mienydd671972010-10-04 16:33:58 +0200776
Derek Allard2067d1a2008-11-13 22:59:24 +0000777 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
778 return $r;
779 }
780 elseif ( ! is_object($this->xh[$parser]['value']))
781 {
782 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
783 return $r;
784 }
Barry Mienydd671972010-10-04 16:33:58 +0200785
Derek Allard2067d1a2008-11-13 22:59:24 +0000786 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500787 // DISPLAY XML CONTENT for DEBUGGING
Barry Mienydd671972010-10-04 16:33:58 +0200788 //-------------------------------------
789
Derek Allard2067d1a2008-11-13 22:59:24 +0000790 if ($this->debug === TRUE)
791 {
792 echo "<pre>";
Barry Mienydd671972010-10-04 16:33:58 +0200793
Derek Allard2067d1a2008-11-13 22:59:24 +0000794 if (count($this->xh[$parser]['headers'] > 0))
795 {
796 echo "---HEADERS---\n";
797 foreach ($this->xh[$parser]['headers'] as $header)
798 {
799 echo "$header\n";
800 }
801 echo "---END HEADERS---\n\n";
802 }
Barry Mienydd671972010-10-04 16:33:58 +0200803
Derek Allard2067d1a2008-11-13 22:59:24 +0000804 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 echo "---PARSED---\n" ;
807 var_dump($this->xh[$parser]['value']);
808 echo "\n---END PARSED---</pre>";
809 }
Barry Mienydd671972010-10-04 16:33:58 +0200810
Derek Allard2067d1a2008-11-13 22:59:24 +0000811 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500812 // SEND RESPONSE
Derek Allard2067d1a2008-11-13 22:59:24 +0000813 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200814
Derek Allard2067d1a2008-11-13 22:59:24 +0000815 $v = $this->xh[$parser]['value'];
Barry Mienydd671972010-10-04 16:33:58 +0200816
Derek Allard2067d1a2008-11-13 22:59:24 +0000817 if ($this->xh[$parser]['isf'])
818 {
819 $errno_v = $v->me['struct']['faultCode'];
820 $errstr_v = $v->me['struct']['faultString'];
821 $errno = $errno_v->scalarval();
822
823 if ($errno == 0)
824 {
825 // FAULT returned, errno needs to reflect that
826 $errno = -1;
827 }
828
829 $r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval());
830 }
831 else
832 {
833 $r = new XML_RPC_Response($v);
834 }
835
836 $r->headers = $this->xh[$parser]['headers'];
837 return $r;
838 }
Barry Mienydd671972010-10-04 16:33:58 +0200839
Derek Allard2067d1a2008-11-13 22:59:24 +0000840 // ------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500841 // Begin Return Message Parsing section
Derek Allard2067d1a2008-11-13 22:59:24 +0000842 // ------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200843
Derek Allard2067d1a2008-11-13 22:59:24 +0000844 // quick explanation of components:
Derek Jones37f4b9c2011-07-01 17:56:50 -0500845 // ac - used to accumulate values
846 // isf - used to indicate a fault
847 // lv - used to indicate "looking for a value": implements
Derek Allard2067d1a2008-11-13 22:59:24 +0000848 // the logic to allow values with no types to be strings
Derek Jones37f4b9c2011-07-01 17:56:50 -0500849 // params - used to store parameters in method calls
850 // method - used to store method name
Derek Allard2067d1a2008-11-13 22:59:24 +0000851 // stack - array with parent tree of the xml element,
852 // used to validate the nesting of elements
853
854 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500855 // Start Element Handler
Derek Allard2067d1a2008-11-13 22:59:24 +0000856 //-------------------------------------
857
858 function open_tag($the_parser, $name, $attrs)
859 {
860 // If invalid nesting, then return
861 if ($this->xh[$the_parser]['isf'] > 1) return;
Barry Mienydd671972010-10-04 16:33:58 +0200862
Derek Allard2067d1a2008-11-13 22:59:24 +0000863 // Evaluate and check for correct nesting of XML elements
Barry Mienydd671972010-10-04 16:33:58 +0200864
Derek Allard2067d1a2008-11-13 22:59:24 +0000865 if (count($this->xh[$the_parser]['stack']) == 0)
866 {
867 if ($name != 'METHODRESPONSE' && $name != 'METHODCALL')
868 {
869 $this->xh[$the_parser]['isf'] = 2;
870 $this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing';
871 return;
872 }
873 }
874 else
875 {
876 // not top level element: see if parent is OK
877 if ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE))
878 {
879 $this->xh[$the_parser]['isf'] = 2;
880 $this->xh[$the_parser]['isf_reason'] = "XML-RPC element $name cannot be child of ".$this->xh[$the_parser]['stack'][0];
881 return;
882 }
883 }
Barry Mienydd671972010-10-04 16:33:58 +0200884
Derek Allard2067d1a2008-11-13 22:59:24 +0000885 switch($name)
886 {
887 case 'STRUCT':
888 case 'ARRAY':
889 // Creates array for child elements
Barry Mienydd671972010-10-04 16:33:58 +0200890
Derek Allard2067d1a2008-11-13 22:59:24 +0000891 $cur_val = array('value' => array(),
892 'type' => $name);
Barry Mienydd671972010-10-04 16:33:58 +0200893
Derek Allard2067d1a2008-11-13 22:59:24 +0000894 array_unshift($this->xh[$the_parser]['valuestack'], $cur_val);
895 break;
896 case 'METHODNAME':
897 case 'NAME':
898 $this->xh[$the_parser]['ac'] = '';
899 break;
900 case 'FAULT':
901 $this->xh[$the_parser]['isf'] = 1;
902 break;
903 case 'PARAM':
Pascal Kriete8761ef52011-02-14 13:13:52 -0500904 $this->xh[$the_parser]['value'] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000905 break;
906 case 'VALUE':
907 $this->xh[$the_parser]['vt'] = 'value';
908 $this->xh[$the_parser]['ac'] = '';
909 $this->xh[$the_parser]['lv'] = 1;
910 break;
911 case 'I4':
912 case 'INT':
913 case 'STRING':
914 case 'BOOLEAN':
915 case 'DOUBLE':
916 case 'DATETIME.ISO8601':
917 case 'BASE64':
918 if ($this->xh[$the_parser]['vt'] != 'value')
919 {
920 //two data elements inside a value: an error occurred!
921 $this->xh[$the_parser]['isf'] = 2;
922 $this->xh[$the_parser]['isf_reason'] = "'Twas a $name element following a ".$this->xh[$the_parser]['vt']." element inside a single value";
923 return;
924 }
Barry Mienydd671972010-10-04 16:33:58 +0200925
Derek Allard2067d1a2008-11-13 22:59:24 +0000926 $this->xh[$the_parser]['ac'] = '';
927 break;
928 case 'MEMBER':
929 // Set name of <member> to nothing to prevent errors later if no <name> is found
930 $this->xh[$the_parser]['valuestack'][0]['name'] = '';
Barry Mienydd671972010-10-04 16:33:58 +0200931
Derek Allard2067d1a2008-11-13 22:59:24 +0000932 // Set NULL value to check to see if value passed for this param/member
Pascal Kriete8761ef52011-02-14 13:13:52 -0500933 $this->xh[$the_parser]['value'] = NULL;
Derek Allard2067d1a2008-11-13 22:59:24 +0000934 break;
935 case 'DATA':
936 case 'METHODCALL':
937 case 'METHODRESPONSE':
938 case 'PARAMS':
939 // valid elements that add little to processing
940 break;
941 default:
942 /// An Invalid Element is Found, so we have trouble
943 $this->xh[$the_parser]['isf'] = 2;
944 $this->xh[$the_parser]['isf_reason'] = "Invalid XML-RPC element found: $name";
945 break;
946 }
Barry Mienydd671972010-10-04 16:33:58 +0200947
Derek Allard2067d1a2008-11-13 22:59:24 +0000948 // Add current element name to stack, to allow validation of nesting
949 array_unshift($this->xh[$the_parser]['stack'], $name);
950
951 if ($name != 'VALUE') $this->xh[$the_parser]['lv'] = 0;
952 }
953 // END
954
955
956 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -0500957 // End Element Handler
Derek Allard2067d1a2008-11-13 22:59:24 +0000958 //-------------------------------------
959
960 function closing_tag($the_parser, $name)
961 {
962 if ($this->xh[$the_parser]['isf'] > 1) return;
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 // Remove current element from stack and set variable
965 // NOTE: If the XML validates, then we do not have to worry about
Derek Jones37f4b9c2011-07-01 17:56:50 -0500966 // the opening and closing of elements. Nesting is checked on the opening
Derek Allard2067d1a2008-11-13 22:59:24 +0000967 // tag so we be safe there as well.
Barry Mienydd671972010-10-04 16:33:58 +0200968
Derek Allard2067d1a2008-11-13 22:59:24 +0000969 $curr_elem = array_shift($this->xh[$the_parser]['stack']);
Barry Mienydd671972010-10-04 16:33:58 +0200970
Derek Allard2067d1a2008-11-13 22:59:24 +0000971 switch($name)
972 {
973 case 'STRUCT':
974 case 'ARRAY':
975 $cur_val = array_shift($this->xh[$the_parser]['valuestack']);
976 $this->xh[$the_parser]['value'] = ( ! isset($cur_val['values'])) ? array() : $cur_val['values'];
977 $this->xh[$the_parser]['vt'] = strtolower($name);
978 break;
979 case 'NAME':
980 $this->xh[$the_parser]['valuestack'][0]['name'] = $this->xh[$the_parser]['ac'];
981 break;
982 case 'BOOLEAN':
983 case 'I4':
984 case 'INT':
985 case 'STRING':
986 case 'DOUBLE':
987 case 'DATETIME.ISO8601':
988 case 'BASE64':
989 $this->xh[$the_parser]['vt'] = strtolower($name);
Barry Mienydd671972010-10-04 16:33:58 +0200990
Derek Allard2067d1a2008-11-13 22:59:24 +0000991 if ($name == 'STRING')
992 {
993 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
994 }
995 elseif ($name=='DATETIME.ISO8601')
996 {
997 $this->xh[$the_parser]['vt'] = $this->xmlrpcDateTime;
998 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
999 }
1000 elseif ($name=='BASE64')
1001 {
1002 $this->xh[$the_parser]['value'] = base64_decode($this->xh[$the_parser]['ac']);
1003 }
1004 elseif ($name=='BOOLEAN')
1005 {
1006 // Translated BOOLEAN values to TRUE AND FALSE
1007 if ($this->xh[$the_parser]['ac'] == '1')
1008 {
1009 $this->xh[$the_parser]['value'] = TRUE;
1010 }
1011 else
1012 {
1013 $this->xh[$the_parser]['value'] = FALSE;
1014 }
1015 }
1016 elseif ($name=='DOUBLE')
1017 {
1018 // we have a DOUBLE
1019 // we must check that only 0123456789-.<space> are characters here
1020 if ( ! preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh[$the_parser]['ac']))
1021 {
1022 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1023 }
1024 else
1025 {
1026 $this->xh[$the_parser]['value'] = (double)$this->xh[$the_parser]['ac'];
1027 }
1028 }
1029 else
1030 {
1031 // we have an I4/INT
1032 // we must check that only 0123456789-<space> are characters here
1033 if ( ! preg_match('/^[+-]?[0-9\t ]+$/', $this->xh[$the_parser]['ac']))
1034 {
1035 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1036 }
1037 else
1038 {
1039 $this->xh[$the_parser]['value'] = (int)$this->xh[$the_parser]['ac'];
1040 }
1041 }
1042 $this->xh[$the_parser]['ac'] = '';
1043 $this->xh[$the_parser]['lv'] = 3; // indicate we've found a value
1044 break;
1045 case 'VALUE':
1046 // This if() detects if no scalar was inside <VALUE></VALUE>
1047 if ($this->xh[$the_parser]['vt']=='value')
1048 {
1049 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1050 $this->xh[$the_parser]['vt'] = $this->xmlrpcString;
1051 }
Barry Mienydd671972010-10-04 16:33:58 +02001052
Derek Allard2067d1a2008-11-13 22:59:24 +00001053 // build the XML-RPC value out of the data received, and substitute it
1054 $temp = new XML_RPC_Values($this->xh[$the_parser]['value'], $this->xh[$the_parser]['vt']);
Barry Mienydd671972010-10-04 16:33:58 +02001055
Derek Allard2067d1a2008-11-13 22:59:24 +00001056 if (count($this->xh[$the_parser]['valuestack']) && $this->xh[$the_parser]['valuestack'][0]['type'] == 'ARRAY')
1057 {
1058 // Array
1059 $this->xh[$the_parser]['valuestack'][0]['values'][] = $temp;
1060 }
1061 else
1062 {
1063 // Struct
1064 $this->xh[$the_parser]['value'] = $temp;
1065 }
1066 break;
1067 case 'MEMBER':
1068 $this->xh[$the_parser]['ac']='';
Barry Mienydd671972010-10-04 16:33:58 +02001069
Derek Allard2067d1a2008-11-13 22:59:24 +00001070 // If value add to array in the stack for the last element built
1071 if ($this->xh[$the_parser]['value'])
1072 {
1073 $this->xh[$the_parser]['valuestack'][0]['values'][$this->xh[$the_parser]['valuestack'][0]['name']] = $this->xh[$the_parser]['value'];
1074 }
1075 break;
1076 case 'DATA':
1077 $this->xh[$the_parser]['ac']='';
1078 break;
1079 case 'PARAM':
1080 if ($this->xh[$the_parser]['value'])
1081 {
1082 $this->xh[$the_parser]['params'][] = $this->xh[$the_parser]['value'];
1083 }
1084 break;
1085 case 'METHODNAME':
1086 $this->xh[$the_parser]['method'] = ltrim($this->xh[$the_parser]['ac']);
1087 break;
1088 case 'PARAMS':
1089 case 'FAULT':
1090 case 'METHODCALL':
1091 case 'METHORESPONSE':
1092 // We're all good kids with nuthin' to do
1093 break;
1094 default:
Derek Jones37f4b9c2011-07-01 17:56:50 -05001095 // End of an Invalid Element. Taken care of during the opening tag though
Derek Allard2067d1a2008-11-13 22:59:24 +00001096 break;
1097 }
1098 }
1099
1100 //-------------------------------------
Derek Jones37f4b9c2011-07-01 17:56:50 -05001101 // Parses Character Data
Derek Allard2067d1a2008-11-13 22:59:24 +00001102 //-------------------------------------
1103
1104 function character_data($the_parser, $data)
1105 {
1106 if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already
Barry Mienydd671972010-10-04 16:33:58 +02001107
Derek Allard2067d1a2008-11-13 22:59:24 +00001108 // If a value has not been found
1109 if ($this->xh[$the_parser]['lv'] != 3)
1110 {
1111 if ($this->xh[$the_parser]['lv'] == 1)
1112 {
1113 $this->xh[$the_parser]['lv'] = 2; // Found a value
1114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Pascal Kriete14287f32011-02-14 13:39:34 -05001116 if ( ! @isset($this->xh[$the_parser]['ac']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001117 {
1118 $this->xh[$the_parser]['ac'] = '';
1119 }
Barry Mienydd671972010-10-04 16:33:58 +02001120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 $this->xh[$the_parser]['ac'] .= $data;
1122 }
1123 }
Barry Mienydd671972010-10-04 16:33:58 +02001124
1125
Derek Allard2067d1a2008-11-13 22:59:24 +00001126 function addParam($par) { $this->params[]=$par; }
Barry Mienydd671972010-10-04 16:33:58 +02001127
Derek Allard2067d1a2008-11-13 22:59:24 +00001128 function output_parameters($array=FALSE)
1129 {
Barry Mienydd671972010-10-04 16:33:58 +02001130 $CI =& get_instance();
Derek Jones37f4b9c2011-07-01 17:56:50 -05001131
Derek Allard2067d1a2008-11-13 22:59:24 +00001132 if ($array !== FALSE && is_array($array))
1133 {
1134 while (list($key) = each($array))
1135 {
1136 if (is_array($array[$key]))
1137 {
1138 $array[$key] = $this->output_parameters($array[$key]);
1139 }
1140 else
1141 {
Derek Jonesa9647e82010-03-02 22:59:07 -06001142 // 'bits' is for the MetaWeblog API image bits
1143 // @todo - this needs to be made more general purpose
Robin Sowell66a3fc02010-03-18 09:44:55 -04001144 $array[$key] = ($key == 'bits' OR $this->xss_clean == FALSE) ? $array[$key] : $CI->security->xss_clean($array[$key]);
Derek Allard2067d1a2008-11-13 22:59:24 +00001145 }
1146 }
Barry Mienydd671972010-10-04 16:33:58 +02001147
Derek Allard2067d1a2008-11-13 22:59:24 +00001148 $parameters = $array;
1149 }
1150 else
1151 {
1152 $parameters = array();
Barry Mienydd671972010-10-04 16:33:58 +02001153
Derek Jones33559102009-02-02 18:50:38 +00001154 for ($i = 0; $i < count($this->params); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001155 {
1156 $a_param = $this->decode_message($this->params[$i]);
Barry Mienydd671972010-10-04 16:33:58 +02001157
Derek Allard2067d1a2008-11-13 22:59:24 +00001158 if (is_array($a_param))
1159 {
1160 $parameters[] = $this->output_parameters($a_param);
1161 }
1162 else
1163 {
Robin Sowell66a3fc02010-03-18 09:44:55 -04001164 $parameters[] = ($this->xss_clean) ? $CI->security->xss_clean($a_param) : $a_param;
Derek Allard2067d1a2008-11-13 22:59:24 +00001165 }
Barry Mienydd671972010-10-04 16:33:58 +02001166 }
Derek Allard2067d1a2008-11-13 22:59:24 +00001167 }
Barry Mienydd671972010-10-04 16:33:58 +02001168
Derek Allard2067d1a2008-11-13 22:59:24 +00001169 return $parameters;
1170 }
Barry Mienydd671972010-10-04 16:33:58 +02001171
1172
Derek Allard2067d1a2008-11-13 22:59:24 +00001173 function decode_message($param)
1174 {
1175 $kind = $param->kindOf();
1176
Pascal Kriete14287f32011-02-14 13:39:34 -05001177 if ($kind == 'scalar')
Derek Allard2067d1a2008-11-13 22:59:24 +00001178 {
1179 return $param->scalarval();
1180 }
Pascal Kriete14287f32011-02-14 13:39:34 -05001181 elseif ($kind == 'array')
Derek Allard2067d1a2008-11-13 22:59:24 +00001182 {
1183 reset($param->me);
1184 list($a,$b) = each($param->me);
Barry Mienydd671972010-10-04 16:33:58 +02001185
Derek Allard2067d1a2008-11-13 22:59:24 +00001186 $arr = array();
1187
Derek Jones33559102009-02-02 18:50:38 +00001188 for($i = 0; $i < count($b); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001189 {
1190 $arr[] = $this->decode_message($param->me['array'][$i]);
1191 }
Barry Mienydd671972010-10-04 16:33:58 +02001192
Derek Allard2067d1a2008-11-13 22:59:24 +00001193 return $arr;
1194 }
Pascal Kriete14287f32011-02-14 13:39:34 -05001195 elseif ($kind == 'struct')
Derek Allard2067d1a2008-11-13 22:59:24 +00001196 {
1197 reset($param->me['struct']);
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 $arr = array();
1200
Pascal Kriete14287f32011-02-14 13:39:34 -05001201 while (list($key,$value) = each($param->me['struct']))
Derek Allard2067d1a2008-11-13 22:59:24 +00001202 {
1203 $arr[$key] = $this->decode_message($value);
1204 }
Barry Mienydd671972010-10-04 16:33:58 +02001205
Derek Allard2067d1a2008-11-13 22:59:24 +00001206 return $arr;
1207 }
1208 }
Barry Mienydd671972010-10-04 16:33:58 +02001209
Derek Allard2067d1a2008-11-13 22:59:24 +00001210} // End XML_RPC_Messages class
1211
1212
1213
1214/**
1215 * XML-RPC Values class
1216 *
1217 * @category XML-RPC
Derek Jonesf4a4bd82011-10-20 12:18:42 -05001218 * @author EllisLab Dev Team
Derek Allard2067d1a2008-11-13 22:59:24 +00001219 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
1220 */
1221class XML_RPC_Values extends CI_Xmlrpc
1222{
Barry Mienydd671972010-10-04 16:33:58 +02001223 var $me = array();
Derek Allard2067d1a2008-11-13 22:59:24 +00001224 var $mytype = 0;
1225
Greg Akera9263282010-11-10 15:26:43 -06001226 public function __construct($val=-1, $type='')
Barry Mienydd671972010-10-04 16:33:58 +02001227 {
Greg Akera9263282010-11-10 15:26:43 -06001228 parent::__construct();
Barry Mienydd671972010-10-04 16:33:58 +02001229
Derek Allard2067d1a2008-11-13 22:59:24 +00001230 if ($val != -1 OR $type != '')
1231 {
1232 $type = $type == '' ? 'string' : $type;
Barry Mienydd671972010-10-04 16:33:58 +02001233
Derek Allard2067d1a2008-11-13 22:59:24 +00001234 if ($this->xmlrpcTypes[$type] == 1)
1235 {
1236 $this->addScalar($val,$type);
1237 }
1238 elseif ($this->xmlrpcTypes[$type] == 2)
1239 {
1240 $this->addArray($val);
1241 }
1242 elseif ($this->xmlrpcTypes[$type] == 3)
1243 {
1244 $this->addStruct($val);
1245 }
1246 }
1247 }
1248
1249 function addScalar($val, $type='string')
1250 {
1251 $typeof = $this->xmlrpcTypes[$type];
Barry Mienydd671972010-10-04 16:33:58 +02001252
Derek Allard2067d1a2008-11-13 22:59:24 +00001253 if ($this->mytype==1)
1254 {
1255 echo '<strong>XML_RPC_Values</strong>: scalar can have only one value<br />';
1256 return 0;
1257 }
Barry Mienydd671972010-10-04 16:33:58 +02001258
Derek Allard2067d1a2008-11-13 22:59:24 +00001259 if ($typeof != 1)
1260 {
1261 echo '<strong>XML_RPC_Values</strong>: not a scalar type (${typeof})<br />';
1262 return 0;
1263 }
1264
1265 if ($type == $this->xmlrpcBoolean)
1266 {
1267 if (strcasecmp($val,'true')==0 OR $val==1 OR ($val==true && strcasecmp($val,'false')))
1268 {
1269 $val = 1;
1270 }
1271 else
1272 {
1273 $val=0;
1274 }
1275 }
1276
1277 if ($this->mytype == 2)
1278 {
1279 // adding to an array here
1280 $ar = $this->me['array'];
1281 $ar[] = new XML_RPC_Values($val, $type);
1282 $this->me['array'] = $ar;
1283 }
1284 else
1285 {
1286 // a scalar, so set the value and remember we're scalar
1287 $this->me[$type] = $val;
1288 $this->mytype = $typeof;
1289 }
1290 return 1;
1291 }
1292
1293 function addArray($vals)
1294 {
1295 if ($this->mytype != 0)
1296 {
1297 echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
1298 return 0;
1299 }
1300
1301 $this->mytype = $this->xmlrpcTypes['array'];
1302 $this->me['array'] = $vals;
1303 return 1;
1304 }
1305
1306 function addStruct($vals)
1307 {
1308 if ($this->mytype != 0)
1309 {
1310 echo '<strong>XML_RPC_Values</strong>: already initialized as a [' . $this->kindOf() . ']<br />';
1311 return 0;
1312 }
1313 $this->mytype = $this->xmlrpcTypes['struct'];
1314 $this->me['struct'] = $vals;
1315 return 1;
1316 }
1317
1318 function kindOf()
1319 {
1320 switch($this->mytype)
1321 {
1322 case 3:
1323 return 'struct';
1324 break;
1325 case 2:
1326 return 'array';
1327 break;
1328 case 1:
1329 return 'scalar';
1330 break;
1331 default:
1332 return 'undef';
1333 }
1334 }
1335
1336 function serializedata($typ, $val)
1337 {
1338 $rs = '';
Derek Jonesa9647e82010-03-02 22:59:07 -06001339
Derek Allard2067d1a2008-11-13 22:59:24 +00001340 switch($this->xmlrpcTypes[$typ])
1341 {
1342 case 3:
1343 // struct
1344 $rs .= "<struct>\n";
1345 reset($val);
Pascal Kriete14287f32011-02-14 13:39:34 -05001346 while (list($key2, $val2) = each($val))
Derek Allard2067d1a2008-11-13 22:59:24 +00001347 {
1348 $rs .= "<member>\n<name>{$key2}</name>\n";
1349 $rs .= $this->serializeval($val2);
1350 $rs .= "</member>\n";
1351 }
1352 $rs .= '</struct>';
1353 break;
1354 case 2:
1355 // array
1356 $rs .= "<array>\n<data>\n";
Derek Jones33559102009-02-02 18:50:38 +00001357 for($i=0; $i < count($val); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +00001358 {
1359 $rs .= $this->serializeval($val[$i]);
1360 }
1361 $rs.="</data>\n</array>\n";
1362 break;
1363 case 1:
1364 // others
1365 switch ($typ)
1366 {
1367 case $this->xmlrpcBase64:
Derek Jonesb8d3c3d2009-06-24 15:27:01 +00001368 $rs .= "<{$typ}>" . base64_encode((string)$val) . "</{$typ}>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +00001369 break;
1370 case $this->xmlrpcBoolean:
Derek Jonesb8d3c3d2009-06-24 15:27:01 +00001371 $rs .= "<{$typ}>" . ((bool)$val ? '1' : '0') . "</{$typ}>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +00001372 break;
1373 case $this->xmlrpcString:
Derek Jonesb8d3c3d2009-06-24 15:27:01 +00001374 $rs .= "<{$typ}>" . htmlspecialchars((string)$val). "</{$typ}>\n";
Derek Allard2067d1a2008-11-13 22:59:24 +00001375 break;
1376 default:
1377 $rs .= "<{$typ}>{$val}</{$typ}>\n";
1378 break;
1379 }
1380 default:
1381 break;
1382 }
1383 return $rs;
1384 }
1385
1386 function serialize_class()
1387 {
1388 return $this->serializeval($this);
1389 }
1390
1391 function serializeval($o)
1392 {
1393 $ar = $o->me;
1394 reset($ar);
Barry Mienydd671972010-10-04 16:33:58 +02001395
Derek Allard2067d1a2008-11-13 22:59:24 +00001396 list($typ, $val) = each($ar);
1397 $rs = "<value>\n".$this->serializedata($typ, $val)."</value>\n";
1398 return $rs;
1399 }
Barry Mienydd671972010-10-04 16:33:58 +02001400
Derek Allard2067d1a2008-11-13 22:59:24 +00001401 function scalarval()
1402 {
1403 reset($this->me);
1404 list($a,$b) = each($this->me);
1405 return $b;
1406 }
1407
1408
1409 //-------------------------------------
1410 // Encode time in ISO-8601 form.
1411 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +02001412
Derek Allard2067d1a2008-11-13 22:59:24 +00001413 // Useful for sending time in XML-RPC
1414
1415 function iso8601_encode($time, $utc=0)
Barry Mienydd671972010-10-04 16:33:58 +02001416 {
Derek Allard2067d1a2008-11-13 22:59:24 +00001417 if ($utc == 1)
1418 {
Syahril Zulkeflib1cbd712011-11-13 23:46:58 +08001419 $t = strftime("%Y%m%dT%H:%i:%s", $time);
Derek Allard2067d1a2008-11-13 22:59:24 +00001420 }
1421 else
1422 {
1423 if (function_exists('gmstrftime'))
Syahril Zulkeflib1cbd712011-11-13 23:46:58 +08001424 $t = gmstrftime("%Y%m%dT%H:%i:%s", $time);
Derek Allard2067d1a2008-11-13 22:59:24 +00001425 else
Syahril Zulkeflib1cbd712011-11-13 23:46:58 +08001426 $t = strftime("%Y%m%dT%H:%i:%s", $time - date('Z'));
Derek Allard2067d1a2008-11-13 22:59:24 +00001427 }
1428 return $t;
1429 }
Barry Mienydd671972010-10-04 16:33:58 +02001430
Derek Allard2067d1a2008-11-13 22:59:24 +00001431}
1432// END XML_RPC_Values Class
1433
1434/* End of file Xmlrpc.php */
Derek Jonesa3ffbbb2008-05-11 18:18:29 +00001435/* Location: ./system/libraries/Xmlrpc.php */