xmlrpc.inc 12.9 KB
Newer Older
Dries's avatar
   
Dries committed
1
<?php
2
3
4
5
6
7
8
/*
  Drupal XML-RPC library. Based on the IXR - The Incutio XML-RPC Library - (c) Incutio Ltd 2002-2005
   Version 1.7 (beta) - Simon Willison, 23rd May 2005
   Site:   http://scripts.incutio.com/xmlrpc/
   Manual: http://scripts.incutio.com/xmlrpc/manual.php
   This version is made available under the GNU GPL License
*/
Dries's avatar
   
Dries committed
9

10
11
12
13
14
15
16
17
18
19
20
function xmlrpc_value($data, $type = FALSE) {
  $xmlrpc_value = new stdClass();
  $xmlrpc_value->data = $data;
  if (!$type) {
    $type = xmlrpc_value_calculate_type($xmlrpc_value);
  }
  $xmlrpc_value->type = $type;
  if ($type == 'struct') {
    /* Turn all the values in the array in to new xmlrpc_values */
    foreach ($xmlrpc_value->data as $key => $value) {
      $xmlrpc_value->data[$key] = xmlrpc_value($value);
Dries's avatar
   
Dries committed
21
22
    }
  }
23
24
25
26
27
28
  if ($type == 'array') {
    for ($i = 0, $j = count($xmlrpc_value->data); $i < $j; $i++) {
      $xmlrpc_value->data[$i] = xmlrpc_value($xmlrpc_value->data[$i]);
    }
  }
  return $xmlrpc_value;
Dries's avatar
   
Dries committed
29
30
}

31
32
33
34
35
36
37
38
function xmlrpc_value_calculate_type(&$xmlrpc_value) {
  $type = gettype($xmlrpc_value->data);
  switch ($type) {
    case 'boolean': case 'double':
      return $type;
    case 'integer':
      return 'int';
    case 'array':
39
      return range(0, count($xmlrpc_value->data) - 1) === array_keys($xmlrpc_value->data) ? 'array' : 'struct';
40
41
42
    case 'object':
      if ($xmlrpc_value->data->is_date) {
        return 'date';
Dries's avatar
   
Dries committed
43
      }
44
45
      if ($xmlrpc_value->data->is_base64) {
        return 'base64';
Dries's avatar
   
Dries committed
46
      }
47
48
      $xmlrpc_value->data = get_object_vars($xmlrpc_value->data);
      return 'struct';
Dries's avatar
   
Dries committed
49
    default:
50
      return string;
Dries's avatar
   
Dries committed
51
52
53
  }
}

54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
function xmlrpc_value_get_xml($xmlrpc_value) {
  /* Return XML for this value */
  switch ($xmlrpc_value->type) {
    case 'boolean':
      return '<boolean>'. (($xmlrpc_value->data) ? '1' : '0') .'</boolean>';
      break;
    case 'int':
      return '<int>'. $xmlrpc_value->data .'</int>';
      break;
    case 'double':
      return '<double>'. $xmlrpc_value->data .'</double>';
      break;
    case 'string':
      return '<string>'. check_plain($xmlrpc_value->data) .'</string>';
      break;
    case 'array':
      $return = '<array><data>'."\n";
      foreach ($xmlrpc_value->data as $item) {
        $return .= '  <value>'. xmlrpc_value_get_xml($item) ."</value>\n";
      }
      $return .= '</data></array>';
      return $return;
      break;
    case 'struct':
      $return = '<struct>'."\n";
      foreach ($xmlrpc_value->data as $name => $value) {
        $return .= "  <member><name>". check_plain($name) ."</name><value>";
        $return .= xmlrpc_value_get_xml($value)."</value></member>\n";
      }
      $return .= '</struct>';
      return $return;
      break;
    case 'date':
      return xmlrpc_date_get_xml($xmlrpc_value->data);
      break;
    case 'base64':
      return xmlrpc_base64_get_xml($xmlrpc_value->data);
      break;
Dries's avatar
   
Dries committed
92
  }
93
  return FALSE;
Dries's avatar
   
Dries committed
94
95
}

96
97
98
99
100
101
102
function xmlrpc_message($message) {
  $xmlrpc_message = new stdClass();
  $xmlrpc_message->array_structs = array();   // The stack used to keep track of the current array/struct
  $xmlrpc_message->array_structs_types = array(); // Stack keeping track of if things are structs or array
  $xmlrpc_message->current_struct_name = array();  // A stack as well
  $xmlrpc_message->message = $message;
  return $xmlrpc_message;
103
104
}

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
function xmlrpc_message_parse(&$xmlrpc_message) {
  // first remove the XML declaration
  $xmlrpc_message->message = preg_replace('/<\?xml(.*)?\?'.'>/', '', $xmlrpc_message->message);
  if (trim($xmlrpc_message->message) == '') {
    return FALSE;
  }
  $xmlrpc_message->_parser = xml_parser_create();
  // Set XML parser to take the case of tags in to account
  xml_parser_set_option($xmlrpc_message->_parser, XML_OPTION_CASE_FOLDING, FALSE);
  // Set XML parser callback functions
  /* do not set object. $xmlrpc_message does not have member functions any more
  xml_set_object($xmlrpc_message->_parser, $xmlrpc_message); */
  xml_set_element_handler($xmlrpc_message->_parser, 'xmlrpc_message_tag_open', 'xmlrpc_message_tag_close');
  xml_set_character_data_handler($xmlrpc_message->_parser, 'xmlrpc_message_cdata');
  xmlrpc_message_set($xmlrpc_message);
  if (!xml_parse($xmlrpc_message->_parser, $xmlrpc_message->message)) {
    /* die(sprintf('XML error: %s at line %d',
      xml_error_string(xml_get_error_code($xmlrpc_message->_parser)),
      xml_get_current_line_number($xmlrpc_message->_parser))); */
    return FALSE;
  }
  xml_parser_free($xmlrpc_message->_parser);
  // Grab the error messages, if any
  $xmlrpc_message = xmlrpc_message_get();
  if ($xmlrpc_message->messagetype == 'fault') {
    $xmlrpc_message->fault_code = $xmlrpc_message->params[0]['faultCode'];
    $xmlrpc_message->fault_string = $xmlrpc_message->params[0]['faultString'];
  }
  return TRUE;
Dries's avatar
   
Dries committed
134
135
}

136
137
138
139
function xmlrpc_message_set($value = NULL) {
  static $xmlrpc_message;
  if ($value) {
    $xmlrpc_message = $value;
Dries's avatar
   
Dries committed
140
  }
141
  return $xmlrpc_message;
Dries's avatar
   
Dries committed
142
143
}

144
145
146
function xmlrpc_message_get() {
  return xmlrpc_message_set();
}
Dries's avatar
   
Dries committed
147

148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
function xmlrpc_message_tag_open($parser, $tag, $attr) {
  $xmlrpc_message = xmlrpc_message_get();
  $xmlrpc_message->current_tag_contents = '';
  switch($tag) {
    case 'methodCall':
    case 'methodResponse':
    case 'fault':
      $xmlrpc_message->messagetype = $tag;
      break;
    /* Deal with stacks of arrays and structs */
    case 'data':
      $xmlrpc_message->array_structs_types[] = 'array';
      $xmlrpc_message->array_structs[] = array();
      break;
    case 'struct':
      $xmlrpc_message->array_structs_types[] = 'struct';
      $xmlrpc_message->array_structs[] = array();
      break;
Dries's avatar
   
Dries committed
166
  }
167
  xmlrpc_message_set($xmlrpc_message);
Dries's avatar
   
Dries committed
168
169
}

170
171
172
173
174
function xmlrpc_message_cdata($parser, $cdata) {
  $xmlrpc_message = xmlrpc_message_get();
  $xmlrpc_message->current_tag_contents .= $cdata;
  xmlrpc_message_set($xmlrpc_message);
}
Dries's avatar
   
Dries committed
175

176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
function xmlrpc_message_tag_close($parser, $tag) {
  $xmlrpc_message = xmlrpc_message_get();
  $value_flag = FALSE;
  switch($tag) {
    case 'int':
    case 'i4':
      $value = (int)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
    case 'double':
      $value = (double)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
    case 'string':
      $value = $xmlrpc_message->current_tag_contents;
      $value_flag = TRUE;
      break;
    case 'dateTime.iso8601':
      $value = xmlrpc_date(trim($xmlrpc_message->current_tag_contents));
      // $value = $iso->getTimestamp();
      $value_flag = TRUE;
      break;
    case 'value':
      // "If no type is indicated, the type is string."
      if (trim($xmlrpc_message->current_tag_contents) != '') {
        $value = (string)$xmlrpc_message->current_tag_contents;
        $value_flag = TRUE;
Dries's avatar
   
Dries committed
203
      }
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
      break;
    case 'boolean':
      $value = (boolean)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
    case 'base64':
      $value = base64_decode(trim($xmlrpc_message->current_tag_contents));
      $value_flag = TRUE;
      break;
    /* Deal with stacks of arrays and structs */
    case 'data':
    case 'struct':
      $value = array_pop($xmlrpc_message->array_structs );
      array_pop($xmlrpc_message->array_structs_types);
      $value_flag = TRUE;
      break;
    case 'member':
      array_pop($xmlrpc_message->current_struct_name);
      break;
    case 'name':
      $xmlrpc_message->current_struct_name[] = trim($xmlrpc_message->current_tag_contents);
      break;
    case 'methodName':
      $xmlrpc_message->methodname = trim($xmlrpc_message->current_tag_contents);
      break;
Dries's avatar
   
Dries committed
229
  }
230
231
232
233
234
235
  if ($value_flag) {
    if (count($xmlrpc_message->array_structs ) > 0) {
      // Add value to struct or array
      if ($xmlrpc_message->array_structs_types[count($xmlrpc_message->array_structs_types)-1] == 'struct') {
        // Add to struct
        $xmlrpc_message->array_structs [count($xmlrpc_message->array_structs )-1][$xmlrpc_message->current_struct_name[count($xmlrpc_message->current_struct_name)-1]] = $value;
Dries's avatar
   
Dries committed
236
      } else {
237
238
        // Add to array
        $xmlrpc_message->array_structs [count($xmlrpc_message->array_structs )-1][] = $value;
Dries's avatar
   
Dries committed
239
240
      }
    } else {
241
242
      // Just add as a paramater
      $xmlrpc_message->params[] = $value;
Dries's avatar
   
Dries committed
243
244
    }
  }
245
246
  if (!in_array($tag, array("data", "struct", "member"))) {
    $xmlrpc_message->current_tag_contents = '';
Dries's avatar
   
Dries committed
247
  }
248
249
  xmlrpc_message_set($xmlrpc_message);
}
Dries's avatar
   
Dries committed
250

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
function xmlrpc_request($method, $args) {
  $xmlrpc_request = new stdClass();
  $xmlrpc_request->method = $method;
  $xmlrpc_request->args = $args;
  $xmlrpc_request->xml = <<<EOD
<?xml version="1.0"?>
<methodCall>
<methodName>{$xmlrpc_request->method}</methodName>
<params>

EOD;
  foreach ($xmlrpc_request->args as $arg) {
    $xmlrpc_request->xml .= '<param><value>';
    $v = xmlrpc_value($arg);
    $xmlrpc_request->xml .= xmlrpc_value_get_xml($v);
    $xmlrpc_request->xml .= "</value></param>\n";
  }
  $xmlrpc_request->xml .= '</params></methodCall>';
  return $xmlrpc_request;
}
Dries's avatar
   
Dries committed
271
272


273
274
275
276
277
278
279
function xmlrpc_error($code = NULL, $message = NULL) {
  static $xmlrpc_error;
  if ($code) {
    $xmlrpc_error = new stdClass();
    $xmlrpc_error->is_error = TRUE;
    $xmlrpc_error->code = $code;
    $xmlrpc_error->message = $message;
Dries's avatar
   
Dries committed
280
  }
281
  return $xmlrpc_error;
Dries's avatar
   
Dries committed
282
283
}

284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
function xmlrpc_error_get_xml($xmlrpc_error) {
    return <<<EOD
<methodResponse>
  <fault>
  <value>
    <struct>
    <member>
      <name>faultCode</name>
      <value><int>{$xmlrpc_error->code}</int></value>
    </member>
    <member>
      <name>faultString</name>
      <value><string>{$xmlrpc_error->message}</string></value>
    </member>
    </struct>
  </value>
  </fault>
</methodResponse>

EOD;
Dries's avatar
   
Dries committed
304
305
}

306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
function xmlrpc_date($time) {
  $xmlrpc_date = new stdClass();
  $xmlrpc_date->is_date = TRUE;
  // $time can be a PHP timestamp or an ISO one
  if (is_numeric($time)) {
    $xmlrpc_date->year = date('Y', $time);
    $xmlrpc_date->month = date('m', $time);
    $xmlrpc_date->day = date('d', $time);
    $xmlrpc_date->hour = date('H', $time);
    $xmlrpc_date->minute = date('i', $time);
    $xmlrpc_date->second = date('s', $time);
  }
  else {
    $xmlrpc_date->year = substr($time, 0, 4);
    $xmlrpc_date->month = substr($time, 4, 2);
    $xmlrpc_date->day = substr($time, 6, 2);
    $xmlrpc_date->hour = substr($time, 9, 2);
    $xmlrpc_date->minute = substr($time, 12, 2);
    $xmlrpc_date->second = substr($time, 15, 2);
  }
  return $xmlrpc_date;
Dries's avatar
   
Dries committed
327
328
}

329
330
331
function xmlrpc_date_get_xml($xmlrpc_date) {
  return '<dateTime.iso8601>'. $xmlrpc_date->year . $xmlrpc_date->month . $xmlrpc_date->day .'T'. $xmlrpc_date->hour .':'. $xmlrpc_date->minute .':'. $xmlrpc_date->second .'</dateTime.iso8601>';
}
Dries's avatar
   
Dries committed
332

333
334
335
336
337
338
function xmlrpc_base64($data) {
  $xmlrpc_base64 = new stdClass();
  $xmlrpc_base64->is_base64 = TRUE;
  $xmlrpc_base64->data = $data;
  return $xmlrpc_base64;
}
Dries's avatar
   
Dries committed
339

340
341
function xmlrpc_base64_get_xml($xmlrpc_base64) {
  return '<base64>'. base64_encode($xmlrpc_base64->data) .'</base64>';
Dries's avatar
   
Dries committed
342
343
}

344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
 * Perform an XML-RPC request.
 *
 * @param $url
 *    An absolute URL of the XML-RPC server
 * @param $method
 *    The method to be executed
 * @param ...
 *   A variable number of arguments of the method.
 * @return
 *   The return value of the method on success or FALSE. On FALSE, see
 *   xmlrpc_errno() and xmlrpc_error_msg().
 */
function xmlrpc() {
  $args = func_get_args();
  $url = array_shift($args);
  $method = array_shift($args);
  $xmlrpc_request = xmlrpc_request($method, $args);
//  $request .= "Content-Type: text/xml$r";
  $result = drupal_http_request($url, array("Content-Type" => "text/xml"), 'POST', $xmlrpc_request->xml);
  if ($result->code != 200) {
    xmlrpc_error(-$result->code, $result->error);
    return FALSE;
  }
  $message = xmlrpc_message($result->data);
  // Now parse what we've got back
  if (!xmlrpc_message_parse($message)) {
    // XML error
    xmlrpc_error(-32700, t('parse error. not well formed'));
    return FALSE;
  }
  // Is the message a fault?
  if ($message->messagetype == 'fault') {
    xmlrpc_error($message->faultcode, $message->faultstring);
    return FALSE;
  }
  // Message must be OK
  return $message->params[0];
}
Dries's avatar
   
Dries committed
383

384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/**
 * Perform multiple calls in one request if possible.
 *
 * @param $url
 *    An absolute URL of the XML-RPC server
 * @param $calls
 *    An array of calls. Each call is an array, where the first element
 *    is the method name, further elements are the arguments.
 * @return
 */
function xmlrpc_multicall() {
  $args = func_get_args();
  $url = $args[0];
  $client = xmlrpc_client_multicall($url);
  foreach ($args[1] as $call) {
    $method = array_shift($call);
    $calls[] = array(
      'methodName' => $method,
      'params' => $args
    );
  }
  $result = xmlrpc($url, 'system.multicall', $calls);
//    $result = xmlrpc_client_multicall_query($client);
//    $results[$method] = ($result === FALSE) ? xmlrpc_error() : $result;
//    return $results;
}
Dries's avatar
   
Dries committed
410

411
412
413
414
415
416
/**
 * Returns the last XML-RPC client error number
 */
function xmlrpc_errno() {
  $error = xmlrpc_error();
  return $error->code;
Dries's avatar
   
Dries committed
417
418
}

419
420
421
422
423
424
425
/**
 * Returns the last XML-RPC client error message
 */
function xmlrpc_error_msg() {
  $error = xmlrpc_error();
  return $error->message;
}
Dries's avatar
   
Dries committed
426
?>