ViewOnly.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2019 ownCloud GmbH
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing;
  8. use OCP\Files\File;
  9. use OCP\Files\Folder;
  10. use OCP\Files\Node;
  11. use OCP\Files\NotFoundException;
  12. /**
  13. * Handles restricting for download of files
  14. */
  15. class ViewOnly {
  16. public function __construct(
  17. private Folder $userFolder,
  18. ) {
  19. }
  20. /**
  21. * @param string[] $pathsToCheck
  22. * @return bool
  23. */
  24. public function check(array $pathsToCheck): bool {
  25. // If any of elements cannot be downloaded, prevent whole download
  26. foreach ($pathsToCheck as $file) {
  27. try {
  28. $info = $this->userFolder->get($file);
  29. if ($info instanceof File) {
  30. // access to filecache is expensive in the loop
  31. if (!$this->checkFileInfo($info)) {
  32. return false;
  33. }
  34. } elseif ($info instanceof Folder) {
  35. // get directory content is rather cheap query
  36. if (!$this->dirRecursiveCheck($info)) {
  37. return false;
  38. }
  39. }
  40. } catch (NotFoundException $e) {
  41. continue;
  42. }
  43. }
  44. return true;
  45. }
  46. /**
  47. * @param Folder $dirInfo
  48. * @return bool
  49. * @throws NotFoundException
  50. */
  51. private function dirRecursiveCheck(Folder $dirInfo): bool {
  52. if (!$this->checkFileInfo($dirInfo)) {
  53. return false;
  54. }
  55. // If any of elements cannot be downloaded, prevent whole download
  56. $files = $dirInfo->getDirectoryListing();
  57. foreach ($files as $file) {
  58. if ($file instanceof File) {
  59. if (!$this->checkFileInfo($file)) {
  60. return false;
  61. }
  62. } elseif ($file instanceof Folder) {
  63. return $this->dirRecursiveCheck($file);
  64. }
  65. }
  66. return true;
  67. }
  68. /**
  69. * @param Node $fileInfo
  70. * @return bool
  71. * @throws NotFoundException
  72. */
  73. private function checkFileInfo(Node $fileInfo): bool {
  74. // Restrict view-only to nodes which are shared
  75. $storage = $fileInfo->getStorage();
  76. if (!$storage->instanceOfStorage(SharedStorage::class)) {
  77. return true;
  78. }
  79. // Extract extra permissions
  80. /** @var SharedStorage $storage */
  81. $share = $storage->getShare();
  82. $canDownload = true;
  83. // Check if read-only and on whether permission can download is both set and disabled.
  84. $attributes = $share->getAttributes();
  85. if ($attributes !== null) {
  86. $canDownload = $attributes->getAttribute('permissions', 'download');
  87. }
  88. if ($canDownload !== null && !$canDownload) {
  89. return false;
  90. }
  91. return true;
  92. }
  93. }