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
39        parent::serve($data);
40    }
41
42    /**
43     * @inheritdoc
44     */
45    public function call($methodname, $args)
46    {
47        try {
48            $result = $this->remote->call($methodname, $args);
49            return $result;
50        } catch (AccessDeniedException $e) {
51            if (!isset($_SERVER['REMOTE_USER'])) {
52                http_status(401);
53                return new ServerException("server error. not authorized to call method $methodname", -32603);
54            } else {
55                http_status(403);
56                return new ServerException("server error. forbidden to call the method $methodname", -32604);
57            }
58        } catch (RemoteException $e) {
59            return new ServerException($e->getMessage(), $e->getCode());
60        }
61    }
62
63    /**
64     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
65     * @return Date
66     */
67    public function toDate($data)
68    {
69        return new Date($data);
70    }
71
72    /**
73     * @param string $data
74     * @return Base64
75     */
76    public function toFile($data)
77    {
78        return new Base64($data);
79    }
80}
81