xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision cc7691adb9a9fb131cdc07a53e826dbcd58bf0ad)
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        $this->remote->setDateTransformation(array($this, 'toDate'));
24        $this->remote->setFileTransformation(array($this, 'toFile'));
25        parent::__construct(false, false, $wait);
26    }
27
28    /** @inheritdoc */
29    public function serve($data = false)
30    {
31        global $conf;
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        if (
39            !isset($_SERVER['CONTENT_TYPE']) ||
40            ($_SERVER['CONTENT_TYPE'] !== 'text/xml' && $_SERVER['CONTENT_TYPE'] !== 'application/xml')
41        ) {
42            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
43        }
44
45        parent::serve($data);
46    }
47
48    /**
49     * @inheritdoc
50     */
51    public function call($methodname, $args)
52    {
53        try {
54            $result = $this->remote->call($methodname, $args);
55            return $result;
56        } catch (AccessDeniedException $e) {
57            if (!isset($_SERVER['REMOTE_USER'])) {
58                http_status(401);
59                return new ServerException("server error. not authorized to call method $methodname", -32603);
60            } else {
61                http_status(403);
62                return new ServerException("server error. forbidden to call the method $methodname", -32604);
63            }
64        } catch (RemoteException $e) {
65            return new ServerException($e->getMessage(), $e->getCode());
66        }
67    }
68
69    /**
70     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
71     * @return Date
72     */
73    public function toDate($data)
74    {
75        return new Date($data);
76    }
77
78    /**
79     * @param string $data
80     * @return Base64
81     */
82    public function toFile($data)
83    {
84        return new Base64($data);
85    }
86}
87