server/apps/files_trashbin/lib/storage.php

190 lines
5.2 KiB
PHP
Raw Normal View History

2015-01-14 20:06:26 +00:00
<?php
/**
2015-03-26 10:44:34 +00:00
* @author Björn Schießle <schiessle@owncloud.com>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Vincent Petry <pvince81@owncloud.com>
2015-01-14 20:06:26 +00:00
*
2016-01-12 14:02:16 +00:00
* @copyright Copyright (c) 2016, ownCloud, Inc.
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-01-14 20:06:26 +00:00
namespace OCA\Files_Trashbin;
use OC\Files\Filesystem;
2015-01-14 20:06:26 +00:00
use OC\Files\Storage\Wrapper\Wrapper;
use OC\Files\View;
2015-07-07 16:38:58 +00:00
use OCP\IUserManager;
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();
/**
* Disable trash logic
*
* @var bool
*/
private static $disableTrash = false;
2015-07-07 16:38:58 +00:00
/** @var IUserManager */
private $userManager;
function __construct($parameters, IUserManager $userManager = null) {
2015-01-14 20:06:26 +00:00
$this->mountPoint = $parameters['mountPoint'];
2015-07-07 16:38:58 +00:00
$this->userManager = $userManager;
2015-01-14 20:06:26 +00:00
parent::__construct($parameters);
}
/**
* @internal
*/
public static function preRenameHook($params) {
// in cross-storage cases, a rename is a copy + unlink,
// that last unlink must not go to trash
self::$disableTrash = true;
}
/**
* @internal
*/
public static function postRenameHook($params) {
self::$disableTrash = false;
}
/**
* Rename path1 to path2 by calling the wrapped storage.
*
* @param string $path1 first path
* @param string $path2 second path
*/
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;
}
/**
* Deletes the given file by moving it into the trashbin.
*
* @param string $path path of file or folder to delete
*
* @return bool true if the operation succeeded, false otherwise
*/
2015-01-14 20:06:26 +00:00
public function unlink($path) {
return $this->doDelete($path, 'unlink');
}
/**
* 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) {
return $this->doDelete($path, 'rmdir');
}
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){
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
$parts = explode('/', $normalized);
if (count($parts) < 4) {
return false;
}
if ($this->userManager->userExists($parts[1]) && $parts[2] == 'files') {
return true;
}
return false;
}
/**
* 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
*/
private function doDelete($path, $method) {
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
) {
return call_user_func_array([$this->storage, $method], [$path]);
}
// check permissions before we continue, this is especially important for
// shared files
if (!$this->isDeletable($path)) {
return false;
}
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
2015-01-14 20:06:26 +00:00
$result = true;
$view = Filesystem::getView();
if (!isset($this->deletedFiles[$normalized]) && $view instanceof View) {
2015-01-14 20:06:26 +00:00
$this->deletedFiles[$normalized] = $normalized;
if ($filesPath = $view->getRelativePath($normalized)) {
$filesPath = trim($filesPath, '/');
2015-01-14 20:06:26 +00:00
$result = \OCA\Files_Trashbin\Trashbin::move2trash($filesPath);
// in cross-storage cases the file will be copied
// but not deleted, so we delete it here
if ($result) {
call_user_func_array([$this->storage, $method], [$path]);
}
2015-01-14 20:06:26 +00:00
} else {
$result = call_user_func_array([$this->storage, $method], [$path]);
2015-01-14 20:06:26 +00:00
}
unset($this->deletedFiles[$normalized]);
} else if ($this->storage->file_exists($path)) {
$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),
\OC::$server->getUserManager()
);
}, 1);
2015-01-14 20:06:26 +00:00
}
}