-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathFilesPlugin.php
508 lines (458 loc) · 15.8 KB
/
FilesPlugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
<?php
/**
* @author Björn Schießle <[email protected]>
* @author Joas Schilling <[email protected]>
* @author Lukas Reschke <[email protected]>
* @author Michael Jobst <[email protected]>
* @author Morris Jobke <[email protected]>
* @author Robin Appelman <[email protected]>
* @author Robin McCorkell <[email protected]>
* @author Roeland Jago Douma <[email protected]>
* @author Thomas Müller <[email protected]>
* @author Vincent Petry <[email protected]>
*
* @copyright Copyright (c) 2018, ownCloud GmbH
* @license AGPL-3.0
*
* 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.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* 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/>
*
*/
namespace OCA\DAV\Connector\Sabre;
use OC\AppFramework\Http\Request;
use OC\Files\Filesystem;
use OCA\DAV\Files\IProvidesAdditionalHeaders;
use OCP\Files\ForbiddenException;
use OCP\Files\StorageNotAvailableException;
use OCP\IConfig;
use OCP\IRequest;
use Sabre\DAV\Exception\BadRequest;
use Sabre\DAV\Exception\Forbidden;
use Sabre\DAV\Exception\NotFound;
use Sabre\DAV\IFile;
use Sabre\DAV\PropFind;
use Sabre\DAV\PropPatch;
use Sabre\DAV\ServerPlugin;
use Sabre\DAV\Tree;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use OCA\DAV\Connector\Sabre\Node;
class FilesPlugin extends ServerPlugin {
// namespace
public const NS_OWNCLOUD = 'http://owncloud.org/ns';
public const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id';
public const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid';
public const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions';
public const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions';
public const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL';
public const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size';
public const GETETAG_PROPERTYNAME = '{DAV:}getetag';
public const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified';
public const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id';
public const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name';
public const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums';
public const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint';
public const PRIVATE_LINK_PROPERTYNAME = '{http://owncloud.org/ns}privatelink';
/**
* Reference to main server object
*
* @var \Sabre\DAV\Server
*/
private $server;
/**
* @var Tree
*/
private $tree;
/**
* Whether this is public webdav.
* If true, some returned information will be stripped off.
*
* @var bool
*/
private $isPublic;
/**
* @var bool
*/
private $downloadAttachment;
/**
* @var IConfig
*/
private $config;
/**
* @var IRequest
*/
private $request;
/**
* @param Tree $tree
* @param IConfig $config
* @param IRequest $request
* @param bool $isPublic
* @param bool $downloadAttachment
*/
public function __construct(
Tree $tree,
IConfig $config,
IRequest $request,
$isPublic = false,
$downloadAttachment = true
) {
$this->tree = $tree;
$this->config = $config;
$this->request = $request;
$this->isPublic = $isPublic;
$this->downloadAttachment = $downloadAttachment;
}
/**
* This initializes the plugin.
*
* This function is called by \Sabre\DAV\Server, after
* addPlugin is called.
*
* This method should set up the required event subscriptions.
*
* @param \Sabre\DAV\Server $server
* @return void
*/
public function initialize(\Sabre\DAV\Server $server) {
$server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc';
$server->protectedProperties[] = self::FILEID_PROPERTYNAME;
$server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME;
$server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME;
$server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME;
$server->protectedProperties[] = self::SIZE_PROPERTYNAME;
$server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME;
$server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME;
$server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME;
$server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME;
$server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME;
$server->protectedProperties[] = self::PRIVATE_LINK_PROPERTYNAME;
// normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH
$allowedProperties = ['{DAV:}getetag'];
$server->protectedProperties = \array_diff($server->protectedProperties, $allowedProperties);
$this->server = $server;
$this->server->on('propFind', [$this, 'handleGetProperties']);
$this->server->on('propPatch', [$this, 'handleUpdateProperties']);
$this->server->on('afterBind', [$this, 'sendFileIdHeader']);
$this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']);
$this->server->on('afterMethod:GET', [$this, 'httpGet']);
$this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']);
$this->server->on('exception', [$this, 'handleDownloadFailure']);
$this->server->on('afterResponse', function ($request, ResponseInterface $response) {
$body = $response->getBody();
if (\is_resource($body)) {
\fclose($body);
}
});
$this->server->on('beforeMove', [$this, 'checkMove']);
$this->server->on('validateTokens', [$this, 'validateTokens'], 0);
$this->server->on('method:PROPFIND', [$this, 'checkPropFind']);
}
/**
* Check if the file tree can be found and if it's traversable.
* Also check for read permissions if the requested resource is
* a share, since a share can be create-only.
*
* @param RequestInterface $request
*/
public function checkPropFind($request) {
$path = $request->getPath();
$node = $this->server->tree->getNodeForPath($path);
if ($node instanceof Node) {
/** @var Node $node */
'@phan-var Node $node';
$fileInfo = $node->getFileInfo();
list($storage, ) = Filesystem::resolvePath($fileInfo->getPath());
if ($storage->instanceOfStorage('\OCA\Files_Sharing\SharedStorage')) {
/** @var \OCA\Files_Sharing\SharedStorage $storage */
'@phan-var \OCA\Files_Sharing\SharedStorage $storage';
$hasReadPermission = ($storage->getShare()->getPermissions() & \OCP\Constants::PERMISSION_READ) > 0;
if (!$hasReadPermission) {
throw new NotFound();
}
}
}
}
/**
* This methods gets called before the validateTokens-method
* in the sabre implementation due to its higher priority.
*
* @param RequestInterface $request
* @param mixed $conditions
* @throws BadRequest
*/
public function validateTokens($request, &$conditions) {
$method = $request->getMethod();
// Move- and copy-methods need a destination header to be validated.
// The validateTokens-method in the sabre implementation does not check this...
if (\in_array($method, ['MOVE', 'COPY']) && !$request->getHeader('Destination')) {
throw new BadRequest('The destination header was not supplied');
}
}
/**
* Plugin that checks if a move can actually be performed.
*
* @param string $source source path
* @param string $destination destination path
* @throws Forbidden
* @throws NotFound
*/
public function checkMove($source, $destination) {
$sourceNode = $this->tree->getNodeForPath($source);
if (!$sourceNode instanceof Node) {
return;
}
list($sourceDir, ) = \Sabre\Uri\split($source);
list($destinationDir, ) = \Sabre\Uri\split($destination);
if ($sourceDir !== $destinationDir) {
$sourceNodeFileInfo = $sourceNode->getFileInfo();
if ($sourceNodeFileInfo === null) {
throw new NotFound($source . ' does not exist');
}
if (!$sourceNodeFileInfo->isDeletable()) {
throw new Forbidden($source . " cannot be deleted");
}
}
}
/**
* This sets a cookie to be able to recognize the failure of the download
*
* @param \Throwable $ex
*/
public function handleDownloadFailure(\Throwable $ex) {
$queryParams = $this->server->httpRequest->getQueryParameters();
if (isset($queryParams['downloadStartSecret'])) {
$this->setSecretCookie('ocDownloadStarted', '-1');
}
}
/**
* This sets a cookie to be able to recognize the start of the download
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
public function handleDownloadToken(RequestInterface $request, ResponseInterface $response) {
$queryParams = $request->getQueryParameters();
if (isset($queryParams['downloadStartSecret'])) {
$this->setSecretCookie('ocDownloadStarted', $queryParams['downloadStartSecret']);
}
}
/**
* Add headers to file download
*
* @param RequestInterface $request
* @param ResponseInterface $response
*/
public function httpGet(RequestInterface $request, ResponseInterface $response) {
// Only handle valid files
$node = $this->tree->getNodeForPath($request->getPath());
if (!($node instanceof IFile)) {
return;
}
// adds a 'Content-Disposition: attachment' header
if ($this->downloadAttachment) {
$filename = $node->getName();
if ($node instanceof IProvidesAdditionalHeaders) {
$filename = $node->getContentDispositionFileName();
}
if ($this->request->isUserAgent(
[
Request::USER_AGENT_IE,
Request::USER_AGENT_ANDROID_MOBILE_CHROME,
Request::USER_AGENT_FREEBOX,
]
)) {
$response->setHeader('Content-Disposition', 'attachment; filename="' . \rawurlencode($filename) . '"');
} else {
$response->setHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . \rawurlencode($filename)
. '; filename="' . \rawurlencode($filename) . '"');
}
}
if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
//Add OC-Checksum header
/** @var $node File */
$checksum = $node->getChecksum('sha1');
if ($checksum !== null && $checksum !== '') {
$response->addHeader('OC-Checksum', $checksum);
}
// disable nginx buffering so big downloads through ownCloud won't
// cause memory problems in the nginx process.
$response->addHeader('X-Accel-Buffering', 'no');
}
if ($node instanceof IProvidesAdditionalHeaders) {
$headers = $node->getHeaders();
if (\is_array($headers)) {
$response->addHeaders($headers);
}
}
}
/**
* Adds all ownCloud-specific properties
*
* @param PropFind $propFind
* @param \Sabre\DAV\INode $node
* @return void
*/
public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) {
$httpRequest = $this->server->httpRequest;
if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
if (!$node->getFileInfo()->isReadable()) {
// avoid detecting files through this means
throw new NotFound();
}
$propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) {
return $node->getFileId();
});
$propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) {
return $node->getInternalFileId();
});
$propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) {
$perms = $node->getDavPermissions();
if ($this->isPublic) {
// remove mount information
$perms = \str_replace(['S', 'M'], '', $perms);
}
return $perms;
});
$propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) {
try {
return $node->getSharePermissions(
$httpRequest->getRawServerValue('PHP_AUTH_USER')
);
} catch (StorageNotAvailableException $ex) {
return null;
}
});
$propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node) {
return $node->getETag();
});
$propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node) {
$owner = $node->getOwner();
return $owner->getUID();
});
$propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node) {
$owner = $node->getOwner();
$displayName = $owner->getDisplayName();
return $displayName;
});
$propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) {
return $node->getSize();
});
$propFind->handle(self::PRIVATE_LINK_PROPERTYNAME, function () use ($node) {
return \OC::$server->getURLGenerator()
->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileId' => $node->getInternalFileId()]);
});
}
if ($node instanceof \OCA\DAV\Connector\Sabre\Node) {
$propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () {
return $this->config->getSystemValue('data-fingerprint', '');
});
}
if ($node instanceof \OCA\DAV\Connector\Sabre\File) {
$propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) {
/** @var $node \OCA\DAV\Connector\Sabre\File */
try {
$directDownloadUrl = $node->getDirectDownload();
if (isset($directDownloadUrl['url'])) {
return $directDownloadUrl['url'];
}
} catch (StorageNotAvailableException $e) {
return false;
} catch (ForbiddenException $e) {
return false;
}
return false;
});
$propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) {
$checksum = $node->getChecksum();
if ($checksum === null || $checksum === '') {
return null;
}
return new ChecksumList($checksum);
});
}
if ($node instanceof \OCA\DAV\Connector\Sabre\Directory) {
$propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) {
return $node->getSize();
});
}
}
/**
* Update ownCloud-specific properties
*
* @param string $path
* @param PropPatch $propPatch
*
* @return void
*/
public function handleUpdateProperties($path, PropPatch $propPatch) {
$node = $this->tree->getNodeForPath($path);
if (!($node instanceof \OCA\DAV\Connector\Sabre\Node)) {
return;
}
$propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) {
if (empty($time)) {
return false;
}
$node->touch($time);
return true;
});
$propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) {
if (empty($etag)) {
return false;
}
if ($node->setEtag($etag) !== -1) {
return true;
}
return false;
});
}
/**
* @param string $filePath
* @param \Sabre\DAV\INode $node
* @throws \Sabre\DAV\Exception\BadRequest
*/
public function sendFileIdHeader($filePath, \Sabre\DAV\INode $node = null) {
// chunked upload handling
if (\OC_FileChunking::isWebdavChunk()) {
list($path, $name) = \Sabre\Uri\split($filePath);
$info = \OC_FileChunking::decodeName($name);
if (!empty($info)) {
$filePath = $path . '/' . $info['name'];
}
}
// we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder
if (!$this->server->tree->nodeExists($filePath)) {
return;
}
$nodeForPath = $this->server->tree->getNodeForPath($filePath);
if ($nodeForPath instanceof \OCA\DAV\Connector\Sabre\Node) {
$fileId = $nodeForPath->getFileId();
if ($fileId !== null) {
$this->server->httpResponse->setHeader('OC-FileId', $fileId);
}
}
}
/**
* This sets a cookie, which content must not be longer than 32 characters and must only contain
* alphanumeric characters if request is successfuk, or -1 if request failed.
*
* @param $secretName
* @param $secretToken
*/
private function setSecretCookie($secretName, $secretToken) {
if ($secretToken === '-1' || (!isset($secretToken[32])
&& \preg_match('!^[a-zA-Z0-9]+$!', $secretToken) === 1)) {
// FIXME: use $response->setHeader() instead
\setcookie($secretName, $secretToken, \time() + 20, '/');
}
}
}