xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision 104a3b7cee0d6263c9886c95da6cd5d929f79a06)
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();
23*104a3b7cSAndreas Gohr        $this->remote->setDateTransformation([$this, 'toDate']);
24*104a3b7cSAndreas Gohr        $this->remote->setFileTransformation([$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']);
37cc7691adSAndreas Gohr        }
38cc7691adSAndreas Gohr        if (
39cc7691adSAndreas Gohr            !isset($_SERVER['CONTENT_TYPE']) ||
4020264973SAndreas Gohr            (
4120264973SAndreas Gohr                strtolower($_SERVER['CONTENT_TYPE']) !== 'text/xml' &&
4220264973SAndreas Gohr                strtolower($_SERVER['CONTENT_TYPE']) !== 'application/xml'
4320264973SAndreas Gohr            )
44cc7691adSAndreas Gohr        ) {
45cc7691adSAndreas Gohr            throw new ServerException('XML-RPC server accepts XML requests only.', -32606);
463df364a3STimo Richter        }
473df364a3STimo Richter
481498ac42SAndreas Gohr        parent::serve($data);
491498ac42SAndreas Gohr    }
501498ac42SAndreas Gohr
511cdd0090SAndreas Gohr    /**
521cdd0090SAndreas Gohr     * @inheritdoc
531cdd0090SAndreas Gohr     */
54*104a3b7cSAndreas Gohr    protected function call($methodname, $args)
551cdd0090SAndreas Gohr    {
561cdd0090SAndreas Gohr        try {
571cdd0090SAndreas Gohr            $result = $this->remote->call($methodname, $args);
581cdd0090SAndreas Gohr            return $result;
597f8f2456SAndreas Gohr        } catch (AccessDeniedException $e) {
601cdd0090SAndreas Gohr            if (!isset($_SERVER['REMOTE_USER'])) {
611cdd0090SAndreas Gohr                http_status(401);
627f8f2456SAndreas Gohr                return new ServerException("server error. not authorized to call method $methodname", -32603);
631cdd0090SAndreas Gohr            } else {
641cdd0090SAndreas Gohr                http_status(403);
657f8f2456SAndreas Gohr                return new ServerException("server error. forbidden to call the method $methodname", -32604);
661cdd0090SAndreas Gohr            }
671cdd0090SAndreas Gohr        } catch (RemoteException $e) {
687f8f2456SAndreas Gohr            return new ServerException($e->getMessage(), $e->getCode());
691cdd0090SAndreas Gohr        }
701cdd0090SAndreas Gohr    }
711cdd0090SAndreas Gohr
721cdd0090SAndreas Gohr    /**
731cdd0090SAndreas Gohr     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
747f8f2456SAndreas Gohr     * @return Date
751cdd0090SAndreas Gohr     */
761cdd0090SAndreas Gohr    public function toDate($data)
771cdd0090SAndreas Gohr    {
787f8f2456SAndreas Gohr        return new Date($data);
791cdd0090SAndreas Gohr    }
801cdd0090SAndreas Gohr
811cdd0090SAndreas Gohr    /**
821cdd0090SAndreas Gohr     * @param string $data
837f8f2456SAndreas Gohr     * @return Base64
841cdd0090SAndreas Gohr     */
851cdd0090SAndreas Gohr    public function toFile($data)
861cdd0090SAndreas Gohr    {
877f8f2456SAndreas Gohr        return new Base64($data);
881cdd0090SAndreas Gohr    }
891cdd0090SAndreas Gohr}
90