File.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. * @author Thomas Müller <thomas.mueller@tmit.eu>
  9. * @author Vincent Petry <pvince81@owncloud.com>
  10. *
  11. * @license AGPL-3.0
  12. *
  13. * This code is free software: you can redistribute it and/or modify
  14. * it under the terms of the GNU Affero General Public License, version 3,
  15. * as published by the Free Software Foundation.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License, version 3,
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>
  24. *
  25. */
  26. namespace OC\Encryption;
  27. use OC\Cache\CappedMemoryCache;
  28. use OCP\Files\IRootFolder;
  29. use OCP\Files\NotFoundException;
  30. use OCP\Share\IManager;
  31. class File implements \OCP\Encryption\IFile {
  32. /** @var Util */
  33. protected $util;
  34. /** @var IRootFolder */
  35. private $rootFolder;
  36. /** @var IManager */
  37. private $shareManager;
  38. /**
  39. * cache results of already checked folders
  40. *
  41. * @var array
  42. */
  43. protected $cache;
  44. public function __construct(Util $util,
  45. IRootFolder $rootFolder,
  46. IManager $shareManager) {
  47. $this->util = $util;
  48. $this->cache = new CappedMemoryCache();
  49. $this->rootFolder = $rootFolder;
  50. $this->shareManager = $shareManager;
  51. }
  52. /**
  53. * get list of users with access to the file
  54. *
  55. * @param string $path to the file
  56. * @return array ['users' => $uniqueUserIds, 'public' => $public]
  57. */
  58. public function getAccessList($path) {
  59. // Make sure that a share key is generated for the owner too
  60. list($owner, $ownerPath) = $this->util->getUidAndFilename($path);
  61. // always add owner to the list of users with access to the file
  62. $userIds = array($owner);
  63. if (!$this->util->isFile($owner . '/' . $ownerPath)) {
  64. return array('users' => $userIds, 'public' => false);
  65. }
  66. $ownerPath = substr($ownerPath, strlen('/files'));
  67. $userFolder = $this->rootFolder->getUserFolder($owner);
  68. try {
  69. $file = $userFolder->get($ownerPath);
  70. } catch (NotFoundException $e) {
  71. $file = null;
  72. }
  73. $ownerPath = $this->util->stripPartialFileExtension($ownerPath);
  74. // first get the shares for the parent and cache the result so that we don't
  75. // need to check all parents for every file
  76. $parent = dirname($ownerPath);
  77. $parentNode = $userFolder->get($parent);
  78. if (isset($this->cache[$parent])) {
  79. $resultForParents = $this->cache[$parent];
  80. } else {
  81. $resultForParents = $this->shareManager->getAccessList($parentNode);
  82. $this->cache[$parent] = $resultForParents;
  83. }
  84. $userIds = array_merge($userIds, $resultForParents['users']);
  85. $public = $resultForParents['public'] || $resultForParents['remote'];
  86. // Find out who, if anyone, is sharing the file
  87. if ($file !== null) {
  88. $resultForFile = $this->shareManager->getAccessList($file, false);
  89. $userIds = array_merge($userIds, $resultForFile['users']);
  90. $public = $resultForFile['public'] || $resultForFile['remote'] || $public;
  91. }
  92. // check if it is a group mount
  93. if (\OCP\App::isEnabled("files_external")) {
  94. $mounts = \OC_Mount_Config::getSystemMountPoints();
  95. foreach ($mounts as $mount) {
  96. if ($mount['mountpoint'] == substr($ownerPath, 1, strlen($mount['mountpoint']))) {
  97. $mountedFor = $this->util->getUserWithAccessToMountPoint($mount['applicable']['users'], $mount['applicable']['groups']);
  98. $userIds = array_merge($userIds, $mountedFor);
  99. }
  100. }
  101. }
  102. // Remove duplicate UIDs
  103. $uniqueUserIds = array_unique($userIds);
  104. return array('users' => $uniqueUserIds, 'public' => $public);
  105. }
  106. }