blob: 3ae5bad453e5278a7c26813e892269788fc6882d [file] [log] [blame]
Derek Allard2067d1a2008-11-13 22:59:24 +00001<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2/**
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 *
7 * @package CodeIgniter
8 * @author ExpressionEngine Dev Team
Derek Jones7f3719f2010-01-05 13:35:37 +00009 * @copyright Copyright (c) 2008 - 2010, EllisLab, Inc.
Derek Allard2067d1a2008-11-13 22:59:24 +000010 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
15
16if ( ! function_exists('xml_parser_create'))
Barry Mienydd671972010-10-04 16:33:58 +020017{
Derek Allard2067d1a2008-11-13 22:59:24 +000018 show_error('Your PHP installation does not support XML');
19}
20
21
22// ------------------------------------------------------------------------
23
24/**
25 * XML-RPC request handler class
26 *
27 * @package CodeIgniter
28 * @subpackage Libraries
29 * @category XML-RPC
30 * @author ExpressionEngine Dev Team
31 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
32 */
33class CI_Xmlrpc {
34
Barry Mienydd671972010-10-04 16:33:58 +020035 var $debug = FALSE; // Debugging on or off
Derek Allard2067d1a2008-11-13 22:59:24 +000036 var $xmlrpcI4 = 'i4';
37 var $xmlrpcInt = 'int';
38 var $xmlrpcBoolean = 'boolean';
Barry Mienydd671972010-10-04 16:33:58 +020039 var $xmlrpcDouble = 'double';
Derek Allard2067d1a2008-11-13 22:59:24 +000040 var $xmlrpcString = 'string';
Derek Jones7e39c0c2009-06-24 16:25:03 +000041 var $xmlrpcDateTime = 'dateTime.iso8601';
Derek Allard2067d1a2008-11-13 22:59:24 +000042 var $xmlrpcBase64 = 'base64';
43 var $xmlrpcArray = 'array';
44 var $xmlrpcStruct = 'struct';
Barry Mienydd671972010-10-04 16:33:58 +020045
Derek Allard2067d1a2008-11-13 22:59:24 +000046 var $xmlrpcTypes = array();
47 var $valid_parents = array();
48 var $xmlrpcerr = array(); // Response numbers
49 var $xmlrpcstr = array(); // Response strings
Barry Mienydd671972010-10-04 16:33:58 +020050
Derek Allard2067d1a2008-11-13 22:59:24 +000051 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
Barry Mienydd671972010-10-04 16:33:58 +020057
Derek Allard2067d1a2008-11-13 22:59:24 +000058 var $client;
59 var $method;
60 var $data;
61 var $message = '';
Barry Mienydd671972010-10-04 16:33:58 +020062 var $error = ''; // Error string for request
Derek Allard2067d1a2008-11-13 22:59:24 +000063 var $result;
64 var $response = array(); // Response from remote server
65
Robin Sowell66a3fc02010-03-18 09:44:55 -040066 var $xss_clean = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +000067
68 //-------------------------------------
69 // VALUES THAT MULTIPLE CLASSES NEED
70 //-------------------------------------
71
Greg Akera9263282010-11-10 15:26:43 -060072 public function __construct($config = array())
Derek Allard2067d1a2008-11-13 22:59:24 +000073 {
Barry Mienydd671972010-10-04 16:33:58 +020074 $this->xmlrpcName = $this->xmlrpcName;
Derek Allard2067d1a2008-11-13 22:59:24 +000075 $this->xmlrpc_backslash = chr(92).chr(92);
Barry Mienydd671972010-10-04 16:33:58 +020076
Derek Allard2067d1a2008-11-13 22:59:24 +000077 // Types for info sent back and forth
78 $this->xmlrpcTypes = array(
Barry Mienydd671972010-10-04 16:33:58 +020079 $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'
Derek Allard2067d1a2008-11-13 22:59:24 +000088 );
Barry Mienydd671972010-10-04 16:33:58 +020089
Derek Allard2067d1a2008-11-13 22:59:24 +000090 // 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 );
Barry Mienydd671972010-10-04 16:33:58 +0200109
110
Derek Allard2067d1a2008-11-13 22:59:24 +0000111 // 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';
Greg Aker3fff87d2010-04-07 14:23:01 -0500115 $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 +0000116 $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.';
Barry Mienydd671972010-10-04 16:33:58 +0200124
Derek Allard2067d1a2008-11-13 22:59:24 +0000125 $this->initialize($config);
Barry Mienydd671972010-10-04 16:33:58 +0200126
Derek Allard2067d1a2008-11-13 22:59:24 +0000127 log_message('debug', "XML-RPC Class Initialized");
128 }
Barry Mienydd671972010-10-04 16:33:58 +0200129
130
Derek Allard2067d1a2008-11-13 22:59:24 +0000131 //-------------------------------------
132 // Initialize Prefs
133 //-------------------------------------
134
135 function initialize($config = array())
136 {
Derek Jones33559102009-02-02 18:50:38 +0000137 if (count($config) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000138 {
139 foreach ($config as $key => $val)
140 {
141 if (isset($this->$key))
142 {
Barry Mienydd671972010-10-04 16:33:58 +0200143 $this->$key = $val;
Derek Allard2067d1a2008-11-13 22:59:24 +0000144 }
145 }
146 }
147 }
148 // END
Barry Mienydd671972010-10-04 16:33:58 +0200149
Derek Allard2067d1a2008-11-13 22:59:24 +0000150 //-------------------------------------
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 }
Barry Mienydd671972010-10-04 16:33:58 +0200160
Derek Allard2067d1a2008-11-13 22:59:24 +0000161 $parts = parse_url($url);
Barry Mienydd671972010-10-04 16:33:58 +0200162
Derek Allard2067d1a2008-11-13 22:59:24 +0000163 $path = ( ! isset($parts['path'])) ? '/' : $parts['path'];
Barry Mienydd671972010-10-04 16:33:58 +0200164
Derek Allard2067d1a2008-11-13 22:59:24 +0000165 if (isset($parts['query']) && $parts['query'] != '')
166 {
167 $path .= '?'.$parts['query'];
Barry Mienydd671972010-10-04 16:33:58 +0200168 }
169
Derek Allard2067d1a2008-11-13 22:59:24 +0000170 $this->client = new XML_RPC_Client($path, $parts['host'], $port);
171 }
172 // END
Barry Mienydd671972010-10-04 16:33:58 +0200173
Derek Allard2067d1a2008-11-13 22:59:24 +0000174 //-------------------------------------
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
Barry Mienydd671972010-10-04 16:33:58 +0200186
Derek Allard2067d1a2008-11-13 22:59:24 +0000187 //-------------------------------------
188 // Set Methods
189 //-------------------------------------
190
191 function method($function)
192 {
193 $this->method = $function;
194 }
195 // END
Barry Mienydd671972010-10-04 16:33:58 +0200196
Derek Allard2067d1a2008-11-13 22:59:24 +0000197 //-------------------------------------
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 }
Barry Mienydd671972010-10-04 16:33:58 +0200207
Derek Allard2067d1a2008-11-13 22:59:24 +0000208 $this->data = array();
Barry Mienydd671972010-10-04 16:33:58 +0200209
Derek Allard2067d1a2008-11-13 22:59:24 +0000210 foreach($incoming as $key => $value)
211 {
212 $this->data[$key] = $this->values_parsing($value);
213 }
214 }
215 // END
Barry Mienydd671972010-10-04 16:33:58 +0200216
217
Derek Allard2067d1a2008-11-13 22:59:24 +0000218 //-------------------------------------
219 // Set Debug
220 //-------------------------------------
221
222 function set_debug($flag = TRUE)
223 {
224 $this->debug = ($flag == TRUE) ? TRUE : FALSE;
225 }
Barry Mienydd671972010-10-04 16:33:58 +0200226
Derek Allard2067d1a2008-11-13 22:59:24 +0000227 //-------------------------------------
228 // Values Parsing
229 //-------------------------------------
230
231 function values_parsing($value, $return = FALSE)
232 {
Derek Jones8c4b5e72010-01-06 22:21:41 +0000233 if (is_array($value) && array_key_exists(0, $value))
Derek Allard2067d1a2008-11-13 22:59:24 +0000234 {
Derek Jones626d71d2009-06-24 16:30:41 +0000235 if ( ! isset($value['1']) OR (! isset($this->xmlrpcTypes[$value['1']])))
Derek Allard2067d1a2008-11-13 22:59:24 +0000236 {
237 if (is_array($value[0]))
238 {
239 $temp = new XML_RPC_Values($value['0'], 'array');
240 }
241 else
242 {
243 $temp = new XML_RPC_Values($value['0'], 'string');
244 }
245 }
246 elseif(is_array($value['0']) && ($value['1'] == 'struct' OR $value['1'] == 'array'))
247 {
248 while (list($k) = each($value['0']))
249 {
250 $value['0'][$k] = $this->values_parsing($value['0'][$k], TRUE);
251 }
Barry Mienydd671972010-10-04 16:33:58 +0200252
Derek Allard2067d1a2008-11-13 22:59:24 +0000253 $temp = new XML_RPC_Values($value['0'], $value['1']);
254 }
255 else
256 {
257 $temp = new XML_RPC_Values($value['0'], $value['1']);
258 }
259 }
260 else
261 {
262 $temp = new XML_RPC_Values($value, 'string');
263 }
264
265 return $temp;
266 }
267 // END
268
269
270 //-------------------------------------
271 // Sends XML-RPC Request
272 //-------------------------------------
273
274 function send_request()
275 {
276 $this->message = new XML_RPC_Message($this->method,$this->data);
277 $this->message->debug = $this->debug;
Barry Mienydd671972010-10-04 16:33:58 +0200278
Derek Allard2067d1a2008-11-13 22:59:24 +0000279 if ( ! $this->result = $this->client->send($this->message))
280 {
281 $this->error = $this->result->errstr;
282 return FALSE;
283 }
284 elseif( ! is_object($this->result->val))
285 {
286 $this->error = $this->result->errstr;
287 return FALSE;
288 }
Barry Mienydd671972010-10-04 16:33:58 +0200289
Derek Allard2067d1a2008-11-13 22:59:24 +0000290 $this->response = $this->result->decode();
Barry Mienydd671972010-10-04 16:33:58 +0200291
Derek Allard2067d1a2008-11-13 22:59:24 +0000292 return TRUE;
293 }
294 // END
Barry Mienydd671972010-10-04 16:33:58 +0200295
Derek Allard2067d1a2008-11-13 22:59:24 +0000296 //-------------------------------------
297 // Returns Error
298 //-------------------------------------
299
300 function display_error()
301 {
302 return $this->error;
303 }
304 // END
Barry Mienydd671972010-10-04 16:33:58 +0200305
Derek Allard2067d1a2008-11-13 22:59:24 +0000306 //-------------------------------------
307 // Returns Remote Server Response
308 //-------------------------------------
309
310 function display_response()
311 {
312 return $this->response;
313 }
314 // END
Barry Mienydd671972010-10-04 16:33:58 +0200315
Derek Allard2067d1a2008-11-13 22:59:24 +0000316 //-------------------------------------
317 // Sends an Error Message for Server Request
318 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200319
Derek Allard2067d1a2008-11-13 22:59:24 +0000320 function send_error_message($number, $message)
321 {
322 return new XML_RPC_Response('0',$number, $message);
323 }
324 // END
Barry Mienydd671972010-10-04 16:33:58 +0200325
326
Derek Allard2067d1a2008-11-13 22:59:24 +0000327 //-------------------------------------
328 // Send Response for Server Request
329 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200330
Derek Allard2067d1a2008-11-13 22:59:24 +0000331 function send_response($response)
332 {
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
Barry Mienydd671972010-10-04 16:33:58 +0200335
Derek Allard2067d1a2008-11-13 22:59:24 +0000336 $response = $this->values_parsing($response);
Barry Mienydd671972010-10-04 16:33:58 +0200337
Derek Allard2067d1a2008-11-13 22:59:24 +0000338 return new XML_RPC_Response($response);
339 }
340 // END
Barry Mienydd671972010-10-04 16:33:58 +0200341
Derek Allard2067d1a2008-11-13 22:59:24 +0000342} // END XML_RPC Class
343
Barry Mienydd671972010-10-04 16:33:58 +0200344
345
Derek Allard2067d1a2008-11-13 22:59:24 +0000346/**
347 * XML-RPC Client class
348 *
349 * @category XML-RPC
350 * @author ExpressionEngine Dev Team
351 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
352 */
353class XML_RPC_Client extends CI_Xmlrpc
354{
355 var $path = '';
356 var $server = '';
357 var $port = 80;
358 var $errno = '';
359 var $errstring = '';
360 var $timeout = 5;
361 var $no_multicall = false;
362
Greg Akera9263282010-11-10 15:26:43 -0600363 public function __construct($path, $server, $port=80)
Derek Allard2067d1a2008-11-13 22:59:24 +0000364 {
Greg Akera9263282010-11-10 15:26:43 -0600365 parent::__construct();
Barry Mienydd671972010-10-04 16:33:58 +0200366
Derek Allard2067d1a2008-11-13 22:59:24 +0000367 $this->port = $port;
368 $this->server = $server;
369 $this->path = $path;
370 }
Barry Mienydd671972010-10-04 16:33:58 +0200371
Derek Allard2067d1a2008-11-13 22:59:24 +0000372 function send($msg)
373 {
374 if (is_array($msg))
375 {
376 // Multi-call disabled
377 $r = new XML_RPC_Response(0, $this->xmlrpcerr['multicall_recursion'],$this->xmlrpcstr['multicall_recursion']);
378 return $r;
379 }
380
381 return $this->sendPayload($msg);
382 }
383
384 function sendPayload($msg)
Barry Mienydd671972010-10-04 16:33:58 +0200385 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000386 $fp = @fsockopen($this->server, $this->port,$this->errno, $this->errstr, $this->timeout);
Barry Mienydd671972010-10-04 16:33:58 +0200387
Derek Allard2067d1a2008-11-13 22:59:24 +0000388 if ( ! is_resource($fp))
389 {
390 error_log($this->xmlrpcstr['http_error']);
391 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'],$this->xmlrpcstr['http_error']);
392 return $r;
393 }
Barry Mienydd671972010-10-04 16:33:58 +0200394
Derek Allard2067d1a2008-11-13 22:59:24 +0000395 if(empty($msg->payload))
396 {
397 // $msg = XML_RPC_Messages
398 $msg->createPayload();
399 }
Barry Mienydd671972010-10-04 16:33:58 +0200400
Derek Allard2067d1a2008-11-13 22:59:24 +0000401 $r = "\r\n";
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;
Barry Mienydd671972010-10-04 16:33:58 +0200408
Derek Allard2067d1a2008-11-13 22:59:24 +0000409
410 if ( ! fputs($fp, $op, strlen($op)))
411 {
412 error_log($this->xmlrpcstr['http_error']);
413 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']);
414 return $r;
415 }
416 $resp = $msg->parseResponse($fp);
417 fclose($fp);
418 return $resp;
419 }
420
421} // end class XML_RPC_Client
422
423
424/**
425 * XML-RPC Response class
426 *
427 * @category XML-RPC
428 * @author ExpressionEngine Dev Team
429 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
430 */
431class XML_RPC_Response
432{
433 var $val = 0;
434 var $errno = 0;
435 var $errstr = '';
436 var $headers = array();
Robin Sowellff3ecae2010-04-16 16:13:23 -0400437 var $xss_clean = TRUE;
Derek Allard2067d1a2008-11-13 22:59:24 +0000438
Greg Akera9263282010-11-10 15:26:43 -0600439 public function __construct($val, $code = 0, $fstr = '')
Barry Mienydd671972010-10-04 16:33:58 +0200440 {
Derek Allard2067d1a2008-11-13 22:59:24 +0000441 if ($code != 0)
442 {
443 // error
444 $this->errno = $code;
445 $this->errstr = htmlentities($fstr);
446 }
447 else if ( ! is_object($val))
448 {
449 // programmer error, not an object
450 error_log("Invalid type '" . gettype($val) . "' (value: $val) passed to XML_RPC_Response. Defaulting to empty value.");
451 $this->val = new XML_RPC_Values();
452 }
453 else
454 {
455 $this->val = $val;
456 }
457 }
458
459 function faultCode()
460 {
461 return $this->errno;
462 }
463
464 function faultString()
465 {
466 return $this->errstr;
467 }
468
469 function value()
470 {
471 return $this->val;
472 }
Barry Mienydd671972010-10-04 16:33:58 +0200473
Derek Allard2067d1a2008-11-13 22:59:24 +0000474 function prepare_response()
475 {
476 $result = "<methodResponse>\n";
477 if ($this->errno)
478 {
479 $result .= '<fault>
480 <value>
481 <struct>
482 <member>
483 <name>faultCode</name>
484 <value><int>' . $this->errno . '</int></value>
485 </member>
486 <member>
487 <name>faultString</name>
488 <value><string>' . $this->errstr . '</string></value>
489 </member>
490 </struct>
491 </value>
492</fault>';
493 }
494 else
495 {
496 $result .= "<params>\n<param>\n" .
497 $this->val->serialize_class() .
498 "</param>\n</params>";
499 }
500 $result .= "\n</methodResponse>";
501 return $result;
502 }
Barry Mienydd671972010-10-04 16:33:58 +0200503
Derek Allard2067d1a2008-11-13 22:59:24 +0000504 function decode($array=FALSE)
505 {
506 $CI =& get_instance();
507
Derek Jones30841672010-04-26 09:09:21 -0500508 if ($this->xss_clean && ! isset($CI->security))
Derek Jones5640a712010-04-23 11:22:40 -0500509 {
Derek Jones30841672010-04-26 09:09:21 -0500510 $CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -0500511 }
512
Derek Allard2067d1a2008-11-13 22:59:24 +0000513 if ($array !== FALSE && is_array($array))
514 {
515 while (list($key) = each($array))
516 {
517 if (is_array($array[$key]))
518 {
519 $array[$key] = $this->decode($array[$key]);
520 }
521 else
522 {
Robin Sowell66a3fc02010-03-18 09:44:55 -0400523 $array[$key] = ($this->xss_clean) ? $CI->security->xss_clean($array[$key]) : $array[$key];
Derek Allard2067d1a2008-11-13 22:59:24 +0000524 }
525 }
Barry Mienydd671972010-10-04 16:33:58 +0200526
Derek Allard2067d1a2008-11-13 22:59:24 +0000527 $result = $array;
528 }
529 else
530 {
531 $result = $this->xmlrpc_decoder($this->val);
Barry Mienydd671972010-10-04 16:33:58 +0200532
Derek Allard2067d1a2008-11-13 22:59:24 +0000533 if (is_array($result))
534 {
535 $result = $this->decode($result);
536 }
537 else
538 {
Robin Sowell66a3fc02010-03-18 09:44:55 -0400539 $result = ($this->xss_clean) ? $CI->security->xss_clean($result) : $result;
Derek Allard2067d1a2008-11-13 22:59:24 +0000540 }
541 }
Barry Mienydd671972010-10-04 16:33:58 +0200542
Derek Allard2067d1a2008-11-13 22:59:24 +0000543 return $result;
544 }
545
Barry Mienydd671972010-10-04 16:33:58 +0200546
547
Derek Allard2067d1a2008-11-13 22:59:24 +0000548 //-------------------------------------
549 // XML-RPC Object to PHP Types
550 //-------------------------------------
551
552 function xmlrpc_decoder($xmlrpc_val)
553 {
554 $kind = $xmlrpc_val->kindOf();
555
556 if($kind == 'scalar')
557 {
558 return $xmlrpc_val->scalarval();
559 }
560 elseif($kind == 'array')
561 {
562 reset($xmlrpc_val->me);
563 list($a,$b) = each($xmlrpc_val->me);
Derek Jones33559102009-02-02 18:50:38 +0000564 $size = count($b);
Barry Mienydd671972010-10-04 16:33:58 +0200565
Derek Allard2067d1a2008-11-13 22:59:24 +0000566 $arr = array();
567
568 for($i = 0; $i < $size; $i++)
569 {
570 $arr[] = $this->xmlrpc_decoder($xmlrpc_val->me['array'][$i]);
571 }
572 return $arr;
573 }
574 elseif($kind == 'struct')
575 {
576 reset($xmlrpc_val->me['struct']);
577 $arr = array();
578
579 while(list($key,$value) = each($xmlrpc_val->me['struct']))
580 {
581 $arr[$key] = $this->xmlrpc_decoder($value);
582 }
583 return $arr;
584 }
585 }
Barry Mienydd671972010-10-04 16:33:58 +0200586
587
Derek Allard2067d1a2008-11-13 22:59:24 +0000588 //-------------------------------------
589 // ISO-8601 time to server or UTC time
590 //-------------------------------------
591
592 function iso8601_decode($time, $utc=0)
593 {
594 // return a timet in the localtime, or UTC
595 $t = 0;
596 if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2}):([0-9]{2}):([0-9]{2})/', $time, $regs))
597 {
598 if ($utc == 1)
599 $t = gmmktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
600 else
601 $t = mktime($regs[4], $regs[5], $regs[6], $regs[2], $regs[3], $regs[1]);
602 }
603 return $t;
604 }
Barry Mienydd671972010-10-04 16:33:58 +0200605
Derek Allard2067d1a2008-11-13 22:59:24 +0000606} // End Response Class
607
608
609
610/**
611 * XML-RPC Message class
612 *
613 * @category XML-RPC
614 * @author ExpressionEngine Dev Team
615 * @link http://codeigniter.com/user_guide/libraries/xmlrpc.html
616 */
617class XML_RPC_Message extends CI_Xmlrpc
618{
619 var $payload;
620 var $method_name;
621 var $params = array();
Barry Mienydd671972010-10-04 16:33:58 +0200622 var $xh = array();
Derek Allard2067d1a2008-11-13 22:59:24 +0000623
Greg Akera9263282010-11-10 15:26:43 -0600624 public function __construct($method, $pars=0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000625 {
Greg Akera9263282010-11-10 15:26:43 -0600626 parent::__construct();
Barry Mienydd671972010-10-04 16:33:58 +0200627
Derek Allard2067d1a2008-11-13 22:59:24 +0000628 $this->method_name = $method;
Derek Jones33559102009-02-02 18:50:38 +0000629 if (is_array($pars) && count($pars) > 0)
Derek Allard2067d1a2008-11-13 22:59:24 +0000630 {
Derek Jones33559102009-02-02 18:50:38 +0000631 for($i=0; $i<count($pars); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000632 {
633 // $pars[$i] = XML_RPC_Values
634 $this->params[] = $pars[$i];
635 }
636 }
637 }
Barry Mienydd671972010-10-04 16:33:58 +0200638
Derek Allard2067d1a2008-11-13 22:59:24 +0000639 //-------------------------------------
640 // Create Payload to Send
641 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200642
Derek Allard2067d1a2008-11-13 22:59:24 +0000643 function createPayload()
644 {
645 $this->payload = "<?xml version=\"1.0\"?".">\r\n<methodCall>\r\n";
646 $this->payload .= '<methodName>' . $this->method_name . "</methodName>\r\n";
647 $this->payload .= "<params>\r\n";
Barry Mienydd671972010-10-04 16:33:58 +0200648
Derek Jones33559102009-02-02 18:50:38 +0000649 for($i=0; $i<count($this->params); $i++)
Derek Allard2067d1a2008-11-13 22:59:24 +0000650 {
651 // $p = XML_RPC_Values
652 $p = $this->params[$i];
653 $this->payload .= "<param>\r\n".$p->serialize_class()."</param>\r\n";
654 }
Barry Mienydd671972010-10-04 16:33:58 +0200655
Derek Allard2067d1a2008-11-13 22:59:24 +0000656 $this->payload .= "</params>\r\n</methodCall>\r\n";
657 }
Barry Mienydd671972010-10-04 16:33:58 +0200658
Derek Allard2067d1a2008-11-13 22:59:24 +0000659 //-------------------------------------
660 // Parse External XML-RPC Server's Response
661 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200662
Derek Allard2067d1a2008-11-13 22:59:24 +0000663 function parseResponse($fp)
664 {
665 $data = '';
Barry Mienydd671972010-10-04 16:33:58 +0200666
Derek Allard2067d1a2008-11-13 22:59:24 +0000667 while($datum = fread($fp, 4096))
668 {
669 $data .= $datum;
670 }
Barry Mienydd671972010-10-04 16:33:58 +0200671
Derek Allard2067d1a2008-11-13 22:59:24 +0000672 //-------------------------------------
673 // DISPLAY HTTP CONTENT for DEBUGGING
674 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200675
Derek Allard2067d1a2008-11-13 22:59:24 +0000676 if ($this->debug === TRUE)
677 {
678 echo "<pre>";
679 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
680 echo "</pre>";
681 }
Barry Mienydd671972010-10-04 16:33:58 +0200682
Derek Allard2067d1a2008-11-13 22:59:24 +0000683 //-------------------------------------
684 // Check for data
685 //-------------------------------------
686
687 if($data == "")
688 {
689 error_log($this->xmlrpcstr['no_data']);
690 $r = new XML_RPC_Response(0, $this->xmlrpcerr['no_data'], $this->xmlrpcstr['no_data']);
691 return $r;
692 }
Barry Mienydd671972010-10-04 16:33:58 +0200693
694
Derek Allard2067d1a2008-11-13 22:59:24 +0000695 //-------------------------------------
696 // Check for HTTP 200 Response
697 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200698
Derek Allard2067d1a2008-11-13 22:59:24 +0000699 if (strncmp($data, 'HTTP', 4) == 0 && ! preg_match('/^HTTP\/[0-9\.]+ 200 /', $data))
700 {
701 $errstr= substr($data, 0, strpos($data, "\n")-1);
702 $r = new XML_RPC_Response(0, $this->xmlrpcerr['http_error'], $this->xmlrpcstr['http_error']. ' (' . $errstr . ')');
703 return $r;
704 }
Barry Mienydd671972010-10-04 16:33:58 +0200705
Derek Allard2067d1a2008-11-13 22:59:24 +0000706 //-------------------------------------
707 // Create and Set Up XML Parser
708 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200709
Derek Allard2067d1a2008-11-13 22:59:24 +0000710 $parser = xml_parser_create($this->xmlrpc_defencoding);
711
Barry Mienydd671972010-10-04 16:33:58 +0200712 $this->xh[$parser] = array();
713 $this->xh[$parser]['isf'] = 0;
714 $this->xh[$parser]['ac'] = '';
715 $this->xh[$parser]['headers'] = array();
716 $this->xh[$parser]['stack'] = array();
717 $this->xh[$parser]['valuestack'] = array();
718 $this->xh[$parser]['isf_reason'] = 0;
Derek Allard2067d1a2008-11-13 22:59:24 +0000719
720 xml_set_object($parser, $this);
721 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
722 xml_set_element_handler($parser, 'open_tag', 'closing_tag');
723 xml_set_character_data_handler($parser, 'character_data');
724 //xml_set_default_handler($parser, 'default_handler');
725
726
727 //-------------------------------------
728 // GET HEADERS
729 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200730
Derek Allard2067d1a2008-11-13 22:59:24 +0000731 $lines = explode("\r\n", $data);
732 while (($line = array_shift($lines)))
733 {
734 if (strlen($line) < 1)
735 {
736 break;
737 }
738 $this->xh[$parser]['headers'][] = $line;
739 }
740 $data = implode("\r\n", $lines);
Barry Mienydd671972010-10-04 16:33:58 +0200741
742
Derek Allard2067d1a2008-11-13 22:59:24 +0000743 //-------------------------------------
744 // PARSE XML DATA
Barry Mienydd671972010-10-04 16:33:58 +0200745 //-------------------------------------
Derek Allard2067d1a2008-11-13 22:59:24 +0000746
Derek Jones33559102009-02-02 18:50:38 +0000747 if ( ! xml_parse($parser, $data, count($data)))
Derek Allard2067d1a2008-11-13 22:59:24 +0000748 {
749 $errstr = sprintf('XML error: %s at line %d',
750 xml_error_string(xml_get_error_code($parser)),
751 xml_get_current_line_number($parser));
752 //error_log($errstr);
753 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'], $this->xmlrpcstr['invalid_return']);
754 xml_parser_free($parser);
755 return $r;
756 }
757 xml_parser_free($parser);
Barry Mienydd671972010-10-04 16:33:58 +0200758
Derek Allard2067d1a2008-11-13 22:59:24 +0000759 // ---------------------------------------
760 // Got Ourselves Some Badness, It Seems
761 // ---------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200762
Derek Allard2067d1a2008-11-13 22:59:24 +0000763 if ($this->xh[$parser]['isf'] > 1)
764 {
765 if ($this->debug === TRUE)
766 {
767 echo "---Invalid Return---\n";
768 echo $this->xh[$parser]['isf_reason'];
769 echo "---Invalid Return---\n\n";
770 }
Barry Mienydd671972010-10-04 16:33:58 +0200771
Derek Allard2067d1a2008-11-13 22:59:24 +0000772 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
773 return $r;
774 }
775 elseif ( ! is_object($this->xh[$parser]['value']))
776 {
777 $r = new XML_RPC_Response(0, $this->xmlrpcerr['invalid_return'],$this->xmlrpcstr['invalid_return'].' '.$this->xh[$parser]['isf_reason']);
778 return $r;
779 }
Barry Mienydd671972010-10-04 16:33:58 +0200780
Derek Allard2067d1a2008-11-13 22:59:24 +0000781 //-------------------------------------
782 // DISPLAY XML CONTENT for DEBUGGING
Barry Mienydd671972010-10-04 16:33:58 +0200783 //-------------------------------------
784
Derek Allard2067d1a2008-11-13 22:59:24 +0000785 if ($this->debug === TRUE)
786 {
787 echo "<pre>";
Barry Mienydd671972010-10-04 16:33:58 +0200788
Derek Allard2067d1a2008-11-13 22:59:24 +0000789 if (count($this->xh[$parser]['headers'] > 0))
790 {
791 echo "---HEADERS---\n";
792 foreach ($this->xh[$parser]['headers'] as $header)
793 {
794 echo "$header\n";
795 }
796 echo "---END HEADERS---\n\n";
797 }
Barry Mienydd671972010-10-04 16:33:58 +0200798
Derek Allard2067d1a2008-11-13 22:59:24 +0000799 echo "---DATA---\n" . htmlspecialchars($data) . "\n---END DATA---\n\n";
Barry Mienydd671972010-10-04 16:33:58 +0200800
Derek Allard2067d1a2008-11-13 22:59:24 +0000801 echo "---PARSED---\n" ;
802 var_dump($this->xh[$parser]['value']);
803 echo "\n---END PARSED---</pre>";
804 }
Barry Mienydd671972010-10-04 16:33:58 +0200805
Derek Allard2067d1a2008-11-13 22:59:24 +0000806 //-------------------------------------
807 // SEND RESPONSE
808 //-------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200809
Derek Allard2067d1a2008-11-13 22:59:24 +0000810 $v = $this->xh[$parser]['value'];
Barry Mienydd671972010-10-04 16:33:58 +0200811
Derek Allard2067d1a2008-11-13 22:59:24 +0000812 if ($this->xh[$parser]['isf'])
813 {
814 $errno_v = $v->me['struct']['faultCode'];
815 $errstr_v = $v->me['struct']['faultString'];
816 $errno = $errno_v->scalarval();
817
818 if ($errno == 0)
819 {
820 // FAULT returned, errno needs to reflect that
821 $errno = -1;
822 }
823
824 $r = new XML_RPC_Response($v, $errno, $errstr_v->scalarval());
825 }
826 else
827 {
828 $r = new XML_RPC_Response($v);
829 }
830
831 $r->headers = $this->xh[$parser]['headers'];
832 return $r;
833 }
Barry Mienydd671972010-10-04 16:33:58 +0200834
Derek Allard2067d1a2008-11-13 22:59:24 +0000835 // ------------------------------------
836 // Begin Return Message Parsing section
837 // ------------------------------------
Barry Mienydd671972010-10-04 16:33:58 +0200838
Derek Allard2067d1a2008-11-13 22:59:24 +0000839 // quick explanation of components:
840 // ac - used to accumulate values
841 // isf - used to indicate a fault
842 // lv - used to indicate "looking for a value": implements
843 // the logic to allow values with no types to be strings
844 // params - used to store parameters in method calls
845 // method - used to store method name
846 // stack - array with parent tree of the xml element,
847 // used to validate the nesting of elements
848
849 //-------------------------------------
850 // Start Element Handler
851 //-------------------------------------
852
853 function open_tag($the_parser, $name, $attrs)
854 {
855 // If invalid nesting, then return
856 if ($this->xh[$the_parser]['isf'] > 1) return;
Barry Mienydd671972010-10-04 16:33:58 +0200857
Derek Allard2067d1a2008-11-13 22:59:24 +0000858 // Evaluate and check for correct nesting of XML elements
Barry Mienydd671972010-10-04 16:33:58 +0200859
Derek Allard2067d1a2008-11-13 22:59:24 +0000860 if (count($this->xh[$the_parser]['stack']) == 0)
861 {
862 if ($name != 'METHODRESPONSE' && $name != 'METHODCALL')
863 {
864 $this->xh[$the_parser]['isf'] = 2;
865 $this->xh[$the_parser]['isf_reason'] = 'Top level XML-RPC element is missing';
866 return;
867 }
868 }
869 else
870 {
871 // not top level element: see if parent is OK
872 if ( ! in_array($this->xh[$the_parser]['stack'][0], $this->valid_parents[$name], TRUE))
873 {
874 $this->xh[$the_parser]['isf'] = 2;
875 $this->xh[$the_parser]['isf_reason'] = "XML-RPC element $name cannot be child of ".$this->xh[$the_parser]['stack'][0];
876 return;
877 }
878 }
Barry Mienydd671972010-10-04 16:33:58 +0200879
Derek Allard2067d1a2008-11-13 22:59:24 +0000880 switch($name)
881 {
882 case 'STRUCT':
883 case 'ARRAY':
884 // Creates array for child elements
Barry Mienydd671972010-10-04 16:33:58 +0200885
Derek Allard2067d1a2008-11-13 22:59:24 +0000886 $cur_val = array('value' => array(),
887 'type' => $name);
Barry Mienydd671972010-10-04 16:33:58 +0200888
Derek Allard2067d1a2008-11-13 22:59:24 +0000889 array_unshift($this->xh[$the_parser]['valuestack'], $cur_val);
890 break;
891 case 'METHODNAME':
892 case 'NAME':
893 $this->xh[$the_parser]['ac'] = '';
894 break;
895 case 'FAULT':
896 $this->xh[$the_parser]['isf'] = 1;
897 break;
898 case 'PARAM':
899 $this->xh[$the_parser]['value'] = null;
900 break;
901 case 'VALUE':
902 $this->xh[$the_parser]['vt'] = 'value';
903 $this->xh[$the_parser]['ac'] = '';
904 $this->xh[$the_parser]['lv'] = 1;
905 break;
906 case 'I4':
907 case 'INT':
908 case 'STRING':
909 case 'BOOLEAN':
910 case 'DOUBLE':
911 case 'DATETIME.ISO8601':
912 case 'BASE64':
913 if ($this->xh[$the_parser]['vt'] != 'value')
914 {
915 //two data elements inside a value: an error occurred!
916 $this->xh[$the_parser]['isf'] = 2;
917 $this->xh[$the_parser]['isf_reason'] = "'Twas a $name element following a ".$this->xh[$the_parser]['vt']." element inside a single value";
918 return;
919 }
Barry Mienydd671972010-10-04 16:33:58 +0200920
Derek Allard2067d1a2008-11-13 22:59:24 +0000921 $this->xh[$the_parser]['ac'] = '';
922 break;
923 case 'MEMBER':
924 // Set name of <member> to nothing to prevent errors later if no <name> is found
925 $this->xh[$the_parser]['valuestack'][0]['name'] = '';
Barry Mienydd671972010-10-04 16:33:58 +0200926
Derek Allard2067d1a2008-11-13 22:59:24 +0000927 // Set NULL value to check to see if value passed for this param/member
928 $this->xh[$the_parser]['value'] = null;
929 break;
930 case 'DATA':
931 case 'METHODCALL':
932 case 'METHODRESPONSE':
933 case 'PARAMS':
934 // valid elements that add little to processing
935 break;
936 default:
937 /// An Invalid Element is Found, so we have trouble
938 $this->xh[$the_parser]['isf'] = 2;
939 $this->xh[$the_parser]['isf_reason'] = "Invalid XML-RPC element found: $name";
940 break;
941 }
Barry Mienydd671972010-10-04 16:33:58 +0200942
Derek Allard2067d1a2008-11-13 22:59:24 +0000943 // Add current element name to stack, to allow validation of nesting
944 array_unshift($this->xh[$the_parser]['stack'], $name);
945
946 if ($name != 'VALUE') $this->xh[$the_parser]['lv'] = 0;
947 }
948 // END
949
950
951 //-------------------------------------
952 // End Element Handler
953 //-------------------------------------
954
955 function closing_tag($the_parser, $name)
956 {
957 if ($this->xh[$the_parser]['isf'] > 1) return;
Barry Mienydd671972010-10-04 16:33:58 +0200958
Derek Allard2067d1a2008-11-13 22:59:24 +0000959 // Remove current element from stack and set variable
960 // NOTE: If the XML validates, then we do not have to worry about
961 // the opening and closing of elements. Nesting is checked on the opening
962 // tag so we be safe there as well.
Barry Mienydd671972010-10-04 16:33:58 +0200963
Derek Allard2067d1a2008-11-13 22:59:24 +0000964 $curr_elem = array_shift($this->xh[$the_parser]['stack']);
Barry Mienydd671972010-10-04 16:33:58 +0200965
Derek Allard2067d1a2008-11-13 22:59:24 +0000966 switch($name)
967 {
968 case 'STRUCT':
969 case 'ARRAY':
970 $cur_val = array_shift($this->xh[$the_parser]['valuestack']);
971 $this->xh[$the_parser]['value'] = ( ! isset($cur_val['values'])) ? array() : $cur_val['values'];
972 $this->xh[$the_parser]['vt'] = strtolower($name);
973 break;
974 case 'NAME':
975 $this->xh[$the_parser]['valuestack'][0]['name'] = $this->xh[$the_parser]['ac'];
976 break;
977 case 'BOOLEAN':
978 case 'I4':
979 case 'INT':
980 case 'STRING':
981 case 'DOUBLE':
982 case 'DATETIME.ISO8601':
983 case 'BASE64':
984 $this->xh[$the_parser]['vt'] = strtolower($name);
Barry Mienydd671972010-10-04 16:33:58 +0200985
Derek Allard2067d1a2008-11-13 22:59:24 +0000986 if ($name == 'STRING')
987 {
988 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
989 }
990 elseif ($name=='DATETIME.ISO8601')
991 {
992 $this->xh[$the_parser]['vt'] = $this->xmlrpcDateTime;
993 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
994 }
995 elseif ($name=='BASE64')
996 {
997 $this->xh[$the_parser]['value'] = base64_decode($this->xh[$the_parser]['ac']);
998 }
999 elseif ($name=='BOOLEAN')
1000 {
1001 // Translated BOOLEAN values to TRUE AND FALSE
1002 if ($this->xh[$the_parser]['ac'] == '1')
1003 {
1004 $this->xh[$the_parser]['value'] = TRUE;
1005 }
1006 else
1007 {
1008 $this->xh[$the_parser]['value'] = FALSE;
1009 }
1010 }
1011 elseif ($name=='DOUBLE')
1012 {
1013 // we have a DOUBLE
1014 // we must check that only 0123456789-.<space> are characters here
1015 if ( ! preg_match('/^[+-]?[eE0-9\t \.]+$/', $this->xh[$the_parser]['ac']))
1016 {
1017 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1018 }
1019 else
1020 {
1021 $this->xh[$the_parser]['value'] = (double)$this->xh[$the_parser]['ac'];
1022 }
1023 }
1024 else
1025 {
1026 // we have an I4/INT
1027 // we must check that only 0123456789-<space> are characters here
1028 if ( ! preg_match('/^[+-]?[0-9\t ]+$/', $this->xh[$the_parser]['ac']))
1029 {
1030 $this->xh[$the_parser]['value'] = 'ERROR_NON_NUMERIC_FOUND';
1031 }
1032 else
1033 {
1034 $this->xh[$the_parser]['value'] = (int)$this->xh[$the_parser]['ac'];
1035 }
1036 }
1037 $this->xh[$the_parser]['ac'] = '';
1038 $this->xh[$the_parser]['lv'] = 3; // indicate we've found a value
1039 break;
1040 case 'VALUE':
1041 // This if() detects if no scalar was inside <VALUE></VALUE>
1042 if ($this->xh[$the_parser]['vt']=='value')
1043 {
1044 $this->xh[$the_parser]['value'] = $this->xh[$the_parser]['ac'];
1045 $this->xh[$the_parser]['vt'] = $this->xmlrpcString;
1046 }
Barry Mienydd671972010-10-04 16:33:58 +02001047
Derek Allard2067d1a2008-11-13 22:59:24 +00001048 // build the XML-RPC value out of the data received, and substitute it
1049 $temp = new XML_RPC_Values($this->xh[$the_parser]['value'], $this->xh[$the_parser]['vt']);
Barry Mienydd671972010-10-04 16:33:58 +02001050
Derek Allard2067d1a2008-11-13 22:59:24 +00001051 if (count($this->xh[$the_parser]['valuestack']) && $this->xh[$the_parser]['valuestack'][0]['type'] == 'ARRAY')
1052 {
1053 // Array
1054 $this->xh[$the_parser]['valuestack'][0]['values'][] = $temp;
1055 }
1056 else
1057 {
1058 // Struct
1059 $this->xh[$the_parser]['value'] = $temp;
1060 }
1061 break;
1062 case 'MEMBER':
1063 $this->xh[$the_parser]['ac']='';
Barry Mienydd671972010-10-04 16:33:58 +02001064
Derek Allard2067d1a2008-11-13 22:59:24 +00001065 // If value add to array in the stack for the last element built
1066 if ($this->xh[$the_parser]['value'])
1067 {
1068 $this->xh[$the_parser]['valuestack'][0]['values'][$this->xh[$the_parser]['valuestack'][0]['name']] = $this->xh[$the_parser]['value'];
1069 }
1070 break;
1071 case 'DATA':
1072 $this->xh[$the_parser]['ac']='';
1073 break;
1074 case 'PARAM':
1075 if ($this->xh[$the_parser]['value'])
1076 {
1077 $this->xh[$the_parser]['params'][] = $this->xh[$the_parser]['value'];
1078 }
1079 break;
1080 case 'METHODNAME':
1081 $this->xh[$the_parser]['method'] = ltrim($this->xh[$the_parser]['ac']);
1082 break;
1083 case 'PARAMS':
1084 case 'FAULT':
1085 case 'METHODCALL':
1086 case 'METHORESPONSE':
1087 // We're all good kids with nuthin' to do
1088 break;
1089 default:
1090 // End of an Invalid Element. Taken care of during the opening tag though
1091 break;
1092 }
1093 }
1094
1095 //-------------------------------------
1096 // Parses Character Data
1097 //-------------------------------------
1098
1099 function character_data($the_parser, $data)
1100 {
1101 if ($this->xh[$the_parser]['isf'] > 1) return; // XML Fault found already
Barry Mienydd671972010-10-04 16:33:58 +02001102
Derek Allard2067d1a2008-11-13 22:59:24 +00001103 // If a value has not been found
1104 if ($this->xh[$the_parser]['lv'] != 3)
1105 {
1106 if ($this->xh[$the_parser]['lv'] == 1)
1107 {
1108 $this->xh[$the_parser]['lv'] = 2; // Found a value
1109 }
Barry Mienydd671972010-10-04 16:33:58 +02001110
Derek Allard2067d1a2008-11-13 22:59:24 +00001111 if( ! @isset($this->xh[$the_parser]['ac']))
1112 {
1113 $this->xh[$the_parser]['ac'] = '';
1114 }
Barry Mienydd671972010-10-04 16:33:58 +02001115
Derek Allard2067d1a2008-11-13 22:59:24 +00001116 $this->xh[$the_parser]['ac'] .= $data;
1117 }
1118 }
Barry Mienydd671972010-10-04 16:33:58 +02001119
1120
Derek Allard2067d1a2008-11-13 22:59:24 +00001121 function addParam($par) { $this->params[]=$par; }
Barry Mienydd671972010-10-04 16:33:58 +02001122
Derek Allard2067d1a2008-11-13 22:59:24 +00001123 function output_parameters($array=FALSE)
1124 {
Barry Mienydd671972010-10-04 16:33:58 +02001125 $CI =& get_instance();
Derek Allard2067d1a2008-11-13 22:59:24 +00001126
Derek Jones30841672010-04-26 09:09:21 -05001127 if ($this->xss_clean && ! isset($CI->security))
Derek Jones5640a712010-04-23 11:22:40 -05001128 {
Derek Jones30841672010-04-26 09:09:21 -05001129 $CI->load->library('security');
Derek Jones5640a712010-04-23 11:22:40 -05001130 }
Barry Mienydd671972010-10-04 16:33:58 +02001131
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
1177 if($kind == 'scalar')
1178 {
1179 return $param->scalarval();
1180 }
1181 elseif($kind == 'array')
1182 {
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 }
1195 elseif($kind == 'struct')
1196 {
1197 reset($param->me['struct']);
Barry Mienydd671972010-10-04 16:33:58 +02001198
Derek Allard2067d1a2008-11-13 22:59:24 +00001199 $arr = array();
1200
1201 while(list($key,$value) = each($param->me['struct']))
1202 {
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
1218 * @author ExpressionEngine Dev Team
1219 * @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);
1346 while(list($key2, $val2) = each($val))
1347 {
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 {
1419 $t = strftime("%Y%m%dT%H:%M:%S", $time);
1420 }
1421 else
1422 {
1423 if (function_exists('gmstrftime'))
1424 $t = gmstrftime("%Y%m%dT%H:%M:%S", $time);
1425 else
1426 $t = strftime("%Y%m%dT%H:%M:%S", $time - date('Z'));
1427 }
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 */