Storage.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Björn Schießle <bjoern@schiessle.org>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Robin Appelman <robin@icewind.nl>
  8. * @author Vincent Petry <pvince81@owncloud.com>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OCA\Files_Trashbin;
  26. use OC\Files\Filesystem;
  27. use OC\Files\Storage\Wrapper\Wrapper;
  28. use OC\Files\View;
  29. use OCP\IUserManager;
  30. class Storage extends Wrapper {
  31. private $mountPoint;
  32. // remember already deleted files to avoid infinite loops if the trash bin
  33. // move files across storages
  34. private $deletedFiles = array();
  35. /**
  36. * Disable trash logic
  37. *
  38. * @var bool
  39. */
  40. private static $disableTrash = false;
  41. /**
  42. * remember which file/folder was moved out of s shared folder
  43. * in this case we want to add a copy to the owners trash bin
  44. *
  45. * @var array
  46. */
  47. private static $moveOutOfSharedFolder = [];
  48. /** @var IUserManager */
  49. private $userManager;
  50. /**
  51. * Storage constructor.
  52. *
  53. * @param array $parameters
  54. * @param IUserManager|null $userManager
  55. */
  56. public function __construct($parameters, IUserManager $userManager = null) {
  57. $this->mountPoint = $parameters['mountPoint'];
  58. $this->userManager = $userManager;
  59. parent::__construct($parameters);
  60. }
  61. /**
  62. * @internal
  63. */
  64. public static function preRenameHook($params) {
  65. // in cross-storage cases, a rename is a copy + unlink,
  66. // that last unlink must not go to trash, only exception:
  67. // if the file was moved from a shared storage to a local folder,
  68. // in this case the owner should get a copy in his trash bin so that
  69. // they can restore the files again
  70. $oldPath = $params['oldpath'];
  71. $newPath = dirname($params['newpath']);
  72. $currentUser = \OC::$server->getUserSession()->getUser();
  73. $fileMovedOutOfSharedFolder = false;
  74. try {
  75. if ($currentUser) {
  76. $currentUserId = $currentUser->getUID();
  77. $view = new View($currentUserId . '/files');
  78. $fileInfo = $view->getFileInfo($oldPath);
  79. if ($fileInfo) {
  80. $sourceStorage = $fileInfo->getStorage();
  81. $sourceOwner = $view->getOwner($oldPath);
  82. $targetOwner = $view->getOwner($newPath);
  83. if ($sourceOwner !== $targetOwner
  84. && $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')
  85. ) {
  86. $fileMovedOutOfSharedFolder = true;
  87. }
  88. }
  89. }
  90. } catch (\Exception $e) {
  91. // do nothing, in this case we just disable the trashbin and continue
  92. $logger = \OC::$server->getLogger();
  93. $logger->debug('Trashbin storage could not check if a file was moved out of a shared folder: ' . $e->getMessage());
  94. }
  95. if($fileMovedOutOfSharedFolder) {
  96. self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true;
  97. } else {
  98. self::$disableTrash = true;
  99. }
  100. }
  101. /**
  102. * @internal
  103. */
  104. public static function postRenameHook($params) {
  105. self::$disableTrash = false;
  106. }
  107. /**
  108. * Rename path1 to path2 by calling the wrapped storage.
  109. *
  110. * @param string $path1 first path
  111. * @param string $path2 second path
  112. * @return bool
  113. */
  114. public function rename($path1, $path2) {
  115. $result = $this->storage->rename($path1, $path2);
  116. if ($result === false) {
  117. // when rename failed, the post_rename hook isn't triggered,
  118. // but we still want to reenable the trash logic
  119. self::$disableTrash = false;
  120. }
  121. return $result;
  122. }
  123. /**
  124. * Deletes the given file by moving it into the trashbin.
  125. *
  126. * @param string $path path of file or folder to delete
  127. *
  128. * @return bool true if the operation succeeded, false otherwise
  129. */
  130. public function unlink($path) {
  131. if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
  132. $result = $this->doDelete($path, 'unlink', true);
  133. unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
  134. } else {
  135. $result = $this->doDelete($path, 'unlink');
  136. }
  137. return $result;
  138. }
  139. /**
  140. * Deletes the given folder by moving it into the trashbin.
  141. *
  142. * @param string $path path of folder to delete
  143. *
  144. * @return bool true if the operation succeeded, false otherwise
  145. */
  146. public function rmdir($path) {
  147. if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
  148. $result = $this->doDelete($path, 'rmdir', true);
  149. unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
  150. } else {
  151. $result = $this->doDelete($path, 'rmdir');
  152. }
  153. return $result;
  154. }
  155. /**
  156. * check if it is a file located in data/user/files only files in the
  157. * 'files' directory should be moved to the trash
  158. *
  159. * @param $path
  160. * @return bool
  161. */
  162. protected function shouldMoveToTrash($path){
  163. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  164. $parts = explode('/', $normalized);
  165. if (count($parts) < 4) {
  166. return false;
  167. }
  168. if ($this->userManager->userExists($parts[1]) && $parts[2] == 'files') {
  169. return true;
  170. }
  171. return false;
  172. }
  173. /**
  174. * Run the delete operation with the given method
  175. *
  176. * @param string $path path of file or folder to delete
  177. * @param string $method either "unlink" or "rmdir"
  178. * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder)
  179. *
  180. * @return bool true if the operation succeeded, false otherwise
  181. */
  182. private function doDelete($path, $method, $ownerOnly = false) {
  183. if (self::$disableTrash
  184. || !\OC_App::isEnabled('files_trashbin')
  185. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  186. || $this->shouldMoveToTrash($path) === false
  187. ) {
  188. return call_user_func_array([$this->storage, $method], [$path]);
  189. }
  190. // check permissions before we continue, this is especially important for
  191. // shared files
  192. if (!$this->isDeletable($path)) {
  193. return false;
  194. }
  195. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true);
  196. $result = true;
  197. $view = Filesystem::getView();
  198. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  199. $this->deletedFiles[$normalized] = $normalized;
  200. if ($filesPath = $view->getRelativePath($normalized)) {
  201. $filesPath = trim($filesPath, '/');
  202. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly);
  203. // in cross-storage cases the file will be copied
  204. // but not deleted, so we delete it here
  205. if ($result) {
  206. call_user_func_array([$this->storage, $method], [$path]);
  207. }
  208. } else {
  209. $result = call_user_func_array([$this->storage, $method], [$path]);
  210. }
  211. unset($this->deletedFiles[$normalized]);
  212. } else if ($this->storage->file_exists($path)) {
  213. $result = call_user_func_array([$this->storage, $method], [$path]);
  214. }
  215. return $result;
  216. }
  217. /**
  218. * Setup the storate wrapper callback
  219. */
  220. public static function setupStorage() {
  221. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  222. return new \OCA\Files_Trashbin\Storage(
  223. array('storage' => $storage, 'mountPoint' => $mountPoint),
  224. \OC::$server->getUserManager()
  225. );
  226. }, 1);
  227. }
  228. }