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