1
0

ViewOnly.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. // Check whether download-permission was denied (granted if not set)
  83. $attributes = $share->getAttributes();
  84. $canDownload = $attributes?->getAttribute('permissions', 'download');
  85. return $canDownload !== false;
  86. }
  87. }