xref: /dokuwiki/inc/Remote/LegacyApiCore.php (revision 15357ce4c449a1f9c98ab19ad77de75f416fdb82)
1<?php
2
3namespace dokuwiki\Remote;
4
5use dokuwiki\Utf8\PhpString;
6use IXR\DataType\Base64;
7use IXR\DataType\Date;
8
9/**
10 * Provides wrappers for the API calls as they existed in API Version 11
11 *
12 * No guarantees are made about the exact compatibility of the return values.
13 *
14 * @deprecated
15 */
16class LegacyApiCore extends ApiCore
17{
18    /** @inheritdoc */
19    public function getMethods()
20    {
21        $methods = parent::getMethods();
22
23        return array_merge(
24            $methods,
25            [
26                'dokuwiki.getVersion' => new ApiCall([$this, 'legacyGetVersion'], 'legacy'),
27                'dokuwiki.login' => (new ApiCall([$this, 'legacyLogin'], 'legacy'))->setPublic(),
28                'dokuwiki.logoff' => new ApiCall([$this, 'legacyLogoff'], 'legacy'),
29                'dokuwiki.getPagelist' => new ApiCall([$this, 'legacyGetPagelist'], 'legacy'),
30                'dokuwiki.search' => new ApiCall([$this, 'legacySearch'], 'legacy'),
31                'dokuwiki.getTime' => new ApiCall([$this, 'legacyGetTime'], 'legacy'),
32                'dokuwiki.setLocks' => new ApiCall([$this, 'legacySetLocks'], 'legacy'),
33                'dokuwiki.getTitle' => (new ApiCall([$this, 'legacyGetTitle'], 'legacy'))->setPublic(),
34                'dokuwiki.appendPage' => new ApiCall([$this, 'legacyAppendPage'], 'legacy'),
35                'dokuwiki.createUser' => new ApiCall([$this, 'legacyCreateUser'], 'legacy'),
36                'dokuwiki.deleteUsers' => new ApiCall([$this, 'legacyDeleteUsers'], 'legacy'),
37                'wiki.getPage' => new ApiCall([$this, 'legacyGetPage'], 'legacy'),
38                'wiki.getPageVersion' => new ApiCall([$this, 'legacyGetPageVersion'], 'legacy'),
39                'wiki.getPageHTML' => new ApiCall([$this, 'legacyGetPageHTML'], 'legacy'),
40                'wiki.getPageHTMLVersion' => new ApiCall([$this, 'legacyGetPageHTMLVersion'], 'legacy'),
41                'wiki.getAllPages' => new ApiCall([$this, 'legacyGetAllPages'], 'legacy'),
42                'wiki.getAttachments' => new ApiCall([$this, 'legacyGetAttachments'], 'legacy'),
43                'wiki.getBackLinks' => new ApiCall([$this, 'legacyGetBackLinks'], 'legacy'),
44                'wiki.getPageInfo' => new ApiCall([$this, 'legacyGetPageInfo'], 'legacy'),
45                'wiki.getPageInfoVersion' => new ApiCall([$this, 'legacyGetPageInfoVersion'], 'legacy'),
46                'wiki.getPageVersions' => new ApiCall([$this, 'legacyGetPageVersions'], 'legacy'),
47                'wiki.putPage' => new ApiCall([$this, 'legacyPutPage'], 'legacy'),
48                'wiki.listLinks' => new ApiCall([$this, 'legacyListLinks'], 'legacy'),
49                'wiki.getRecentChanges' => new ApiCall([$this, 'legacyGetRecentChanges'], 'legacy'),
50                'wiki.getRecentMediaChanges' => new ApiCall([$this, 'legacyGetRecentMediaChanges'], 'legacy'),
51                'wiki.aclCheck' => new ApiCall([$this, 'legacyAclCheck'], 'legacy'),
52                'wiki.putAttachment' => new ApiCall([$this, 'legacyPutAttachment'], 'legacy'),
53                'wiki.deleteAttachment' => new ApiCall([$this, 'legacyDeleteAttachment'], 'legacy'),
54                'wiki.getAttachment' => new ApiCall([$this, 'legacyGetAttachment'], 'legacy'),
55                'wiki.getAttachmentInfo' => new ApiCall([$this, 'legacyGetAttachmentInfo'], 'legacy'),
56                'dokuwiki.getXMLRPCAPIVersion' => (new ApiCall([$this, 'legacyGetXMLRPCAPIVersion'], 'legacy'))
57                    ->setPublic(),
58                'wiki.getRPCVersionSupported' => (new ApiCall([$this, 'legacyGetRPCVersionSupported'], 'legacy'))
59                    ->setPublic(),
60            ]
61        );
62    }
63
64    /**
65     * This returns a XMLRPC object that will not work for the new JSONRPC API
66     *
67     * @param int $ts
68     * @return Date
69     */
70    protected function toDate($ts)
71    {
72        return new Date($ts);
73    }
74
75
76    /**
77     * @deprecated use core.getWikiVersion instead
78     */
79    public function legacyGetVersion()
80    {
81        return getVersion();
82    }
83
84    /**
85     * @deprecated use core.getWikiTime instead
86     */
87    public function legacyGetTime()
88    {
89        return $this->getWikiTime();
90    }
91
92
93    /**
94     * @deprecated use core.getPage instead
95     */
96    public function legacyGetPage($id)
97    {
98        return $this->getPage($id);
99    }
100
101    /**
102     * @deprecated use core.getPage instead
103     */
104    public function legacyGetPageVersion($id, $rev = '')
105    {
106        return $this->getPage($id, $rev);
107    }
108
109    /**
110     * @deprecated use core.getMedia instead
111     */
112    public function legacyGetAttachment($id)
113    {
114        return new Base64(base64_decode($this->getMedia($id)));
115    }
116
117    /**
118     * @deprecated use core.getMediaInfo instead
119     */
120    public function legacygetAttachmentInfo($id)
121    {
122        $info = $this->getMediaInfo($id);
123        return [
124            'lastModified' => $this->toDate($info->revision),
125            'size' => $info->size,
126        ];
127    }
128
129    /**
130     * @deprecated use core.getPageHTML instead
131     */
132    public function legacyGetPageHTML($id)
133    {
134        return $this->getPageHTML($id);
135    }
136
137    /**
138     * @deprecated use core.getPageHTML instead
139     */
140    public function legacyGetPageHTMLVersion($id, $rev = '')
141    {
142        return $this->getPageHTML($id, (int)$rev);
143    }
144
145    /**
146     * @deprecated use core.listPages instead
147     */
148    public function legacyGetAllPages()
149    {
150        $pages = $this->listPages('', 0);
151
152        $result = [];
153        foreach ($pages as $page) {
154            $result[] = [
155                'id' => $page->id,
156                'perms' => $page->permission,
157                'size' => $page->size,
158                'lastModified' => $this->toDate($page->revision),
159            ];
160        }
161        return $result;
162    }
163
164    /**
165     * @deprecated use core.listPages instead
166     */
167    public function legacyGetPagelist($ns, $opts = [])
168    {
169        $data = $this->listPages($ns, $opts['depth'] ?? 0, $opts['hash'] ?? false);
170        $result = [];
171
172        foreach ($data as $page) {
173            $result[] = [
174                'id' => $page->id,
175                'perms' => $page->permission,
176                'size' => $page->size,
177                'rev' => $page->revision,
178                'lastModified' => $this->toDate($page->revision),
179                'hash' => $page->hash,
180
181            ];
182        }
183
184        return $result;
185    }
186
187    /**
188     * @deprecated use core.searchPages instead
189     */
190    public function legacySearch($query)
191    {
192        $this->searchPages($query);
193        $pages = [];
194
195        foreach ($this->searchPages($query) as $page) {
196            $pages[] = [
197                'id' => $page->id,
198                'score' => $page->score,
199                'rev' => $page->revision,
200                'lastModified' => $this->toDate($page->revision),
201                'size' => $page->size,
202                'snippet' => $page->snippet,
203                'title' => $page->title
204            ];
205        }
206
207        return $pages;
208    }
209
210    /**
211     * @deprecated use core.getWikiTitle instead
212     */
213    public function legacyGetTitle()
214    {
215        return $this->getWikiTitle();
216    }
217
218    /**
219     * @deprecated use core.listMedia instead
220     */
221    public function legacyGetAttachments($ns, $options = [])
222    {
223        $files = $this->listMedia($ns, $options['pattern'] ?? '', $options['depth'] ?? 0, $options['hash'] ?? false);
224        $result = [];
225        foreach ($files as $file) {
226            $result[] = [
227                'id' => $file->id,
228                'perms' => $file->permission,
229                'size' => $file->size,
230                'rev' => $file->revision,
231                'lastModified' => $this->toDate($file->revision),
232                'mtime' => $this->toDate($file->revision),
233                'hash' => $file->hash,
234                'file' => PhpString::basename(mediaFN($file->id)),
235                'writable' => is_writable(mediaFN($file->id)),
236                'isimg' => $file->isimage,
237
238            ];
239        }
240        return $result;
241    }
242
243    /**
244     * @deprecated use core.getPageBackLinks instead
245     */
246    public function legacyGetBackLinks($id)
247    {
248        return $this->getPageBackLinks($id);
249    }
250
251    /**
252     * @deprecated use core.getPageInfo instead
253     */
254    public function legacyGetPageInfo($id)
255    {
256        $info = $this->getPageInfo($id, 0);
257        return [
258            'name' => $info->id,
259            'lastModified' => $this->toDate($info->revision),
260            'author' => $info->author,
261            'version' => $info->revision,
262        ];
263    }
264
265    /**
266     * @deprecated use core.getPageInfo instead
267     */
268    public function legacyGetPageInfoVersion($id, $rev = '')
269    {
270        $info = $this->getPageInfo($id, $rev);
271        return [
272            'name' => $info->id,
273            'lastModified' => $this->toDate($info->revision),
274            'author' => $info->author,
275            'version' => $info->revision,
276        ];
277    }
278
279    /**
280     * @deprecated use core.savePage instead
281     */
282    public function legacyPutPage($id, $text, $params = [])
283    {
284        return $this->savePage($id, $text, $params['sum'] ?? '', $params['minor'] ?? false);
285    }
286
287    /**
288     * @deprecated use core.appendPage instead
289     */
290    public function legacyAppendPage($id, $text, $params = [])
291    {
292        $ok = $this->appendPage($id, $text, $params['summary'] ?? '', $params['minor'] ?? false);
293        if ($ok === true) {
294            return cleanID($id);
295        } else {
296            return $ok;
297        }
298    }
299
300    /**
301     * @deprecated use plugin.usermanager.createUser instead
302     */
303    public function legacyCreateUser($userStruct)
304    {
305        if (!auth_isadmin()) {
306            throw new AccessDeniedException('Only admins are allowed to create users', 114);
307        }
308
309        /** @var AuthPlugin $auth */
310        global $auth;
311
312        if (!$auth->canDo('addUser')) {
313            throw new AccessDeniedException(
314                sprintf('Authentication backend %s can\'t do addUser', $auth->getPluginName()),
315                114
316            );
317        }
318
319        $user = trim($auth->cleanUser($userStruct['user'] ?? ''));
320        $password = $userStruct['password'] ?? '';
321        $name = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $userStruct['name'] ?? ''));
322        $mail = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $userStruct['mail'] ?? ''));
323        $groups = $userStruct['groups'] ?? [];
324
325        $notify = (bool)$userStruct['notify'] ?? false;
326
327        if ($user === '') throw new RemoteException('empty or invalid user', 401);
328        if ($name === '') throw new RemoteException('empty or invalid user name', 402);
329        if (!mail_isvalid($mail)) throw new RemoteException('empty or invalid mail address', 403);
330
331        if ((string)$password === '') {
332            $password = auth_pwgen($user);
333        }
334
335        if (!is_array($groups) || $groups === []) {
336            $groups = null;
337        }
338
339        $ok = $auth->triggerUserMod('create', [$user, $password, $name, $mail, $groups]);
340
341        if ($ok !== false && $ok !== null) {
342            $ok = true;
343        }
344
345        if ($ok) {
346            if ($notify) {
347                auth_sendPassword($user, $password);
348            }
349        }
350
351        return $ok;
352    }
353
354
355    /**
356     * @deprecated use plugin.usermanager.deleteUser instead
357     */
358    public function legacyDeleteUsers($usernames)
359    {
360        if (!auth_isadmin()) {
361            throw new AccessDeniedException('Only admins are allowed to delete users', 114);
362        }
363        /** @var AuthPlugin $auth */
364        global $auth;
365        return (bool)$auth->triggerUserMod('delete', [$usernames]);
366    }
367
368    /**
369     * @deprecated use core.saveMedia instead
370     */
371    public function legacyPutAttachment($id, $file, $params = [])
372    {
373        $ok = $this->saveMedia($id, base64_encode($file), $params['ow'] ?? false);
374        if ($ok === true) {
375            return cleanID($id);
376        } else {
377            return $ok;
378        }
379    }
380
381    /**
382     * @deprecated use core.deleteMedia instead
383     */
384    public function legacyDeleteAttachment($id)
385    {
386        $ok = $this->deleteMedia($id);
387        if ($ok === true) {
388            return 0;
389        } else {
390            return $ok;
391        }
392    }
393
394    /**
395     * @deprecated use core.aclCheck instead
396     */
397    public function legacyAclCheck($id, $user = null, $groups = null)
398    {
399        return $this->aclCheck($id, (string)$user, (string)$groups);
400    }
401
402    /**
403     * @deprecated use core.listLinks instead
404     */
405    public function legacyListLinks($id)
406    {
407        $links = $this->getPageLinks($id);
408        $result = [];
409        foreach ($links as $link) {
410            $result[] = [
411                'type' => $link['type'],
412                'page' => $link['page'],
413                'href' => $link['href'],
414            ];
415        }
416        return $result;
417    }
418
419    /**
420     * @deprecated use core.getRecentChanges instead
421     */
422    public function legacyGetRecentChanges($timestamp)
423    {
424        $recents = $this->getRecentPageChanges($timestamp);
425        $result = [];
426        foreach ($recents as $recent) {
427            $result[] = [
428                'name' => $recent->id,
429                'lastModified' => $this->toDate($recent->revision),
430                'author' => $recent->author,
431                'version' => $recent->revision,
432                'perms' => auth_quickaclcheck($recent->id),
433                'size' => @filesize(wikiFN($recent->id)),
434            ];
435        }
436        return $result;
437    }
438
439    /**
440     * @deprecated use core.getRecentMediaChanges instead
441     */
442    public function legacyGetRecentMediaChanges($timestamp)
443    {
444        $recents = $this->getRecentMediaChanges($timestamp);
445        $result = [];
446        foreach ($recents as $recent) {
447            $result[] = [
448                'name' => $recent->id,
449                'lastModified' => $this->toDate($recent->revision),
450                'author' => $recent->author,
451                'version' => $recent->revision,
452                'perms' => auth_quickaclcheck($recent->id),
453                'size' => @filesize(mediaFN($recent->id)),
454            ];
455        }
456        return $result;
457    }
458
459    /**
460     * @deprecated use core.getPageHistory instead
461     */
462    public function legacyGetPageVersions($id, $first = 0)
463    {
464        $revisions = $this->getPageHistory($id, $first);
465        $result = [];
466
467        foreach ($revisions as $revision) {
468            $result[] = [
469                'user' => $revision->author,
470                'ip' => $revision->ip,
471                'type' => $revision->type,
472                'sum' => $revision->summary,
473                'modified' => $this->toDate($revision->revision),
474                'version' => $revision->revision,
475            ];
476        }
477        return $result;
478    }
479
480    /**
481     * @deprecated Wiki RPC spec is no longer supported
482     */
483    public function legacyGetRPCVersionSupported()
484    {
485        return 2;
486    }
487
488    /**
489     * @deprecated use core.lockPages and core.unlockPages instead
490     */
491    public function legacySetLocks($set)
492    {
493        $locked = $this->lockPages($set['lock']);
494        $lockfail = array_diff($set['lock'], $locked);
495
496        $unlocked = $this->unlockPages($set['unlock']);
497        $unlockfail = array_diff($set['unlock'], $unlocked);
498
499        return [
500            'locked' => $locked,
501            'lockfail' => $lockfail,
502            'unlocked' => $unlocked,
503            'unlockfail' => $unlockfail
504        ];
505    }
506
507    /**
508     * @deprecated use core.getAPIVersion instead
509     */
510    public function legacyGetXMLRPCAPIVersion()
511    {
512        return $this->getAPIVersion();
513    }
514
515    /**
516     * @deprecated use core.login instead
517     */
518    public function legacyLogin($user, $pass)
519    {
520        return parent::login($user, $pass);
521    }
522
523    /**
524     * @deprecated use core.logoff instead
525     */
526    public function legacyLogoff()
527    {
528        return parent::logoff();
529    }
530}
531