Storage.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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\Encryption\Exceptions\GenericEncryptionException;
  30. use OCP\ILogger;
  31. use OCP\IUserManager;
  32. class Storage extends Wrapper {
  33. private $mountPoint;
  34. // remember already deleted files to avoid infinite loops if the trash bin
  35. // move files across storages
  36. private $deletedFiles = array();
  37. /**
  38. * Disable trash logic
  39. *
  40. * @var bool
  41. */
  42. private static $disableTrash = false;
  43. /**
  44. * remember which file/folder was moved out of s shared folder
  45. * in this case we want to add a copy to the owners trash bin
  46. *
  47. * @var array
  48. */
  49. private static $moveOutOfSharedFolder = [];
  50. /** @var IUserManager */
  51. private $userManager;
  52. /** @var ILogger */
  53. private $logger;
  54. /**
  55. * Storage constructor.
  56. *
  57. * @param array $parameters
  58. * @param IUserManager|null $userManager
  59. */
  60. public function __construct($parameters,
  61. IUserManager $userManager = null,
  62. ILogger $logger = null) {
  63. $this->mountPoint = $parameters['mountPoint'];
  64. $this->userManager = $userManager;
  65. $this->logger = $logger;
  66. parent::__construct($parameters);
  67. }
  68. /**
  69. * @internal
  70. */
  71. public static function preRenameHook($params) {
  72. // in cross-storage cases, a rename is a copy + unlink,
  73. // that last unlink must not go to trash, only exception:
  74. // if the file was moved from a shared storage to a local folder,
  75. // in this case the owner should get a copy in his trash bin so that
  76. // they can restore the files again
  77. $oldPath = $params['oldpath'];
  78. $newPath = dirname($params['newpath']);
  79. $currentUser = \OC::$server->getUserSession()->getUser();
  80. $fileMovedOutOfSharedFolder = false;
  81. try {
  82. if ($currentUser) {
  83. $currentUserId = $currentUser->getUID();
  84. $view = new View($currentUserId . '/files');
  85. $fileInfo = $view->getFileInfo($oldPath);
  86. if ($fileInfo) {
  87. $sourceStorage = $fileInfo->getStorage();
  88. $sourceOwner = $view->getOwner($oldPath);
  89. $targetOwner = $view->getOwner($newPath);
  90. if ($sourceOwner !== $targetOwner
  91. && $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')
  92. ) {
  93. $fileMovedOutOfSharedFolder = true;
  94. }
  95. }
  96. }
  97. } catch (\Exception $e) {
  98. // do nothing, in this case we just disable the trashbin and continue
  99. $logger = \OC::$server->getLogger();
  100. $logger->debug('Trashbin storage could not check if a file was moved out of a shared folder: ' . $e->getMessage());
  101. }
  102. if($fileMovedOutOfSharedFolder) {
  103. self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true;
  104. } else {
  105. self::$disableTrash = true;
  106. }
  107. }
  108. /**
  109. * @internal
  110. */
  111. public static function postRenameHook($params) {
  112. self::$disableTrash = false;
  113. }
  114. /**
  115. * Rename path1 to path2 by calling the wrapped storage.
  116. *
  117. * @param string $path1 first path
  118. * @param string $path2 second path
  119. * @return bool
  120. */
  121. public function rename($path1, $path2) {
  122. $result = $this->storage->rename($path1, $path2);
  123. if ($result === false) {
  124. // when rename failed, the post_rename hook isn't triggered,
  125. // but we still want to reenable the trash logic
  126. self::$disableTrash = false;
  127. }
  128. return $result;
  129. }
  130. /**
  131. * Deletes the given file by moving it into the trashbin.
  132. *
  133. * @param string $path path of file or folder to delete
  134. *
  135. * @return bool true if the operation succeeded, false otherwise
  136. */
  137. public function unlink($path) {
  138. try {
  139. if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
  140. $result = $this->doDelete($path, 'unlink', true);
  141. unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
  142. } else {
  143. $result = $this->doDelete($path, 'unlink');
  144. }
  145. } catch (GenericEncryptionException $e) {
  146. // in case of a encryption exception we delete the file right away
  147. $this->logger->info(
  148. "Can't move file" . $path .
  149. "to the trash bin, therefore it was deleted right away");
  150. $result = $this->storage->unlink($path);
  151. }
  152. return $result;
  153. }
  154. /**
  155. * Deletes the given folder by moving it into the trashbin.
  156. *
  157. * @param string $path path of folder to delete
  158. *
  159. * @return bool true if the operation succeeded, false otherwise
  160. */
  161. public function rmdir($path) {
  162. if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
  163. $result = $this->doDelete($path, 'rmdir', true);
  164. unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
  165. } else {
  166. $result = $this->doDelete($path, 'rmdir');
  167. }
  168. return $result;
  169. }
  170. /**
  171. * check if it is a file located in data/user/files only files in the
  172. * 'files' directory should be moved to the trash
  173. *
  174. * @param $path
  175. * @return bool
  176. */
  177. protected function shouldMoveToTrash($path){
  178. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  179. $parts = explode('/', $normalized);
  180. if (count($parts) < 4) {
  181. return false;
  182. }
  183. if ($this->userManager->userExists($parts[1]) && $parts[2] == 'files') {
  184. return true;
  185. }
  186. return false;
  187. }
  188. /**
  189. * Run the delete operation with the given method
  190. *
  191. * @param string $path path of file or folder to delete
  192. * @param string $method either "unlink" or "rmdir"
  193. * @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder)
  194. *
  195. * @return bool true if the operation succeeded, false otherwise
  196. */
  197. private function doDelete($path, $method, $ownerOnly = false) {
  198. if (self::$disableTrash
  199. || !\OC_App::isEnabled('files_trashbin')
  200. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  201. || $this->shouldMoveToTrash($path) === false
  202. ) {
  203. return call_user_func_array([$this->storage, $method], [$path]);
  204. }
  205. // check permissions before we continue, this is especially important for
  206. // shared files
  207. if (!$this->isDeletable($path)) {
  208. return false;
  209. }
  210. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true);
  211. $result = true;
  212. $view = Filesystem::getView();
  213. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  214. $this->deletedFiles[$normalized] = $normalized;
  215. if ($filesPath = $view->getRelativePath($normalized)) {
  216. $filesPath = trim($filesPath, '/');
  217. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly);
  218. // in cross-storage cases the file will be copied
  219. // but not deleted, so we delete it here
  220. if ($result) {
  221. call_user_func_array([$this->storage, $method], [$path]);
  222. }
  223. } else {
  224. $result = call_user_func_array([$this->storage, $method], [$path]);
  225. }
  226. unset($this->deletedFiles[$normalized]);
  227. } else if ($this->storage->file_exists($path)) {
  228. $result = call_user_func_array([$this->storage, $method], [$path]);
  229. }
  230. return $result;
  231. }
  232. /**
  233. * Setup the storate wrapper callback
  234. */
  235. public static function setupStorage() {
  236. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  237. return new \OCA\Files_Trashbin\Storage(
  238. array('storage' => $storage, 'mountPoint' => $mountPoint),
  239. \OC::$server->getUserManager(),
  240. \OC::$server->getLogger()
  241. );
  242. }, 1);
  243. }
  244. }