xref: /dokuwiki/lib/exe/xmlrpc.php (revision b89a0fa1b47aed94928a7468ef7742d7a344f02e)
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.getPageVersions',
98            'this:pageVersions',
99            array('struct','string','int'),
100            'Returns the available revisions of the page.'
101        );
102        $this->addCallback(
103            'wiki.putPage',
104            'this:putPage',
105            array('int', 'string', 'string', 'struct'),
106            'Saves a wiki page.'
107        );
108        $this->addCallback(
109            'wiki.listLinks',
110            'this:listLinks',
111            array('struct','string'),
112            'Lists all links contained in a wiki page.'
113        );
114        $this->addCallback(
115            'wiki.getRecentChanges',
116            'this:getRecentChanges',
117            array('struct','int'),
118            'Returns a strukt about all recent changes since given timestamp.'
119        );
120
121        $this->serve();
122    }
123
124    /**
125     * Return a raw wiki page
126     */
127    function rawPage($id,$rev=''){
128        if(auth_quickaclcheck($id) < AUTH_READ){
129            return new IXR_Error(1, 'You are not allowed to read this page');
130        }
131        $text = rawWiki($id,$rev);
132        if(!$text) {
133            $data = array($id);
134            return trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
135        } else {
136            return $text;
137        }
138    }
139
140    /**
141     * Return a wiki page rendered to html
142     */
143    function htmlPage($id,$rev=''){
144        if(auth_quickaclcheck($id) < AUTH_READ){
145            return new IXR_Error(1, 'You are not allowed to read this page');
146        }
147        return p_wiki_xhtml($id,$rev,false);
148    }
149
150    /**
151     * List all pages - we use the indexer list here
152     */
153    function listPages(){
154        require_once(DOKU_INC.'inc/fulltext.php');
155        return ft_pageLookup('');
156    }
157
158    /**
159     * Return a list of backlinks
160     */
161    function listBackLinks($id){
162        require_once(DOKU_INC.'inc/fulltext.php');
163        return ft_backlinks($id);
164    }
165
166    /**
167     * Return some basic data about a page
168     */
169    function pageInfo($id,$rev=''){
170        if(auth_quickaclcheck($id) < AUTH_READ){
171            return new IXR_Error(1, 'You are not allowed to read this page');
172        }
173        $file = wikiFN($id,$rev);
174        $time = @filemtime($file);
175        if(!$time){
176            return new IXR_Error(10, 'The requested page does not exist');
177        }
178
179        $info = getRevisionInfo($id, $time, 1024);
180
181        $data = array(
182            'name'         => $id,
183            'lastModified' => new IXR_Date($time),
184            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
185            'version'      => $time
186        );
187
188        return ($data);
189    }
190
191    /**
192     * Save a wiki page
193     *
194     * @author Michael Klier <chi@chimeric.de>
195     */
196    function putPage($id, $text, $params) {
197        global $TEXT;
198        global $lang;
199
200        $id    = cleanID($id);
201        $TEXT  = trim($text);
202        $sum   = $params['sum'];
203        $minor = $params['minor'];
204
205        if(empty($id))
206            return new IXR_Error(1, 'Empty page ID');
207
208        if(!page_exists($id) && empty($TEXT)) {
209            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
210        }
211
212        if(auth_quickaclcheck($id) < AUTH_EDIT)
213            return new IXR_Error(1, 'You are not allowed to edit this page');
214
215        // Check, if page is locked
216        if(checklock($id))
217            return new IXR_Error(1, 'The page is currently locked');
218
219        // SPAM check
220        if(checkwordblock())
221            return new IXR_Error(1, 'Positive wordblock check');
222
223        // autoset summary on new pages
224        if(!page_exists($id) && empty($sum)) {
225            $sum = $lang['created'];
226        }
227
228        // autoset summary on deleted pages
229        if(page_exists($id) && empty($TEXT) && empty($sum)) {
230            $sum = $lang['deleted'];
231        }
232
233        lock($id);
234
235        saveWikiText($id,$TEXT,$sum,$minor);
236
237        unlock($id);
238
239        return 0;
240    }
241
242    /**
243     * Lists all links contained in a wiki page
244     *
245     * @author Michael Klier <chi@chimeric.de>
246     */
247    function listLinks($id) {
248        if(auth_quickaclcheck($id) < AUTH_READ){
249            return new IXR_Error(1, 'You are not allowed to read this page');
250        }
251        $links = array();
252
253        // resolve page instructions
254        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
255
256        // instantiate new Renderer - needed for interwiki links
257        include(DOKU_INC.'inc/parser/xhtml.php');
258        $Renderer = new Doku_Renderer_xhtml();
259        $Renderer->interwiki = getInterwiki();
260
261        // parse parse instructions
262        foreach($ins as $in) {
263            $link = array();
264            switch($in[0]) {
265                case 'internallink':
266                    $link['type'] = 'local';
267                    $link['page'] = $in[1][0];
268                    $link['href'] = wl($in[1][0]);
269                    array_push($links,$link);
270                    break;
271                case 'externallink':
272                    $link['type'] = 'extern';
273                    $link['page'] = $in[1][0];
274                    $link['href'] = $in[1][0];
275                    array_push($links,$link);
276                    break;
277                case 'interwikilink':
278                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
279                    $link['type'] = 'extern';
280                    $link['page'] = $url;
281                    $link['href'] = $url;
282                    array_push($links,$link);
283                    break;
284            }
285        }
286
287        return ($links);
288    }
289
290    /**
291     * Returns a list of recent changes since give timestamp
292     *
293     * @author Michael Klier <chi@chimeric.de>
294     */
295    function getRecentChanges($timestamp) {
296        global $conf;
297
298        if(strlen($timestamp) != 10)
299            return new IXR_Error(20, 'The provided value is not a valid timestamp');
300
301        $changes = array();
302
303        require_once(DOKU_INC.'inc/changelog.php');
304        require_once(DOKU_INC.'inc/pageutils.php');
305
306        // read changes
307        $lines = @file($conf['changelog']);
308
309        if(empty($lines))
310            return new IXR_Error(10, 'The changelog could not be read');
311
312        // we start searching at the end of the list
313        $lines = array_reverse($lines);
314
315        // cache seen pages and skip them
316        $seen = array();
317
318        foreach($lines as $line) {
319
320            if(empty($line)) continue; // skip empty lines
321
322            $logline = parseChangelogLine($line);
323
324            if($logline === false) continue;
325
326            // skip seen ones
327            if(isset($seen[$logline['id']])) continue;
328
329            // skip minors
330            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
331
332            // remember in seen to skip additional sights
333            $seen[$logline['id']] = 1;
334
335            // check if it's a hidden page
336            if(isHiddenPage($logline['id'])) continue;
337
338            // check ACL
339            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
340
341            // check existance
342            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
343
344            // check if logline is still in the queried time frame
345            if($logline['date'] >= $timestamp) {
346                $change['name']         = $logline['id'];
347                $change['lastModified'] = new IXR_Date($logline['date']);
348                $change['author']       = $logline['user'];
349                $change['version']      = $logline['date'];
350                array_push($changes, $change);
351            } else {
352                $changes = array_reverse($changes);
353                return ($changes);
354            }
355        }
356        // in case we still have nothing at this point
357        return new IXR_Error(30, 'There are no changes in the specified timeframe');
358    }
359
360    /**
361     * Returns a list of available revisions of a given wiki page
362     *
363     * @author Michael Klier <chi@chimeric.de>
364     */
365    function pageVersions($id, $first) {
366        global $conf;
367
368        $versions = array();
369
370        if(empty($id))
371            return new IXR_Error(1, 'Empty page ID');
372
373        require_once(DOKU_INC.'inc/changelog.php');
374
375        $revisions = getRevisions($id, $first, $conf['recent']+1);
376
377        if(count($revisions)==0 && $first!=0) {
378            $first=0;
379            $revisions = getRevisions($id, $first, $conf['recent']+1);
380        }
381
382        if(count($revisions)>0 && $first==0) {
383            array_unshift($revisions, '');  // include current revision
384            array_pop($revisions);          // remove extra log entry
385        }
386
387        $hasNext = false;
388        if(count($revisions)>$conf['recent']) {
389            $hasNext = true;
390            array_pop($revisions); // remove extra log entry
391        }
392
393        if(!empty($revisions)) {
394            foreach($revisions as $rev) {
395                $file = wikiFN($id,$rev);
396                $time = @filemtime($file);
397                // we check if the page actually exists, if this is not the
398                // case this can lead to less pages being returned than
399                // specified via $conf['recent']
400                if($time){
401                    $info = getRevisionInfo($id, $time, 1024);
402                    if(!empty($info)) {
403                        $data['user'] = $info['user'];
404                        $data['ip']   = $info['ip'];
405                        $data['type'] = $info['type'];
406                        $data['sum']  = $info['sum'];
407                        $data['modified'] = new IXR_Date($info['date']);
408                        $data['version'] = $info['date'];
409                        array_push($versions, $data);
410                    }
411                }
412            }
413            return $versions;
414        } else {
415            return array();
416        }
417    }
418
419    /**
420     * The version of Wiki RPC API supported
421     */
422    function wiki_RPCVersion(){
423        return 2;
424    }
425}
426
427$server = new dokuwiki_xmlrpc_server();
428
429// vim:ts=4:sw=4:enc=utf-8:
430