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