5 use PhpXmlRpc\Helper\XMLParser;
10 * Takes an xmlrpc value in object format and translates it into native PHP types.
12 * Works with xmlrpc requests objects as input, too.
14 * Given proper options parameter, can rebuild generic php object instances
15 * (provided those have been encoded to xmlrpc format using a corresponding
16 * option in php_xmlrpc_encode())
17 * PLEASE NOTE that rebuilding php objects involves calling their constructor function.
18 * This means that the remote communication end can decide which php code will
19 * get executed on your server, leaving the door possibly open to 'php-injection'
20 * style of attacks (provided you have some classes defined on your server that
21 * might wreak havoc if instances are built outside an appropriate context).
22 * Make sure you trust the remote server/client before eanbling this!
24 * @author Dan Libby (dan@libby.com)
26 * @param Value|Request $xmlrpcVal
27 * @param array $options if 'decode_php_objs' is set in the options array, xmlrpc structs can be decoded into php objects; if 'dates_as_objects' is set xmlrpc datetimes are decoded as php DateTime objects (standard is
31 public function decode($xmlrpcVal, $options = array())
33 switch ($xmlrpcVal->kindOf()) {
35 if (in_array('extension_api', $options)) {
36 reset($xmlrpcVal->me);
37 list($typ, $val) = each($xmlrpcVal->me);
39 case 'dateTime.iso8601':
40 $xmlrpcVal->scalar = $val;
41 $xmlrpcVal->type = 'datetime';
42 $xmlrpcVal->timestamp = \PhpXmlRpc\Helper\Date::iso8601Decode($val);
46 $xmlrpcVal->scalar = $val;
47 $xmlrpcVal->type = $typ;
51 return $xmlrpcVal->scalarval();
54 if (in_array('dates_as_objects', $options) && $xmlrpcVal->scalartyp() == 'dateTime.iso8601') {
55 // we return a Datetime object instead of a string
56 // since now the constructor of xmlrpc value accepts safely strings, ints and datetimes,
57 // we cater to all 3 cases here
58 $out = $xmlrpcVal->scalarval();
59 if (is_string($out)) {
60 $out = strtotime($out);
63 $result = new \Datetime();
64 $result->setTimestamp($out);
67 } elseif (is_a($out, 'Datetime')) {
72 return $xmlrpcVal->scalarval();
75 foreach($xmlrpcVal as $value) {
76 $arr[] = $this->decode($value, $options);
81 // If user said so, try to rebuild php objects for specific struct vals.
82 /// @todo should we raise a warning for class not found?
83 // shall we check for proper subclass of xmlrpc value instead of
84 // presence of _php_class to detect what we can do?
85 if (in_array('decode_php_objs', $options) && $xmlrpcVal->_php_class != ''
86 && class_exists($xmlrpcVal->_php_class)
88 $obj = @new $xmlrpcVal->_php_class();
89 foreach ($xmlrpcVal as $key => $value) {
90 $obj->$key = $this->decode($value, $options);
96 foreach ($xmlrpcVal as $key => $value) {
97 $arr[$key] = $this->decode($value, $options);
103 $paramCount = $xmlrpcVal->getNumParams();
105 for ($i = 0; $i < $paramCount; $i++) {
106 $arr[] = $this->decode($xmlrpcVal->getParam($i), $options);
114 * Takes native php types and encodes them into xmlrpc PHP object format.
115 * It will not re-encode xmlrpc value objects.
117 * Feature creep -- could support more types via optional type argument
118 * (string => datetime support has been added, ??? => base64 not yet)
120 * If given a proper options parameter, php object instances will be encoded
121 * into 'special' xmlrpc values, that can later be decoded into php objects
122 * by calling php_xmlrpc_decode() with a corresponding option
124 * @author Dan Libby (dan@libby.com)
126 * @param mixed $phpVal the value to be converted into an xmlrpc value object
127 * @param array $options can include 'encode_php_objs', 'auto_dates', 'null_extension' or 'extension_api'
129 * @return \PhpXmlrpc\Value
131 public function encode($phpVal, $options = array())
133 $type = gettype($phpVal);
136 if (in_array('auto_dates', $options) && preg_match('/^[0-9]{8}T[0-9]{2}:[0-9]{2}:[0-9]{2}$/', $phpVal)) {
137 $xmlrpcVal = new Value($phpVal, Value::$xmlrpcDateTime);
139 $xmlrpcVal = new Value($phpVal, Value::$xmlrpcString);
143 $xmlrpcVal = new Value($phpVal, Value::$xmlrpcInt);
146 $xmlrpcVal = new Value($phpVal, Value::$xmlrpcDouble);
148 // <G_Giunta_2001-02-29>
149 // Add support for encoding/decoding of booleans, since they are supported in PHP
151 $xmlrpcVal = new Value($phpVal, Value::$xmlrpcBoolean);
153 // </G_Giunta_2001-02-29>
155 // PHP arrays can be encoded to either xmlrpc structs or arrays,
156 // depending on wheter they are hashes or plain 0..n integer indexed
157 // A shorter one-liner would be
158 // $tmp = array_diff(array_keys($phpVal), range(0, count($phpVal)-1));
159 // but execution time skyrockets!
163 foreach ($phpVal as $key => $val) {
164 $arr[$key] = $this->encode($val, $options);
165 if (!$ko && $key !== $j) {
171 $xmlrpcVal = new Value($arr, Value::$xmlrpcStruct);
173 $xmlrpcVal = new Value($arr, Value::$xmlrpcArray);
177 if (is_a($phpVal, 'PhpXmlRpc\Value')) {
178 $xmlrpcVal = $phpVal;
179 } elseif (is_a($phpVal, 'DateTime')) {
180 $xmlrpcVal = new Value($phpVal->format('Ymd\TH:i:s'), Value::$xmlrpcStruct);
184 while (list($k, $v) = each($phpVal)) {
185 $arr[$k] = $this->encode($v, $options);
187 $xmlrpcVal = new Value($arr, Value::$xmlrpcStruct);
188 if (in_array('encode_php_objs', $options)) {
189 // let's save original class name into xmlrpc value:
190 // might be useful later on...
191 $xmlrpcVal->_php_class = get_class($phpVal);
196 if (in_array('extension_api', $options)) {
197 $xmlrpcVal = new Value('', Value::$xmlrpcString);
198 } elseif (in_array('null_extension', $options)) {
199 $xmlrpcVal = new Value('', Value::$xmlrpcNull);
201 $xmlrpcVal = new Value();
205 if (in_array('extension_api', $options)) {
206 $xmlrpcVal = new Value((int)$phpVal, Value::$xmlrpcInt);
208 $xmlrpcVal = new Value();
210 // catch "user function", "unknown type"
212 // giancarlo pinerolo <ping@alt.it>
214 // an empty object in case, not a boolean.
215 $xmlrpcVal = new Value();
223 * Convert the xml representation of a method response, method request or single
224 * xmlrpc value into the appropriate object (a.k.a. deserialize).
226 * @param string $xmlVal
227 * @param array $options
229 * @return mixed false on error, or an instance of either Value, Request or Response
231 public function decodeXml($xmlVal, $options = array())
233 // 'guestimate' encoding
234 $valEncoding = XMLParser::guessEncoding('', $xmlVal);
235 if ($valEncoding != '') {
237 // Since parsing will fail if charset is not specified in the xml prologue,
238 // the encoding is not UTF8 and there are non-ascii chars in the text, we try to work round that...
239 // The following code might be better for mb_string enabled installs, but
240 // makes the lib about 200% slower...
241 //if (!is_valid_charset($valEncoding, array('UTF-8'))
242 if (!in_array($valEncoding, array('UTF-8', 'US-ASCII')) && !XMLParser::hasEncoding($xmlVal)) {
243 if ($valEncoding == 'ISO-8859-1') {
244 $xmlVal = utf8_encode($xmlVal);
246 if (extension_loaded('mbstring')) {
247 $xmlVal = mb_convert_encoding($xmlVal, 'UTF-8', $valEncoding);
249 error_log('XML-RPC: ' . __METHOD__ . ': invalid charset encoding of xml text: ' . $valEncoding);
255 $parser = xml_parser_create();
256 xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, true);
257 // What if internal encoding is not in one of the 3 allowed?
258 // we use the broadest one, ie. utf8!
259 if (!in_array(PhpXmlRpc::$xmlrpc_internalencoding, array('UTF-8', 'ISO-8859-1', 'US-ASCII'))) {
260 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
262 xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, PhpXmlRpc::$xmlrpc_internalencoding);
265 $xmlRpcParser = new XMLParser();
266 xml_set_object($parser, $xmlRpcParser);
268 xml_set_element_handler($parser, 'xmlrpc_se_any', 'xmlrpc_ee');
269 xml_set_character_data_handler($parser, 'xmlrpc_cd');
270 xml_set_default_handler($parser, 'xmlrpc_dh');
271 if (!xml_parse($parser, $xmlVal, 1)) {
272 $errstr = sprintf('XML error: %s at line %d, column %d',
273 xml_error_string(xml_get_error_code($parser)),
274 xml_get_current_line_number($parser), xml_get_current_column_number($parser));
276 xml_parser_free($parser);
280 xml_parser_free($parser);
281 if ($xmlRpcParser->_xh['isf'] > 1) {
282 // test that $xmlrpc->_xh['value'] is an obj, too???
284 error_log($xmlRpcParser->_xh['isf_reason']);
288 switch ($xmlRpcParser->_xh['rt']) {
289 case 'methodresponse':
290 $v = &$xmlRpcParser->_xh['value'];
291 if ($xmlRpcParser->_xh['isf'] == 1) {
292 $vc = $v['faultCode'];
293 $vs = $v['faultString'];
294 $r = new Response(0, $vc->scalarval(), $vs->scalarval());
296 $r = new Response($v);
301 $req = new Request($xmlRpcParser->_xh['method']);
302 for ($i = 0; $i < count($xmlRpcParser->_xh['params']); $i++) {
303 $req->addParam($xmlRpcParser->_xh['params'][$i]);
308 return $xmlRpcParser->_xh['value'];