xref: /dokuwiki/lib/exe/xmlrpc.php (revision dd87735d917b53fa3e3ac66675834419ed24f832)
1<?php
2
3use dokuwiki\Remote\AccessDeniedException;
4use dokuwiki\Remote\Api;
5use dokuwiki\Remote\RemoteException;
6
7if(!defined('DOKU_INC')) define('DOKU_INC', dirname(__FILE__).'/../../');
8
9require_once(DOKU_INC.'inc/init.php');
10session_write_close();  //close session
11
12if(!$conf['remote']) die((new IXR_Error(-32605, "XML-RPC server not enabled."))->getXml());
13
14/**
15 * Contains needed wrapper functions and registers all available
16 * XMLRPC functions.
17 */
18class dokuwiki_xmlrpc_server extends IXR_Server {
19    protected $remote;
20
21    /**
22     * Constructor. Register methods and run Server
23     */
24    public function __construct(){
25        $this->remote = new Api();
26        $this->remote->setDateTransformation(array($this, 'toDate'));
27        $this->remote->setFileTransformation(array($this, 'toFile'));
28        parent::__construct();
29    }
30
31    /**
32     * @param string $methodname
33     * @param array $args
34     * @return IXR_Error|mixed
35     */
36    public function call($methodname, $args){
37        try {
38            $result = $this->remote->call($methodname, $args);
39            return $result;
40        } catch (AccessDeniedException $e) {
41            if (!isset($_SERVER['REMOTE_USER'])) {
42                http_status(401);
43                return new IXR_Error(-32603, "server error. not authorized to call method $methodname");
44            } else {
45                http_status(403);
46                return new IXR_Error(-32604, "server error. forbidden to call the method $methodname");
47            }
48        } catch (RemoteException $e) {
49            return new IXR_Error($e->getCode(), $e->getMessage());
50        }
51    }
52
53    /**
54     * @param string|int $data iso date(yyyy[-]mm[-]dd[ hh:mm[:ss]]) or timestamp
55     * @return IXR_Date
56     */
57    public function toDate($data) {
58        return new IXR_Date($data);
59    }
60
61    /**
62     * @param string $data
63     * @return IXR_Base64
64     */
65    public function toFile($data) {
66        return new IXR_Base64($data);
67    }
68}
69
70$server = new dokuwiki_xmlrpc_server();
71
72// vim:ts=4:sw=4:et:
73