File.php 3.2 KB

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