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