ViewOnly.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * @author Piotr Mrowczynski piotr@owncloud.com
  4. *
  5. * @copyright Copyright (c) 2019, ownCloud GmbH
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\Files_Sharing;
  22. use OCP\Files\File;
  23. use OCP\Files\Folder;
  24. use OCP\Files\Node;
  25. use OCP\Files\NotFoundException;
  26. /**
  27. * Handles restricting for download of files
  28. */
  29. class ViewOnly {
  30. /** @var Folder */
  31. private $userFolder;
  32. public function __construct(Folder $userFolder) {
  33. $this->userFolder = $userFolder;
  34. }
  35. /**
  36. * @param string[] $pathsToCheck
  37. * @return bool
  38. */
  39. public function check(array $pathsToCheck): bool {
  40. // If any of elements cannot be downloaded, prevent whole download
  41. foreach ($pathsToCheck as $file) {
  42. try {
  43. $info = $this->userFolder->get($file);
  44. if ($info instanceof File) {
  45. // access to filecache is expensive in the loop
  46. if (!$this->checkFileInfo($info)) {
  47. return false;
  48. }
  49. } elseif ($info instanceof Folder) {
  50. // get directory content is rather cheap query
  51. if (!$this->dirRecursiveCheck($info)) {
  52. return false;
  53. }
  54. }
  55. } catch (NotFoundException $e) {
  56. continue;
  57. }
  58. }
  59. return true;
  60. }
  61. /**
  62. * @param Folder $dirInfo
  63. * @return bool
  64. * @throws NotFoundException
  65. */
  66. private function dirRecursiveCheck(Folder $dirInfo): bool {
  67. if (!$this->checkFileInfo($dirInfo)) {
  68. return false;
  69. }
  70. // If any of elements cannot be downloaded, prevent whole download
  71. $files = $dirInfo->getDirectoryListing();
  72. foreach ($files as $file) {
  73. if ($file instanceof File) {
  74. if (!$this->checkFileInfo($file)) {
  75. return false;
  76. }
  77. } elseif ($file instanceof Folder) {
  78. return $this->dirRecursiveCheck($file);
  79. }
  80. }
  81. return true;
  82. }
  83. /**
  84. * @param Node $fileInfo
  85. * @return bool
  86. * @throws NotFoundException
  87. */
  88. private function checkFileInfo(Node $fileInfo): bool {
  89. // Restrict view-only to nodes which are shared
  90. $storage = $fileInfo->getStorage();
  91. if (!$storage->instanceOfStorage(SharedStorage::class)) {
  92. return true;
  93. }
  94. // Extract extra permissions
  95. /** @var \OCA\Files_Sharing\SharedStorage $storage */
  96. $share = $storage->getShare();
  97. $canDownload = true;
  98. // Check if read-only and on whether permission can download is both set and disabled.
  99. $attributes = $share->getAttributes();
  100. if ($attributes !== null) {
  101. $canDownload = $attributes->getAttribute('permissions', 'download');
  102. }
  103. if ($canDownload !== null && !$canDownload) {
  104. return false;
  105. }
  106. return true;
  107. }
  108. }