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