xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision 0e0fd3b7b5152356c1ef84db24eabbae0d18b38b)
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        $this->remote->setDateTransformation(array($this, 'toDate'));
25        $this->remote->setFileTransformation(array($this, 'toFile'));
26        parent::__construct(false, false, $wait);
27    }
28
29    /**
30     * @inheritdoc
31     */
32    public function call($methodname, $args)
33    {
34        try {
35            $result = $this->remote->call($methodname, $args);
36            return $result;
37        } catch (AccessDeniedException $e) {
38            if (!isset($_SERVER['REMOTE_USER'])) {
39                http_status(401);
40                return new Error(-32603, "server error. not authorized to call method $methodname");
41            } else {
42                http_status(403);
43                return new Error(-32604, "server error. forbidden to call the method $methodname");
44            }
45        } catch (RemoteException $e) {
46            return new Error($e->getCode(), $e->getMessage());
47        }
48    }
49
50    /**
51     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
52     * @return Date
53     */
54    public function toDate($data)
55    {
56        return new Date($data);
57    }
58
59    /**
60     * @param string $data
61     * @return Base64
62     */
63    public function toFile($data)
64    {
65        return new Base64($data);
66    }
67}
68