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