xref: /dokuwiki/inc/Remote/ApiCore.php (revision 535851891f27d4edf214975656242cb81be5b6ac)
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;
8104a3b7cSAndreas Gohruse dokuwiki\Extension\AuthPlugin;
9cbb44eabSAndreas Gohruse dokuwiki\Extension\Event;
102d85e841SAndreas Gohruse dokuwiki\Utf8\Sort;
11dd87735dSAndreas Gohr
12dd87735dSAndreas Gohr/**
13dd87735dSAndreas Gohr * Provides the core methods for the remote API.
14dd87735dSAndreas Gohr * The methods are ordered in 'wiki.<method>' and 'dokuwiki.<method>' namespaces
15dd87735dSAndreas Gohr */
16dd87735dSAndreas Gohrclass ApiCore
17dd87735dSAndreas Gohr{
18dd87735dSAndreas Gohr    /** @var int Increased whenever the API is changed */
1974981a4eSAndreas Gohr    public const API_VERSION = 11;
20dd87735dSAndreas Gohr
21dd87735dSAndreas Gohr
22dd87735dSAndreas Gohr    /** @var Api */
23dd87735dSAndreas Gohr    private $api;
24dd87735dSAndreas Gohr
25dd87735dSAndreas Gohr    /**
26dd87735dSAndreas Gohr     * @param Api $api
27dd87735dSAndreas Gohr     */
28dd87735dSAndreas Gohr    public function __construct(Api $api)
29dd87735dSAndreas Gohr    {
30dd87735dSAndreas Gohr        $this->api = $api;
31dd87735dSAndreas Gohr    }
32dd87735dSAndreas Gohr
33dd87735dSAndreas Gohr    /**
34dd87735dSAndreas Gohr     * Returns details about the core methods
35dd87735dSAndreas Gohr     *
36dd87735dSAndreas Gohr     * @return array
37dd87735dSAndreas Gohr     */
38e6a9d76fSSyntaxseed    public function getRemoteInfo()
39dd87735dSAndreas Gohr    {
40104a3b7cSAndreas Gohr        return [
4142e66c7aSAndreas Gohr            'dokuwiki.getVersion' => new ApiCall('getVersion'),
4242e66c7aSAndreas Gohr            'dokuwiki.login' => (new ApiCall([$this, 'login']))
4342e66c7aSAndreas Gohr                ->setPublic(),
4442e66c7aSAndreas Gohr            'dokuwiki.logoff' => new ApiCall([$this, 'logoff']),
4542e66c7aSAndreas Gohr            'dokuwiki.getPagelist' => new ApiCall([$this, 'readNamespace']),
4642e66c7aSAndreas Gohr            'dokuwiki.search' => new ApiCall([$this, 'search']),
4742e66c7aSAndreas Gohr            'dokuwiki.getTime' => (new ApiCall('time'))
4842e66c7aSAndreas Gohr                ->setSummary('Returns the current server time')
4942e66c7aSAndreas Gohr                ->setReturnDescription('unix timestamp'),
5042e66c7aSAndreas Gohr            'dokuwiki.setLocks' => new ApiCall([$this, 'setLocks']),
5142e66c7aSAndreas Gohr            'dokuwiki.getTitle' => (new ApiCall([$this, 'getTitle']))
5242e66c7aSAndreas Gohr                ->setPublic(),
5342e66c7aSAndreas Gohr            'dokuwiki.appendPage' => new ApiCall([$this, 'appendPage']),
5442e66c7aSAndreas Gohr            'dokuwiki.createUser' => new ApiCall([$this, 'createUser']),
5542e66c7aSAndreas Gohr            'dokuwiki.deleteUsers' => new ApiCall([$this, 'deleteUsers']),
5642e66c7aSAndreas Gohr            'wiki.getPage' => (new ApiCall([$this, 'rawPage']))
5742e66c7aSAndreas Gohr                ->limitArgs(['id']),
5842e66c7aSAndreas Gohr            'wiki.getPageVersion' => (new ApiCall([$this, 'rawPage']))
5942e66c7aSAndreas Gohr                ->setSummary('Get a specific revision of a wiki page'),
6042e66c7aSAndreas Gohr            'wiki.getPageHTML' => (new ApiCall([$this, 'htmlPage']))
6142e66c7aSAndreas Gohr                ->limitArgs(['id']),
6242e66c7aSAndreas Gohr            'wiki.getPageHTMLVersion' => (new ApiCall([$this, 'htmlPage']))
6342e66c7aSAndreas Gohr                ->setSummary('Get the HTML for a specific revision of a wiki page'),
6442e66c7aSAndreas Gohr            'wiki.getAllPages' => new ApiCall([$this, 'listPages']),
6542e66c7aSAndreas Gohr            'wiki.getAttachments' => new ApiCall([$this, 'listAttachments']),
6642e66c7aSAndreas Gohr            'wiki.getBackLinks' => new ApiCall([$this, 'listBackLinks']),
6742e66c7aSAndreas Gohr            'wiki.getPageInfo' => (new ApiCall([$this, 'pageInfo']))
6842e66c7aSAndreas Gohr                ->limitArgs(['id']),
6942e66c7aSAndreas Gohr            'wiki.getPageInfoVersion' => (new ApiCall([$this, 'pageInfo']))
7042e66c7aSAndreas Gohr                ->setSummary('Get some basic data about a specific revison of a wiki page'),
7142e66c7aSAndreas Gohr            'wiki.getPageVersions' => new ApiCall([$this, 'pageVersions']),
7242e66c7aSAndreas Gohr            'wiki.putPage' => new ApiCall([$this, 'putPage']),
7342e66c7aSAndreas Gohr            'wiki.listLinks' => new ApiCall([$this, 'listLinks']),
7442e66c7aSAndreas Gohr            'wiki.getRecentChanges' => new ApiCall([$this, 'getRecentChanges']),
7542e66c7aSAndreas Gohr            'wiki.getRecentMediaChanges' => new ApiCall([$this, 'getRecentMediaChanges']),
7642e66c7aSAndreas Gohr            'wiki.aclCheck' => new ApiCall([$this, 'aclCheck']),
7742e66c7aSAndreas Gohr            'wiki.putAttachment' => new ApiCall([$this, 'putAttachment']),
7842e66c7aSAndreas Gohr            'wiki.deleteAttachment' => new ApiCall([$this, 'deleteAttachment']),
7942e66c7aSAndreas Gohr            'wiki.getAttachment' => new ApiCall([$this, 'getAttachment']),
8042e66c7aSAndreas Gohr            'wiki.getAttachmentInfo' => new ApiCall([$this, 'getAttachmentInfo']),
8142e66c7aSAndreas Gohr            'dokuwiki.getXMLRPCAPIVersion' => (new ApiCall([$this, 'getAPIVersion']))->setPublic(),
8242e66c7aSAndreas Gohr            'wiki.getRPCVersionSupported' => (new ApiCall([$this, 'wikiRpcVersion']))->setPublic(),
83104a3b7cSAndreas Gohr        ];
84dd87735dSAndreas Gohr    }
85dd87735dSAndreas Gohr
86dd87735dSAndreas Gohr    /**
87dd87735dSAndreas Gohr     * Return a raw wiki page
88dd87735dSAndreas Gohr     *
89dd87735dSAndreas Gohr     * @param string $id wiki page id
90dd87735dSAndreas Gohr     * @param int|string $rev revision timestamp of the page or empty string
91dd87735dSAndreas Gohr     * @return string page text.
92dd87735dSAndreas Gohr     * @throws AccessDeniedException if no permission for page
93dd87735dSAndreas Gohr     */
94dd87735dSAndreas Gohr    public function rawPage($id, $rev = '')
95dd87735dSAndreas Gohr    {
96dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
97dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
98dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this file', 111);
99dd87735dSAndreas Gohr        }
100dd87735dSAndreas Gohr        $text = rawWiki($id, $rev);
101dd87735dSAndreas Gohr        if (!$text) {
102dd87735dSAndreas Gohr            return pageTemplate($id);
103dd87735dSAndreas Gohr        } else {
104dd87735dSAndreas Gohr            return $text;
105dd87735dSAndreas Gohr        }
106dd87735dSAndreas Gohr    }
107dd87735dSAndreas Gohr
108dd87735dSAndreas Gohr    /**
109dd87735dSAndreas Gohr     * Return a media file
110dd87735dSAndreas Gohr     *
111dd87735dSAndreas Gohr     * @param string $id file id
112dd87735dSAndreas Gohr     * @return mixed media file
113dd87735dSAndreas Gohr     * @throws AccessDeniedException no permission for media
114dd87735dSAndreas Gohr     * @throws RemoteException not exist
115104a3b7cSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
116104a3b7cSAndreas Gohr     *
117dd87735dSAndreas Gohr     */
118dd87735dSAndreas Gohr    public function getAttachment($id)
119dd87735dSAndreas Gohr    {
120dd87735dSAndreas Gohr        $id = cleanID($id);
121dd87735dSAndreas Gohr        if (auth_quickaclcheck(getNS($id) . ':*') < AUTH_READ) {
122dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this file', 211);
123dd87735dSAndreas Gohr        }
124dd87735dSAndreas Gohr
125dd87735dSAndreas Gohr        $file = mediaFN($id);
126dd87735dSAndreas Gohr        if (!@ file_exists($file)) {
127dd87735dSAndreas Gohr            throw new RemoteException('The requested file does not exist', 221);
128dd87735dSAndreas Gohr        }
129dd87735dSAndreas Gohr
130dd87735dSAndreas Gohr        $data = io_readFile($file, false);
131dd87735dSAndreas Gohr        return $this->api->toFile($data);
132dd87735dSAndreas Gohr    }
133dd87735dSAndreas Gohr
134dd87735dSAndreas Gohr    /**
135dd87735dSAndreas Gohr     * Return info about a media file
136dd87735dSAndreas Gohr     *
137dd87735dSAndreas Gohr     * @param string $id page id
138dd87735dSAndreas Gohr     * @return array
139104a3b7cSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
140104a3b7cSAndreas Gohr     *
141dd87735dSAndreas Gohr     */
142dd87735dSAndreas Gohr    public function getAttachmentInfo($id)
143dd87735dSAndreas Gohr    {
144dd87735dSAndreas Gohr        $id = cleanID($id);
145104a3b7cSAndreas Gohr        $info = ['lastModified' => $this->api->toDate(0), 'size' => 0];
146dd87735dSAndreas Gohr
147dd87735dSAndreas Gohr        $file = mediaFN($id);
148dd87735dSAndreas Gohr        if (auth_quickaclcheck(getNS($id) . ':*') >= AUTH_READ) {
149dd87735dSAndreas Gohr            if (file_exists($file)) {
150dd87735dSAndreas Gohr                $info['lastModified'] = $this->api->toDate(filemtime($file));
151dd87735dSAndreas Gohr                $info['size'] = filesize($file);
152dd87735dSAndreas Gohr            } else {
153dd87735dSAndreas Gohr                //Is it deleted media with changelog?
154dd87735dSAndreas Gohr                $medialog = new MediaChangeLog($id);
155dd87735dSAndreas Gohr                $revisions = $medialog->getRevisions(0, 1);
156dd87735dSAndreas Gohr                if (!empty($revisions)) {
157dd87735dSAndreas Gohr                    $info['lastModified'] = $this->api->toDate($revisions[0]);
158dd87735dSAndreas Gohr                }
159dd87735dSAndreas Gohr            }
160dd87735dSAndreas Gohr        }
161dd87735dSAndreas Gohr
162dd87735dSAndreas Gohr        return $info;
163dd87735dSAndreas Gohr    }
164dd87735dSAndreas Gohr
165dd87735dSAndreas Gohr    /**
166dd87735dSAndreas Gohr     * Return a wiki page rendered to html
167dd87735dSAndreas Gohr     *
168dd87735dSAndreas Gohr     * @param string $id page id
169dd87735dSAndreas Gohr     * @param string|int $rev revision timestamp or empty string
170dd87735dSAndreas Gohr     * @return null|string html
171dd87735dSAndreas Gohr     * @throws AccessDeniedException no access to page
172dd87735dSAndreas Gohr     */
173dd87735dSAndreas Gohr    public function htmlPage($id, $rev = '')
174dd87735dSAndreas Gohr    {
175dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
176dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
177dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
178dd87735dSAndreas Gohr        }
179dd87735dSAndreas Gohr        return p_wiki_xhtml($id, $rev, false);
180dd87735dSAndreas Gohr    }
181dd87735dSAndreas Gohr
182dd87735dSAndreas Gohr    /**
183dd87735dSAndreas Gohr     * List all pages - we use the indexer list here
184dd87735dSAndreas Gohr     *
185dd87735dSAndreas Gohr     * @return array
186dd87735dSAndreas Gohr     */
187dd87735dSAndreas Gohr    public function listPages()
188dd87735dSAndreas Gohr    {
189104a3b7cSAndreas Gohr        $list = [];
190dd87735dSAndreas Gohr        $pages = idx_get_indexer()->getPages();
191dd87735dSAndreas Gohr        $pages = array_filter(array_filter($pages, 'isVisiblePage'), 'page_exists');
1922d85e841SAndreas Gohr        Sort::ksort($pages);
193dd87735dSAndreas Gohr
194dd87735dSAndreas Gohr        foreach (array_keys($pages) as $idx) {
195dd87735dSAndreas Gohr            $perm = auth_quickaclcheck($pages[$idx]);
196dd87735dSAndreas Gohr            if ($perm < AUTH_READ) {
197dd87735dSAndreas Gohr                continue;
198dd87735dSAndreas Gohr            }
199104a3b7cSAndreas Gohr            $page = [];
200dd87735dSAndreas Gohr            $page['id'] = trim($pages[$idx]);
201dd87735dSAndreas Gohr            $page['perms'] = $perm;
202dd87735dSAndreas Gohr            $page['size'] = @filesize(wikiFN($pages[$idx]));
203dd87735dSAndreas Gohr            $page['lastModified'] = $this->api->toDate(@filemtime(wikiFN($pages[$idx])));
204dd87735dSAndreas Gohr            $list[] = $page;
205dd87735dSAndreas Gohr        }
206dd87735dSAndreas Gohr
207dd87735dSAndreas Gohr        return $list;
208dd87735dSAndreas Gohr    }
209dd87735dSAndreas Gohr
210dd87735dSAndreas Gohr    /**
211dd87735dSAndreas Gohr     * List all pages in the given namespace (and below)
212dd87735dSAndreas Gohr     *
213dd87735dSAndreas Gohr     * @param string $ns
214dd87735dSAndreas Gohr     * @param array $opts
215dd87735dSAndreas Gohr     *    $opts['depth']   recursion level, 0 for all
216dd87735dSAndreas Gohr     *    $opts['hash']    do md5 sum of content?
217dd87735dSAndreas Gohr     * @return array
218dd87735dSAndreas Gohr     */
219104a3b7cSAndreas Gohr    public function readNamespace($ns, $opts = [])
220dd87735dSAndreas Gohr    {
221dd87735dSAndreas Gohr        global $conf;
222dd87735dSAndreas Gohr
223104a3b7cSAndreas Gohr        if (!is_array($opts)) $opts = [];
224dd87735dSAndreas Gohr
225dd87735dSAndreas Gohr        $ns = cleanID($ns);
226dd87735dSAndreas Gohr        $dir = utf8_encodeFN(str_replace(':', '/', $ns));
227104a3b7cSAndreas Gohr        $data = [];
228dd87735dSAndreas Gohr        $opts['skipacl'] = 0; // no ACL skipping for XMLRPC
229dd87735dSAndreas Gohr        search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
230dd87735dSAndreas Gohr        return $data;
231dd87735dSAndreas Gohr    }
232dd87735dSAndreas Gohr
233dd87735dSAndreas Gohr    /**
234dd87735dSAndreas Gohr     * List all pages in the given namespace (and below)
235dd87735dSAndreas Gohr     *
236dd87735dSAndreas Gohr     * @param string $query
237dd87735dSAndreas Gohr     * @return array
238dd87735dSAndreas Gohr     */
239dd87735dSAndreas Gohr    public function search($query)
240dd87735dSAndreas Gohr    {
241104a3b7cSAndreas Gohr        $regex = [];
242dd87735dSAndreas Gohr        $data = ft_pageSearch($query, $regex);
243104a3b7cSAndreas Gohr        $pages = [];
244dd87735dSAndreas Gohr
245dd87735dSAndreas Gohr        // prepare additional data
246dd87735dSAndreas Gohr        $idx = 0;
247dd87735dSAndreas Gohr        foreach ($data as $id => $score) {
248dd87735dSAndreas Gohr            $file = wikiFN($id);
249dd87735dSAndreas Gohr
250dd87735dSAndreas Gohr            if ($idx < FT_SNIPPET_NUMBER) {
251dd87735dSAndreas Gohr                $snippet = ft_snippet($id, $regex);
252dd87735dSAndreas Gohr                $idx++;
253dd87735dSAndreas Gohr            } else {
254dd87735dSAndreas Gohr                $snippet = '';
255dd87735dSAndreas Gohr            }
256dd87735dSAndreas Gohr
257104a3b7cSAndreas Gohr            $pages[] = [
258dd87735dSAndreas Gohr                'id' => $id,
259104a3b7cSAndreas Gohr                'score' => (int)$score,
260dd87735dSAndreas Gohr                'rev' => filemtime($file),
261dd87735dSAndreas Gohr                'mtime' => filemtime($file),
262dd87735dSAndreas Gohr                'size' => filesize($file),
263dd87735dSAndreas Gohr                'snippet' => $snippet,
264dd87735dSAndreas Gohr                'title' => useHeading('navigation') ? p_get_first_heading($id) : $id
265104a3b7cSAndreas Gohr            ];
266dd87735dSAndreas Gohr        }
267dd87735dSAndreas Gohr        return $pages;
268dd87735dSAndreas Gohr    }
269dd87735dSAndreas Gohr
270dd87735dSAndreas Gohr    /**
271dd87735dSAndreas Gohr     * Returns the wiki title.
272dd87735dSAndreas Gohr     *
273dd87735dSAndreas Gohr     * @return string
274dd87735dSAndreas Gohr     */
275dd87735dSAndreas Gohr    public function getTitle()
276dd87735dSAndreas Gohr    {
277dd87735dSAndreas Gohr        global $conf;
278dd87735dSAndreas Gohr        return $conf['title'];
279dd87735dSAndreas Gohr    }
280dd87735dSAndreas Gohr
281dd87735dSAndreas Gohr    /**
282dd87735dSAndreas Gohr     * List all media files.
283dd87735dSAndreas Gohr     *
284dd87735dSAndreas Gohr     * Available options are 'recursive' for also including the subnamespaces
285dd87735dSAndreas Gohr     * in the listing, and 'pattern' for filtering the returned files against
286dd87735dSAndreas Gohr     * a regular expression matching their name.
287dd87735dSAndreas Gohr     *
288dd87735dSAndreas Gohr     * @param string $ns
289dd87735dSAndreas Gohr     * @param array $options
290dd87735dSAndreas Gohr     *   $options['depth']     recursion level, 0 for all
291dd87735dSAndreas Gohr     *   $options['showmsg']   shows message if invalid media id is used
292dd87735dSAndreas Gohr     *   $options['pattern']   check given pattern
293dd87735dSAndreas Gohr     *   $options['hash']      add hashes to result list
294dd87735dSAndreas Gohr     * @return array
295dd87735dSAndreas Gohr     * @throws AccessDeniedException no access to the media files
296104a3b7cSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
297104a3b7cSAndreas Gohr     *
298dd87735dSAndreas Gohr     */
299104a3b7cSAndreas Gohr    public function listAttachments($ns, $options = [])
300dd87735dSAndreas Gohr    {
301dd87735dSAndreas Gohr        global $conf;
302dd87735dSAndreas Gohr
303dd87735dSAndreas Gohr        $ns = cleanID($ns);
304dd87735dSAndreas Gohr
305104a3b7cSAndreas Gohr        if (!is_array($options)) $options = [];
306dd87735dSAndreas Gohr        $options['skipacl'] = 0; // no ACL skipping for XMLRPC
307dd87735dSAndreas Gohr
308dd87735dSAndreas Gohr        if (auth_quickaclcheck($ns . ':*') >= AUTH_READ) {
309dd87735dSAndreas Gohr            $dir = utf8_encodeFN(str_replace(':', '/', $ns));
310dd87735dSAndreas Gohr
311104a3b7cSAndreas Gohr            $data = [];
312dd87735dSAndreas Gohr            search($data, $conf['mediadir'], 'search_media', $options, $dir);
313dd87735dSAndreas Gohr            $len = count($data);
314104a3b7cSAndreas Gohr            if (!$len) return [];
315dd87735dSAndreas Gohr
316dd87735dSAndreas Gohr            for ($i = 0; $i < $len; $i++) {
317dd87735dSAndreas Gohr                unset($data[$i]['meta']);
318dd87735dSAndreas Gohr                $data[$i]['perms'] = $data[$i]['perm'];
319dd87735dSAndreas Gohr                unset($data[$i]['perm']);
320dd87735dSAndreas Gohr                $data[$i]['lastModified'] = $this->api->toDate($data[$i]['mtime']);
321dd87735dSAndreas Gohr            }
322dd87735dSAndreas Gohr            return $data;
323dd87735dSAndreas Gohr        } else {
324dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to list media files.', 215);
325dd87735dSAndreas Gohr        }
326dd87735dSAndreas Gohr    }
327dd87735dSAndreas Gohr
328dd87735dSAndreas Gohr    /**
329dd87735dSAndreas Gohr     * Return a list of backlinks
330dd87735dSAndreas Gohr     *
331dd87735dSAndreas Gohr     * @param string $id page id
332dd87735dSAndreas Gohr     * @return array
333dd87735dSAndreas Gohr     */
334dd87735dSAndreas Gohr    public function listBackLinks($id)
335dd87735dSAndreas Gohr    {
336dd87735dSAndreas Gohr        return ft_backlinks($this->resolvePageId($id));
337dd87735dSAndreas Gohr    }
338dd87735dSAndreas Gohr
339dd87735dSAndreas Gohr    /**
340dd87735dSAndreas Gohr     * Return some basic data about a page
341dd87735dSAndreas Gohr     *
342dd87735dSAndreas Gohr     * @param string $id page id
343dd87735dSAndreas Gohr     * @param string|int $rev revision timestamp or empty string
344dd87735dSAndreas Gohr     * @return array
345dd87735dSAndreas Gohr     * @throws AccessDeniedException no access for page
346dd87735dSAndreas Gohr     * @throws RemoteException page not exist
347dd87735dSAndreas Gohr     */
348dd87735dSAndreas Gohr    public function pageInfo($id, $rev = '')
349dd87735dSAndreas Gohr    {
350dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
351dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
352dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
353dd87735dSAndreas Gohr        }
354dd87735dSAndreas Gohr        $file = wikiFN($id, $rev);
355dd87735dSAndreas Gohr        $time = @filemtime($file);
356dd87735dSAndreas Gohr        if (!$time) {
357dd87735dSAndreas Gohr            throw new RemoteException('The requested page does not exist', 121);
358dd87735dSAndreas Gohr        }
359dd87735dSAndreas Gohr
360dd87735dSAndreas Gohr        // set revision to current version if empty, use revision otherwise
361dd87735dSAndreas Gohr        // as the timestamps of old files are not necessarily correct
362dd87735dSAndreas Gohr        if ($rev === '') {
363dd87735dSAndreas Gohr            $rev = $time;
364dd87735dSAndreas Gohr        }
365dd87735dSAndreas Gohr
366dd87735dSAndreas Gohr        $pagelog = new PageChangeLog($id, 1024);
367dd87735dSAndreas Gohr        $info = $pagelog->getRevisionInfo($rev);
368dd87735dSAndreas Gohr
369104a3b7cSAndreas Gohr        $data = [
370dd87735dSAndreas Gohr            'name' => $id,
371dd87735dSAndreas Gohr            'lastModified' => $this->api->toDate($rev),
372104a3b7cSAndreas Gohr            'author' => is_array($info) ? ($info['user'] ?: $info['ip']) : null,
373dd87735dSAndreas Gohr            'version' => $rev
374104a3b7cSAndreas Gohr        ];
375dd87735dSAndreas Gohr
376dd87735dSAndreas Gohr        return ($data);
377dd87735dSAndreas Gohr    }
378dd87735dSAndreas Gohr
379dd87735dSAndreas Gohr    /**
380dd87735dSAndreas Gohr     * Save a wiki page
381dd87735dSAndreas Gohr     *
382dd87735dSAndreas Gohr     * @param string $id page id
383dd87735dSAndreas Gohr     * @param string $text wiki text
384dd87735dSAndreas Gohr     * @param array $params parameters: summary, minor edit
385dd87735dSAndreas Gohr     * @return bool
386dd87735dSAndreas Gohr     * @throws AccessDeniedException no write access for page
387dd87735dSAndreas Gohr     * @throws RemoteException no id, empty new page or locked
388104a3b7cSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
389104a3b7cSAndreas Gohr     *
390dd87735dSAndreas Gohr     */
391104a3b7cSAndreas Gohr    public function putPage($id, $text, $params = [])
392dd87735dSAndreas Gohr    {
393dd87735dSAndreas Gohr        global $TEXT;
394dd87735dSAndreas Gohr        global $lang;
395dd87735dSAndreas Gohr
396dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
397dd87735dSAndreas Gohr        $TEXT = cleanText($text);
398*53585189SAndreas Gohr        $sum = $params['sum'] ?? '';
399*53585189SAndreas Gohr        $minor = $params['minor'] ?? false;
400dd87735dSAndreas Gohr
401dd87735dSAndreas Gohr        if (empty($id)) {
402dd87735dSAndreas Gohr            throw new RemoteException('Empty page ID', 131);
403dd87735dSAndreas Gohr        }
404dd87735dSAndreas Gohr
405dd87735dSAndreas Gohr        if (!page_exists($id) && trim($TEXT) == '') {
406dd87735dSAndreas Gohr            throw new RemoteException('Refusing to write an empty new wiki page', 132);
407dd87735dSAndreas Gohr        }
408dd87735dSAndreas Gohr
409dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_EDIT) {
410dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to edit this page', 112);
411dd87735dSAndreas Gohr        }
412dd87735dSAndreas Gohr
413dd87735dSAndreas Gohr        // Check, if page is locked
414dd87735dSAndreas Gohr        if (checklock($id)) {
415dd87735dSAndreas Gohr            throw new RemoteException('The page is currently locked', 133);
416dd87735dSAndreas Gohr        }
417dd87735dSAndreas Gohr
418dd87735dSAndreas Gohr        // SPAM check
419dd87735dSAndreas Gohr        if (checkwordblock()) {
420dd87735dSAndreas Gohr            throw new RemoteException('Positive wordblock check', 134);
421dd87735dSAndreas Gohr        }
422dd87735dSAndreas Gohr
423dd87735dSAndreas Gohr        // autoset summary on new pages
424dd87735dSAndreas Gohr        if (!page_exists($id) && empty($sum)) {
425dd87735dSAndreas Gohr            $sum = $lang['created'];
426dd87735dSAndreas Gohr        }
427dd87735dSAndreas Gohr
428dd87735dSAndreas Gohr        // autoset summary on deleted pages
429dd87735dSAndreas Gohr        if (page_exists($id) && empty($TEXT) && empty($sum)) {
430dd87735dSAndreas Gohr            $sum = $lang['deleted'];
431dd87735dSAndreas Gohr        }
432dd87735dSAndreas Gohr
433dd87735dSAndreas Gohr        lock($id);
434dd87735dSAndreas Gohr
435dd87735dSAndreas Gohr        saveWikiText($id, $TEXT, $sum, $minor);
436dd87735dSAndreas Gohr
437dd87735dSAndreas Gohr        unlock($id);
438dd87735dSAndreas Gohr
439dd87735dSAndreas Gohr        // run the indexer if page wasn't indexed yet
440dd87735dSAndreas Gohr        idx_addPage($id);
441dd87735dSAndreas Gohr
442dd87735dSAndreas Gohr        return true;
443dd87735dSAndreas Gohr    }
444dd87735dSAndreas Gohr
445dd87735dSAndreas Gohr    /**
446dd87735dSAndreas Gohr     * Appends text to a wiki page.
447dd87735dSAndreas Gohr     *
448dd87735dSAndreas Gohr     * @param string $id page id
449dd87735dSAndreas Gohr     * @param string $text wiki text
450dd87735dSAndreas Gohr     * @param array $params such as summary,minor
451dd87735dSAndreas Gohr     * @return bool|string
452dd87735dSAndreas Gohr     * @throws RemoteException
453dd87735dSAndreas Gohr     */
454104a3b7cSAndreas Gohr    public function appendPage($id, $text, $params = [])
455dd87735dSAndreas Gohr    {
456dd87735dSAndreas Gohr        $currentpage = $this->rawPage($id);
457dd87735dSAndreas Gohr        if (!is_string($currentpage)) {
458dd87735dSAndreas Gohr            return $currentpage;
459dd87735dSAndreas Gohr        }
460dd87735dSAndreas Gohr        return $this->putPage($id, $currentpage . $text, $params);
461dd87735dSAndreas Gohr    }
462dd87735dSAndreas Gohr
463dd87735dSAndreas Gohr    /**
4640e0fd3b7SMichael Wegener     * Create one or more users
4650e0fd3b7SMichael Wegener     *
46605438aa9SMichael Wegener     * @param array[] $userStruct User struct
4670e0fd3b7SMichael Wegener     *
46805438aa9SMichael Wegener     * @return boolean Create state
4690e0fd3b7SMichael Wegener     *
4700e0fd3b7SMichael Wegener     * @throws AccessDeniedException
4710e0fd3b7SMichael Wegener     * @throws RemoteException
4720e0fd3b7SMichael Wegener     */
473f0e32bb9SMichael Wegener    public function createUser($userStruct)
4740e0fd3b7SMichael Wegener    {
4750e0fd3b7SMichael Wegener        if (!auth_isadmin()) {
4760e0fd3b7SMichael Wegener            throw new AccessDeniedException('Only admins are allowed to create users', 114);
4770e0fd3b7SMichael Wegener        }
4780e0fd3b7SMichael Wegener
479104a3b7cSAndreas Gohr        /** @var AuthPlugin $auth */
4800e0fd3b7SMichael Wegener        global $auth;
4810e0fd3b7SMichael Wegener
4820e0fd3b7SMichael Wegener        if (!$auth->canDo('addUser')) {
4830e0fd3b7SMichael Wegener            throw new AccessDeniedException(
4840e0fd3b7SMichael Wegener                sprintf('Authentication backend %s can\'t do addUser', $auth->getPluginName()),
4850e0fd3b7SMichael Wegener                114
4860e0fd3b7SMichael Wegener            );
4870e0fd3b7SMichael Wegener        }
4880e0fd3b7SMichael Wegener
489f0e32bb9SMichael Wegener        $user = trim($auth->cleanUser($userStruct['user'] ?? ''));
490f0e32bb9SMichael Wegener        $password = $userStruct['password'] ?? '';
491f0e32bb9SMichael Wegener        $name = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $userStruct['name'] ?? ''));
492f0e32bb9SMichael Wegener        $mail = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $userStruct['mail'] ?? ''));
493f0e32bb9SMichael Wegener        $groups = $userStruct['groups'] ?? [];
4940e0fd3b7SMichael Wegener
4954396d6ecSMichael Wegener        $notify = (bool)$userStruct['notify'] ?? false;
496f0e32bb9SMichael Wegener
497b1d4a667SAndreas Gohr        if ($user === '') throw new RemoteException('empty or invalid user', 401);
498b1d4a667SAndreas Gohr        if ($name === '') throw new RemoteException('empty or invalid user name', 402);
499b1d4a667SAndreas Gohr        if (!mail_isvalid($mail)) throw new RemoteException('empty or invalid mail address', 403);
500f0e32bb9SMichael Wegener
501104a3b7cSAndreas Gohr        if ((string)$password === '') {
502f0e32bb9SMichael Wegener            $password = auth_pwgen($user);
503f0e32bb9SMichael Wegener        }
504f0e32bb9SMichael Wegener
505104a3b7cSAndreas Gohr        if (!is_array($groups) || $groups === []) {
506f0e32bb9SMichael Wegener            $groups = null;
507f0e32bb9SMichael Wegener        }
508f0e32bb9SMichael Wegener
509104a3b7cSAndreas Gohr        $ok = $auth->triggerUserMod('create', [$user, $password, $name, $mail, $groups]);
510f0e32bb9SMichael Wegener
511f0e32bb9SMichael Wegener        if ($ok !== false && $ok !== null) {
512f0e32bb9SMichael Wegener            $ok = true;
513f0e32bb9SMichael Wegener        }
514f0e32bb9SMichael Wegener
5150e0fd3b7SMichael Wegener        if ($ok) {
516f0e32bb9SMichael Wegener            if ($notify) {
517f0e32bb9SMichael Wegener                auth_sendPassword($user, $password);
5180e0fd3b7SMichael Wegener            }
5190e0fd3b7SMichael Wegener        }
520f0e32bb9SMichael Wegener
521f0e32bb9SMichael Wegener        return $ok;
5220e0fd3b7SMichael Wegener    }
5230e0fd3b7SMichael Wegener
5240e0fd3b7SMichael Wegener
5250e0fd3b7SMichael Wegener    /**
5268eb28c6dSAndreas Gohr     * Remove one or more users from the list of registered users
5278eb28c6dSAndreas Gohr     *
5288eb28c6dSAndreas Gohr     * @param string[] $usernames List of usernames to remove
5298eb28c6dSAndreas Gohr     *
5308eb28c6dSAndreas Gohr     * @return bool
5318eb28c6dSAndreas Gohr     *
5328eb28c6dSAndreas Gohr     * @throws AccessDeniedException
5338eb28c6dSAndreas Gohr     */
5348eb28c6dSAndreas Gohr    public function deleteUsers($usernames)
5358eb28c6dSAndreas Gohr    {
5368eb28c6dSAndreas Gohr        if (!auth_isadmin()) {
5378eb28c6dSAndreas Gohr            throw new AccessDeniedException('Only admins are allowed to delete users', 114);
5388eb28c6dSAndreas Gohr        }
539104a3b7cSAndreas Gohr        /** @var AuthPlugin $auth */
5408eb28c6dSAndreas Gohr        global $auth;
541104a3b7cSAndreas Gohr        return (bool)$auth->triggerUserMod('delete', [$usernames]);
5428eb28c6dSAndreas Gohr    }
5438eb28c6dSAndreas Gohr
5448eb28c6dSAndreas Gohr    /**
545dd87735dSAndreas Gohr     * Uploads a file to the wiki.
546dd87735dSAndreas Gohr     *
547dd87735dSAndreas Gohr     * Michael Klier <chi@chimeric.de>
548dd87735dSAndreas Gohr     *
549dd87735dSAndreas Gohr     * @param string $id page id
550dd87735dSAndreas Gohr     * @param string $file
551dd87735dSAndreas Gohr     * @param array $params such as overwrite
552dd87735dSAndreas Gohr     * @return false|string
553dd87735dSAndreas Gohr     * @throws RemoteException
554dd87735dSAndreas Gohr     */
555104a3b7cSAndreas Gohr    public function putAttachment($id, $file, $params = [])
556dd87735dSAndreas Gohr    {
557dd87735dSAndreas Gohr        $id = cleanID($id);
558dd87735dSAndreas Gohr        $auth = auth_quickaclcheck(getNS($id) . ':*');
559dd87735dSAndreas Gohr
560dd87735dSAndreas Gohr        if (!isset($id)) {
561dd87735dSAndreas Gohr            throw new RemoteException('Filename not given.', 231);
562dd87735dSAndreas Gohr        }
563dd87735dSAndreas Gohr
564dd87735dSAndreas Gohr        global $conf;
565dd87735dSAndreas Gohr
566dd87735dSAndreas Gohr        $ftmp = $conf['tmpdir'] . '/' . md5($id . clientIP());
567dd87735dSAndreas Gohr
568dd87735dSAndreas Gohr        // save temporary file
569dd87735dSAndreas Gohr        @unlink($ftmp);
570dd87735dSAndreas Gohr        io_saveFile($ftmp, $file);
571dd87735dSAndreas Gohr
572104a3b7cSAndreas Gohr        $res = media_save(['name' => $ftmp], $id, $params['ow'], $auth, 'rename');
573dd87735dSAndreas Gohr        if (is_array($res)) {
574dd87735dSAndreas Gohr            throw new RemoteException($res[0], -$res[1]);
575dd87735dSAndreas Gohr        } else {
576dd87735dSAndreas Gohr            return $res;
577dd87735dSAndreas Gohr        }
578dd87735dSAndreas Gohr    }
579dd87735dSAndreas Gohr
580dd87735dSAndreas Gohr    /**
581dd87735dSAndreas Gohr     * Deletes a file from the wiki.
582dd87735dSAndreas Gohr     *
583dd87735dSAndreas Gohr     * @param string $id page id
584dd87735dSAndreas Gohr     * @return int
585dd87735dSAndreas Gohr     * @throws AccessDeniedException no permissions
586dd87735dSAndreas Gohr     * @throws RemoteException file in use or not deleted
587104a3b7cSAndreas Gohr     * @author Gina Haeussge <osd@foosel.net>
588104a3b7cSAndreas Gohr     *
589dd87735dSAndreas Gohr     */
590dd87735dSAndreas Gohr    public function deleteAttachment($id)
591dd87735dSAndreas Gohr    {
592dd87735dSAndreas Gohr        $id = cleanID($id);
593dd87735dSAndreas Gohr        $auth = auth_quickaclcheck(getNS($id) . ':*');
594dd87735dSAndreas Gohr        $res = media_delete($id, $auth);
595dd87735dSAndreas Gohr        if ($res & DOKU_MEDIA_DELETED) {
596dd87735dSAndreas Gohr            return 0;
597dd87735dSAndreas Gohr        } elseif ($res & DOKU_MEDIA_NOT_AUTH) {
598dd87735dSAndreas Gohr            throw new AccessDeniedException('You don\'t have permissions to delete files.', 212);
599dd87735dSAndreas Gohr        } elseif ($res & DOKU_MEDIA_INUSE) {
600dd87735dSAndreas Gohr            throw new RemoteException('File is still referenced', 232);
601dd87735dSAndreas Gohr        } else {
602dd87735dSAndreas Gohr            throw new RemoteException('Could not delete file', 233);
603dd87735dSAndreas Gohr        }
604dd87735dSAndreas Gohr    }
605dd87735dSAndreas Gohr
606dd87735dSAndreas Gohr    /**
607dd87735dSAndreas Gohr     * Returns the permissions of a given wiki page for the current user or another user
608dd87735dSAndreas Gohr     *
609dd87735dSAndreas Gohr     * @param string $id page id
610dd87735dSAndreas Gohr     * @param string|null $user username
611dd87735dSAndreas Gohr     * @param array|null $groups array of groups
612dd87735dSAndreas Gohr     * @return int permission level
613dd87735dSAndreas Gohr     */
614dd87735dSAndreas Gohr    public function aclCheck($id, $user = null, $groups = null)
615dd87735dSAndreas Gohr    {
616104a3b7cSAndreas Gohr        /** @var AuthPlugin $auth */
617dd87735dSAndreas Gohr        global $auth;
618dd87735dSAndreas Gohr
619dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
620dd87735dSAndreas Gohr        if ($user === null) {
621dd87735dSAndreas Gohr            return auth_quickaclcheck($id);
622dd87735dSAndreas Gohr        } else {
623dd87735dSAndreas Gohr            if ($groups === null) {
624dd87735dSAndreas Gohr                $userinfo = $auth->getUserData($user);
625dd87735dSAndreas Gohr                if ($userinfo === false) {
626104a3b7cSAndreas Gohr                    $groups = [];
627dd87735dSAndreas Gohr                } else {
628dd87735dSAndreas Gohr                    $groups = $userinfo['grps'];
629dd87735dSAndreas Gohr                }
630dd87735dSAndreas Gohr            }
631dd87735dSAndreas Gohr            return auth_aclcheck($id, $user, $groups);
632dd87735dSAndreas Gohr        }
633dd87735dSAndreas Gohr    }
634dd87735dSAndreas Gohr
635dd87735dSAndreas Gohr    /**
636dd87735dSAndreas Gohr     * Lists all links contained in a wiki page
637dd87735dSAndreas Gohr     *
638dd87735dSAndreas Gohr     * @param string $id page id
639dd87735dSAndreas Gohr     * @return array
640dd87735dSAndreas Gohr     * @throws AccessDeniedException  no read access for page
641104a3b7cSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
642104a3b7cSAndreas Gohr     *
643dd87735dSAndreas Gohr     */
644dd87735dSAndreas Gohr    public function listLinks($id)
645dd87735dSAndreas Gohr    {
646dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
647dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
648dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
649dd87735dSAndreas Gohr        }
650104a3b7cSAndreas Gohr        $links = [];
651dd87735dSAndreas Gohr
652dd87735dSAndreas Gohr        // resolve page instructions
653dd87735dSAndreas Gohr        $ins = p_cached_instructions(wikiFN($id));
654dd87735dSAndreas Gohr
655dd87735dSAndreas Gohr        // instantiate new Renderer - needed for interwiki links
656dd87735dSAndreas Gohr        $Renderer = new Doku_Renderer_xhtml();
657dd87735dSAndreas Gohr        $Renderer->interwiki = getInterwiki();
658dd87735dSAndreas Gohr
659dd87735dSAndreas Gohr        // parse parse instructions
660dd87735dSAndreas Gohr        foreach ($ins as $in) {
661104a3b7cSAndreas Gohr            $link = [];
662dd87735dSAndreas Gohr            switch ($in[0]) {
663dd87735dSAndreas Gohr                case 'internallink':
664dd87735dSAndreas Gohr                    $link['type'] = 'local';
665dd87735dSAndreas Gohr                    $link['page'] = $in[1][0];
666dd87735dSAndreas Gohr                    $link['href'] = wl($in[1][0]);
667104a3b7cSAndreas Gohr                    $links[] = $link;
668dd87735dSAndreas Gohr                    break;
669dd87735dSAndreas Gohr                case 'externallink':
670dd87735dSAndreas Gohr                    $link['type'] = 'extern';
671dd87735dSAndreas Gohr                    $link['page'] = $in[1][0];
672dd87735dSAndreas Gohr                    $link['href'] = $in[1][0];
673104a3b7cSAndreas Gohr                    $links[] = $link;
674dd87735dSAndreas Gohr                    break;
675dd87735dSAndreas Gohr                case 'interwikilink':
676dd87735dSAndreas Gohr                    $url = $Renderer->_resolveInterWiki($in[1][2], $in[1][3]);
677dd87735dSAndreas Gohr                    $link['type'] = 'extern';
678dd87735dSAndreas Gohr                    $link['page'] = $url;
679dd87735dSAndreas Gohr                    $link['href'] = $url;
680104a3b7cSAndreas Gohr                    $links[] = $link;
681dd87735dSAndreas Gohr                    break;
682dd87735dSAndreas Gohr            }
683dd87735dSAndreas Gohr        }
684dd87735dSAndreas Gohr
685dd87735dSAndreas Gohr        return ($links);
686dd87735dSAndreas Gohr    }
687dd87735dSAndreas Gohr
688dd87735dSAndreas Gohr    /**
689dd87735dSAndreas Gohr     * Returns a list of recent changes since give timestamp
690dd87735dSAndreas Gohr     *
691dd87735dSAndreas Gohr     * @param int $timestamp unix timestamp
692dd87735dSAndreas Gohr     * @return array
693dd87735dSAndreas Gohr     * @throws RemoteException no valid timestamp
694104a3b7cSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
695104a3b7cSAndreas Gohr     *
696104a3b7cSAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
697dd87735dSAndreas Gohr     */
698dd87735dSAndreas Gohr    public function getRecentChanges($timestamp)
699dd87735dSAndreas Gohr    {
700dd87735dSAndreas Gohr        if (strlen($timestamp) != 10) {
701dd87735dSAndreas Gohr            throw new RemoteException('The provided value is not a valid timestamp', 311);
702dd87735dSAndreas Gohr        }
703dd87735dSAndreas Gohr
704dd87735dSAndreas Gohr        $recents = getRecentsSince($timestamp);
705dd87735dSAndreas Gohr
706104a3b7cSAndreas Gohr        $changes = [];
707dd87735dSAndreas Gohr
708dd87735dSAndreas Gohr        foreach ($recents as $recent) {
709104a3b7cSAndreas Gohr            $change = [];
710dd87735dSAndreas Gohr            $change['name'] = $recent['id'];
711dd87735dSAndreas Gohr            $change['lastModified'] = $this->api->toDate($recent['date']);
712dd87735dSAndreas Gohr            $change['author'] = $recent['user'];
713dd87735dSAndreas Gohr            $change['version'] = $recent['date'];
714dd87735dSAndreas Gohr            $change['perms'] = $recent['perms'];
715dd87735dSAndreas Gohr            $change['size'] = @filesize(wikiFN($recent['id']));
716104a3b7cSAndreas Gohr            $changes[] = $change;
717dd87735dSAndreas Gohr        }
718dd87735dSAndreas Gohr
719104a3b7cSAndreas Gohr        if ($changes !== []) {
720dd87735dSAndreas Gohr            return $changes;
721dd87735dSAndreas Gohr        } else {
722dd87735dSAndreas Gohr            // in case we still have nothing at this point
723dd87735dSAndreas Gohr            throw new RemoteException('There are no changes in the specified timeframe', 321);
724dd87735dSAndreas Gohr        }
725dd87735dSAndreas Gohr    }
726dd87735dSAndreas Gohr
727dd87735dSAndreas Gohr    /**
728dd87735dSAndreas Gohr     * Returns a list of recent media changes since give timestamp
729dd87735dSAndreas Gohr     *
730dd87735dSAndreas Gohr     * @param int $timestamp unix timestamp
731dd87735dSAndreas Gohr     * @return array
732dd87735dSAndreas Gohr     * @throws RemoteException no valid timestamp
733104a3b7cSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
734104a3b7cSAndreas Gohr     *
735104a3b7cSAndreas Gohr     * @author Michael Hamann <michael@content-space.de>
736dd87735dSAndreas Gohr     */
737dd87735dSAndreas Gohr    public function getRecentMediaChanges($timestamp)
738dd87735dSAndreas Gohr    {
739dd87735dSAndreas Gohr        if (strlen($timestamp) != 10)
740dd87735dSAndreas Gohr            throw new RemoteException('The provided value is not a valid timestamp', 311);
741dd87735dSAndreas Gohr
742dd87735dSAndreas Gohr        $recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
743dd87735dSAndreas Gohr
744104a3b7cSAndreas Gohr        $changes = [];
745dd87735dSAndreas Gohr
746dd87735dSAndreas Gohr        foreach ($recents as $recent) {
747104a3b7cSAndreas Gohr            $change = [];
748dd87735dSAndreas Gohr            $change['name'] = $recent['id'];
749dd87735dSAndreas Gohr            $change['lastModified'] = $this->api->toDate($recent['date']);
750dd87735dSAndreas Gohr            $change['author'] = $recent['user'];
751dd87735dSAndreas Gohr            $change['version'] = $recent['date'];
752dd87735dSAndreas Gohr            $change['perms'] = $recent['perms'];
753dd87735dSAndreas Gohr            $change['size'] = @filesize(mediaFN($recent['id']));
754104a3b7cSAndreas Gohr            $changes[] = $change;
755dd87735dSAndreas Gohr        }
756dd87735dSAndreas Gohr
757104a3b7cSAndreas Gohr        if ($changes !== []) {
758dd87735dSAndreas Gohr            return $changes;
759dd87735dSAndreas Gohr        } else {
760dd87735dSAndreas Gohr            // in case we still have nothing at this point
761dd87735dSAndreas Gohr            throw new RemoteException('There are no changes in the specified timeframe', 321);
762dd87735dSAndreas Gohr        }
763dd87735dSAndreas Gohr    }
764dd87735dSAndreas Gohr
765dd87735dSAndreas Gohr    /**
766dd87735dSAndreas Gohr     * Returns a list of available revisions of a given wiki page
767dd87735dSAndreas Gohr     * Number of returned pages is set by $conf['recent']
768dd87735dSAndreas Gohr     * However not accessible pages are skipped, so less than $conf['recent'] could be returned
769dd87735dSAndreas Gohr     *
770dd87735dSAndreas Gohr     * @param string $id page id
771dd87735dSAndreas Gohr     * @param int $first skip the first n changelog lines
772dd87735dSAndreas Gohr     *                      0 = from current(if exists)
773dd87735dSAndreas Gohr     *                      1 = from 1st old rev
774dd87735dSAndreas Gohr     *                      2 = from 2nd old rev, etc
775dd87735dSAndreas Gohr     * @return array
776dd87735dSAndreas Gohr     * @throws AccessDeniedException no read access for page
777dd87735dSAndreas Gohr     * @throws RemoteException empty id
778104a3b7cSAndreas Gohr     * @author Michael Klier <chi@chimeric.de>
779104a3b7cSAndreas Gohr     *
780dd87735dSAndreas Gohr     */
781a9284ce8SPhy    public function pageVersions($id, $first = 0)
782dd87735dSAndreas Gohr    {
783dd87735dSAndreas Gohr        $id = $this->resolvePageId($id);
784dd87735dSAndreas Gohr        if (auth_quickaclcheck($id) < AUTH_READ) {
785dd87735dSAndreas Gohr            throw new AccessDeniedException('You are not allowed to read this page', 111);
786dd87735dSAndreas Gohr        }
787dd87735dSAndreas Gohr        global $conf;
788dd87735dSAndreas Gohr
789104a3b7cSAndreas Gohr        $versions = [];
790dd87735dSAndreas Gohr
791dd87735dSAndreas Gohr        if (empty($id)) {
792dd87735dSAndreas Gohr            throw new RemoteException('Empty page ID', 131);
793dd87735dSAndreas Gohr        }
794dd87735dSAndreas Gohr
795dd87735dSAndreas Gohr        $first = (int)$first;
796dd87735dSAndreas Gohr        $first_rev = $first - 1;
797efc3ac2fSsplitbrain        $first_rev = max(0, $first_rev);
798104a3b7cSAndreas Gohr
799dd87735dSAndreas Gohr        $pagelog = new PageChangeLog($id);
800dd87735dSAndreas Gohr        $revisions = $pagelog->getRevisions($first_rev, $conf['recent']);
801dd87735dSAndreas Gohr
802dd87735dSAndreas Gohr        if ($first == 0) {
803dd87735dSAndreas Gohr            array_unshift($revisions, '');  // include current revision
804dd87735dSAndreas Gohr            if (count($revisions) > $conf['recent']) {
805dd87735dSAndreas Gohr                array_pop($revisions);          // remove extra log entry
806dd87735dSAndreas Gohr            }
807dd87735dSAndreas Gohr        }
808dd87735dSAndreas Gohr
809dd87735dSAndreas Gohr        if (!empty($revisions)) {
810dd87735dSAndreas Gohr            foreach ($revisions as $rev) {
811dd87735dSAndreas Gohr                $file = wikiFN($id, $rev);
812dd87735dSAndreas Gohr                $time = @filemtime($file);
813dd87735dSAndreas Gohr                // we check if the page actually exists, if this is not the
814dd87735dSAndreas Gohr                // case this can lead to less pages being returned than
815dd87735dSAndreas Gohr                // specified via $conf['recent']
816dd87735dSAndreas Gohr                if ($time) {
817dd87735dSAndreas Gohr                    $pagelog->setChunkSize(1024);
818104a3b7cSAndreas Gohr                    $info = $pagelog->getRevisionInfo($rev ?: $time);
819dd87735dSAndreas Gohr                    if (!empty($info)) {
820104a3b7cSAndreas Gohr                        $data = [];
821dd87735dSAndreas Gohr                        $data['user'] = $info['user'];
822dd87735dSAndreas Gohr                        $data['ip'] = $info['ip'];
823dd87735dSAndreas Gohr                        $data['type'] = $info['type'];
824dd87735dSAndreas Gohr                        $data['sum'] = $info['sum'];
825dd87735dSAndreas Gohr                        $data['modified'] = $this->api->toDate($info['date']);
826dd87735dSAndreas Gohr                        $data['version'] = $info['date'];
827104a3b7cSAndreas Gohr                        $versions[] = $data;
828dd87735dSAndreas Gohr                    }
829dd87735dSAndreas Gohr                }
830dd87735dSAndreas Gohr            }
831dd87735dSAndreas Gohr            return $versions;
832dd87735dSAndreas Gohr        } else {
833104a3b7cSAndreas Gohr            return [];
834dd87735dSAndreas Gohr        }
835dd87735dSAndreas Gohr    }
836dd87735dSAndreas Gohr
837dd87735dSAndreas Gohr    /**
838dd87735dSAndreas Gohr     * The version of Wiki RPC API supported
83942e66c7aSAndreas Gohr     *
84042e66c7aSAndreas Gohr     * This is the version of the Wiki RPC specification implemented. Since that specification
84142e66c7aSAndreas Gohr     * is no longer maintained, this will always return 2
84242e66c7aSAndreas Gohr     *
84342e66c7aSAndreas Gohr     * You probably want to look at dokuwiki.getXMLRPCAPIVersion instead
84442e66c7aSAndreas Gohr     *
84542e66c7aSAndreas Gohr     * @return int
846dd87735dSAndreas Gohr     */
847dd87735dSAndreas Gohr    public function wikiRpcVersion()
848dd87735dSAndreas Gohr    {
849dd87735dSAndreas Gohr        return 2;
850dd87735dSAndreas Gohr    }
851dd87735dSAndreas Gohr
852dd87735dSAndreas Gohr    /**
853dd87735dSAndreas Gohr     * Locks or unlocks a given batch of pages
854dd87735dSAndreas Gohr     *
855dd87735dSAndreas Gohr     * Give an associative array with two keys: lock and unlock. Both should contain a
856dd87735dSAndreas Gohr     * list of pages to lock or unlock
857dd87735dSAndreas Gohr     *
858dd87735dSAndreas Gohr     * Returns an associative array with the keys locked, lockfail, unlocked and
859dd87735dSAndreas Gohr     * unlockfail, each containing lists of pages.
860dd87735dSAndreas Gohr     *
861dd87735dSAndreas Gohr     * @param array[] $set list pages with array('lock' => array, 'unlock' => array)
862dd87735dSAndreas Gohr     * @return array
863dd87735dSAndreas Gohr     */
864dd87735dSAndreas Gohr    public function setLocks($set)
865dd87735dSAndreas Gohr    {
866104a3b7cSAndreas Gohr        $locked = [];
867104a3b7cSAndreas Gohr        $lockfail = [];
868104a3b7cSAndreas Gohr        $unlocked = [];
869104a3b7cSAndreas Gohr        $unlockfail = [];
870dd87735dSAndreas Gohr
871104a3b7cSAndreas Gohr        foreach ($set['lock'] as $id) {
872dd87735dSAndreas Gohr            $id = $this->resolvePageId($id);
873dd87735dSAndreas Gohr            if (auth_quickaclcheck($id) < AUTH_EDIT || checklock($id)) {
874dd87735dSAndreas Gohr                $lockfail[] = $id;
875dd87735dSAndreas Gohr            } else {
876dd87735dSAndreas Gohr                lock($id);
877dd87735dSAndreas Gohr                $locked[] = $id;
878dd87735dSAndreas Gohr            }
879dd87735dSAndreas Gohr        }
880dd87735dSAndreas Gohr
881104a3b7cSAndreas Gohr        foreach ($set['unlock'] as $id) {
882dd87735dSAndreas Gohr            $id = $this->resolvePageId($id);
883dd87735dSAndreas Gohr            if (auth_quickaclcheck($id) < AUTH_EDIT || !unlock($id)) {
884dd87735dSAndreas Gohr                $unlockfail[] = $id;
885dd87735dSAndreas Gohr            } else {
886dd87735dSAndreas Gohr                $unlocked[] = $id;
887dd87735dSAndreas Gohr            }
888dd87735dSAndreas Gohr        }
889dd87735dSAndreas Gohr
890104a3b7cSAndreas Gohr        return [
891dd87735dSAndreas Gohr            'locked' => $locked,
892dd87735dSAndreas Gohr            'lockfail' => $lockfail,
893dd87735dSAndreas Gohr            'unlocked' => $unlocked,
894104a3b7cSAndreas Gohr            'unlockfail' => $unlockfail
895104a3b7cSAndreas Gohr        ];
896dd87735dSAndreas Gohr    }
897dd87735dSAndreas Gohr
898dd87735dSAndreas Gohr    /**
899dd87735dSAndreas Gohr     * Return API version
900dd87735dSAndreas Gohr     *
901dd87735dSAndreas Gohr     * @return int
902dd87735dSAndreas Gohr     */
903dd87735dSAndreas Gohr    public function getAPIVersion()
904dd87735dSAndreas Gohr    {
905dd87735dSAndreas Gohr        return self::API_VERSION;
906dd87735dSAndreas Gohr    }
907dd87735dSAndreas Gohr
908dd87735dSAndreas Gohr    /**
909dd87735dSAndreas Gohr     * Login
910dd87735dSAndreas Gohr     *
911dd87735dSAndreas Gohr     * @param string $user
912dd87735dSAndreas Gohr     * @param string $pass
913dd87735dSAndreas Gohr     * @return int
914dd87735dSAndreas Gohr     */
915dd87735dSAndreas Gohr    public function login($user, $pass)
916dd87735dSAndreas Gohr    {
917dd87735dSAndreas Gohr        global $conf;
918104a3b7cSAndreas Gohr        /** @var AuthPlugin $auth */
919dd87735dSAndreas Gohr        global $auth;
920dd87735dSAndreas Gohr
921dd87735dSAndreas Gohr        if (!$conf['useacl']) return 0;
9226547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin) return 0;
923dd87735dSAndreas Gohr
924dd87735dSAndreas Gohr        @session_start(); // reopen session for login
92581e99965SPhy        $ok = null;
926dd87735dSAndreas Gohr        if ($auth->canDo('external')) {
927dd87735dSAndreas Gohr            $ok = $auth->trustExternal($user, $pass, false);
92881e99965SPhy        }
92981e99965SPhy        if ($ok === null) {
930104a3b7cSAndreas Gohr            $evdata = [
931dd87735dSAndreas Gohr                'user' => $user,
932dd87735dSAndreas Gohr                'password' => $pass,
933dd87735dSAndreas Gohr                'sticky' => false,
934104a3b7cSAndreas Gohr                'silent' => true
935104a3b7cSAndreas Gohr            ];
936cbb44eabSAndreas Gohr            $ok = Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper');
937dd87735dSAndreas Gohr        }
938dd87735dSAndreas Gohr        session_write_close(); // we're done with the session
939dd87735dSAndreas Gohr
940dd87735dSAndreas Gohr        return $ok;
941dd87735dSAndreas Gohr    }
942dd87735dSAndreas Gohr
943dd87735dSAndreas Gohr    /**
944dd87735dSAndreas Gohr     * Log off
945dd87735dSAndreas Gohr     *
946dd87735dSAndreas Gohr     * @return int
947dd87735dSAndreas Gohr     */
948dd87735dSAndreas Gohr    public function logoff()
949dd87735dSAndreas Gohr    {
950dd87735dSAndreas Gohr        global $conf;
951dd87735dSAndreas Gohr        global $auth;
952dd87735dSAndreas Gohr        if (!$conf['useacl']) return 0;
9536547cfc7SGerrit Uitslag        if (!$auth instanceof AuthPlugin) return 0;
954dd87735dSAndreas Gohr
955dd87735dSAndreas Gohr        auth_logoff();
956dd87735dSAndreas Gohr
957dd87735dSAndreas Gohr        return 1;
958dd87735dSAndreas Gohr    }
959dd87735dSAndreas Gohr
960dd87735dSAndreas Gohr    /**
961dd87735dSAndreas Gohr     * Resolve page id
962dd87735dSAndreas Gohr     *
963dd87735dSAndreas Gohr     * @param string $id page id
964dd87735dSAndreas Gohr     * @return string
965dd87735dSAndreas Gohr     */
966dd87735dSAndreas Gohr    private function resolvePageId($id)
967dd87735dSAndreas Gohr    {
968dd87735dSAndreas Gohr        $id = cleanID($id);
969dd87735dSAndreas Gohr        if (empty($id)) {
970dd87735dSAndreas Gohr            global $conf;
971dd87735dSAndreas Gohr            $id = cleanID($conf['start']);
972dd87735dSAndreas Gohr        }
973dd87735dSAndreas Gohr        return $id;
974dd87735dSAndreas Gohr    }
975dd87735dSAndreas Gohr}
976