xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision cc7691adb9a9fb131cdc07a53e826dbcd58bf0ad)
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;
87f8f2456SAndreas Gohruse IXR\Server\Server;
97f8f2456SAndreas Gohr
101cdd0090SAndreas Gohr/**
111cdd0090SAndreas Gohr * Contains needed wrapper functions and registers all available XMLRPC functions.
121cdd0090SAndreas Gohr */
137f8f2456SAndreas Gohrclass XmlRpcServer extends Server
141cdd0090SAndreas Gohr{
151cdd0090SAndreas Gohr    protected $remote;
161cdd0090SAndreas Gohr
171cdd0090SAndreas Gohr    /**
181cdd0090SAndreas Gohr     * Constructor. Register methods and run Server
191cdd0090SAndreas Gohr     */
20fe52a7caSMark Janssen    public function __construct($wait=false)
211cdd0090SAndreas Gohr    {
221cdd0090SAndreas Gohr        $this->remote = new Api();
231cdd0090SAndreas Gohr        $this->remote->setDateTransformation(array($this, 'toDate'));
241cdd0090SAndreas Gohr        $this->remote->setFileTransformation(array($this, 'toFile'));
25fe52a7caSMark Janssen        parent::__construct(false, false, $wait);
261cdd0090SAndreas Gohr    }
271cdd0090SAndreas Gohr
281498ac42SAndreas Gohr    /** @inheritdoc */
291498ac42SAndreas Gohr    public function serve($data = false)
301498ac42SAndreas Gohr    {
311498ac42SAndreas Gohr        global $conf;
321498ac42SAndreas Gohr        if (!$conf['remote']) {
331498ac42SAndreas Gohr            throw new ServerException("XML-RPC server not enabled.", -32605);
341498ac42SAndreas Gohr        }
353df364a3STimo Richter        if (!empty($conf['remotecors'])) {
363df364a3STimo Richter            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
37*cc7691adSAndreas Gohr        }
38*cc7691adSAndreas Gohr        if (
39*cc7691adSAndreas Gohr            !isset($_SERVER['CONTENT_TYPE']) ||
40*cc7691adSAndreas Gohr            ($_SERVER['CONTENT_TYPE'] !== 'text/xml' && $_SERVER['CONTENT_TYPE'] !== 'application/xml')
41*cc7691adSAndreas Gohr        ) {
42*cc7691adSAndreas Gohr            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
433df364a3STimo Richter        }
443df364a3STimo Richter
451498ac42SAndreas Gohr        parent::serve($data);
461498ac42SAndreas Gohr    }
471498ac42SAndreas Gohr
481cdd0090SAndreas Gohr    /**
491cdd0090SAndreas Gohr     * @inheritdoc
501cdd0090SAndreas Gohr     */
511cdd0090SAndreas Gohr    public function call($methodname, $args)
521cdd0090SAndreas Gohr    {
531cdd0090SAndreas Gohr        try {
541cdd0090SAndreas Gohr            $result = $this->remote->call($methodname, $args);
551cdd0090SAndreas Gohr            return $result;
567f8f2456SAndreas Gohr        } catch (AccessDeniedException $e) {
571cdd0090SAndreas Gohr            if (!isset($_SERVER['REMOTE_USER'])) {
581cdd0090SAndreas Gohr                http_status(401);
597f8f2456SAndreas Gohr                return new ServerException("server error. not authorized to call method $methodname", -32603);
601cdd0090SAndreas Gohr            } else {
611cdd0090SAndreas Gohr                http_status(403);
627f8f2456SAndreas Gohr                return new ServerException("server error. forbidden to call the method $methodname", -32604);
631cdd0090SAndreas Gohr            }
641cdd0090SAndreas Gohr        } catch (RemoteException $e) {
657f8f2456SAndreas Gohr            return new ServerException($e->getMessage(), $e->getCode());
661cdd0090SAndreas Gohr        }
671cdd0090SAndreas Gohr    }
681cdd0090SAndreas Gohr
691cdd0090SAndreas Gohr    /**
701cdd0090SAndreas Gohr     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
717f8f2456SAndreas Gohr     * @return Date
721cdd0090SAndreas Gohr     */
731cdd0090SAndreas Gohr    public function toDate($data)
741cdd0090SAndreas Gohr    {
757f8f2456SAndreas Gohr        return new Date($data);
761cdd0090SAndreas Gohr    }
771cdd0090SAndreas Gohr
781cdd0090SAndreas Gohr    /**
791cdd0090SAndreas Gohr     * @param string $data
807f8f2456SAndreas Gohr     * @return Base64
811cdd0090SAndreas Gohr     */
821cdd0090SAndreas Gohr    public function toFile($data)
831cdd0090SAndreas Gohr    {
847f8f2456SAndreas Gohr        return new Base64($data);
851cdd0090SAndreas Gohr    }
861cdd0090SAndreas Gohr}
87