[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/IXR/ -> class-IXR-server.php (source)

   1  <?php
   2  
   3  /**
   4   * IXR_Server
   5   *
   6   * @package IXR
   7   * @since 1.5.0
   8   */
   9  class IXR_Server
  10  {
  11      var $data;
  12      var $callbacks = array();
  13      var $message;
  14      var $capabilities;
  15  
  16      /**
  17       * PHP5 constructor.
  18       */
  19      function __construct( $callbacks = false, $data = false, $wait = false )
  20      {
  21          $this->setCapabilities();
  22          if ($callbacks) {
  23              $this->callbacks = $callbacks;
  24          }
  25          $this->setCallbacks();
  26          if (!$wait) {
  27              $this->serve($data);
  28          }
  29      }
  30  
  31      /**
  32       * PHP4 constructor.
  33       */
  34  	public function IXR_Server( $callbacks = false, $data = false, $wait = false ) {
  35          self::__construct( $callbacks, $data, $wait );
  36      }
  37  
  38      function serve($data = false)
  39      {
  40          if (!$data) {
  41              if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD'] !== 'POST') {
  42                  if ( function_exists( 'status_header' ) ) {
  43                      status_header( 405 ); // WP #20986
  44                      header( 'Allow: POST' );
  45                  }
  46                  header('Content-Type: text/plain'); // merged from WP #9093
  47                  die('XML-RPC server accepts POST requests only.');
  48              }
  49  
  50              $data = file_get_contents('php://input');
  51          }
  52          $this->message = new IXR_Message($data);
  53          if (!$this->message->parse()) {
  54              $this->error(-32700, 'parse error. not well formed');
  55          }
  56          if ($this->message->messageType != 'methodCall') {
  57              $this->error(-32600, 'server error. invalid xml-rpc. not conforming to spec. Request must be a methodCall');
  58          }
  59          $result = $this->call($this->message->methodName, $this->message->params);
  60  
  61          // Is the result an error?
  62          if (is_a($result, 'IXR_Error')) {
  63              $this->error($result);
  64          }
  65  
  66          // Encode the result
  67          $r = new IXR_Value($result);
  68          $resultxml = $r->getXml();
  69  
  70          // Create the XML
  71          $xml = <<<EOD
  72  <methodResponse>
  73    <params>
  74      <param>
  75        <value>
  76        $resultxml
  77        </value>
  78      </param>
  79    </params>
  80  </methodResponse>
  81  
  82  EOD;
  83        // Send it
  84        $this->output($xml);
  85      }
  86  
  87      function call($methodname, $args)
  88      {
  89          if (!$this->hasMethod($methodname)) {
  90              return new IXR_Error(-32601, 'server error. requested method '.$methodname.' does not exist.');
  91          }
  92          $method = $this->callbacks[$methodname];
  93  
  94          // Perform the callback and send the response
  95          if (count($args) == 1) {
  96              // If only one parameter just send that instead of the whole array
  97              $args = $args[0];
  98          }
  99  
 100          // Are we dealing with a function or a method?
 101          if (is_string($method) && substr($method, 0, 5) == 'this:') {
 102              // It's a class method - check it exists
 103              $method = substr($method, 5);
 104              if (!method_exists($this, $method)) {
 105                  return new IXR_Error(-32601, 'server error. requested class method "'.$method.'" does not exist.');
 106              }
 107  
 108              //Call the method
 109              $result = $this->$method($args);
 110          } else {
 111              // It's a function - does it exist?
 112              if (is_array($method)) {
 113                  if (!is_callable(array($method[0], $method[1]))) {
 114                      return new IXR_Error(-32601, 'server error. requested object method "'.$method[1].'" does not exist.');
 115                  }
 116              } else if (!function_exists($method)) {
 117                  return new IXR_Error(-32601, 'server error. requested function "'.$method.'" does not exist.');
 118              }
 119  
 120              // Call the function
 121              $result = call_user_func($method, $args);
 122          }
 123          return $result;
 124      }
 125  
 126      function error($error, $message = false)
 127      {
 128          // Accepts either an error object or an error code and message
 129          if ($message && !is_object($error)) {
 130              $error = new IXR_Error($error, $message);
 131          }
 132  
 133          if ( function_exists( 'status_header' ) ) {
 134              status_header( $error->code );
 135          }
 136  
 137          $this->output($error->getXml());
 138      }
 139  
 140      function output($xml)
 141      {
 142          $charset = function_exists('get_option') ? get_option('blog_charset') : '';
 143          if ($charset)
 144              $xml = '<?xml version="1.0" encoding="'.$charset.'"?>'."\n".$xml;
 145          else
 146              $xml = '<?xml version="1.0"?>'."\n".$xml;
 147          $length = strlen($xml);
 148          header('Connection: close');
 149          if ($charset)
 150              header('Content-Type: text/xml; charset='.$charset);
 151          else
 152              header('Content-Type: text/xml');
 153          header('Date: '.gmdate('r'));
 154          echo $xml;
 155          exit;
 156      }
 157  
 158      function hasMethod($method)
 159      {
 160          return in_array($method, array_keys($this->callbacks));
 161      }
 162  
 163      function setCapabilities()
 164      {
 165          // Initialises capabilities array
 166          $this->capabilities = array(
 167              'xmlrpc' => array(
 168                  'specUrl' => 'http://www.xmlrpc.com/spec',
 169                  'specVersion' => 1
 170          ),
 171              'faults_interop' => array(
 172                  'specUrl' => 'http://xmlrpc-epi.sourceforge.net/specs/rfc.fault_codes.php',
 173                  'specVersion' => 20010516
 174          ),
 175              'system.multicall' => array(
 176                  'specUrl' => 'http://www.xmlrpc.com/discuss/msgReader$1208',
 177                  'specVersion' => 1
 178          ),
 179          );
 180      }
 181  
 182      function getCapabilities($args)
 183      {
 184          return $this->capabilities;
 185      }
 186  
 187      function setCallbacks()
 188      {
 189          $this->callbacks['system.getCapabilities'] = 'this:getCapabilities';
 190          $this->callbacks['system.listMethods'] = 'this:listMethods';
 191          $this->callbacks['system.multicall'] = 'this:multiCall';
 192      }
 193  
 194      function listMethods($args)
 195      {
 196          // Returns a list of methods - uses array_reverse to ensure user defined
 197          // methods are listed before server defined methods
 198          return array_reverse(array_keys($this->callbacks));
 199      }
 200  
 201      function multiCall($methodcalls)
 202      {
 203          // See http://www.xmlrpc.com/discuss/msgReader$1208
 204          $return = array();
 205          foreach ($methodcalls as $call) {
 206              $method = $call['methodName'];
 207              $params = $call['params'];
 208              if ($method == 'system.multicall') {
 209                  $result = new IXR_Error(-32600, 'Recursive calls to system.multicall are forbidden');
 210              } else {
 211                  $result = $this->call($method, $params);
 212              }
 213              if (is_a($result, 'IXR_Error')) {
 214                  $return[] = array(
 215                      'faultCode' => $result->code,
 216                      'faultString' => $result->message
 217                  );
 218              } else {
 219                  $return[] = array($result);
 220              }
 221          }
 222          return $return;
 223      }
 224  }


Generated: Mon Mar 8 01:00:04 2021 Cross-referenced by PHPXref 0.7.1