File.php 7.5 KB

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