xref: /dokuwiki/lib/exe/xmlrpc.php (revision 51597811fb665e459be288068ab39bb691bf6de1)
1<?php
2if(!defined('DOKU_INC')) define('DOKU_INC',dirname(__FILE__).'/../../');
3
4// fix when '<?xml' isn't on the very first line
5if(isset($HTTP_RAW_POST_DATA)) $HTTP_RAW_POST_DATA = trim($HTTP_RAW_POST_DATA);
6
7
8require_once(DOKU_INC.'inc/init.php');
9
10if(!$conf['xmlrpc']) {
11    die('XML-RPC server not enabled.');
12}
13
14require_once(DOKU_INC.'inc/common.php');
15require_once(DOKU_INC.'inc/auth.php');
16session_write_close();  //close session
17require_once(DOKU_INC.'inc/IXR_Library.php');
18
19
20/**
21 * Contains needed wrapper functions and registers all available
22 * XMLRPC functions.
23 */
24class dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
25    var $methods = array();
26
27    /**
28     * Constructor. Register methods and run Server
29     */
30    function dokuwiki_xmlrpc_server(){
31        $this->IXR_IntrospectionServer();
32
33        /* DokuWiki's own methods */
34        $this->addCallback(
35            'dokuwiki.getVersion',
36            'getVersion',
37            array('string'),
38            'Returns the running DokuWiki version.'
39        );
40
41        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
42        $this->addCallback(
43            'wiki.getRPCVersionSupported',
44            'this:wiki_RPCVersion',
45            array('int'),
46            'Returns 2 with the supported RPC API version.'
47        );
48        $this->addCallback(
49            'wiki.getPage',
50            'this:rawPage',
51            array('string','string'),
52            'Get the raw Wiki text of page, latest version.'
53        );
54        $this->addCallback(
55            'wiki.getPageVersion',
56            'this:rawPage',
57            array('string','string','int'),
58            'Get the raw Wiki text of page.'
59        );
60        $this->addCallback(
61            'wiki.getPageHTML',
62            'this:htmlPage',
63            array('string','string'),
64            'Return page in rendered HTML, latest version.'
65        );
66        $this->addCallback(
67            'wiki.getPageHTMLVersion',
68            'this:htmlPage',
69            array('string','string','int'),
70            'Return page in rendered HTML.'
71        );
72        $this->addCallback(
73            'wiki.getAllPages',
74            'this:listPages',
75            array('struct'),
76            'Returns a list of all pages. The result is an array of utf8 pagenames.'
77        );
78        $this->addCallback(
79            'wiki.getBackLinks',
80            'this:listBackLinks',
81            array('struct','string'),
82            'Returns the pages that link to this page.'
83        );
84        $this->addCallback(
85            'wiki.getPageInfo',
86            'this:pageInfo',
87            array('struct','string'),
88            'Returns a struct with infos about the page.'
89        );
90        $this->addCallback(
91            'wiki.getPageInfoVersion',
92            'this:pageInfo',
93            array('struct','string','int'),
94            'Returns a struct with infos about the page.'
95        );
96        $this->addCallback(
97            'wiki.putPage',
98            'this:putPage',
99            array('int', 'string', 'string', 'struct'),
100            'Saves a wiki page.'
101        );
102        $this->addCallback(
103            'wiki.listLinks',
104            'this:listLinks',
105            array('struct','string'),
106            'Lists all links contained in a wiki page.'
107        );
108        $this->addCallback(
109            'wiki.getRecentChanges',
110            'this:getRecentChanges',
111            array('struct','int'),
112            'Returns a strukt about all recent changes since given timestamp.'
113        );
114
115        $this->serve();
116    }
117
118    /**
119     * Return a raw wiki page
120     */
121    function rawPage($id,$rev=''){
122        if(auth_quickaclcheck($id) < AUTH_READ){
123            return new IXR_Error(1, 'You are not allowed to read this page');
124        }
125        return rawWiki($id,$rev);
126    }
127
128    /**
129     * Return a wiki page rendered to html
130     */
131    function htmlPage($id,$rev=''){
132        if(auth_quickaclcheck($id) < AUTH_READ){
133            return new IXR_Error(1, 'You are not allowed to read this page');
134        }
135        return p_wiki_xhtml($id,$rev,false);
136    }
137
138    /**
139     * List all pages - we use the indexer list here
140     */
141    function listPages(){
142        require_once(DOKU_INC.'inc/fulltext.php');
143        return ft_pageLookup('');
144    }
145
146    /**
147     * Return a list of backlinks
148     */
149    function listBackLinks($id){
150        require_once(DOKU_INC.'inc/fulltext.php');
151        return ft_backlinks($id);
152    }
153
154    /**
155     * Return some basic data about a page
156     */
157    function pageInfo($id,$rev=''){
158        if(auth_quickaclcheck($id) < AUTH_READ){
159            return new IXR_Error(1, 'You are not allowed to read this page');
160        }
161        $file = wikiFN($id,$rev);
162        $time = @filemtime($file);
163        if(!$time){
164            return new IXR_Error(10, 'The requested page does not exist');
165        }
166
167        $info = getRevisionInfo($id, $time, 1024);
168
169        $data = array(
170            'name'         => $id,
171            'lastModified' => new IXR_Date($time),
172            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
173            'version'      => $time
174        );
175
176        return ($data);
177    }
178
179    /**
180     * Save a wiki page
181     *
182     * @author Michael Klier <chi@chimeric.de>
183     */
184    function putPage($id, $text, $params) {
185        global $TEXT;
186        global $lang;
187
188        $id    = cleanID($id);
189        $TEXT  = trim($text);
190        $sum   = $params['sum'];
191        $minor = $params['minor'];
192
193        if(empty($id))
194            return new IXR_Error(1, 'Empty page ID');
195
196        if(!page_exists($id) && empty($TEXT)) {
197            return new IXR_ERROR(1, 'Refusing to write an empty new wiki page');
198        }
199
200        if(auth_quickaclcheck($id) < AUTH_WRITE)
201            return new IXR_Error(1, 'You are not allowed to edit this page');
202
203        // Check, if page is locked
204        if(checklock($id))
205            return new IXR_Error(1, 'The page is currently locked');
206
207        // SPAM check
208        if(checkwordblock())
209            return new IXR_Error(1, 'Positive wordblock check');
210
211        // autoset summary on new pages
212        if(!page_exists($id) && empty($sum)) {
213            $sum = $lang['created'];
214        }
215
216        // autoset summary on deleted pages
217        if(page_exists($id) && empty($TEXT) && empty($sum)) {
218            $sum = $lang['deleted'];
219        }
220
221        lock($id);
222
223        saveWikiText($id,$TEXT,$sum,$minor);
224
225        unlock($id);
226
227        return 0;
228    }
229
230    /**
231     * Lists all links contained in a wiki page
232     *
233     * @author Michael Klier <chi@chimeric.de>
234     */
235    function listLinks($id) {
236        if(auth_quickaclcheck($id) < AUTH_READ){
237            return new IXR_Error(1, 'You are not allowed to read this page');
238        }
239        $links = array();
240
241        // resolve page instructions
242        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
243
244        // instantiate new Renderer - needed for interwiki links
245        include(DOKU_INC.'inc/parser/xhtml.php');
246        $Renderer = new Doku_Renderer_xhtml();
247        $Renderer->interwiki = getInterwiki();
248
249        // parse parse instructions
250        foreach($ins as $in) {
251            $link = array();
252            switch($in[0]) {
253                case 'internallink':
254                    $link['type'] = 'local';
255                    $link['page'] = $in[1][0];
256                    $link['href'] = wl($in[1][0]);
257                    array_push($links,$link);
258                    break;
259                case 'externallink':
260                    $link['type'] = 'extern';
261                    $link['page'] = $in[1][0];
262                    $link['href'] = $in[1][0];
263                    array_push($links,$link);
264                    break;
265                case 'interwikilink':
266                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
267                    $link['type'] = 'extern';
268                    $link['page'] = $url;
269                    $link['href'] = $url;
270                    array_push($links,$link);
271                    break;
272            }
273        }
274
275        return ($links);
276    }
277
278    /**
279     * Returns a list of recent changes since give timestamp
280     *
281     * @author Michael Klier <chi@chimeric.de>
282     */
283    function getRecentChanges($timestamp) {
284        global $conf;
285
286        if(strlen($timestamp) != 10)
287            return new IXR_Error(20, 'The provided value is not a valid timestamp');
288
289        $changes = array();
290
291        require_once(DOKU_INC.'inc/changelog.php');
292        require_once(DOKU_INC.'inc/pageutils.php');
293
294        // read changes
295        $lines = @file($conf['changelog']);
296
297        if(empty($lines))
298            return new IXR_Error(10, 'The changelog could not be read');
299
300        // we start searching at the end of the list
301        $lines = array_reverse($lines);
302
303        // cache seen pages and skip them
304        $seen = array();
305
306        foreach($lines as $line) {
307
308            if(empty($line)) continue; // skip empty lines
309
310            $logline = parseChangelogLine($line);
311
312            if($logline === false) continue;
313
314            // skip seen ones
315            if(isset($seen[$logline['id']])) continue;
316
317            // skip minors
318            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
319
320            // remember in seen to skip additional sights
321            $seen[$logline['id']] = 1;
322
323            // check if it's a hidden page
324            if(isHiddenPage($logline['id'])) continue;
325
326            // check ACL
327            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
328
329            // check existance
330            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
331
332            // check if logline is still in the queried time frame
333            if($logline['date'] >= $timestamp) {
334                $change['name']         = $logline['id'];
335                $change['lastModified'] = new IXR_Date($logline['date']);
336                $change['author']       = $logline['user'];
337                $change['version']      = $logline['date'];
338                array_push($changes, $change);
339            } else {
340                $changes = array_reverse($changes);
341                return ($changes);
342            }
343        }
344        // in case we still have nothing at this point
345        return new IXR_Error(30, 'There are no changes in the specified timeframe');
346    }
347
348    /**
349     * The version of Wiki RPC API supported
350     */
351    function wiki_RPCVersion(){
352        return 2;
353    }
354}
355
356$server = new dokuwiki_xmlrpc_server();
357
358// vim:ts=4:sw=4:enc=utf-8:
359