2015-01-14 23:06:26 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
|
|
|
* @author Julius Härtl <jus@bitgrid.net>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-03-26 13:44:34 +03:00
|
|
|
* @author Vincent Petry <pvince81@owncloud.com>
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* @license AGPL-3.0
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03: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 13:28:53 +03:00
|
|
|
*
|
2015-03-26 13:44:34 +03:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2015-01-14 23:06:26 +03:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2015-03-26 13:44:34 +03: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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-01-14 23:06:26 +03:00
|
|
|
*
|
|
|
|
*/
|
2015-02-26 13:37:37 +03:00
|
|
|
|
2015-01-14 23:06:26 +03:00
|
|
|
namespace OCA\Files_Trashbin;
|
|
|
|
|
2015-01-23 16:19:36 +03:00
|
|
|
use OC\Files\Filesystem;
|
2015-01-14 23:06:26 +03:00
|
|
|
use OC\Files\Storage\Wrapper\Wrapper;
|
2017-09-14 14:59:09 +03:00
|
|
|
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
|
2018-09-10 15:40:35 +03:00
|
|
|
use OCA\Files_Trashbin\Trash\ITrashManager;
|
2017-03-14 14:05:25 +03:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2017-09-14 14:59:09 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2018-09-10 15:40:35 +03:00
|
|
|
use OCP\Files\Mount\IMountPoint;
|
2017-09-14 14:59:09 +03:00
|
|
|
use OCP\Files\Node;
|
2017-03-14 14:05:25 +03:00
|
|
|
use OCP\ILogger;
|
2015-07-07 19:38:58 +03:00
|
|
|
use OCP\IUserManager;
|
2019-05-28 20:46:36 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2015-01-14 23:06:26 +03:00
|
|
|
|
|
|
|
class Storage extends Wrapper {
|
2018-09-10 15:40:35 +03:00
|
|
|
/** @var IMountPoint */
|
2015-01-14 23:06:26 +03:00
|
|
|
private $mountPoint;
|
2016-12-21 17:42:57 +03:00
|
|
|
|
2015-07-07 19:38:58 +03:00
|
|
|
/** @var IUserManager */
|
|
|
|
private $userManager;
|
|
|
|
|
2017-03-14 14:05:25 +03:00
|
|
|
/** @var ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2019-05-28 20:46:36 +03:00
|
|
|
/** @var EventDispatcherInterface */
|
2017-09-14 14:59:09 +03:00
|
|
|
private $eventDispatcher;
|
|
|
|
|
|
|
|
/** @var IRootFolder */
|
|
|
|
private $rootFolder;
|
|
|
|
|
2018-09-10 15:40:35 +03:00
|
|
|
/** @var ITrashManager */
|
|
|
|
private $trashManager;
|
|
|
|
|
2016-12-21 17:42:57 +03:00
|
|
|
/**
|
|
|
|
* Storage constructor.
|
|
|
|
*
|
|
|
|
* @param array $parameters
|
2018-09-10 15:40:35 +03:00
|
|
|
* @param ITrashManager $trashManager
|
2016-12-21 17:42:57 +03:00
|
|
|
* @param IUserManager|null $userManager
|
2017-09-14 14:59:09 +03:00
|
|
|
* @param ILogger|null $logger
|
2019-05-28 20:46:36 +03:00
|
|
|
* @param EventDispatcherInterface|null $eventDispatcher
|
2017-09-14 14:59:09 +03:00
|
|
|
* @param IRootFolder|null $rootFolder
|
2016-12-21 17:42:57 +03:00
|
|
|
*/
|
2018-09-10 15:40:35 +03:00
|
|
|
public function __construct(
|
|
|
|
$parameters,
|
|
|
|
ITrashManager $trashManager = null,
|
|
|
|
IUserManager $userManager = null,
|
|
|
|
ILogger $logger = null,
|
2019-05-28 20:46:36 +03:00
|
|
|
EventDispatcherInterface $eventDispatcher = null,
|
2018-09-10 15:40:35 +03:00
|
|
|
IRootFolder $rootFolder = null
|
|
|
|
) {
|
2015-01-14 23:06:26 +03:00
|
|
|
$this->mountPoint = $parameters['mountPoint'];
|
2018-09-10 15:40:35 +03:00
|
|
|
$this->trashManager = $trashManager;
|
2015-07-07 19:38:58 +03:00
|
|
|
$this->userManager = $userManager;
|
2017-03-14 14:05:25 +03:00
|
|
|
$this->logger = $logger;
|
2017-09-14 14:59:09 +03:00
|
|
|
$this->eventDispatcher = $eventDispatcher;
|
|
|
|
$this->rootFolder = $rootFolder;
|
2015-01-14 23:06:26 +03:00
|
|
|
parent::__construct($parameters);
|
|
|
|
}
|
|
|
|
|
2015-01-26 14:22:22 +03:00
|
|
|
/**
|
|
|
|
* Deletes the given file by moving it into the trashbin.
|
|
|
|
*
|
2015-05-11 18:53:21 +03:00
|
|
|
* @param string $path path of file or folder to delete
|
|
|
|
*
|
|
|
|
* @return bool true if the operation succeeded, false otherwise
|
2015-01-26 14:22:22 +03:00
|
|
|
*/
|
2015-01-14 23:06:26 +03:00
|
|
|
public function unlink($path) {
|
2017-03-14 14:05:25 +03:00
|
|
|
try {
|
2018-09-10 15:40:35 +03:00
|
|
|
return $this->doDelete($path, 'unlink');
|
2017-03-14 14:05:25 +03:00
|
|
|
} catch (GenericEncryptionException $e) {
|
|
|
|
// in case of a encryption exception we delete the file right away
|
|
|
|
$this->logger->info(
|
2020-09-17 09:58:10 +03:00
|
|
|
"Can't move file " . $path .
|
|
|
|
" to the trash bin, therefore it was deleted right away");
|
2017-03-14 14:05:25 +03:00
|
|
|
|
2018-09-10 15:40:35 +03:00
|
|
|
return $this->storage->unlink($path);
|
2016-12-21 17:42:57 +03:00
|
|
|
}
|
2015-05-11 18:53:21 +03: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) {
|
2018-09-10 15:40:35 +03:00
|
|
|
return $this->doDelete($path, 'rmdir');
|
2015-05-11 18:53:21 +03:00
|
|
|
}
|
|
|
|
|
2015-07-07 19:38:58 +03: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
|
|
|
|
*/
|
2018-09-10 15:40:35 +03:00
|
|
|
protected function shouldMoveToTrash($path) {
|
2019-08-05 20:10:49 +03:00
|
|
|
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
|
|
|
|
$parts = explode('/', $normalized);
|
2020-11-13 10:30:35 +03:00
|
|
|
if (count($parts) < 4 || strpos($normalized, '/appdata_') === 0) {
|
2019-08-05 20:10:49 +03:00
|
|
|
return false;
|
|
|
|
}
|
2017-09-14 14:59:09 +03:00
|
|
|
|
|
|
|
// check if there is a app which want to disable the trash bin for this file
|
|
|
|
$fileId = $this->storage->getCache()->getId($path);
|
2019-08-07 17:47:43 +03:00
|
|
|
$owner = $this->storage->getOwner($path);
|
2021-01-22 15:52:43 +03:00
|
|
|
if ($owner === false || $this->storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class)) {
|
2019-08-07 17:47:43 +03:00
|
|
|
$nodes = $this->rootFolder->getById($fileId);
|
|
|
|
} else {
|
|
|
|
$nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
|
|
|
|
}
|
|
|
|
|
2017-09-14 14:59:09 +03:00
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$event = $this->createMoveToTrashEvent($node);
|
|
|
|
$this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
|
|
|
|
if ($event->shouldMoveToTrashBin() === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-07 17:47:43 +03:00
|
|
|
if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
|
2015-07-07 19:38:58 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-09-14 14:59:09 +03:00
|
|
|
/**
|
|
|
|
* get move to trash event
|
|
|
|
*
|
|
|
|
* @param Node $node
|
|
|
|
* @return MoveToTrashEvent
|
|
|
|
*/
|
|
|
|
protected function createMoveToTrashEvent(Node $node) {
|
2018-01-26 02:02:03 +03:00
|
|
|
return new MoveToTrashEvent($node);
|
2017-09-14 14:59:09 +03:00
|
|
|
}
|
|
|
|
|
2015-05-11 18:53:21 +03: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"
|
|
|
|
*
|
|
|
|
* @return bool true if the operation succeeded, false otherwise
|
|
|
|
*/
|
2018-09-10 15:40:35 +03:00
|
|
|
private function doDelete($path, $method) {
|
|
|
|
if (
|
|
|
|
!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
|
2015-04-21 19:28:15 +03:00
|
|
|
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
|
2015-07-07 19:38:58 +03:00
|
|
|
|| $this->shouldMoveToTrash($path) === false
|
2015-04-21 19:28:15 +03:00
|
|
|
) {
|
2018-09-10 15:40:35 +03:00
|
|
|
return call_user_func([$this->storage, $method], $path);
|
2015-01-26 14:22:22 +03:00
|
|
|
}
|
2015-09-30 15:35:11 +03:00
|
|
|
|
|
|
|
// check permissions before we continue, this is especially important for
|
|
|
|
// shared files
|
|
|
|
if (!$this->isDeletable($path)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:40:35 +03:00
|
|
|
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
|
|
|
|
if (!$isMovedToTrash) {
|
|
|
|
return call_user_func([$this->storage, $method], $path);
|
|
|
|
} else {
|
|
|
|
return true;
|
2015-01-14 23:06:26 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Setup the storate wrapper callback
|
|
|
|
*/
|
|
|
|
public static function setupStorage() {
|
|
|
|
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
|
2015-07-07 19:38:58 +03:00
|
|
|
return new \OCA\Files_Trashbin\Storage(
|
2018-09-10 15:40:35 +03:00
|
|
|
['storage' => $storage, 'mountPoint' => $mountPoint],
|
|
|
|
\OC::$server->query(ITrashManager::class),
|
2017-03-14 14:05:25 +03:00
|
|
|
\OC::$server->getUserManager(),
|
2017-09-14 14:59:09 +03:00
|
|
|
\OC::$server->getLogger(),
|
|
|
|
\OC::$server->getEventDispatcher(),
|
|
|
|
\OC::$server->getLazyRootFolder()
|
2015-07-07 19:38:58 +03:00
|
|
|
);
|
2015-04-01 18:22:04 +03:00
|
|
|
}, 1);
|
2015-01-14 23:06:26 +03:00
|
|
|
}
|
|
|
|
|
2018-09-10 15:40:35 +03:00
|
|
|
public function getMountPoint() {
|
|
|
|
return $this->mountPoint;
|
|
|
|
}
|
2015-01-14 23:06:26 +03:00
|
|
|
}
|