xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision 4385690f2a92b6ccf5d1a28cfe27e575c49d574d)
11cdd0090SAndreas Gohr<?php
21cdd0090SAndreas Gohr
31cdd0090SAndreas Gohrnamespace dokuwiki\Remote;
41cdd0090SAndreas Gohr
57f8f2456SAndreas Gohruse IXR\DataType\Base64;
67f8f2456SAndreas Gohruse IXR\DataType\Date;
77f8f2456SAndreas Gohruse IXR\Exception\ServerException;
8*4385690fSAndreas Gohruse IXR\Message\Error;
97f8f2456SAndreas Gohruse IXR\Server\Server;
107f8f2456SAndreas Gohr
111cdd0090SAndreas Gohr/**
121cdd0090SAndreas Gohr * Contains needed wrapper functions and registers all available XMLRPC functions.
131cdd0090SAndreas Gohr */
147f8f2456SAndreas Gohrclass XmlRpcServer extends Server
151cdd0090SAndreas Gohr{
161cdd0090SAndreas Gohr    protected $remote;
171cdd0090SAndreas Gohr
181cdd0090SAndreas Gohr    /**
191cdd0090SAndreas Gohr     * Constructor. Register methods and run Server
201cdd0090SAndreas Gohr     */
21fe52a7caSMark Janssen    public function __construct($wait = false)
221cdd0090SAndreas Gohr    {
231cdd0090SAndreas Gohr        $this->remote = new Api();
24fe52a7caSMark Janssen        parent::__construct(false, false, $wait);
251cdd0090SAndreas Gohr    }
261cdd0090SAndreas Gohr
271498ac42SAndreas Gohr    /** @inheritdoc */
281498ac42SAndreas Gohr    public function serve($data = false)
291498ac42SAndreas Gohr    {
301498ac42SAndreas Gohr        global $conf;
311498ac42SAndreas Gohr        if (!$conf['remote']) {
321498ac42SAndreas Gohr            throw new ServerException("XML-RPC server not enabled.", -32605);
331498ac42SAndreas Gohr        }
343df364a3STimo Richter        if (!empty($conf['remotecors'])) {
353df364a3STimo Richter            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
36cc7691adSAndreas Gohr        }
37cc7691adSAndreas Gohr        if (
38cc7691adSAndreas Gohr            !isset($_SERVER['CONTENT_TYPE']) ||
3920264973SAndreas Gohr            (
4020264973SAndreas Gohr                strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
4120264973SAndreas Gohr                strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
4220264973SAndreas Gohr            )
43cc7691adSAndreas Gohr        ) {
44cc7691adSAndreas Gohr            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
453df364a3STimo Richter        }
463df364a3STimo Richter
471498ac42SAndreas Gohr        parent::serve($data);
481498ac42SAndreas Gohr    }
491498ac42SAndreas Gohr
501cdd0090SAndreas Gohr    /**
511cdd0090SAndreas Gohr     * @inheritdoc
521cdd0090SAndreas Gohr     */
53104a3b7cSAndreas Gohr    protected function call($methodname, $args)
541cdd0090SAndreas Gohr    {
551cdd0090SAndreas Gohr        try {
561cdd0090SAndreas Gohr            $result = $this->remote->call($methodname, $args);
571cdd0090SAndreas Gohr            return $result;
587f8f2456SAndreas Gohr        } catch (AccessDeniedException $e) {
591cdd0090SAndreas Gohr            if (!isset($_SERVER['REMOTE_USER'])) {
601cdd0090SAndreas Gohr                http_status(401);
61*4385690fSAndreas Gohr                return new Error(-32603, "server error. not authorized to call method $methodname");
621cdd0090SAndreas Gohr            } else {
631cdd0090SAndreas Gohr                http_status(403);
64*4385690fSAndreas Gohr                return new Error(-32604, "server error. forbidden to call the method $methodname");
651cdd0090SAndreas Gohr            }
661cdd0090SAndreas Gohr        } catch (RemoteException $e) {
67*4385690fSAndreas Gohr            return new Error($e->getCode(), $e->getMessage());
681cdd0090SAndreas Gohr        }
691cdd0090SAndreas Gohr    }
701cdd0090SAndreas Gohr}
71