storage.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * @author Björn Schießle <schiessle@owncloud.com>
  4. * @author Morris Jobke <hey@morrisjobke.de>
  5. * @author Robin Appelman <icewind@owncloud.com>
  6. * @author Vincent Petry <pvince81@owncloud.com>
  7. *
  8. * @copyright Copyright (c) 2016, ownCloud, Inc.
  9. * @license AGPL-3.0
  10. *
  11. * This code is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License, version 3,
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License, version 3,
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>
  22. *
  23. */
  24. namespace OCA\Files_Trashbin;
  25. use OC\Files\Filesystem;
  26. use OC\Files\Storage\Wrapper\Wrapper;
  27. use OC\Files\View;
  28. use OCP\IUserManager;
  29. class Storage extends Wrapper {
  30. private $mountPoint;
  31. // remember already deleted files to avoid infinite loops if the trash bin
  32. // move files across storages
  33. private $deletedFiles = array();
  34. /**
  35. * Disable trash logic
  36. *
  37. * @var bool
  38. */
  39. private static $disableTrash = false;
  40. /** @var IUserManager */
  41. private $userManager;
  42. function __construct($parameters, IUserManager $userManager = null) {
  43. $this->mountPoint = $parameters['mountPoint'];
  44. $this->userManager = $userManager;
  45. parent::__construct($parameters);
  46. }
  47. /**
  48. * @internal
  49. */
  50. public static function preRenameHook($params) {
  51. // in cross-storage cases, a rename is a copy + unlink,
  52. // that last unlink must not go to trash
  53. self::$disableTrash = true;
  54. }
  55. /**
  56. * @internal
  57. */
  58. public static function postRenameHook($params) {
  59. self::$disableTrash = false;
  60. }
  61. /**
  62. * Rename path1 to path2 by calling the wrapped storage.
  63. *
  64. * @param string $path1 first path
  65. * @param string $path2 second path
  66. */
  67. public function rename($path1, $path2) {
  68. $result = $this->storage->rename($path1, $path2);
  69. if ($result === false) {
  70. // when rename failed, the post_rename hook isn't triggered,
  71. // but we still want to reenable the trash logic
  72. self::$disableTrash = false;
  73. }
  74. return $result;
  75. }
  76. /**
  77. * Deletes the given file by moving it into the trashbin.
  78. *
  79. * @param string $path path of file or folder to delete
  80. *
  81. * @return bool true if the operation succeeded, false otherwise
  82. */
  83. public function unlink($path) {
  84. return $this->doDelete($path, 'unlink');
  85. }
  86. /**
  87. * Deletes the given folder by moving it into the trashbin.
  88. *
  89. * @param string $path path of folder to delete
  90. *
  91. * @return bool true if the operation succeeded, false otherwise
  92. */
  93. public function rmdir($path) {
  94. return $this->doDelete($path, 'rmdir');
  95. }
  96. /**
  97. * check if it is a file located in data/user/files only files in the
  98. * 'files' directory should be moved to the trash
  99. *
  100. * @param $path
  101. * @return bool
  102. */
  103. protected function shouldMoveToTrash($path){
  104. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  105. $parts = explode('/', $normalized);
  106. if (count($parts) < 4) {
  107. return false;
  108. }
  109. if ($this->userManager->userExists($parts[1]) && $parts[2] == 'files') {
  110. return true;
  111. }
  112. return false;
  113. }
  114. /**
  115. * Run the delete operation with the given method
  116. *
  117. * @param string $path path of file or folder to delete
  118. * @param string $method either "unlink" or "rmdir"
  119. *
  120. * @return bool true if the operation succeeded, false otherwise
  121. */
  122. private function doDelete($path, $method) {
  123. if (self::$disableTrash
  124. || !\OC_App::isEnabled('files_trashbin')
  125. || (pathinfo($path, PATHINFO_EXTENSION) === 'part')
  126. || $this->shouldMoveToTrash($path) === false
  127. ) {
  128. return call_user_func_array([$this->storage, $method], [$path]);
  129. }
  130. // check permissions before we continue, this is especially important for
  131. // shared files
  132. if (!$this->isDeletable($path)) {
  133. return false;
  134. }
  135. $normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
  136. $result = true;
  137. $view = Filesystem::getView();
  138. if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
  139. $this->deletedFiles[$normalized] = $normalized;
  140. if ($filesPath = $view->getRelativePath($normalized)) {
  141. $filesPath = trim($filesPath, '/');
  142. $result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
  143. // in cross-storage cases the file will be copied
  144. // but not deleted, so we delete it here
  145. if ($result) {
  146. call_user_func_array([$this->storage, $method], [$path]);
  147. }
  148. } else {
  149. $result = call_user_func_array([$this->storage, $method], [$path]);
  150. }
  151. unset($this->deletedFiles[$normalized]);
  152. } else if ($this->storage->file_exists($path)) {
  153. $result = call_user_func_array([$this->storage, $method], [$path]);
  154. }
  155. return $result;
  156. }
  157. /**
  158. * Setup the storate wrapper callback
  159. */
  160. public static function setupStorage() {
  161. \OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
  162. return new \OCA\Files_Trashbin\Storage(
  163. array('storage' => $storage, 'mountPoint' => $mountPoint),
  164. \OC::$server->getUserManager()
  165. );
  166. }, 1);
  167. }
  168. }