Passed
Push — master ( cfa2d2...a2ecac )
by Robin
19:44 queued 06:06
created

Storage::enableTrash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Bjoern Schiessle <[email protected]>
6
 * @author Björn Schießle <[email protected]>
7
 * @author Christoph Wurst <[email protected]>
8
 * @author Julius Härtl <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Vincent Petry <[email protected]>
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
30
namespace OCA\Files_Trashbin;
31
32
use OC\Files\Filesystem;
33
use OC\Files\Storage\Wrapper\Wrapper;
34
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
35
use OCA\Files_Trashbin\Trash\ITrashManager;
36
use OCP\Encryption\Exceptions\GenericEncryptionException;
37
use OCP\Files\IRootFolder;
38
use OCP\Files\Mount\IMountPoint;
39
use OCP\Files\Node;
40
use OCP\Files\Storage\IStorage;
41
use OCP\ILogger;
42
use OCP\IUserManager;
43
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
44
45
class Storage extends Wrapper {
46
	/** @var IMountPoint */
47
	private $mountPoint;
48
49
	/** @var  IUserManager */
50
	private $userManager;
51
52
	/** @var ILogger */
53
	private $logger;
54
55
	/** @var EventDispatcherInterface */
56
	private $eventDispatcher;
57
58
	/** @var IRootFolder */
59
	private $rootFolder;
60
61
	/** @var ITrashManager */
62
	private $trashManager;
63
64
	private $trashEnabled = true;
65
66
	/**
67
	 * Storage constructor.
68
	 *
69
	 * @param array $parameters
70
	 * @param ITrashManager $trashManager
71
	 * @param IUserManager|null $userManager
72
	 * @param ILogger|null $logger
73
	 * @param EventDispatcherInterface|null $eventDispatcher
74
	 * @param IRootFolder|null $rootFolder
75
	 */
76
	public function __construct(
77
		$parameters,
78
		ITrashManager $trashManager = null,
79
		IUserManager $userManager = null,
80
		ILogger $logger = null,
81
		EventDispatcherInterface $eventDispatcher = null,
82
		IRootFolder $rootFolder = null
83
	) {
84
		$this->mountPoint = $parameters['mountPoint'];
85
		$this->trashManager = $trashManager;
86
		$this->userManager = $userManager;
87
		$this->logger = $logger;
88
		$this->eventDispatcher = $eventDispatcher;
89
		$this->rootFolder = $rootFolder;
90
		parent::__construct($parameters);
91
	}
92
93
	/**
94
	 * Deletes the given file by moving it into the trashbin.
95
	 *
96
	 * @param string $path path of file or folder to delete
97
	 *
98
	 * @return bool true if the operation succeeded, false otherwise
99
	 */
100
	public function unlink($path) {
101
		if ($this->trashEnabled) {
102
			try {
103
				return $this->doDelete($path, 'unlink');
104
			} catch (GenericEncryptionException $e) {
105
				// in case of a encryption exception we delete the file right away
106
				$this->logger->info(
107
					"Can't move file " . $path .
108
					" to the trash bin, therefore it was deleted right away");
109
110
				return $this->storage->unlink($path);
111
			}
112
		} else {
113
			return $this->storage->unlink($path);
114
		}
115
	}
116
117
	/**
118
	 * Deletes the given folder by moving it into the trashbin.
119
	 *
120
	 * @param string $path path of folder to delete
121
	 *
122
	 * @return bool true if the operation succeeded, false otherwise
123
	 */
124
	public function rmdir($path) {
125
		if ($this->trashEnabled) {
126
			return $this->doDelete($path, 'rmdir');
127
		} else {
128
			return $this->storage->rmdir($path);
129
		}
130
	}
131
132
	/**
133
	 * check if it is a file located in data/user/files only files in the
134
	 * 'files' directory should be moved to the trash
135
	 *
136
	 * @param $path
137
	 * @return bool
138
	 */
139
	protected function shouldMoveToTrash($path) {
140
		$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path);
0 ignored issues
show
Bug introduced by
Are you sure $this->mountPoint of type OCP\Files\Mount\IMountPoint can be used in concatenation? Consider adding a __toString()-method. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
		$normalized = Filesystem::normalizePath(/** @scrutinizer ignore-type */ $this->mountPoint . '/' . $path);
Loading history...
141
		$parts = explode('/', $normalized);
142
		if (count($parts) < 4 || strpos($normalized, '/appdata_') === 0) {
143
			return false;
144
		}
145
146
		// check if there is a app which want to disable the trash bin for this file
147
		$fileId = $this->storage->getCache()->getId($path);
148
		$owner = $this->storage->getOwner($path);
149
		if ($owner === false || $this->storage->instanceOfStorage(\OCA\Files_Sharing\External\Storage::class)) {
150
			$nodes = $this->rootFolder->getById($fileId);
151
		} else {
152
			$nodes = $this->rootFolder->getUserFolder($owner)->getById($fileId);
153
		}
154
155
		foreach ($nodes as $node) {
156
			$event = $this->createMoveToTrashEvent($node);
157
			$this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event);
0 ignored issues
show
Bug introduced by
'OCA\Files_Trashbin::moveToTrash' of type string is incompatible with the type object expected by parameter $event of Symfony\Contracts\EventD...erInterface::dispatch(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
			$this->eventDispatcher->dispatch(/** @scrutinizer ignore-type */ 'OCA\Files_Trashbin::moveToTrash', $event);
Loading history...
Unused Code introduced by
The call to Symfony\Contracts\EventD...erInterface::dispatch() has too many arguments starting with $event. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

157
			$this->eventDispatcher->/** @scrutinizer ignore-call */ 
158
                           dispatch('OCA\Files_Trashbin::moveToTrash', $event);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
158
			if ($event->shouldMoveToTrashBin() === false) {
159
				return false;
160
			}
161
		}
162
163
		if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) {
164
			return true;
165
		}
166
167
		return false;
168
	}
169
170
	/**
171
	 * get move to trash event
172
	 *
173
	 * @param Node $node
174
	 * @return MoveToTrashEvent
175
	 */
176
	protected function createMoveToTrashEvent(Node $node) {
177
		return new MoveToTrashEvent($node);
178
	}
179
180
	/**
181
	 * Run the delete operation with the given method
182
	 *
183
	 * @param string $path path of file or folder to delete
184
	 * @param string $method either "unlink" or "rmdir"
185
	 *
186
	 * @return bool true if the operation succeeded, false otherwise
187
	 */
188
	private function doDelete($path, $method) {
189
		if (
190
			!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin')
191
			|| (pathinfo($path, PATHINFO_EXTENSION) === 'part')
192
			|| $this->shouldMoveToTrash($path) === false
193
		) {
194
			return call_user_func([$this->storage, $method], $path);
195
		}
196
197
		// check permissions before we continue, this is especially important for
198
		// shared files
199
		if (!$this->isDeletable($path)) {
200
			return false;
201
		}
202
203
		$isMovedToTrash = $this->trashManager->moveToTrash($this, $path);
204
		if (!$isMovedToTrash) {
205
			return call_user_func([$this->storage, $method], $path);
206
		} else {
207
			return true;
208
		}
209
	}
210
211
	/**
212
	 * Setup the storate wrapper callback
213
	 */
214
	public static function setupStorage() {
215
		\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) {
216
			return new \OCA\Files_Trashbin\Storage(
217
				['storage' => $storage, 'mountPoint' => $mountPoint],
218
				\OC::$server->query(ITrashManager::class),
219
				\OC::$server->getUserManager(),
220
				\OC::$server->getLogger(),
221
				\OC::$server->getEventDispatcher(),
222
				\OC::$server->getLazyRootFolder()
223
			);
224
		}, 1);
225
	}
226
227
	public function getMountPoint() {
228
		return $this->mountPoint;
229
	}
230
231
	public function moveFromStorage(IStorage $sourceStorage, $sourceInternalPath, $targetInternalPath) {
232
		$sourceIsTrashbin = $sourceStorage->instanceOfStorage(Storage::class);
233
		try {
234
			// the fallback for moving between storage involves a copy+delete
235
			// we don't want to trigger the trashbin when doing the delete
236
			if ($sourceIsTrashbin) {
237
				/** @var Storage $sourceStorage */
238
				$sourceStorage->disableTrash();
239
			}
240
			$result = $this->getWrapperStorage()->moveFromStorage($sourceStorage, $sourceInternalPath, $targetInternalPath);
241
			if ($sourceIsTrashbin) {
242
				/** @var Storage $sourceStorage */
243
				$sourceStorage->enableTrash();
244
			}
245
			return $result;
246
		} catch (\Exception $e) {
247
			if ($sourceIsTrashbin) {
248
				/** @var Storage $sourceStorage */
249
				$sourceStorage->enableTrash();
250
			}
251
			throw $e;
252
		}
253
	}
254
255
	protected function disableTrash() {
256
		$this->trashEnabled = false;
257
	}
258
259
	protected function enableTrash() {
260
		$this->trashEnabled = true;
261
	}
262
}
263