|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc. |
|
4
|
|
|
* |
|
5
|
|
|
* @author Björn Schießle <[email protected]> |
|
6
|
|
|
* @author Joas Schilling <[email protected]> |
|
7
|
|
|
* @author Morris Jobke <[email protected]> |
|
8
|
|
|
* @author Robin Appelman <[email protected]> |
|
9
|
|
|
* @author Roeland Jago Douma <[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_Sharing; |
|
29
|
|
|
|
|
30
|
|
|
use OC\Cache\CappedMemoryCache; |
|
31
|
|
|
use OC\Files\Filesystem; |
|
32
|
|
|
use OC\Files\Mount\MountPoint; |
|
33
|
|
|
use OC\Files\Mount\MoveableMount; |
|
34
|
|
|
use OC\Files\View; |
|
35
|
|
|
use OCP\Files\Storage\IStorageFactory; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Shared mount points can be moved by the user |
|
39
|
|
|
*/ |
|
40
|
|
|
class SharedMount extends MountPoint implements MoveableMount { |
|
41
|
|
|
/** |
|
42
|
|
|
* @var \OCA\Files_Sharing\SharedStorage $storage |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $storage = null; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @var \OC\Files\View |
|
48
|
|
|
*/ |
|
49
|
|
|
private $recipientView; |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @var string |
|
53
|
|
|
*/ |
|
54
|
|
|
private $user; |
|
55
|
|
|
|
|
56
|
|
|
/** @var \OCP\Share\IShare */ |
|
57
|
|
|
private $superShare; |
|
58
|
|
|
|
|
59
|
|
|
/** @var \OCP\Share\IShare[] */ |
|
60
|
|
|
private $groupedShares; |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param string $storage |
|
64
|
|
|
* @param SharedMount[] $mountpoints |
|
65
|
|
|
* @param array $arguments |
|
66
|
|
|
* @param IStorageFactory $loader |
|
67
|
|
|
* @param View $recipientView |
|
68
|
|
|
*/ |
|
69
|
|
|
public function __construct($storage, array $mountpoints, $arguments, IStorageFactory $loader, View $recipientView, CappedMemoryCache $folderExistCache) { |
|
70
|
|
|
$this->user = $arguments['user']; |
|
71
|
|
|
$this->recipientView = $recipientView; |
|
72
|
|
|
|
|
73
|
|
|
$this->superShare = $arguments['superShare']; |
|
74
|
|
|
$this->groupedShares = $arguments['groupedShares']; |
|
75
|
|
|
|
|
76
|
|
|
$newMountPoint = $this->verifyMountPoint($this->superShare, $mountpoints, $folderExistCache); |
|
77
|
|
|
$absMountPoint = '/' . $this->user . '/files' . $newMountPoint; |
|
78
|
|
|
parent::__construct($storage, $absMountPoint, $arguments, $loader); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* check if the parent folder exists otherwise move the mount point up |
|
83
|
|
|
* |
|
84
|
|
|
* @param \OCP\Share\IShare $share |
|
85
|
|
|
* @param SharedMount[] $mountpoints |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
|
|
private function verifyMountPoint(\OCP\Share\IShare $share, array $mountpoints, CappedMemoryCache $folderExistCache) { |
|
89
|
|
|
|
|
90
|
|
|
$mountPoint = basename($share->getTarget()); |
|
91
|
|
|
$parent = dirname($share->getTarget()); |
|
92
|
|
|
|
|
93
|
|
|
if ($folderExistCache->hasKey($parent)) { |
|
94
|
|
|
$parentExists = $folderExistCache->get($parent); |
|
95
|
|
|
} else { |
|
96
|
|
|
$parentExists = $this->recipientView->is_dir($parent); |
|
97
|
|
|
$folderExistCache->set($parent, $parentExists); |
|
98
|
|
|
} |
|
99
|
|
|
if (!$parentExists) { |
|
100
|
|
|
$parent = Helper::getShareFolder($this->recipientView); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$newMountPoint = $this->generateUniqueTarget( |
|
104
|
|
|
\OC\Files\Filesystem::normalizePath($parent . '/' . $mountPoint), |
|
105
|
|
|
$this->recipientView, |
|
106
|
|
|
$mountpoints |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
if ($newMountPoint !== $share->getTarget()) { |
|
110
|
|
|
$this->updateFileTarget($newMountPoint, $share); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $newMountPoint; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* update fileTarget in the database if the mount point changed |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $newPath |
|
120
|
|
|
* @param \OCP\Share\IShare $share |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
private function updateFileTarget($newPath, &$share) { |
|
124
|
|
|
$share->setTarget($newPath); |
|
125
|
|
|
|
|
126
|
|
|
foreach ($this->groupedShares as $tmpShare) { |
|
127
|
|
|
$tmpShare->setTarget($newPath); |
|
128
|
|
|
\OC::$server->getShareManager()->moveShare($tmpShare, $this->user); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param string $path |
|
135
|
|
|
* @param View $view |
|
136
|
|
|
* @param SharedMount[] $mountpoints |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
*/ |
|
139
|
|
|
private function generateUniqueTarget($path, $view, array $mountpoints) { |
|
140
|
|
|
$pathinfo = pathinfo($path); |
|
141
|
|
|
$ext = (isset($pathinfo['extension'])) ? '.' . $pathinfo['extension'] : ''; |
|
142
|
|
|
$name = $pathinfo['filename']; |
|
143
|
|
|
$dir = $pathinfo['dirname']; |
|
144
|
|
|
|
|
145
|
|
|
$i = 2; |
|
146
|
|
|
$absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
147
|
|
|
while ($view->file_exists($path) || isset($mountpoints[$absolutePath])) { |
|
148
|
|
|
$path = Filesystem::normalizePath($dir . '/' . $name . ' (' . $i . ')' . $ext); |
|
149
|
|
|
$absolutePath = $this->recipientView->getAbsolutePath($path) . '/'; |
|
150
|
|
|
$i++; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $path; |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Format a path to be relative to the /user/files/ directory |
|
158
|
|
|
* |
|
159
|
|
|
* @param string $path the absolute path |
|
160
|
|
|
* @return string e.g. turns '/admin/files/test.txt' into '/test.txt' |
|
161
|
|
|
* @throws \OCA\Files_Sharing\Exceptions\BrokenPath |
|
162
|
|
|
*/ |
|
163
|
|
|
protected function stripUserFilesPath($path) { |
|
164
|
|
|
$trimmed = ltrim($path, '/'); |
|
165
|
|
|
$split = explode('/', $trimmed); |
|
166
|
|
|
|
|
167
|
|
|
// it is not a file relative to data/user/files |
|
168
|
|
|
if (count($split) < 3 || $split[1] !== 'files') { |
|
169
|
|
|
\OCP\Util::writeLog('file sharing', |
|
170
|
|
|
'Can not strip userid and "files/" from path: ' . $path, |
|
171
|
|
|
\OCP\Util::ERROR); |
|
172
|
|
|
throw new \OCA\Files_Sharing\Exceptions\BrokenPath('Path does not start with /user/files', 10); |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
// skip 'user' and 'files' |
|
176
|
|
|
$sliced = array_slice($split, 2); |
|
177
|
|
|
$relPath = implode('/', $sliced); |
|
178
|
|
|
|
|
179
|
|
|
return '/' . $relPath; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Move the mount point to $target |
|
184
|
|
|
* |
|
185
|
|
|
* @param string $target the target mount point |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
|
|
public function moveMount($target) { |
|
189
|
|
|
|
|
190
|
|
|
$relTargetPath = $this->stripUserFilesPath($target); |
|
191
|
|
|
$share = $this->storage->getShare(); |
|
192
|
|
|
|
|
193
|
|
|
$result = true; |
|
194
|
|
|
|
|
195
|
|
|
try { |
|
196
|
|
|
$this->updateFileTarget($relTargetPath, $share); |
|
197
|
|
|
$this->setMountPoint($target); |
|
198
|
|
|
$this->storage->setMountPoint($relTargetPath); |
|
199
|
|
|
} catch (\Exception $e) { |
|
200
|
|
|
\OCP\Util::writeLog('file sharing', |
|
201
|
|
|
'Could not rename mount point for shared folder "' . $this->getMountPoint() . '" to "' . $target . '"', |
|
202
|
|
|
\OCP\Util::ERROR); |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
return $result; |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
/** |
|
209
|
|
|
* Remove the mount points |
|
210
|
|
|
* |
|
211
|
|
|
* @return bool |
|
212
|
|
|
*/ |
|
213
|
|
|
public function removeMount() { |
|
214
|
|
|
$mountManager = \OC\Files\Filesystem::getMountManager(); |
|
215
|
|
|
/** @var $storage \OCA\Files_Sharing\SharedStorage */ |
|
216
|
|
|
$storage = $this->getStorage(); |
|
217
|
|
|
$result = $storage->unshareStorage(); |
|
218
|
|
|
$mountManager->removeMount($this->mountPoint); |
|
219
|
|
|
|
|
220
|
|
|
return $result; |
|
221
|
|
|
} |
|
222
|
|
|
|
|
223
|
|
|
/** |
|
224
|
|
|
* @return \OCP\Share\IShare |
|
225
|
|
|
*/ |
|
226
|
|
|
public function getShare() { |
|
227
|
|
|
return $this->superShare; |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Get the file id of the root of the storage |
|
232
|
|
|
* |
|
233
|
|
|
* @return int |
|
234
|
|
|
*/ |
|
235
|
|
|
public function getStorageRootId() { |
|
236
|
|
|
return $this->getShare()->getNodeId(); |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @return int |
|
241
|
|
|
*/ |
|
242
|
|
|
public function getNumericStorageId() { |
|
243
|
|
|
if (!is_null($this->getShare()->getNodeCacheEntry())) { |
|
244
|
|
|
return $this->getShare()->getNodeCacheEntry()->getStorageId(); |
|
245
|
|
|
} else { |
|
246
|
|
|
$builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
247
|
|
|
|
|
248
|
|
|
$query = $builder->select('storage') |
|
249
|
|
|
->from('filecache') |
|
250
|
|
|
->where($builder->expr()->eq('fileid', $builder->createNamedParameter($this->getStorageRootId()))); |
|
251
|
|
|
|
|
252
|
|
|
$result = $query->execute(); |
|
253
|
|
|
$row = $result->fetch(); |
|
254
|
|
|
$result->closeCursor(); |
|
255
|
|
|
if ($row) { |
|
256
|
|
|
return (int)$row['storage']; |
|
257
|
|
|
} |
|
258
|
|
|
return -1; |
|
259
|
|
|
} |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
public function getMountType() { |
|
263
|
|
|
return 'shared'; |
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|