[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/inc/Remote/ -> XmlRpcServer.php (source)

   1  <?php
   2  
   3  namespace dokuwiki\Remote;
   4  
   5  use IXR\DataType\Base64;
   6  use IXR\DataType\Date;
   7  use IXR\Exception\ServerException;
   8  use IXR\Server\Server;
   9  
  10  /**
  11   * Contains needed wrapper functions and registers all available XMLRPC functions.
  12   */
  13  class XmlRpcServer extends Server
  14  {
  15      protected $remote;
  16  
  17      /**
  18       * Constructor. Register methods and run Server
  19       */
  20      public function __construct($wait = false)
  21      {
  22          $this->remote = new Api();
  23          $this->remote->setDateTransformation([$this, 'toDate']);
  24          $this->remote->setFileTransformation([$this, 'toFile']);
  25          parent::__construct(false, false, $wait);
  26      }
  27  
  28      /** @inheritdoc */
  29      public function serve($data = false)
  30      {
  31          global $conf;
  32          if (!$conf['remote']) {
  33              throw new ServerException("XML-RPC server not enabled.", -32605);
  34          }
  35          if (!empty($conf['remotecors'])) {
  36              header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
  37          }
  38          if (
  39              !isset($_SERVER['CONTENT_TYPE']) ||
  40              (
  41                  strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
  42                  strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
  43              )
  44          ) {
  45              throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
  46          }
  47  
  48          parent::serve($data);
  49      }
  50  
  51      /**
  52       * @inheritdoc
  53       */
  54      protected function call($methodname, $args)
  55      {
  56          try {
  57              $result = $this->remote->call($methodname, $args);
  58              return $result;
  59          } catch (AccessDeniedException $e) {
  60              if (!isset($_SERVER['REMOTE_USER'])) {
  61                  http_status(401);
  62                  return new ServerException("server error. not authorized to call method $methodname", -32603);
  63              } else {
  64                  http_status(403);
  65                  return new ServerException("server error. forbidden to call the method $methodname", -32604);
  66              }
  67          } catch (RemoteException $e) {
  68              return new ServerException($e->getMessage(), $e->getCode());
  69          }
  70      }
  71  
  72      /**
  73       * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
  74       * @return Date
  75       */
  76      public function toDate($data)
  77      {
  78          return new Date($data);
  79      }
  80  
  81      /**
  82       * @param string $data
  83       * @return Base64
  84       */
  85      public function toFile($data)
  86      {
  87          return new Base64($data);
  88      }
  89  }