1<?php
2
3namespace dokuwiki\Remote;
4
5/**
6 * Contains needed wrapper functions and registers all available XMLRPC functions.
7 */
8class XmlRpcServer extends \IXR_Server
9{
10    protected $remote;
11
12    /**
13     * Constructor. Register methods and run Server
14     */
15    public function __construct($wait=false)
16    {
17        $this->remote = new Api();
18        $this->remote->setDateTransformation(array($this, 'toDate'));
19        $this->remote->setFileTransformation(array($this, 'toFile'));
20        parent::__construct(false, false, $wait);
21    }
22
23    /**
24     * @inheritdoc
25     */
26    public function call($methodname, $args)
27    {
28        try {
29            $result = $this->remote->call($methodname, $args);
30            return $result;
31        } /** @noinspection PhpRedundantCatchClauseInspection */ catch (AccessDeniedException $e) {
32            if (!isset($_SERVER['REMOTE_USER'])) {
33                http_status(401);
34                return new \IXR_Error(-32603, "server error. not authorized to call method $methodname");
35            } else {
36                http_status(403);
37                return new \IXR_Error(-32604, "server error. forbidden to call the method $methodname");
38            }
39        } catch (RemoteException $e) {
40            return new \IXR_Error($e->getCode(), $e->getMessage());
41        }
42    }
43
44    /**
45     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
46     * @return \IXR_Date
47     */
48    public function toDate($data)
49    {
50        return new \IXR_Date($data);
51    }
52
53    /**
54     * @param string $data
55     * @return \IXR_Base64
56     */
57    public function toFile($data)
58    {
59        return new \IXR_Base64($data);
60    }
61}
62