You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

251 lines
7.3 KiB

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