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