File.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Andreas Fischer <bantu@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Björn Schießle <bjoern@schiessle.org>
  9. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Gapczynski <GapczynskiM@gmail.com>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <vincent@nextcloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OCA\Files_Sharing\ShareBackend;
  35. use OCA\FederatedFileSharing\FederatedShareProvider;
  36. use OCP\Share\IShare;
  37. use Psr\Log\LoggerInterface;
  38. class File implements \OCP\Share_Backend_File_Dependent {
  39. public const FORMAT_SHARED_STORAGE = 0;
  40. public const FORMAT_GET_FOLDER_CONTENTS = 1;
  41. public const FORMAT_FILE_APP_ROOT = 2;
  42. public const FORMAT_OPENDIR = 3;
  43. public const FORMAT_GET_ALL = 4;
  44. public const FORMAT_PERMISSIONS = 5;
  45. public const FORMAT_TARGET_NAMES = 6;
  46. private $path;
  47. /** @var FederatedShareProvider */
  48. private $federatedShareProvider;
  49. public function __construct(FederatedShareProvider $federatedShareProvider = null) {
  50. if ($federatedShareProvider) {
  51. $this->federatedShareProvider = $federatedShareProvider;
  52. } else {
  53. $this->federatedShareProvider = \OC::$server->query(FederatedShareProvider::class);
  54. }
  55. }
  56. public function isValidSource($itemSource, $uidOwner) {
  57. try {
  58. $path = \OC\Files\Filesystem::getPath($itemSource);
  59. // FIXME: attributes should not be set here,
  60. // keeping this pattern for now to avoid unexpected
  61. // regressions
  62. $this->path = \OC\Files\Filesystem::normalizePath(basename($path));
  63. return true;
  64. } catch (\OCP\Files\NotFoundException $e) {
  65. return false;
  66. }
  67. }
  68. public function getFilePath($itemSource, $uidOwner) {
  69. if (isset($this->path)) {
  70. $path = $this->path;
  71. $this->path = null;
  72. return $path;
  73. } else {
  74. try {
  75. $path = \OC\Files\Filesystem::getPath($itemSource);
  76. return $path;
  77. } catch (\OCP\Files\NotFoundException $e) {
  78. return false;
  79. }
  80. }
  81. }
  82. /**
  83. * create unique target
  84. *
  85. * @param string $itemSource
  86. * @param string $shareWith
  87. * @param array $exclude (optional)
  88. * @return string
  89. */
  90. public function generateTarget($itemSource, $shareWith, $exclude = null) {
  91. $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder();
  92. $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($itemSource));
  93. // for group shares we return the target right away
  94. if ($shareWith === false) {
  95. return $target;
  96. }
  97. \OC\Files\Filesystem::initMountPoints($shareWith);
  98. $view = new \OC\Files\View('/' . $shareWith . '/files');
  99. if (!$view->is_dir($shareFolder)) {
  100. $dir = '';
  101. $subdirs = explode('/', $shareFolder);
  102. foreach ($subdirs as $subdir) {
  103. $dir = $dir . '/' . $subdir;
  104. if (!$view->is_dir($dir)) {
  105. $view->mkdir($dir);
  106. }
  107. }
  108. }
  109. $excludeList = is_array($exclude) ? $exclude : [];
  110. return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view);
  111. }
  112. public function formatItems($items, $format, $parameters = null) {
  113. if ($format === self::FORMAT_SHARED_STORAGE) {
  114. // Only 1 item should come through for this format call
  115. $item = array_shift($items);
  116. return [
  117. 'parent' => $item['parent'],
  118. 'path' => $item['path'],
  119. 'storage' => $item['storage'],
  120. 'permissions' => $item['permissions'],
  121. 'uid_owner' => $item['uid_owner'],
  122. ];
  123. } elseif ($format === self::FORMAT_GET_FOLDER_CONTENTS) {
  124. $files = [];
  125. foreach ($items as $item) {
  126. $file = [];
  127. $file['fileid'] = $item['file_source'];
  128. $file['storage'] = $item['storage'];
  129. $file['path'] = $item['file_target'];
  130. $file['parent'] = $item['file_parent'];
  131. $file['name'] = basename($item['file_target']);
  132. $file['mimetype'] = $item['mimetype'];
  133. $file['mimepart'] = $item['mimepart'];
  134. $file['mtime'] = $item['mtime'];
  135. $file['encrypted'] = $item['encrypted'];
  136. $file['etag'] = $item['etag'];
  137. $file['uid_owner'] = $item['uid_owner'];
  138. $file['displayname_owner'] = $item['displayname_owner'];
  139. $storage = \OC\Files\Filesystem::getStorage('/');
  140. $cache = $storage->getCache();
  141. $file['size'] = $item['size'];
  142. $files[] = $file;
  143. }
  144. return $files;
  145. } elseif ($format === self::FORMAT_OPENDIR) {
  146. $files = [];
  147. foreach ($items as $item) {
  148. $files[] = basename($item['file_target']);
  149. }
  150. return $files;
  151. } elseif ($format === self::FORMAT_GET_ALL) {
  152. $ids = [];
  153. foreach ($items as $item) {
  154. $ids[] = $item['file_source'];
  155. }
  156. return $ids;
  157. } elseif ($format === self::FORMAT_PERMISSIONS) {
  158. $filePermissions = [];
  159. foreach ($items as $item) {
  160. $filePermissions[$item['file_source']] = $item['permissions'];
  161. }
  162. return $filePermissions;
  163. } elseif ($format === self::FORMAT_TARGET_NAMES) {
  164. $targets = [];
  165. foreach ($items as $item) {
  166. $targets[] = $item['file_target'];
  167. }
  168. return $targets;
  169. }
  170. return [];
  171. }
  172. /**
  173. * check if server2server share is enabled
  174. *
  175. * @param int $shareType
  176. * @return boolean
  177. */
  178. public function isShareTypeAllowed($shareType) {
  179. if ($shareType === IShare::TYPE_REMOTE) {
  180. return $this->federatedShareProvider->isOutgoingServer2serverShareEnabled();
  181. }
  182. if ($shareType === IShare::TYPE_REMOTE_GROUP) {
  183. return $this->federatedShareProvider->isOutgoingServer2serverGroupShareEnabled();
  184. }
  185. return true;
  186. }
  187. /**
  188. * resolve reshares to return the correct source item
  189. * @param array $source
  190. * @return array source item
  191. */
  192. protected static function resolveReshares($source) {
  193. if (isset($source['parent'])) {
  194. $parent = $source['parent'];
  195. while (isset($parent)) {
  196. $qb = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  197. $qb->select('parent', 'uid_owner')
  198. ->from('share')
  199. ->where(
  200. $qb->expr()->eq('id', $qb->createNamedParameter($parent))
  201. );
  202. $result = $qb->execute();
  203. $item = $result->fetch();
  204. $result->closeCursor();
  205. if (isset($item['parent'])) {
  206. $parent = $item['parent'];
  207. } else {
  208. $fileOwner = $item['uid_owner'];
  209. break;
  210. }
  211. }
  212. } else {
  213. $fileOwner = $source['uid_owner'];
  214. }
  215. if (isset($fileOwner)) {
  216. $source['fileOwner'] = $fileOwner;
  217. } else {
  218. \OCP\Server::get(LoggerInterface::class)->error('No owner found for reshare', ['app' => 'files_sharing']);
  219. }
  220. return $source;
  221. }
  222. /**
  223. * @param string $target
  224. * @param array $share
  225. * @return array|false source item
  226. */
  227. public static function getSource($target, $share) {
  228. if ($share['item_type'] === 'folder' && $target !== '') {
  229. // note: in case of ext storage mount points the path might be empty
  230. // which would cause a leading slash to appear
  231. $share['path'] = ltrim($share['path'] . '/' . $target, '/');
  232. }
  233. return self::resolveReshares($share);
  234. }
  235. }