xref: /dokuwiki/inc/Remote/ApiCore.php (revision e6a9d76f2ac37911f6a929689546fc2d074b8295)
1dd87735dSAndreas Gohr<?php
2dd87735dSAndreas Gohr
3dd87735dSAndreas Gohrnamespace dokuwiki\Remote;
4dd87735dSAndreas Gohr
5dd87735dSAndreas Gohruse Doku_Renderer_xhtml;
60c3a5702SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
70c3a5702SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog;
8cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
92d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
10dd87735dSAndreas Gohr
11dd87735dSAndreas Gohrdefine('DOKU_API_VERSION', 10);
12dd87735dSAndreas Gohr
13dd87735dSAndreas Gohr/**
14dd87735dSAndreas Gohr * Provides the core methods for the remote API.
15dd87735dSAndreas Gohr * The methods are ordered in 'wiki.<method>' and 'dokuwiki.<method>' namespaces
16dd87735dSAndreas Gohr */
17dd87735dSAndreas Gohrclass ApiCore
18dd87735dSAndreas Gohr{
19dd87735dSAndreas Gohr    /** @var int Increased whenever the API is changed */
20dd87735dSAndreas Gohr    const API_VERSION = 10;
21dd87735dSAndreas Gohr
22dd87735dSAndreas Gohr
23dd87735dSAndreas Gohr    /** @var Api */
24dd87735dSAndreas Gohr    private $api;
25dd87735dSAndreas Gohr
26dd87735dSAndreas Gohr    /**
27dd87735dSAndreas Gohr     * @param Api $api
28dd87735dSAndreas Gohr     */
29dd87735dSAndreas Gohr    public function __construct(Api $api)
30dd87735dSAndreas Gohr    {
31dd87735dSAndreas Gohr        $this->api = $api;
32dd87735dSAndreas Gohr    }
33dd87735dSAndreas Gohr
34dd87735dSAndreas Gohr    /**
35dd87735dSAndreas Gohr     * Returns details about the core methods
36dd87735dSAndreas Gohr     *
37dd87735dSAndreas Gohr     * @return array
38dd87735dSAndreas Gohr     */
39*e6a9d76fSSyntaxseed    public function getRemoteInfo()
40dd87735dSAndreas Gohr    {
41dd87735dSAndreas Gohr        return array(
42dd87735dSAndreas Gohr            'dokuwiki.getVersion' => array(
43dd87735dSAndreas Gohr                'args' => array(),
44dd87735dSAndreas Gohr                'return' => 'string',
45dd87735dSAndreas Gohr                'doc' => 'Returns the running DokuWiki version.'
46dd87735dSAndreas Gohr            ), 'dokuwiki.login' => array(
47dd87735dSAndreas Gohr                'args' => array('string', 'string'),
48dd87735dSAndreas Gohr                'return' => 'int',
49dd87735dSAndreas Gohr                'doc' => 'Tries to login with the given credentials and sets auth cookies.',
50dd87735dSAndreas Gohr                'public' => '1'
51dd87735dSAndreas Gohr            ), 'dokuwiki.logoff' => array(
52dd87735dSAndreas Gohr                'args' => array(),
53dd87735dSAndreas Gohr                'return' => 'int',
54dd87735dSAndreas Gohr                'doc' => 'Tries to logoff by expiring auth cookies and the associated PHP session.'
55dd87735dSAndreas Gohr            ), 'dokuwiki.getPagelist' => array(
56dd87735dSAndreas Gohr                'args' => array('string', 'array'),
57dd87735dSAndreas Gohr                'return' => 'array',
58dd87735dSAndreas Gohr                'doc' => 'List all pages within the given namespace.',
59dd87735dSAndreas Gohr                'name' => 'readNamespace'
60dd87735dSAndreas Gohr            ), 'dokuwiki.search' => array(
61dd87735dSAndreas Gohr                'args' => array('string'),
62dd87735dSAndreas Gohr                'return' => 'array',
63dd87735dSAndreas Gohr                'doc' => 'Perform a fulltext search and return a list of matching pages'
64dd87735dSAndreas Gohr            ), 'dokuwiki.getTime' => array(
65dd87735dSAndreas Gohr                'args' => array(),
66dd87735dSAndreas Gohr                'return' => 'int',
67dd87735dSAndreas Gohr                'doc' => 'Returns the current time at the remote wiki server as Unix timestamp.',
68dd87735dSAndreas Gohr            ), 'dokuwiki.setLocks' => array(
69dd87735dSAndreas Gohr                'args' => array('array'),
70dd87735dSAndreas Gohr                'return' => 'array',
71dd87735dSAndreas Gohr                'doc' => 'Lock or unlock pages.'
72dd87735dSAndreas Gohr            ), 'dokuwiki.getTitle' => array(
73dd87735dSAndreas Gohr                'args' => array(),
74dd87735dSAndreas Gohr                'return' => 'string',
75dd87735dSAndreas Gohr                'doc' => 'Returns the wiki title.',
76dd87735dSAndreas Gohr                'public' => '1'
77dd87735dSAndreas Gohr            ), 'dokuwiki.appendPage' => array(
78dd87735dSAndreas Gohr                'args' => array('string', 'string', 'array'),
79dd87735dSAndreas Gohr                'return' => 'bool',
80dd87735dSAndreas Gohr                'doc' => 'Append text to a wiki page.'
818eb28c6dSAndreas Gohr            ), 'dokuwiki.deleteUsers' => array(
828eb28c6dSAndreas Gohr                'args' => array('array'),
838eb28c6dSAndreas Gohr                'return' => 'bool',
848eb28c6dSAndreas Gohr                'doc' => 'Remove one or more users from the list of registered users.'
85dd87735dSAndreas Gohr            ),  'wiki.getPage' => array(
86dd87735dSAndreas Gohr                'args' => array('string'),
87dd87735dSAndreas Gohr                'return' => 'string',
88dd87735dSAndreas Gohr                'doc' => 'Get the raw Wiki text of page, latest version.',
89dd87735dSAndreas Gohr                'name' => 'rawPage',
90dd87735dSAndreas Gohr            ), 'wiki.getPageVersion' => array(
91dd87735dSAndreas Gohr                'args' => array('string', 'int'),
92dd87735dSAndreas Gohr                'name' => 'rawPage',
93dd87735dSAndreas Gohr                'return' => 'string',
94dd87735dSAndreas Gohr                'doc' => 'Return a raw wiki page'
95dd87735dSAndreas Gohr            ), 'wiki.getPageHTML' => array(
96dd87735dSAndreas Gohr                'args' => array('string'),
97dd87735dSAndreas Gohr                'return' => 'string',
98dd87735dSAndreas Gohr                'doc' => 'Return page in rendered HTML, latest version.',
99dd87735dSAndreas Gohr                'name' => 'htmlPage'
100dd87735dSAndreas Gohr            ), 'wiki.getPageHTMLVersion' => array(
101dd87735dSAndreas Gohr                'args' => array('string', 'int'),
102dd87735dSAndreas Gohr                'return' => 'string',
103dd87735dSAndreas Gohr                'doc' => 'Return page in rendered HTML.',
104dd87735dSAndreas Gohr                'name' => 'htmlPage'
105dd87735dSAndreas Gohr            ), 'wiki.getAllPages' => array(
106dd87735dSAndreas Gohr                'args' => array(),
107dd87735dSAndreas Gohr                'return' => 'array',
108dd87735dSAndreas Gohr                'doc' => 'Returns a list of all pages. The result is an array of utf8 pagenames.',
109dd87735dSAndreas Gohr                'name' => 'listPages'
110dd87735dSAndreas Gohr            ), 'wiki.getAttachments' => array(
111dd87735dSAndreas Gohr                'args' => array('string', 'array'),
112dd87735dSAndreas Gohr                'return' => 'array',
113dd87735dSAndreas Gohr                'doc' => 'Returns a list of all media files.',
114dd87735dSAndreas Gohr                'name' => 'listAttachments'
115dd87735dSAndreas Gohr            ), 'wiki.getBackLinks' => array(
116dd87735dSAndreas Gohr                'args' => array('string'),
117dd87735dSAndreas Gohr                'return' => 'array',
118dd87735dSAndreas Gohr                'doc' => 'Returns the pages that link to this page.',
119dd87735dSAndreas Gohr                'name' => 'listBackLinks'
120dd87735dSAndreas Gohr            ), 'wiki.getPageInfo' => array(
121dd87735dSAndreas Gohr                'args' => array('string'),
122dd87735dSAndreas Gohr                'return' => 'array',
123dd87735dSAndreas Gohr                'doc' => 'Returns a struct with info about the page, latest version.',
124dd87735dSAndreas Gohr                'name' => 'pageInfo'
125dd87735dSAndreas Gohr            ), 'wiki.getPageInfoVersion' => array(
126dd87735dSAndreas Gohr                'args' => array('string', 'int'),
127dd87735dSAndreas Gohr                'return' => 'array',
128dd87735dSAndreas Gohr                'doc' => 'Returns a struct with info about the page.',
129dd87735dSAndreas Gohr                'name' => 'pageInfo'
130dd87735dSAndreas Gohr            ), 'wiki.getPageVersions' => array(
131dd87735dSAndreas Gohr                'args' => array('string', 'int'),
132dd87735dSAndreas Gohr                'return' => 'array',
133dd87735dSAndreas Gohr                'doc' => 'Returns the available revisions of the page.',
134dd87735dSAndreas Gohr                'name' => 'pageVersions'
135dd87735dSAndreas Gohr            ), 'wiki.putPage' => array(
136dd87735dSAndreas Gohr                'args' => array('string', 'string', 'array'),
137dd87735dSAndreas Gohr                'return' => 'bool',
138dd87735dSAndreas Gohr                'doc' => 'Saves a wiki page.'
139dd87735dSAndreas Gohr            ), 'wiki.listLinks' => array(
140dd87735dSAndreas Gohr                'args' => array('string'),
141dd87735dSAndreas Gohr                'return' => 'array',
142dd87735dSAndreas Gohr                'doc' => 'Lists all links contained in a wiki page.'
143dd87735dSAndreas Gohr            ), 'wiki.getRecentChanges' => array(
144dd87735dSAndreas Gohr                'args' => array('int'),
145dd87735dSAndreas Gohr                'return' => 'array',
14686125ddaSMoisés Braga Ribeiro                'doc' => 'Returns a struct about all recent changes since given timestamp.'
147dd87735dSAndreas Gohr            ), 'wiki.getRecentMediaChanges' => array(
148dd87735dSAndreas Gohr                'args' => array('int'),
149dd87735dSAndreas Gohr                'return' => 'array',
15086125ddaSMoisés Braga Ribeiro                'doc' => 'Returns a struct about all recent media changes since given timestamp.'
151dd87735dSAndreas Gohr            ), 'wiki.aclCheck' => array(
152dd87735dSAndreas Gohr                'args' => array('string', 'string', 'array'),
153dd87735dSAndreas Gohr                'return' => 'int',
154dd87735dSAndreas Gohr                'doc' => 'Returns the permissions of a given wiki page. By default, for current user/groups'
155dd87735dSAndreas Gohr            ), 'wiki.putAttachment' => array(
156dd87735dSAndreas Gohr                'args' => array('string', 'file', 'array'),
157dd87735dSAndreas Gohr                'return' => 'array',
158dd87735dSAndreas Gohr                'doc' => 'Upload a file to the wiki.'
159dd87735dSAndreas Gohr            ), 'wiki.deleteAttachment' => array(
160dd87735dSAndreas Gohr                'args' => array('string'),
161dd87735dSAndreas Gohr                'return' => 'int',
162dd87735dSAndreas Gohr                'doc' => 'Delete a file from the wiki.'
163dd87735dSAndreas Gohr            ), 'wiki.getAttachment' => array(
164dd87735dSAndreas Gohr                'args' => array('string'),
165dd87735dSAndreas Gohr                'doc' => 'Return a media file',
166dd87735dSAndreas Gohr                'return' => 'file',
167dd87735dSAndreas Gohr                'name' => 'getAttachment',
168dd87735dSAndreas Gohr            ), 'wiki.getAttachmentInfo' => array(
169dd87735dSAndreas Gohr                'args' => array('string'),
170dd87735dSAndreas Gohr                'return' => 'array',
171dd87735dSAndreas Gohr                'doc' => 'Returns a struct with info about the attachment.'
172dd87735dSAndreas Gohr            ), 'dokuwiki.getXMLRPCAPIVersion' => array(
173dd87735dSAndreas Gohr                'args' => array(),
174dd87735dSAndreas Gohr                'name' => 'getAPIVersion',
175dd87735dSAndreas Gohr                'return' => 'int',
176dd87735dSAndreas Gohr                'doc' => 'Returns the XMLRPC API version.',
177dd87735dSAndreas Gohr                'public' => '1',
178dd87735dSAndreas Gohr            ), 'wiki.getRPCVersionSupported' => array(
179dd87735dSAndreas Gohr                'args' => array(),
180dd87735dSAndreas Gohr                'name' => 'wikiRpcVersion',
181dd87735dSAndreas Gohr                'return' => 'int',
182dd87735dSAndreas Gohr                'doc' => 'Returns 2 with the supported RPC API version.',
183dd87735dSAndreas Gohr                'public' => '1'
184dd87735dSAndreas Gohr            ),
185dd87735dSAndreas Gohr
186dd87735dSAndreas Gohr        );
187dd87735dSAndreas Gohr    }
188dd87735dSAndreas Gohr
189dd87735dSAndreas Gohr    /**
190dd87735dSAndreas Gohr     * @return string
191dd87735dSAndreas Gohr     */
192dd87735dSAndreas Gohr    public function getVersion()
193dd87735dSAndreas Gohr    {
194dd87735dSAndreas Gohr        return getVersion();
195dd87735dSAndreas Gohr    }
196dd87735dSAndreas Gohr
197dd87735dSAndreas Gohr    /**
198dd87735dSAndreas Gohr     * @return int unix timestamp
199dd87735dSAndreas Gohr     */
200dd87735dSAndreas Gohr    public function getTime()
201dd87735dSAndreas Gohr    {
202dd87735dSAndreas Gohr        return time();
203dd87735dSAndreas Gohr    }
204dd87735dSAndreas Gohr
205dd87735dSAndreas Gohr    /**
206dd87735dSAndreas Gohr     * Return a raw wiki page
207dd87735dSAndreas Gohr     *
208dd87735dSAndreas Gohr     * @param string $id wiki page id
209dd87735dSAndreas Gohr     * @param int|string $rev revision timestamp of the page or empty string
210dd87735dSAndreas Gohr     * @return string page text.
211dd87735dSAndreas Gohr     * @throws AccessDeniedException if no permission for page
212dd87735dSAndreas Gohr     */
213dd87735dSAndreas Gohr    public function rawPage($id, $rev = '')
214dd87735dSAndreas Gohr    {
215dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
216dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
217dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this file', 111);
218dd87735dSAndreas Gohr        }
219dd87735dSAndreas Gohr        $text = rawWiki($id, $rev);
220dd87735dSAndreas Gohr        if (!$text) {
221dd87735dSAndreas Gohr            return pageTemplate($id);
222dd87735dSAndreas Gohr        } else {
223dd87735dSAndreas Gohr            return $text;
224dd87735dSAndreas Gohr        }
225dd87735dSAndreas Gohr    }
226dd87735dSAndreas Gohr
227dd87735dSAndreas Gohr    /**
228dd87735dSAndreas Gohr     * Return a media file
229dd87735dSAndreas Gohr     *
230dd87735dSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
231dd87735dSAndreas Gohr     *
232dd87735dSAndreas Gohr     * @param string $id file id
233dd87735dSAndreas Gohr     * @return mixed media file
234dd87735dSAndreas Gohr     * @throws AccessDeniedException no permission for media
235dd87735dSAndreas Gohr     * @throws RemoteException not exist
236dd87735dSAndreas Gohr     */
237dd87735dSAndreas Gohr    public function getAttachment($id)
238dd87735dSAndreas Gohr    {
239dd87735dSAndreas Gohr        $id = cleanID($id);
240dd87735dSAndreas Gohr        if (auth_quickaclcheck(getNS($id) . ':*') < AUTH_READ) {
241dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this file', 211);
242dd87735dSAndreas Gohr        }
243dd87735dSAndreas Gohr
244dd87735dSAndreas Gohr        $file = mediaFN($id);
245dd87735dSAndreas Gohr        if (!@ file_exists($file)) {
246dd87735dSAndreas Gohr            throw new RemoteException('The requested file does not exist', 221);
247dd87735dSAndreas Gohr        }
248dd87735dSAndreas Gohr
249dd87735dSAndreas Gohr        $data = io_readFile($file, false);
250dd87735dSAndreas Gohr        return $this->api->toFile($data);
251dd87735dSAndreas Gohr    }
252dd87735dSAndreas Gohr
253dd87735dSAndreas Gohr    /**
254dd87735dSAndreas Gohr     * Return info about a media file
255dd87735dSAndreas Gohr     *
256dd87735dSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
257dd87735dSAndreas Gohr     *
258dd87735dSAndreas Gohr     * @param string $id page id
259dd87735dSAndreas Gohr     * @return array
260dd87735dSAndreas Gohr     */
261dd87735dSAndreas Gohr    public function getAttachmentInfo($id)
262dd87735dSAndreas Gohr    {
263dd87735dSAndreas Gohr        $id = cleanID($id);
264dd87735dSAndreas Gohr        $info = array(
265dd87735dSAndreas Gohr            'lastModified' => $this->api->toDate(0),
266dd87735dSAndreas Gohr            'size' => 0,
267dd87735dSAndreas Gohr        );
268dd87735dSAndreas Gohr
269dd87735dSAndreas Gohr        $file = mediaFN($id);
270dd87735dSAndreas Gohr        if (auth_quickaclcheck(getNS($id) . ':*') >= AUTH_READ) {
271dd87735dSAndreas Gohr            if (file_exists($file)) {
272dd87735dSAndreas Gohr                $info['lastModified'] = $this->api->toDate(filemtime($file));
273dd87735dSAndreas Gohr                $info['size'] = filesize($file);
274dd87735dSAndreas Gohr            } else {
275dd87735dSAndreas Gohr                //Is it deleted media with changelog?
276dd87735dSAndreas Gohr                $medialog = new MediaChangeLog($id);
277dd87735dSAndreas Gohr                $revisions = $medialog->getRevisions(0, 1);
278dd87735dSAndreas Gohr                if (!empty($revisions)) {
279dd87735dSAndreas Gohr                    $info['lastModified'] = $this->api->toDate($revisions[0]);
280dd87735dSAndreas Gohr                }
281dd87735dSAndreas Gohr            }
282dd87735dSAndreas Gohr        }
283dd87735dSAndreas Gohr
284dd87735dSAndreas Gohr        return $info;
285dd87735dSAndreas Gohr    }
286dd87735dSAndreas Gohr
287dd87735dSAndreas Gohr    /**
288dd87735dSAndreas Gohr     * Return a wiki page rendered to html
289dd87735dSAndreas Gohr     *
290dd87735dSAndreas Gohr     * @param string $id page id
291dd87735dSAndreas Gohr     * @param string|int $rev revision timestamp or empty string
292dd87735dSAndreas Gohr     * @return null|string html
293dd87735dSAndreas Gohr     * @throws AccessDeniedException no access to page
294dd87735dSAndreas Gohr     */
295dd87735dSAndreas Gohr    public function htmlPage($id, $rev = '')
296dd87735dSAndreas Gohr    {
297dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
298dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
299dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
300dd87735dSAndreas Gohr        }
301dd87735dSAndreas Gohr        return p_wiki_xhtml($id, $rev, false);
302dd87735dSAndreas Gohr    }
303dd87735dSAndreas Gohr
304dd87735dSAndreas Gohr    /**
305dd87735dSAndreas Gohr     * List all pages - we use the indexer list here
306dd87735dSAndreas Gohr     *
307dd87735dSAndreas Gohr     * @return array
308dd87735dSAndreas Gohr     */
309dd87735dSAndreas Gohr    public function listPages()
310dd87735dSAndreas Gohr    {
311dd87735dSAndreas Gohr        $list = array();
312dd87735dSAndreas Gohr        $pages = idx_get_indexer()->getPages();
313dd87735dSAndreas Gohr        $pages = array_filter(array_filter($pages, 'isVisiblePage'), 'page_exists');
3142d85e841SAndreas Gohr        Sort::ksort($pages);
315dd87735dSAndreas Gohr
316dd87735dSAndreas Gohr        foreach (array_keys($pages) as $idx) {
317dd87735dSAndreas Gohr            $perm = auth_quickaclcheck($pages[$idx]);
318dd87735dSAndreas Gohr            if ($perm < AUTH_READ) {
319dd87735dSAndreas Gohr                continue;
320dd87735dSAndreas Gohr            }
321dd87735dSAndreas Gohr            $page = array();
322dd87735dSAndreas Gohr            $page['id'] = trim($pages[$idx]);
323dd87735dSAndreas Gohr            $page['perms'] = $perm;
324dd87735dSAndreas Gohr            $page['size'] = @filesize(wikiFN($pages[$idx]));
325dd87735dSAndreas Gohr            $page['lastModified'] = $this->api->toDate(@filemtime(wikiFN($pages[$idx])));
326dd87735dSAndreas Gohr            $list[] = $page;
327dd87735dSAndreas Gohr        }
328dd87735dSAndreas Gohr
329dd87735dSAndreas Gohr        return $list;
330dd87735dSAndreas Gohr    }
331dd87735dSAndreas Gohr
332dd87735dSAndreas Gohr    /**
333dd87735dSAndreas Gohr     * List all pages in the given namespace (and below)
334dd87735dSAndreas Gohr     *
335dd87735dSAndreas Gohr     * @param string $ns
336dd87735dSAndreas Gohr     * @param array $opts
337dd87735dSAndreas Gohr     *    $opts['depth']   recursion level, 0 for all
338dd87735dSAndreas Gohr     *    $opts['hash']    do md5 sum of content?
339dd87735dSAndreas Gohr     * @return array
340dd87735dSAndreas Gohr     */
341a9284ce8SPhy    public function readNamespace($ns, $opts = array())
342dd87735dSAndreas Gohr    {
343dd87735dSAndreas Gohr        global $conf;
344dd87735dSAndreas Gohr
345dd87735dSAndreas Gohr        if (!is_array($opts)) $opts = array();
346dd87735dSAndreas Gohr
347dd87735dSAndreas Gohr        $ns = cleanID($ns);
348dd87735dSAndreas Gohr        $dir = utf8_encodeFN(str_replace(':', '/', $ns));
349dd87735dSAndreas Gohr        $data = array();
350dd87735dSAndreas Gohr        $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
351dd87735dSAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
352dd87735dSAndreas Gohr        return $data;
353dd87735dSAndreas Gohr    }
354dd87735dSAndreas Gohr
355dd87735dSAndreas Gohr    /**
356dd87735dSAndreas Gohr     * List all pages in the given namespace (and below)
357dd87735dSAndreas Gohr     *
358dd87735dSAndreas Gohr     * @param string $query
359dd87735dSAndreas Gohr     * @return array
360dd87735dSAndreas Gohr     */
361dd87735dSAndreas Gohr    public function search($query)
362dd87735dSAndreas Gohr    {
363dd87735dSAndreas Gohr        $regex = array();
364dd87735dSAndreas Gohr        $data = ft_pageSearch($query, $regex);
365dd87735dSAndreas Gohr        $pages = array();
366dd87735dSAndreas Gohr
367dd87735dSAndreas Gohr        // prepare additional data
368dd87735dSAndreas Gohr        $idx = 0;
369dd87735dSAndreas Gohr        foreach ($data as $id => $score) {
370dd87735dSAndreas Gohr            $file = wikiFN($id);
371dd87735dSAndreas Gohr
372dd87735dSAndreas Gohr            if ($idx < FT_SNIPPET_NUMBER) {
373dd87735dSAndreas Gohr                $snippet = ft_snippet($id, $regex);
374dd87735dSAndreas Gohr                $idx++;
375dd87735dSAndreas Gohr            } else {
376dd87735dSAndreas Gohr                $snippet = '';
377dd87735dSAndreas Gohr            }
378dd87735dSAndreas Gohr
379dd87735dSAndreas Gohr            $pages[] = array(
380dd87735dSAndreas Gohr                'id' => $id,
381dd87735dSAndreas Gohr                'score' => intval($score),
382dd87735dSAndreas Gohr                'rev' => filemtime($file),
383dd87735dSAndreas Gohr                'mtime' => filemtime($file),
384dd87735dSAndreas Gohr                'size' => filesize($file),
385dd87735dSAndreas Gohr                'snippet' => $snippet,
386dd87735dSAndreas Gohr                'title' => useHeading('navigation') ? p_get_first_heading($id) : $id
387dd87735dSAndreas Gohr            );
388dd87735dSAndreas Gohr        }
389dd87735dSAndreas Gohr        return $pages;
390dd87735dSAndreas Gohr    }
391dd87735dSAndreas Gohr
392dd87735dSAndreas Gohr    /**
393dd87735dSAndreas Gohr     * Returns the wiki title.
394dd87735dSAndreas Gohr     *
395dd87735dSAndreas Gohr     * @return string
396dd87735dSAndreas Gohr     */
397dd87735dSAndreas Gohr    public function getTitle()
398dd87735dSAndreas Gohr    {
399dd87735dSAndreas Gohr        global $conf;
400dd87735dSAndreas Gohr        return $conf['title'];
401dd87735dSAndreas Gohr    }
402dd87735dSAndreas Gohr
403dd87735dSAndreas Gohr    /**
404dd87735dSAndreas Gohr     * List all media files.
405dd87735dSAndreas Gohr     *
406dd87735dSAndreas Gohr     * Available options are 'recursive' for also including the subnamespaces
407dd87735dSAndreas Gohr     * in the listing, and 'pattern' for filtering the returned files against
408dd87735dSAndreas Gohr     * a regular expression matching their name.
409dd87735dSAndreas Gohr     *
410dd87735dSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
411dd87735dSAndreas Gohr     *
412dd87735dSAndreas Gohr     * @param string $ns
413dd87735dSAndreas Gohr     * @param array $options
414dd87735dSAndreas Gohr     *   $options['depth']     recursion level, 0 for all
415dd87735dSAndreas Gohr     *   $options['showmsg']   shows message if invalid media id is used
416dd87735dSAndreas Gohr     *   $options['pattern']   check given pattern
417dd87735dSAndreas Gohr     *   $options['hash']      add hashes to result list
418dd87735dSAndreas Gohr     * @return array
419dd87735dSAndreas Gohr     * @throws AccessDeniedException no access to the media files
420dd87735dSAndreas Gohr     */
421dd87735dSAndreas Gohr    public function listAttachments($ns, $options = array())
422dd87735dSAndreas Gohr    {
423dd87735dSAndreas Gohr        global $conf;
424dd87735dSAndreas Gohr
425dd87735dSAndreas Gohr        $ns = cleanID($ns);
426dd87735dSAndreas Gohr
427dd87735dSAndreas Gohr        if (!is_array($options)) $options = array();
428dd87735dSAndreas Gohr        $options['skipacl'] = 0; // no ACL skipping for XMLRPC
429dd87735dSAndreas Gohr
430dd87735dSAndreas Gohr        if (auth_quickaclcheck($ns . ':*') >= AUTH_READ) {
431dd87735dSAndreas Gohr            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
432dd87735dSAndreas Gohr
433dd87735dSAndreas Gohr            $data = array();
434dd87735dSAndreas Gohr            search($data, $conf['mediadir'], 'search_media', $options, $dir);
435dd87735dSAndreas Gohr            $len = count($data);
436dd87735dSAndreas Gohr            if (!$len) return array();
437dd87735dSAndreas Gohr
438dd87735dSAndreas Gohr            for ($i = 0; $i < $len; $i++) {
439dd87735dSAndreas Gohr                unset($data[$i]['meta']);
440dd87735dSAndreas Gohr                $data[$i]['perms'] = $data[$i]['perm'];
441dd87735dSAndreas Gohr                unset($data[$i]['perm']);
442dd87735dSAndreas Gohr                $data[$i]['lastModified'] = $this->api->toDate($data[$i]['mtime']);
443dd87735dSAndreas Gohr            }
444dd87735dSAndreas Gohr            return $data;
445dd87735dSAndreas Gohr        } else {
446dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to list media files.', 215);
447dd87735dSAndreas Gohr        }
448dd87735dSAndreas Gohr    }
449dd87735dSAndreas Gohr
450dd87735dSAndreas Gohr    /**
451dd87735dSAndreas Gohr     * Return a list of backlinks
452dd87735dSAndreas Gohr     *
453dd87735dSAndreas Gohr     * @param string $id page id
454dd87735dSAndreas Gohr     * @return array
455dd87735dSAndreas Gohr     */
456dd87735dSAndreas Gohr    public function listBackLinks($id)
457dd87735dSAndreas Gohr    {
458dd87735dSAndreas Gohr        return ft_backlinks($this->resolvePageId($id));
459dd87735dSAndreas Gohr    }
460dd87735dSAndreas Gohr
461dd87735dSAndreas Gohr    /**
462dd87735dSAndreas Gohr     * Return some basic data about a page
463dd87735dSAndreas Gohr     *
464dd87735dSAndreas Gohr     * @param string $id page id
465dd87735dSAndreas Gohr     * @param string|int $rev revision timestamp or empty string
466dd87735dSAndreas Gohr     * @return array
467dd87735dSAndreas Gohr     * @throws AccessDeniedException no access for page
468dd87735dSAndreas Gohr     * @throws RemoteException page not exist
469dd87735dSAndreas Gohr     */
470dd87735dSAndreas Gohr    public function pageInfo($id, $rev = '')
471dd87735dSAndreas Gohr    {
472dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
473dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
474dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
475dd87735dSAndreas Gohr        }
476dd87735dSAndreas Gohr        $file = wikiFN($id, $rev);
477dd87735dSAndreas Gohr        $time = @filemtime($file);
478dd87735dSAndreas Gohr        if (!$time) {
479dd87735dSAndreas Gohr            throw new RemoteException('The requested page does not exist', 121);
480dd87735dSAndreas Gohr        }
481dd87735dSAndreas Gohr
482dd87735dSAndreas Gohr        // set revision to current version if empty, use revision otherwise
483dd87735dSAndreas Gohr        // as the timestamps of old files are not necessarily correct
484dd87735dSAndreas Gohr        if ($rev === '') {
485dd87735dSAndreas Gohr            $rev = $time;
486dd87735dSAndreas Gohr        }
487dd87735dSAndreas Gohr
488dd87735dSAndreas Gohr        $pagelog = new PageChangeLog($id, 1024);
489dd87735dSAndreas Gohr        $info = $pagelog->getRevisionInfo($rev);
490dd87735dSAndreas Gohr
491dd87735dSAndreas Gohr        $data = array(
492dd87735dSAndreas Gohr            'name' => $id,
493dd87735dSAndreas Gohr            'lastModified' => $this->api->toDate($rev),
4940a444b5aSPhy            'author' => is_array($info) ? (($info['user']) ? $info['user'] : $info['ip']) : null,
495dd87735dSAndreas Gohr            'version' => $rev
496dd87735dSAndreas Gohr        );
497dd87735dSAndreas Gohr
498dd87735dSAndreas Gohr        return ($data);
499dd87735dSAndreas Gohr    }
500dd87735dSAndreas Gohr
501dd87735dSAndreas Gohr    /**
502dd87735dSAndreas Gohr     * Save a wiki page
503dd87735dSAndreas Gohr     *
504dd87735dSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
505dd87735dSAndreas Gohr     *
506dd87735dSAndreas Gohr     * @param string $id page id
507dd87735dSAndreas Gohr     * @param string $text wiki text
508dd87735dSAndreas Gohr     * @param array $params parameters: summary, minor edit
509dd87735dSAndreas Gohr     * @return bool
510dd87735dSAndreas Gohr     * @throws AccessDeniedException no write access for page
511dd87735dSAndreas Gohr     * @throws RemoteException no id, empty new page or locked
512dd87735dSAndreas Gohr     */
513a9284ce8SPhy    public function putPage($id, $text, $params = array())
514dd87735dSAndreas Gohr    {
515dd87735dSAndreas Gohr        global $TEXT;
516dd87735dSAndreas Gohr        global $lang;
517dd87735dSAndreas Gohr
518dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
519dd87735dSAndreas Gohr        $TEXT = cleanText($text);
520dd87735dSAndreas Gohr        $sum = $params['sum'];
521dd87735dSAndreas Gohr        $minor = $params['minor'];
522dd87735dSAndreas Gohr
523dd87735dSAndreas Gohr        if (empty($id)) {
524dd87735dSAndreas Gohr            throw new RemoteException('Empty page ID', 131);
525dd87735dSAndreas Gohr        }
526dd87735dSAndreas Gohr
527dd87735dSAndreas Gohr        if (!page_exists($id) && trim($TEXT) == '') {
528dd87735dSAndreas Gohr            throw new RemoteException('Refusing to write an empty new wiki page', 132);
529dd87735dSAndreas Gohr        }
530dd87735dSAndreas Gohr
531dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_EDIT) {
532dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to edit this page', 112);
533dd87735dSAndreas Gohr        }
534dd87735dSAndreas Gohr
535dd87735dSAndreas Gohr        // Check, if page is locked
536dd87735dSAndreas Gohr        if (checklock($id)) {
537dd87735dSAndreas Gohr            throw new RemoteException('The page is currently locked', 133);
538dd87735dSAndreas Gohr        }
539dd87735dSAndreas Gohr
540dd87735dSAndreas Gohr        // SPAM check
541dd87735dSAndreas Gohr        if (checkwordblock()) {
542dd87735dSAndreas Gohr            throw new RemoteException('Positive wordblock check', 134);
543dd87735dSAndreas Gohr        }
544dd87735dSAndreas Gohr
545dd87735dSAndreas Gohr        // autoset summary on new pages
546dd87735dSAndreas Gohr        if (!page_exists($id) && empty($sum)) {
547dd87735dSAndreas Gohr            $sum = $lang['created'];
548dd87735dSAndreas Gohr        }
549dd87735dSAndreas Gohr
550dd87735dSAndreas Gohr        // autoset summary on deleted pages
551dd87735dSAndreas Gohr        if (page_exists($id) && empty($TEXT) && empty($sum)) {
552dd87735dSAndreas Gohr            $sum = $lang['deleted'];
553dd87735dSAndreas Gohr        }
554dd87735dSAndreas Gohr
555dd87735dSAndreas Gohr        lock($id);
556dd87735dSAndreas Gohr
557dd87735dSAndreas Gohr        saveWikiText($id, $TEXT, $sum, $minor);
558dd87735dSAndreas Gohr
559dd87735dSAndreas Gohr        unlock($id);
560dd87735dSAndreas Gohr
561dd87735dSAndreas Gohr        // run the indexer if page wasn't indexed yet
562dd87735dSAndreas Gohr        idx_addPage($id);
563dd87735dSAndreas Gohr
564dd87735dSAndreas Gohr        return true;
565dd87735dSAndreas Gohr    }
566dd87735dSAndreas Gohr
567dd87735dSAndreas Gohr    /**
568dd87735dSAndreas Gohr     * Appends text to a wiki page.
569dd87735dSAndreas Gohr     *
570dd87735dSAndreas Gohr     * @param string $id page id
571dd87735dSAndreas Gohr     * @param string $text wiki text
572dd87735dSAndreas Gohr     * @param array $params such as summary,minor
573dd87735dSAndreas Gohr     * @return bool|string
574dd87735dSAndreas Gohr     * @throws RemoteException
575dd87735dSAndreas Gohr     */
576a9284ce8SPhy    public function appendPage($id, $text, $params = array())
577dd87735dSAndreas Gohr    {
578dd87735dSAndreas Gohr        $currentpage = $this->rawPage($id);
579dd87735dSAndreas Gohr        if (!is_string($currentpage)) {
580dd87735dSAndreas Gohr            return $currentpage;
581dd87735dSAndreas Gohr        }
582dd87735dSAndreas Gohr        return $this->putPage($id, $currentpage . $text, $params);
583dd87735dSAndreas Gohr    }
584dd87735dSAndreas Gohr
585dd87735dSAndreas Gohr    /**
5868eb28c6dSAndreas Gohr     * Remove one or more users from the list of registered users
5878eb28c6dSAndreas Gohr     *
5888eb28c6dSAndreas Gohr     * @param string[] $usernames List of usernames to remove
5898eb28c6dSAndreas Gohr     *
5908eb28c6dSAndreas Gohr     * @return bool
5918eb28c6dSAndreas Gohr     *
5928eb28c6dSAndreas Gohr     * @throws AccessDeniedException
5938eb28c6dSAndreas Gohr     */
5948eb28c6dSAndreas Gohr    public function deleteUsers($usernames)
5958eb28c6dSAndreas Gohr    {
5968eb28c6dSAndreas Gohr        if (!auth_isadmin()) {
5978eb28c6dSAndreas Gohr            throw new AccessDeniedException('Only admins are allowed to delete users', 114);
5988eb28c6dSAndreas Gohr        }
5992a93a6adSAndreas Gohr        /** @var \dokuwiki\Extension\AuthPlugin $auth */
6008eb28c6dSAndreas Gohr        global $auth;
6019ddafcedSAndreas Gohr        return (bool)$auth->triggerUserMod('delete', array($usernames));
6028eb28c6dSAndreas Gohr    }
6038eb28c6dSAndreas Gohr
6048eb28c6dSAndreas Gohr    /**
605dd87735dSAndreas Gohr     * Uploads a file to the wiki.
606dd87735dSAndreas Gohr     *
607dd87735dSAndreas Gohr     * Michael Klier <chi@chimeric.de>
608dd87735dSAndreas Gohr     *
609dd87735dSAndreas Gohr     * @param string $id page id
610dd87735dSAndreas Gohr     * @param string $file
611dd87735dSAndreas Gohr     * @param array $params such as overwrite
612dd87735dSAndreas Gohr     * @return false|string
613dd87735dSAndreas Gohr     * @throws RemoteException
614dd87735dSAndreas Gohr     */
615a9284ce8SPhy    public function putAttachment($id, $file, $params = array())
616dd87735dSAndreas Gohr    {
617dd87735dSAndreas Gohr        $id = cleanID($id);
618dd87735dSAndreas Gohr        $auth = auth_quickaclcheck(getNS($id) . ':*');
619dd87735dSAndreas Gohr
620dd87735dSAndreas Gohr        if (!isset($id)) {
621dd87735dSAndreas Gohr            throw new RemoteException('Filename not given.', 231);
622dd87735dSAndreas Gohr        }
623dd87735dSAndreas Gohr
624dd87735dSAndreas Gohr        global $conf;
625dd87735dSAndreas Gohr
626dd87735dSAndreas Gohr        $ftmp = $conf['tmpdir'] . '/' . md5($id . clientIP());
627dd87735dSAndreas Gohr
628dd87735dSAndreas Gohr        // save temporary file
629dd87735dSAndreas Gohr        @unlink($ftmp);
630dd87735dSAndreas Gohr        io_saveFile($ftmp, $file);
631dd87735dSAndreas Gohr
632dd87735dSAndreas Gohr        $res = media_save(array('name' => $ftmp), $id, $params['ow'], $auth, 'rename');
633dd87735dSAndreas Gohr        if (is_array($res)) {
634dd87735dSAndreas Gohr            throw new RemoteException($res[0], -$res[1]);
635dd87735dSAndreas Gohr        } else {
636dd87735dSAndreas Gohr            return $res;
637dd87735dSAndreas Gohr        }
638dd87735dSAndreas Gohr    }
639dd87735dSAndreas Gohr
640dd87735dSAndreas Gohr    /**
641dd87735dSAndreas Gohr     * Deletes a file from the wiki.
642dd87735dSAndreas Gohr     *
643dd87735dSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
644dd87735dSAndreas Gohr     *
645dd87735dSAndreas Gohr     * @param string $id page id
646dd87735dSAndreas Gohr     * @return int
647dd87735dSAndreas Gohr     * @throws AccessDeniedException no permissions
648dd87735dSAndreas Gohr     * @throws RemoteException file in use or not deleted
649dd87735dSAndreas Gohr     */
650dd87735dSAndreas Gohr    public function deleteAttachment($id)
651dd87735dSAndreas Gohr    {
652dd87735dSAndreas Gohr        $id = cleanID($id);
653dd87735dSAndreas Gohr        $auth = auth_quickaclcheck(getNS($id) . ':*');
654dd87735dSAndreas Gohr        $res = media_delete($id, $auth);
655dd87735dSAndreas Gohr        if ($res & DOKU_MEDIA_DELETED) {
656dd87735dSAndreas Gohr            return 0;
657dd87735dSAndreas Gohr        } elseif ($res & DOKU_MEDIA_NOT_AUTH) {
658dd87735dSAndreas Gohr            throw new AccessDeniedException('You don\'t have permissions to delete files.', 212);
659dd87735dSAndreas Gohr        } elseif ($res & DOKU_MEDIA_INUSE) {
660dd87735dSAndreas Gohr            throw new RemoteException('File is still referenced', 232);
661dd87735dSAndreas Gohr        } else {
662dd87735dSAndreas Gohr            throw new RemoteException('Could not delete file', 233);
663dd87735dSAndreas Gohr        }
664dd87735dSAndreas Gohr    }
665dd87735dSAndreas Gohr
666dd87735dSAndreas Gohr    /**
667dd87735dSAndreas Gohr     * Returns the permissions of a given wiki page for the current user or another user
668dd87735dSAndreas Gohr     *
669dd87735dSAndreas Gohr     * @param string $id page id
670dd87735dSAndreas Gohr     * @param string|null $user username
671dd87735dSAndreas Gohr     * @param array|null $groups array of groups
672dd87735dSAndreas Gohr     * @return int permission level
673dd87735dSAndreas Gohr     */
674dd87735dSAndreas Gohr    public function aclCheck($id, $user = null, $groups = null)
675dd87735dSAndreas Gohr    {
6762a93a6adSAndreas Gohr        /** @var \dokuwiki\Extension\AuthPlugin $auth */
677dd87735dSAndreas Gohr        global $auth;
678dd87735dSAndreas Gohr
679dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
680dd87735dSAndreas Gohr        if ($user === null) {
681dd87735dSAndreas Gohr            return auth_quickaclcheck($id);
682dd87735dSAndreas Gohr        } else {
683dd87735dSAndreas Gohr            if ($groups === null) {
684dd87735dSAndreas Gohr                $userinfo = $auth->getUserData($user);
685dd87735dSAndreas Gohr                if ($userinfo === false) {
686dd87735dSAndreas Gohr                    $groups = array();
687dd87735dSAndreas Gohr                } else {
688dd87735dSAndreas Gohr                    $groups = $userinfo['grps'];
689dd87735dSAndreas Gohr                }
690dd87735dSAndreas Gohr            }
691dd87735dSAndreas Gohr            return auth_aclcheck($id, $user, $groups);
692dd87735dSAndreas Gohr        }
693dd87735dSAndreas Gohr    }
694dd87735dSAndreas Gohr
695dd87735dSAndreas Gohr    /**
696dd87735dSAndreas Gohr     * Lists all links contained in a wiki page
697dd87735dSAndreas Gohr     *
698dd87735dSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
699dd87735dSAndreas Gohr     *
700dd87735dSAndreas Gohr     * @param string $id page id
701dd87735dSAndreas Gohr     * @return array
702dd87735dSAndreas Gohr     * @throws AccessDeniedException  no read access for page
703dd87735dSAndreas Gohr     */
704dd87735dSAndreas Gohr    public function listLinks($id)
705dd87735dSAndreas Gohr    {
706dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
707dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
708dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
709dd87735dSAndreas Gohr        }
710dd87735dSAndreas Gohr        $links = array();
711dd87735dSAndreas Gohr
712dd87735dSAndreas Gohr        // resolve page instructions
713dd87735dSAndreas Gohr        $ins = p_cached_instructions(wikiFN($id));
714dd87735dSAndreas Gohr
715dd87735dSAndreas Gohr        // instantiate new Renderer - needed for interwiki links
716dd87735dSAndreas Gohr        $Renderer = new Doku_Renderer_xhtml();
717dd87735dSAndreas Gohr        $Renderer->interwiki = getInterwiki();
718dd87735dSAndreas Gohr
719dd87735dSAndreas Gohr        // parse parse instructions
720dd87735dSAndreas Gohr        foreach ($ins as $in) {
721dd87735dSAndreas Gohr            $link = array();
722dd87735dSAndreas Gohr            switch ($in[0]) {
723dd87735dSAndreas Gohr                case 'internallink':
724dd87735dSAndreas Gohr                    $link['type'] = 'local';
725dd87735dSAndreas Gohr                    $link['page'] = $in[1][0];
726dd87735dSAndreas Gohr                    $link['href'] = wl($in[1][0]);
727dd87735dSAndreas Gohr                    array_push($links, $link);
728dd87735dSAndreas Gohr                    break;
729dd87735dSAndreas Gohr                case 'externallink':
730dd87735dSAndreas Gohr                    $link['type'] = 'extern';
731dd87735dSAndreas Gohr                    $link['page'] = $in[1][0];
732dd87735dSAndreas Gohr                    $link['href'] = $in[1][0];
733dd87735dSAndreas Gohr                    array_push($links, $link);
734dd87735dSAndreas Gohr                    break;
735dd87735dSAndreas Gohr                case 'interwikilink':
736dd87735dSAndreas Gohr                    $url = $Renderer->_resolveInterWiki($in[1][2], $in[1][3]);
737dd87735dSAndreas Gohr                    $link['type'] = 'extern';
738dd87735dSAndreas Gohr                    $link['page'] = $url;
739dd87735dSAndreas Gohr                    $link['href'] = $url;
740dd87735dSAndreas Gohr                    array_push($links, $link);
741dd87735dSAndreas Gohr                    break;
742dd87735dSAndreas Gohr            }
743dd87735dSAndreas Gohr        }
744dd87735dSAndreas Gohr
745dd87735dSAndreas Gohr        return ($links);
746dd87735dSAndreas Gohr    }
747dd87735dSAndreas Gohr
748dd87735dSAndreas Gohr    /**
749dd87735dSAndreas Gohr     * Returns a list of recent changes since give timestamp
750dd87735dSAndreas Gohr     *
751dd87735dSAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
752dd87735dSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
753dd87735dSAndreas Gohr     *
754dd87735dSAndreas Gohr     * @param int $timestamp unix timestamp
755dd87735dSAndreas Gohr     * @return array
756dd87735dSAndreas Gohr     * @throws RemoteException no valid timestamp
757dd87735dSAndreas Gohr     */
758dd87735dSAndreas Gohr    public function getRecentChanges($timestamp)
759dd87735dSAndreas Gohr    {
760dd87735dSAndreas Gohr        if (strlen($timestamp) != 10) {
761dd87735dSAndreas Gohr            throw new RemoteException('The provided value is not a valid timestamp', 311);
762dd87735dSAndreas Gohr        }
763dd87735dSAndreas Gohr
764dd87735dSAndreas Gohr        $recents = getRecentsSince($timestamp);
765dd87735dSAndreas Gohr
766dd87735dSAndreas Gohr        $changes = array();
767dd87735dSAndreas Gohr
768dd87735dSAndreas Gohr        foreach ($recents as $recent) {
769dd87735dSAndreas Gohr            $change = array();
770dd87735dSAndreas Gohr            $change['name'] = $recent['id'];
771dd87735dSAndreas Gohr            $change['lastModified'] = $this->api->toDate($recent['date']);
772dd87735dSAndreas Gohr            $change['author'] = $recent['user'];
773dd87735dSAndreas Gohr            $change['version'] = $recent['date'];
774dd87735dSAndreas Gohr            $change['perms'] = $recent['perms'];
775dd87735dSAndreas Gohr            $change['size'] = @filesize(wikiFN($recent['id']));
776dd87735dSAndreas Gohr            array_push($changes, $change);
777dd87735dSAndreas Gohr        }
778dd87735dSAndreas Gohr
779dd87735dSAndreas Gohr        if (!empty($changes)) {
780dd87735dSAndreas Gohr            return $changes;
781dd87735dSAndreas Gohr        } else {
782dd87735dSAndreas Gohr            // in case we still have nothing at this point
783dd87735dSAndreas Gohr            throw new RemoteException('There are no changes in the specified timeframe', 321);
784dd87735dSAndreas Gohr        }
785dd87735dSAndreas Gohr    }
786dd87735dSAndreas Gohr
787dd87735dSAndreas Gohr    /**
788dd87735dSAndreas Gohr     * Returns a list of recent media changes since give timestamp
789dd87735dSAndreas Gohr     *
790dd87735dSAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
791dd87735dSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
792dd87735dSAndreas Gohr     *
793dd87735dSAndreas Gohr     * @param int $timestamp unix timestamp
794dd87735dSAndreas Gohr     * @return array
795dd87735dSAndreas Gohr     * @throws RemoteException no valid timestamp
796dd87735dSAndreas Gohr     */
797dd87735dSAndreas Gohr    public function getRecentMediaChanges($timestamp)
798dd87735dSAndreas Gohr    {
799dd87735dSAndreas Gohr        if (strlen($timestamp) != 10)
800dd87735dSAndreas Gohr            throw new RemoteException('The provided value is not a valid timestamp', 311);
801dd87735dSAndreas Gohr
802dd87735dSAndreas Gohr        $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
803dd87735dSAndreas Gohr
804dd87735dSAndreas Gohr        $changes = array();
805dd87735dSAndreas Gohr
806dd87735dSAndreas Gohr        foreach ($recents as $recent) {
807dd87735dSAndreas Gohr            $change = array();
808dd87735dSAndreas Gohr            $change['name'] = $recent['id'];
809dd87735dSAndreas Gohr            $change['lastModified'] = $this->api->toDate($recent['date']);
810dd87735dSAndreas Gohr            $change['author'] = $recent['user'];
811dd87735dSAndreas Gohr            $change['version'] = $recent['date'];
812dd87735dSAndreas Gohr            $change['perms'] = $recent['perms'];
813dd87735dSAndreas Gohr            $change['size'] = @filesize(mediaFN($recent['id']));
814dd87735dSAndreas Gohr            array_push($changes, $change);
815dd87735dSAndreas Gohr        }
816dd87735dSAndreas Gohr
817dd87735dSAndreas Gohr        if (!empty($changes)) {
818dd87735dSAndreas Gohr            return $changes;
819dd87735dSAndreas Gohr        } else {
820dd87735dSAndreas Gohr            // in case we still have nothing at this point
821dd87735dSAndreas Gohr            throw new RemoteException('There are no changes in the specified timeframe', 321);
822dd87735dSAndreas Gohr        }
823dd87735dSAndreas Gohr    }
824dd87735dSAndreas Gohr
825dd87735dSAndreas Gohr    /**
826dd87735dSAndreas Gohr     * Returns a list of available revisions of a given wiki page
827dd87735dSAndreas Gohr     * Number of returned pages is set by $conf['recent']
828dd87735dSAndreas Gohr     * However not accessible pages are skipped, so less than $conf['recent'] could be returned
829dd87735dSAndreas Gohr     *
830dd87735dSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
831dd87735dSAndreas Gohr     *
832dd87735dSAndreas Gohr     * @param string $id page id
833dd87735dSAndreas Gohr     * @param int $first skip the first n changelog lines
834dd87735dSAndreas Gohr     *                      0 = from current(if exists)
835dd87735dSAndreas Gohr     *                      1 = from 1st old rev
836dd87735dSAndreas Gohr     *                      2 = from 2nd old rev, etc
837dd87735dSAndreas Gohr     * @return array
838dd87735dSAndreas Gohr     * @throws AccessDeniedException no read access for page
839dd87735dSAndreas Gohr     * @throws RemoteException empty id
840dd87735dSAndreas Gohr     */
841a9284ce8SPhy    public function pageVersions($id, $first = 0)
842dd87735dSAndreas Gohr    {
843dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
844dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
845dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
846dd87735dSAndreas Gohr        }
847dd87735dSAndreas Gohr        global $conf;
848dd87735dSAndreas Gohr
849dd87735dSAndreas Gohr        $versions = array();
850dd87735dSAndreas Gohr
851dd87735dSAndreas Gohr        if (empty($id)) {
852dd87735dSAndreas Gohr            throw new RemoteException('Empty page ID', 131);
853dd87735dSAndreas Gohr        }
854dd87735dSAndreas Gohr
855dd87735dSAndreas Gohr        $first = (int) $first;
856dd87735dSAndreas Gohr        $first_rev = $first - 1;
857dd87735dSAndreas Gohr        $first_rev = $first_rev < 0 ? 0 : $first_rev;
858dd87735dSAndreas Gohr        $pagelog = new PageChangeLog($id);
859dd87735dSAndreas Gohr        $revisions = $pagelog->getRevisions($first_rev, $conf['recent']);
860dd87735dSAndreas Gohr
861dd87735dSAndreas Gohr        if ($first == 0) {
862dd87735dSAndreas Gohr            array_unshift($revisions, '');  // include current revision
863dd87735dSAndreas Gohr            if (count($revisions) > $conf['recent']) {
864dd87735dSAndreas Gohr                array_pop($revisions);          // remove extra log entry
865dd87735dSAndreas Gohr            }
866dd87735dSAndreas Gohr        }
867dd87735dSAndreas Gohr
868dd87735dSAndreas Gohr        if (!empty($revisions)) {
869dd87735dSAndreas Gohr            foreach ($revisions as $rev) {
870dd87735dSAndreas Gohr                $file = wikiFN($id, $rev);
871dd87735dSAndreas Gohr                $time = @filemtime($file);
872dd87735dSAndreas Gohr                // we check if the page actually exists, if this is not the
873dd87735dSAndreas Gohr                // case this can lead to less pages being returned than
874dd87735dSAndreas Gohr                // specified via $conf['recent']
875dd87735dSAndreas Gohr                if ($time) {
876dd87735dSAndreas Gohr                    $pagelog->setChunkSize(1024);
877dd87735dSAndreas Gohr                    $info = $pagelog->getRevisionInfo($rev ? $rev : $time);
878dd87735dSAndreas Gohr                    if (!empty($info)) {
879dd87735dSAndreas Gohr                        $data = array();
880dd87735dSAndreas Gohr                        $data['user'] = $info['user'];
881dd87735dSAndreas Gohr                        $data['ip'] = $info['ip'];
882dd87735dSAndreas Gohr                        $data['type'] = $info['type'];
883dd87735dSAndreas Gohr                        $data['sum'] = $info['sum'];
884dd87735dSAndreas Gohr                        $data['modified'] = $this->api->toDate($info['date']);
885dd87735dSAndreas Gohr                        $data['version'] = $info['date'];
886dd87735dSAndreas Gohr                        array_push($versions, $data);
887dd87735dSAndreas Gohr                    }
888dd87735dSAndreas Gohr                }
889dd87735dSAndreas Gohr            }
890dd87735dSAndreas Gohr            return $versions;
891dd87735dSAndreas Gohr        } else {
892dd87735dSAndreas Gohr            return array();
893dd87735dSAndreas Gohr        }
894dd87735dSAndreas Gohr    }
895dd87735dSAndreas Gohr
896dd87735dSAndreas Gohr    /**
897dd87735dSAndreas Gohr     * The version of Wiki RPC API supported
898dd87735dSAndreas Gohr     */
899dd87735dSAndreas Gohr    public function wikiRpcVersion()
900dd87735dSAndreas Gohr    {
901dd87735dSAndreas Gohr        return 2;
902dd87735dSAndreas Gohr    }
903dd87735dSAndreas Gohr
904dd87735dSAndreas Gohr    /**
905dd87735dSAndreas Gohr     * Locks or unlocks a given batch of pages
906dd87735dSAndreas Gohr     *
907dd87735dSAndreas Gohr     * Give an associative array with two keys: lock and unlock. Both should contain a
908dd87735dSAndreas Gohr     * list of pages to lock or unlock
909dd87735dSAndreas Gohr     *
910dd87735dSAndreas Gohr     * Returns an associative array with the keys locked, lockfail, unlocked and
911dd87735dSAndreas Gohr     * unlockfail, each containing lists of pages.
912dd87735dSAndreas Gohr     *
913dd87735dSAndreas Gohr     * @param array[] $set list pages with array('lock' => array, 'unlock' => array)
914dd87735dSAndreas Gohr     * @return array
915dd87735dSAndreas Gohr     */
916dd87735dSAndreas Gohr    public function setLocks($set)
917dd87735dSAndreas Gohr    {
918dd87735dSAndreas Gohr        $locked = array();
919dd87735dSAndreas Gohr        $lockfail = array();
920dd87735dSAndreas Gohr        $unlocked = array();
921dd87735dSAndreas Gohr        $unlockfail = array();
922dd87735dSAndreas Gohr
923dd87735dSAndreas Gohr        foreach ((array) $set['lock'] as $id) {
924dd87735dSAndreas Gohr            $id = $this->resolvePageId($id);
925dd87735dSAndreas Gohr            if (auth_quickaclcheck($id) < AUTH_EDIT || checklock($id)) {
926dd87735dSAndreas Gohr                $lockfail[] = $id;
927dd87735dSAndreas Gohr            } else {
928dd87735dSAndreas Gohr                lock($id);
929dd87735dSAndreas Gohr                $locked[] = $id;
930dd87735dSAndreas Gohr            }
931dd87735dSAndreas Gohr        }
932dd87735dSAndreas Gohr
933dd87735dSAndreas Gohr        foreach ((array) $set['unlock'] as $id) {
934dd87735dSAndreas Gohr            $id = $this->resolvePageId($id);
935dd87735dSAndreas Gohr            if (auth_quickaclcheck($id) < AUTH_EDIT || !unlock($id)) {
936dd87735dSAndreas Gohr                $unlockfail[] = $id;
937dd87735dSAndreas Gohr            } else {
938dd87735dSAndreas Gohr                $unlocked[] = $id;
939dd87735dSAndreas Gohr            }
940dd87735dSAndreas Gohr        }
941dd87735dSAndreas Gohr
942dd87735dSAndreas Gohr        return array(
943dd87735dSAndreas Gohr            'locked' => $locked,
944dd87735dSAndreas Gohr            'lockfail' => $lockfail,
945dd87735dSAndreas Gohr            'unlocked' => $unlocked,
946dd87735dSAndreas Gohr            'unlockfail' => $unlockfail,
947dd87735dSAndreas Gohr        );
948dd87735dSAndreas Gohr    }
949dd87735dSAndreas Gohr
950dd87735dSAndreas Gohr    /**
951dd87735dSAndreas Gohr     * Return API version
952dd87735dSAndreas Gohr     *
953dd87735dSAndreas Gohr     * @return int
954dd87735dSAndreas Gohr     */
955dd87735dSAndreas Gohr    public function getAPIVersion()
956dd87735dSAndreas Gohr    {
957dd87735dSAndreas Gohr        return self::API_VERSION;
958dd87735dSAndreas Gohr    }
959dd87735dSAndreas Gohr
960dd87735dSAndreas Gohr    /**
961dd87735dSAndreas Gohr     * Login
962dd87735dSAndreas Gohr     *
963dd87735dSAndreas Gohr     * @param string $user
964dd87735dSAndreas Gohr     * @param string $pass
965dd87735dSAndreas Gohr     * @return int
966dd87735dSAndreas Gohr     */
967dd87735dSAndreas Gohr    public function login($user, $pass)
968dd87735dSAndreas Gohr    {
969dd87735dSAndreas Gohr        global $conf;
9702a93a6adSAndreas Gohr        /** @var \dokuwiki\Extension\AuthPlugin $auth */
971dd87735dSAndreas Gohr        global $auth;
972dd87735dSAndreas Gohr
973dd87735dSAndreas Gohr        if (!$conf['useacl']) return 0;
974dd87735dSAndreas Gohr        if (!$auth) return 0;
975dd87735dSAndreas Gohr
976dd87735dSAndreas Gohr        @session_start(); // reopen session for login
97781e99965SPhy        $ok = null;
978dd87735dSAndreas Gohr        if ($auth->canDo('external')) {
979dd87735dSAndreas Gohr            $ok = $auth->trustExternal($user, $pass, false);
98081e99965SPhy        }
98181e99965SPhy        if ($ok === null){
982dd87735dSAndreas Gohr            $evdata = array(
983dd87735dSAndreas Gohr                'user' => $user,
984dd87735dSAndreas Gohr                'password' => $pass,
985dd87735dSAndreas Gohr                'sticky' => false,
986dd87735dSAndreas Gohr                'silent' => true,
987dd87735dSAndreas Gohr            );
988cbb44eabSAndreas Gohr            $ok = Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper');
989dd87735dSAndreas Gohr        }
990dd87735dSAndreas Gohr        session_write_close(); // we're done with the session
991dd87735dSAndreas Gohr
992dd87735dSAndreas Gohr        return $ok;
993dd87735dSAndreas Gohr    }
994dd87735dSAndreas Gohr
995dd87735dSAndreas Gohr    /**
996dd87735dSAndreas Gohr     * Log off
997dd87735dSAndreas Gohr     *
998dd87735dSAndreas Gohr     * @return int
999dd87735dSAndreas Gohr     */
1000dd87735dSAndreas Gohr    public function logoff()
1001dd87735dSAndreas Gohr    {
1002dd87735dSAndreas Gohr        global $conf;
1003dd87735dSAndreas Gohr        global $auth;
1004dd87735dSAndreas Gohr        if (!$conf['useacl']) return 0;
1005dd87735dSAndreas Gohr        if (!$auth) return 0;
1006dd87735dSAndreas Gohr
1007dd87735dSAndreas Gohr        auth_logoff();
1008dd87735dSAndreas Gohr
1009dd87735dSAndreas Gohr        return 1;
1010dd87735dSAndreas Gohr    }
1011dd87735dSAndreas Gohr
1012dd87735dSAndreas Gohr    /**
1013dd87735dSAndreas Gohr     * Resolve page id
1014dd87735dSAndreas Gohr     *
1015dd87735dSAndreas Gohr     * @param string $id page id
1016dd87735dSAndreas Gohr     * @return string
1017dd87735dSAndreas Gohr     */
1018dd87735dSAndreas Gohr    private function resolvePageId($id)
1019dd87735dSAndreas Gohr    {
1020dd87735dSAndreas Gohr        $id = cleanID($id);
1021dd87735dSAndreas Gohr        if (empty($id)) {
1022dd87735dSAndreas Gohr            global $conf;
1023dd87735dSAndreas Gohr            $id = cleanID($conf['start']);
1024dd87735dSAndreas Gohr        }
1025dd87735dSAndreas Gohr        return $id;
1026dd87735dSAndreas Gohr    }
1027dd87735dSAndreas Gohr}
1028