xref: /dokuwiki/lib/exe/xmlrpc.php (revision 6fc3aa1afd3b25c8ada5bd8245997e0fa3a8cdee)
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 */
103ee5b583SAndreas Gohrdefine('DOKU_XMLRPC_API_VERSION',2);
11797c0d11SAndreas Gohr
12797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
13797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
14797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
15797c0d11SAndreas Gohrsession_write_close();  //close session
16593bf8f6SMichael Klier
173ee5b583SAndreas Gohrif(!$conf['xmlrpc']) die('XML-RPC server not enabled.');
18593bf8f6SMichael Klier
19797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/IXR_Library.php');
20797c0d11SAndreas Gohr
21797c0d11SAndreas Gohr
22797c0d11SAndreas Gohr/**
23797c0d11SAndreas Gohr * Contains needed wrapper functions and registers all available
24797c0d11SAndreas Gohr * XMLRPC functions.
25797c0d11SAndreas Gohr */
26797c0d11SAndreas Gohrclass dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
27797c0d11SAndreas Gohr    var $methods       = array();
283ee5b583SAndreas Gohr    var $public_methods = array();
293ee5b583SAndreas Gohr
303ee5b583SAndreas Gohr    /**
313ee5b583SAndreas Gohr     * Checks if the current user is allowed to execute non anonymous methods
323ee5b583SAndreas Gohr     */
333ee5b583SAndreas Gohr    function checkAuth(){
343ee5b583SAndreas Gohr        global $conf;
353ee5b583SAndreas Gohr        global $USERINFO;
363ee5b583SAndreas Gohr
373ee5b583SAndreas Gohr        if(!$conf['useacl']) return true; //no ACL - then no checks
383ee5b583SAndreas Gohr
393ee5b583SAndreas Gohr        $allowed = explode(',',$conf['xmlrpcuser']);
403ee5b583SAndreas Gohr        $allowed = array_map('trim', $allowed);
413ee5b583SAndreas Gohr        $allowed = array_unique($allowed);
423ee5b583SAndreas Gohr        $allowed = array_filter($allowed);
433ee5b583SAndreas Gohr
443ee5b583SAndreas Gohr        if(!count($allowed)) return true; //no restrictions
453ee5b583SAndreas Gohr
463ee5b583SAndreas Gohr        $user   = $_SERVER['REMOTE_USER'];
473ee5b583SAndreas Gohr        $groups = (array) $USERINFO['grps'];
483ee5b583SAndreas Gohr
493ee5b583SAndreas Gohr        if(in_array($user,$allowed)) return true; //user explicitly mentioned
503ee5b583SAndreas Gohr
513ee5b583SAndreas Gohr        //check group memberships
523ee5b583SAndreas Gohr        foreach($groups as $group){
533ee5b583SAndreas Gohr            if(in_array('@'.$group,$allowed)) return true;
543ee5b583SAndreas Gohr        }
553ee5b583SAndreas Gohr
563ee5b583SAndreas Gohr        //still here? no access!
573ee5b583SAndreas Gohr        return false;
583ee5b583SAndreas Gohr    }
593ee5b583SAndreas Gohr
603ee5b583SAndreas Gohr    /**
613ee5b583SAndreas Gohr     * Adds a callback, extends parent method
623ee5b583SAndreas Gohr     *
633ee5b583SAndreas Gohr     * add another parameter to define if anonymous access to
643ee5b583SAndreas Gohr     * this method should be granted.
653ee5b583SAndreas Gohr     */
663ee5b583SAndreas Gohr    function addCallback($method, $callback, $args, $help, $public=false){
673ee5b583SAndreas Gohr        if($public) $this->public_methods[] = $method;
683ee5b583SAndreas Gohr        return parent::addCallback($method, $callback, $args, $help);
693ee5b583SAndreas Gohr    }
703ee5b583SAndreas Gohr
713ee5b583SAndreas Gohr    /**
723ee5b583SAndreas Gohr     * Execute a call, extends parent method
733ee5b583SAndreas Gohr     *
743ee5b583SAndreas Gohr     * Checks for authentication first
753ee5b583SAndreas Gohr     */
763ee5b583SAndreas Gohr    function call($methodname, $args){
773ee5b583SAndreas Gohr        if(!in_array($methodname,$this->public_methods) && !$this->checkAuth()){
783ee5b583SAndreas Gohr            return new IXR_Error(-32603, 'server error. not authorized to call method "'.$methodname.'".');
793ee5b583SAndreas Gohr        }
803ee5b583SAndreas Gohr        return parent::call($methodname, $args);
813ee5b583SAndreas Gohr    }
82797c0d11SAndreas Gohr
83797c0d11SAndreas Gohr    /**
84797c0d11SAndreas Gohr     * Constructor. Register methods and run Server
85797c0d11SAndreas Gohr     */
86797c0d11SAndreas Gohr    function dokuwiki_xmlrpc_server(){
87797c0d11SAndreas Gohr        $this->IXR_IntrospectionServer();
88797c0d11SAndreas Gohr
89797c0d11SAndreas Gohr        /* DokuWiki's own methods */
90797c0d11SAndreas Gohr        $this->addCallback(
91445e8084SAndreas Gohr            'dokuwiki.getXMLRPCAPIVersion',
92445e8084SAndreas Gohr            'this:getAPIVersion',
93445e8084SAndreas Gohr            array('integer'),
943ee5b583SAndreas Gohr            'Returns the XMLRPC API version.',
953ee5b583SAndreas Gohr            true
96445e8084SAndreas Gohr        );
97445e8084SAndreas Gohr
98445e8084SAndreas Gohr        $this->addCallback(
99797c0d11SAndreas Gohr            'dokuwiki.getVersion',
100797c0d11SAndreas Gohr            'getVersion',
101797c0d11SAndreas Gohr            array('string'),
1023ee5b583SAndreas Gohr            'Returns the running DokuWiki version.',
1033ee5b583SAndreas Gohr            true
104797c0d11SAndreas Gohr        );
105797c0d11SAndreas Gohr
1061b11c097SAndreas Gohr        $this->addCallback(
107445e8084SAndreas Gohr            'dokuwiki.login',
108445e8084SAndreas Gohr            'this:login',
109445e8084SAndreas Gohr            array('integer','string','string'),
1103ee5b583SAndreas Gohr            'Tries to login with the given credentials and sets auth cookies.',
1113ee5b583SAndreas Gohr            true
112445e8084SAndreas Gohr        );
113445e8084SAndreas Gohr
114445e8084SAndreas Gohr        $this->addCallback(
1151b11c097SAndreas Gohr            'dokuwiki.getPagelist',
1161b11c097SAndreas Gohr            'this:readNamespace',
1171b11c097SAndreas Gohr            array('struct','string','struct'),
1181b11c097SAndreas Gohr            'List all pages within the given namespace.'
1191b11c097SAndreas Gohr        );
1201b11c097SAndreas Gohr
1211b11c097SAndreas Gohr        $this->addCallback(
1221b11c097SAndreas Gohr            'dokuwiki.getTime',
1231b11c097SAndreas Gohr            'time',
1241b11c097SAndreas Gohr            array('int'),
1251b11c097SAndreas Gohr            'Return the current time at the wiki server.'
1261b11c097SAndreas Gohr        );
1271b11c097SAndreas Gohr
12828ec3c76SAndreas Gohr        $this->addCallback(
12928ec3c76SAndreas Gohr            'dokuwiki.setLocks',
13028ec3c76SAndreas Gohr            'this:setLocks',
13128ec3c76SAndreas Gohr            array('struct','struct'),
13228ec3c76SAndreas Gohr            'Lock or unlock pages.'
13328ec3c76SAndreas Gohr        );
13428ec3c76SAndreas Gohr
135797c0d11SAndreas Gohr        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
136797c0d11SAndreas Gohr        $this->addCallback(
137797c0d11SAndreas Gohr            'wiki.getRPCVersionSupported',
138797c0d11SAndreas Gohr            'this:wiki_RPCVersion',
139797c0d11SAndreas Gohr            array('int'),
1403ee5b583SAndreas Gohr            'Returns 2 with the supported RPC API version.',
1413ee5b583SAndreas Gohr            true
142797c0d11SAndreas Gohr        );
143797c0d11SAndreas Gohr        $this->addCallback(
144797c0d11SAndreas Gohr            'wiki.getPage',
145797c0d11SAndreas Gohr            'this:rawPage',
146797c0d11SAndreas Gohr            array('string','string'),
147797c0d11SAndreas Gohr            'Get the raw Wiki text of page, latest version.'
148797c0d11SAndreas Gohr        );
149797c0d11SAndreas Gohr        $this->addCallback(
150797c0d11SAndreas Gohr            'wiki.getPageVersion',
151797c0d11SAndreas Gohr            'this:rawPage',
152797c0d11SAndreas Gohr            array('string','string','int'),
153797c0d11SAndreas Gohr            'Get the raw Wiki text of page.'
154797c0d11SAndreas Gohr        );
155797c0d11SAndreas Gohr        $this->addCallback(
156797c0d11SAndreas Gohr            'wiki.getPageHTML',
157797c0d11SAndreas Gohr            'this:htmlPage',
158797c0d11SAndreas Gohr            array('string','string'),
159797c0d11SAndreas Gohr            'Return page in rendered HTML, latest version.'
160797c0d11SAndreas Gohr        );
161797c0d11SAndreas Gohr        $this->addCallback(
162797c0d11SAndreas Gohr            'wiki.getPageHTMLVersion',
163797c0d11SAndreas Gohr            'this:htmlPage',
164797c0d11SAndreas Gohr            array('string','string','int'),
165797c0d11SAndreas Gohr            'Return page in rendered HTML.'
166797c0d11SAndreas Gohr        );
167797c0d11SAndreas Gohr        $this->addCallback(
168797c0d11SAndreas Gohr            'wiki.getAllPages',
169797c0d11SAndreas Gohr            'this:listPages',
170797c0d11SAndreas Gohr            array('struct'),
171797c0d11SAndreas Gohr            'Returns a list of all pages. The result is an array of utf8 pagenames.'
172797c0d11SAndreas Gohr        );
173797c0d11SAndreas Gohr        $this->addCallback(
17426bec61eSMichael Klier            'wiki.getAttachments',
17526bec61eSMichael Klier            'this:listAttachments',
176c63d1645SGina Haeussge            array('struct', 'string', 'struct'),
17726bec61eSMichael Klier            'Returns a list of all media files.'
17826bec61eSMichael Klier        );
17926bec61eSMichael Klier        $this->addCallback(
180797c0d11SAndreas Gohr            'wiki.getBackLinks',
181797c0d11SAndreas Gohr            'this:listBackLinks',
182797c0d11SAndreas Gohr            array('struct','string'),
183797c0d11SAndreas Gohr            'Returns the pages that link to this page.'
184797c0d11SAndreas Gohr        );
185797c0d11SAndreas Gohr        $this->addCallback(
186797c0d11SAndreas Gohr            'wiki.getPageInfo',
187797c0d11SAndreas Gohr            'this:pageInfo',
188797c0d11SAndreas Gohr            array('struct','string'),
189797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
190797c0d11SAndreas Gohr        );
191797c0d11SAndreas Gohr        $this->addCallback(
192797c0d11SAndreas Gohr            'wiki.getPageInfoVersion',
193797c0d11SAndreas Gohr            'this:pageInfo',
194797c0d11SAndreas Gohr            array('struct','string','int'),
195797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
196797c0d11SAndreas Gohr        );
1973a1dad2dSDennis Ploeger        $this->addCallback(
19873056168SMichael Klier            'wiki.getPageVersions',
19973056168SMichael Klier            'this:pageVersions',
20073056168SMichael Klier            array('struct','string','int'),
20173056168SMichael Klier            'Returns the available revisions of the page.'
20273056168SMichael Klier        );
20373056168SMichael Klier        $this->addCallback(
2043a1dad2dSDennis Ploeger            'wiki.putPage',
2053a1dad2dSDennis Ploeger            'this:putPage',
206222572bfSMichael Klier            array('int', 'string', 'string', 'struct'),
207fdd2e9d6SMichael Klier            'Saves a wiki page.'
2083a1dad2dSDennis Ploeger        );
209beccd742SMichael Klier        $this->addCallback(
210beccd742SMichael Klier            'wiki.listLinks',
211beccd742SMichael Klier            'this:listLinks',
212beccd742SMichael Klier            array('struct','string'),
213fdd2e9d6SMichael Klier            'Lists all links contained in a wiki page.'
214beccd742SMichael Klier        );
21563dd0d58SMichael Klier        $this->addCallback(
21663dd0d58SMichael Klier            'wiki.getRecentChanges',
21763dd0d58SMichael Klier            'this:getRecentChanges',
21863dd0d58SMichael Klier            array('struct','int'),
21999c8d7f2Smichael            'Returns a struct about all recent changes since given timestamp.'
22099c8d7f2Smichael        );
22199c8d7f2Smichael        $this->addCallback(
22299c8d7f2Smichael            'wiki.getRecentMediaChanges',
22399c8d7f2Smichael            'this:getRecentMediaChanges',
22499c8d7f2Smichael            array('struct','int'),
22599c8d7f2Smichael            'Returns a struct about all recent media changes since given timestamp.'
22663dd0d58SMichael Klier        );
227e62b9ea5SMichael Klier        $this->addCallback(
228e62b9ea5SMichael Klier            'wiki.aclCheck',
229e62b9ea5SMichael Klier            'this:aclCheck',
230c63d1645SGina Haeussge            array('int', 'string'),
231e62b9ea5SMichael Klier            'Returns the permissions of a given wiki page.'
232e62b9ea5SMichael Klier        );
2332aca132fSMichael Klier        $this->addCallback(
2342aca132fSMichael Klier            'wiki.putAttachment',
2352aca132fSMichael Klier            'this:putAttachment',
2362aca132fSMichael Klier            array('struct', 'string', 'base64', 'struct'),
2372aca132fSMichael Klier            'Upload a file to the wiki.'
2382aca132fSMichael Klier        );
239cfef3001SGina Haeussge        $this->addCallback(
240f01ff8c1SGina Haeussge            'wiki.deleteAttachment',
241f01ff8c1SGina Haeussge            'this:deleteAttachment',
242f01ff8c1SGina Haeussge            array('int', 'string'),
243f01ff8c1SGina Haeussge            'Delete a file from the wiki.'
244f01ff8c1SGina Haeussge        );
245f01ff8c1SGina Haeussge        $this->addCallback(
246cfef3001SGina Haeussge            'wiki.getAttachment',
247cfef3001SGina Haeussge            'this:getAttachment',
248c63d1645SGina Haeussge            array('base64', 'string'),
249cfef3001SGina Haeussge            'Download a file from the wiki.'
250cfef3001SGina Haeussge        );
2515672e868SGina Haeussge        $this->addCallback(
2525672e868SGina Haeussge            'wiki.getAttachmentInfo',
2535672e868SGina Haeussge            'this:getAttachmentInfo',
254c63d1645SGina Haeussge            array('struct', 'string'),
2555672e868SGina Haeussge            'Returns a struct with infos about the attachment.'
2565672e868SGina Haeussge        );
257797c0d11SAndreas Gohr
258bb32615dSMichael Klier        /**
259bb32615dSMichael Klier         * Trigger XMLRPC_CALLBACK_REGISTER, action plugins can use this event
260bb32615dSMichael Klier         * to extend the XMLRPC interface and register their own callbacks.
261bb32615dSMichael Klier         *
262bb32615dSMichael Klier         * Event data:
263bb32615dSMichael Klier         *  The XMLRPC server object:
264bb32615dSMichael Klier         *
265bb32615dSMichael Klier         *  $event->data->addCallback() - register a callback, the second
266bb32615dSMichael Klier         *  paramter has to be of the form "plugin:<pluginname>:<plugin
267bb32615dSMichael Klier         *  method>"
268bb32615dSMichael Klier         *
269bb32615dSMichael Klier         *  $event->data->callbacks - an array which holds all awaylable
270bb32615dSMichael Klier         *  callbacks
271bb32615dSMichael Klier         */
272bb32615dSMichael Klier        trigger_event('XMLRPC_CALLBACK_REGISTER', $this);
273bb32615dSMichael Klier
274797c0d11SAndreas Gohr        $this->serve();
275797c0d11SAndreas Gohr    }
276797c0d11SAndreas Gohr
277797c0d11SAndreas Gohr    /**
278797c0d11SAndreas Gohr     * Return a raw wiki page
279797c0d11SAndreas Gohr     */
280797c0d11SAndreas Gohr    function rawPage($id,$rev=''){
281797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
282797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
283797c0d11SAndreas Gohr        }
2842c176304SMichael Klier        $text = rawWiki($id,$rev);
2852c176304SMichael Klier        if(!$text) {
2862c176304SMichael Klier            $data = array($id);
2872c176304SMichael Klier            return trigger_event('HTML_PAGE_FROMTEMPLATE',$data,'pageTemplate',true);
2882c176304SMichael Klier        } else {
2892c176304SMichael Klier            return $text;
2902c176304SMichael Klier        }
291797c0d11SAndreas Gohr    }
292797c0d11SAndreas Gohr
293797c0d11SAndreas Gohr    /**
294cfef3001SGina Haeussge     * Return a media file encoded in base64
295c63d1645SGina Haeussge     *
296c63d1645SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
297cfef3001SGina Haeussge     */
298cfef3001SGina Haeussge    function getAttachment($id){
299c63d1645SGina Haeussge        $id = cleanID($id);
300cfef3001SGina Haeussge        if (auth_quickaclcheck(getNS($id).':*') < AUTH_READ)
301cfef3001SGina Haeussge            return new IXR_Error(1, 'You are not allowed to read this file');
302cfef3001SGina Haeussge
303cfef3001SGina Haeussge        $file = mediaFN($id);
304cfef3001SGina Haeussge        if (!@ file_exists($file))
305cfef3001SGina Haeussge            return new IXR_Error(1, 'The requested file does not exist');
306cfef3001SGina Haeussge
307cfef3001SGina Haeussge        $data = io_readFile($file, false);
308cfef3001SGina Haeussge        $base64 = base64_encode($data);
309cfef3001SGina Haeussge        return $base64;
310cfef3001SGina Haeussge    }
311cfef3001SGina Haeussge
312cfef3001SGina Haeussge    /**
3135672e868SGina Haeussge     * Return info about a media file
3145672e868SGina Haeussge     *
3155672e868SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
3165672e868SGina Haeussge     */
3175672e868SGina Haeussge    function getAttachmentInfo($id){
3185672e868SGina Haeussge        $id = cleanID($id);
3195672e868SGina Haeussge        $info = array(
3205672e868SGina Haeussge            'lastModified' => 0,
3215672e868SGina Haeussge            'size' => 0,
3225672e868SGina Haeussge        );
3235672e868SGina Haeussge
3245672e868SGina Haeussge        $file = mediaFN($id);
3255672e868SGina Haeussge        if ((auth_quickaclcheck(getNS($id).':*') >= AUTH_READ) && file_exists($file)){
3265672e868SGina Haeussge            $info['lastModified'] = new IXR_Date(filemtime($file));
3275672e868SGina Haeussge            $info['size'] = filesize($file);
3285672e868SGina Haeussge        }
3295672e868SGina Haeussge
3305672e868SGina Haeussge        return $info;
3315672e868SGina Haeussge    }
3325672e868SGina Haeussge
3335672e868SGina Haeussge    /**
334797c0d11SAndreas Gohr     * Return a wiki page rendered to html
335797c0d11SAndreas Gohr     */
336797c0d11SAndreas Gohr    function htmlPage($id,$rev=''){
337797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
338797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
339797c0d11SAndreas Gohr        }
340797c0d11SAndreas Gohr        return p_wiki_xhtml($id,$rev,false);
341797c0d11SAndreas Gohr    }
342797c0d11SAndreas Gohr
343797c0d11SAndreas Gohr    /**
344797c0d11SAndreas Gohr     * List all pages - we use the indexer list here
345797c0d11SAndreas Gohr     */
346797c0d11SAndreas Gohr    function listPages(){
347dfd13e55SMichael Klier        global $conf;
348dfd13e55SMichael Klier
349dfd13e55SMichael Klier        $list  = array();
350dfd13e55SMichael Klier        $pages = file($conf['indexdir'] . '/page.idx');
351dfd13e55SMichael Klier        $pages = array_filter($pages, 'isVisiblePage');
352dfd13e55SMichael Klier
353dfd13e55SMichael Klier        foreach(array_keys($pages) as $idx) {
354dfd13e55SMichael Klier            if(page_exists($pages[$idx])) {
355dfd13e55SMichael Klier                $perm = auth_quickaclcheck($pages[$idx]);
356dfd13e55SMichael Klier                if($perm >= AUTH_READ) {
357dfd13e55SMichael Klier                    $page = array();
358dfd13e55SMichael Klier                    $page['id'] = trim($pages[$idx]);
359dfd13e55SMichael Klier                    $page['perms'] = $perm;
360dfd13e55SMichael Klier                    $page['size'] = @filesize(wikiFN($pages[$idx]));
361e070c6f3SGina Haeussge                    $page['lastModified'] = new IXR_Date(@filemtime(wikiFN($pages[$idx])));
362dfd13e55SMichael Klier                    $list[] = $page;
363dfd13e55SMichael Klier                }
364dfd13e55SMichael Klier            }
365dfd13e55SMichael Klier        }
366dfd13e55SMichael Klier
367dfd13e55SMichael Klier        return $list;
368797c0d11SAndreas Gohr    }
369797c0d11SAndreas Gohr
370797c0d11SAndreas Gohr    /**
3711b11c097SAndreas Gohr     * List all pages in the given namespace (and below)
3721b11c097SAndreas Gohr     */
3731b11c097SAndreas Gohr    function readNamespace($ns,$opts){
3741b11c097SAndreas Gohr        global $conf;
3751b11c097SAndreas Gohr
3761b11c097SAndreas Gohr        if(!is_array($opts)) $opts=array();
3771b11c097SAndreas Gohr
3781b11c097SAndreas Gohr        $ns = cleanID($ns);
3791b11c097SAndreas Gohr        $dir = utf8_encodeFN(str_replace(':', '/', $ns));
3801b11c097SAndreas Gohr        $data = array();
3811b11c097SAndreas Gohr        require_once(DOKU_INC.'inc/search.php');
382*6fc3aa1aSAndreas Gohr        $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
3831b11c097SAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
3841b11c097SAndreas Gohr        return $data;
3851b11c097SAndreas Gohr    }
3861b11c097SAndreas Gohr
3871b11c097SAndreas Gohr    /**
38826bec61eSMichael Klier     * List all media files.
3893275953aSGina Haeussge     *
3903275953aSGina Haeussge     * Available options are 'recursive' for also including the subnamespaces
3913275953aSGina Haeussge     * in the listing, and 'pattern' for filtering the returned files against
3923275953aSGina Haeussge     * a regular expression matching their name.
3933275953aSGina Haeussge     *
3943275953aSGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
39526bec61eSMichael Klier     */
3963275953aSGina Haeussge    function listAttachments($ns, $options = array()) {
39726bec61eSMichael Klier        global $conf;
39826bec61eSMichael Klier        global $lang;
39926bec61eSMichael Klier
40026bec61eSMichael Klier        $ns = cleanID($ns);
40126bec61eSMichael Klier
402*6fc3aa1aSAndreas Gohr        if (!is_array($options)) $options = array();
403*6fc3aa1aSAndreas Gohr        $options['skipacl'] = 0; // no ACL skipping for XMLRPC
4043275953aSGina Haeussge
4053275953aSGina Haeussge
40626bec61eSMichael Klier        if(auth_quickaclcheck($ns.':*') >= AUTH_READ) {
40726bec61eSMichael Klier            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
40826bec61eSMichael Klier
40926bec61eSMichael Klier            $data = array();
41026bec61eSMichael Klier            require_once(DOKU_INC.'inc/search.php');
411224122cfSAndreas Gohr            search($data, $conf['mediadir'], 'search_media', $options, $dir);
412224122cfSAndreas Gohr            $len = count($data);
413224122cfSAndreas Gohr            if(!$len) return array();
41426bec61eSMichael Klier
415224122cfSAndreas Gohr            for($i=0; $i<$len; $i++) {
416224122cfSAndreas Gohr                unset($data[$i]['meta']);
417224122cfSAndreas Gohr                $data[$i]['lastModified'] = new IXR_Date($data[$i]['mtime']);
41826bec61eSMichael Klier            }
419224122cfSAndreas Gohr            return $data;
42026bec61eSMichael Klier        } else {
42126bec61eSMichael Klier            return new IXR_Error(1, 'You are not allowed to list media files.');
42226bec61eSMichael Klier        }
42326bec61eSMichael Klier    }
42426bec61eSMichael Klier
42526bec61eSMichael Klier    /**
426797c0d11SAndreas Gohr     * Return a list of backlinks
427797c0d11SAndreas Gohr     */
428beccd742SMichael Klier    function listBackLinks($id){
429797c0d11SAndreas Gohr        require_once(DOKU_INC.'inc/fulltext.php');
430797c0d11SAndreas Gohr        return ft_backlinks($id);
431797c0d11SAndreas Gohr    }
432797c0d11SAndreas Gohr
433797c0d11SAndreas Gohr    /**
43463dd0d58SMichael Klier     * Return some basic data about a page
435797c0d11SAndreas Gohr     */
436797c0d11SAndreas Gohr    function pageInfo($id,$rev=''){
437797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
438797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
439797c0d11SAndreas Gohr        }
440797c0d11SAndreas Gohr        $file = wikiFN($id,$rev);
441797c0d11SAndreas Gohr        $time = @filemtime($file);
442797c0d11SAndreas Gohr        if(!$time){
443797c0d11SAndreas Gohr            return new IXR_Error(10, 'The requested page does not exist');
444797c0d11SAndreas Gohr        }
445797c0d11SAndreas Gohr
446797c0d11SAndreas Gohr        $info = getRevisionInfo($id, $time, 1024);
447797c0d11SAndreas Gohr
448797c0d11SAndreas Gohr        $data = array(
449797c0d11SAndreas Gohr            'name'         => $id,
450797c0d11SAndreas Gohr            'lastModified' => new IXR_Date($time),
451797c0d11SAndreas Gohr            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
452797c0d11SAndreas Gohr            'version'      => $time
453797c0d11SAndreas Gohr        );
45463dd0d58SMichael Klier
45563dd0d58SMichael Klier        return ($data);
456797c0d11SAndreas Gohr    }
457797c0d11SAndreas Gohr
458797c0d11SAndreas Gohr    /**
4593a1dad2dSDennis Ploeger     * Save a wiki page
460222572bfSMichael Klier     *
461222572bfSMichael Klier     * @author Michael Klier <chi@chimeric.de>
4623a1dad2dSDennis Ploeger     */
463222572bfSMichael Klier    function putPage($id, $text, $params) {
4643a1dad2dSDennis Ploeger        global $TEXT;
465a6a229ceSMichael Klier        global $lang;
466593bf8f6SMichael Klier        global $conf;
4673a1dad2dSDennis Ploeger
468222572bfSMichael Klier        $id    = cleanID($id);
469222572bfSMichael Klier        $TEXT  = trim($text);
470222572bfSMichael Klier        $sum   = $params['sum'];
471222572bfSMichael Klier        $minor = $params['minor'];
472222572bfSMichael Klier
473222572bfSMichael Klier        if(empty($id))
474fdd2e9d6SMichael Klier            return new IXR_Error(1, 'Empty page ID');
475222572bfSMichael Klier
47651597811SMichael Klier        if(!page_exists($id) && empty($TEXT)) {
47751597811SMichael Klier            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
47851597811SMichael Klier        }
47951597811SMichael Klier
480055b0144SChris Smith        if(auth_quickaclcheck($id) < AUTH_EDIT)
481222572bfSMichael Klier            return new IXR_Error(1, 'You are not allowed to edit this page');
4823a1dad2dSDennis Ploeger
4833a1dad2dSDennis Ploeger        // Check, if page is locked
484222572bfSMichael Klier        if(checklock($id))
485222572bfSMichael Klier            return new IXR_Error(1, 'The page is currently locked');
486222572bfSMichael Klier
487a6a229ceSMichael Klier        // SPAM check
4883a1dad2dSDennis Ploeger        if(checkwordblock())
489222572bfSMichael Klier            return new IXR_Error(1, 'Positive wordblock check');
4903a1dad2dSDennis Ploeger
491a6a229ceSMichael Klier        // autoset summary on new pages
492a6a229ceSMichael Klier        if(!page_exists($id) && empty($sum)) {
493a6a229ceSMichael Klier            $sum = $lang['created'];
494a6a229ceSMichael Klier        }
495a6a229ceSMichael Klier
496a6a229ceSMichael Klier        // autoset summary on deleted pages
497a6a229ceSMichael Klier        if(page_exists($id) && empty($TEXT) && empty($sum)) {
498a6a229ceSMichael Klier            $sum = $lang['deleted'];
499a6a229ceSMichael Klier        }
500a6a229ceSMichael Klier
501222572bfSMichael Klier        lock($id);
5023a1dad2dSDennis Ploeger
503222572bfSMichael Klier        saveWikiText($id,$TEXT,$sum,$minor);
5043a1dad2dSDennis Ploeger
505222572bfSMichael Klier        unlock($id);
5063a1dad2dSDennis Ploeger
507593bf8f6SMichael Klier        // run the indexer if page wasn't indexed yet
508593bf8f6SMichael Klier        if(!@file_exists(metaFN($id, '.indexed'))) {
509593bf8f6SMichael Klier            // try to aquire a lock
510593bf8f6SMichael Klier            $lock = $conf['lockdir'].'/_indexer.lock';
511593bf8f6SMichael Klier            while(!@mkdir($lock,$conf['dmode'])){
512593bf8f6SMichael Klier                usleep(50);
513593bf8f6SMichael Klier                if(time()-@filemtime($lock) > 60*5){
514593bf8f6SMichael Klier                    // looks like a stale lock - remove it
515593bf8f6SMichael Klier                    @rmdir($lock);
516593bf8f6SMichael Klier                }else{
517593bf8f6SMichael Klier                    return false;
518593bf8f6SMichael Klier                }
519593bf8f6SMichael Klier            }
520593bf8f6SMichael Klier            if($conf['dperm']) chmod($lock, $conf['dperm']);
521593bf8f6SMichael Klier
522593bf8f6SMichael Klier            require_once(DOKU_INC.'inc/indexer.php');
523593bf8f6SMichael Klier
524593bf8f6SMichael Klier            // do the work
525593bf8f6SMichael Klier            idx_addPage($id);
526593bf8f6SMichael Klier
527593bf8f6SMichael Klier            // we're finished - save and free lock
528593bf8f6SMichael Klier            io_saveFile(metaFN($id,'.indexed'),INDEXER_VERSION);
529593bf8f6SMichael Klier            @rmdir($lock);
530593bf8f6SMichael Klier        }
531593bf8f6SMichael Klier
5323a1dad2dSDennis Ploeger        return 0;
533beccd742SMichael Klier    }
5343a1dad2dSDennis Ploeger
535beccd742SMichael Klier    /**
5362aca132fSMichael Klier     * Uploads a file to the wiki.
5372aca132fSMichael Klier     *
5382aca132fSMichael Klier     * Michael Klier <chi@chimeric.de>
5392aca132fSMichael Klier     */
540f01ff8c1SGina Haeussge    function putAttachment($id, $file, $params) {
5412aca132fSMichael Klier        global $conf;
5422aca132fSMichael Klier        global $lang;
5432aca132fSMichael Klier
544f01ff8c1SGina Haeussge        $auth = auth_quickaclcheck(getNS($id).':*');
5452aca132fSMichael Klier        if($auth >= AUTH_UPLOAD) {
546f01ff8c1SGina Haeussge            if(!isset($id)) {
5472aca132fSMichael Klier                return new IXR_ERROR(1, 'Filename not given.');
5482aca132fSMichael Klier            }
5492aca132fSMichael Klier
550f01ff8c1SGina Haeussge            $ftmp = $conf['tmpdir'] . '/' . $id;
5512aca132fSMichael Klier
5522aca132fSMichael Klier            // save temporary file
5532aca132fSMichael Klier            @unlink($ftmp);
5542aca132fSMichael Klier            $buff = base64_decode($file);
5552aca132fSMichael Klier            io_saveFile($ftmp, $buff);
5562aca132fSMichael Klier
5572aca132fSMichael Klier            // get filename
558ecebf3a8SAndreas Gohr            list($iext, $imime,$dl) = mimetype($id);
559f01ff8c1SGina Haeussge            $id = cleanID($id);
5602aca132fSMichael Klier            $fn = mediaFN($id);
5612aca132fSMichael Klier
5622aca132fSMichael Klier            // get filetype regexp
5632aca132fSMichael Klier            $types = array_keys(getMimeTypes());
5642aca132fSMichael Klier            $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
5652aca132fSMichael Klier            $regex = join('|',$types);
5662aca132fSMichael Klier
5672aca132fSMichael Klier            // because a temp file was created already
5682aca132fSMichael Klier            if(preg_match('/\.('.$regex.')$/i',$fn)) {
5692aca132fSMichael Klier                //check for overwrite
57099c8d7f2Smichael                $overwrite = @file_exists($fn);
57199c8d7f2Smichael                if($overwrite && (!$params['ow'] || $auth < AUTH_DELETE)) {
572224122cfSAndreas Gohr                    return new IXR_ERROR(1, $lang['uploadexist'].'1');
5732aca132fSMichael Klier                }
5742aca132fSMichael Klier                // check for valid content
5752aca132fSMichael Klier                @require_once(DOKU_INC.'inc/media.php');
5762aca132fSMichael Klier                $ok = media_contentcheck($ftmp, $imime);
5772aca132fSMichael Klier                if($ok == -1) {
578224122cfSAndreas Gohr                    return new IXR_ERROR(1, sprintf($lang['uploadexist'].'2', ".$iext"));
5792aca132fSMichael Klier                } elseif($ok == -2) {
5802aca132fSMichael Klier                    return new IXR_ERROR(1, $lang['uploadspam']);
5812aca132fSMichael Klier                } elseif($ok == -3) {
5822aca132fSMichael Klier                    return new IXR_ERROR(1, $lang['uploadxss']);
5832aca132fSMichael Klier                }
5842aca132fSMichael Klier
5852aca132fSMichael Klier                // prepare event data
5862aca132fSMichael Klier                $data[0] = $ftmp;
5872aca132fSMichael Klier                $data[1] = $fn;
5882aca132fSMichael Klier                $data[2] = $id;
5892aca132fSMichael Klier                $data[3] = $imime;
59099c8d7f2Smichael                $data[4] = $overwrite;
5912aca132fSMichael Klier
5922aca132fSMichael Klier                // trigger event
5932aca132fSMichael Klier                require_once(DOKU_INC.'inc/events.php');
5942aca132fSMichael Klier                return trigger_event('MEDIA_UPLOAD_FINISH', $data, array($this, '_media_upload_action'), true);
5952aca132fSMichael Klier
5962aca132fSMichael Klier            } else {
5972aca132fSMichael Klier                return new IXR_ERROR(1, $lang['uploadwrong']);
5982aca132fSMichael Klier            }
5992aca132fSMichael Klier        } else {
6002aca132fSMichael Klier            return new IXR_ERROR(1, "You don't have permissions to upload files.");
6012aca132fSMichael Klier        }
6022aca132fSMichael Klier    }
6032aca132fSMichael Klier
6042aca132fSMichael Klier    /**
605f01ff8c1SGina Haeussge     * Deletes a file from the wiki.
606f01ff8c1SGina Haeussge     *
607f01ff8c1SGina Haeussge     * @author Gina Haeussge <osd@foosel.net>
608f01ff8c1SGina Haeussge     */
609f01ff8c1SGina Haeussge    function deleteAttachment($id){
610f01ff8c1SGina Haeussge        $auth = auth_quickaclcheck(getNS($id).':*');
611f01ff8c1SGina Haeussge        if($auth < AUTH_DELETE) return new IXR_ERROR(1, "You don't have permissions to delete files.");
612f01ff8c1SGina Haeussge        global $conf;
613f01ff8c1SGina Haeussge        global $lang;
614f01ff8c1SGina Haeussge
615f01ff8c1SGina Haeussge        // check for references if needed
616f01ff8c1SGina Haeussge        $mediareferences = array();
617f01ff8c1SGina Haeussge        if($conf['refcheck']){
618f01ff8c1SGina Haeussge            require_once(DOKU_INC.'inc/fulltext.php');
619f01ff8c1SGina Haeussge            $mediareferences = ft_mediause($id,$conf['refshow']);
620f01ff8c1SGina Haeussge        }
621f01ff8c1SGina Haeussge
622f01ff8c1SGina Haeussge        if(!count($mediareferences)){
623f01ff8c1SGina Haeussge            $file = mediaFN($id);
624f01ff8c1SGina Haeussge            if(@unlink($file)){
62599c8d7f2Smichael                require_once(DOKU_INC.'inc/changelog.php');
62699c8d7f2Smichael                addMediaLogEntry(time(), $id, DOKU_CHANGE_TYPE_DELETE);
627f01ff8c1SGina Haeussge                io_sweepNS($id,'mediadir');
628f01ff8c1SGina Haeussge                return 0;
629f01ff8c1SGina Haeussge            }
630f01ff8c1SGina Haeussge            //something went wrong
631f01ff8c1SGina Haeussge               return new IXR_ERROR(1, 'Could not delete file');
632f01ff8c1SGina Haeussge        } else {
633f01ff8c1SGina Haeussge            return new IXR_ERROR(1, 'File is still referenced');
634f01ff8c1SGina Haeussge        }
635f01ff8c1SGina Haeussge    }
636f01ff8c1SGina Haeussge
637f01ff8c1SGina Haeussge    /**
6382aca132fSMichael Klier     * Moves the temporary file to its final destination.
6392aca132fSMichael Klier     *
6402aca132fSMichael Klier     * Michael Klier <chi@chimeric.de>
6412aca132fSMichael Klier     */
6422aca132fSMichael Klier    function _media_upload_action($data) {
6432aca132fSMichael Klier        global $conf;
6442aca132fSMichael Klier
64599c8d7f2Smichael        if(is_array($data) && count($data)===5) {
6462aca132fSMichael Klier            io_createNamespace($data[2], 'media');
6472aca132fSMichael Klier            if(rename($data[0], $data[1])) {
6482aca132fSMichael Klier                chmod($data[1], $conf['fmode']);
6492aca132fSMichael Klier                media_notify($data[2], $data[1], $data[3]);
65099c8d7f2Smichael                // add a log entry to the media changelog
65199c8d7f2Smichael                require_once(DOKU_INC.'inc/changelog.php');
65299c8d7f2Smichael                if ($data[4]) {
65399c8d7f2Smichael                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_EDIT);
65499c8d7f2Smichael                } else {
65599c8d7f2Smichael                    addMediaLogEntry(time(), $data[2], DOKU_CHANGE_TYPE_CREATE);
65699c8d7f2Smichael                }
6572aca132fSMichael Klier                return $data[2];
6582aca132fSMichael Klier            } else {
6592aca132fSMichael Klier                return new IXR_ERROR(1, 'Upload failed.');
6602aca132fSMichael Klier            }
6612aca132fSMichael Klier        } else {
6622aca132fSMichael Klier            return new IXR_ERROR(1, 'Upload failed.');
6632aca132fSMichael Klier        }
6642aca132fSMichael Klier    }
6652aca132fSMichael Klier
6662aca132fSMichael Klier    /**
667e62b9ea5SMichael Klier    * Returns the permissions of a given wiki page
668e62b9ea5SMichael Klier    */
669e62b9ea5SMichael Klier    function aclCheck($id) {
670e62b9ea5SMichael Klier        return auth_quickaclcheck($id);
671e62b9ea5SMichael Klier    }
672e62b9ea5SMichael Klier
673e62b9ea5SMichael Klier    /**
674beccd742SMichael Klier     * Lists all links contained in a wiki page
67563dd0d58SMichael Klier     *
67663dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
677beccd742SMichael Klier     */
678beccd742SMichael Klier    function listLinks($id) {
679beccd742SMichael Klier        if(auth_quickaclcheck($id) < AUTH_READ){
680beccd742SMichael Klier            return new IXR_Error(1, 'You are not allowed to read this page');
681beccd742SMichael Klier        }
682beccd742SMichael Klier        $links = array();
683beccd742SMichael Klier
684beccd742SMichael Klier        // resolve page instructions
685beccd742SMichael Klier        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
686beccd742SMichael Klier
687beccd742SMichael Klier        // instantiate new Renderer - needed for interwiki links
688beccd742SMichael Klier        include(DOKU_INC.'inc/parser/xhtml.php');
689beccd742SMichael Klier        $Renderer = new Doku_Renderer_xhtml();
690beccd742SMichael Klier        $Renderer->interwiki = getInterwiki();
691beccd742SMichael Klier
692beccd742SMichael Klier        // parse parse instructions
693beccd742SMichael Klier        foreach($ins as $in) {
694beccd742SMichael Klier            $link = array();
695beccd742SMichael Klier            switch($in[0]) {
696beccd742SMichael Klier                case 'internallink':
697beccd742SMichael Klier                    $link['type'] = 'local';
698beccd742SMichael Klier                    $link['page'] = $in[1][0];
699beccd742SMichael Klier                    $link['href'] = wl($in[1][0]);
700beccd742SMichael Klier                    array_push($links,$link);
701beccd742SMichael Klier                    break;
702beccd742SMichael Klier                case 'externallink':
703beccd742SMichael Klier                    $link['type'] = 'extern';
704beccd742SMichael Klier                    $link['page'] = $in[1][0];
705beccd742SMichael Klier                    $link['href'] = $in[1][0];
706beccd742SMichael Klier                    array_push($links,$link);
707beccd742SMichael Klier                    break;
708beccd742SMichael Klier                case 'interwikilink':
709beccd742SMichael Klier                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
710beccd742SMichael Klier                    $link['type'] = 'extern';
711beccd742SMichael Klier                    $link['page'] = $url;
712beccd742SMichael Klier                    $link['href'] = $url;
713beccd742SMichael Klier                    array_push($links,$link);
714beccd742SMichael Klier                    break;
715beccd742SMichael Klier            }
716beccd742SMichael Klier        }
717beccd742SMichael Klier
71863dd0d58SMichael Klier        return ($links);
71963dd0d58SMichael Klier    }
72063dd0d58SMichael Klier
72163dd0d58SMichael Klier    /**
72263dd0d58SMichael Klier     * Returns a list of recent changes since give timestamp
72363dd0d58SMichael Klier     *
72499c8d7f2Smichael     * @author Michael Hamann <michael@content-space.de>
72563dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
72663dd0d58SMichael Klier     */
72763dd0d58SMichael Klier    function getRecentChanges($timestamp) {
72863dd0d58SMichael Klier        if(strlen($timestamp) != 10)
72963dd0d58SMichael Klier            return new IXR_Error(20, 'The provided value is not a valid timestamp');
73063dd0d58SMichael Klier
73163dd0d58SMichael Klier        require_once(DOKU_INC.'inc/changelog.php');
73263dd0d58SMichael Klier        require_once(DOKU_INC.'inc/pageutils.php');
73363dd0d58SMichael Klier
73499c8d7f2Smichael        $recents = getRecentsSince($timestamp);
73563dd0d58SMichael Klier
73699c8d7f2Smichael        $changes = array();
73763dd0d58SMichael Klier
73899c8d7f2Smichael        foreach ($recents as $recent) {
73999c8d7f2Smichael            $change = array();
74099c8d7f2Smichael            $change['name']         = $recent['id'];
74199c8d7f2Smichael            $change['lastModified'] = new IXR_Date($recent['date']);
74299c8d7f2Smichael            $change['author']       = $recent['user'];
74399c8d7f2Smichael            $change['version']      = $recent['date'];
74499c8d7f2Smichael            $change['perms']        = $recent['perms'];
74599c8d7f2Smichael            $change['size']         = @filesize(wikiFN($recent['id']));
74663dd0d58SMichael Klier            array_push($changes, $change);
74799c8d7f2Smichael        }
74899c8d7f2Smichael
74999c8d7f2Smichael        if (!empty($changes)) {
75099c8d7f2Smichael            return $changes;
75163dd0d58SMichael Klier        } else {
75263dd0d58SMichael Klier            // in case we still have nothing at this point
75363dd0d58SMichael Klier            return new IXR_Error(30, 'There are no changes in the specified timeframe');
7543a1dad2dSDennis Ploeger        }
75599c8d7f2Smichael    }
75699c8d7f2Smichael
75799c8d7f2Smichael    /**
75899c8d7f2Smichael     * Returns a list of recent media changes since give timestamp
75999c8d7f2Smichael     *
76099c8d7f2Smichael     * @author Michael Hamann <michael@content-space.de>
76199c8d7f2Smichael     * @author Michael Klier <chi@chimeric.de>
76299c8d7f2Smichael     */
76399c8d7f2Smichael    function getRecentMediaChanges($timestamp) {
76499c8d7f2Smichael        if(strlen($timestamp) != 10)
76599c8d7f2Smichael            return new IXR_Error(20, 'The provided value is not a valid timestamp');
76699c8d7f2Smichael
76799c8d7f2Smichael        require_once(DOKU_INC.'inc/changelog.php');
76899c8d7f2Smichael        require_once(DOKU_INC.'inc/pageutils.php');
76999c8d7f2Smichael
77099c8d7f2Smichael        $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
77199c8d7f2Smichael
77299c8d7f2Smichael        $changes = array();
77399c8d7f2Smichael
77499c8d7f2Smichael        foreach ($recents as $recent) {
77599c8d7f2Smichael            $change = array();
77699c8d7f2Smichael            $change['name']         = $recent['id'];
77799c8d7f2Smichael            $change['lastModified'] = new IXR_Date($recent['date']);
77899c8d7f2Smichael            $change['author']       = $recent['user'];
77999c8d7f2Smichael            $change['version']      = $recent['date'];
78099c8d7f2Smichael            $change['perms']        = $recent['perms'];
781a4da2756Smichael            $change['size']         = @filesize(mediaFN($recent['id']));
78299c8d7f2Smichael            array_push($changes, $change);
78399c8d7f2Smichael        }
78499c8d7f2Smichael
78599c8d7f2Smichael        if (!empty($changes)) {
78699c8d7f2Smichael            return $changes;
78799c8d7f2Smichael        } else {
78899c8d7f2Smichael            // in case we still have nothing at this point
78999c8d7f2Smichael            return new IXR_Error(30, 'There are no changes in the specified timeframe');
79099c8d7f2Smichael        }
79199c8d7f2Smichael    }
7923a1dad2dSDennis Ploeger
7933a1dad2dSDennis Ploeger    /**
79473056168SMichael Klier     * Returns a list of available revisions of a given wiki page
79573056168SMichael Klier     *
79673056168SMichael Klier     * @author Michael Klier <chi@chimeric.de>
79773056168SMichael Klier     */
79873056168SMichael Klier    function pageVersions($id, $first) {
79973056168SMichael Klier        global $conf;
80073056168SMichael Klier
80173056168SMichael Klier        $versions = array();
80273056168SMichael Klier
80373056168SMichael Klier        if(empty($id))
80473056168SMichael Klier            return new IXR_Error(1, 'Empty page ID');
80573056168SMichael Klier
80673056168SMichael Klier        require_once(DOKU_INC.'inc/changelog.php');
80773056168SMichael Klier
80873056168SMichael Klier        $revisions = getRevisions($id, $first, $conf['recent']+1);
80973056168SMichael Klier
81073056168SMichael Klier        if(count($revisions)==0 && $first!=0) {
81173056168SMichael Klier            $first=0;
81273056168SMichael Klier            $revisions = getRevisions($id, $first, $conf['recent']+1);
81373056168SMichael Klier        }
81473056168SMichael Klier
81545c63471SMichael Klier        if(count($revisions)>0 && $first==0) {
81645c63471SMichael Klier            array_unshift($revisions, '');  // include current revision
81745c63471SMichael Klier            array_pop($revisions);          // remove extra log entry
81845c63471SMichael Klier        }
81945c63471SMichael Klier
82073056168SMichael Klier        $hasNext = false;
82173056168SMichael Klier        if(count($revisions)>$conf['recent']) {
82273056168SMichael Klier            $hasNext = true;
82373056168SMichael Klier            array_pop($revisions); // remove extra log entry
82473056168SMichael Klier        }
82573056168SMichael Klier
82673056168SMichael Klier        if(!empty($revisions)) {
82773056168SMichael Klier            foreach($revisions as $rev) {
82873056168SMichael Klier                $file = wikiFN($id,$rev);
82973056168SMichael Klier                $time = @filemtime($file);
83045c63471SMichael Klier                // we check if the page actually exists, if this is not the
83145c63471SMichael Klier                // case this can lead to less pages being returned than
83245c63471SMichael Klier                // specified via $conf['recent']
83373056168SMichael Klier                if($time){
83473056168SMichael Klier                    $info = getRevisionInfo($id, $time, 1024);
83573056168SMichael Klier                    if(!empty($info)) {
83673056168SMichael Klier                        $data['user'] = $info['user'];
83773056168SMichael Klier                        $data['ip']   = $info['ip'];
83873056168SMichael Klier                        $data['type'] = $info['type'];
83973056168SMichael Klier                        $data['sum']  = $info['sum'];
84073056168SMichael Klier                        $data['modified'] = new IXR_Date($info['date']);
84173056168SMichael Klier                        $data['version'] = $info['date'];
84273056168SMichael Klier                        array_push($versions, $data);
84373056168SMichael Klier                    }
84473056168SMichael Klier                }
84573056168SMichael Klier            }
84673056168SMichael Klier            return $versions;
84773056168SMichael Klier        } else {
84873056168SMichael Klier            return array();
84973056168SMichael Klier        }
85073056168SMichael Klier    }
85173056168SMichael Klier
85273056168SMichael Klier    /**
853797c0d11SAndreas Gohr     * The version of Wiki RPC API supported
854797c0d11SAndreas Gohr     */
855797c0d11SAndreas Gohr    function wiki_RPCVersion(){
856797c0d11SAndreas Gohr        return 2;
857797c0d11SAndreas Gohr    }
8581b11c097SAndreas Gohr
85928ec3c76SAndreas Gohr
86028ec3c76SAndreas Gohr    /**
86128ec3c76SAndreas Gohr     * Locks or unlocks a given batch of pages
86228ec3c76SAndreas Gohr     *
86328ec3c76SAndreas Gohr     * Give an associative array with two keys: lock and unlock. Both should contain a
86428ec3c76SAndreas Gohr     * list of pages to lock or unlock
86528ec3c76SAndreas Gohr     *
86628ec3c76SAndreas Gohr     * Returns an associative array with the keys locked, lockfail, unlocked and
86728ec3c76SAndreas Gohr     * unlockfail, each containing lists of pages.
86828ec3c76SAndreas Gohr     */
86928ec3c76SAndreas Gohr    function setLocks($set){
87028ec3c76SAndreas Gohr        $locked     = array();
87128ec3c76SAndreas Gohr        $lockfail   = array();
87228ec3c76SAndreas Gohr        $unlocked   = array();
87328ec3c76SAndreas Gohr        $unlockfail = array();
87428ec3c76SAndreas Gohr
87528ec3c76SAndreas Gohr        foreach((array) $set['lock'] as $id){
87628ec3c76SAndreas Gohr            if(checklock($id)){
87728ec3c76SAndreas Gohr                $lockfail[] = $id;
87828ec3c76SAndreas Gohr            }else{
87928ec3c76SAndreas Gohr                lock($id);
88028ec3c76SAndreas Gohr                $locked[] = $id;
88128ec3c76SAndreas Gohr            }
88228ec3c76SAndreas Gohr        }
88328ec3c76SAndreas Gohr
88428ec3c76SAndreas Gohr        foreach((array) $set['unlock'] as $id){
88528ec3c76SAndreas Gohr            if(unlock($id)){
88628ec3c76SAndreas Gohr                $unlocked[] = $id;
88728ec3c76SAndreas Gohr            }else{
88828ec3c76SAndreas Gohr                $unlockfail[] = $id;
88928ec3c76SAndreas Gohr            }
89028ec3c76SAndreas Gohr        }
89128ec3c76SAndreas Gohr
89228ec3c76SAndreas Gohr        return array(
89328ec3c76SAndreas Gohr            'locked'     => $locked,
89428ec3c76SAndreas Gohr            'lockfail'   => $lockfail,
89528ec3c76SAndreas Gohr            'unlocked'   => $unlocked,
89628ec3c76SAndreas Gohr            'unlockfail' => $unlockfail,
89728ec3c76SAndreas Gohr        );
89828ec3c76SAndreas Gohr    }
89928ec3c76SAndreas Gohr
900445e8084SAndreas Gohr    function getAPIVersion(){
901445e8084SAndreas Gohr        return DOKU_XMLRPC_API_VERSION;
902445e8084SAndreas Gohr    }
903445e8084SAndreas Gohr
904445e8084SAndreas Gohr    function login($user,$pass){
905445e8084SAndreas Gohr        global $conf;
906445e8084SAndreas Gohr        global $auth;
907445e8084SAndreas Gohr        if(!$conf['useacl']) return 0;
908445e8084SAndreas Gohr        if(!$auth) return 0;
909445e8084SAndreas Gohr        if($auth->canDo('external')){
910445e8084SAndreas Gohr            return $auth->trustExternal($user,$pass,false);
911445e8084SAndreas Gohr        }else{
912445e8084SAndreas Gohr            return auth_login($user,$pass,false,true);
913445e8084SAndreas Gohr        }
914445e8084SAndreas Gohr    }
9153ee5b583SAndreas Gohr
9163ee5b583SAndreas Gohr
917797c0d11SAndreas Gohr}
918797c0d11SAndreas Gohr
919797c0d11SAndreas Gohr$server = new dokuwiki_xmlrpc_server();
920797c0d11SAndreas Gohr
9212aca132fSMichael Klier// vim:ts=4:sw=4:et:enc=utf-8:
922