2015-01-14 20:06:26 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 14:49:16 +00:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 17:56:05 +00:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2017-11-06 14:56:42 +00:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 16:13:36 +00:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2015-03-26 10:44:34 +00:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-01-14 20:06:26 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 20:06:26 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
2015-02-23 10:28:53 +00:00
|
|
|
*
|
2015-03-26 10:44:34 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 20:06:26 +00:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 10:44:34 +00:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 20:06:26 +00:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 10:37:37 +00:00
|
|
|
|
2015-01-14 20:06:26 +00:00
|
|
|
namespace OCA\Files_Trashbin;
|
|
|
|
|
2015-01-23 13:19:36 +00:00
|
|
|
use OC\Files\Filesystem;
|
2015-01-14 20:06:26 +00:00
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
2015-10-27 11:10:23 +00:00
|
|
|
use OC\Files\View;
|
2017-09-14 11:59:09 +00:00
|
|
|
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
|
2017-03-14 11:05:25 +00:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2017-09-14 11:59:09 +00:00
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\Node;
|
2017-03-14 11:05:25 +00:00
|
|
|
use OCP\ILogger;
|
2015-07-07 16:38:58 +00:00
|
|
|
use OCP\IUserManager;
|
2017-09-14 11:59:09 +00:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
2015-01-14 20:06:26 +00:00
|
|
|
|
|
|
|
class Storage extends Wrapper {
|
|
|
|
|
|
|
|
private $mountPoint;
|
|
|
|
// remember already deleted files to avoid infinite loops if the trash bin
|
|
|
|
// move files across storages
|
|
|
|
private $deletedFiles = array();
|
|
|
|
|
2015-01-26 11:22:22 +00:00
|
|
|
/**
|
|
|
|
* Disable trash logic
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $disableTrash = false;
|
|
|
|
|
2016-12-21 16:43:14 +00:00
|
|
|
/**
|
|
|
|
* remember which file/folder was moved out of s shared folder
|
|
|
|
* in this case we want to add a copy to the owners trash bin
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-12-21 14:42:57 +00:00
|
|
|
private static $moveOutOfSharedFolder = [];
|
|
|
|
|
2015-07-07 16:38:58 +00:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2017-03-14 11:05:25 +00:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2017-09-14 11:59:09 +00:00
|
|
|
/** @var EventDispatcher */
|
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
|
|
|
|
2016-12-21 14:42:57 +00:00
|
|
|
/**
|
|
|
|
* Storage constructor.
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
|
|
|
* @param IUserManager|null $userManager
|
2017-09-14 11:59:09 +00:00
|
|
|
* @param ILogger|null $logger
|
|
|
|
* @param EventDispatcher|null $eventDispatcher
|
|
|
|
* @param IRootFolder|null $rootFolder
|
2016-12-21 14:42:57 +00:00
|
|
|
*/
|
2017-03-14 11:05:25 +00:00
|
|
|
public function __construct($parameters,
|
|
|
|
IUserManager $userManager = null,
|
2017-09-14 11:59:09 +00:00
|
|
|
ILogger $logger = null,
|
|
|
|
EventDispatcher $eventDispatcher = null,
|
|
|
|
IRootFolder $rootFolder = null) {
|
2015-01-14 20:06:26 +00:00
|
|
|
$this->mountPoint = $parameters['mountPoint'];
|
2015-07-07 16:38:58 +00:00
|
|
|
$this->userManager = $userManager;
|
2017-03-14 11:05:25 +00:00
|
|
|
$this->logger = $logger;
|
2017-09-14 11:59:09 +00:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2015-01-14 20:06:26 +00:00
|
|
|
parent::__construct($parameters);
|
|
|
|
}
|
|
|
|
|
2015-01-26 11:22:22 +00:00
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public static function preRenameHook($params) {
|
|
|
|
// in cross-storage cases, a rename is a copy + unlink,
|
2016-12-21 14:42:57 +00:00
|
|
|
// that last unlink must not go to trash, only exception:
|
|
|
|
// if the file was moved from a shared storage to a local folder,
|
|
|
|
// in this case the owner should get a copy in his trash bin so that
|
|
|
|
// they can restore the files again
|
|
|
|
|
|
|
|
$oldPath = $params['oldpath'];
|
|
|
|
$newPath = dirname($params['newpath']);
|
|
|
|
$currentUser = \OC::$server->getUserSession()->getUser();
|
|
|
|
|
|
|
|
$fileMovedOutOfSharedFolder = false;
|
|
|
|
|
2016-12-21 16:43:14 +00:00
|
|
|
try {
|
|
|
|
if ($currentUser) {
|
|
|
|
$currentUserId = $currentUser->getUID();
|
|
|
|
|
|
|
|
$view = new View($currentUserId . '/files');
|
|
|
|
$fileInfo = $view->getFileInfo($oldPath);
|
|
|
|
if ($fileInfo) {
|
|
|
|
$sourceStorage = $fileInfo->getStorage();
|
|
|
|
$sourceOwner = $view->getOwner($oldPath);
|
|
|
|
$targetOwner = $view->getOwner($newPath);
|
|
|
|
|
|
|
|
if ($sourceOwner !== $targetOwner
|
|
|
|
&& $sourceStorage->instanceOfStorage('OCA\Files_Sharing\SharedStorage')
|
|
|
|
) {
|
|
|
|
$fileMovedOutOfSharedFolder = true;
|
|
|
|
}
|
|
|
|
}
|
2016-12-21 14:42:57 +00:00
|
|
|
}
|
2016-12-21 16:43:14 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
// do nothing, in this case we just disable the trashbin and continue
|
2018-01-17 14:21:56 +00:00
|
|
|
\OC::$server->getLogger()->logException($e, [
|
|
|
|
'message' => 'Trashbin storage could not check if a file was moved out of a shared folder.',
|
|
|
|
'level' => \OCP\Util::DEBUG,
|
|
|
|
'app' => 'files_trashbin',
|
|
|
|
]);
|
2016-12-21 14:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if($fileMovedOutOfSharedFolder) {
|
|
|
|
self::$moveOutOfSharedFolder['/' . $currentUserId . '/files' . $oldPath] = true;
|
|
|
|
} else {
|
|
|
|
self::$disableTrash = true;
|
|
|
|
}
|
|
|
|
|
2015-01-26 11:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @internal
|
|
|
|
*/
|
|
|
|
public static function postRenameHook($params) {
|
|
|
|
self::$disableTrash = false;
|
|
|
|
}
|
|
|
|
|
2015-03-18 18:22:15 +00:00
|
|
|
/**
|
|
|
|
* Rename path1 to path2 by calling the wrapped storage.
|
|
|
|
*
|
|
|
|
* @param string $path1 first path
|
|
|
|
* @param string $path2 second path
|
2016-12-21 14:42:57 +00:00
|
|
|
* @return bool
|
2015-03-18 18:22:15 +00:00
|
|
|
*/
|
|
|
|
public function rename($path1, $path2) {
|
|
|
|
$result = $this->storage->rename($path1, $path2);
|
|
|
|
if ($result === false) {
|
|
|
|
// when rename failed, the post_rename hook isn't triggered,
|
|
|
|
// but we still want to reenable the trash logic
|
|
|
|
self::$disableTrash = false;
|
|
|
|
}
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
2015-01-26 11:22:22 +00:00
|
|
|
/**
|
|
|
|
* Deletes the given file by moving it into the trashbin.
|
|
|
|
*
|
2015-05-11 15:53:21 +00:00
|
|
|
* @param string $path path of file or folder to delete
|
|
|
|
*
|
|
|
|
* @return bool true if the operation succeeded, false otherwise
|
2015-01-26 11:22:22 +00:00
|
|
|
*/
|
2015-01-14 20:06:26 +00:00
|
|
|
public function unlink($path) {
|
2017-03-14 11:05:25 +00:00
|
|
|
try {
|
|
|
|
if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
|
|
|
|
$result = $this->doDelete($path, 'unlink', true);
|
|
|
|
unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
|
|
|
|
} else {
|
|
|
|
$result = $this->doDelete($path, 'unlink');
|
|
|
|
}
|
|
|
|
} catch (GenericEncryptionException $e) {
|
|
|
|
// in case of a encryption exception we delete the file right away
|
|
|
|
$this->logger->info(
|
|
|
|
"Can't move file" . $path .
|
|
|
|
"to the trash bin, therefore it was deleted right away");
|
|
|
|
|
|
|
|
$result = $this->storage->unlink($path);
|
2016-12-21 14:42:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2015-05-11 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Deletes the given folder by moving it into the trashbin.
|
|
|
|
*
|
|
|
|
* @param string $path path of folder to delete
|
|
|
|
*
|
|
|
|
* @return bool true if the operation succeeded, false otherwise
|
|
|
|
*/
|
|
|
|
public function rmdir($path) {
|
2016-12-21 14:42:57 +00:00
|
|
|
if (isset(self::$moveOutOfSharedFolder[$this->mountPoint . $path])) {
|
|
|
|
$result = $this->doDelete($path, 'rmdir', true);
|
|
|
|
unset(self::$moveOutOfSharedFolder[$this->mountPoint . $path]);
|
|
|
|
} else {
|
|
|
|
$result = $this->doDelete($path, 'rmdir');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2015-05-11 15:53:21 +00:00
|
|
|
}
|
|
|
|
|
2015-07-07 16:38:58 +00:00
|
|
|
/**
|
|
|
|
* check if it is a file located in data/user/files only files in the
|
|
|
|
* 'files' directory should be moved to the trash
|
|
|
|
*
|
|
|
|
* @param $path
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function shouldMoveToTrash($path){
|
2017-09-14 11:59:09 +00:00
|
|
|
|
|
|
|
// check if there is a app which want to disable the trash bin for this file
|
|
|
|
$fileId = $this->storage->getCache()->getId($path);
|
|
|
|
$nodes = $this->rootFolder->getById($fileId);
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$event = $this->createMoveToTrashEvent($node);
|
|
|
|
$this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
|
|
|
|
if ($event->shouldMoveToTrashBin() === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-07 16:38:58 +00:00
|
|
|
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
|
|
|
|
$parts = explode('/', $normalized);
|
|
|
|
if (count($parts) < 4) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-01 10:47:56 +00:00
|
|
|
if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
|
2015-07-07 16:38:58 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 11:59:09 +00:00
|
|
|
/**
|
|
|
|
* get move to trash event
|
|
|
|
*
|
|
|
|
* @param Node $node
|
|
|
|
* @return MoveToTrashEvent
|
|
|
|
*/
|
|
|
|
protected function createMoveToTrashEvent(Node $node) {
|
2018-01-25 23:02:03 +00:00
|
|
|
return new MoveToTrashEvent($node);
|
2017-09-14 11:59:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 15:53:21 +00:00
|
|
|
/**
|
|
|
|
* Run the delete operation with the given method
|
|
|
|
*
|
|
|
|
* @param string $path path of file or folder to delete
|
|
|
|
* @param string $method either "unlink" or "rmdir"
|
2016-12-21 14:42:57 +00:00
|
|
|
* @param bool $ownerOnly delete for owner only (if file gets moved out of a shared folder)
|
2015-05-11 15:53:21 +00:00
|
|
|
*
|
|
|
|
* @return bool true if the operation succeeded, false otherwise
|
|
|
|
*/
|
2016-12-21 14:42:57 +00:00
|
|
|
private function doDelete($path, $method, $ownerOnly = false) {
|
2015-04-21 16:28:15 +00:00
|
|
|
if (self::$disableTrash
|
|
|
|
|| !\OC_App::isEnabled('files_trashbin')
|
|
|
|
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
|
2015-07-07 16:38:58 +00:00
|
|
|
|| $this->shouldMoveToTrash($path) === false
|
2015-04-21 16:28:15 +00:00
|
|
|
) {
|
2015-05-11 15:53:21 +00:00
|
|
|
return call_user_func_array([$this->storage, $method], [$path]);
|
2015-01-26 11:22:22 +00:00
|
|
|
}
|
2015-09-30 12:35:11 +00:00
|
|
|
|
|
|
|
// check permissions before we continue, this is especially important for
|
|
|
|
// shared files
|
|
|
|
if (!$this->isDeletable($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-04-29 10:42:37 +00:00
|
|
|
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path, true, false, true);
|
2015-01-14 20:06:26 +00:00
|
|
|
$result = true;
|
2015-10-27 11:10:23 +00:00
|
|
|
$view = Filesystem::getView();
|
|
|
|
if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
|
2015-01-14 20:06:26 +00:00
|
|
|
$this->deletedFiles[$normalized] = $normalized;
|
2015-01-23 13:19:36 +00:00
|
|
|
if ($filesPath = $view->getRelativePath($normalized)) {
|
|
|
|
$filesPath = trim($filesPath, '/');
|
2016-12-21 14:42:57 +00:00
|
|
|
$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath, $ownerOnly);
|
2015-01-21 15:29:52 +00:00
|
|
|
// in cross-storage cases the file will be copied
|
|
|
|
// but not deleted, so we delete it here
|
2015-01-29 14:52:40 +00:00
|
|
|
if ($result) {
|
2015-05-11 15:53:21 +00:00
|
|
|
call_user_func_array([$this->storage, $method], [$path]);
|
2015-01-29 14:52:40 +00:00
|
|
|
}
|
2015-01-14 20:06:26 +00:00
|
|
|
} else {
|
2015-05-11 15:53:21 +00:00
|
|
|
$result = call_user_func_array([$this->storage, $method], [$path]);
|
2015-01-14 20:06:26 +00:00
|
|
|
}
|
|
|
|
unset($this->deletedFiles[$normalized]);
|
2015-01-28 15:30:42 +00:00
|
|
|
} else if ($this->storage->file_exists($path)) {
|
2015-05-11 15:53:21 +00:00
|
|
|
$result = call_user_func_array([$this->storage, $method], [$path]);
|
2015-01-14 20:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the storate wrapper callback
|
|
|
|
*/
|
|
|
|
public static function setupStorage() {
|
|
|
|
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
|
2015-07-07 16:38:58 +00:00
|
|
|
return new \OCA\Files_Trashbin\Storage(
|
|
|
|
array('storage' => $storage, 'mountPoint' => $mountPoint),
|
2017-03-14 11:05:25 +00:00
|
|
|
\OC::$server->getUserManager(),
|
2017-09-14 11:59:09 +00:00
|
|
|
\OC::$server->getLogger(),
|
|
|
|
\OC::$server->getEventDispatcher(),
|
|
|
|
\OC::$server->getLazyRootFolder()
|
2015-07-07 16:38:58 +00:00
|
|
|
);
|
2015-04-01 15:22:04 +00:00
|
|
|
}, 1);
|
2015-01-14 20:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|