aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/inc/Remote/XmlRpcServer.php
blob: fec958ca79cf59c800113404007eaf37e1e5185d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php

namespace dokuwiki\Remote;

use IXR\DataType\Base64;
use IXR\DataType\Date;
use IXR\Exception\ServerException;
use IXR\Server\Server;

/**
 * Contains needed wrapper functions and registers all available XMLRPC functions.
 */
class XmlRpcServer extends Server
{
    protected $remote;

    /**
     * Constructor. Register methods and run Server
     */
    public function __construct($wait=false)
    {
        $this->remote = new Api();
        $this->remote->setDateTransformation(array($this, 'toDate'));
        $this->remote->setFileTransformation(array($this, 'toFile'));
        parent::__construct(false, false, $wait);
    }

    /** @inheritdoc  */
    public function serve($data = false)
    {
        global $conf;
        if (!$conf['remote']) {
            throw new ServerException("XML-RPC server not enabled.", -32605);
        }
        if (!empty($conf['remotecors'])) {
            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
        }

        parent::serve($data);
    }

    /**
     * @inheritdoc
     */
    public function call($methodname, $args)
    {
        try {
            $result = $this->remote->call($methodname, $args);
            return $result;
        } catch (AccessDeniedException $e) {
            if (!isset($_SERVER['REMOTE_USER'])) {
                http_status(401);
                return new ServerException("server error. not authorized to call method $methodname", -32603);
            } else {
                http_status(403);
                return new ServerException("server error. forbidden to call the method $methodname", -32604);
            }
        } catch (RemoteException $e) {
            return new ServerException($e->getMessage(), $e->getCode());
        }
    }

    /**
     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
     * @return Date
     */
    public function toDate($data)
    {
        return new Date($data);
    }

    /**
     * @param string $data
     * @return Base64
     */
    public function toFile($data)
    {
        return new Base64($data);
    }
}