|
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 Joas Schilling <[email protected]> |
|
8
|
|
|
* @author Morris Jobke <[email protected]> |
|
9
|
|
|
* @author Robin Appelman <[email protected]> |
|
10
|
|
|
* @author Vincent Petry <[email protected]> |
|
11
|
|
|
* |
|
12
|
|
|
* @license AGPL-3.0 |
|
13
|
|
|
* |
|
14
|
|
|
* This code is free software: you can redistribute it and/or modify |
|
15
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
|
16
|
|
|
* as published by the Free Software Foundation. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3, |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace OCA\Files_Trashbin; |
|
29
|
|
|
|
|
30
|
|
|
use OC\Files\Filesystem; |
|
31
|
|
|
use OC\Files\Storage\Wrapper\Wrapper; |
|
32
|
|
|
use OC\Files\View; |
|
33
|
|
|
use OCA\Files_Trashbin\Events\MoveToTrashEvent; |
|
34
|
|
|
use OCA\Files_Trashbin\Trash\ITrashManager; |
|
35
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException; |
|
36
|
|
|
use OCP\Files\IRootFolder; |
|
37
|
|
|
use OCP\Files\Mount\IMountPoint; |
|
38
|
|
|
use OCP\Files\Node; |
|
39
|
|
|
use OCP\ILogger; |
|
40
|
|
|
use OCP\IUserManager; |
|
41
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
|
42
|
|
|
|
|
43
|
|
|
class Storage extends Wrapper { |
|
44
|
|
|
/** @var IMountPoint */ |
|
45
|
|
|
private $mountPoint; |
|
46
|
|
|
|
|
47
|
|
|
/** @var IUserManager */ |
|
48
|
|
|
private $userManager; |
|
49
|
|
|
|
|
50
|
|
|
/** @var ILogger */ |
|
51
|
|
|
private $logger; |
|
52
|
|
|
|
|
53
|
|
|
/** @var EventDispatcher */ |
|
54
|
|
|
private $eventDispatcher; |
|
55
|
|
|
|
|
56
|
|
|
/** @var IRootFolder */ |
|
57
|
|
|
private $rootFolder; |
|
58
|
|
|
|
|
59
|
|
|
/** @var ITrashManager */ |
|
60
|
|
|
private $trashManager; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Storage constructor. |
|
64
|
|
|
* |
|
65
|
|
|
* @param array $parameters |
|
66
|
|
|
* @param ITrashManager $trashManager |
|
67
|
|
|
* @param IUserManager|null $userManager |
|
68
|
|
|
* @param ILogger|null $logger |
|
69
|
|
|
* @param EventDispatcher|null $eventDispatcher |
|
70
|
|
|
* @param IRootFolder|null $rootFolder |
|
71
|
|
|
*/ |
|
72
|
|
|
public function __construct( |
|
73
|
|
|
$parameters, |
|
74
|
|
|
ITrashManager $trashManager = null, |
|
75
|
|
|
IUserManager $userManager = null, |
|
76
|
|
|
ILogger $logger = null, |
|
77
|
|
|
EventDispatcher $eventDispatcher = null, |
|
78
|
|
|
IRootFolder $rootFolder = null |
|
79
|
|
|
) { |
|
80
|
|
|
$this->mountPoint = $parameters['mountPoint']; |
|
81
|
|
|
$this->trashManager = $trashManager; |
|
82
|
|
|
$this->userManager = $userManager; |
|
83
|
|
|
$this->logger = $logger; |
|
84
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
85
|
|
|
$this->rootFolder = $rootFolder; |
|
86
|
|
|
parent::__construct($parameters); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Deletes the given file by moving it into the trashbin. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $path path of file or folder to delete |
|
93
|
|
|
* |
|
94
|
|
|
* @return bool true if the operation succeeded, false otherwise |
|
95
|
|
|
*/ |
|
96
|
|
|
public function unlink($path) { |
|
97
|
|
|
try { |
|
98
|
|
|
return $this->doDelete($path, 'unlink'); |
|
99
|
|
|
} catch (GenericEncryptionException $e) { |
|
100
|
|
|
// in case of a encryption exception we delete the file right away |
|
101
|
|
|
$this->logger->info( |
|
102
|
|
|
"Can't move file" . $path . |
|
103
|
|
|
"to the trash bin, therefore it was deleted right away"); |
|
104
|
|
|
|
|
105
|
|
|
return $this->storage->unlink($path); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Deletes the given folder by moving it into the trashbin. |
|
111
|
|
|
* |
|
112
|
|
|
* @param string $path path of folder to delete |
|
113
|
|
|
* |
|
114
|
|
|
* @return bool true if the operation succeeded, false otherwise |
|
115
|
|
|
*/ |
|
116
|
|
|
public function rmdir($path) { |
|
117
|
|
|
return $this->doDelete($path, 'rmdir'); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* check if it is a file located in data/user/files only files in the |
|
122
|
|
|
* 'files' directory should be moved to the trash |
|
123
|
|
|
* |
|
124
|
|
|
* @param $path |
|
125
|
|
|
* @return bool |
|
126
|
|
|
*/ |
|
127
|
|
|
protected function shouldMoveToTrash($path) { |
|
128
|
|
|
|
|
129
|
|
|
// check if there is a app which want to disable the trash bin for this file |
|
130
|
|
|
$fileId = $this->storage->getCache()->getId($path); |
|
131
|
|
|
$nodes = $this->rootFolder->getById($fileId); |
|
132
|
|
|
foreach ($nodes as $node) { |
|
133
|
|
|
$event = $this->createMoveToTrashEvent($node); |
|
134
|
|
|
$this->eventDispatcher->dispatch('OCA\Files_Trashbin::moveToTrash', $event); |
|
135
|
|
|
if ($event->shouldMoveToTrashBin() === false) { |
|
136
|
|
|
return false; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$normalized = Filesystem::normalizePath($this->mountPoint . '/' . $path); |
|
141
|
|
|
$parts = explode('/', $normalized); |
|
142
|
|
|
if (count($parts) < 4) { |
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
if ($parts[2] === 'files' && $this->userManager->userExists($parts[1])) { |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return false; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* get move to trash event |
|
155
|
|
|
* |
|
156
|
|
|
* @param Node $node |
|
157
|
|
|
* @return MoveToTrashEvent |
|
158
|
|
|
*/ |
|
159
|
|
|
protected function createMoveToTrashEvent(Node $node) { |
|
160
|
|
|
return new MoveToTrashEvent($node); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Run the delete operation with the given method |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $path path of file or folder to delete |
|
167
|
|
|
* @param string $method either "unlink" or "rmdir" |
|
168
|
|
|
* |
|
169
|
|
|
* @return bool true if the operation succeeded, false otherwise |
|
170
|
|
|
*/ |
|
171
|
|
|
private function doDelete($path, $method) { |
|
172
|
|
|
if ( |
|
173
|
|
|
!\OC::$server->getAppManager()->isEnabledForUser('files_trashbin') |
|
174
|
|
|
|| (pathinfo($path, PATHINFO_EXTENSION) === 'part') |
|
175
|
|
|
|| $this->shouldMoveToTrash($path) === false |
|
176
|
|
|
) { |
|
177
|
|
|
return call_user_func([$this->storage, $method], $path); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
// check permissions before we continue, this is especially important for |
|
181
|
|
|
// shared files |
|
182
|
|
|
if (!$this->isDeletable($path)) { |
|
183
|
|
|
return false; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
$isMovedToTrash = $this->trashManager->moveToTrash($this, $path); |
|
187
|
|
|
if (!$isMovedToTrash) { |
|
188
|
|
|
return call_user_func([$this->storage, $method], $path); |
|
189
|
|
|
} else { |
|
190
|
|
|
return true; |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Setup the storate wrapper callback |
|
196
|
|
|
*/ |
|
197
|
|
|
public static function setupStorage() { |
|
198
|
|
|
\OC\Files\Filesystem::addStorageWrapper('oc_trashbin', function ($mountPoint, $storage) { |
|
199
|
|
|
return new \OCA\Files_Trashbin\Storage( |
|
200
|
|
|
['storage' => $storage, 'mountPoint' => $mountPoint], |
|
201
|
|
|
\OC::$server->query(ITrashManager::class), |
|
202
|
|
|
\OC::$server->getUserManager(), |
|
203
|
|
|
\OC::$server->getLogger(), |
|
204
|
|
|
\OC::$server->getEventDispatcher(), |
|
205
|
|
|
\OC::$server->getLazyRootFolder() |
|
206
|
|
|
); |
|
207
|
|
|
}, 1); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
public function getMountPoint() { |
|
211
|
|
|
return $this->mountPoint; |
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|