file.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle, Michael Gapczynski
  6. * @copyright 2012 Michael Gapczynski <mtgap@owncloud.com>
  7. * 2014 Bjoern Schiessle <schiessle@owncloud.com>
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. class OC_Share_Backend_File implements OCP\Share_Backend_File_Dependent {
  23. const FORMAT_SHARED_STORAGE = 0;
  24. const FORMAT_GET_FOLDER_CONTENTS = 1;
  25. const FORMAT_FILE_APP_ROOT = 2;
  26. const FORMAT_OPENDIR = 3;
  27. const FORMAT_GET_ALL = 4;
  28. const FORMAT_PERMISSIONS = 5;
  29. const FORMAT_TARGET_NAMES = 6;
  30. private $path;
  31. public function isValidSource($itemSource, $uidOwner) {
  32. $path = \OC\Files\Filesystem::getPath($itemSource);
  33. if ($path) {
  34. // FIXME: attributes should not be set here,
  35. // keeping this pattern for now to avoid unexpected
  36. // regressions
  37. $this->path = \OC\Files\Filesystem::normalizePath(basename($path));
  38. return true;
  39. }
  40. return false;
  41. }
  42. public function getFilePath($itemSource, $uidOwner) {
  43. if (isset($this->path)) {
  44. $path = $this->path;
  45. $this->path = null;
  46. return $path;
  47. } else {
  48. $path = \OC\Files\Filesystem::getPath($itemSource);
  49. if ($path) {
  50. return $path;
  51. }
  52. }
  53. return false;
  54. }
  55. /**
  56. * create unique target
  57. * @param string $filePath
  58. * @param string $shareWith
  59. * @param array $exclude (optional)
  60. * @return string
  61. */
  62. public function generateTarget($filePath, $shareWith, $exclude = null) {
  63. $shareFolder = \OCA\Files_Sharing\Helper::getShareFolder();
  64. $target = \OC\Files\Filesystem::normalizePath($shareFolder . '/' . basename($filePath));
  65. // for group shares we return the target right away
  66. if ($shareWith === false) {
  67. return $target;
  68. }
  69. \OC\Files\Filesystem::initMountPoints($shareWith);
  70. $view = new \OC\Files\View('/' . $shareWith . '/files');
  71. if (!$view->is_dir($shareFolder)) {
  72. $dir = '';
  73. $subdirs = explode('/', $shareFolder);
  74. foreach ($subdirs as $subdir) {
  75. $dir = $dir . '/' . $subdir;
  76. if (!$view->is_dir($dir)) {
  77. $view->mkdir($dir);
  78. }
  79. }
  80. }
  81. $excludeList = (is_array($exclude)) ? $exclude : array();
  82. return \OCA\Files_Sharing\Helper::generateUniqueTarget($target, $excludeList, $view);
  83. }
  84. public function formatItems($items, $format, $parameters = null) {
  85. if ($format == self::FORMAT_SHARED_STORAGE) {
  86. // Only 1 item should come through for this format call
  87. return array(
  88. 'parent' => $items[key($items)]['parent'],
  89. 'path' => $items[key($items)]['path'],
  90. 'storage' => $items[key($items)]['storage'],
  91. 'permissions' => $items[key($items)]['permissions'],
  92. 'uid_owner' => $items[key($items)]['uid_owner'],
  93. );
  94. } else if ($format == self::FORMAT_GET_FOLDER_CONTENTS) {
  95. $files = array();
  96. foreach ($items as $item) {
  97. $file = array();
  98. $file['fileid'] = $item['file_source'];
  99. $file['storage'] = $item['storage'];
  100. $file['path'] = $item['file_target'];
  101. $file['parent'] = $item['file_parent'];
  102. $file['name'] = basename($item['file_target']);
  103. $file['mimetype'] = $item['mimetype'];
  104. $file['mimepart'] = $item['mimepart'];
  105. $file['mtime'] = $item['mtime'];
  106. $file['encrypted'] = $item['encrypted'];
  107. $file['etag'] = $item['etag'];
  108. $file['uid_owner'] = $item['uid_owner'];
  109. $file['displayname_owner'] = $item['displayname_owner'];
  110. $storage = \OC\Files\Filesystem::getStorage('/');
  111. $cache = $storage->getCache();
  112. if ($item['encrypted'] or ($item['unencrypted_size'] > 0 and $cache->getMimetype($item['mimetype']) === 'httpd/unix-directory')) {
  113. $file['size'] = $item['unencrypted_size'];
  114. $file['encrypted_size'] = $item['size'];
  115. } else {
  116. $file['size'] = $item['size'];
  117. }
  118. $files[] = $file;
  119. }
  120. return $files;
  121. } else if ($format == self::FORMAT_OPENDIR) {
  122. $files = array();
  123. foreach ($items as $item) {
  124. $files[] = basename($item['file_target']);
  125. }
  126. return $files;
  127. } else if ($format == self::FORMAT_GET_ALL) {
  128. $ids = array();
  129. foreach ($items as $item) {
  130. $ids[] = $item['file_source'];
  131. }
  132. return $ids;
  133. } else if ($format === self::FORMAT_PERMISSIONS) {
  134. $filePermissions = array();
  135. foreach ($items as $item) {
  136. $filePermissions[$item['file_source']] = $item['permissions'];
  137. }
  138. return $filePermissions;
  139. } else if ($format === self::FORMAT_TARGET_NAMES) {
  140. $targets = array();
  141. foreach ($items as $item) {
  142. $targets[] = $item['file_target'];
  143. }
  144. return $targets;
  145. }
  146. return array();
  147. }
  148. /**
  149. * resolve reshares to return the correct source item
  150. * @param array $source
  151. * @return array source item
  152. */
  153. protected static function resolveReshares($source) {
  154. if (isset($source['parent'])) {
  155. $parent = $source['parent'];
  156. while (isset($parent)) {
  157. $query = \OC_DB::prepare('SELECT `parent`, `uid_owner` FROM `*PREFIX*share` WHERE `id` = ?', 1);
  158. $item = $query->execute(array($parent))->fetchRow();
  159. if (isset($item['parent'])) {
  160. $parent = $item['parent'];
  161. } else {
  162. $fileOwner = $item['uid_owner'];
  163. break;
  164. }
  165. }
  166. } else {
  167. $fileOwner = $source['uid_owner'];
  168. }
  169. if (isset($fileOwner)) {
  170. $source['fileOwner'] = $fileOwner;
  171. } else {
  172. \OCP\Util::writeLog('files_sharing', "No owner found for reshare", \OCP\Util::ERROR);
  173. }
  174. return $source;
  175. }
  176. /**
  177. * @param string $target
  178. * @param string $mountPoint
  179. * @param string $itemType
  180. * @return array|false source item
  181. */
  182. public static function getSource($target, $mountPoint, $itemType) {
  183. if ($itemType === 'folder') {
  184. $source = \OCP\Share::getItemSharedWith('folder', $mountPoint, \OC_Share_Backend_File::FORMAT_SHARED_STORAGE);
  185. if ($source && $target !== '') {
  186. $source['path'] = $source['path'].'/'.$target;
  187. }
  188. } else {
  189. $source = \OCP\Share::getItemSharedWith('file', $mountPoint, \OC_Share_Backend_File::FORMAT_SHARED_STORAGE);
  190. }
  191. if ($source) {
  192. return self::resolveReshares($source);
  193. }
  194. \OCP\Util::writeLog('files_sharing', 'File source not found for: '.$target, \OCP\Util::DEBUG);
  195. return false;
  196. }
  197. }