xref: /dokuwiki/lib/exe/xmlrpc.php (revision e3776c06c37cc197709dac60892604dfea894ac2)
1797c0d11SAndreas Gohr<?php
27aec69d1SGuy Brandif(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
3797c0d11SAndreas Gohr
4797c0d11SAndreas Gohr// fix when '<?xml' isn't on the very first line
5797c0d11SAndreas Gohrif(isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
6797c0d11SAndreas Gohr
7445e8084SAndreas Gohr/**
8445e8084SAndreas Gohr * Increased whenever the API is changed
9445e8084SAndreas Gohr */
10ba9418bcSHakan Sandelldefine('DOKU_XMLRPC_API_VERSION',5);
11797c0d11SAndreas Gohr
12797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
13797c0d11SAndreas Gohrsession_write_close();  //close session
14593bf8f6SMichael Klier
153ee5b583SAndreas Gohrif(!$conf['xmlrpc']) die('XML-RPC server not enabled.');
16593bf8f6SMichael Klier
17797c0d11SAndreas Gohr/**
18797c0d11SAndreas Gohr * Contains needed wrapper functions and registers all available
19797c0d11SAndreas Gohr * XMLRPC functions.
20797c0d11SAndreas Gohr */
21797c0d11SAndreas Gohrclass dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
22797c0d11SAndreas Gohr    var $methods       = array();
233ee5b583SAndreas Gohr    var $public_methods = array();
243ee5b583SAndreas Gohr
253ee5b583SAndreas Gohr    /**
263ee5b583SAndreas Gohr     * Checks if the current user is allowed to execute non anonymous methods
273ee5b583SAndreas Gohr     */
283ee5b583SAndreas Gohr    function checkAuth(){
293ee5b583SAndreas Gohr        global $conf;
303ee5b583SAndreas Gohr        global $USERINFO;
313ee5b583SAndreas Gohr
323ee5b583SAndreas Gohr        if(!$conf['useacl']) return true; //no ACL - then no checks
333ee5b583SAndreas Gohr
343ee5b583SAndreas Gohr        $allowed = explode(',',$conf['xmlrpcuser']);
353ee5b583SAndreas Gohr        $allowed = array_map('trim', $allowed);
363ee5b583SAndreas Gohr        $allowed = array_unique($allowed);
373ee5b583SAndreas Gohr        $allowed = array_filter($allowed);
383ee5b583SAndreas Gohr
393ee5b583SAndreas Gohr        if(!count($allowed)) return true; //no restrictions
403ee5b583SAndreas Gohr
413ee5b583SAndreas Gohr        $user   = $_SERVER['REMOTE_USER'];
423ee5b583SAndreas Gohr        $groups = (array) $USERINFO['grps'];
433ee5b583SAndreas Gohr
443ee5b583SAndreas Gohr        if(in_array($user,$allowed)) return true; //user explicitly mentioned
453ee5b583SAndreas Gohr
463ee5b583SAndreas Gohr        //check group memberships
473ee5b583SAndreas Gohr        foreach($groups as $group){
483ee5b583SAndreas Gohr            if(in_array('@'.$group,$allowed)) return true;
493ee5b583SAndreas Gohr        }
503ee5b583SAndreas Gohr
513ee5b583SAndreas Gohr        //still here? no access!
523ee5b583SAndreas Gohr        return false;
533ee5b583SAndreas Gohr    }
543ee5b583SAndreas Gohr
553ee5b583SAndreas Gohr    /**
563ee5b583SAndreas Gohr     * Adds a callback, extends parent method
573ee5b583SAndreas Gohr     *
583ee5b583SAndreas Gohr     * add another parameter to define if anonymous access to
593ee5b583SAndreas Gohr     * this method should be granted.
603ee5b583SAndreas Gohr     */
613ee5b583SAndreas Gohr    function addCallback($method, $callback, $args, $help, $public=false){
623ee5b583SAndreas Gohr        if($public) $this->public_methods[] = $method;
633ee5b583SAndreas Gohr        return parent::addCallback($method, $callback, $args, $help);
643ee5b583SAndreas Gohr    }
653ee5b583SAndreas Gohr
663ee5b583SAndreas Gohr    /**
673ee5b583SAndreas Gohr     * Execute a call, extends parent method
683ee5b583SAndreas Gohr     *
693ee5b583SAndreas Gohr     * Checks for authentication first
703ee5b583SAndreas Gohr     */
713ee5b583SAndreas Gohr    function call($methodname, $args){
723ee5b583SAndreas Gohr        if(!in_array($methodname,$this->public_methods) && !$this->checkAuth()){
733ee5b583SAndreas Gohr            return new IXR_Error(-32603, 'server error. not authorized to call method "'.$methodname.'".');
743ee5b583SAndreas Gohr        }
753ee5b583SAndreas Gohr        return parent::call($methodname, $args);
763ee5b583SAndreas Gohr    }
77797c0d11SAndreas Gohr
78797c0d11SAndreas Gohr    /**
79797c0d11SAndreas Gohr     * Constructor. Register methods and run Server
80797c0d11SAndreas Gohr     */
81797c0d11SAndreas Gohr    function dokuwiki_xmlrpc_server(){
82797c0d11SAndreas Gohr        $this->IXR_IntrospectionServer();
83797c0d11SAndreas Gohr
84797c0d11SAndreas Gohr        /* DokuWiki's own methods */
85797c0d11SAndreas Gohr        $this->addCallback(
86445e8084SAndreas Gohr            'dokuwiki.getXMLRPCAPIVersion',
87445e8084SAndreas Gohr            'this:getAPIVersion',
88445e8084SAndreas Gohr            array('integer'),
893ee5b583SAndreas Gohr            'Returns the XMLRPC API version.',
903ee5b583SAndreas Gohr            true
91445e8084SAndreas Gohr        );
92445e8084SAndreas Gohr
93445e8084SAndreas Gohr        $this->addCallback(
94797c0d11SAndreas Gohr            'dokuwiki.getVersion',
95797c0d11SAndreas Gohr            'getVersion',
96797c0d11SAndreas Gohr            array('string'),
973ee5b583SAndreas Gohr            'Returns the running DokuWiki version.',
983ee5b583SAndreas Gohr            true
99797c0d11SAndreas Gohr        );
100797c0d11SAndreas Gohr
1011b11c097SAndreas Gohr        $this->addCallback(
102445e8084SAndreas Gohr            'dokuwiki.login',
103445e8084SAndreas Gohr            'this:login',
104445e8084SAndreas Gohr            array('integer','string','string'),
1053ee5b583SAndreas Gohr            'Tries to login with the given credentials and sets auth cookies.',
1063ee5b583SAndreas Gohr            true
107445e8084SAndreas Gohr        );
108445e8084SAndreas Gohr
109445e8084SAndreas Gohr        $this->addCallback(
1101b11c097SAndreas Gohr            'dokuwiki.getPagelist',
1111b11c097SAndreas Gohr            'this:readNamespace',
1121b11c097SAndreas Gohr            array('struct','string','struct'),
1131b11c097SAndreas Gohr            'List all pages within the given namespace.'
1141b11c097SAndreas Gohr        );
1151b11c097SAndreas Gohr
1161b11c097SAndreas Gohr        $this->addCallback(
117f71f4f53SAndreas Gohr            'dokuwiki.search',
118f71f4f53SAndreas Gohr            'this:search',
119f71f4f53SAndreas Gohr            array('struct','string'),
120f71f4f53SAndreas Gohr            'Perform a fulltext search and return a list of matching pages'
121f71f4f53SAndreas Gohr        );
122f71f4f53SAndreas Gohr
123f71f4f53SAndreas Gohr        $this->addCallback(
1241b11c097SAndreas Gohr            'dokuwiki.getTime',
1251b11c097SAndreas Gohr            'time',
1261b11c097SAndreas Gohr            array('int'),
1271b11c097SAndreas Gohr            'Return the current time at the wiki server.'
1281b11c097SAndreas Gohr        );
1291b11c097SAndreas Gohr
13028ec3c76SAndreas Gohr        $this->addCallback(
13128ec3c76SAndreas Gohr            'dokuwiki.setLocks',
13228ec3c76SAndreas Gohr            'this:setLocks',
13328ec3c76SAndreas Gohr            array('struct','struct'),
13428ec3c76SAndreas Gohr            'Lock or unlock pages.'
13528ec3c76SAndreas Gohr        );
13628ec3c76SAndreas Gohr
137e6f4c9d4SGeorges-Etienne Legendre
138e6f4c9d4SGeorges-Etienne Legendre        $this->addCallback(
139e6f4c9d4SGeorges-Etienne Legendre            'dokuwiki.getTitle',
140e6f4c9d4SGeorges-Etienne Legendre            'this:getTitle',
141e6f4c9d4SGeorges-Etienne Legendre            array('string'),
142e6f4c9d4SGeorges-Etienne Legendre            'Returns the wiki title.',
143e6f4c9d4SGeorges-Etienne Legendre            true
144e6f4c9d4SGeorges-Etienne Legendre        );
145e6f4c9d4SGeorges-Etienne Legendre
146ba9418bcSHakan Sandell        $this->addCallback(
147ba9418bcSHakan Sandell            'dokuwiki.appendPage',
148ba9418bcSHakan Sandell            'this:appendPage',
149ba9418bcSHakan Sandell            array('int', 'string', 'string', 'struct'),
150ba9418bcSHakan Sandell            'Append text to a wiki page.'
151ba9418bcSHakan Sandell        );
152ba9418bcSHakan Sandell
153797c0d11SAndreas Gohr        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
154797c0d11SAndreas Gohr        $this->addCallback(
155797c0d11SAndreas Gohr            'wiki.getRPCVersionSupported',
156797c0d11SAndreas Gohr            'this:wiki_RPCVersion',
157797c0d11SAndreas Gohr            array('int'),
1583ee5b583SAndreas Gohr            'Returns 2 with the supported RPC API version.',
1593ee5b583SAndreas Gohr            true
160797c0d11SAndreas Gohr        );
161797c0d11SAndreas Gohr        $this->addCallback(
162797c0d11SAndreas Gohr            'wiki.getPage',
163797c0d11SAndreas Gohr            'this:rawPage',
164797c0d11SAndreas Gohr            array('string','string'),
165797c0d11SAndreas Gohr            'Get the raw Wiki text of page, latest version.'
166797c0d11SAndreas Gohr        );
167797c0d11SAndreas Gohr        $this->addCallback(
168797c0d11SAndreas Gohr            'wiki.getPageVersion',
169797c0d11SAndreas Gohr            'this:rawPage',
170797c0d11SAndreas Gohr            array('string','string','int'),
171797c0d11SAndreas Gohr            'Get the raw Wiki text of page.'
172797c0d11SAndreas Gohr        );
173797c0d11SAndreas Gohr        $this->addCallback(
174797c0d11SAndreas Gohr            'wiki.getPageHTML',
175797c0d11SAndreas Gohr            'this:htmlPage',
176797c0d11SAndreas Gohr            array('string','string'),
177797c0d11SAndreas Gohr            'Return page in rendered HTML, latest version.'
178797c0d11SAndreas Gohr        );
179797c0d11SAndreas Gohr        $this->addCallback(
180797c0d11SAndreas Gohr            'wiki.getPageHTMLVersion',
181797c0d11SAndreas Gohr            'this:htmlPage',
182797c0d11SAndreas Gohr            array('string','string','int'),
183797c0d11SAndreas Gohr            'Return page in rendered HTML.'
184797c0d11SAndreas Gohr        );
185797c0d11SAndreas Gohr        $this->addCallback(
186797c0d11SAndreas Gohr            'wiki.getAllPages',
187797c0d11SAndreas Gohr            'this:listPages',
188797c0d11SAndreas Gohr            array('struct'),
189797c0d11SAndreas Gohr            'Returns a list of all pages. The result is an array of utf8 pagenames.'
190797c0d11SAndreas Gohr        );
191797c0d11SAndreas Gohr        $this->addCallback(
19226bec61eSMichael Klier            'wiki.getAttachments',
19326bec61eSMichael Klier            'this:listAttachments',
194c63d1645SGina Haeussge            array('struct', 'string', 'struct'),
19526bec61eSMichael Klier            'Returns a list of all media files.'
19626bec61eSMichael Klier        );
19726bec61eSMichael Klier        $this->addCallback(
198797c0d11SAndreas Gohr            'wiki.getBackLinks',
199797c0d11SAndreas Gohr            'this:listBackLinks',
200797c0d11SAndreas Gohr            array('struct','string'),
201797c0d11SAndreas Gohr            'Returns the pages that link to this page.'
202797c0d11SAndreas Gohr        );
203797c0d11SAndreas Gohr        $this->addCallback(
204797c0d11SAndreas Gohr            'wiki.getPageInfo',
205797c0d11SAndreas Gohr            'this:pageInfo',
206797c0d11SAndreas Gohr            array('struct','string'),
207797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
208797c0d11SAndreas Gohr        );
209797c0d11SAndreas Gohr        $this->addCallback(
210797c0d11SAndreas Gohr            'wiki.getPageInfoVersion',
211797c0d11SAndreas Gohr            'this:pageInfo',
212797c0d11SAndreas Gohr            array('struct','string','int'),
213797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
214797c0d11SAndreas Gohr        );
2153a1dad2dSDennis Ploeger        $this->addCallback(
21673056168SMichael Klier            'wiki.getPageVersions',
21773056168SMichael Klier            'this:pageVersions',
21873056168SMichael Klier            array('struct','string','int'),
21973056168SMichael Klier            'Returns the available revisions of the page.'
22073056168SMichael Klier        );
22173056168SMichael Klier        $this->addCallback(
2223a1dad2dSDennis Ploeger            'wiki.putPage',
2233a1dad2dSDennis Ploeger            'this:putPage',
224222572bfSMichael Klier            array('int', 'string', 'string', 'struct'),
225fdd2e9d6SMichael Klier            'Saves a wiki page.'
2263a1dad2dSDennis Ploeger        );
227beccd742SMichael Klier        $this->addCallback(
228beccd742SMichael Klier            'wiki.listLinks',
229beccd742SMichael Klier            'this:listLinks',
230beccd742SMichael Klier            array('struct','string'),
231fdd2e9d6SMichael Klier            'Lists all links contained in a wiki page.'
232beccd742SMichael Klier        );
23363dd0d58SMichael Klier        $this->addCallback(
23463dd0d58SMichael Klier            'wiki.getRecentChanges',
23563dd0d58SMichael Klier            'this:getRecentChanges',
23663dd0d58SMichael Klier            array('struct','int'),
23799c8d7f2Smichael            'Returns a struct about all recent changes since given timestamp.'
23899c8d7f2Smichael        );
23999c8d7f2Smichael        $this->addCallback(
24099c8d7f2Smichael            'wiki.getRecentMediaChanges',
24199c8d7f2Smichael            'this:getRecentMediaChanges',
24299c8d7f2Smichael            array('struct','int'),
24399c8d7f2Smichael            'Returns a struct about all recent media changes since given timestamp.'
24463dd0d58SMichael Klier        );
245e62b9ea5SMichael Klier        $this->addCallback(
246e62b9ea5SMichael Klier            'wiki.aclCheck',
247e62b9ea5SMichael Klier            'this:aclCheck',
248c63d1645SGina Haeussge            array('int', 'string'),
249e62b9ea5SMichael Klier            'Returns the permissions of a given wiki page.'
250e62b9ea5SMichael Klier        );
2512aca132fSMichael Klier        $this->addCallback(
2522aca132fSMichael Klier            'wiki.putAttachment',
2532aca132fSMichael Klier            'this:putAttachment',
2542aca132fSMichael Klier            array('struct', 'string', 'base64', 'struct'),
2552aca132fSMichael Klier            'Upload a file to the wiki.'
2562aca132fSMichael Klier        );
257cfef3001SGina Haeussge        $this->addCallback(
258f01ff8c1SGina Haeussge            'wiki.deleteAttachment',
259f01ff8c1SGina Haeussge            'this:deleteAttachment',
260f01ff8c1SGina Haeussge            array('int', 'string'),
261f01ff8c1SGina Haeussge            'Delete a file from the wiki.'
262f01ff8c1SGina Haeussge        );
263f01ff8c1SGina Haeussge        $this->addCallback(
264cfef3001SGina Haeussge            'wiki.getAttachment',
265cfef3001SGina Haeussge            'this:getAttachment',
266c63d1645SGina Haeussge            array('base64', 'string'),
267cfef3001SGina Haeussge            'Download a file from the wiki.'
268cfef3001SGina Haeussge        );
2695672e868SGina Haeussge        $this->addCallback(
2705672e868SGina Haeussge            'wiki.getAttachmentInfo',
2715672e868SGina Haeussge            'this:getAttachmentInfo',
272c63d1645SGina Haeussge            array('struct', 'string'),
2735672e868SGina Haeussge            'Returns a struct with infos about the attachment.'
2745672e868SGina Haeussge        );
275797c0d11SAndreas Gohr
276bb32615dSMichael Klier        /**
277bb32615dSMichael Klier         * Trigger XMLRPC_CALLBACK_REGISTER, action plugins can use this event
278bb32615dSMichael Klier         * to extend the XMLRPC interface and register their own callbacks.
279bb32615dSMichael Klier         *
280bb32615dSMichael Klier         * Event data:
281bb32615dSMichael Klier         *  The XMLRPC server object:
282bb32615dSMichael Klier         *
283bb32615dSMichael Klier         *  $event->data->addCallback() - register a callback, the second
284bb32615dSMichael Klier         *  paramter has to be of the form "plugin:<pluginname>:<plugin
285bb32615dSMichael Klier         *  method>"
286bb32615dSMichael Klier         *
287bb32615dSMichael Klier         *  $event->data->callbacks - an array which holds all awaylable
288bb32615dSMichael Klier         *  callbacks
289bb32615dSMichael Klier         */
290bb32615dSMichael Klier        trigger_event('XMLRPC_CALLBACK_REGISTER', $this);
291bb32615dSMichael Klier
292797c0d11SAndreas Gohr        $this->serve();
293797c0d11SAndreas Gohr    }
294797c0d11SAndreas Gohr
295797c0d11SAndreas Gohr    /**
296797c0d11SAndreas Gohr     * Return a raw wiki page
297797c0d11SAndreas Gohr     */
298797c0d11SAndreas Gohr    function rawPage($id,$rev=''){
299797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
300797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
301797c0d11SAndreas Gohr        }
3022c176304SMichael Klier        $text = rawWiki($id,$rev);
3032c176304SMichael Klier        if(!$text) {
304fe17917eSAdrian Lang            return pageTemplate($id);
3052c176304SMichael Klier        } else {
3062c176304SMichael Klier            return $text;
3072c176304SMichael Klier        }
308797c0d11SAndreas Gohr    }
309797c0d11SAndreas Gohr
310797c0d11SAndreas Gohr    /**
311cfef3001SGina Haeussge     * Return a media file encoded in base64
312c63d1645SGina Haeussge     *
313c63d1645SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
314cfef3001SGina Haeussge     */
315cfef3001SGina Haeussge    function getAttachment($id){
316c63d1645SGina Haeussge        $id = cleanID($id);
317cfef3001SGina Haeussge        if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ)
318cfef3001SGina Haeussge            return new IXR_Error(1, 'You are not allowed to read this file');
319cfef3001SGina Haeussge
320cfef3001SGina Haeussge        $file = mediaFN($id);
321cfef3001SGina Haeussge        if (!@ file_exists($file))
322cfef3001SGina Haeussge            return new IXR_Error(1, 'The requested file does not exist');
323cfef3001SGina Haeussge
324cfef3001SGina Haeussge        $data = io_readFile($file, false);
325cfef3001SGina Haeussge        $base64 = base64_encode($data);
326cfef3001SGina Haeussge        return $base64;
327cfef3001SGina Haeussge    }
328cfef3001SGina Haeussge
329cfef3001SGina Haeussge    /**
3305672e868SGina Haeussge     * Return info about a media file
3315672e868SGina Haeussge     *
3325672e868SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
3335672e868SGina Haeussge     */
3345672e868SGina Haeussge    function getAttachmentInfo($id){
3355672e868SGina Haeussge        $id = cleanID($id);
3365672e868SGina Haeussge        $info = array(
3375672e868SGina Haeussge            'lastModified' => 0,
3385672e868SGina Haeussge            'size' => 0,
3395672e868SGina Haeussge        );
3405672e868SGina Haeussge
3415672e868SGina Haeussge        $file = mediaFN($id);
3425672e868SGina Haeussge        if ((auth_quickaclcheck(getNS($id).':*') >= AUTH_READ) && file_exists($file)){
3435672e868SGina Haeussge            $info['lastModified'] = new IXR_Date(filemtime($file));
3445672e868SGina Haeussge            $info['size'] = filesize($file);
3455672e868SGina Haeussge        }
3465672e868SGina Haeussge
3475672e868SGina Haeussge        return $info;
3485672e868SGina Haeussge    }
3495672e868SGina Haeussge
3505672e868SGina Haeussge    /**
351797c0d11SAndreas Gohr     * Return a wiki page rendered to html
352797c0d11SAndreas Gohr     */
353797c0d11SAndreas Gohr    function htmlPage($id,$rev=''){
354797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
355797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
356797c0d11SAndreas Gohr        }
357797c0d11SAndreas Gohr        return p_wiki_xhtml($id,$rev,false);
358797c0d11SAndreas Gohr    }
359797c0d11SAndreas Gohr
360797c0d11SAndreas Gohr    /**
361797c0d11SAndreas Gohr     * List all pages - we use the indexer list here
362797c0d11SAndreas Gohr     */
363797c0d11SAndreas Gohr    function listPages(){
364dfd13e55SMichael Klier        $list  = array();
365a0070b52SAdrian Lang        $pages = array_filter(array_filter(idx_getIndex('page', ''),
366a0070b52SAdrian Lang                                           'isVisiblePage'),
367a0070b52SAdrian Lang                              'page_exists');
368dfd13e55SMichael Klier
369dfd13e55SMichael Klier        foreach(array_keys($pages) as $idx) {
370dfd13e55SMichael Klier            $perm = auth_quickaclcheck($pages[$idx]);
371a0070b52SAdrian Lang            if($perm < AUTH_READ) {
372a0070b52SAdrian Lang                continue;
373a0070b52SAdrian Lang            }
374dfd13e55SMichael Klier            $page = array();
375dfd13e55SMichael Klier            $page['id'] = trim($pages[$idx]);
376dfd13e55SMichael Klier            $page['perms'] = $perm;
377dfd13e55SMichael Klier            $page['size'] = @filesize(wikiFN($pages[$idx]));
378e070c6f3SGina Haeussge            $page['lastModified'] = new IXR_Date(@filemtime(wikiFN($pages[$idx])));
379dfd13e55SMichael Klier            $list[] = $page;
380dfd13e55SMichael Klier        }
381dfd13e55SMichael Klier
382dfd13e55SMichael Klier        return $list;
383797c0d11SAndreas Gohr    }
384797c0d11SAndreas Gohr
385797c0d11SAndreas Gohr    /**
3861b11c097SAndreas Gohr     * List all pages in the given namespace (and below)
3871b11c097SAndreas Gohr     */
3881b11c097SAndreas Gohr    function readNamespace($ns,$opts){
3891b11c097SAndreas Gohr        global $conf;
3901b11c097SAndreas Gohr
3911b11c097SAndreas Gohr        if(!is_array($opts)) $opts=array();
3921b11c097SAndreas Gohr
3931b11c097SAndreas Gohr        $ns = cleanID($ns);
3941b11c097SAndreas Gohr        $dir = utf8_encodeFN(str_replace(':', '/', $ns));
3951b11c097SAndreas Gohr        $data = array();
3966fc3aa1aSAndreas Gohr        $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
3971b11c097SAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
3981b11c097SAndreas Gohr        return $data;
3991b11c097SAndreas Gohr    }
4001b11c097SAndreas Gohr
4011b11c097SAndreas Gohr    /**
402f71f4f53SAndreas Gohr     * List all pages in the given namespace (and below)
403f71f4f53SAndreas Gohr     */
404f71f4f53SAndreas Gohr    function search($query){
405f71f4f53SAndreas Gohr        require_once(DOKU_INC.'inc/fulltext.php');
406f71f4f53SAndreas Gohr
407f71f4f53SAndreas Gohr        $regex = '';
408f71f4f53SAndreas Gohr        $data  = ft_pageSearch($query,$regex);
409f71f4f53SAndreas Gohr        $pages = array();
410f71f4f53SAndreas Gohr
411f71f4f53SAndreas Gohr        // prepare additional data
412f71f4f53SAndreas Gohr        $idx = 0;
413f71f4f53SAndreas Gohr        foreach($data as $id => $score){
414f71f4f53SAndreas Gohr            $file = wikiFN($id);
415f71f4f53SAndreas Gohr
416f71f4f53SAndreas Gohr            if($idx < FT_SNIPPET_NUMBER){
417f71f4f53SAndreas Gohr                $snippet = ft_snippet($id,$regex);
418f71f4f53SAndreas Gohr                $idx++;
419f71f4f53SAndreas Gohr            }else{
420f71f4f53SAndreas Gohr                $snippet = '';
421f71f4f53SAndreas Gohr            }
422f71f4f53SAndreas Gohr
423f71f4f53SAndreas Gohr            $pages[] = array(
424f71f4f53SAndreas Gohr                'id'      => $id,
425f71f4f53SAndreas Gohr                'score'   => $score,
426f71f4f53SAndreas Gohr                'rev'     => filemtime($file),
427f71f4f53SAndreas Gohr                'mtime'   => filemtime($file),
428f71f4f53SAndreas Gohr                'size'    => filesize($file),
429f71f4f53SAndreas Gohr                'snippet' => $snippet,
430f71f4f53SAndreas Gohr            );
431f71f4f53SAndreas Gohr        }
432ac1ffddeSGeorges-Etienne Legendre        return $pages;
433f71f4f53SAndreas Gohr    }
434f71f4f53SAndreas Gohr
435e6f4c9d4SGeorges-Etienne Legendre    /**
436e6f4c9d4SGeorges-Etienne Legendre     * Returns the wiki title.
437e6f4c9d4SGeorges-Etienne Legendre     */
438e6f4c9d4SGeorges-Etienne Legendre    function getTitle(){
439e6f4c9d4SGeorges-Etienne Legendre        global $conf;
440e6f4c9d4SGeorges-Etienne Legendre        return $conf['title'];
441e6f4c9d4SGeorges-Etienne Legendre    }
442f71f4f53SAndreas Gohr
443f71f4f53SAndreas Gohr    /**
44426bec61eSMichael Klier     * List all media files.
4453275953aSGina Haeussge     *
4463275953aSGina Haeussge     * Available options are 'recursive' for also including the subnamespaces
4473275953aSGina Haeussge     * in the listing, and 'pattern' for filtering the returned files against
4483275953aSGina Haeussge     * a regular expression matching their name.
4493275953aSGina Haeussge     *
4503275953aSGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
45126bec61eSMichael Klier     */
4523275953aSGina Haeussge    function listAttachments($ns, $options = array()) {
45326bec61eSMichael Klier        global $conf;
45426bec61eSMichael Klier        global $lang;
45526bec61eSMichael Klier
45626bec61eSMichael Klier        $ns = cleanID($ns);
45726bec61eSMichael Klier
4586fc3aa1aSAndreas Gohr        if (!is_array($options)) $options = array();
4596fc3aa1aSAndreas Gohr        $options['skipacl'] = 0; // no ACL skipping for XMLRPC
4603275953aSGina Haeussge
4613275953aSGina Haeussge
46226bec61eSMichael Klier        if(auth_quickaclcheck($ns.':*') >= AUTH_READ) {
46326bec61eSMichael Klier            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
46426bec61eSMichael Klier
46526bec61eSMichael Klier            $data = array();
466224122cfSAndreas Gohr            search($data, $conf['mediadir'], 'search_media', $options, $dir);
467224122cfSAndreas Gohr            $len = count($data);
468224122cfSAndreas Gohr            if(!$len) return array();
46926bec61eSMichael Klier
470224122cfSAndreas Gohr            for($i=0; $i<$len; $i++) {
471224122cfSAndreas Gohr                unset($data[$i]['meta']);
472224122cfSAndreas Gohr                $data[$i]['lastModified'] = new IXR_Date($data[$i]['mtime']);
47326bec61eSMichael Klier            }
474224122cfSAndreas Gohr            return $data;
47526bec61eSMichael Klier        } else {
47626bec61eSMichael Klier            return new IXR_Error(1, 'You are not allowed to list media files.');
47726bec61eSMichael Klier        }
47826bec61eSMichael Klier    }
47926bec61eSMichael Klier
48026bec61eSMichael Klier    /**
481797c0d11SAndreas Gohr     * Return a list of backlinks
482797c0d11SAndreas Gohr     */
483beccd742SMichael Klier    function listBackLinks($id){
48486228f10SDominik Eckelmann        return ft_backlinks(cleanID($id));
485797c0d11SAndreas Gohr    }
486797c0d11SAndreas Gohr
487797c0d11SAndreas Gohr    /**
48863dd0d58SMichael Klier     * Return some basic data about a page
489797c0d11SAndreas Gohr     */
490797c0d11SAndreas Gohr    function pageInfo($id,$rev=''){
491797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
492797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
493797c0d11SAndreas Gohr        }
494797c0d11SAndreas Gohr        $file = wikiFN($id,$rev);
495797c0d11SAndreas Gohr        $time = @filemtime($file);
496797c0d11SAndreas Gohr        if(!$time){
497797c0d11SAndreas Gohr            return new IXR_Error(10, 'The requested page does not exist');
498797c0d11SAndreas Gohr        }
499797c0d11SAndreas Gohr
500797c0d11SAndreas Gohr        $info = getRevisionInfo($id, $time, 1024);
501797c0d11SAndreas Gohr
502797c0d11SAndreas Gohr        $data = array(
503797c0d11SAndreas Gohr            'name'         => $id,
504797c0d11SAndreas Gohr            'lastModified' => new IXR_Date($time),
505797c0d11SAndreas Gohr            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
506797c0d11SAndreas Gohr            'version'      => $time
507797c0d11SAndreas Gohr        );
50863dd0d58SMichael Klier
50963dd0d58SMichael Klier        return ($data);
510797c0d11SAndreas Gohr    }
511797c0d11SAndreas Gohr
512797c0d11SAndreas Gohr    /**
5133a1dad2dSDennis Ploeger     * Save a wiki page
514222572bfSMichael Klier     *
515222572bfSMichael Klier     * @author Michael Klier <chi@chimeric.de>
5163a1dad2dSDennis Ploeger     */
517222572bfSMichael Klier    function putPage($id, $text, $params) {
5183a1dad2dSDennis Ploeger        global $TEXT;
519a6a229ceSMichael Klier        global $lang;
520593bf8f6SMichael Klier        global $conf;
5213a1dad2dSDennis Ploeger
522222572bfSMichael Klier        $id    = cleanID($id);
52356523eecSAndreas Gohr        $TEXT  = cleanText($text);
524222572bfSMichael Klier        $sum   = $params['sum'];
525222572bfSMichael Klier        $minor = $params['minor'];
526222572bfSMichael Klier
527222572bfSMichael Klier        if(empty($id))
528fdd2e9d6SMichael Klier            return new IXR_Error(1, 'Empty page ID');
529222572bfSMichael Klier
53056523eecSAndreas Gohr        if(!page_exists($id) && trim($TEXT) == '' ) {
53151597811SMichael Klier            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
53251597811SMichael Klier        }
53351597811SMichael Klier
534055b0144SChris Smith        if(auth_quickaclcheck($id) < AUTH_EDIT)
535222572bfSMichael Klier            return new IXR_Error(1, 'You are not allowed to edit this page');
5363a1dad2dSDennis Ploeger
5373a1dad2dSDennis Ploeger        // Check, if page is locked
538222572bfSMichael Klier        if(checklock($id))
539222572bfSMichael Klier            return new IXR_Error(1, 'The page is currently locked');
540222572bfSMichael Klier
541a6a229ceSMichael Klier        // SPAM check
5423a1dad2dSDennis Ploeger        if(checkwordblock())
543222572bfSMichael Klier            return new IXR_Error(1, 'Positive wordblock check');
5443a1dad2dSDennis Ploeger
545a6a229ceSMichael Klier        // autoset summary on new pages
546a6a229ceSMichael Klier        if(!page_exists($id) && empty($sum)) {
547a6a229ceSMichael Klier            $sum = $lang['created'];
548a6a229ceSMichael Klier        }
549a6a229ceSMichael Klier
550a6a229ceSMichael Klier        // autoset summary on deleted pages
551a6a229ceSMichael Klier        if(page_exists($id) && empty($TEXT) && empty($sum)) {
552a6a229ceSMichael Klier            $sum = $lang['deleted'];
553a6a229ceSMichael Klier        }
554a6a229ceSMichael Klier
555222572bfSMichael Klier        lock($id);
5563a1dad2dSDennis Ploeger
557222572bfSMichael Klier        saveWikiText($id,$TEXT,$sum,$minor);
5583a1dad2dSDennis Ploeger
559222572bfSMichael Klier        unlock($id);
5603a1dad2dSDennis Ploeger
561593bf8f6SMichael Klier        // run the indexer if page wasn't indexed yet
562593bf8f6SMichael Klier        if(!@file_exists(metaFN($id, '.indexed'))) {
563593bf8f6SMichael Klier            // try to aquire a lock
564593bf8f6SMichael Klier            $lock = $conf['lockdir'].'/_indexer.lock';
565593bf8f6SMichael Klier            while(!@mkdir($lock,$conf['dmode'])){
566593bf8f6SMichael Klier                usleep(50);
567593bf8f6SMichael Klier                if(time()-@filemtime($lock) > 60*5){
568593bf8f6SMichael Klier                    // looks like a stale lock - remove it
569593bf8f6SMichael Klier                    @rmdir($lock);
570593bf8f6SMichael Klier                }else{
571593bf8f6SMichael Klier                    return false;
572593bf8f6SMichael Klier                }
573593bf8f6SMichael Klier            }
574593bf8f6SMichael Klier            if($conf['dperm']) chmod($lock, $conf['dperm']);
575593bf8f6SMichael Klier
576593bf8f6SMichael Klier            // do the work
577593bf8f6SMichael Klier            idx_addPage($id);
578593bf8f6SMichael Klier
579593bf8f6SMichael Klier            // we're finished - save and free lock
580593bf8f6SMichael Klier            io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
581593bf8f6SMichael Klier            @rmdir($lock);
582593bf8f6SMichael Klier        }
583593bf8f6SMichael Klier
5843a1dad2dSDennis Ploeger        return 0;
585beccd742SMichael Klier    }
5863a1dad2dSDennis Ploeger
587beccd742SMichael Klier    /**
588ba9418bcSHakan Sandell     * Appends text to a wiki page.
589ba9418bcSHakan Sandell     */
590ba9418bcSHakan Sandell    function appendPage($id, $text, $params) {
591ba9418bcSHakan Sandell        $currentpage = $this->rawPage($id);
592ba9418bcSHakan Sandell        if (!is_string($currentpage)) {
593ba9418bcSHakan Sandell            return $currentpage;
594ba9418bcSHakan Sandell        }
595ba9418bcSHakan Sandell        return $this->putPage($id, $currentpage.$text, $params);
596ba9418bcSHakan Sandell    }
597ba9418bcSHakan Sandell
598ba9418bcSHakan Sandell    /**
5992aca132fSMichael Klier     * Uploads a file to the wiki.
6002aca132fSMichael Klier     *
6012aca132fSMichael Klier     * Michael Klier <chi@chimeric.de>
6022aca132fSMichael Klier     */
603f01ff8c1SGina Haeussge    function putAttachment($id, $file, $params) {
6042aca132fSMichael Klier        global $conf;
6052aca132fSMichael Klier        global $lang;
6062aca132fSMichael Klier
607f01ff8c1SGina Haeussge        $auth = auth_quickaclcheck(getNS($id).':*');
6082aca132fSMichael Klier        if($auth >= AUTH_UPLOAD) {
609f01ff8c1SGina Haeussge            if(!isset($id)) {
6102aca132fSMichael Klier                return new IXR_ERROR(1, 'Filename not given.');
6112aca132fSMichael Klier            }
6122aca132fSMichael Klier
613c77fa67bSMichael Hamann            $ftmp = $conf['tmpdir'] . '/' . md5($id.clientIP());
6142aca132fSMichael Klier
6152aca132fSMichael Klier            // save temporary file
6162aca132fSMichael Klier            @unlink($ftmp);
6172aca132fSMichael Klier            $buff = base64_decode($file);
6182aca132fSMichael Klier            io_saveFile($ftmp, $buff);
6192aca132fSMichael Klier
6202aca132fSMichael Klier            // get filename
621ecebf3a8SAndreas Gohr            list($iext, $imime,$dl) = mimetype($id);
622f01ff8c1SGina Haeussge            $id = cleanID($id);
6232aca132fSMichael Klier            $fn = mediaFN($id);
6242aca132fSMichael Klier
6252aca132fSMichael Klier            // get filetype regexp
6262aca132fSMichael Klier            $types = array_keys(getMimeTypes());
6272aca132fSMichael Klier            $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
6282aca132fSMichael Klier            $regex = join('|',$types);
6292aca132fSMichael Klier
6302aca132fSMichael Klier            // because a temp file was created already
6312aca132fSMichael Klier            if(preg_match('/\.('.$regex.')$/i',$fn)) {
6322aca132fSMichael Klier                //check for overwrite
63399c8d7f2Smichael                $overwrite = @file_exists($fn);
63499c8d7f2Smichael                if($overwrite && (!$params['ow'] || $auth < AUTH_DELETE)) {
635224122cfSAndreas Gohr                    return new IXR_ERROR(1, $lang['uploadexist'].'1');
6362aca132fSMichael Klier                }
6372aca132fSMichael Klier                // check for valid content
6382aca132fSMichael Klier                $ok = media_contentcheck($ftmp, $imime);
6392aca132fSMichael Klier                if($ok == -1) {
640224122cfSAndreas Gohr                    return new IXR_ERROR(1, sprintf($lang['uploadexist'].'2', ".$iext"));
6412aca132fSMichael Klier                } elseif($ok == -2) {
6422aca132fSMichael Klier                    return new IXR_ERROR(1, $lang['uploadspam']);
6432aca132fSMichael Klier                } elseif($ok == -3) {
6442aca132fSMichael Klier                    return new IXR_ERROR(1, $lang['uploadxss']);
6452aca132fSMichael Klier                }
6462aca132fSMichael Klier
6472aca132fSMichael Klier                // prepare event data
6482aca132fSMichael Klier                $data[0] = $ftmp;
6492aca132fSMichael Klier                $data[1] = $fn;
6502aca132fSMichael Klier                $data[2] = $id;
6512aca132fSMichael Klier                $data[3] = $imime;
65299c8d7f2Smichael                $data[4] = $overwrite;
6532aca132fSMichael Klier
6542aca132fSMichael Klier                // trigger event
6552aca132fSMichael Klier                return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true);
6562aca132fSMichael Klier
6572aca132fSMichael Klier            } else {
6582aca132fSMichael Klier                return new IXR_ERROR(1, $lang['uploadwrong']);
6592aca132fSMichael Klier            }
6602aca132fSMichael Klier        } else {
6612aca132fSMichael Klier            return new IXR_ERROR(1, "You don't have permissions to upload files.");
6622aca132fSMichael Klier        }
6632aca132fSMichael Klier    }
6642aca132fSMichael Klier
6652aca132fSMichael Klier    /**
666f01ff8c1SGina Haeussge     * Deletes a file from the wiki.
667f01ff8c1SGina Haeussge     *
668f01ff8c1SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
669f01ff8c1SGina Haeussge     */
670f01ff8c1SGina Haeussge    function deleteAttachment($id){
671f01ff8c1SGina Haeussge        $auth = auth_quickaclcheck(getNS($id).':*');
672f01ff8c1SGina Haeussge        if($auth < AUTH_DELETE) return new IXR_ERROR(1, "You don't have permissions to delete files.");
673f01ff8c1SGina Haeussge        global $conf;
674f01ff8c1SGina Haeussge        global $lang;
675f01ff8c1SGina Haeussge
676f01ff8c1SGina Haeussge        // check for references if needed
677f01ff8c1SGina Haeussge        $mediareferences = array();
678f01ff8c1SGina Haeussge        if($conf['refcheck']){
679f01ff8c1SGina Haeussge            $mediareferences = ft_mediause($id,$conf['refshow']);
680f01ff8c1SGina Haeussge        }
681f01ff8c1SGina Haeussge
682f01ff8c1SGina Haeussge        if(!count($mediareferences)){
683f01ff8c1SGina Haeussge            $file = mediaFN($id);
684f01ff8c1SGina Haeussge            if(@unlink($file)){
68599c8d7f2Smichael                addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE);
686f01ff8c1SGina Haeussge                io_sweepNS($id,'mediadir');
687f01ff8c1SGina Haeussge                return 0;
688f01ff8c1SGina Haeussge            }
689f01ff8c1SGina Haeussge            //something went wrong
690f01ff8c1SGina Haeussge               return new IXR_ERROR(1, 'Could not delete file');
691f01ff8c1SGina Haeussge        } else {
692f01ff8c1SGina Haeussge            return new IXR_ERROR(1, 'File is still referenced');
693f01ff8c1SGina Haeussge        }
694f01ff8c1SGina Haeussge    }
695f01ff8c1SGina Haeussge
696f01ff8c1SGina Haeussge    /**
6972aca132fSMichael Klier     * Moves the temporary file to its final destination.
6982aca132fSMichael Klier     *
6992aca132fSMichael Klier     * Michael Klier <chi@chimeric.de>
7002aca132fSMichael Klier     */
7012aca132fSMichael Klier    function _media_upload_action($data) {
7022aca132fSMichael Klier        global $conf;
7032aca132fSMichael Klier
70499c8d7f2Smichael        if(is_array($data) && count($data)===5) {
7052aca132fSMichael Klier            io_createNamespace($data[2], 'media');
7062aca132fSMichael Klier            if(rename($data[0], $data[1])) {
7072aca132fSMichael Klier                chmod($data[1], $conf['fmode']);
7082aca132fSMichael Klier                media_notify($data[2], $data[1], $data[3]);
70999c8d7f2Smichael                // add a log entry to the media changelog
71099c8d7f2Smichael                if ($data[4]) {
71199c8d7f2Smichael                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_EDIT);
71299c8d7f2Smichael                } else {
71399c8d7f2Smichael                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_CREATE);
71499c8d7f2Smichael                }
7152aca132fSMichael Klier                return $data[2];
7162aca132fSMichael Klier            } else {
7172aca132fSMichael Klier                return new IXR_ERROR(1, 'Upload failed.');
7182aca132fSMichael Klier            }
7192aca132fSMichael Klier        } else {
7202aca132fSMichael Klier            return new IXR_ERROR(1, 'Upload failed.');
7212aca132fSMichael Klier        }
7222aca132fSMichael Klier    }
7232aca132fSMichael Klier
7242aca132fSMichael Klier    /**
725e62b9ea5SMichael Klier    * Returns the permissions of a given wiki page
726e62b9ea5SMichael Klier    */
727e62b9ea5SMichael Klier    function aclCheck($id) {
728e62b9ea5SMichael Klier        return auth_quickaclcheck($id);
729e62b9ea5SMichael Klier    }
730e62b9ea5SMichael Klier
731e62b9ea5SMichael Klier    /**
732beccd742SMichael Klier     * Lists all links contained in a wiki page
73363dd0d58SMichael Klier     *
73463dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
735beccd742SMichael Klier     */
736beccd742SMichael Klier    function listLinks($id) {
737beccd742SMichael Klier        if(auth_quickaclcheck($id) < AUTH_READ){
738beccd742SMichael Klier            return new IXR_Error(1, 'You are not allowed to read this page');
739beccd742SMichael Klier        }
740beccd742SMichael Klier        $links = array();
741beccd742SMichael Klier
742beccd742SMichael Klier        // resolve page instructions
743beccd742SMichael Klier        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
744beccd742SMichael Klier
745beccd742SMichael Klier        // instantiate new Renderer - needed for interwiki links
746beccd742SMichael Klier        include(DOKU_INC.'inc/parser/xhtml.php');
747beccd742SMichael Klier        $Renderer = new Doku_Renderer_xhtml();
748beccd742SMichael Klier        $Renderer->interwiki = getInterwiki();
749beccd742SMichael Klier
750beccd742SMichael Klier        // parse parse instructions
751beccd742SMichael Klier        foreach($ins as $in) {
752beccd742SMichael Klier            $link = array();
753beccd742SMichael Klier            switch($in[0]) {
754beccd742SMichael Klier                case 'internallink':
755beccd742SMichael Klier                    $link['type'] = 'local';
756beccd742SMichael Klier                    $link['page'] = $in[1][0];
757beccd742SMichael Klier                    $link['href'] = wl($in[1][0]);
758beccd742SMichael Klier                    array_push($links,$link);
759beccd742SMichael Klier                    break;
760beccd742SMichael Klier                case 'externallink':
761beccd742SMichael Klier                    $link['type'] = 'extern';
762beccd742SMichael Klier                    $link['page'] = $in[1][0];
763beccd742SMichael Klier                    $link['href'] = $in[1][0];
764beccd742SMichael Klier                    array_push($links,$link);
765beccd742SMichael Klier                    break;
766beccd742SMichael Klier                case 'interwikilink':
767beccd742SMichael Klier                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
768beccd742SMichael Klier                    $link['type'] = 'extern';
769beccd742SMichael Klier                    $link['page'] = $url;
770beccd742SMichael Klier                    $link['href'] = $url;
771beccd742SMichael Klier                    array_push($links,$link);
772beccd742SMichael Klier                    break;
773beccd742SMichael Klier            }
774beccd742SMichael Klier        }
775beccd742SMichael Klier
77663dd0d58SMichael Klier        return ($links);
77763dd0d58SMichael Klier    }
77863dd0d58SMichael Klier
77963dd0d58SMichael Klier    /**
78063dd0d58SMichael Klier     * Returns a list of recent changes since give timestamp
78163dd0d58SMichael Klier     *
78299c8d7f2Smichael     * @author Michael Hamann <michael@content-space.de>
78363dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
78463dd0d58SMichael Klier     */
78563dd0d58SMichael Klier    function getRecentChanges($timestamp) {
78663dd0d58SMichael Klier        if(strlen($timestamp) != 10)
78763dd0d58SMichael Klier            return new IXR_Error(20, 'The provided value is not a valid timestamp');
78863dd0d58SMichael Klier
78999c8d7f2Smichael        $recents = getRecentsSince($timestamp);
79063dd0d58SMichael Klier
79199c8d7f2Smichael        $changes = array();
79263dd0d58SMichael Klier
79399c8d7f2Smichael        foreach ($recents as $recent) {
79499c8d7f2Smichael            $change = array();
79599c8d7f2Smichael            $change['name']         = $recent['id'];
79699c8d7f2Smichael            $change['lastModified'] = new IXR_Date($recent['date']);
79799c8d7f2Smichael            $change['author']       = $recent['user'];
79899c8d7f2Smichael            $change['version']      = $recent['date'];
79999c8d7f2Smichael            $change['perms']        = $recent['perms'];
80099c8d7f2Smichael            $change['size']         = @filesize(wikiFN($recent['id']));
80163dd0d58SMichael Klier            array_push($changes, $change);
80299c8d7f2Smichael        }
80399c8d7f2Smichael
80499c8d7f2Smichael        if (!empty($changes)) {
80599c8d7f2Smichael            return $changes;
80663dd0d58SMichael Klier        } else {
80763dd0d58SMichael Klier            // in case we still have nothing at this point
80863dd0d58SMichael Klier            return new IXR_Error(30, 'There are no changes in the specified timeframe');
8093a1dad2dSDennis Ploeger        }
81099c8d7f2Smichael    }
81199c8d7f2Smichael
81299c8d7f2Smichael    /**
81399c8d7f2Smichael     * Returns a list of recent media changes since give timestamp
81499c8d7f2Smichael     *
81599c8d7f2Smichael     * @author Michael Hamann <michael@content-space.de>
81699c8d7f2Smichael     * @author Michael Klier <chi@chimeric.de>
81799c8d7f2Smichael     */
81899c8d7f2Smichael    function getRecentMediaChanges($timestamp) {
81999c8d7f2Smichael        if(strlen($timestamp) != 10)
82099c8d7f2Smichael            return new IXR_Error(20, 'The provided value is not a valid timestamp');
82199c8d7f2Smichael
82299c8d7f2Smichael        $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
82399c8d7f2Smichael
82499c8d7f2Smichael        $changes = array();
82599c8d7f2Smichael
82699c8d7f2Smichael        foreach ($recents as $recent) {
82799c8d7f2Smichael            $change = array();
82899c8d7f2Smichael            $change['name']         = $recent['id'];
82999c8d7f2Smichael            $change['lastModified'] = new IXR_Date($recent['date']);
83099c8d7f2Smichael            $change['author']       = $recent['user'];
83199c8d7f2Smichael            $change['version']      = $recent['date'];
83299c8d7f2Smichael            $change['perms']        = $recent['perms'];
833a4da2756Smichael            $change['size']         = @filesize(mediaFN($recent['id']));
83499c8d7f2Smichael            array_push($changes, $change);
83599c8d7f2Smichael        }
83699c8d7f2Smichael
83799c8d7f2Smichael        if (!empty($changes)) {
83899c8d7f2Smichael            return $changes;
83999c8d7f2Smichael        } else {
84099c8d7f2Smichael            // in case we still have nothing at this point
84199c8d7f2Smichael            return new IXR_Error(30, 'There are no changes in the specified timeframe');
84299c8d7f2Smichael        }
84399c8d7f2Smichael    }
8443a1dad2dSDennis Ploeger
8453a1dad2dSDennis Ploeger    /**
84673056168SMichael Klier     * Returns a list of available revisions of a given wiki page
84773056168SMichael Klier     *
84873056168SMichael Klier     * @author Michael Klier <chi@chimeric.de>
84973056168SMichael Klier     */
85073056168SMichael Klier    function pageVersions($id, $first) {
85173056168SMichael Klier        global $conf;
85273056168SMichael Klier
85373056168SMichael Klier        $versions = array();
85473056168SMichael Klier
85573056168SMichael Klier        if(empty($id))
85673056168SMichael Klier            return new IXR_Error(1, 'Empty page ID');
85773056168SMichael Klier
85873056168SMichael Klier        $revisions = getRevisions($id, $first, $conf['recent']+1);
85973056168SMichael Klier
86073056168SMichael Klier        if(count($revisions)==0 && $first!=0) {
86173056168SMichael Klier            $first=0;
86273056168SMichael Klier            $revisions = getRevisions($id, $first, $conf['recent']+1);
86373056168SMichael Klier        }
86473056168SMichael Klier
86545c63471SMichael Klier        if(count($revisions)>0 && $first==0) {
86645c63471SMichael Klier            array_unshift($revisions, '');  // include current revision
86745c63471SMichael Klier            array_pop($revisions);          // remove extra log entry
86845c63471SMichael Klier        }
86945c63471SMichael Klier
87073056168SMichael Klier        $hasNext = false;
87173056168SMichael Klier        if(count($revisions)>$conf['recent']) {
87273056168SMichael Klier            $hasNext = true;
87373056168SMichael Klier            array_pop($revisions); // remove extra log entry
87473056168SMichael Klier        }
87573056168SMichael Klier
87673056168SMichael Klier        if(!empty($revisions)) {
87773056168SMichael Klier            foreach($revisions as $rev) {
87873056168SMichael Klier                $file = wikiFN($id,$rev);
87973056168SMichael Klier                $time = @filemtime($file);
88045c63471SMichael Klier                // we check if the page actually exists, if this is not the
88145c63471SMichael Klier                // case this can lead to less pages being returned than
88245c63471SMichael Klier                // specified via $conf['recent']
88373056168SMichael Klier                if($time){
88473056168SMichael Klier                    $info = getRevisionInfo($id, $time, 1024);
88573056168SMichael Klier                    if(!empty($info)) {
88673056168SMichael Klier                        $data['user'] = $info['user'];
88773056168SMichael Klier                        $data['ip']   = $info['ip'];
88873056168SMichael Klier                        $data['type'] = $info['type'];
88973056168SMichael Klier                        $data['sum']  = $info['sum'];
89073056168SMichael Klier                        $data['modified'] = new IXR_Date($info['date']);
89173056168SMichael Klier                        $data['version'] = $info['date'];
89273056168SMichael Klier                        array_push($versions, $data);
89373056168SMichael Klier                    }
89473056168SMichael Klier                }
89573056168SMichael Klier            }
89673056168SMichael Klier            return $versions;
89773056168SMichael Klier        } else {
89873056168SMichael Klier            return array();
89973056168SMichael Klier        }
90073056168SMichael Klier    }
90173056168SMichael Klier
90273056168SMichael Klier    /**
903797c0d11SAndreas Gohr     * The version of Wiki RPC API supported
904797c0d11SAndreas Gohr     */
905797c0d11SAndreas Gohr    function wiki_RPCVersion(){
906797c0d11SAndreas Gohr        return 2;
907797c0d11SAndreas Gohr    }
9081b11c097SAndreas Gohr
90928ec3c76SAndreas Gohr
91028ec3c76SAndreas Gohr    /**
91128ec3c76SAndreas Gohr     * Locks or unlocks a given batch of pages
91228ec3c76SAndreas Gohr     *
91328ec3c76SAndreas Gohr     * Give an associative array with two keys: lock and unlock. Both should contain a
91428ec3c76SAndreas Gohr     * list of pages to lock or unlock
91528ec3c76SAndreas Gohr     *
91628ec3c76SAndreas Gohr     * Returns an associative array with the keys locked, lockfail, unlocked and
91728ec3c76SAndreas Gohr     * unlockfail, each containing lists of pages.
91828ec3c76SAndreas Gohr     */
91928ec3c76SAndreas Gohr    function setLocks($set){
92028ec3c76SAndreas Gohr        $locked     = array();
92128ec3c76SAndreas Gohr        $lockfail   = array();
92228ec3c76SAndreas Gohr        $unlocked   = array();
92328ec3c76SAndreas Gohr        $unlockfail = array();
92428ec3c76SAndreas Gohr
92528ec3c76SAndreas Gohr        foreach((array) $set['lock'] as $id){
92628ec3c76SAndreas Gohr            if(checklock($id)){
92728ec3c76SAndreas Gohr                $lockfail[] = $id;
92828ec3c76SAndreas Gohr            }else{
92928ec3c76SAndreas Gohr                lock($id);
93028ec3c76SAndreas Gohr                $locked[] = $id;
93128ec3c76SAndreas Gohr            }
93228ec3c76SAndreas Gohr        }
93328ec3c76SAndreas Gohr
93428ec3c76SAndreas Gohr        foreach((array) $set['unlock'] as $id){
93528ec3c76SAndreas Gohr            if(unlock($id)){
93628ec3c76SAndreas Gohr                $unlocked[] = $id;
93728ec3c76SAndreas Gohr            }else{
93828ec3c76SAndreas Gohr                $unlockfail[] = $id;
93928ec3c76SAndreas Gohr            }
94028ec3c76SAndreas Gohr        }
94128ec3c76SAndreas Gohr
94228ec3c76SAndreas Gohr        return array(
94328ec3c76SAndreas Gohr            'locked'     => $locked,
94428ec3c76SAndreas Gohr            'lockfail'   => $lockfail,
94528ec3c76SAndreas Gohr            'unlocked'   => $unlocked,
94628ec3c76SAndreas Gohr            'unlockfail' => $unlockfail,
94728ec3c76SAndreas Gohr        );
94828ec3c76SAndreas Gohr    }
94928ec3c76SAndreas Gohr
950445e8084SAndreas Gohr    function getAPIVersion(){
951445e8084SAndreas Gohr        return DOKU_XMLRPC_API_VERSION;
952445e8084SAndreas Gohr    }
953445e8084SAndreas Gohr
954445e8084SAndreas Gohr    function login($user,$pass){
955445e8084SAndreas Gohr        global $conf;
956445e8084SAndreas Gohr        global $auth;
957445e8084SAndreas Gohr        if(!$conf['useacl']) return 0;
958445e8084SAndreas Gohr        if(!$auth) return 0;
959445e8084SAndreas Gohr        if($auth->canDo('external')){
960445e8084SAndreas Gohr            return $auth->trustExternal($user,$pass,false);
961445e8084SAndreas Gohr        }else{
962445e8084SAndreas Gohr            return auth_login($user,$pass,false,true);
963445e8084SAndreas Gohr        }
964445e8084SAndreas Gohr    }
9653ee5b583SAndreas Gohr
9663ee5b583SAndreas Gohr
967797c0d11SAndreas Gohr}
968797c0d11SAndreas Gohr
969797c0d11SAndreas Gohr$server = new dokuwiki_xmlrpc_server();
970797c0d11SAndreas Gohr
971*e3776c06SMichael Hamann// vim:ts=4:sw=4:et:
972