Storage.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Björn Schießle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Julius Härtl <jus@bitgrid.net>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Vincent Petry <pvince81@owncloud.com>
  13. *
  14. * @license AGPL-3.0
  15. *
  16. * This code is free software: you can redistribute it and/or modify
  17. * it under the terms of the GNU Affero General Public License, version 3,
  18. * as published by the Free Software Foundation.
  19. *
  20. * This program is distributed in the hope that it will be useful,
  21. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  23. * GNU Affero General Public License for more details.
  24. *
  25. * You should have received a copy of the GNU Affero General Public License, version 3,
  26. * along with this program. If not, see <http://www.gnu.org/licenses/>
  27. *
  28. */
  29. namespace OCA\Files_Trashbin;
  30. use OC\Files\Filesystem;
  31. use OC\Files\Storage\Wrapper\Wrapper;
  32. use OCA\Files_Trashbin\Events\MoveToTrashEvent;
  33. use OCA\Files_Trashbin\Trash\ITrashManager;
  34. use OCP\Encryption\Exceptions\GenericEncryptionException;
  35. use OCP\Files\IRootFolder;
  36. use OCP\Files\Mount\IMountPoint;
  37. use OCP\Files\Node;
  38. use OCP\ILogger;
  39. use OCP\IUserManager;
  40. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  41. class Storage extends Wrapper {
  42. /** @var IMountPoint */
  43. private $mountPoint;
  44. /** @var IUserManager */
  45. private $userManager;
  46. /** @var ILogger */
  47. private $logger;
  48. /** @var EventDispatcherInterface */
  49. private $eventDispatcher;
  50. /** @var IRootFolder */
  51. private $rootFolder;
  52. /** @var ITrashManager */
  53. private $trashManager;
  54. /**
  55. * Storage constructor.
  56. *
  57. * @param array $parameters
  58. * @param ITrashManager $trashManager
  59. * @param IUserManager|null $userManager
  60. * @param ILogger|null $logger
  61. * @param EventDispatcherInterface|null $eventDispatcher
  62. * @param IRootFolder|null $rootFolder
  63. */
  64. public function __construct(
  65. $parameters,
  66. ITrashManager $trashManager = null,
  67. IUserManager $userManager = null,
  68. ILogger $logger = null,
  69. EventDispatcherInterface $eventDispatcher = null,
  70. IRootFolder $rootFolder = null
  71. ) {
  72. $this->mountPoint = $parameters['mountPoint'];
  73. $this->trashManager = $trashManager;
  74. $this->userManager = $userManager;
  75. $this->logger = $logger;
  76. $this->eventDispatcher = $eventDispatcher;
  77. $this->rootFolder = $rootFolder;
  78. parent::__construct($parameters);
  79. }
  80. /**
  81. * Deletes the given file by moving it into the trashbin.
  82. *
  83. * @param string $path path of file or folder to delete
  84. *
  85. * @return bool true if the operation succeeded, false otherwise
  86. */
  87. public function unlink($path) {
  88. try {
  89. return $this->doDelete($path, 'unlink');
  90. } catch (GenericEncryptionException $e) {
  91. // in case of a encryption exception we delete the file right away
  92. $this->logger->info(
  93. "Can't move file" . $path .
  94. "to the trash bin, therefore it was deleted right away");
  95. return $this->storage->unlink($path);
  96. }
  97. }
  98. /**
  99. * Deletes the given folder by moving it into the trashbin.
  100. *
  101. * @param string $path path of folder to delete
  102. *
  103. * @return bool true if the operation succeeded, false otherwise
  104. */
  105. public function rmdir($path) {
  106. return $this->doDelete($path, 'rmdir');
  107. }
  108. /**
  109. * check if it is a file located in data/user/files only files in the
  110. * 'files' directory should be moved to the trash
  111. *
  112. * @param $path
  113. * @return bool
  114. */
  115. protected function shouldMoveToTrash($path) {
  116. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  117. $parts = explode('/', $normalized);
  118. if (count($parts) < 4) {
  119. return false;
  120. }
  121. // check if there is a app which want to disable the trash bin for this file
  122. $fileId = $this->storage->getCache()->getId($path);
  123. $owner = $this->storage->getOwner($path);
  124. if ($owner === false) {
  125. $nodes = $this->rootFolder->getById($fileId);
  126. } else {
  127. $nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
  128. }
  129. foreach ($nodes as $node) {
  130. $event = $this->createMoveToTrashEvent($node);
  131. $this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
  132. if ($event->shouldMoveToTrashBin() === false) {
  133. return false;
  134. }
  135. }
  136. if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
  137. return true;
  138. }
  139. return false;
  140. }
  141. /**
  142. * get move to trash event
  143. *
  144. * @param Node $node
  145. * @return MoveToTrashEvent
  146. */
  147. protected function createMoveToTrashEvent(Node $node) {
  148. return new MoveToTrashEvent($node);
  149. }
  150. /**
  151. * Run the delete operation with the given method
  152. *
  153. * @param string $path path of file or folder to delete
  154. * @param string $method either "unlink" or "rmdir"
  155. *
  156. * @return bool true if the operation succeeded, false otherwise
  157. */
  158. private function doDelete($path, $method) {
  159. if (
  160. !\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
  161. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  162. || $this->shouldMoveToTrash($path) === false
  163. ) {
  164. return call_user_func([$this->storage, $method], $path);
  165. }
  166. // check permissions before we continue, this is especially important for
  167. // shared files
  168. if (!$this->isDeletable($path)) {
  169. return false;
  170. }
  171. $isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
  172. if (!$isMovedToTrash) {
  173. return call_user_func([$this->storage, $method], $path);
  174. } else {
  175. return true;
  176. }
  177. }
  178. /**
  179. * Setup the storate wrapper callback
  180. */
  181. public static function setupStorage() {
  182. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  183. return new \OCA\Files_Trashbin\Storage(
  184. ['storage' => $storage, 'mountPoint' => $mountPoint],
  185. \OC::$server->query(ITrashManager::class),
  186. \OC::$server->getUserManager(),
  187. \OC::$server->getLogger(),
  188. \OC::$server->getEventDispatcher(),
  189. \OC::$server->getLazyRootFolder()
  190. );
  191. }, 1);
  192. }
  193. public function getMountPoint() {
  194. return $this->mountPoint;
  195. }
  196. }