xref: /dokuwiki/lib/exe/xmlrpc.php (revision 63dd0d5823c2f4374954eeeff5fb17cc1cf68a29)
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
7797c0d11SAndreas Gohr
8797c0d11SAndreas Gohr//EXPERIMENTAL CODE
9797c0d11SAndreas Gohrdie('remove me to get it work');
10797c0d11SAndreas Gohr
11797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/init.php');
12797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/common.php');
13797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/auth.php');
14797c0d11SAndreas Gohrsession_write_close();  //close session
15797c0d11SAndreas Gohrrequire_once(DOKU_INC.'inc/IXR_Library.php');
16797c0d11SAndreas Gohr
17797c0d11SAndreas Gohr
18797c0d11SAndreas Gohr/**
19797c0d11SAndreas Gohr * Contains needed wrapper functions and registers all available
20797c0d11SAndreas Gohr * XMLRPC functions.
21797c0d11SAndreas Gohr */
22797c0d11SAndreas Gohrclass dokuwiki_xmlrpc_server extends IXR_IntrospectionServer {
23797c0d11SAndreas Gohr    var $methods = array();
24797c0d11SAndreas Gohr
25797c0d11SAndreas Gohr    /**
26797c0d11SAndreas Gohr     * Constructor. Register methods and run Server
27797c0d11SAndreas Gohr     */
28797c0d11SAndreas Gohr    function dokuwiki_xmlrpc_server(){
29797c0d11SAndreas Gohr        $this->IXR_IntrospectionServer();
30797c0d11SAndreas Gohr
31797c0d11SAndreas Gohr        /* DokuWiki's own methods */
32797c0d11SAndreas Gohr        $this->addCallback(
33797c0d11SAndreas Gohr            'dokuwiki.getVersion',
34797c0d11SAndreas Gohr            'getVersion',
35797c0d11SAndreas Gohr            array('string'),
36797c0d11SAndreas Gohr            'Returns the running DokuWiki version'
37797c0d11SAndreas Gohr        );
38797c0d11SAndreas Gohr
39797c0d11SAndreas Gohr        /* Wiki API v2 http://www.jspwiki.org/wiki/WikiRPCInterface2 */
40797c0d11SAndreas Gohr        $this->addCallback(
41797c0d11SAndreas Gohr            'wiki.getRPCVersionSupported',
42797c0d11SAndreas Gohr            'this:wiki_RPCVersion',
43797c0d11SAndreas Gohr            array('int'),
44797c0d11SAndreas Gohr            'Returns 2 with the supported RPC API version'
45797c0d11SAndreas Gohr        );
46797c0d11SAndreas Gohr        $this->addCallback(
47797c0d11SAndreas Gohr            'wiki.getPage',
48797c0d11SAndreas Gohr            'this:rawPage',
49797c0d11SAndreas Gohr            array('string','string'),
50797c0d11SAndreas Gohr            'Get the raw Wiki text of page, latest version.'
51797c0d11SAndreas Gohr        );
52797c0d11SAndreas Gohr        $this->addCallback(
53797c0d11SAndreas Gohr            'wiki.getPageVersion',
54797c0d11SAndreas Gohr            'this:rawPage',
55797c0d11SAndreas Gohr            array('string','string','int'),
56797c0d11SAndreas Gohr            'Get the raw Wiki text of page.'
57797c0d11SAndreas Gohr        );
58797c0d11SAndreas Gohr        $this->addCallback(
59797c0d11SAndreas Gohr            'wiki.getPageHTML',
60797c0d11SAndreas Gohr            'this:htmlPage',
61797c0d11SAndreas Gohr            array('string','string'),
62797c0d11SAndreas Gohr            'Return page in rendered HTML, latest version.'
63797c0d11SAndreas Gohr        );
64797c0d11SAndreas Gohr        $this->addCallback(
65797c0d11SAndreas Gohr            'wiki.getPageHTMLVersion',
66797c0d11SAndreas Gohr            'this:htmlPage',
67797c0d11SAndreas Gohr            array('string','string','int'),
68797c0d11SAndreas Gohr            'Return page in rendered HTML.'
69797c0d11SAndreas Gohr        );
70797c0d11SAndreas Gohr        $this->addCallback(
71797c0d11SAndreas Gohr            'wiki.getAllPages',
72797c0d11SAndreas Gohr            'this:listPages',
73797c0d11SAndreas Gohr            array('struct'),
74797c0d11SAndreas Gohr            'Returns a list of all pages. The result is an array of utf8 pagenames.'
75797c0d11SAndreas Gohr        );
76797c0d11SAndreas Gohr        $this->addCallback(
77797c0d11SAndreas Gohr            'wiki.getBackLinks',
78797c0d11SAndreas Gohr            'this:listBackLinks',
79797c0d11SAndreas Gohr            array('struct','string'),
80797c0d11SAndreas Gohr            'Returns the pages that link to this page.'
81797c0d11SAndreas Gohr        );
82797c0d11SAndreas Gohr        $this->addCallback(
83797c0d11SAndreas Gohr            'wiki.getPageInfo',
84797c0d11SAndreas Gohr            'this:pageInfo',
85797c0d11SAndreas Gohr            array('struct','string'),
86797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
87797c0d11SAndreas Gohr        );
88797c0d11SAndreas Gohr        $this->addCallback(
89797c0d11SAndreas Gohr            'wiki.getPageInfoVersion',
90797c0d11SAndreas Gohr            'this:pageInfo',
91797c0d11SAndreas Gohr            array('struct','string','int'),
92797c0d11SAndreas Gohr            'Returns a struct with infos about the page.'
93797c0d11SAndreas Gohr        );
943a1dad2dSDennis Ploeger        $this->addCallback(
953a1dad2dSDennis Ploeger            'wiki.putPage',
963a1dad2dSDennis Ploeger            'this:putPage',
973a1dad2dSDennis Ploeger            array('int', 'string', 'string'),
983a1dad2dSDennis Ploeger            'Saves a wiki page'
993a1dad2dSDennis Ploeger        );
100beccd742SMichael Klier        $this->addCallback(
101beccd742SMichael Klier            'wiki.listLinks',
102beccd742SMichael Klier            'this:listLinks',
103beccd742SMichael Klier            array('struct','string'),
104beccd742SMichael Klier            'Lists all links contained in a wiki page'
105beccd742SMichael Klier        );
106*63dd0d58SMichael Klier        $this->addCallback(
107*63dd0d58SMichael Klier            'wiki.getRecentChanges',
108*63dd0d58SMichael Klier            'this:getRecentChanges',
109*63dd0d58SMichael Klier            array('struct','int'),
110*63dd0d58SMichael Klier            'Returns a strukt about all recent changes since given timestamp.'
111*63dd0d58SMichael Klier        );
112797c0d11SAndreas Gohr
113797c0d11SAndreas Gohr        $this->serve();
114797c0d11SAndreas Gohr    }
115797c0d11SAndreas Gohr
116797c0d11SAndreas Gohr    /**
117797c0d11SAndreas Gohr     * Return a raw wiki page
118797c0d11SAndreas Gohr     */
119797c0d11SAndreas Gohr    function rawPage($id,$rev=''){
120797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
121797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
122797c0d11SAndreas Gohr        }
123797c0d11SAndreas Gohr        return rawWiki($id,$rev);
124797c0d11SAndreas Gohr    }
125797c0d11SAndreas Gohr
126797c0d11SAndreas Gohr    /**
127797c0d11SAndreas Gohr     * Return a wiki page rendered to html
128797c0d11SAndreas Gohr     */
129797c0d11SAndreas Gohr    function htmlPage($id,$rev=''){
130797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
131797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
132797c0d11SAndreas Gohr        }
133797c0d11SAndreas Gohr        return p_wiki_xhtml($id,$rev,false);
134797c0d11SAndreas Gohr    }
135797c0d11SAndreas Gohr
136797c0d11SAndreas Gohr    /**
137797c0d11SAndreas Gohr     * List all pages - we use the indexer list here
138797c0d11SAndreas Gohr     */
139797c0d11SAndreas Gohr    function listPages(){
140797c0d11SAndreas Gohr        require_once(DOKU_INC.'inc/fulltext.php');
141797c0d11SAndreas Gohr        return ft_pageLookup('');
142797c0d11SAndreas Gohr    }
143797c0d11SAndreas Gohr
144797c0d11SAndreas Gohr    /**
145797c0d11SAndreas Gohr     * Return a list of backlinks
146797c0d11SAndreas Gohr     */
147beccd742SMichael Klier    function listBackLinks($id){
148797c0d11SAndreas Gohr        require_once(DOKU_INC.'inc/fulltext.php');
149797c0d11SAndreas Gohr        return ft_backlinks($id);
150797c0d11SAndreas Gohr    }
151797c0d11SAndreas Gohr
152797c0d11SAndreas Gohr    /**
153*63dd0d58SMichael Klier     * Return some basic data about a page
154797c0d11SAndreas Gohr     */
155797c0d11SAndreas Gohr    function pageInfo($id,$rev=''){
156797c0d11SAndreas Gohr        if(auth_quickaclcheck($id) < AUTH_READ){
157797c0d11SAndreas Gohr            return new IXR_Error(1, 'You are not allowed to read this page');
158797c0d11SAndreas Gohr        }
159797c0d11SAndreas Gohr        $file = wikiFN($id,$rev);
160797c0d11SAndreas Gohr        $time = @filemtime($file);
161797c0d11SAndreas Gohr        if(!$time){
162797c0d11SAndreas Gohr            return new IXR_Error(10, 'The requested page does not exist');
163797c0d11SAndreas Gohr        }
164797c0d11SAndreas Gohr
165797c0d11SAndreas Gohr        $info = getRevisionInfo($id, $time, 1024);
166797c0d11SAndreas Gohr
167797c0d11SAndreas Gohr        $data = array(
168797c0d11SAndreas Gohr            'name'         => $id,
169797c0d11SAndreas Gohr            'lastModified' => new IXR_Date($time),
170797c0d11SAndreas Gohr            'author'       => (($info['user']) ? $info['user'] : $info['ip']),
171797c0d11SAndreas Gohr            'version'      => $time
172797c0d11SAndreas Gohr        );
173*63dd0d58SMichael Klier
174*63dd0d58SMichael Klier        return ($data);
175797c0d11SAndreas Gohr    }
176797c0d11SAndreas Gohr
177797c0d11SAndreas Gohr    /**
1783a1dad2dSDennis Ploeger     * Save a wiki page
179*63dd0d58SMichael Klier     * FIXME check ACL !!!
1803a1dad2dSDennis Ploeger     */
1813a1dad2dSDennis Ploeger    function putPage($put_id, $put_text, $put_summary='', $put_minor=0, $put_rev='', $put_pre='', $put_suf='') {
1823a1dad2dSDennis Ploeger        global $TEXT;
1833a1dad2dSDennis Ploeger
1843a1dad2dSDennis Ploeger        $TEXT = $put_text;
1853a1dad2dSDennis Ploeger
1863a1dad2dSDennis Ploeger        // Check, if page is locked
187beccd742SMichael Klier        if (checklock($put_id) !== false)
1883a1dad2dSDennis Ploeger            return 1;
1893a1dad2dSDennis Ploeger
1903a1dad2dSDennis Ploeger        //spam check
1913a1dad2dSDennis Ploeger        if(checkwordblock())
1923a1dad2dSDennis Ploeger            return 2;
1933a1dad2dSDennis Ploeger
1943a1dad2dSDennis Ploeger        lock($put_id);
1953a1dad2dSDennis Ploeger
196beccd742SMichael Klier        saveWikiText($put_id,con($put_pre,$put_text,$put_suf,1),$put_summary,$put_minor);
1973a1dad2dSDennis Ploeger
1983a1dad2dSDennis Ploeger        unlock($put_id);
1993a1dad2dSDennis Ploeger
2003a1dad2dSDennis Ploeger        return 0;
201beccd742SMichael Klier    }
2023a1dad2dSDennis Ploeger
203beccd742SMichael Klier    /**
204beccd742SMichael Klier     * Lists all links contained in a wiki page
205*63dd0d58SMichael Klier     *
206*63dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
207beccd742SMichael Klier     */
208beccd742SMichael Klier    function listLinks($id) {
209beccd742SMichael Klier        if(auth_quickaclcheck($id) < AUTH_READ){
210beccd742SMichael Klier            return new IXR_Error(1, 'You are not allowed to read this page');
211beccd742SMichael Klier        }
212beccd742SMichael Klier        $links = array();
213beccd742SMichael Klier
214beccd742SMichael Klier        // resolve page instructions
215beccd742SMichael Klier        $ins   = p_cached_instructions(wikiFN(cleanID($id)));
216beccd742SMichael Klier
217beccd742SMichael Klier        // instantiate new Renderer - needed for interwiki links
218beccd742SMichael Klier        include(DOKU_INC.'inc/parser/xhtml.php');
219beccd742SMichael Klier        $Renderer = new Doku_Renderer_xhtml();
220beccd742SMichael Klier        $Renderer->interwiki = getInterwiki();
221beccd742SMichael Klier
222beccd742SMichael Klier        // parse parse instructions
223beccd742SMichael Klier        foreach($ins as $in) {
224beccd742SMichael Klier            $link = array();
225beccd742SMichael Klier            switch($in[0]) {
226beccd742SMichael Klier                case 'internallink':
227beccd742SMichael Klier                    $link['type'] = 'local';
228beccd742SMichael Klier                    $link['page'] = $in[1][0];
229beccd742SMichael Klier                    $link['href'] = wl($in[1][0]);
230beccd742SMichael Klier                    array_push($links,$link);
231beccd742SMichael Klier                    break;
232beccd742SMichael Klier                case 'externallink':
233beccd742SMichael Klier                    $link['type'] = 'extern';
234beccd742SMichael Klier                    $link['page'] = $in[1][0];
235beccd742SMichael Klier                    $link['href'] = $in[1][0];
236beccd742SMichael Klier                    array_push($links,$link);
237beccd742SMichael Klier                    break;
238beccd742SMichael Klier                case 'interwikilink':
239beccd742SMichael Klier                    $url = $Renderer->_resolveInterWiki($in[1][2],$in[1][3]);
240beccd742SMichael Klier                    $link['type'] = 'extern';
241beccd742SMichael Klier                    $link['page'] = $url;
242beccd742SMichael Klier                    $link['href'] = $url;
243beccd742SMichael Klier                    array_push($links,$link);
244beccd742SMichael Klier                    break;
245beccd742SMichael Klier            }
246beccd742SMichael Klier        }
247beccd742SMichael Klier
248*63dd0d58SMichael Klier        return ($links);
249*63dd0d58SMichael Klier    }
250*63dd0d58SMichael Klier
251*63dd0d58SMichael Klier    /**
252*63dd0d58SMichael Klier     * Returns a list of recent changes since give timestamp
253*63dd0d58SMichael Klier     *
254*63dd0d58SMichael Klier     * @author Michael Klier <chi@chimeric.de>
255*63dd0d58SMichael Klier     */
256*63dd0d58SMichael Klier    function getRecentChanges($timestamp) {
257*63dd0d58SMichael Klier        global $conf;
258*63dd0d58SMichael Klier
259*63dd0d58SMichael Klier        if(strlen($timestamp) != 10)
260*63dd0d58SMichael Klier            return new IXR_Error(20, 'The provided value is not a valid timestamp');
261*63dd0d58SMichael Klier
262*63dd0d58SMichael Klier        $changes = array();
263*63dd0d58SMichael Klier
264*63dd0d58SMichael Klier        require_once(DOKU_INC.'inc/changelog.php');
265*63dd0d58SMichael Klier        require_once(DOKU_INC.'inc/pageutils.php');
266*63dd0d58SMichael Klier
267*63dd0d58SMichael Klier        // read changes
268*63dd0d58SMichael Klier        $lines = @file($conf['changelog']);
269*63dd0d58SMichael Klier
270*63dd0d58SMichael Klier        if(empty($lines))
271*63dd0d58SMichael Klier            return new IXR_Error(10, 'The changelog could not be read');
272*63dd0d58SMichael Klier
273*63dd0d58SMichael Klier        // we start searching at the end of the list
274*63dd0d58SMichael Klier        $lines = array_reverse($lines);
275*63dd0d58SMichael Klier
276*63dd0d58SMichael Klier        // cache seen pages and skip them
277*63dd0d58SMichael Klier        $seen = array();
278*63dd0d58SMichael Klier
279*63dd0d58SMichael Klier        foreach($lines as $line) {
280*63dd0d58SMichael Klier
281*63dd0d58SMichael Klier            if(empty($line)) continue; // skip empty lines
282*63dd0d58SMichael Klier
283*63dd0d58SMichael Klier            $logline = parseChangelogLine($line);
284*63dd0d58SMichael Klier
285*63dd0d58SMichael Klier            if($logline === false) continue;
286*63dd0d58SMichael Klier
287*63dd0d58SMichael Klier            // skip seen ones
288*63dd0d58SMichael Klier            if(isset($seen[$logline['id']])) continue;
289*63dd0d58SMichael Klier
290*63dd0d58SMichael Klier            // skip minors
291*63dd0d58SMichael Klier            if($logline['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT && ($flags & RECENTS_SKIP_MINORS)) continue;
292*63dd0d58SMichael Klier
293*63dd0d58SMichael Klier            // remember in seen to skip additional sights
294*63dd0d58SMichael Klier            $seen[$logline['id']] = 1;
295*63dd0d58SMichael Klier
296*63dd0d58SMichael Klier            // check if it's a hidden page
297*63dd0d58SMichael Klier            if(isHiddenPage($logline['id'])) continue;
298*63dd0d58SMichael Klier
299*63dd0d58SMichael Klier            // check ACL
300*63dd0d58SMichael Klier            if(auth_quickaclcheck($logline['id']) < AUTH_READ) continue;
301*63dd0d58SMichael Klier
302*63dd0d58SMichael Klier            // check existance
303*63dd0d58SMichael Klier            if((!@file_exists(wikiFN($logline['id']))) && ($flags & RECENTS_SKIP_DELETED)) continue;
304*63dd0d58SMichael Klier
305*63dd0d58SMichael Klier            // check if logline is still in the queried time frame
306*63dd0d58SMichael Klier            if($logline['date'] >= $timestamp) {
307*63dd0d58SMichael Klier                $change['name']         = $logline['id'];
308*63dd0d58SMichael Klier                $change['lastModified'] = $logline['date'];
309*63dd0d58SMichael Klier                $change['author']       = $logline['user'];
310*63dd0d58SMichael Klier                $change['version']      = $logline['date'];
311*63dd0d58SMichael Klier                array_push($changes, $change);
312*63dd0d58SMichael Klier            } else {
313*63dd0d58SMichael Klier                $changes = array_reverse($changes);
314*63dd0d58SMichael Klier                return ($changes);
315*63dd0d58SMichael Klier            }
316*63dd0d58SMichael Klier        }
317*63dd0d58SMichael Klier        // in case we still have nothing at this point
318*63dd0d58SMichael Klier        return new IXR_Error(30, 'There are no changes in the specified timeframe');
3193a1dad2dSDennis Ploeger    }
3203a1dad2dSDennis Ploeger
3213a1dad2dSDennis Ploeger    /**
322797c0d11SAndreas Gohr     * The version of Wiki RPC API supported
323797c0d11SAndreas Gohr     */
324797c0d11SAndreas Gohr    function wiki_RPCVersion(){
325797c0d11SAndreas Gohr        return 2;
326797c0d11SAndreas Gohr    }
327797c0d11SAndreas Gohr
328797c0d11SAndreas Gohr}
329797c0d11SAndreas Gohr
330797c0d11SAndreas Gohr$server = new dokuwiki_xmlrpc_server();
331797c0d11SAndreas Gohr
332*63dd0d58SMichael Klier// vim:ts=4:sw=4:enc=utf-8:
333