aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Files/Storage/DAV.php
diff options
context:
space:
mode:
Diffstat (limited to 'lib/private/Files/Storage/DAV.php')
-rw-r--r--lib/private/Files/Storage/DAV.php277
1 files changed, 95 insertions, 182 deletions
diff --git a/lib/private/Files/Storage/DAV.php b/lib/private/Files/Storage/DAV.php
index 6c7d329c48e..2d166b5438d 100644
--- a/lib/private/Files/Storage/DAV.php
+++ b/lib/private/Files/Storage/DAV.php
@@ -1,39 +1,9 @@
<?php
+
/**
- * @copyright Copyright (c) 2016, ownCloud, Inc.
- *
- * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
- * @author Bart Visscher <bartv@thisnet.nl>
- * @author Björn Schießle <bjoern@schiessle.org>
- * @author Carlos Cerrillo <ccerrillo@gmail.com>
- * @author Christoph Wurst <christoph@winzerhof-wurst.at>
- * @author Daniel Kesselberg <mail@danielkesselberg.de>
- * @author Joas Schilling <coding@schilljs.com>
- * @author Jörn Friedrich Dreyer <jfd@butonic.de>
- * @author Lukas Reschke <lukas@statuscode.ch>
- * @author Michael Gapczynski <GapczynskiM@gmail.com>
- * @author Morris Jobke <hey@morrisjobke.de>
- * @author Philipp Kapfer <philipp.kapfer@gmx.at>
- * @author Robin Appelman <robin@icewind.nl>
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- * @author Thomas Müller <thomas.mueller@tmit.eu>
- * @author Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
- * @author Vincent Petry <vincent@nextcloud.com>
- *
- * @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/>
- *
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
*/
namespace OC\Files\Storage;
@@ -50,9 +20,11 @@ use OCP\Files\ForbiddenException;
use OCP\Files\IMimeTypeDetector;
use OCP\Files\StorageInvalidException;
use OCP\Files\StorageNotAvailableException;
+use OCP\Http\Client\IClient;
use OCP\Http\Client\IClientService;
use OCP\ICertificateManager;
use OCP\IConfig;
+use OCP\Server;
use OCP\Util;
use Psr\Http\Message\ResponseInterface;
use Psr\Log\LoggerInterface;
@@ -111,31 +83,31 @@ class DAV extends Common {
];
/**
- * @param array $params
+ * @param array $parameters
* @throws \Exception
*/
- public function __construct($params) {
+ public function __construct(array $parameters) {
$this->statCache = new ArrayCache();
- $this->httpClientService = \OC::$server->getHTTPClientService();
- if (isset($params['host']) && isset($params['user']) && isset($params['password'])) {
- $host = $params['host'];
+ $this->httpClientService = Server::get(IClientService::class);
+ if (isset($parameters['host']) && isset($parameters['user']) && isset($parameters['password'])) {
+ $host = $parameters['host'];
//remove leading http[s], will be generated in createBaseUri()
- if (str_starts_with($host, "https://")) {
+ if (str_starts_with($host, 'https://')) {
$host = substr($host, 8);
- } elseif (str_starts_with($host, "http://")) {
+ } elseif (str_starts_with($host, 'http://')) {
$host = substr($host, 7);
}
$this->host = $host;
- $this->user = $params['user'];
- $this->password = $params['password'];
- if (isset($params['authType'])) {
- $this->authType = $params['authType'];
+ $this->user = $parameters['user'];
+ $this->password = $parameters['password'];
+ if (isset($parameters['authType'])) {
+ $this->authType = $parameters['authType'];
}
- if (isset($params['secure'])) {
- if (is_string($params['secure'])) {
- $this->secure = ($params['secure'] === 'true');
+ if (isset($parameters['secure'])) {
+ if (is_string($parameters['secure'])) {
+ $this->secure = ($parameters['secure'] === 'true');
} else {
- $this->secure = (bool)$params['secure'];
+ $this->secure = (bool)$parameters['secure'];
}
} else {
$this->secure = false;
@@ -144,20 +116,20 @@ class DAV extends Common {
// inject mock for testing
$this->certManager = \OC::$server->getCertificateManager();
}
- $this->root = $params['root'] ?? '/';
+ $this->root = rawurldecode($parameters['root'] ?? '/');
$this->root = '/' . ltrim($this->root, '/');
$this->root = rtrim($this->root, '/') . '/';
} else {
throw new \Exception('Invalid webdav storage configuration');
}
- $this->logger = \OC::$server->get(LoggerInterface::class);
- $this->eventLogger = \OC::$server->get(IEventLogger::class);
+ $this->logger = Server::get(LoggerInterface::class);
+ $this->eventLogger = Server::get(IEventLogger::class);
// This timeout value will be used for the download and upload of files
- $this->timeout = \OC::$server->get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', 30);
+ $this->timeout = Server::get(IConfig::class)->getSystemValueInt('davstorage.request_timeout', IClient::DEFAULT_REQUEST_TIMEOUT);
$this->mimeTypeDetector = \OC::$server->getMimeTypeDetector();
}
- protected function init() {
+ protected function init(): void {
if ($this->ready) {
return;
}
@@ -172,7 +144,7 @@ class DAV extends Common {
$settings['authType'] = $this->authType;
}
- $proxy = \OC::$server->getConfig()->getSystemValueString('proxy', '');
+ $proxy = Server::get(IConfig::class)->getSystemValueString('proxy', '');
if ($proxy !== '') {
$settings['proxy'] = $proxy;
}
@@ -192,13 +164,13 @@ class DAV extends Common {
$lastRequestStart = 0;
$this->client->on('beforeRequest', function (RequestInterface $request) use (&$lastRequestStart) {
- $this->logger->debug("sending dav " . $request->getMethod() . " request to external storage: " . $request->getAbsoluteUrl(), ['app' => 'dav']);
+ $this->logger->debug('sending dav ' . $request->getMethod() . ' request to external storage: ' . $request->getAbsoluteUrl(), ['app' => 'dav']);
$lastRequestStart = microtime(true);
- $this->eventLogger->start('fs:storage:dav:request', "Sending dav request to external storage");
+ $this->eventLogger->start('fs:storage:dav:request', 'Sending dav request to external storage');
});
$this->client->on('afterRequest', function (RequestInterface $request) use (&$lastRequestStart) {
$elapsed = microtime(true) - $lastRequestStart;
- $this->logger->debug("dav " . $request->getMethod() . " request to external storage: " . $request->getAbsoluteUrl() . " took " . round($elapsed * 1000, 1) . "ms", ['app' => 'dav']);
+ $this->logger->debug('dav ' . $request->getMethod() . ' request to external storage: ' . $request->getAbsoluteUrl() . ' took ' . round($elapsed * 1000, 1) . 'ms', ['app' => 'dav']);
$this->eventLogger->end('fs:storage:dav:request');
});
}
@@ -206,27 +178,24 @@ class DAV extends Common {
/**
* Clear the stat cache
*/
- public function clearStatCache() {
+ public function clearStatCache(): void {
$this->statCache->clear();
}
- /** {@inheritdoc} */
- public function getId() {
+ public function getId(): string {
return 'webdav::' . $this->user . '@' . $this->host . '/' . $this->root;
}
- /** {@inheritdoc} */
- public function createBaseUri() {
+ public function createBaseUri(): string {
$baseUri = 'http';
if ($this->secure) {
$baseUri .= 's';
}
- $baseUri .= '://' . $this->host . $this->root;
+ $baseUri .= '://' . $this->host . $this->encodePath($this->root);
return $baseUri;
}
- /** {@inheritdoc} */
- public function mkdir($path) {
+ public function mkdir(string $path): bool {
$this->init();
$path = $this->cleanPath($path);
$result = $this->simpleResponse('MKCOL', $path, null, 201);
@@ -236,8 +205,7 @@ class DAV extends Common {
return $result;
}
- /** {@inheritdoc} */
- public function rmdir($path) {
+ public function rmdir(string $path): bool {
$this->init();
$path = $this->cleanPath($path);
// FIXME: some WebDAV impl return 403 when trying to DELETE
@@ -248,8 +216,7 @@ class DAV extends Common {
return $result;
}
- /** {@inheritdoc} */
- public function opendir($path) {
+ public function opendir(string $path) {
$this->init();
$path = $this->cleanPath($path);
try {
@@ -273,16 +240,17 @@ class DAV extends Common {
*
* @param string $path path to propfind
*
- * @return array|boolean propfind response or false if the entry was not found
+ * @return array|false propfind response or false if the entry was not found
*
* @throws ClientHttpException
*/
- protected function propfind($path) {
+ protected function propfind(string $path): array|false {
$path = $this->cleanPath($path);
$cachedResponse = $this->statCache->get($path);
// we either don't know it, or we know it exists but need more details
if (is_null($cachedResponse) || $cachedResponse === true) {
$this->init();
+ $response = false;
try {
$response = $this->client->propFind(
$this->encodePath($path),
@@ -293,9 +261,9 @@ class DAV extends Common {
if ($e->getHttpStatus() === 404 || $e->getHttpStatus() === 405) {
$this->statCache->clear($path . '/');
$this->statCache->set($path, false);
- return false;
+ } else {
+ $this->convertException($e, $path);
}
- $this->convertException($e, $path);
} catch (\Exception $e) {
$this->convertException($e, $path);
}
@@ -305,27 +273,25 @@ class DAV extends Common {
return $response;
}
- /** {@inheritdoc} */
- public function filetype($path) {
+ public function filetype(string $path): string|false {
try {
$response = $this->propfind($path);
if ($response === false) {
return false;
}
$responseType = [];
- if (isset($response["{DAV:}resourcetype"])) {
+ if (isset($response['{DAV:}resourcetype'])) {
/** @var ResourceType[] $response */
- $responseType = $response["{DAV:}resourcetype"]->getValue();
+ $responseType = $response['{DAV:}resourcetype']->getValue();
}
- return (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
+ return (count($responseType) > 0 && $responseType[0] == '{DAV:}collection') ? 'dir' : 'file';
} catch (\Exception $e) {
$this->convertException($e, $path);
}
return false;
}
- /** {@inheritdoc} */
- public function file_exists($path) {
+ public function file_exists(string $path): bool {
try {
$path = $this->cleanPath($path);
$cachedState = $this->statCache->get($path);
@@ -343,8 +309,7 @@ class DAV extends Common {
return false;
}
- /** {@inheritdoc} */
- public function unlink($path) {
+ public function unlink(string $path): bool {
$this->init();
$path = $this->cleanPath($path);
$result = $this->simpleResponse('DELETE', $path, null, 204);
@@ -353,8 +318,7 @@ class DAV extends Common {
return $result;
}
- /** {@inheritdoc} */
- public function fopen($path, $mode) {
+ public function fopen(string $path, string $mode) {
$this->init();
$path = $this->cleanPath($path);
switch ($mode) {
@@ -382,11 +346,17 @@ class DAV extends Common {
if ($response->getStatusCode() === Http::STATUS_LOCKED) {
throw new \OCP\Lock\LockedException($path);
} else {
- \OC::$server->get(LoggerInterface::class)->error('Guzzle get returned status code ' . $response->getStatusCode(), ['app' => 'webdav client']);
+ $this->logger->error('Guzzle get returned status code ' . $response->getStatusCode(), ['app' => 'webdav client']);
}
}
- return $response->getBody();
+ $content = $response->getBody();
+
+ if ($content === null || is_string($content)) {
+ return false;
+ }
+
+ return $content;
case 'w':
case 'wb':
case 'a':
@@ -410,7 +380,7 @@ class DAV extends Common {
if (!$this->isUpdatable($path)) {
return false;
}
- if ($mode === 'w' or $mode === 'w+') {
+ if ($mode === 'w' || $mode === 'w+') {
$tmpFile = $tempManager->getTemporaryFile($ext);
} else {
$tmpFile = $this->getCachedFile($path);
@@ -426,18 +396,16 @@ class DAV extends Common {
$this->writeBack($tmpFile, $path);
});
}
+
+ return false;
}
- /**
- * @param string $tmpFile
- */
- public function writeBack($tmpFile, $path) {
+ public function writeBack(string $tmpFile, string $path): void {
$this->uploadFile($tmpFile, $path);
unlink($tmpFile);
}
- /** {@inheritdoc} */
- public function free_space($path) {
+ public function free_space(string $path): int|float|false {
$this->init();
$path = $this->cleanPath($path);
try {
@@ -455,8 +423,7 @@ class DAV extends Common {
}
}
- /** {@inheritdoc} */
- public function touch($path, $mtime = null) {
+ public function touch(string $path, ?int $mtime = null): bool {
$this->init();
if (is_null($mtime)) {
$mtime = time();
@@ -493,23 +460,14 @@ class DAV extends Common {
return true;
}
- /**
- * @param string $path
- * @param mixed $data
- * @return int|float|false
- */
- public function file_put_contents($path, $data) {
+ public function file_put_contents(string $path, mixed $data): int|float|false {
$path = $this->cleanPath($path);
$result = parent::file_put_contents($path, $data);
$this->statCache->remove($path);
return $result;
}
- /**
- * @param string $path
- * @param string $target
- */
- protected function uploadFile($path, $target) {
+ protected function uploadFile(string $path, string $target): void {
$this->init();
// invalidate
@@ -529,8 +487,7 @@ class DAV extends Common {
$this->removeCachedFile($target);
}
- /** {@inheritdoc} */
- public function rename($source, $target) {
+ public function rename(string $source, string $target): bool {
$this->init();
$source = $this->cleanPath($source);
$target = $this->cleanPath($target);
@@ -561,8 +518,7 @@ class DAV extends Common {
return false;
}
- /** {@inheritdoc} */
- public function copy($source, $target) {
+ public function copy(string $source, string $target): bool {
$this->init();
$source = $this->cleanPath($source);
$target = $this->cleanPath($target);
@@ -590,7 +546,7 @@ class DAV extends Common {
return false;
}
- public function getMetaData($path) {
+ public function getMetaData(string $path): ?array {
if (Filesystem::isFileBlacklisted($path)) {
throw new ForbiddenException('Invalid path: ' . $path, false);
}
@@ -612,11 +568,11 @@ class DAV extends Common {
}
$responseType = [];
- if (isset($response["{DAV:}resourcetype"])) {
+ if (isset($response['{DAV:}resourcetype'])) {
/** @var ResourceType[] $response */
- $responseType = $response["{DAV:}resourcetype"]->getValue();
+ $responseType = $response['{DAV:}resourcetype']->getValue();
}
- $type = (count($responseType) > 0 and $responseType[0] == "{DAV:}collection") ? 'dir' : 'file';
+ $type = (count($responseType) > 0 && $responseType[0] == '{DAV:}collection') ? 'dir' : 'file';
if ($type === 'dir') {
$mimeType = 'httpd/unix-directory';
} elseif (isset($response['{DAV:}getcontenttype'])) {
@@ -652,31 +608,18 @@ class DAV extends Common {
];
}
- /** {@inheritdoc} */
- public function stat($path) {
+ public function stat(string $path): array|false {
$meta = $this->getMetaData($path);
- if (!$meta) {
- return false;
- } else {
- return $meta;
- }
+ return $meta ?: false;
+
}
- /** {@inheritdoc} */
- public function getMimeType($path) {
+ public function getMimeType(string $path): string|false {
$meta = $this->getMetaData($path);
- if ($meta) {
- return $meta['mimetype'];
- } else {
- return false;
- }
+ return $meta ? $meta['mimetype'] : false;
}
- /**
- * @param string $path
- * @return string
- */
- public function cleanPath($path) {
+ public function cleanPath(string $path): string {
if ($path === '') {
return $path;
}
@@ -691,21 +634,17 @@ class DAV extends Common {
* @param string $path to encode
* @return string encoded path
*/
- protected function encodePath($path) {
+ protected function encodePath(string $path): string {
// slashes need to stay
return str_replace('%2F', '/', rawurlencode($path));
}
/**
- * @param string $method
- * @param string $path
- * @param string|resource|null $body
- * @param int $expected
* @return bool
* @throws StorageInvalidException
* @throws StorageNotAvailableException
*/
- protected function simpleResponse($method, $path, $body, $expected) {
+ protected function simpleResponse(string $method, string $path, ?string $body, int $expected): bool {
$path = $this->cleanPath($path);
try {
$response = $this->client->request($method, $this->encodePath($path), $body);
@@ -727,55 +666,37 @@ class DAV extends Common {
/**
* check if curl is installed
*/
- public static function checkDependencies() {
+ public static function checkDependencies(): bool {
return true;
}
- /** {@inheritdoc} */
- public function isUpdatable($path) {
+ public function isUpdatable(string $path): bool {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_UPDATE);
}
- /** {@inheritdoc} */
- public function isCreatable($path) {
+ public function isCreatable(string $path): bool {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_CREATE);
}
- /** {@inheritdoc} */
- public function isSharable($path) {
+ public function isSharable(string $path): bool {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_SHARE);
}
- /** {@inheritdoc} */
- public function isDeletable($path) {
+ public function isDeletable(string $path): bool {
return (bool)($this->getPermissions($path) & Constants::PERMISSION_DELETE);
}
- /** {@inheritdoc} */
- public function getPermissions($path) {
+ public function getPermissions(string $path): int {
$stat = $this->getMetaData($path);
- if ($stat) {
- return $stat['permissions'];
- } else {
- return 0;
- }
+ return $stat ? $stat['permissions'] : 0;
}
- /** {@inheritdoc} */
- public function getETag($path) {
+ public function getETag(string $path): string|false {
$meta = $this->getMetaData($path);
- if ($meta) {
- return $meta['etag'];
- } else {
- return null;
- }
+ return $meta ? $meta['etag'] : false;
}
- /**
- * @param string $permissionsString
- * @return int
- */
- protected function parsePermissions($permissionsString) {
+ protected function parsePermissions(string $permissionsString): int {
$permissions = Constants::PERMISSION_READ;
if (str_contains($permissionsString, 'R')) {
$permissions |= Constants::PERMISSION_SHARE;
@@ -793,15 +714,7 @@ class DAV extends Common {
return $permissions;
}
- /**
- * check if a file or folder has been updated since $time
- *
- * @param string $path
- * @param int $time
- * @throws \OCP\Files\StorageNotAvailableException
- * @return bool
- */
- public function hasUpdated($path, $time) {
+ public function hasUpdated(string $path, int $time): bool {
$this->init();
$path = $this->cleanPath($path);
try {
@@ -862,13 +775,13 @@ class DAV extends Common {
* @param string $path optional path from the operation
*
* @throws StorageInvalidException if the storage is invalid, for example
- * when the authentication expired or is invalid
+ * when the authentication expired or is invalid
* @throws StorageNotAvailableException if the storage is not available,
- * which might be temporary
+ * which might be temporary
* @throws ForbiddenException if the action is not allowed
*/
- protected function convertException(Exception $e, $path = '') {
- \OC::$server->get(LoggerInterface::class)->debug($e->getMessage(), ['app' => 'files_external', 'exception' => $e]);
+ protected function convertException(Exception $e, string $path = ''): void {
+ $this->logger->debug($e->getMessage(), ['app' => 'files_external', 'exception' => $e]);
if ($e instanceof ClientHttpException) {
if ($e->getHttpStatus() === Http::STATUS_LOCKED) {
throw new \OCP\Lock\LockedException($path);
@@ -899,7 +812,7 @@ class DAV extends Common {
// TODO: only log for now, but in the future need to wrap/rethrow exception
}
- public function getDirectoryContent($directory): \Traversable {
+ public function getDirectoryContent(string $directory): \Traversable {
$this->init();
$directory = $this->cleanPath($directory);
try {