More tabs/to/spaces conversion, plus remove closing php tags as they are not consider...
[plcapi.git] / lib / phpxmlrpc.php
1 <?php
2
3 class Phpxmlrpc {
4
5     public $xmlrpcI4 = "i4";
6     public $xmlrpcInt = "int";
7     public $xmlrpcBoolean = "boolean";
8     public $xmlrpcDouble = "double";
9     public $xmlrpcString = "string";
10     public $xmlrpcDateTime = "dateTime.iso8601";
11     public $xmlrpcBase64 = "base64";
12     public $xmlrpcArray = "array";
13     public $xmlrpcStruct = "struct";
14     public $xmlrpcValue = "undefined";
15     public $xmlrpcNull = "null";
16
17     public $xmlrpcTypes;
18
19     public $xmlrpc_valid_parents = array(
20         'VALUE' => array('MEMBER', 'DATA', 'PARAM', 'FAULT'),
21         'BOOLEAN' => array('VALUE'),
22         'I4' => array('VALUE'),
23         'INT' => array('VALUE'),
24         'STRING' => array('VALUE'),
25         'DOUBLE' => array('VALUE'),
26         'DATETIME.ISO8601' => array('VALUE'),
27         'BASE64' => array('VALUE'),
28         'MEMBER' => array('STRUCT'),
29         'NAME' => array('MEMBER'),
30         'DATA' => array('ARRAY'),
31         'ARRAY' => array('VALUE'),
32         'STRUCT' => array('VALUE'),
33         'PARAM' => array('PARAMS'),
34         'METHODNAME' => array('METHODCALL'),
35         'PARAMS' => array('METHODCALL', 'METHODRESPONSE'),
36         'FAULT' => array('METHODRESPONSE'),
37         'NIL' => array('VALUE'), // only used when extension activated
38         'EX:NIL' => array('VALUE') // only used when extension activated
39     );
40
41     // tables used for transcoding different charsets into us-ascii xml
42     public $xml_iso88591_Entities = array("in" => array(), "out" => array());
43
44     /// @todo add to iso table the characters from cp_1252 range, i.e. 128 to 159?
45     /// These will NOT be present in true ISO-8859-1, but will save the unwary
46     /// windows user from sending junk (though no luck when reciving them...)
47     /*
48     $GLOBALS['xml_cp1252_Entities']=array();
49     for ($i = 128; $i < 160; $i++)
50     {
51         $GLOBALS['xml_cp1252_Entities']['in'][] = chr($i);
52     }
53     $GLOBALS['xml_cp1252_Entities']['out'] = array(
54         '&#x20AC;', '?',        '&#x201A;', '&#x0192;',
55         '&#x201E;', '&#x2026;', '&#x2020;', '&#x2021;',
56         '&#x02C6;', '&#x2030;', '&#x0160;', '&#x2039;',
57         '&#x0152;', '?',        '&#x017D;', '?',
58         '?',        '&#x2018;', '&#x2019;', '&#x201C;',
59         '&#x201D;', '&#x2022;', '&#x2013;', '&#x2014;',
60         '&#x02DC;', '&#x2122;', '&#x0161;', '&#x203A;',
61         '&#x0153;', '?',        '&#x017E;', '&#x0178;'
62     );
63     */
64
65     public $xmlrpcerr = array(
66         'unknown_method'=>1,
67         'invalid_return'=>2,
68         'incorrect_params'=>3,
69         'introspect_unknown'=>4,
70         'http_error'=>5,
71         'no_data'=>6,
72         'no_ssl'=>7,
73         'curl_fail'=>8,
74         'invalid_request'=>15,
75         'no_curl'=>16,
76         'server_error'=>17,
77         'multicall_error'=>18,
78         'multicall_notstruct'=>9,
79         'multicall_nomethod'=>10,
80         'multicall_notstring'=>11,
81         'multicall_recursion'=>12,
82         'multicall_noparams'=>13,
83         'multicall_notarray'=>14,
84
85         'cannot_decompress'=>103,
86         'decompress_fail'=>104,
87         'dechunk_fail'=>105,
88         'server_cannot_decompress'=>106,
89         'server_decompress_fail'=>107
90     );
91
92     public $xmlrpcstr = array(
93         'unknown_method'=>'Unknown method',
94         'invalid_return'=>'Invalid return payload: enable debugging to examine incoming payload',
95         'incorrect_params'=>'Incorrect parameters passed to method',
96         'introspect_unknown'=>"Can't introspect: method unknown",
97         'http_error'=>"Didn't receive 200 OK from remote server.",
98         'no_data'=>'No data received from server.',
99         'no_ssl'=>'No SSL support compiled in.',
100         'curl_fail'=>'CURL error',
101         'invalid_request'=>'Invalid request payload',
102         'no_curl'=>'No CURL support compiled in.',
103         'server_error'=>'Internal server error',
104         'multicall_error'=>'Received from server invalid multicall response',
105         'multicall_notstruct'=>'system.multicall expected struct',
106         'multicall_nomethod'=>'missing methodName',
107         'multicall_notstring'=>'methodName is not a string',
108         'multicall_recursion'=>'recursive system.multicall forbidden',
109         'multicall_noparams'=>'missing params',
110         'multicall_notarray'=>'params is not an array',
111
112         'cannot_decompress'=>'Received from server compressed HTTP and cannot decompress',
113         'decompress_fail'=>'Received from server invalid compressed HTTP',
114         'dechunk_fail'=>'Received from server invalid chunked HTTP',
115         'server_cannot_decompress'=>'Received from client compressed HTTP request and cannot decompress',
116         'server_decompress_fail'=>'Received from client invalid compressed HTTP request'
117     );
118
119     // The charset encoding used by the server for received messages and
120     // by the client for received responses when received charset cannot be determined
121     // or is not supported
122     public $xmlrpc_defencoding = "UTF-8";
123
124     // The encoding used internally by PHP.
125     // String values received as xml will be converted to this, and php strings will be converted to xml
126     // as if having been coded with this
127     public $xmlrpc_internalencoding = "ISO-8859-1"; // TODO: maybe this would be better as UTF-8, or atleast configurable?
128
129     public $xmlrpcName = "XML-RPC for PHP";
130     public $xmlrpcVersion = "3.0.0.beta";
131
132     // let user errors start at 800
133     public $xmlrpcerruser = 800;
134     // let XML parse errors start at 100
135     public $xmlrpcerrxml = 100;
136
137     // set to TRUE to enable correct decoding of <NIL/> and <EX:NIL/> values
138     public $xmlrpc_null_extension = false;
139
140     // set to TRUE to enable encoding of php NULL values to <EX:NIL/> instead of <NIL/>
141     public $xmlrpc_null_apache_encoding = false;
142
143     public $xmlrpc_null_apache_encoding_ns = "http://ws.apache.org/xmlrpc/namespaces/extensions";
144
145     // used to store state during parsing
146     // quick explanation of components:
147     //   ac - used to accumulate values
148     //   isf - used to indicate a parsing fault (2) or xmlrpcresp fault (1)
149     //   isf_reason - used for storing xmlrpcresp fault string
150     //   lv - used to indicate "looking for a value": implements
151     //        the logic to allow values with no types to be strings
152     //   params - used to store parameters in method calls
153     //   method - used to store method name
154     //   stack - array with genealogy of xml elements names:
155     //           used to validate nesting of xmlrpc elements
156     public $_xh = null;
157
158     private static $instance = null;
159
160     private function __construct() {
161         $this->xmlrpcTypes = array(
162             $this->xmlrpcI4 => 1,
163             $this->xmlrpcInt => 1,
164             $this->xmlrpcBoolean => 1,
165             $this->xmlrpcDouble => 1,
166             $this->xmlrpcString => 1,
167             $this->xmlrpcDateTime => 1,
168             $this->xmlrpcBase64 => 1,
169             $this->xmlrpcArray => 2,
170             $this->xmlrpcStruct => 3,
171             $this->xmlrpcNull => 1
172         );
173
174         for($i = 0; $i < 32; $i++) {
175             $this->xml_iso88591_Entities["in"][] = chr($i);
176             $this->xml_iso88591_Entities["out"][] = "&#{$i};";
177         }
178
179         for($i = 160; $i < 256; $i++) {
180             $this->xml_iso88591_Entities["in"][] = chr($i);
181             $this->xml_iso88591_Entities["out"][] = "&#{$i};";
182         }
183     }
184
185     /**
186      * This class is singleton for performance reasons: this way the ASCII array needs to be done only once.
187      */
188     public static function instance() {
189         if(Phpxmlrpc::$instance === null) {
190             Phpxmlrpc::$instance = new Phpxmlrpc();
191         }
192
193         return Phpxmlrpc::$instance;
194     }
195 }