File: XmlRpcServer.php

package info (click to toggle)
dokuwiki 2024-02-06b%2Bdfsg-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 24,624 kB
  • sloc: php: 97,851; javascript: 3,724; sh: 599; makefile: 70; xml: 34
file content (70 lines) | stat: -rw-r--r-- 1,970 bytes parent folder | download
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
<?php

namespace dokuwiki\Remote;

use IXR\DataType\Base64;
use IXR\DataType\Date;
use IXR\Exception\ServerException;
use IXR\Message\Error;
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();
        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']);
        }
        if (
            !isset($_SERVER['CONTENT_TYPE']) ||
            (
                strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
                strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
            )
        ) {
            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
        }

        parent::serve($data);
    }

    /**
     * @inheritdoc
     */
    protected 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 Error(-32603, "server error. not authorized to call method $methodname");
            } else {
                http_status(403);
                return new Error(-32604, "server error. forbidden to call the method $methodname");
            }
        } catch (RemoteException $e) {
            return new Error($e->getCode(), $e->getMessage());
        }
    }
}