2022-05-18 14:54:27 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2024-06-06 19:48:28 +02:00
|
|
|
* SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
* SPDX-FileCopyrightText: 2019 ownCloud GmbH
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
2022-05-18 14:54:27 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Files_Sharing;
|
|
|
|
|
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\Node;
|
|
|
|
use OCP\Files\NotFoundException;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles restricting for download of files
|
|
|
|
*/
|
|
|
|
class ViewOnly {
|
|
|
|
|
2024-10-18 12:04:22 +02:00
|
|
|
public function __construct(
|
|
|
|
private Folder $userFolder,
|
|
|
|
) {
|
2022-05-18 14:54:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string[] $pathsToCheck
|
|
|
|
* @return bool
|
|
|
|
*/
|
2022-06-13 12:48:35 +02:00
|
|
|
public function check(array $pathsToCheck): bool {
|
2022-05-18 14:54:27 +02:00
|
|
|
// If any of elements cannot be downloaded, prevent whole download
|
|
|
|
foreach ($pathsToCheck as $file) {
|
|
|
|
try {
|
|
|
|
$info = $this->userFolder->get($file);
|
|
|
|
if ($info instanceof File) {
|
|
|
|
// access to filecache is expensive in the loop
|
|
|
|
if (!$this->checkFileInfo($info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($info instanceof Folder) {
|
|
|
|
// get directory content is rather cheap query
|
|
|
|
if (!$this->dirRecursiveCheck($info)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (NotFoundException $e) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Folder $dirInfo
|
|
|
|
* @return bool
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2022-06-13 12:48:35 +02:00
|
|
|
private function dirRecursiveCheck(Folder $dirInfo): bool {
|
2022-05-18 14:54:27 +02:00
|
|
|
if (!$this->checkFileInfo($dirInfo)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If any of elements cannot be downloaded, prevent whole download
|
|
|
|
$files = $dirInfo->getDirectoryListing();
|
|
|
|
foreach ($files as $file) {
|
|
|
|
if ($file instanceof File) {
|
|
|
|
if (!$this->checkFileInfo($file)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} elseif ($file instanceof Folder) {
|
|
|
|
return $this->dirRecursiveCheck($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Node $fileInfo
|
|
|
|
* @return bool
|
|
|
|
* @throws NotFoundException
|
|
|
|
*/
|
2022-06-13 12:48:35 +02:00
|
|
|
private function checkFileInfo(Node $fileInfo): bool {
|
2022-05-18 14:54:27 +02:00
|
|
|
// Restrict view-only to nodes which are shared
|
|
|
|
$storage = $fileInfo->getStorage();
|
|
|
|
if (!$storage->instanceOfStorage(SharedStorage::class)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Extract extra permissions
|
2024-10-10 12:40:31 +02:00
|
|
|
/** @var SharedStorage $storage */
|
2022-05-18 14:54:27 +02:00
|
|
|
$share = $storage->getShare();
|
|
|
|
|
2024-09-09 00:21:50 +02:00
|
|
|
// Check whether download-permission was denied (granted if not set)
|
2022-06-13 12:48:35 +02:00
|
|
|
$attributes = $share->getAttributes();
|
2024-09-09 00:21:50 +02:00
|
|
|
$canDownload = $attributes?->getAttribute('permissions', 'download');
|
2022-05-18 14:54:27 +02:00
|
|
|
|
2024-09-09 00:21:50 +02:00
|
|
|
return $canDownload !== false;
|
2022-05-18 14:54:27 +02:00
|
|
|
}
|
|
|
|
}
|