xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision e9472a985ab65f69aeef6868ad4c301bd76c0685)
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        if (!$conf['remote']) {
32            throw new ServerException("XML-RPC server not enabled.", -32605);
33        }
34        if (!empty($conf['remotecors'])) {
35            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
36        }
37        if (
38            !isset($_SERVER['CONTENT_TYPE']) ||
39            (
40                strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
41                strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
42            )
43        ) {
44            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
45        }
46
47        parent::serve($data);
48    }
49
50    /**
51     * @inheritdoc
52     */
53    protected function call($methodname, $args)
54    {
55        try {
56            $result = $this->remote->call($methodname, $args);
57            return $result;
58        } catch (AccessDeniedException $e) {
59            if (!isset($_SERVER['REMOTE_USER'])) {
60                http_status(401);
61                return new Error(-32603, "server error. not authorized to call method $methodname");
62            } else {
63                http_status(403);
64                return new Error(-32604, "server error. forbidden to call the method $methodname");
65            }
66        } catch (RemoteException $e) {
67            return new Error($e->getCode(), $e->getMessage());
68        }
69    }
70}
71