File.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 OC\Files\Filesystem;
  9. use OC\Files\View;
  10. use OCA\FederatedFileSharing\FederatedShareProvider;
  11. use OCA\Files_Sharing\Helper;
  12. use OCP\Files\NotFoundException;
  13. use OCP\Server;
  14. use OCP\Share\IShare;
  15. use OCP\Share_Backend_File_Dependent;
  16. use Psr\Log\LoggerInterface;
  17. class File implements Share_Backend_File_Dependent {
  18. public const FORMAT_SHARED_STORAGE = 0;
  19. public const FORMAT_GET_FOLDER_CONTENTS = 1;
  20. public const FORMAT_FILE_APP_ROOT = 2;
  21. public const FORMAT_OPENDIR = 3;
  22. public const FORMAT_GET_ALL = 4;
  23. public const FORMAT_PERMISSIONS = 5;
  24. public const FORMAT_TARGET_NAMES = 6;
  25. private $path;
  26. public function __construct(
  27. private ?FederatedShareProvider $federatedShareProvider = null,
  28. ) {
  29. if ($federatedShareProvider) {
  30. $this->federatedShareProvider = $federatedShareProvider;
  31. } else {
  32. $this->federatedShareProvider = \OC::$server->query(FederatedShareProvider::class);
  33. }
  34. }
  35. public function isValidSource($itemSource, $uidOwner) {
  36. try {
  37. $path = Filesystem::getPath($itemSource);
  38. // FIXME: attributes should not be set here,
  39. // keeping this pattern for now to avoid unexpected
  40. // regressions
  41. $this->path = Filesystem::normalizePath(basename($path));
  42. return true;
  43. } catch (NotFoundException $e) {
  44. return false;
  45. }
  46. }
  47. public function getFilePath($itemSource, $uidOwner) {
  48. if (isset($this->path)) {
  49. $path = $this->path;
  50. $this->path = null;
  51. return $path;
  52. } else {
  53. try {
  54. $path = Filesystem::getPath($itemSource);
  55. return $path;
  56. } catch (NotFoundException $e) {
  57. return false;
  58. }
  59. }
  60. }
  61. /**
  62. * create unique target
  63. *
  64. * @param string $itemSource
  65. * @param string $shareWith
  66. * @return string
  67. */
  68. public function generateTarget($itemSource, $shareWith) {
  69. $shareFolder = Helper::getShareFolder();
  70. $target = Filesystem::normalizePath($shareFolder . '/' . basename($itemSource));
  71. Filesystem::initMountPoints($shareWith);
  72. $view = new View('/' . $shareWith . '/files');
  73. if (!$view->is_dir($shareFolder)) {
  74. $dir = '';
  75. $subdirs = explode('/', $shareFolder);
  76. foreach ($subdirs as $subdir) {
  77. $dir = $dir . '/' . $subdir;
  78. if (!$view->is_dir($dir)) {
  79. $view->mkdir($dir);
  80. }
  81. }
  82. }
  83. return Helper::generateUniqueTarget($target, $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 = 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->executeQuery();
  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. 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. }