xref: /dokuwiki/lib/exe/xmlrpc.php (revision fdd2e9d64295969d5945f11cb7d457aa599a028f)
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
187        $id    = cleanID($id);
188        $TEXT  = trim($text);
189        $sum   = $params['sum'];
190        $minor = $params['minor'];
191
192        if(empty($id))
193            return new IXR_Error(1, 'Empty page ID');
194
195        if(auth_quickaclcheck($id) < AUTH_WRITE)
196            return new IXR_Error(1, 'You are not allowed to edit this page');
197
198        // Check, if page is locked
199        if(checklock($id))
200            return new IXR_Error(1, 'The page is currently locked');
201
202        if(empty($TEXT))
203            return new IXR_Error(1, 'No text supplied');
204
205        //spam check
206        if(checkwordblock())
207            return new IXR_Error(1, 'Positive wordblock check');
208
209        lock($id);
210
211        saveWikiText($id,$TEXT,$sum,$minor);
212
213        unlock($id);
214
215        return 0;
216    }
217
218    /**
219     * Lists all links contained in a wiki page
220     *
221     * @author Michael Klier <chi@chimeric.de>
222     */
223    function listLinks($id) {
224        if(auth_quickaclcheck($id) < AUTH_READ){
225            return new IXR_Error(1, 'You are not allowed to read this page');
226        }
227        $links = array();
228
229        // resolve page instructions
230        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
231
232        // instantiate new Renderer - needed for interwiki links
233        include(DOKU_INC.'inc/parser/xhtml.php');
234        $Renderer = new Doku_Renderer_xhtml();
235        $Renderer->interwiki = getInterwiki();
236
237        // parse parse instructions
238        foreach($ins as $in) {
239            $link = array();
240            switch($in[0]) {
241                case 'internallink':
242                    $link['type'] = 'local';
243                    $link['page'] = $in[1][0];
244                    $link['href'] = wl($in[1][0]);
245                    array_push($links,$link);
246                    break;
247                case 'externallink':
248                    $link['type'] = 'extern';
249                    $link['page'] = $in[1][0];
250                    $link['href'] = $in[1][0];
251                    array_push($links,$link);
252                    break;
253                case 'interwikilink':
254                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
255                    $link['type'] = 'extern';
256                    $link['page'] = $url;
257                    $link['href'] = $url;
258                    array_push($links,$link);
259                    break;
260            }
261        }
262
263        return ($links);
264    }
265
266    /**
267     * Returns a list of recent changes since give timestamp
268     *
269     * @author Michael Klier <chi@chimeric.de>
270     */
271    function getRecentChanges($timestamp) {
272        global $conf;
273
274        if(strlen($timestamp) != 10)
275            return new IXR_Error(20, 'The provided value is not a valid timestamp');
276
277        $changes = array();
278
279        require_once(DOKU_INC.'inc/changelog.php');
280        require_once(DOKU_INC.'inc/pageutils.php');
281
282        // read changes
283        $lines = @file($conf['changelog']);
284
285        if(empty($lines))
286            return new IXR_Error(10, 'The changelog could not be read');
287
288        // we start searching at the end of the list
289        $lines = array_reverse($lines);
290
291        // cache seen pages and skip them
292        $seen = array();
293
294        foreach($lines as $line) {
295
296            if(empty($line)) continue; // skip empty lines
297
298            $logline = parseChangelogLine($line);
299
300            if($logline === false) continue;
301
302            // skip seen ones
303            if(isset($seen[$logline['id']])) continue;
304
305            // skip minors
306            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
307
308            // remember in seen to skip additional sights
309            $seen[$logline['id']] = 1;
310
311            // check if it's a hidden page
312            if(isHiddenPage($logline['id'])) continue;
313
314            // check ACL
315            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
316
317            // check existance
318            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
319
320            // check if logline is still in the queried time frame
321            if($logline['date'] >= $timestamp) {
322                $change['name']         = $logline['id'];
323                $change['lastModified'] = new IXR_Date($logline['date']);
324                $change['author']       = $logline['user'];
325                $change['version']      = $logline['date'];
326                array_push($changes, $change);
327            } else {
328                $changes = array_reverse($changes);
329                return ($changes);
330            }
331        }
332        // in case we still have nothing at this point
333        return new IXR_Error(30, 'There are no changes in the specified timeframe');
334    }
335
336    /**
337     * The version of Wiki RPC API supported
338     */
339    function wiki_RPCVersion(){
340        return 2;
341    }
342}
343
344$server = new dokuwiki_xmlrpc_server();
345
346// vim:ts=4:sw=4:enc=utf-8:
347