xref: /dokuwiki/lib/exe/xmlrpc.php (revision cfef30015aa05745995a8ff122b6f3e80f2167a3)
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');
9require_once(DOKU_INC.'inc/common.php');
10require_once(DOKU_INC.'inc/auth.php');
11session_write_close();  //close session
12
13if(!$conf['xmlrpc']) {
14    die('XML-RPC server not enabled.');
15    // FIXME check for groups allowed
16}
17
18require_once(DOKU_INC.'inc/IXR_Library.php');
19
20
21/**
22 * Contains needed wrapper functions and registers all available
23 * XMLRPC functions.
24 */
25class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
26    var $methods = array();
27
28    /**
29     * Constructor. Register methods and run Server
30     */
31    function dokuwiki_xmlrpc_server(){
32        $this->IXR_IntrospectionServer();
33
34        /* DokuWiki's own methods */
35        $this->addCallback(
36            'dokuwiki.getVersion',
37            'getVersion',
38            array('string'),
39            'Returns the running DokuWiki version.'
40        );
41
42        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
43        $this->addCallback(
44            'wiki.getRPCVersionSupported',
45            'this:wiki_RPCVersion',
46            array('int'),
47            'Returns 2 with the supported RPC API version.'
48        );
49        $this->addCallback(
50            'wiki.getPage',
51            'this:rawPage',
52            array('string','string'),
53            'Get the raw Wiki text of page, latest version.'
54        );
55        $this->addCallback(
56            'wiki.getPageVersion',
57            'this:rawPage',
58            array('string','string','int'),
59            'Get the raw Wiki text of page.'
60        );
61        $this->addCallback(
62            'wiki.getPageHTML',
63            'this:htmlPage',
64            array('string','string'),
65            'Return page in rendered HTML, latest version.'
66        );
67        $this->addCallback(
68            'wiki.getPageHTMLVersion',
69            'this:htmlPage',
70            array('string','string','int'),
71            'Return page in rendered HTML.'
72        );
73        $this->addCallback(
74            'wiki.getAllPages',
75            'this:listPages',
76            array('struct'),
77            'Returns a list of all pages. The result is an array of utf8 pagenames.'
78        );
79        $this->addCallback(
80            'wiki.getAttachments',
81            'this:listAttachments',
82            array('struct'),
83            'Returns a list of all media files.'
84        );
85        $this->addCallback(
86            'wiki.getBackLinks',
87            'this:listBackLinks',
88            array('struct','string'),
89            'Returns the pages that link to this page.'
90        );
91        $this->addCallback(
92            'wiki.getPageInfo',
93            'this:pageInfo',
94            array('struct','string'),
95            'Returns a struct with infos about the page.'
96        );
97        $this->addCallback(
98            'wiki.getPageInfoVersion',
99            'this:pageInfo',
100            array('struct','string','int'),
101            'Returns a struct with infos about the page.'
102        );
103        $this->addCallback(
104            'wiki.getPageVersions',
105            'this:pageVersions',
106            array('struct','string','int'),
107            'Returns the available revisions of the page.'
108        );
109        $this->addCallback(
110            'wiki.putPage',
111            'this:putPage',
112            array('int', 'string', 'string', 'struct'),
113            'Saves a wiki page.'
114        );
115        $this->addCallback(
116            'wiki.listLinks',
117            'this:listLinks',
118            array('struct','string'),
119            'Lists all links contained in a wiki page.'
120        );
121        $this->addCallback(
122            'wiki.getRecentChanges',
123            'this:getRecentChanges',
124            array('struct','int'),
125            'Returns a strukt about all recent changes since given timestamp.'
126        );
127        $this->addCallback(
128                'wiki.aclCheck',
129                'this:aclCheck',
130                array('struct', 'string'),
131                'Returns the permissions of a given wiki page.'
132        );
133        $this->addCallback(
134                'wiki.putAttachment',
135                'this:putAttachment',
136                array('struct', 'string', 'base64', 'struct'),
137                'Upload a file to the wiki.'
138        );
139        $this->addCallback(
140        		'wiki.getAttachment',
141        		'this:getAttachment',
142        		array('string'),
143        		'Download a file from the wiki.'
144        );
145
146        $this->serve();
147    }
148
149    /**
150     * Return a raw wiki page
151     */
152    function rawPage($id,$rev=''){
153        if(auth_quickaclcheck($id) < AUTH_READ){
154            return new IXR_Error(1, 'You are not allowed to read this page');
155        }
156        $text = rawWiki($id,$rev);
157        if(!$text) {
158            $data = array($id);
159            return trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
160        } else {
161            return $text;
162        }
163    }
164
165    /**
166     * Return a media file encoded in base64
167     */
168    function getAttachment($id){
169    	if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ)
170    		return new IXR_Error(1, 'You are not allowed to read this file');
171
172		$file = mediaFN($id);
173		if (!@ file_exists($file))
174			return new IXR_Error(1, 'The requested file does not exist');
175
176		$data = io_readFile($file, false);
177		$base64 = base64_encode($data);
178		return $base64;
179    }
180
181    /**
182     * Return a wiki page rendered to html
183     */
184    function htmlPage($id,$rev=''){
185        if(auth_quickaclcheck($id) < AUTH_READ){
186            return new IXR_Error(1, 'You are not allowed to read this page');
187        }
188        return p_wiki_xhtml($id,$rev,false);
189    }
190
191    /**
192     * List all pages - we use the indexer list here
193     */
194    function listPages(){
195        require_once(DOKU_INC.'inc/fulltext.php');
196        return ft_pageLookup('');
197    }
198
199    /**
200     * List all media files.
201     */
202    function listAttachments($ns) {
203        global $conf;
204        global $lang;
205
206        $ns = cleanID($ns);
207
208        if(auth_quickaclcheck($ns.':*') >= AUTH_READ) {
209            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
210
211            $data = array();
212            require_once(DOKU_INC.'inc/search.php');
213            search($data, $conf['mediadir'], 'search_media', array('recursive' => true), $dir);
214
215            if(!count($data)) {
216                return array();
217            }
218
219            $files = array();
220            foreach($data as $item) {
221                $file = array();
222                $file['id']       = $item['id'];
223                $file['size']     = $item['size'];
224                $file['mtime']    = $item['mtime'];
225                $file['isimg']    = $item['isimg'];
226                $file['writable'] = $item['writeable'];
227                array_push($files, $file);
228            }
229
230            return $files;
231
232        } else {
233            return new IXR_Error(1, 'You are not allowed to list media files.');
234        }
235    }
236
237    /**
238     * Return a list of backlinks
239     */
240    function listBackLinks($id){
241        require_once(DOKU_INC.'inc/fulltext.php');
242        return ft_backlinks($id);
243    }
244
245    /**
246     * Return some basic data about a page
247     */
248    function pageInfo($id,$rev=''){
249        if(auth_quickaclcheck($id) < AUTH_READ){
250            return new IXR_Error(1, 'You are not allowed to read this page');
251        }
252        $file = wikiFN($id,$rev);
253        $time = @filemtime($file);
254        if(!$time){
255            return new IXR_Error(10, 'The requested page does not exist');
256        }
257
258        $info = getRevisionInfo($id, $time, 1024);
259
260        $data = array(
261            'name'         => $id,
262            'lastModified' => new IXR_Date($time),
263            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
264            'version'      => $time
265        );
266
267        return ($data);
268    }
269
270    /**
271     * Save a wiki page
272     *
273     * @author Michael Klier <chi@chimeric.de>
274     */
275    function putPage($id, $text, $params) {
276        global $TEXT;
277        global $lang;
278        global $conf;
279
280        $id    = cleanID($id);
281        $TEXT  = trim($text);
282        $sum   = $params['sum'];
283        $minor = $params['minor'];
284
285        if(empty($id))
286            return new IXR_Error(1, 'Empty page ID');
287
288        if(!page_exists($id) && empty($TEXT)) {
289            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
290        }
291
292        if(auth_quickaclcheck($id) < AUTH_EDIT)
293            return new IXR_Error(1, 'You are not allowed to edit this page');
294
295        // Check, if page is locked
296        if(checklock($id))
297            return new IXR_Error(1, 'The page is currently locked');
298
299        // SPAM check
300        if(checkwordblock())
301            return new IXR_Error(1, 'Positive wordblock check');
302
303        // autoset summary on new pages
304        if(!page_exists($id) && empty($sum)) {
305            $sum = $lang['created'];
306        }
307
308        // autoset summary on deleted pages
309        if(page_exists($id) && empty($TEXT) && empty($sum)) {
310            $sum = $lang['deleted'];
311        }
312
313        lock($id);
314
315        saveWikiText($id,$TEXT,$sum,$minor);
316
317        unlock($id);
318
319        // run the indexer if page wasn't indexed yet
320        if(!@file_exists(metaFN($id, '.indexed'))) {
321            // try to aquire a lock
322            $lock = $conf['lockdir'].'/_indexer.lock';
323            while(!@mkdir($lock,$conf['dmode'])){
324                usleep(50);
325                if(time()-@filemtime($lock) > 60*5){
326                    // looks like a stale lock - remove it
327                    @rmdir($lock);
328                }else{
329                    return false;
330                }
331            }
332            if($conf['dperm']) chmod($lock, $conf['dperm']);
333
334            require_once(DOKU_INC.'inc/indexer.php');
335
336            // do the work
337            idx_addPage($id);
338
339            // we're finished - save and free lock
340            io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
341            @rmdir($lock);
342        }
343
344        return 0;
345    }
346
347    /**
348     * Uploads a file to the wiki.
349     *
350     * Michael Klier <chi@chimeric.de>
351     */
352    function putAttachment($ns, $file, $params) {
353        global $conf;
354        global $lang;
355
356        $auth = auth_quickaclcheck($ns.':*');
357        if($auth >= AUTH_UPLOAD) {
358            if(!isset($params['name'])) {
359                return new IXR_ERROR(1, 'Filename not given.');
360            }
361
362            $ftmp = $conf['tmpdir'] . '/' . $params['name'];
363            $name = $params['name'];
364
365            // save temporary file
366            @unlink($ftmp);
367            $buff = base64_decode($file);
368            io_saveFile($ftmp, $buff);
369
370            // get filename
371            list($iext, $imime) = mimetype($name);
372            $id = cleanID($ns.':'.$name);
373            $fn = mediaFN($id);
374
375            // get filetype regexp
376            $types = array_keys(getMimeTypes());
377            $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
378            $regex = join('|',$types);
379
380            // because a temp file was created already
381            if(preg_match('/\.('.$regex.')$/i',$fn)) {
382                //check for overwrite
383                if(@file_exists($fn) && (!$params['ow'] || $auth < AUTH_DELETE)) {
384                    return new IXR_ERROR(1, $lang['uploadexist']);
385                }
386                // check for valid content
387                @require_once(DOKU_INC.'inc/media.php');
388                $ok = media_contentcheck($ftmp, $imime);
389                if($ok == -1) {
390                    return new IXR_ERROR(1, sprintf($lang['uploadexist'], ".$iext"));
391                } elseif($ok == -2) {
392                    return new IXR_ERROR(1, $lang['uploadspam']);
393                } elseif($ok == -3) {
394                    return new IXR_ERROR(1, $lang['uploadxss']);
395                }
396
397                // prepare event data
398                $data[0] = $ftmp;
399                $data[1] = $fn;
400                $data[2] = $id;
401                $data[3] = $imime;
402
403                // trigger event
404                require_once(DOKU_INC.'inc/events.php');
405                return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true);
406
407            } else {
408                return new IXR_ERROR(1, $lang['uploadwrong']);
409            }
410        } else {
411            return new IXR_ERROR(1, "You don't have permissions to upload files.");
412        }
413    }
414
415    /**
416     * Moves the temporary file to its final destination.
417     *
418     * Michael Klier <chi@chimeric.de>
419     */
420    function _media_upload_action($data) {
421        global $conf;
422
423        if(is_array($data) && count($data)===4) {
424            io_createNamespace($data[2], 'media');
425            if(rename($data[0], $data[1])) {
426                chmod($data[1], $conf['fmode']);
427                media_notify($data[2], $data[1], $data[3]);
428                return $data[2];
429            } else {
430                return new IXR_ERROR(1, 'Upload failed.');
431            }
432        } else {
433            return new IXR_ERROR(1, 'Upload failed.');
434        }
435    }
436
437    /**
438    * Returns the permissions of a given wiki page
439    */
440    function aclCheck($id) {
441        return auth_quickaclcheck($id);
442    }
443
444    /**
445     * Lists all links contained in a wiki page
446     *
447     * @author Michael Klier <chi@chimeric.de>
448     */
449    function listLinks($id) {
450        if(auth_quickaclcheck($id) < AUTH_READ){
451            return new IXR_Error(1, 'You are not allowed to read this page');
452        }
453        $links = array();
454
455        // resolve page instructions
456        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
457
458        // instantiate new Renderer - needed for interwiki links
459        include(DOKU_INC.'inc/parser/xhtml.php');
460        $Renderer = new Doku_Renderer_xhtml();
461        $Renderer->interwiki = getInterwiki();
462
463        // parse parse instructions
464        foreach($ins as $in) {
465            $link = array();
466            switch($in[0]) {
467                case 'internallink':
468                    $link['type'] = 'local';
469                    $link['page'] = $in[1][0];
470                    $link['href'] = wl($in[1][0]);
471                    array_push($links,$link);
472                    break;
473                case 'externallink':
474                    $link['type'] = 'extern';
475                    $link['page'] = $in[1][0];
476                    $link['href'] = $in[1][0];
477                    array_push($links,$link);
478                    break;
479                case 'interwikilink':
480                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
481                    $link['type'] = 'extern';
482                    $link['page'] = $url;
483                    $link['href'] = $url;
484                    array_push($links,$link);
485                    break;
486            }
487        }
488
489        return ($links);
490    }
491
492    /**
493     * Returns a list of recent changes since give timestamp
494     *
495     * @author Michael Klier <chi@chimeric.de>
496     */
497    function getRecentChanges($timestamp) {
498        global $conf;
499
500        if(strlen($timestamp) != 10)
501            return new IXR_Error(20, 'The provided value is not a valid timestamp');
502
503        $changes = array();
504
505        require_once(DOKU_INC.'inc/changelog.php');
506        require_once(DOKU_INC.'inc/pageutils.php');
507
508        // read changes
509        $lines = @file($conf['changelog']);
510
511        if(empty($lines))
512            return new IXR_Error(10, 'The changelog could not be read');
513
514        // we start searching at the end of the list
515        $lines = array_reverse($lines);
516
517        // cache seen pages and skip them
518        $seen = array();
519
520        foreach($lines as $line) {
521
522            if(empty($line)) continue; // skip empty lines
523
524            $logline = parseChangelogLine($line);
525
526            if($logline === false) continue;
527
528            // skip seen ones
529            if(isset($seen[$logline['id']])) continue;
530
531            // skip minors
532            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
533
534            // remember in seen to skip additional sights
535            $seen[$logline['id']] = 1;
536
537            // check if it's a hidden page
538            if(isHiddenPage($logline['id'])) continue;
539
540            // check ACL
541            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
542
543            // check existance
544            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
545
546            // check if logline is still in the queried time frame
547            if($logline['date'] >= $timestamp) {
548                $change['name']         = $logline['id'];
549                $change['lastModified'] = new IXR_Date($logline['date']);
550                $change['author']       = $logline['user'];
551                $change['version']      = $logline['date'];
552                array_push($changes, $change);
553            } else {
554                $changes = array_reverse($changes);
555                return ($changes);
556            }
557        }
558        // in case we still have nothing at this point
559        return new IXR_Error(30, 'There are no changes in the specified timeframe');
560    }
561
562    /**
563     * Returns a list of available revisions of a given wiki page
564     *
565     * @author Michael Klier <chi@chimeric.de>
566     */
567    function pageVersions($id, $first) {
568        global $conf;
569
570        $versions = array();
571
572        if(empty($id))
573            return new IXR_Error(1, 'Empty page ID');
574
575        require_once(DOKU_INC.'inc/changelog.php');
576
577        $revisions = getRevisions($id, $first, $conf['recent']+1);
578
579        if(count($revisions)==0 && $first!=0) {
580            $first=0;
581            $revisions = getRevisions($id, $first, $conf['recent']+1);
582        }
583
584        if(count($revisions)>0 && $first==0) {
585            array_unshift($revisions, '');  // include current revision
586            array_pop($revisions);          // remove extra log entry
587        }
588
589        $hasNext = false;
590        if(count($revisions)>$conf['recent']) {
591            $hasNext = true;
592            array_pop($revisions); // remove extra log entry
593        }
594
595        if(!empty($revisions)) {
596            foreach($revisions as $rev) {
597                $file = wikiFN($id,$rev);
598                $time = @filemtime($file);
599                // we check if the page actually exists, if this is not the
600                // case this can lead to less pages being returned than
601                // specified via $conf['recent']
602                if($time){
603                    $info = getRevisionInfo($id, $time, 1024);
604                    if(!empty($info)) {
605                        $data['user'] = $info['user'];
606                        $data['ip']   = $info['ip'];
607                        $data['type'] = $info['type'];
608                        $data['sum']  = $info['sum'];
609                        $data['modified'] = new IXR_Date($info['date']);
610                        $data['version'] = $info['date'];
611                        array_push($versions, $data);
612                    }
613                }
614            }
615            return $versions;
616        } else {
617            return array();
618        }
619    }
620
621    /**
622     * The version of Wiki RPC API supported
623     */
624    function wiki_RPCVersion(){
625        return 2;
626    }
627}
628
629$server = new dokuwiki_xmlrpc_server();
630
631// vim:ts=4:sw=4:et:enc=utf-8:
632