aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Remote/XmlRpcServer.php
blob: a1566b3d208af9ac586c2685309eb7af5d1a4093 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php

namespace dokuwiki\Remote;

use IXR\DataType\Base64;
use IXR\DataType\Date;
use IXR\Exception\ServerException;
use IXR\Message\Error;
use IXR\Server\Server;

/**
 * Contains needed wrapper functions and registers all available XMLRPC functions.
 */
class XmlRpcServer extends Server
{
    protected $remote;

    /**
     * Constructor. Register methods and run Server
     */
    public function __construct($wait = false)
    {
        $this->remote = new Api();
        parent::__construct(false, false, $wait);
    }

    /** @inheritdoc */
    public function serve($data = false)
    {
        global $conf;
        global $INPUT;
        if (!$conf['remote']) {
            throw new ServerException("XML-RPC server not enabled.", -32605);
        }
        if (!empty($conf['remotecors'])) {
            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
        }
        [$contentType] = explode(';', $INPUT->server->str('CONTENT_TYPE'), 2); // ignore charset
        $contentType = strtolower($contentType); // mime types are case-insensitive
        if ($contentType !== 'text/xml' && $contentType !== 'application/xml') {
            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
        }

        parent::serve($data);
    }

    /**
     * @inheritdoc
     */
    protected function call($methodname, $args)
    {
        try {
            $result = $this->remote->call($methodname, $args);
            return $result;
        } catch (AccessDeniedException $e) {
            if (!isset($_SERVER['REMOTE_USER'])) {
                http_status(401);
                return new Error(-32603, "server error. not authorized to call method $methodname");
            } else {
                http_status(403);
                return new Error(-32604, "server error. forbidden to call the method $methodname");
            }
        } catch (RemoteException $e) {
            return new Error($e->getCode(), $e->getMessage());
        }
    }
}