xmlrpc.inc 17.3 KB
Newer Older
Dries's avatar
Dries committed
1
<?php
2

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

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

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

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

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

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

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

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

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

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

132 133
    case 'base64':
      return xmlrpc_base64_get_xml($xmlrpc_value->data);
Dries's avatar
Dries committed
134
  }
135
  return FALSE;
Dries's avatar
Dries committed
136 137
}

138
/**
139
 * Constructs an object representing an XML-RPC message.
140 141
 *
 * @param $message
142 143 144 145 146 147
 *   A string containing an XML message.
 *
 * @return object
 *   An XML-RPC object containing the message.
 *
 * @see http://www.xmlrpc.com/spec
148
 */
149 150
function xmlrpc_message($message) {
  $xmlrpc_message = new stdClass();
151 152 153 154 155 156
  // 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();
157 158
  $xmlrpc_message->message = $message;
  return $xmlrpc_message;
159 160
}

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

  // Grab the error messages, if any.
187
  $xmlrpc_message = xmlrpc_message_get();
188 189 190 191
  if (!isset($xmlrpc_message->messagetype)) {
    return FALSE;
  }
  elseif ($xmlrpc_message->messagetype == 'fault') {
192 193 194 195
    $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
196 197
}

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

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

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

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

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

257 258 259
/**
 * Handles character data for XML parsing in xmlrpc_message_parse().
 */
260 261 262 263 264
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
265

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

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

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

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

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

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

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

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

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

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

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

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

390 391 392 393 394 395 396 397 398 399 400 401 402 403
/**
 * 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.
 */
404
function xmlrpc_error($code = NULL, $message = NULL, $reset = FALSE) {
405
  static $xmlrpc_error;
406
  if (isset($code)) {
407 408 409 410
    $xmlrpc_error = new stdClass();
    $xmlrpc_error->is_error = TRUE;
    $xmlrpc_error->code = $code;
    $xmlrpc_error->message = $message;
Dries's avatar
Dries committed
411
  }
412 413 414
  elseif ($reset) {
    $xmlrpc_error = NULL;
  }
415
  return $xmlrpc_error;
Dries's avatar
Dries committed
416 417
}

418 419 420 421 422 423 424 425 426
/**
 * 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.
 */
427
function xmlrpc_error_get_xml($xmlrpc_error) {
428
  return <<<EOD
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446
<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
447 448
}

449 450 451 452 453 454 455 456 457
/**
 * 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.
 */
458 459 460 461 462
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)) {
463 464 465 466 467 468 469
    $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);
470 471
  }
  else {
472
    $xmlrpc_date->iso8601 = $time;
473
    $time = str_replace(array('-', ':'), '', $time);
474 475 476 477
    $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);
478
    $xmlrpc_date->minute = substr($time, 11, 2);
479
    $xmlrpc_date->second = substr($time, 13, 2);
480 481
  }
  return $xmlrpc_date;
Dries's avatar
Dries committed
482 483
}

484 485 486 487 488 489 490 491 492
/**
 * 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.
 */
493
function xmlrpc_date_get_xml($xmlrpc_date) {
494
  return '<dateTime.iso8601>' . $xmlrpc_date->year . $xmlrpc_date->month . $xmlrpc_date->day . 'T' . $xmlrpc_date->hour . ':' . $xmlrpc_date->minute . ':' . $xmlrpc_date->second . '</dateTime.iso8601>';
495
}
Dries's avatar
Dries committed
496

497 498 499 500 501 502 503 504 505
/**
 * 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.
 */
506 507 508 509 510 511
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
512

513 514 515 516 517 518 519 520 521
/**
 * 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.
 */
522
function xmlrpc_base64_get_xml($xmlrpc_base64) {
523
  return '<base64>' . base64_encode($xmlrpc_base64->data) . '</base64>';
Dries's avatar
Dries committed
524 525
}

526
/**
527
 * Performs one or more XML-RPC requests.
528
 *
529
 * @param $url
530 531 532 533 534 535 536 537
 *   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().
538
 *
539 540 541 542 543 544
 * @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.
545
 */
546
function _xmlrpc($url, $args, $options = array()) {
547
  xmlrpc_clear_error();
548
  if (count($args) > 1) {
549
    $multicall_args = array();
550 551
    foreach ($args as $method => $call) {
      $multicall_args[] = array('methodName' => $method, 'params' => $call);
552
    }
553
    $method = 'system.multicall';
554 555 556
    $args = array($multicall_args);
  }
  else {
557 558
    $method = key($args);
    $args = $args[$method];
559
  }
560
  $xmlrpc_request = xmlrpc_request($method, $args);
561 562 563 564
  // Required options which will replace any that are passed in.
  $options['method'] = 'POST';
  $options['headers']['Content-Type'] = 'text/xml';
  $options['data'] = $xmlrpc_request->xml;
565
  $result = drupal_http_request($url, $options);
566
  if ($result->code != 200) {
567
    xmlrpc_error($result->code, $result->error);
568 569 570 571 572 573
    return FALSE;
  }
  $message = xmlrpc_message($result->data);
  // Now parse what we've got back
  if (!xmlrpc_message_parse($message)) {
    // XML error
574
    xmlrpc_error(-32700, t('Parse error. Not well formed'));
575 576 577 578
    return FALSE;
  }
  // Is the message a fault?
  if ($message->messagetype == 'fault') {
579
    xmlrpc_error($message->fault_code, $message->fault_string);
580 581
    return FALSE;
  }
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
  // 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;
599
}
Dries's avatar
Dries committed
600

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

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

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