Issues (2553)

dav/lib/Connector/Sabre/ChecksumUpdatePlugin.php (1 issue)

1
<?php
2
3
declare(strict_types=1);
4
/**
5
 * @copyright Copyright (c) 2021 Robin Appelman <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OCA\DAV\Connector\Sabre;
25
26
use Sabre\DAV\ServerPlugin;
27
use Sabre\HTTP\RequestInterface;
28
use Sabre\HTTP\ResponseInterface;
29
use Sabre\DAV\Server;
30
31
class ChecksumUpdatePlugin extends ServerPlugin {
32
	protected ?Server $server = null;
33
34
	public function initialize(Server $server) {
35
		$this->server = $server;
36
		$server->on('method:PATCH', [$this, 'httpPatch']);
37
	}
38
39
	public function getPluginName(): string {
40
		return 'checksumupdate';
41
	}
42
43
	/** @return string[] */
44
	public function getHTTPMethods($path): array {
45
		$tree = $this->server->tree;
46
47
		if ($tree->nodeExists($path)) {
48
			$node = $tree->getNodeForPath($path);
49
			if ($node instanceof File) {
50
				return ['PATCH'];
51
			}
52
		}
53
54
		return [];
55
	}
56
57
	/** @return string[] */
58
	public function getFeatures(): array {
59
		return ['nextcloud-checksum-update'];
60
	}
61
62
	public function httpPatch(RequestInterface $request, ResponseInterface $response) {
63
		$path = $request->getPath();
64
65
		$node = $this->server->tree->getNodeForPath($path);
66
		if ($node instanceof File) {
67
			$type = strtolower(
68
				(string)$request->getHeader('X-Recalculate-Hash')
69
			);
70
71
			$hash = $node->hash($type);
72
			if ($hash) {
73
				$checksum = strtoupper($type) . ':' . $hash;
0 ignored issues
show
Are you sure $hash of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

73
				$checksum = strtoupper($type) . ':' . /** @scrutinizer ignore-type */ $hash;
Loading history...
74
				$node->setChecksum($checksum);
75
				$response->addHeader('OC-Checksum', $checksum);
76
				$response->setHeader('Content-Length', '0');
77
				$response->setStatus(204);
78
79
				return false;
80
			}
81
		}
82
	}
83
}
84