xmlrpc.inc 17.3 KB
Newer Older
1
<?php
2 3
// $Id$

4 5
/**
 * @file
6 7 8
 * Drupal XML-RPC library.
 *
 * Based on the IXR - The Incutio XML-RPC Library - (c) Incutio Ltd 2002-2005
9 10 11 12
 * 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
13
 */
14

15
/**
16
 * Turns a data structure into objects with 'data' and 'type' attributes.
17 18 19
 *
 * @param $data
 *   The data structure.
20
 * @param $type
21 22 23 24
 *   Optional type to assign to $data.
 *
 * @return object
 *   An XML-RPC data object containing the input $data.
25
 */
26 27 28 29 30 31 32 33
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') {
34
    // Turn all the values in the array into new xmlrpc_values
35 36
    foreach ($xmlrpc_value->data as $key => $value) {
      $xmlrpc_value->data[$key] = xmlrpc_value($value);
37 38
    }
  }
39 40 41 42 43 44
  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;
45 46
}

Dries's avatar
Dries committed
47
/**
48
 * Maps a PHP type to an XML-RPC type.
Dries's avatar
Dries committed
49 50 51
 *
 * @param $xmlrpc_value
 *   Variable whose type should be mapped.
52 53 54 55
 *
 * @return string
 *   The corresponding XML-RPC type.
 *
56
 * @see http://www.xmlrpc.com/spec#scalars
Dries's avatar
Dries committed
57
 */
58
function xmlrpc_value_calculate_type($xmlrpc_value) {
59 60
  // http://www.php.net/gettype: Never use gettype() to test for a certain type
  // [...] Instead, use the is_* functions.
Dries's avatar
Dries committed
61 62 63 64 65 66 67
  if (is_bool($xmlrpc_value->data)) {
    return 'boolean';
  }
  if (is_double($xmlrpc_value->data)) {
    return 'double';
  }
  if (is_int($xmlrpc_value->data)) {
68
    return 'int';
69
  }
Dries's avatar
Dries committed
70 71 72 73 74
  if (is_array($xmlrpc_value->data)) {
    // empty or integer-indexed arrays are 'array', string-indexed arrays 'struct'
    return empty($xmlrpc_value->data) || range(0, count($xmlrpc_value->data) - 1) === array_keys($xmlrpc_value->data) ? 'array' : 'struct';
  }
  if (is_object($xmlrpc_value->data)) {
75
    if (isset($xmlrpc_value->data->is_date)) {
Dries's avatar
Dries committed
76 77
      return 'date';
    }
78
    if (isset($xmlrpc_value->data->is_base64)) {
Dries's avatar
Dries committed
79 80 81 82 83 84 85
      return 'base64';
    }
    $xmlrpc_value->data = get_object_vars($xmlrpc_value->data);
    return 'struct';
  }
  // default
  return 'string';
86 87
}

88
/**
89
 * Generates XML representing the given value.
90 91
 *
 * @param $xmlrpc_value
92
 *   A value to be represented in XML.
93
 *
94
 * @return
95
 *   XML representation of $xmlrpc_value.
96
 */
97 98 99
function xmlrpc_value_get_xml($xmlrpc_value) {
  switch ($xmlrpc_value->type) {
    case 'boolean':
100
      return '<boolean>' . (($xmlrpc_value->data) ? '1' : '0') . '</boolean>';
101

102
    case 'int':
103
      return '<int>' . $xmlrpc_value->data . '</int>';
104

105
    case 'double':
106
      return '<double>' . $xmlrpc_value->data . '</double>';
107

108
    case 'string':
109 110
      // Note: we don't escape apostrophes because of the many blogging clients
      // that don't support numerical entities (and XML in general) properly.
111
      return '<string>' . htmlspecialchars($xmlrpc_value->data) . '</string>';
112

113
    case 'array':
114
      $return = '<array><data>' . "\n";
115
      foreach ($xmlrpc_value->data as $item) {
116
        $return .= '  <value>' . xmlrpc_value_get_xml($item) . "</value>\n";
117 118 119
      }
      $return .= '</data></array>';
      return $return;
120

121
    case 'struct':
122
      $return = '<struct>' . "\n";
123
      foreach ($xmlrpc_value->data as $name => $value) {
124 125
        $return .= "  <member><name>" . check_plain($name) . "</name><value>";
        $return .= xmlrpc_value_get_xml($value) . "</value></member>\n";
126 127 128
      }
      $return .= '</struct>';
      return $return;
129

130 131
    case 'date':
      return xmlrpc_date_get_xml($xmlrpc_value->data);
132

133 134
    case 'base64':
      return xmlrpc_base64_get_xml($xmlrpc_value->data);
135
  }
136
  return FALSE;
137 138
}

139
/**
140
 * Constructs an object representing an XML-RPC message.
141 142
 *
 * @param $message
143 144 145 146 147 148
 *   A string containing an XML message.
 *
 * @return object
 *   An XML-RPC object containing the message.
 *
 * @see http://www.xmlrpc.com/spec
149
 */
150 151
function xmlrpc_message($message) {
  $xmlrpc_message = new stdClass();
152 153 154 155 156 157
  // The stack used to keep track of the current array/struct
  $xmlrpc_message->array_structs = array();
  // The stack used to keep track of if things are structs or array
  $xmlrpc_message->array_structs_types = array();
  // A stack as well
  $xmlrpc_message->current_struct_name = array();
158 159
  $xmlrpc_message->message = $message;
  return $xmlrpc_message;
160 161
}

162
/**
163
 * Parses an XML-RPC message.
164 165 166
 *
 * If parsing fails, the faultCode and faultString will be added to the message
 * object.
167 168
 *
 * @param $xmlrpc_message
169 170
 *   An object generated by xmlrpc_message().
 *
171
 * @return
172
 *   TRUE if parsing succeeded; FALSE otherwise.
173
 */
174
function xmlrpc_message_parse($xmlrpc_message) {
175
  $xmlrpc_message->_parser = xml_parser_create();
176
  // Set XML parser to take the case of tags into account.
177 178 179 180 181 182 183 184 185
  xml_parser_set_option($xmlrpc_message->_parser, XML_OPTION_CASE_FOLDING, FALSE);
  // Set XML parser callback functions
  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)) {
    return FALSE;
  }
  xml_parser_free($xmlrpc_message->_parser);
186 187

  // Grab the error messages, if any.
188
  $xmlrpc_message = xmlrpc_message_get();
189 190 191 192
  if (!isset($xmlrpc_message->messagetype)) {
    return FALSE;
  }
  elseif ($xmlrpc_message->messagetype == 'fault') {
193 194 195 196
    $xmlrpc_message->fault_code = $xmlrpc_message->params[0]['faultCode'];
    $xmlrpc_message->fault_string = $xmlrpc_message->params[0]['faultString'];
  }
  return TRUE;
197 198
}

199
/**
200
 * Stores a copy of the most recent XML-RPC message object temporarily.
201 202
 *
 * @param $value
203 204 205 206 207 208
 *   An XML-RPC message to store, or NULL to keep the last message.
 *
 * @return object
 *   The most recently stored message.
 *
 * @see xmlrpc_message_get()
209
 */
210 211 212 213
function xmlrpc_message_set($value = NULL) {
  static $xmlrpc_message;
  if ($value) {
    $xmlrpc_message = $value;
214
  }
215
  return $xmlrpc_message;
216 217
}

218 219 220 221 222 223 224 225
/**
 * Returns the most recently stored XML-RPC message object.
 *
 * @return object
 *   The most recently stored message.
 *
 * @see xmlrpc_message_set()
 */
226 227 228
function xmlrpc_message_get() {
  return xmlrpc_message_set();
}
229

230 231 232
/**
 * Handles opening tags for XML parsing in xmlrpc_message_parse().
 */
233 234 235
function xmlrpc_message_tag_open($parser, $tag, $attr) {
  $xmlrpc_message = xmlrpc_message_get();
  $xmlrpc_message->current_tag_contents = '';
236
  $xmlrpc_message->last_open = $tag;
237
  switch ($tag) {
238 239 240 241 242
    case 'methodCall':
    case 'methodResponse':
    case 'fault':
      $xmlrpc_message->messagetype = $tag;
      break;
243

244
    // Deal with stacks of arrays and structs
245 246 247 248
    case 'data':
      $xmlrpc_message->array_structs_types[] = 'array';
      $xmlrpc_message->array_structs[] = array();
      break;
249

250 251 252 253
    case 'struct':
      $xmlrpc_message->array_structs_types[] = 'struct';
      $xmlrpc_message->array_structs[] = array();
      break;
254
  }
255
  xmlrpc_message_set($xmlrpc_message);
256 257
}

258 259 260
/**
 * Handles character data for XML parsing in xmlrpc_message_parse().
 */
261 262 263 264 265
function xmlrpc_message_cdata($parser, $cdata) {
  $xmlrpc_message = xmlrpc_message_get();
  $xmlrpc_message->current_tag_contents .= $cdata;
  xmlrpc_message_set($xmlrpc_message);
}
266

267 268 269
/**
 * Handles closing tags for XML parsing in xmlrpc_message_parse().
 */
270 271 272
function xmlrpc_message_tag_close($parser, $tag) {
  $xmlrpc_message = xmlrpc_message_get();
  $value_flag = FALSE;
273
  switch ($tag) {
274 275 276 277 278
    case 'int':
    case 'i4':
      $value = (int)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
279

280 281 282 283
    case 'double':
      $value = (double)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
284

285 286 287 288
    case 'string':
      $value = $xmlrpc_message->current_tag_contents;
      $value_flag = TRUE;
      break;
289

290 291 292 293 294
    case 'dateTime.iso8601':
      $value = xmlrpc_date(trim($xmlrpc_message->current_tag_contents));
      // $value = $iso->getTimestamp();
      $value_flag = TRUE;
      break;
295

296
    case 'value':
297
      // If no type is indicated, the type is string
298
      // We take special care for empty values
299
      if (trim($xmlrpc_message->current_tag_contents) != '' || (isset($xmlrpc_message->last_open) && ($xmlrpc_message->last_open == 'value'))) {
300
        $value = (string) $xmlrpc_message->current_tag_contents;
301 302 303
        $value_flag = TRUE;
      }
      unset($xmlrpc_message->last_open);
304
      break;
305

306 307 308 309
    case 'boolean':
      $value = (boolean)trim($xmlrpc_message->current_tag_contents);
      $value_flag = TRUE;
      break;
310

311 312 313 314
    case 'base64':
      $value = base64_decode(trim($xmlrpc_message->current_tag_contents));
      $value_flag = TRUE;
      break;
315

316
    // Deal with stacks of arrays and structs
317 318
    case 'data':
    case 'struct':
319
      $value = array_pop($xmlrpc_message->array_structs);
320 321 322
      array_pop($xmlrpc_message->array_structs_types);
      $value_flag = TRUE;
      break;
323

324 325 326
    case 'member':
      array_pop($xmlrpc_message->current_struct_name);
      break;
327

328 329 330
    case 'name':
      $xmlrpc_message->current_struct_name[] = trim($xmlrpc_message->current_tag_contents);
      break;
331

332 333 334
    case 'methodName':
      $xmlrpc_message->methodname = trim($xmlrpc_message->current_tag_contents);
      break;
335
  }
336
  if ($value_flag) {
337
    if (count($xmlrpc_message->array_structs) > 0) {
338
      // Add value to struct or array
339
      if ($xmlrpc_message->array_structs_types[count($xmlrpc_message->array_structs_types) - 1] == 'struct') {
340
        // Add to struct
341
        $xmlrpc_message->array_structs[count($xmlrpc_message->array_structs) - 1][$xmlrpc_message->current_struct_name[count($xmlrpc_message->current_struct_name) - 1]] = $value;
342 343
      }
      else {
344
        // Add to array
345
        $xmlrpc_message->array_structs[count($xmlrpc_message->array_structs) - 1][] = $value;
346
      }
347 348
    }
    else {
349
      // Just add as a parameter
350
      $xmlrpc_message->params[] = $value;
351 352
    }
  }
353 354
  if (!in_array($tag, array("data", "struct", "member"))) {
    $xmlrpc_message->current_tag_contents = '';
355
  }
356 357
  xmlrpc_message_set($xmlrpc_message);
}
358

359
/**
360
 * Constructs an object representing an XML-RPC request.
361 362
 *
 * @param $method
363
 *   The name of the method to be called.
364 365
 * @param $args
 *   An array of parameters to send with the method.
366 367 368
 *
 * @return object
 *   An XML-RPC object representing the request.
369
 */
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
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;
}
390

391 392 393 394 395 396 397 398 399 400 401 402 403 404
/**
 * Generates, temporarily saves, and returns an XML-RPC error object.
 *
 * @param $code
 *   The error code.
 * @param $message
 *   The error message.
 * @param $reset
 *   TRUE to empty the temporary error storage. Ignored if $code is supplied.
 *
 * @return object
 *   An XML-RPC error object representing $code and $message, or the most
 *   recently stored error object if omitted.
 */
405
function xmlrpc_error($code = NULL, $message = NULL, $reset = FALSE) {
406
  static $xmlrpc_error;
407
  if (isset($code)) {
408 409 410 411
    $xmlrpc_error = new stdClass();
    $xmlrpc_error->is_error = TRUE;
    $xmlrpc_error->code = $code;
    $xmlrpc_error->message = $message;
412
  }
413 414 415
  elseif ($reset) {
    $xmlrpc_error = NULL;
  }
416
  return $xmlrpc_error;
417 418
}

419 420 421 422 423 424 425 426 427
/**
 * Converts an XML-RPC error object into XML.
 *
 * @param $xmlrpc_error
 *   The XML-RPC error object.
 *
 * @return string
 *   An XML representation of the error as an XML methodResponse.
 */
428
function xmlrpc_error_get_xml($xmlrpc_error) {
429
  return <<<EOD
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
<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;
448 449
}

450 451 452 453 454 455 456 457 458
/**
 * Converts a PHP or ISO date/time to an XML-RPC object.
 *
 * @param $time
 *   A PHP timestamp or an ISO date-time string.
 *
 * @return object
 *   An XML-RPC time/date object.
 */
459 460 461 462 463
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)) {
464 465 466 467 468 469 470
    $xmlrpc_date->year = gmdate('Y', $time);
    $xmlrpc_date->month = gmdate('m', $time);
    $xmlrpc_date->day = gmdate('d', $time);
    $xmlrpc_date->hour = gmdate('H', $time);
    $xmlrpc_date->minute = gmdate('i', $time);
    $xmlrpc_date->second = gmdate('s', $time);
    $xmlrpc_date->iso8601 = gmdate('Ymd\TH:i:s', $time);
471 472
  }
  else {
473
    $xmlrpc_date->iso8601 = $time;
474
    $time = str_replace(array('-', ':'), '', $time);
475 476 477 478
    $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);
479
    $xmlrpc_date->minute = substr($time, 11, 2);
480
    $xmlrpc_date->second = substr($time, 13, 2);
481 482
  }
  return $xmlrpc_date;
483 484
}

485 486 487 488 489 490 491 492 493
/**
 * Converts an XML-RPC date-time object into XML.
 *
 * @param $xmlrpc_date
 *   The XML-RPC date-time object.
 *
 * @return string
 *   An XML representation of the date/time as XML.
 */
494
function xmlrpc_date_get_xml($xmlrpc_date) {
495
  return '<dateTime.iso8601>' . $xmlrpc_date->year . $xmlrpc_date->month . $xmlrpc_date->day . 'T' . $xmlrpc_date->hour . ':' . $xmlrpc_date->minute . ':' . $xmlrpc_date->second . '</dateTime.iso8601>';
496
}
497

498 499 500 501 502 503 504 505 506
/**
 * Returns an XML-RPC base 64 object.
 *
 * @param $data
 *   Base 64 data to store in returned object.
 *
 * @return object
 *   An XML-RPC base 64 object.
 */
507 508 509 510 511 512
function xmlrpc_base64($data) {
  $xmlrpc_base64 = new stdClass();
  $xmlrpc_base64->is_base64 = TRUE;
  $xmlrpc_base64->data = $data;
  return $xmlrpc_base64;
}
513

514 515 516 517 518 519 520 521 522
/**
 * Converts an XML-RPC base 64 object into XML.
 *
 * @param $xmlrpc_base64
 *   The XML-RPC base 64 object.
 *
 * @return string
 *   An XML representation of the base 64 data as XML.
 */
523
function xmlrpc_base64_get_xml($xmlrpc_base64) {
524
  return '<base64>' . base64_encode($xmlrpc_base64->data) . '</base64>';
525 526
}

527
/**
528
 * Performs one or more XML-RPC requests.
529
 *
530
 * @param $url
531 532 533 534 535 536 537 538
 *   An absolute URL of the XML-RPC endpoint, e.g.,
 *   http://example.com/xmlrpc.php
 * @param $args
 *   An associative array whose keys are the methods to call and whose values
 *   are the arguments to pass to the respective method. If multiple methods
 *   are specified, a system.multicall is performed.
 * @param $options
 *   (optional) An array of options to pass along to drupal_http_request().
539
 *
540 541 542 543 544 545
 * @return
 *   A single response (single request) or an array of responses (multicall
 *   request). Each response is the return value of the method, just as if it
 *   has been a local function call, on success, or FALSE on failure. If FALSE
 *   is returned, see xmlrpc_errno() and xmlrpc_error_msg() to get more
 *   information.
546
 */
547
function _xmlrpc($url, $args, $options = array()) {
548
  xmlrpc_clear_error();
549
  if (count($args) > 1) {
550
    $multicall_args = array();
551 552
    foreach ($args as $method => $call) {
      $multicall_args[] = array('methodName' => $method, 'params' => $call);
553
    }
554
    $method = 'system.multicall';
555 556 557
    $args = array($multicall_args);
  }
  else {
558 559
    $method = key($args);
    $args = $args[$method];
560
  }
561
  $xmlrpc_request = xmlrpc_request($method, $args);
562 563 564 565
  // Required options which will replace any that are passed in.
  $options['method'] = 'POST';
  $options['headers']['Content-Type'] = 'text/xml';
  $options['data'] = $xmlrpc_request->xml;
566
  $result = drupal_http_request($url, $options);
567
  if ($result->code != 200) {
568
    xmlrpc_error($result->code, $result->error);
569 570 571 572 573 574
    return FALSE;
  }
  $message = xmlrpc_message($result->data);
  // Now parse what we've got back
  if (!xmlrpc_message_parse($message)) {
    // XML error
575
    xmlrpc_error(-32700, t('Parse error. Not well formed'));
576 577 578 579
    return FALSE;
  }
  // Is the message a fault?
  if ($message->messagetype == 'fault') {
580
    xmlrpc_error($message->fault_code, $message->fault_string);
581 582
    return FALSE;
  }
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599
  // We now know that the message is well-formed and a non-fault result.
  if ($method == 'system.multicall') {
    // Return per-method results or error objects.
    $return = array();
    foreach ($message->params[0] as $result) {
      if (array_keys($result) == array(0)) {
        $return[] = $result[0];
      }
      else {
        $return[] = xmlrpc_error($result['faultCode'], $result['faultString']);
      }
    }
  }
  else {
    $return = $message->params[0];
  }
  return $return;
600
}
601

602
/**
603
 * Returns the last XML-RPC client error number.
604 605 606
 */
function xmlrpc_errno() {
  $error = xmlrpc_error();
607
  return ($error != NULL ? $error->code : NULL);
608 609
}

610
/**
611
 * Returns the last XML-RPC client error message.
612 613 614
 */
function xmlrpc_error_msg() {
  $error = xmlrpc_error();
615
  return ($error != NULL ? $error->message : NULL);
616
}
617 618

/**
619 620 621
 * Clears any previously-saved errors.
 *
 * @see xmlrpc_error()
622 623 624 625
 */
function xmlrpc_clear_error() {
  xmlrpc_error(NULL, NULL, TRUE);
}
626