xref: /dokuwiki/inc/Remote/XmlRpcServer.php (revision 3df364a338f91f787f996b83392bc95fe38abc2c)
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        }
35*3df364a3STimo Richter        if (!empty($conf['remotecors'])) {
36*3df364a3STimo Richter            header('Access-Control-Allow-Origin: ' . $conf['remotecors']);
37*3df364a3STimo Richter        }
38*3df364a3STimo Richter
391498ac42SAndreas Gohr        parent::serve($data);
401498ac42SAndreas Gohr    }
411498ac42SAndreas Gohr
421cdd0090SAndreas Gohr    /**
431cdd0090SAndreas Gohr     * @inheritdoc
441cdd0090SAndreas Gohr     */
451cdd0090SAndreas Gohr    public function call($methodname, $args)
461cdd0090SAndreas Gohr    {
471cdd0090SAndreas Gohr        try {
481cdd0090SAndreas Gohr            $result = $this->remote->call($methodname, $args);
491cdd0090SAndreas Gohr            return $result;
507f8f2456SAndreas Gohr        } catch (AccessDeniedException $e) {
511cdd0090SAndreas Gohr            if (!isset($_SERVER['REMOTE_USER'])) {
521cdd0090SAndreas Gohr                http_status(401);
537f8f2456SAndreas Gohr                return new ServerException("server error. not authorized to call method $methodname", -32603);
541cdd0090SAndreas Gohr            } else {
551cdd0090SAndreas Gohr                http_status(403);
567f8f2456SAndreas Gohr                return new ServerException("server error. forbidden to call the method $methodname", -32604);
571cdd0090SAndreas Gohr            }
581cdd0090SAndreas Gohr        } catch (RemoteException $e) {
597f8f2456SAndreas Gohr            return new ServerException($e->getMessage(), $e->getCode());
601cdd0090SAndreas Gohr        }
611cdd0090SAndreas Gohr    }
621cdd0090SAndreas Gohr
631cdd0090SAndreas Gohr    /**
641cdd0090SAndreas Gohr     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
657f8f2456SAndreas Gohr     * @return Date
661cdd0090SAndreas Gohr     */
671cdd0090SAndreas Gohr    public function toDate($data)
681cdd0090SAndreas Gohr    {
697f8f2456SAndreas Gohr        return new Date($data);
701cdd0090SAndreas Gohr    }
711cdd0090SAndreas Gohr
721cdd0090SAndreas Gohr    /**
731cdd0090SAndreas Gohr     * @param string $data
747f8f2456SAndreas Gohr     * @return Base64
751cdd0090SAndreas Gohr     */
761cdd0090SAndreas Gohr    public function toFile($data)
771cdd0090SAndreas Gohr    {
787f8f2456SAndreas Gohr        return new Base64($data);
791cdd0090SAndreas Gohr    }
801cdd0090SAndreas Gohr}
81