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