File.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\ShareBackend;
  8. use OCA\FederatedFileSharing\FederatedShareProvider;
  9. use OCP\Share\IShare;
  10. use Psr\Log\LoggerInterface;
  11. class File implements \OCP\Share_Backend_File_Dependent {
  12. public const FORMAT_SHARED_STORAGE = 0;
  13. public const FORMAT_GET_FOLDER_CONTENTS = 1;
  14. public const FORMAT_FILE_APP_ROOT = 2;
  15. public const FORMAT_OPENDIR = 3;
  16. public const FORMAT_GET_ALL = 4;
  17. public const FORMAT_PERMISSIONS = 5;
  18. public const FORMAT_TARGET_NAMES = 6;
  19. private $path;
  20. /** @var FederatedShareProvider */
  21. private $federatedShareProvider;
  22. public function __construct(?FederatedShareProvider $federatedShareProvider = null) {
  23. if ($federatedShareProvider) {
  24. $this->federatedShareProvider = $federatedShareProvider;
  25. } else {
  26. $this->federatedShareProvider = \OC::$server->query(FederatedShareProvider::class);
  27. }
  28. }
  29. public function isValidSource($itemSource, $uidOwner) {
  30. try {
  31. $path = \OC\Files\Filesystem::getPath($itemSource);
  32. // FIXME: attributes should not be set here,
  33. // keeping this pattern for now to avoid unexpected
  34. // regressions
  35. $this->path = \OC\Files\Filesystem::normalizePath(basename($path));
  36. return true;
  37. } catch (\OCP\Files\NotFoundException $e) {
  38. return false;
  39. }
  40. }
  41. public function getFilePath($itemSource, $uidOwner) {
  42. if (isset($this->path)) {
  43. $path = $this->path;
  44. $this->path = null;
  45. return $path;
  46. } else {
  47. try {
  48. $path = \OC\Files\Filesystem::getPath($itemSource);
  49. return $path;
  50. } catch (\OCP\Files\NotFoundException $e) {
  51. return false;
  52. }
  53. }
  54. }
  55. /**
  56. * create unique target
  57. *
  58. * @param string $itemSource
  59. * @param string $shareWith
  60. * @param array $exclude (optional)
  61. * @return string
  62. */
  63. public function generateTarget($itemSource, $shareWith, $exclude = null) {
  64. $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder();
  65. $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($itemSource));
  66. // for group shares we return the target right away
  67. if ($shareWith === false) {
  68. return $target;
  69. }
  70. \OC\Files\Filesystem::initMountPoints($shareWith);
  71. $view = new \OC\Files\View('/' . $shareWith . '/files');
  72. if (!$view->is_dir($shareFolder)) {
  73. $dir = '';
  74. $subdirs = explode('/', $shareFolder);
  75. foreach ($subdirs as $subdir) {
  76. $dir = $dir . '/' . $subdir;
  77. if (!$view->is_dir($dir)) {
  78. $view->mkdir($dir);
  79. }
  80. }
  81. }
  82. $excludeList = is_array($exclude) ? $exclude : [];
  83. return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view);
  84. }
  85. public function formatItems($items, $format, $parameters = null) {
  86. if ($format === self::FORMAT_SHARED_STORAGE) {
  87. // Only 1 item should come through for this format call
  88. $item = array_shift($items);
  89. return [
  90. 'parent' => $item['parent'],
  91. 'path' => $item['path'],
  92. 'storage' => $item['storage'],
  93. 'permissions' => $item['permissions'],
  94. 'uid_owner' => $item['uid_owner'],
  95. ];
  96. } elseif ($format === self::FORMAT_GET_FOLDER_CONTENTS) {
  97. $files = [];
  98. foreach ($items as $item) {
  99. $file = [];
  100. $file['fileid'] = $item['file_source'];
  101. $file['storage'] = $item['storage'];
  102. $file['path'] = $item['file_target'];
  103. $file['parent'] = $item['file_parent'];
  104. $file['name'] = basename($item['file_target']);
  105. $file['mimetype'] = $item['mimetype'];
  106. $file['mimepart'] = $item['mimepart'];
  107. $file['mtime'] = $item['mtime'];
  108. $file['encrypted'] = $item['encrypted'];
  109. $file['etag'] = $item['etag'];
  110. $file['uid_owner'] = $item['uid_owner'];
  111. $file['displayname_owner'] = $item['displayname_owner'];
  112. $storage = \OC\Files\Filesystem::getStorage('/');
  113. $cache = $storage->getCache();
  114. $file['size'] = $item['size'];
  115. $files[] = $file;
  116. }
  117. return $files;
  118. } elseif ($format === self::FORMAT_OPENDIR) {
  119. $files = [];
  120. foreach ($items as $item) {
  121. $files[] = basename($item['file_target']);
  122. }
  123. return $files;
  124. } elseif ($format === self::FORMAT_GET_ALL) {
  125. $ids = [];
  126. foreach ($items as $item) {
  127. $ids[] = $item['file_source'];
  128. }
  129. return $ids;
  130. } elseif ($format === self::FORMAT_PERMISSIONS) {
  131. $filePermissions = [];
  132. foreach ($items as $item) {
  133. $filePermissions[$item['file_source']] = $item['permissions'];
  134. }
  135. return $filePermissions;
  136. } elseif ($format === self::FORMAT_TARGET_NAMES) {
  137. $targets = [];
  138. foreach ($items as $item) {
  139. $targets[] = $item['file_target'];
  140. }
  141. return $targets;
  142. }
  143. return [];
  144. }
  145. /**
  146. * check if server2server share is enabled
  147. *
  148. * @param int $shareType
  149. * @return boolean
  150. */
  151. public function isShareTypeAllowed($shareType) {
  152. if ($shareType === IShare::TYPE_REMOTE) {
  153. return $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
  154. }
  155. if ($shareType === IShare::TYPE_REMOTE_GROUP) {
  156. return $this->federatedShareProvider->isOutgoingServer2serverGroupShareEnabled();
  157. }
  158. return true;
  159. }
  160. /**
  161. * resolve reshares to return the correct source item
  162. * @param array $source
  163. * @return array source item
  164. */
  165. protected static function resolveReshares($source) {
  166. if (isset($source['parent'])) {
  167. $parent = $source['parent'];
  168. while (isset($parent)) {
  169. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  170. $qb->select('parent', 'uid_owner')
  171. ->from('share')
  172. ->where(
  173. $qb->expr()->eq('id', $qb->createNamedParameter($parent))
  174. );
  175. $result = $qb->execute();
  176. $item = $result->fetch();
  177. $result->closeCursor();
  178. if (isset($item['parent'])) {
  179. $parent = $item['parent'];
  180. } else {
  181. $fileOwner = $item['uid_owner'];
  182. break;
  183. }
  184. }
  185. } else {
  186. $fileOwner = $source['uid_owner'];
  187. }
  188. if (isset($fileOwner)) {
  189. $source['fileOwner'] = $fileOwner;
  190. } else {
  191. \OCP\Server::get(LoggerInterface::class)->error('No owner found for reshare', ['app' => 'files_sharing']);
  192. }
  193. return $source;
  194. }
  195. /**
  196. * @param string $target
  197. * @param array $share
  198. * @return array|false source item
  199. */
  200. public static function getSource($target, $share) {
  201. if ($share['item_type'] === 'folder' && $target !== '') {
  202. // note: in case of ext storage mount points the path might be empty
  203. // which would cause a leading slash to appear
  204. $share['path'] = ltrim($share['path'] . '/' . $target, '/');
  205. }
  206. return self::resolveReshares($share);
  207. }
  208. }