1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
|
<?php
namespace dokuwiki\Remote;
use Doku_Renderer_xhtml;
use dokuwiki\ChangeLog\PageChangeLog;
use dokuwiki\Extension\AuthPlugin;
use dokuwiki\Extension\Event;
use dokuwiki\Remote\Response\Link;
use dokuwiki\Remote\Response\Media;
use dokuwiki\Remote\Response\MediaChange;
use dokuwiki\Remote\Response\Page;
use dokuwiki\Remote\Response\PageChange;
use dokuwiki\Remote\Response\PageHit;
use dokuwiki\Remote\Response\User;
use dokuwiki\Utf8\Sort;
/**
* Provides the core methods for the remote API.
* The methods are ordered in 'wiki.<method>' and 'dokuwiki.<method>' namespaces
*/
class ApiCore
{
/** @var int Increased whenever the API is changed */
public const API_VERSION = 12;
/**
* Returns details about the core methods
*
* @return array
*/
public function getMethods()
{
return [
'core.getAPIVersion' => (new ApiCall([$this, 'getAPIVersion'], 'info'))->setPublic(),
'core.getWikiVersion' => new ApiCall('getVersion', 'info'),
'core.getWikiTitle' => (new ApiCall([$this, 'getWikiTitle'], 'info'))->setPublic(),
'core.getWikiTime' => (new ApiCall([$this, 'getWikiTime'], 'info')),
'core.login' => (new ApiCall([$this, 'login'], 'user'))->setPublic(),
'core.logoff' => new ApiCall([$this, 'logoff'], 'user'),
'core.whoAmI' => (new ApiCall([$this, 'whoAmI'], 'user')),
'core.aclCheck' => new ApiCall([$this, 'aclCheck'], 'user'),
'core.listPages' => new ApiCall([$this, 'listPages'], 'pages'),
'core.searchPages' => new ApiCall([$this, 'searchPages'], 'pages'),
'core.getRecentPageChanges' => new ApiCall([$this, 'getRecentPageChanges'], 'pages'),
'core.getPage' => (new ApiCall([$this, 'getPage'], 'pages')),
'core.getPageHTML' => (new ApiCall([$this, 'getPageHTML'], 'pages')),
'core.getPageInfo' => (new ApiCall([$this, 'getPageInfo'], 'pages')),
'core.getPageHistory' => new ApiCall([$this, 'getPageHistory'], 'pages'),
'core.getPageLinks' => new ApiCall([$this, 'getPageLinks'], 'pages'),
'core.getPageBackLinks' => new ApiCall([$this, 'getPageBackLinks'], 'pages'),
'core.lockPages' => new ApiCall([$this, 'lockPages'], 'pages'),
'core.unlockPages' => new ApiCall([$this, 'unlockPages'], 'pages'),
'core.savePage' => new ApiCall([$this, 'savePage'], 'pages'),
'core.appendPage' => new ApiCall([$this, 'appendPage'], 'pages'),
'core.listMedia' => new ApiCall([$this, 'listMedia'], 'media'),
'core.getRecentMediaChanges' => new ApiCall([$this, 'getRecentMediaChanges'], 'media'),
'core.getMedia' => new ApiCall([$this, 'getMedia'], 'media'),
'core.getMediaInfo' => new ApiCall([$this, 'getMediaInfo'], 'media'),
// todo: implement getMediaHistory
// todo: implement getMediaUsage
'core.saveMedia' => new ApiCall([$this, 'saveMedia'], 'media'),
'core.deleteMedia' => new ApiCall([$this, 'deleteMedia'], 'media'),
];
}
// region info
/**
* Return the API version
*
* This is the version of the DokuWiki API. It increases whenever the API definition changes.
*
* When developing a client, you should check this version and make sure you can handle it.
*
* @return int
*/
public function getAPIVersion()
{
return self::API_VERSION;
}
/**
* Returns the wiki title
*
* @link https://www.dokuwiki.org/config:title
* @return string
*/
public function getWikiTitle()
{
global $conf;
return $conf['title'];
}
/**
* Return the current server time
*
* Returns a Unix timestamp (seconds since 1970-01-01 00:00:00 UTC).
*
* You can use this to compensate for differences between your client's time and the
* server's time when working with last modified timestamps (revisions).
*
* @return int A unix timestamp
*/
public function getWikiTime()
{
return time();
}
// endregion
// region user
/**
* Login
*
* This will use the given credentials and attempt to login the user. This will set the
* appropriate cookies, which can be used for subsequent requests.
*
* Use of this mechanism is discouraged. Using token authentication is preferred.
*
* @param string $user The user name
* @param string $pass The password
* @return int If the login was successful
*/
public function login($user, $pass)
{
global $conf;
/** @var AuthPlugin $auth */
global $auth;
if (!$conf['useacl']) return 0;
if (!$auth instanceof AuthPlugin) return 0;
@session_start(); // reopen session for login
$ok = null;
if ($auth->canDo('external')) {
$ok = $auth->trustExternal($user, $pass, false);
}
if ($ok === null) {
$evdata = [
'user' => $user,
'password' => $pass,
'sticky' => false,
'silent' => true
];
$ok = Event::createAndTrigger('AUTH_LOGIN_CHECK', $evdata, 'auth_login_wrapper');
}
session_write_close(); // we're done with the session
return $ok;
}
/**
* Log off
*
* Attempt to log out the current user, deleting the appropriate cookies
*
* Use of this mechanism is discouraged. Using token authentication is preferred.
*
* @return int 0 on failure, 1 on success
*/
public function logoff()
{
global $conf;
global $auth;
if (!$conf['useacl']) return 0;
if (!$auth instanceof AuthPlugin) return 0;
auth_logoff();
return 1;
}
/**
* Info about the currently authenticated user
*
* @return User
*/
public function whoAmI()
{
return new User();
}
/**
* Check ACL Permissions
*
* This call allows to check the permissions for a given page/media and user/group combination.
* If no user/group is given, the current user is used.
*
* Read the link below to learn more about the permission levels.
*
* @link https://www.dokuwiki.org/acl#background_info
* @param string $page A page or media ID
* @param string $user username
* @param string[] $groups array of groups
* @return int permission level
* @throws RemoteException
*/
public function aclCheck($page, $user = '', $groups = [])
{
/** @var AuthPlugin $auth */
global $auth;
$page = $this->checkPage($page, 0, false, AUTH_NONE);
if ($user === '') {
return auth_quickaclcheck($page);
} else {
if ($groups === []) {
$userinfo = $auth->getUserData($user);
if ($userinfo === false) {
$groups = [];
} else {
$groups = $userinfo['grps'];
}
}
return auth_aclcheck($page, $user, $groups);
}
}
// endregion
// region pages
/**
* List all pages in the given namespace (and below)
*
* Setting the `depth` to `0` and the `namespace` to `""` will return all pages in the wiki.
*
* Note: author information is not available in this call.
*
* @param string $namespace The namespace to search. Empty string for root namespace
* @param int $depth How deep to search. 0 for all subnamespaces
* @param bool $hash Whether to include a MD5 hash of the page content
* @return Page[] A list of matching pages
* @todo might be a good idea to replace search_allpages with search_universal
*/
public function listPages($namespace = '', $depth = 1, $hash = false)
{
global $conf;
$namespace = cleanID($namespace);
// shortcut for all pages
if ($namespace === '' && $depth === 0) {
return $this->getAllPages($hash);
}
// search_allpages handles depth weird, we need to add the given namespace depth
if ($depth) {
$depth += substr_count($namespace, ':') + 1;
}
// run our search iterator to get the pages
$dir = utf8_encodeFN(str_replace(':', '/', $namespace));
$data = [];
$opts['skipacl'] = 0;
$opts['depth'] = $depth;
$opts['hash'] = $hash;
search($data, $conf['datadir'], 'search_allpages', $opts, $dir);
return array_map(static fn($item) => new Page(
$item['id'],
0, // we're searching current revisions only
$item['mtime'],
'', // not returned by search_allpages
$item['size'],
null, // not returned by search_allpages
$item['hash'] ?? ''
), $data);
}
/**
* Get all pages at once
*
* This is uses the page index and is quicker than iterating which is done in listPages()
*
* @return Page[] A list of all pages
* @see listPages()
*/
protected function getAllPages($hash = false)
{
$list = [];
$pages = idx_get_indexer()->getPages();
Sort::ksort($pages);
foreach (array_keys($pages) as $idx) {
$perm = auth_quickaclcheck($pages[$idx]);
if ($perm < AUTH_READ || isHiddenPage($pages[$idx]) || !page_exists($pages[$idx])) {
continue;
}
$page = new Page($pages[$idx], 0, 0, '', null, $perm);
if ($hash) $page->calculateHash();
$list[] = $page;
}
return $list;
}
/**
* Do a fulltext search
*
* This executes a full text search and returns the results. The query uses the standard
* DokuWiki search syntax.
*
* Snippets are provided for the first 15 results only. The title is either the first heading
* or the page id depending on the wiki's configuration.
*
* @link https://www.dokuwiki.org/search#syntax
* @param string $query The search query as supported by the DokuWiki search
* @return PageHit[] A list of matching pages
*/
public function searchPages($query)
{
$regex = [];
$data = ft_pageSearch($query, $regex);
$pages = [];
// prepare additional data
$idx = 0;
foreach ($data as $id => $score) {
if ($idx < FT_SNIPPET_NUMBER) {
$snippet = ft_snippet($id, $regex);
$idx++;
} else {
$snippet = '';
}
$pages[] = new PageHit(
$id,
$snippet,
$score,
useHeading('navigation') ? p_get_first_heading($id) : $id
);
}
return $pages;
}
/**
* Get recent page changes
*
* Returns a list of recent changes to wiki pages. The results can be limited to changes newer than
* a given timestamp.
*
* Only changes within the configured `$conf['recent']` range are returned. This is the default
* when no timestamp is given.
*
* @link https://www.dokuwiki.org/config:recent
* @param int $timestamp Only show changes newer than this unix timestamp
* @return PageChange[]
* @author Michael Klier <chi@chimeric.de>
* @author Michael Hamann <michael@content-space.de>
*/
public function getRecentPageChanges($timestamp = 0)
{
$recents = getRecentsSince($timestamp);
$changes = [];
foreach ($recents as $recent) {
$changes[] = new PageChange(
$recent['id'],
$recent['date'],
$recent['user'],
$recent['ip'],
$recent['sum'],
$recent['type'],
$recent['sizechange']
);
}
return $changes;
}
/**
* Get a wiki page's syntax
*
* Returns the syntax of the given page. When no revision is given, the current revision is returned.
*
* A non-existing page (or revision) will return an empty string usually. For the current revision
* a page template will be returned if configured.
*
* Read access is required for the page.
*
* @param string $page wiki page id
* @param int $rev Revision timestamp to access an older revision
* @return string the syntax of the page
* @throws AccessDeniedException
* @throws RemoteException
*/
public function getPage($page, $rev = 0)
{
$page = $this->checkPage($page, $rev, false);
$text = rawWiki($page, $rev);
if (!$text && !$rev) {
return pageTemplate($page);
} else {
return $text;
}
}
/**
* Return a wiki page rendered to HTML
*
* The page is rendered to HTML as it would be in the wiki. The HTML consist only of the data for the page
* content itself, no surrounding structural tags, header, footers, sidebars etc are returned.
*
* References in the HTML are relative to the wiki base URL unless the `canonical` configuration is set.
*
* If the page does not exist, an error is returned.
*
* @link https://www.dokuwiki.org/config:canonical
* @param string $page page id
* @param int $rev revision timestamp
* @return string Rendered HTML for the page
* @throws AccessDeniedException
* @throws RemoteException
*/
public function getPageHTML($page, $rev = 0)
{
$page = $this->checkPage($page, $rev);
return (string)p_wiki_xhtml($page, $rev, false);
}
/**
* Return some basic data about a page
*
* The call will return an error if the requested page does not exist.
*
* Read access is required for the page.
*
* @param string $page page id
* @param int $rev revision timestamp
* @param bool $author whether to include the author information
* @param bool $hash whether to include the MD5 hash of the page content
* @return Page
* @throws AccessDeniedException
* @throws RemoteException
*/
public function getPageInfo($page, $rev = 0, $author = false, $hash = false)
{
$page = $this->checkPage($page, $rev);
$result = new Page($page, $rev);
if ($author) $result->retrieveAuthor();
if ($hash) $result->calculateHash();
return $result;
}
/**
* Returns a list of available revisions of a given wiki page
*
* The number of returned pages is set by `$conf['recent']`, but non accessible revisions pages
* are skipped, so less than that may be returned.
*
* @link https://www.dokuwiki.org/config:recent
* @param string $page page id
* @param int $first skip the first n changelog lines, 0 starts at the current revision
* @return PageChange[]
* @throws AccessDeniedException
* @throws RemoteException
* @author Michael Klier <chi@chimeric.de>
*/
public function getPageHistory($page, $first = 0)
{
global $conf;
$page = $this->checkPage($page, 0, false);
$pagelog = new PageChangeLog($page);
$pagelog->setChunkSize(1024);
// old revisions are counted from 0, so we need to subtract 1 for the current one
$revisions = $pagelog->getRevisions($first - 1, $conf['recent']);
$result = [];
foreach ($revisions as $rev) {
if (!page_exists($page, $rev)) continue; // skip non-existing revisions
$info = $pagelog->getRevisionInfo($rev);
$result[] = new PageChange(
$page,
$rev,
$info['user'],
$info['ip'],
$info['sum'],
$info['type'],
$info['sizechange']
);
}
return $result;
}
/**
* Get a page's links
*
* This returns a list of links found in the given page. This includes internal, external and interwiki links
*
* If a link occurs multiple times on the page, it will be returned multiple times.
*
* Read access for the given page is needed and page has to exist.
*
* @param string $page page id
* @return Link[] A list of links found on the given page
* @throws AccessDeniedException
* @throws RemoteException
* @todo returning link titles would be a nice addition
* @todo hash handling seems not to be correct
* @todo maybe return the same link only once?
* @author Michael Klier <chi@chimeric.de>
*/
public function getPageLinks($page)
{
$page = $this->checkPage($page);
// resolve page instructions
$ins = p_cached_instructions(wikiFN($page));
// instantiate new Renderer - needed for interwiki links
$Renderer = new Doku_Renderer_xhtml();
$Renderer->interwiki = getInterwiki();
// parse instructions
$links = [];
foreach ($ins as $in) {
switch ($in[0]) {
case 'internallink':
$links[] = new Link('local', $in[1][0], wl($in[1][0]));
break;
case 'externallink':
$links[] = new Link('extern', $in[1][0], $in[1][0]);
break;
case 'interwikilink':
$url = $Renderer->_resolveInterWiki($in[1][2], $in[1][3]);
$links[] = new Link('interwiki', $in[1][0], $url);
break;
}
}
return ($links);
}
/**
* Get a page's backlinks
*
* A backlink is a wiki link on another page that links to the given page.
*
* Only links from pages readable by the current user are returned. The page itself
* needs to be readable. Otherwise an error is returned.
*
* @param string $page page id
* @return string[] A list of pages linking to the given page
* @throws AccessDeniedException
* @throws RemoteException
*/
public function getPageBackLinks($page)
{
$page = $this->checkPage($page, 0, false);
return ft_backlinks($page);
}
/**
* Lock the given set of pages
*
* This call will try to lock all given pages. It will return a list of pages that were
* successfully locked. If a page could not be locked, eg. because a different user is
* currently holding a lock, that page will be missing from the returned list.
*
* You should always ensure that the list of returned pages matches the given list of
* pages. It's up to you to decide how to handle failed locking.
*
* Note: you can only lock pages that you have write access for. It is possible to create
* a lock for a page that does not exist, yet.
*
* Note: it is not necessary to lock a page before saving it. The `savePage()` call will
* automatically lock and unlock the page for you. However if you plan to do related
* operations on multiple pages, locking them all at once beforehand can be useful.
*
* @param string[] $pages A list of pages to lock
* @return string[] A list of pages that were successfully locked
*/
public function lockPages($pages)
{
$locked = [];
foreach ($pages as $id) {
$id = cleanID($id);
if ($id === '') continue;
if (auth_quickaclcheck($id) < AUTH_EDIT || checklock($id)) {
continue;
}
lock($id);
$locked[] = $id;
}
return $locked;
}
/**
* Unlock the given set of pages
*
* This call will try to unlock all given pages. It will return a list of pages that were
* successfully unlocked. If a page could not be unlocked, eg. because a different user is
* currently holding a lock, that page will be missing from the returned list.
*
* You should always ensure that the list of returned pages matches the given list of
* pages. It's up to you to decide how to handle failed unlocking.
*
* Note: you can only unlock pages that you have write access for.
*
* @param string[] $pages A list of pages to unlock
* @return string[] A list of pages that were successfully unlocked
*/
public function unlockPages($pages)
{
$unlocked = [];
foreach ($pages as $id) {
$id = cleanID($id);
if ($id === '') continue;
if (auth_quickaclcheck($id) < AUTH_EDIT || !unlock($id)) {
continue;
}
$unlocked[] = $id;
}
return $unlocked;
}
/**
* Save a wiki page
*
* Saves the given wiki text to the given page. If the page does not exist, it will be created.
* Just like in the wiki, saving an empty text will delete the page.
*
* You need write permissions for the given page and the page may not be locked by another user.
*
* @param string $page page id
* @param string $text wiki text
* @param string $summary edit summary
* @param bool $isminor whether this is a minor edit
* @return bool Returns true on success
* @throws AccessDeniedException no write access for page
* @throws RemoteException no id, empty new page or locked
* @author Michael Klier <chi@chimeric.de>
*/
public function savePage($page, $text, $summary = '', $isminor = false)
{
global $TEXT;
global $lang;
$page = $this->checkPage($page, 0, false, AUTH_EDIT);
$TEXT = cleanText($text);
if (!page_exists($page) && trim($TEXT) == '') {
throw new RemoteException('Refusing to write an empty new wiki page', 132);
}
// Check, if page is locked
if (checklock($page)) {
throw new RemoteException('The page is currently locked', 133);
}
// SPAM check
if (checkwordblock()) {
throw new RemoteException('The page content was blocked', 134);
}
// autoset summary on new pages
if (!page_exists($page) && empty($summary)) {
$summary = $lang['created'];
}
// autoset summary on deleted pages
if (page_exists($page) && empty($TEXT) && empty($summary)) {
$summary = $lang['deleted'];
}
// FIXME auto set a summary in other cases "API Edit" might be a good idea?
lock($page);
saveWikiText($page, $TEXT, $summary, $isminor);
unlock($page);
// run the indexer if page wasn't indexed yet
idx_addPage($page);
return true;
}
/**
* Appends text to the end of a wiki page
*
* If the page does not exist, it will be created. If a page template for the non-existant
* page is configured, the given text will appended to that template.
*
* The call will create a new page revision.
*
* You need write permissions for the given page.
*
* @param string $page page id
* @param string $text wiki text
* @param string $summary edit summary
* @param bool $isminor whether this is a minor edit
* @return bool Returns true on success
* @throws AccessDeniedException
* @throws RemoteException
*/
public function appendPage($page, $text, $summary = '', $isminor = false)
{
$currentpage = $this->getPage($page);
if (!is_string($currentpage)) {
$currentpage = '';
}
return $this->savePage($page, $currentpage . $text, $summary, $isminor);
}
// endregion
// region media
/**
* List all media files in the given namespace (and below)
*
* Setting the `depth` to `0` and the `namespace` to `""` will return all media files in the wiki.
*
* When `pattern` is given, it needs to be a valid regular expression as understood by PHP's
* `preg_match()` including delimiters.
* The pattern is matched against the full media ID, including the namespace.
*
* @link https://www.php.net/manual/en/reference.pcre.pattern.syntax.php
* @param string $namespace The namespace to search. Empty string for root namespace
* @param string $pattern A regular expression to filter the returned files
* @param int $depth How deep to search. 0 for all subnamespaces
* @param bool $hash Whether to include a MD5 hash of the media content
* @return Media[]
* @author Gina Haeussge <osd@foosel.net>
*/
public function listMedia($namespace = '', $pattern = '', $depth = 1, $hash = false)
{
global $conf;
$namespace = cleanID($namespace);
$options = [
'skipacl' => 0,
'depth' => $depth,
'hash' => $hash,
'pattern' => $pattern,
];
$dir = utf8_encodeFN(str_replace(':', '/', $namespace));
$data = [];
search($data, $conf['mediadir'], 'search_media', $options, $dir);
return array_map(static fn($item) => new Media(
$item['id'],
0, // we're searching current revisions only
$item['mtime'],
$item['size'],
$item['perm'],
$item['isimg'],
$item['hash'] ?? ''
), $data);
}
/**
* Get recent media changes
*
* Returns a list of recent changes to media files. The results can be limited to changes newer than
* a given timestamp.
*
* Only changes within the configured `$conf['recent']` range are returned. This is the default
* when no timestamp is given.
*
* @link https://www.dokuwiki.org/config:recent
* @param int $timestamp Only show changes newer than this unix timestamp
* @return MediaChange[]
* @author Michael Klier <chi@chimeric.de>
* @author Michael Hamann <michael@content-space.de>
*/
public function getRecentMediaChanges($timestamp = 0)
{
$recents = getRecentsSince($timestamp, null, '', RECENTS_MEDIA_CHANGES);
$changes = [];
foreach ($recents as $recent) {
$changes[] = new MediaChange(
$recent['id'],
$recent['date'],
$recent['user'],
$recent['ip'],
$recent['sum'],
$recent['type'],
$recent['sizechange']
);
}
return $changes;
}
/**
* Get a media file's content
*
* Returns the content of the given media file. When no revision is given, the current revision is returned.
*
* @link https://en.wikipedia.org/wiki/Base64
* @param string $media file id
* @param int $rev revision timestamp
* @return string Base64 encoded media file contents
* @throws AccessDeniedException no permission for media
* @throws RemoteException not exist
* @author Gina Haeussge <osd@foosel.net>
*
*/
public function getMedia($media, $rev = 0)
{
$media = cleanID($media);
if (auth_quickaclcheck($media) < AUTH_READ) {
throw new AccessDeniedException('You are not allowed to read this media file', 211);
}
$file = mediaFN($media, $rev);
if (!@ file_exists($file)) {
throw new RemoteException('The requested media file (revision) does not exist', 221);
}
$data = io_readFile($file, false);
return base64_encode($data);
}
/**
* Return info about a media file
*
* The call will return an error if the requested media file does not exist.
*
* Read access is required for the media file.
*
* @param string $media file id
* @param int $rev revision timestamp
* @param bool $author whether to include the author information
* @param bool $hash whether to include the MD5 hash of the media content
* @return Media
* @throws AccessDeniedException no permission for media
* @throws RemoteException if not exist
* @author Gina Haeussge <osd@foosel.net>
*/
public function getMediaInfo($media, $rev = 0, $author = false, $hash = false)
{
$media = cleanID($media);
if (auth_quickaclcheck($media) < AUTH_READ) {
throw new AccessDeniedException('You are not allowed to read this media file', 211);
}
if (!media_exists($media, $rev)) {
throw new RemoteException('The requested media file does not exist', 221);
}
$info = new Media($media, $rev);
if ($hash) $info->calculateHash();
if ($author) $info->retrieveAuthor();
return $info;
}
/**
* Uploads a file to the wiki
*
* The file data has to be passed as a base64 encoded string.
*
* @link https://en.wikipedia.org/wiki/Base64
* @param string $media media id
* @param string $base64 Base64 encoded file contents
* @param bool $overwrite Should an existing file be overwritten?
* @return bool Should always be true
* @throws RemoteException
* @author Michael Klier <chi@chimeric.de>
*/
public function saveMedia($media, $base64, $overwrite = false)
{
$media = cleanID($media);
$auth = auth_quickaclcheck(getNS($media) . ':*');
if ($media === '') {
throw new RemoteException('Empty or invalid media ID given', 231);
}
// clean up base64 encoded data
$base64 = strtr($base64, [
"\n" => '', // strip newlines
"\r" => '', // strip carriage returns
'-' => '+', // RFC4648 base64url
'_' => '/', // RFC4648 base64url
' ' => '+', // JavaScript data uri
]);
$data = base64_decode($base64, true);
if ($data === false) {
throw new RemoteException('Invalid base64 encoded data', 234);
}
if ($data === '') {
throw new RemoteException('Empty file given', 235);
}
// save temporary file
global $conf;
$ftmp = $conf['tmpdir'] . '/' . md5($media . clientIP());
@unlink($ftmp);
io_saveFile($ftmp, $data);
$res = media_save(['name' => $ftmp], $media, $overwrite, $auth, 'rename');
if (is_array($res)) {
throw new RemoteException('Failed to save media: ' . $res[0], 236);
}
return (bool)$res; // should always be true at this point
}
/**
* Deletes a file from the wiki
*
* You need to have delete permissions for the file.
*
* @param string $media media id
* @return bool Should always be true
* @throws AccessDeniedException no permissions
* @throws RemoteException file in use or not deleted
* @author Gina Haeussge <osd@foosel.net>
*
*/
public function deleteMedia($media)
{
$media = cleanID($media);
$auth = auth_quickaclcheck($media);
$res = media_delete($media, $auth);
if ($res & DOKU_MEDIA_DELETED) {
return true;
} elseif ($res & DOKU_MEDIA_NOT_AUTH) {
throw new AccessDeniedException('You are not allowed to delete this media file', 212);
} elseif ($res & DOKU_MEDIA_INUSE) {
throw new RemoteException('Media file is still referenced', 232);
} elseif (!media_exists($media)) {
throw new RemoteException('The media file requested to delete does not exist', 221);
} else {
throw new RemoteException('Failed to delete media file', 233);
}
}
// endregion
/**
* Convenience method for page checks
*
* This method will perform multiple tasks:
*
* - clean the given page id
* - disallow an empty page id
* - check if the page exists (unless disabled)
* - check if the user has the required access level (pass AUTH_NONE to skip)
*
* @param string $id page id
* @param int $rev page revision
* @param bool $existCheck
* @param int $minAccess
* @return string the cleaned page id
* @throws AccessDeniedException
* @throws RemoteException
*/
private function checkPage($id, $rev = 0, $existCheck = true, $minAccess = AUTH_READ)
{
$id = cleanID($id);
if ($id === '') {
throw new RemoteException('Empty or invalid page ID given', 131);
}
if ($existCheck && !page_exists($id, $rev)) {
throw new RemoteException('The requested page (revision) does not exist', 121);
}
if ($minAccess && auth_quickaclcheck($id) < $minAccess) {
throw new AccessDeniedException('You are not allowed to read this page', 111);
}
return $id;
}
}
|