file.php 6.8 KB

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