code style: line breaks
[dokuwiki.git] / inc / Remote / XmlRpcServer.php
blobe41b5d79188482a24fa602ab07c9431fe58c6b60
1 <?php
3 namespace dokuwiki\Remote;
5 use IXR\DataType\Base64;
6 use IXR\DataType\Date;
7 use IXR\Exception\ServerException;
8 use IXR\Server\Server;
10 /**
11 * Contains needed wrapper functions and registers all available XMLRPC functions.
13 class XmlRpcServer extends Server
15 protected $remote;
17 /**
18 * Constructor. Register methods and run Server
20 public function __construct($wait = false)
22 $this->remote = new Api();
23 $this->remote->setDateTransformation([$this, 'toDate']);
24 $this->remote->setFileTransformation([$this, 'toFile']);
25 parent::__construct(false, false, $wait);
28 /** @inheritdoc */
29 public function serve($data = false)
31 global $conf;
32 if (!$conf['remote']) {
33 throw new ServerException("XML-RPC server not enabled.", -32605);
35 if (!empty($conf['remotecors'])) {
36 header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
38 if (
39 !isset($_SERVER['CONTENT_TYPE']) ||
41 strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
42 strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
44 ) {
45 throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
48 parent::serve($data);
51 /**
52 * @inheritdoc
54 protected function call($methodname, $args)
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);
67 } catch (RemoteException $e) {
68 return new ServerException($e->getMessage(), $e->getCode());
72 /**
73 * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
74 * @return Date
76 public function toDate($data)
78 return new Date($data);
81 /**
82 * @param string $data
83 * @return Base64
85 public function toFile($data)
87 return new Base64($data);