xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision d48c2b252a339bed693da46fae768d93f4b3fe41)
1<?php
2
3namespace dokuwiki\Remote;
4
5use IXR\DataType\Base64;
6use IXR\DataType\Date;
7use IXR\Exception\ServerException;
8use IXR\Server\Server;
9
10/**
11 * Contains needed wrapper functions and registers all available XMLRPC functions.
12 */
13class 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        parent::__construct(false, false, $wait);
24    }
25
26    /** @inheritdoc */
27    public function serve($data = false)
28    {
29        global $conf;
30        if (!$conf['remote']) {
31            throw new ServerException("XML-RPC server not enabled.", -32605);
32        }
33        if (!empty($conf['remotecors'])) {
34            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
35        }
36        if (
37            !isset($_SERVER['CONTENT_TYPE']) ||
38            (
39                strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
40                strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
41            )
42        ) {
43            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
44        }
45
46        parent::serve($data);
47    }
48
49    /**
50     * @inheritdoc
51     */
52    protected function call($methodname, $args)
53    {
54        try {
55            $result = $this->remote->call($methodname, $args);
56            return $result;
57        } catch (AccessDeniedException $e) {
58            if (!isset($_SERVER['REMOTE_USER'])) {
59                http_status(401);
60                return new ServerException("server error. not authorized to call method $methodname", -32603);
61            } else {
62                http_status(403);
63                return new ServerException("server error. forbidden to call the method $methodname", -32604);
64            }
65        } catch (RemoteException $e) {
66            return new ServerException($e->getMessage(), $e->getCode());
67        }
68    }
69}
70