xref: /dokuwiki/lib/exe/xmlrpc.php (revision 35e6af3e780b32ba4cd5dab9e4c190c484d2b838)
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/**
8 * Increased whenever the API is changed
9 */
10define('DOKU_XMLRPC_API_VERSION',5);
11
12require_once(DOKU_INC.'inc/init.php');
13session_write_close();  //close session
14
15if(!$conf['xmlrpc']) die('XML-RPC server not enabled.');
16
17/**
18 * Contains needed wrapper functions and registers all available
19 * XMLRPC functions.
20 */
21class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
22    var $methods       = array();
23    var $public_methods = array();
24
25    /**
26     * Checks if the current user is allowed to execute non anonymous methods
27     */
28    function checkAuth(){
29        global $conf;
30        global $USERINFO;
31
32        if(!$conf['useacl']) return true; //no ACL - then no checks
33
34        $allowed = explode(',',$conf['xmlrpcuser']);
35        $allowed = array_map('trim', $allowed);
36        $allowed = array_unique($allowed);
37        $allowed = array_filter($allowed);
38
39        if(!count($allowed)) return true; //no restrictions
40
41        $user   = $_SERVER['REMOTE_USER'];
42        $groups = (array) $USERINFO['grps'];
43
44        if(in_array($user,$allowed)) return true; //user explicitly mentioned
45
46        //check group memberships
47        foreach($groups as $group){
48            if(in_array('@'.$group,$allowed)) return true;
49        }
50
51        //still here? no access!
52        return false;
53    }
54
55    /**
56     * Adds a callback, extends parent method
57     *
58     * add another parameter to define if anonymous access to
59     * this method should be granted.
60     */
61    function addCallback($method, $callback, $args, $help, $public=false){
62        if($public) $this->public_methods[] = $method;
63        return parent::addCallback($method, $callback, $args, $help);
64    }
65
66    /**
67     * Execute a call, extends parent method
68     *
69     * Checks for authentication first
70     */
71    function call($methodname, $args){
72        if(!in_array($methodname,$this->public_methods) && !$this->checkAuth()){
73            return new IXR_Error(-32603, 'server error. not authorized to call method "'.$methodname.'".');
74        }
75        return parent::call($methodname, $args);
76    }
77
78    /**
79     * Constructor. Register methods and run Server
80     */
81    function dokuwiki_xmlrpc_server(){
82        $this->IXR_IntrospectionServer();
83
84        /* DokuWiki's own methods */
85        $this->addCallback(
86            'dokuwiki.getXMLRPCAPIVersion',
87            'this:getAPIVersion',
88            array('integer'),
89            'Returns the XMLRPC API version.',
90            true
91        );
92
93        $this->addCallback(
94            'dokuwiki.getVersion',
95            'getVersion',
96            array('string'),
97            'Returns the running DokuWiki version.',
98            true
99        );
100
101        $this->addCallback(
102            'dokuwiki.login',
103            'this:login',
104            array('integer','string','string'),
105            'Tries to login with the given credentials and sets auth cookies.',
106            true
107        );
108
109        $this->addCallback(
110            'dokuwiki.getPagelist',
111            'this:readNamespace',
112            array('struct','string','struct'),
113            'List all pages within the given namespace.'
114        );
115
116        $this->addCallback(
117            'dokuwiki.search',
118            'this:search',
119            array('struct','string'),
120            'Perform a fulltext search and return a list of matching pages'
121        );
122
123        $this->addCallback(
124            'dokuwiki.getTime',
125            'time',
126            array('int'),
127            'Return the current time at the wiki server.'
128        );
129
130        $this->addCallback(
131            'dokuwiki.setLocks',
132            'this:setLocks',
133            array('struct','struct'),
134            'Lock or unlock pages.'
135        );
136
137
138        $this->addCallback(
139            'dokuwiki.getTitle',
140            'this:getTitle',
141            array('string'),
142            'Returns the wiki title.',
143            true
144        );
145
146        $this->addCallback(
147            'dokuwiki.appendPage',
148            'this:appendPage',
149            array('int', 'string', 'string', 'struct'),
150            'Append text to a wiki page.'
151        );
152
153        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
154        $this->addCallback(
155            'wiki.getRPCVersionSupported',
156            'this:wiki_RPCVersion',
157            array('int'),
158            'Returns 2 with the supported RPC API version.',
159            true
160        );
161        $this->addCallback(
162            'wiki.getPage',
163            'this:rawPage',
164            array('string','string'),
165            'Get the raw Wiki text of page, latest version.'
166        );
167        $this->addCallback(
168            'wiki.getPageVersion',
169            'this:rawPage',
170            array('string','string','int'),
171            'Get the raw Wiki text of page.'
172        );
173        $this->addCallback(
174            'wiki.getPageHTML',
175            'this:htmlPage',
176            array('string','string'),
177            'Return page in rendered HTML, latest version.'
178        );
179        $this->addCallback(
180            'wiki.getPageHTMLVersion',
181            'this:htmlPage',
182            array('string','string','int'),
183            'Return page in rendered HTML.'
184        );
185        $this->addCallback(
186            'wiki.getAllPages',
187            'this:listPages',
188            array('struct'),
189            'Returns a list of all pages. The result is an array of utf8 pagenames.'
190        );
191        $this->addCallback(
192            'wiki.getAttachments',
193            'this:listAttachments',
194            array('struct', 'string', 'struct'),
195            'Returns a list of all media files.'
196        );
197        $this->addCallback(
198            'wiki.getBackLinks',
199            'this:listBackLinks',
200            array('struct','string'),
201            'Returns the pages that link to this page.'
202        );
203        $this->addCallback(
204            'wiki.getPageInfo',
205            'this:pageInfo',
206            array('struct','string'),
207            'Returns a struct with infos about the page.'
208        );
209        $this->addCallback(
210            'wiki.getPageInfoVersion',
211            'this:pageInfo',
212            array('struct','string','int'),
213            'Returns a struct with infos about the page.'
214        );
215        $this->addCallback(
216            'wiki.getPageVersions',
217            'this:pageVersions',
218            array('struct','string','int'),
219            'Returns the available revisions of the page.'
220        );
221        $this->addCallback(
222            'wiki.putPage',
223            'this:putPage',
224            array('int', 'string', 'string', 'struct'),
225            'Saves a wiki page.'
226        );
227        $this->addCallback(
228            'wiki.listLinks',
229            'this:listLinks',
230            array('struct','string'),
231            'Lists all links contained in a wiki page.'
232        );
233        $this->addCallback(
234            'wiki.getRecentChanges',
235            'this:getRecentChanges',
236            array('struct','int'),
237            'Returns a struct about all recent changes since given timestamp.'
238        );
239        $this->addCallback(
240            'wiki.getRecentMediaChanges',
241            'this:getRecentMediaChanges',
242            array('struct','int'),
243            'Returns a struct about all recent media changes since given timestamp.'
244        );
245        $this->addCallback(
246            'wiki.aclCheck',
247            'this:aclCheck',
248            array('int', 'string'),
249            'Returns the permissions of a given wiki page.'
250        );
251        $this->addCallback(
252            'wiki.putAttachment',
253            'this:putAttachment',
254            array('struct', 'string', 'base64', 'struct'),
255            'Upload a file to the wiki.'
256        );
257        $this->addCallback(
258            'wiki.deleteAttachment',
259            'this:deleteAttachment',
260            array('int', 'string'),
261            'Delete a file from the wiki.'
262        );
263        $this->addCallback(
264            'wiki.getAttachment',
265            'this:getAttachment',
266            array('base64', 'string'),
267            'Download a file from the wiki.'
268        );
269        $this->addCallback(
270            'wiki.getAttachmentInfo',
271            'this:getAttachmentInfo',
272            array('struct', 'string'),
273            'Returns a struct with infos about the attachment.'
274        );
275
276        /**
277         * Trigger XMLRPC_CALLBACK_REGISTER, action plugins can use this event
278         * to extend the XMLRPC interface and register their own callbacks.
279         *
280         * Event data:
281         *  The XMLRPC server object:
282         *
283         *  $event->data->addCallback() - register a callback, the second
284         *  paramter has to be of the form "plugin:<pluginname>:<plugin
285         *  method>"
286         *
287         *  $event->data->callbacks - an array which holds all awaylable
288         *  callbacks
289         */
290        trigger_event('XMLRPC_CALLBACK_REGISTER', $this);
291
292        $this->serve();
293    }
294
295    /**
296     * Return a raw wiki page
297     */
298    function rawPage($id,$rev=''){
299        if(auth_quickaclcheck($id) < AUTH_READ){
300            return new IXR_Error(1, 'You are not allowed to read this page');
301        }
302        $text = rawWiki($id,$rev);
303        if(!$text) {
304            return pageTemplate($id);
305        } else {
306            return $text;
307        }
308    }
309
310    /**
311     * Return a media file encoded in base64
312     *
313     * @author Gina Haeussge <osd@foosel.net>
314     */
315    function getAttachment($id){
316        $id = cleanID($id);
317        if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ)
318            return new IXR_Error(1, 'You are not allowed to read this file');
319
320        $file = mediaFN($id);
321        if (!@ file_exists($file))
322            return new IXR_Error(1, 'The requested file does not exist');
323
324        $data = io_readFile($file, false);
325        $base64 = base64_encode($data);
326        return $base64;
327    }
328
329    /**
330     * Return info about a media file
331     *
332     * @author Gina Haeussge <osd@foosel.net>
333     */
334    function getAttachmentInfo($id){
335        $id = cleanID($id);
336        $info = array(
337            'lastModified' => 0,
338            'size' => 0,
339        );
340
341        $file = mediaFN($id);
342        if ((auth_quickaclcheck(getNS($id).':*') >= AUTH_READ) && file_exists($file)){
343            $info['lastModified'] = new IXR_Date(filemtime($file));
344            $info['size'] = filesize($file);
345        }
346
347        return $info;
348    }
349
350    /**
351     * Return a wiki page rendered to html
352     */
353    function htmlPage($id,$rev=''){
354        if(auth_quickaclcheck($id) < AUTH_READ){
355            return new IXR_Error(1, 'You are not allowed to read this page');
356        }
357        return p_wiki_xhtml($id,$rev,false);
358    }
359
360    /**
361     * List all pages - we use the indexer list here
362     */
363    function listPages(){
364        $list  = array();
365        $pages = array_filter(array_filter(idx_getIndex('page', ''),
366                                           'isVisiblePage'),
367                              'page_exists');
368
369        foreach(array_keys($pages) as $idx) {
370            $perm = auth_quickaclcheck($pages[$idx]);
371            if($perm < AUTH_READ) {
372                continue;
373            }
374            $page = array();
375            $page['id'] = trim($pages[$idx]);
376            $page['perms'] = $perm;
377            $page['size'] = @filesize(wikiFN($pages[$idx]));
378            $page['lastModified'] = new IXR_Date(@filemtime(wikiFN($pages[$idx])));
379            $list[] = $page;
380        }
381
382        return $list;
383    }
384
385    /**
386     * List all pages in the given namespace (and below)
387     */
388    function readNamespace($ns,$opts){
389        global $conf;
390
391        if(!is_array($opts)) $opts=array();
392
393        $ns = cleanID($ns);
394        $dir = utf8_encodeFN(str_replace(':', '/', $ns));
395        $data = array();
396        $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
397        search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
398        return $data;
399    }
400
401    /**
402     * List all pages in the given namespace (and below)
403     */
404    function search($query){
405        require_once(DOKU_INC.'inc/fulltext.php');
406
407        $regex = '';
408        $data  = ft_pageSearch($query,$regex);
409        $pages = array();
410
411        // prepare additional data
412        $idx = 0;
413        foreach($data as $id => $score){
414            $file = wikiFN($id);
415
416            if($idx < FT_SNIPPET_NUMBER){
417                $snippet = ft_snippet($id,$regex);
418                $idx++;
419            }else{
420                $snippet = '';
421            }
422
423            $pages[] = array(
424                'id'      => $id,
425                'score'   => $score,
426                'rev'     => filemtime($file),
427                'mtime'   => filemtime($file),
428                'size'    => filesize($file),
429                'snippet' => $snippet,
430            );
431        }
432        return $pages;
433    }
434
435    /**
436     * Returns the wiki title.
437     */
438    function getTitle(){
439        global $conf;
440        return $conf['title'];
441    }
442
443    /**
444     * List all media files.
445     *
446     * Available options are 'recursive' for also including the subnamespaces
447     * in the listing, and 'pattern' for filtering the returned files against
448     * a regular expression matching their name.
449     *
450     * @author Gina Haeussge <osd@foosel.net>
451     */
452    function listAttachments($ns, $options = array()) {
453        global $conf;
454        global $lang;
455
456        $ns = cleanID($ns);
457
458        if (!is_array($options)) $options = array();
459        $options['skipacl'] = 0; // no ACL skipping for XMLRPC
460
461
462        if(auth_quickaclcheck($ns.':*') >= AUTH_READ) {
463            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
464
465            $data = array();
466            search($data, $conf['mediadir'], 'search_media', $options, $dir);
467            $len = count($data);
468            if(!$len) return array();
469
470            for($i=0; $i<$len; $i++) {
471                unset($data[$i]['meta']);
472                $data[$i]['lastModified'] = new IXR_Date($data[$i]['mtime']);
473            }
474            return $data;
475        } else {
476            return new IXR_Error(1, 'You are not allowed to list media files.');
477        }
478    }
479
480    /**
481     * Return a list of backlinks
482     */
483    function listBackLinks($id){
484        return ft_backlinks(cleanID($id));
485    }
486
487    /**
488     * Return some basic data about a page
489     */
490    function pageInfo($id,$rev=''){
491        if(auth_quickaclcheck($id) < AUTH_READ){
492            return new IXR_Error(1, 'You are not allowed to read this page');
493        }
494        $file = wikiFN($id,$rev);
495        $time = @filemtime($file);
496        if(!$time){
497            return new IXR_Error(10, 'The requested page does not exist');
498        }
499
500        $info = getRevisionInfo($id, $time, 1024);
501
502        $data = array(
503            'name'         => $id,
504            'lastModified' => new IXR_Date($time),
505            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
506            'version'      => $time
507        );
508
509        return ($data);
510    }
511
512    /**
513     * Save a wiki page
514     *
515     * @author Michael Klier <chi@chimeric.de>
516     */
517    function putPage($id, $text, $params) {
518        global $TEXT;
519        global $lang;
520        global $conf;
521
522        $id    = cleanID($id);
523        $TEXT  = cleanText($text);
524        $sum   = $params['sum'];
525        $minor = $params['minor'];
526
527        if(empty($id))
528            return new IXR_Error(1, 'Empty page ID');
529
530        if(!page_exists($id) && trim($TEXT) == '' ) {
531            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
532        }
533
534        if(auth_quickaclcheck($id) < AUTH_EDIT)
535            return new IXR_Error(1, 'You are not allowed to edit this page');
536
537        // Check, if page is locked
538        if(checklock($id))
539            return new IXR_Error(1, 'The page is currently locked');
540
541        // SPAM check
542        if(checkwordblock())
543            return new IXR_Error(1, 'Positive wordblock check');
544
545        // autoset summary on new pages
546        if(!page_exists($id) && empty($sum)) {
547            $sum = $lang['created'];
548        }
549
550        // autoset summary on deleted pages
551        if(page_exists($id) && empty($TEXT) && empty($sum)) {
552            $sum = $lang['deleted'];
553        }
554
555        lock($id);
556
557        saveWikiText($id,$TEXT,$sum,$minor);
558
559        unlock($id);
560
561        // run the indexer if page wasn't indexed yet
562        if(!@file_exists(metaFN($id, '.indexed'))) {
563            // try to aquire a lock
564            $lock = $conf['lockdir'].'/_indexer.lock';
565            while(!@mkdir($lock,$conf['dmode'])){
566                usleep(50);
567                if(time()-@filemtime($lock) > 60*5){
568                    // looks like a stale lock - remove it
569                    @rmdir($lock);
570                }else{
571                    return false;
572                }
573            }
574            if($conf['dperm']) chmod($lock, $conf['dperm']);
575
576            // do the work
577            idx_addPage($id);
578
579            // we're finished - save and free lock
580            io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
581            @rmdir($lock);
582        }
583
584        return 0;
585    }
586
587    /**
588     * Appends text to a wiki page.
589     */
590    function appendPage($id, $text, $params) {
591        $currentpage = $this->rawPage($id);
592        if (!is_string($currentpage)) {
593            return $currentpage;
594        }
595        return $this->putPage($id, $currentpage.$text, $params);
596    }
597
598    /**
599     * Uploads a file to the wiki.
600     *
601     * Michael Klier <chi@chimeric.de>
602     */
603    function putAttachment($id, $file, $params) {
604        global $conf;
605        global $lang;
606
607        $auth = auth_quickaclcheck(getNS($id).':*');
608        if($auth >= AUTH_UPLOAD) {
609            if(!isset($id)) {
610                return new IXR_ERROR(1, 'Filename not given.');
611            }
612
613            $ftmp = $conf['tmpdir'] . '/' . md5($id.clientIP());
614
615            // save temporary file
616            @unlink($ftmp);
617            $buff = base64_decode($file);
618            io_saveFile($ftmp, $buff);
619
620            // get filename
621            list($iext, $imime,$dl) = mimetype($id);
622            $id = cleanID($id);
623            $fn = mediaFN($id);
624
625            // get filetype regexp
626            $types = array_keys(getMimeTypes());
627            $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
628            $regex = join('|',$types);
629
630            // because a temp file was created already
631            if(preg_match('/\.('.$regex.')$/i',$fn)) {
632                //check for overwrite
633                $overwrite = @file_exists($fn);
634                if($overwrite && (!$params['ow'] || $auth < AUTH_DELETE)) {
635                    return new IXR_ERROR(1, $lang['uploadexist'].'1');
636                }
637                // check for valid content
638                $ok = media_contentcheck($ftmp, $imime);
639                if($ok == -1) {
640                    return new IXR_ERROR(1, sprintf($lang['uploadexist'].'2', ".$iext"));
641                } elseif($ok == -2) {
642                    return new IXR_ERROR(1, $lang['uploadspam']);
643                } elseif($ok == -3) {
644                    return new IXR_ERROR(1, $lang['uploadxss']);
645                }
646
647                // prepare event data
648                $data[0] = $ftmp;
649                $data[1] = $fn;
650                $data[2] = $id;
651                $data[3] = $imime;
652                $data[4] = $overwrite;
653
654                // trigger event
655                return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true);
656
657            } else {
658                return new IXR_ERROR(1, $lang['uploadwrong']);
659            }
660        } else {
661            return new IXR_ERROR(1, "You don't have permissions to upload files.");
662        }
663    }
664
665    /**
666     * Deletes a file from the wiki.
667     *
668     * @author Gina Haeussge <osd@foosel.net>
669     */
670    function deleteAttachment($id){
671        $auth = auth_quickaclcheck(getNS($id).':*');
672        if($auth < AUTH_DELETE) return new IXR_ERROR(1, "You don't have permissions to delete files.");
673        global $conf;
674        global $lang;
675
676        // check for references if needed
677        $mediareferences = array();
678        if($conf['refcheck']){
679            $mediareferences = ft_mediause($id,$conf['refshow']);
680        }
681
682        if(!count($mediareferences)){
683            $file = mediaFN($id);
684            if(@unlink($file)){
685                addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE);
686                io_sweepNS($id,'mediadir');
687                return 0;
688            }
689            //something went wrong
690               return new IXR_ERROR(1, 'Could not delete file');
691        } else {
692            return new IXR_ERROR(1, 'File is still referenced');
693        }
694    }
695
696    /**
697     * Moves the temporary file to its final destination.
698     *
699     * Michael Klier <chi@chimeric.de>
700     */
701    function _media_upload_action($data) {
702        global $conf;
703
704        if(is_array($data) && count($data)===5) {
705            io_createNamespace($data[2], 'media');
706            if(rename($data[0], $data[1])) {
707                chmod($data[1], $conf['fmode']);
708                media_notify($data[2], $data[1], $data[3]);
709                // add a log entry to the media changelog
710                if ($data[4]) {
711                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_EDIT);
712                } else {
713                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_CREATE);
714                }
715                return $data[2];
716            } else {
717                return new IXR_ERROR(1, 'Upload failed.');
718            }
719        } else {
720            return new IXR_ERROR(1, 'Upload failed.');
721        }
722    }
723
724    /**
725    * Returns the permissions of a given wiki page
726    */
727    function aclCheck($id) {
728        return auth_quickaclcheck($id);
729    }
730
731    /**
732     * Lists all links contained in a wiki page
733     *
734     * @author Michael Klier <chi@chimeric.de>
735     */
736    function listLinks($id) {
737        if(auth_quickaclcheck($id) < AUTH_READ){
738            return new IXR_Error(1, 'You are not allowed to read this page');
739        }
740        $links = array();
741
742        // resolve page instructions
743        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
744
745        // instantiate new Renderer - needed for interwiki links
746        include(DOKU_INC.'inc/parser/xhtml.php');
747        $Renderer = new Doku_Renderer_xhtml();
748        $Renderer->interwiki = getInterwiki();
749
750        // parse parse instructions
751        foreach($ins as $in) {
752            $link = array();
753            switch($in[0]) {
754                case 'internallink':
755                    $link['type'] = 'local';
756                    $link['page'] = $in[1][0];
757                    $link['href'] = wl($in[1][0]);
758                    array_push($links,$link);
759                    break;
760                case 'externallink':
761                    $link['type'] = 'extern';
762                    $link['page'] = $in[1][0];
763                    $link['href'] = $in[1][0];
764                    array_push($links,$link);
765                    break;
766                case 'interwikilink':
767                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
768                    $link['type'] = 'extern';
769                    $link['page'] = $url;
770                    $link['href'] = $url;
771                    array_push($links,$link);
772                    break;
773            }
774        }
775
776        return ($links);
777    }
778
779    /**
780     * Returns a list of recent changes since give timestamp
781     *
782     * @author Michael Hamann <michael@content-space.de>
783     * @author Michael Klier <chi@chimeric.de>
784     */
785    function getRecentChanges($timestamp) {
786        if(strlen($timestamp) != 10)
787            return new IXR_Error(20, 'The provided value is not a valid timestamp');
788
789        $recents = getRecentsSince($timestamp);
790
791        $changes = array();
792
793        foreach ($recents as $recent) {
794            $change = array();
795            $change['name']         = $recent['id'];
796            $change['lastModified'] = new IXR_Date($recent['date']);
797            $change['author']       = $recent['user'];
798            $change['version']      = $recent['date'];
799            $change['perms']        = $recent['perms'];
800            $change['size']         = @filesize(wikiFN($recent['id']));
801            array_push($changes, $change);
802        }
803
804        if (!empty($changes)) {
805            return $changes;
806        } else {
807            // in case we still have nothing at this point
808            return new IXR_Error(30, 'There are no changes in the specified timeframe');
809        }
810    }
811
812    /**
813     * Returns a list of recent media changes since give timestamp
814     *
815     * @author Michael Hamann <michael@content-space.de>
816     * @author Michael Klier <chi@chimeric.de>
817     */
818    function getRecentMediaChanges($timestamp) {
819        if(strlen($timestamp) != 10)
820            return new IXR_Error(20, 'The provided value is not a valid timestamp');
821
822        $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
823
824        $changes = array();
825
826        foreach ($recents as $recent) {
827            $change = array();
828            $change['name']         = $recent['id'];
829            $change['lastModified'] = new IXR_Date($recent['date']);
830            $change['author']       = $recent['user'];
831            $change['version']      = $recent['date'];
832            $change['perms']        = $recent['perms'];
833            $change['size']         = @filesize(mediaFN($recent['id']));
834            array_push($changes, $change);
835        }
836
837        if (!empty($changes)) {
838            return $changes;
839        } else {
840            // in case we still have nothing at this point
841            return new IXR_Error(30, 'There are no changes in the specified timeframe');
842        }
843    }
844
845    /**
846     * Returns a list of available revisions of a given wiki page
847     *
848     * @author Michael Klier <chi@chimeric.de>
849     */
850    function pageVersions($id, $first) {
851        global $conf;
852
853        $versions = array();
854
855        if(empty($id))
856            return new IXR_Error(1, 'Empty page ID');
857
858        $revisions = getRevisions($id, $first, $conf['recent']+1);
859
860        if(count($revisions)==0 && $first!=0) {
861            $first=0;
862            $revisions = getRevisions($id, $first, $conf['recent']+1);
863        }
864
865        if(count($revisions)>0 && $first==0) {
866            array_unshift($revisions, '');  // include current revision
867            array_pop($revisions);          // remove extra log entry
868        }
869
870        $hasNext = false;
871        if(count($revisions)>$conf['recent']) {
872            $hasNext = true;
873            array_pop($revisions); // remove extra log entry
874        }
875
876        if(!empty($revisions)) {
877            foreach($revisions as $rev) {
878                $file = wikiFN($id,$rev);
879                $time = @filemtime($file);
880                // we check if the page actually exists, if this is not the
881                // case this can lead to less pages being returned than
882                // specified via $conf['recent']
883                if($time){
884                    $info = getRevisionInfo($id, $time, 1024);
885                    if(!empty($info)) {
886                        $data['user'] = $info['user'];
887                        $data['ip']   = $info['ip'];
888                        $data['type'] = $info['type'];
889                        $data['sum']  = $info['sum'];
890                        $data['modified'] = new IXR_Date($info['date']);
891                        $data['version'] = $info['date'];
892                        array_push($versions, $data);
893                    }
894                }
895            }
896            return $versions;
897        } else {
898            return array();
899        }
900    }
901
902    /**
903     * The version of Wiki RPC API supported
904     */
905    function wiki_RPCVersion(){
906        return 2;
907    }
908
909
910    /**
911     * Locks or unlocks a given batch of pages
912     *
913     * Give an associative array with two keys: lock and unlock. Both should contain a
914     * list of pages to lock or unlock
915     *
916     * Returns an associative array with the keys locked, lockfail, unlocked and
917     * unlockfail, each containing lists of pages.
918     */
919    function setLocks($set){
920        $locked     = array();
921        $lockfail   = array();
922        $unlocked   = array();
923        $unlockfail = array();
924
925        foreach((array) $set['lock'] as $id){
926            if(checklock($id)){
927                $lockfail[] = $id;
928            }else{
929                lock($id);
930                $locked[] = $id;
931            }
932        }
933
934        foreach((array) $set['unlock'] as $id){
935            if(unlock($id)){
936                $unlocked[] = $id;
937            }else{
938                $unlockfail[] = $id;
939            }
940        }
941
942        return array(
943            'locked'     => $locked,
944            'lockfail'   => $lockfail,
945            'unlocked'   => $unlocked,
946            'unlockfail' => $unlockfail,
947        );
948    }
949
950    function getAPIVersion(){
951        return DOKU_XMLRPC_API_VERSION;
952    }
953
954    function login($user,$pass){
955        global $conf;
956        global $auth;
957        if(!$conf['useacl']) return 0;
958        if(!$auth) return 0;
959        if($auth->canDo('external')){
960            return $auth->trustExternal($user,$pass,false);
961        }else{
962            return auth_login($user,$pass,false,true);
963        }
964    }
965
966
967}
968
969$server = new dokuwiki_xmlrpc_server();
970
971// vim:ts=4:sw=4:et:
972