xref: /dokuwiki/lib/exe/xmlrpc.php (revision a6a229ce529f6740e106fe8322cf3ab708e03063)
1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
3
4// fix when '<?xml' isn't on the very first line
5if(isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
6
7
8require_once(DOKU_INC.'inc/init.php');
9
10if(!$conf['xmlrpc']) {
11    die('XML-RPC server not enabled.');
12}
13
14require_once(DOKU_INC.'inc/common.php');
15require_once(DOKU_INC.'inc/auth.php');
16session_write_close();  //close session
17require_once(DOKU_INC.'inc/IXR_Library.php');
18
19
20/**
21 * Contains needed wrapper functions and registers all available
22 * XMLRPC functions.
23 */
24class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
25    var $methods = array();
26
27    /**
28     * Constructor. Register methods and run Server
29     */
30    function dokuwiki_xmlrpc_server(){
31        $this->IXR_IntrospectionServer();
32
33        /* DokuWiki's own methods */
34        $this->addCallback(
35            'dokuwiki.getVersion',
36            'getVersion',
37            array('string'),
38            'Returns the running DokuWiki version.'
39        );
40
41        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
42        $this->addCallback(
43            'wiki.getRPCVersionSupported',
44            'this:wiki_RPCVersion',
45            array('int'),
46            'Returns 2 with the supported RPC API version.'
47        );
48        $this->addCallback(
49            'wiki.getPage',
50            'this:rawPage',
51            array('string','string'),
52            'Get the raw Wiki text of page, latest version.'
53        );
54        $this->addCallback(
55            'wiki.getPageVersion',
56            'this:rawPage',
57            array('string','string','int'),
58            'Get the raw Wiki text of page.'
59        );
60        $this->addCallback(
61            'wiki.getPageHTML',
62            'this:htmlPage',
63            array('string','string'),
64            'Return page in rendered HTML, latest version.'
65        );
66        $this->addCallback(
67            'wiki.getPageHTMLVersion',
68            'this:htmlPage',
69            array('string','string','int'),
70            'Return page in rendered HTML.'
71        );
72        $this->addCallback(
73            'wiki.getAllPages',
74            'this:listPages',
75            array('struct'),
76            'Returns a list of all pages. The result is an array of utf8 pagenames.'
77        );
78        $this->addCallback(
79            'wiki.getBackLinks',
80            'this:listBackLinks',
81            array('struct','string'),
82            'Returns the pages that link to this page.'
83        );
84        $this->addCallback(
85            'wiki.getPageInfo',
86            'this:pageInfo',
87            array('struct','string'),
88            'Returns a struct with infos about the page.'
89        );
90        $this->addCallback(
91            'wiki.getPageInfoVersion',
92            'this:pageInfo',
93            array('struct','string','int'),
94            'Returns a struct with infos about the page.'
95        );
96        $this->addCallback(
97            'wiki.putPage',
98            'this:putPage',
99            array('int', 'string', 'string', 'struct'),
100            'Saves a wiki page.'
101        );
102        $this->addCallback(
103            'wiki.listLinks',
104            'this:listLinks',
105            array('struct','string'),
106            'Lists all links contained in a wiki page.'
107        );
108        $this->addCallback(
109            'wiki.getRecentChanges',
110            'this:getRecentChanges',
111            array('struct','int'),
112            'Returns a strukt about all recent changes since given timestamp.'
113        );
114
115        $this->serve();
116    }
117
118    /**
119     * Return a raw wiki page
120     */
121    function rawPage($id,$rev=''){
122        if(auth_quickaclcheck($id) < AUTH_READ){
123            return new IXR_Error(1, 'You are not allowed to read this page');
124        }
125        return rawWiki($id,$rev);
126    }
127
128    /**
129     * Return a wiki page rendered to html
130     */
131    function htmlPage($id,$rev=''){
132        if(auth_quickaclcheck($id) < AUTH_READ){
133            return new IXR_Error(1, 'You are not allowed to read this page');
134        }
135        return p_wiki_xhtml($id,$rev,false);
136    }
137
138    /**
139     * List all pages - we use the indexer list here
140     */
141    function listPages(){
142        require_once(DOKU_INC.'inc/fulltext.php');
143        return ft_pageLookup('');
144    }
145
146    /**
147     * Return a list of backlinks
148     */
149    function listBackLinks($id){
150        require_once(DOKU_INC.'inc/fulltext.php');
151        return ft_backlinks($id);
152    }
153
154    /**
155     * Return some basic data about a page
156     */
157    function pageInfo($id,$rev=''){
158        if(auth_quickaclcheck($id) < AUTH_READ){
159            return new IXR_Error(1, 'You are not allowed to read this page');
160        }
161        $file = wikiFN($id,$rev);
162        $time = @filemtime($file);
163        if(!$time){
164            return new IXR_Error(10, 'The requested page does not exist');
165        }
166
167        $info = getRevisionInfo($id, $time, 1024);
168
169        $data = array(
170            'name'         => $id,
171            'lastModified' => new IXR_Date($time),
172            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
173            'version'      => $time
174        );
175
176        return ($data);
177    }
178
179    /**
180     * Save a wiki page
181     *
182     * @author Michael Klier <chi@chimeric.de>
183     */
184    function putPage($id, $text, $params) {
185        global $TEXT;
186        global $lang;
187
188        $id    = cleanID($id);
189        $TEXT  = trim($text);
190        $sum   = $params['sum'];
191        $minor = $params['minor'];
192
193        if(empty($id))
194            return new IXR_Error(1, 'Empty page ID');
195
196        if(auth_quickaclcheck($id) < AUTH_WRITE)
197            return new IXR_Error(1, 'You are not allowed to edit this page');
198
199        // Check, if page is locked
200        if(checklock($id))
201            return new IXR_Error(1, 'The page is currently locked');
202
203        // SPAM check
204        if(checkwordblock())
205            return new IXR_Error(1, 'Positive wordblock check');
206
207        // autoset summary on new pages
208        if(!page_exists($id) && empty($sum)) {
209            $sum = $lang['created'];
210        }
211
212        // autoset summary on deleted pages
213        if(page_exists($id) && empty($TEXT) && empty($sum)) {
214            $sum = $lang['deleted'];
215        }
216
217        lock($id);
218
219        saveWikiText($id,$TEXT,$sum,$minor);
220
221        unlock($id);
222
223        return 0;
224    }
225
226    /**
227     * Lists all links contained in a wiki page
228     *
229     * @author Michael Klier <chi@chimeric.de>
230     */
231    function listLinks($id) {
232        if(auth_quickaclcheck($id) < AUTH_READ){
233            return new IXR_Error(1, 'You are not allowed to read this page');
234        }
235        $links = array();
236
237        // resolve page instructions
238        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
239
240        // instantiate new Renderer - needed for interwiki links
241        include(DOKU_INC.'inc/parser/xhtml.php');
242        $Renderer = new Doku_Renderer_xhtml();
243        $Renderer->interwiki = getInterwiki();
244
245        // parse parse instructions
246        foreach($ins as $in) {
247            $link = array();
248            switch($in[0]) {
249                case 'internallink':
250                    $link['type'] = 'local';
251                    $link['page'] = $in[1][0];
252                    $link['href'] = wl($in[1][0]);
253                    array_push($links,$link);
254                    break;
255                case 'externallink':
256                    $link['type'] = 'extern';
257                    $link['page'] = $in[1][0];
258                    $link['href'] = $in[1][0];
259                    array_push($links,$link);
260                    break;
261                case 'interwikilink':
262                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
263                    $link['type'] = 'extern';
264                    $link['page'] = $url;
265                    $link['href'] = $url;
266                    array_push($links,$link);
267                    break;
268            }
269        }
270
271        return ($links);
272    }
273
274    /**
275     * Returns a list of recent changes since give timestamp
276     *
277     * @author Michael Klier <chi@chimeric.de>
278     */
279    function getRecentChanges($timestamp) {
280        global $conf;
281
282        if(strlen($timestamp) != 10)
283            return new IXR_Error(20, 'The provided value is not a valid timestamp');
284
285        $changes = array();
286
287        require_once(DOKU_INC.'inc/changelog.php');
288        require_once(DOKU_INC.'inc/pageutils.php');
289
290        // read changes
291        $lines = @file($conf['changelog']);
292
293        if(empty($lines))
294            return new IXR_Error(10, 'The changelog could not be read');
295
296        // we start searching at the end of the list
297        $lines = array_reverse($lines);
298
299        // cache seen pages and skip them
300        $seen = array();
301
302        foreach($lines as $line) {
303
304            if(empty($line)) continue; // skip empty lines
305
306            $logline = parseChangelogLine($line);
307
308            if($logline === false) continue;
309
310            // skip seen ones
311            if(isset($seen[$logline['id']])) continue;
312
313            // skip minors
314            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
315
316            // remember in seen to skip additional sights
317            $seen[$logline['id']] = 1;
318
319            // check if it's a hidden page
320            if(isHiddenPage($logline['id'])) continue;
321
322            // check ACL
323            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
324
325            // check existance
326            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
327
328            // check if logline is still in the queried time frame
329            if($logline['date'] >= $timestamp) {
330                $change['name']         = $logline['id'];
331                $change['lastModified'] = new IXR_Date($logline['date']);
332                $change['author']       = $logline['user'];
333                $change['version']      = $logline['date'];
334                array_push($changes, $change);
335            } else {
336                $changes = array_reverse($changes);
337                return ($changes);
338            }
339        }
340        // in case we still have nothing at this point
341        return new IXR_Error(30, 'There are no changes in the specified timeframe');
342    }
343
344    /**
345     * The version of Wiki RPC API supported
346     */
347    function wiki_RPCVersion(){
348        return 2;
349    }
350}
351
352$server = new dokuwiki_xmlrpc_server();
353
354// vim:ts=4:sw=4:enc=utf-8:
355