xmlrpc.inc 15.5 KB
Newer Older
Dries's avatar
 
Dries committed
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
 */
Dries's avatar
 
Dries committed
14

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

84 85 86 87
/**
 * Generate XML representing the given value.
 *
 * @param $xmlrpc_value
88
 *   A value to be represented in XML.
89 90 91
 * @return
 *   XML representation of value.
 */
92 93 94
function xmlrpc_value_get_xml($xmlrpc_value) {
  switch ($xmlrpc_value->type) {
    case 'boolean':
95
      return '<boolean>' . (($xmlrpc_value->data) ? '1' : '0') . '</boolean>';
96 97
      break;
    case 'int':
98
      return '<int>' . $xmlrpc_value->data . '</int>';
99 100
      break;
    case 'double':
101
      return '<double>' . $xmlrpc_value->data . '</double>';
102 103
      break;
    case 'string':
104 105
      // Note: we don't escape apostrophes because of the many blogging clients
      // that don't support numerical entities (and XML in general) properly.
106
      return '<string>' . htmlspecialchars($xmlrpc_value->data) . '</string>';
107 108
      break;
    case 'array':
109
      $return = '<array><data>' . "\n";
110
      foreach ($xmlrpc_value->data as $item) {
111
        $return .= '  <value>' . xmlrpc_value_get_xml($item) . "</value>\n";
112 113 114 115 116
      }
      $return .= '</data></array>';
      return $return;
      break;
    case 'struct':
117
      $return = '<struct>' . "\n";
118
      foreach ($xmlrpc_value->data as $name => $value) {
119 120
        $return .= "  <member><name>" . check_plain($name) . "</name><value>";
        $return .= xmlrpc_value_get_xml($value) . "</value></member>\n";
121 122 123 124 125 126 127 128 129 130
      }
      $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
131
  }
132
  return FALSE;
Dries's avatar
 
Dries committed
133 134
}

135 136 137 138 139 140 141 142
/**
 * Construct an object representing an XML-RPC message.
 *
 * @param $message
 *   String containing XML as defined at http://www.xmlrpc.com/spec
 * @return
 *   Object
 */
143 144 145
function xmlrpc_message($message) {
  $xmlrpc_message = new stdClass();
  $xmlrpc_message->array_structs = array();   // The stack used to keep track of the current array/struct
146
  $xmlrpc_message->array_structs_types = array(); // The stack used to keep track of if things are structs or array
147 148 149
  $xmlrpc_message->current_struct_name = array();  // A stack as well
  $xmlrpc_message->message = $message;
  return $xmlrpc_message;
150 151
}

152
/**
153
 * Parses an XML-RPC message.
154 155 156
 *
 * If parsing fails, the faultCode and faultString will be added to the message
 * object.
157 158 159 160 161 162
 *
 * @param $xmlrpc_message
 *   Object generated by xmlrpc_message()
 * @return
 *   TRUE if parsing succeeded; FALSE otherwise
 */
163
function xmlrpc_message_parse($xmlrpc_message) {
164
  $xmlrpc_message->_parser = xml_parser_create();
165
  // Set XML parser to take the case of tags into account.
166 167 168 169 170 171 172 173 174
  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);
175 176

  // Grab the error messages, if any.
177
  $xmlrpc_message = xmlrpc_message_get();
178 179 180 181
  if (!isset($xmlrpc_message->messagetype)) {
    return FALSE;
  }
  elseif ($xmlrpc_message->messagetype == 'fault') {
182 183 184 185
    $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
186 187
}

188 189 190 191 192 193 194 195
/**
 * Store a copy of the $xmlrpc_message object temporarily.
 *
 * @param $value
 *   Object
 * @return
 *   The most recently stored $xmlrpc_message
 */
196 197 198 199
function xmlrpc_message_set($value = NULL) {
  static $xmlrpc_message;
  if ($value) {
    $xmlrpc_message = $value;
Dries's avatar
 
Dries committed
200
  }
201
  return $xmlrpc_message;
Dries's avatar
 
Dries committed
202 203
}

204 205 206
function xmlrpc_message_get() {
  return xmlrpc_message_set();
}
Dries's avatar
 
Dries committed
207

208 209 210
function xmlrpc_message_tag_open($parser, $tag, $attr) {
  $xmlrpc_message = xmlrpc_message_get();
  $xmlrpc_message->current_tag_contents = '';
211
  $xmlrpc_message->last_open = $tag;
212
  switch ($tag) {
213 214 215 216 217
    case 'methodCall':
    case 'methodResponse':
    case 'fault':
      $xmlrpc_message->messagetype = $tag;
      break;
218
    // Deal with stacks of arrays and structs
219 220 221 222 223 224 225 226
    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
227
  }
228
  xmlrpc_message_set($xmlrpc_message);
Dries's avatar
 
Dries committed
229 230
}

231 232 233 234 235
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
236

237 238 239
function xmlrpc_message_tag_close($parser, $tag) {
  $xmlrpc_message = xmlrpc_message_get();
  $value_flag = FALSE;
240
  switch ($tag) {
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
    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':
260
      // If no type is indicated, the type is string
261
      // We take special care for empty values
262
      if (trim($xmlrpc_message->current_tag_contents) != '' || (isset($xmlrpc_message->last_open) && ($xmlrpc_message->last_open == 'value'))) {
263
        $value = (string) $xmlrpc_message->current_tag_contents;
264 265 266
        $value_flag = TRUE;
      }
      unset($xmlrpc_message->last_open);
267 268 269 270 271 272 273 274 275
      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;
276
    // Deal with stacks of arrays and structs
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    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
292
  }
293 294 295 296 297 298
  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;
299 300
      }
      else {
301 302
        // Add to array
        $xmlrpc_message->array_structs [count($xmlrpc_message->array_structs )-1][] = $value;
Dries's avatar
 
Dries committed
303
      }
304 305
    }
    else {
306
      // Just add as a parameter
307
      $xmlrpc_message->params[] = $value;
Dries's avatar
 
Dries committed
308 309
    }
  }
310 311
  if (!in_array($tag, array("data", "struct", "member"))) {
    $xmlrpc_message->current_tag_contents = '';
Dries's avatar
 
Dries committed
312
  }
313 314
  xmlrpc_message_set($xmlrpc_message);
}
Dries's avatar
 
Dries committed
315

316 317 318 319 320 321 322 323 324 325
/**
 * Construct an object representing an XML-RPC request
 *
 * @param $method
 *   The name of the method to be called
 * @param $args
 *   An array of parameters to send with the method.
 * @return
 *   Object
 */
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
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
346 347


348
function xmlrpc_error($code = NULL, $message = NULL, $reset = FALSE) {
349
  static $xmlrpc_error;
350
  if (isset($code)) {
351 352 353 354
    $xmlrpc_error = new stdClass();
    $xmlrpc_error->is_error = TRUE;
    $xmlrpc_error->code = $code;
    $xmlrpc_error->message = $message;
Dries's avatar
 
Dries committed
355
  }
356 357 358
  elseif ($reset) {
    $xmlrpc_error = NULL;
  }
359
  return $xmlrpc_error;
Dries's avatar
 
Dries committed
360 361
}

362
function xmlrpc_error_get_xml($xmlrpc_error) {
363
  return <<<EOD
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
<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
382 383
}

384 385 386 387 388
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)) {
389 390 391 392 393 394 395
    $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);
396 397
  }
  else {
398
    $xmlrpc_date->iso8601 = $time;
399
    $time = str_replace(array('-', ':'), '', $time);
400 401 402 403
    $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);
404
    $xmlrpc_date->minute = substr($time, 11, 2);
405
    $xmlrpc_date->second = substr($time, 13, 2);
406 407
  }
  return $xmlrpc_date;
Dries's avatar
 
Dries committed
408 409
}

410
function xmlrpc_date_get_xml($xmlrpc_date) {
411
  return '<dateTime.iso8601>' . $xmlrpc_date->year . $xmlrpc_date->month . $xmlrpc_date->day . 'T' . $xmlrpc_date->hour . ':' . $xmlrpc_date->minute . ':' . $xmlrpc_date->second . '</dateTime.iso8601>';
412
}
Dries's avatar
 
Dries committed
413

414 415 416 417 418 419
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
420

421
function xmlrpc_base64_get_xml($xmlrpc_base64) {
422
  return '<base64>' . base64_encode($xmlrpc_base64->data) . '</base64>';
Dries's avatar
 
Dries committed
423 424
}

425
/**
426
 * Performs one or more XML-RPC request(s).
427
 *
428 429 430 431 432 433
 * @param $url
 *   An absolute URL of the XML-RPC endpoint.
 *     Example:
 *     http://www.example.com/xmlrpc.php
 * @param ...
 *   For one request:
434 435
 *     The method name followed by a variable number of arguments to the
 *     method.
436
 *   For multiple requests (system.multicall):
437 438
 *     An array of call arrays. Each call array follows the pattern of the
 *     single request: method name followed by the arguments to the method.
439
 * @return
440 441 442 443 444 445 446 447 448
 *   Either the return value of the method on success, or FALSE.  If FALSE is
 *   returned, see xmlrpc_errno() and xmlrpc_error_msg().
 *   - For a non-multicall request: the result just as if this had been a local
 *     function call.
 *   - For a multicall request: an array of results. Each result will either be
 *     a one-element array containing the result returned by the method called,
 *     or an xmlrpc_error object if the call failed.
 *
 * @see xmlrpc_error()
449
 */
450
function _xmlrpc() {
451 452
  $args = func_get_args();
  $url = array_shift($args);
453
  xmlrpc_clear_error();
454 455 456 457
  if (is_array($args[0])) {
    $method = 'system.multicall';
    $multicall_args = array();
    foreach ($args[0] as $call) {
458
      $multicall_args[] = array('methodName' => array_shift($call), 'params' => $call);
459 460 461 462 463 464
    }
    $args = array($multicall_args);
  }
  else {
    $method = array_shift($args);
  }
465
  $xmlrpc_request = xmlrpc_request($method, $args);
466 467 468 469 470 471
  $options = array(
    'headers' => array('Content-Type' => 'text/xml'),
    'method' => 'POST',
    'data' => $xmlrpc_request->xml,
  );
  $result = drupal_http_request($url, $options);
472
  if ($result->code != 200) {
473
    xmlrpc_error($result->code, $result->error);
474 475 476 477 478 479
    return FALSE;
  }
  $message = xmlrpc_message($result->data);
  // Now parse what we've got back
  if (!xmlrpc_message_parse($message)) {
    // XML error
480
    xmlrpc_error(-32700, t('Parse error. Not well formed'));
481 482 483 484
    return FALSE;
  }
  // Is the message a fault?
  if ($message->messagetype == 'fault') {
485
    xmlrpc_error($message->fault_code, $message->fault_string);
486 487
    return FALSE;
  }
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
  // 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;
505
}
Dries's avatar
 
Dries committed
506

507 508 509 510 511
/**
 * Returns the last XML-RPC client error number
 */
function xmlrpc_errno() {
  $error = xmlrpc_error();
512
  return ($error != NULL ? $error->code : NULL);
Dries's avatar
 
Dries committed
513 514
}

515 516 517 518 519
/**
 * Returns the last XML-RPC client error message
 */
function xmlrpc_error_msg() {
  $error = xmlrpc_error();
520
  return ($error != NULL ? $error->message : NULL);
521
}
522 523 524 525 526 527 528

/**
 * Clears any previous error.
 */
function xmlrpc_clear_error() {
  xmlrpc_error(NULL, NULL, TRUE);
}