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