1<?php
2
3namespace dokuwiki\Remote;
4
5use IXR\DataType\Base64;
6use IXR\DataType\Date;
7use IXR\Exception\ServerException;
8use IXR\Message\Error;
9use IXR\Server\Server;
10
11/**
12 * Contains needed wrapper functions and registers all available XMLRPC functions.
13 */
14class XmlRpcServer extends Server
15{
16    protected $remote;
17
18    /**
19     * Constructor. Register methods and run Server
20     */
21    public function __construct($wait = false)
22    {
23        $this->remote = new Api();
24        parent::__construct(false, false, $wait);
25    }
26
27    /** @inheritdoc */
28    public function serve($data = false)
29    {
30        global $conf;
31        global $INPUT;
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        [$contentType] = explode(';', $INPUT->server->str('CONTENT_TYPE'), 2); // ignore charset
39        $contentType = strtolower($contentType); // mime types are case-insensitive
40        if ($contentType !== 'text/xml' && $contentType !== 'application/xml') {
41            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
42        }
43
44        parent::serve($data);
45    }
46
47    /**
48     * @inheritdoc
49     */
50    protected function call($methodname, $args)
51    {
52        try {
53            $result = $this->remote->call($methodname, $args);
54            return $result;
55        } catch (AccessDeniedException $e) {
56            if (!isset($_SERVER['REMOTE_USER'])) {
57                http_status(401);
58                return new Error(-32603, "server error. not authorized to call method $methodname");
59            } else {
60                http_status(403);
61                return new Error(-32604, "server error. forbidden to call the method $methodname");
62            }
63        } catch (RemoteException $e) {
64            return new Error($e->getCode(), $e->getMessage());
65        }
66    }
67}
68