@@ -38,678 +38,678 @@ |
||
| 38 | 38 | use Sabre\HTTP\ResponseInterface; |
| 39 | 39 | |
| 40 | 40 | class FilesPlugin extends ServerPlugin { |
| 41 | - // namespace |
|
| 42 | - public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 43 | - public const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
| 44 | - public const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
|
| 45 | - public const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid'; |
|
| 46 | - public const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions'; |
|
| 47 | - public const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions'; |
|
| 48 | - public const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions'; |
|
| 49 | - public const SHARE_ATTRIBUTES_PROPERTYNAME = '{http://nextcloud.org/ns}share-attributes'; |
|
| 50 | - public const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL'; |
|
| 51 | - public const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size'; |
|
| 52 | - public const GETETAG_PROPERTYNAME = '{DAV:}getetag'; |
|
| 53 | - public const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified'; |
|
| 54 | - public const CREATIONDATE_PROPERTYNAME = '{DAV:}creationdate'; |
|
| 55 | - public const DISPLAYNAME_PROPERTYNAME = '{DAV:}displayname'; |
|
| 56 | - public const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id'; |
|
| 57 | - public const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name'; |
|
| 58 | - public const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums'; |
|
| 59 | - public const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint'; |
|
| 60 | - public const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview'; |
|
| 61 | - public const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type'; |
|
| 62 | - public const MOUNT_ROOT_PROPERTYNAME = '{http://nextcloud.org/ns}is-mount-root'; |
|
| 63 | - public const IS_FEDERATED_PROPERTYNAME = '{http://nextcloud.org/ns}is-federated'; |
|
| 64 | - public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag'; |
|
| 65 | - public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time'; |
|
| 66 | - public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time'; |
|
| 67 | - public const SHARE_NOTE = '{http://nextcloud.org/ns}note'; |
|
| 68 | - public const SHARE_HIDE_DOWNLOAD_PROPERTYNAME = '{http://nextcloud.org/ns}hide-download'; |
|
| 69 | - public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count'; |
|
| 70 | - public const SUBFILE_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-file-count'; |
|
| 71 | - public const FILE_METADATA_PREFIX = '{http://nextcloud.org/ns}metadata-'; |
|
| 72 | - public const HIDDEN_PROPERTYNAME = '{http://nextcloud.org/ns}hidden'; |
|
| 73 | - |
|
| 74 | - /** Reference to main server object */ |
|
| 75 | - private ?Server $server = null; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * @param Tree $tree |
|
| 79 | - * @param IConfig $config |
|
| 80 | - * @param IRequest $request |
|
| 81 | - * @param IPreview $previewManager |
|
| 82 | - * @param IUserSession $userSession |
|
| 83 | - * @param bool $isPublic Whether this is public WebDAV. If true, some returned information will be stripped off. |
|
| 84 | - * @param bool $downloadAttachment |
|
| 85 | - * @return void |
|
| 86 | - */ |
|
| 87 | - public function __construct( |
|
| 88 | - private Tree $tree, |
|
| 89 | - private IConfig $config, |
|
| 90 | - private IRequest $request, |
|
| 91 | - private IPreview $previewManager, |
|
| 92 | - private IUserSession $userSession, |
|
| 93 | - private IFilenameValidator $validator, |
|
| 94 | - private bool $isPublic = false, |
|
| 95 | - private bool $downloadAttachment = true, |
|
| 96 | - ) { |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - /** |
|
| 100 | - * This initializes the plugin. |
|
| 101 | - * |
|
| 102 | - * This function is called by \Sabre\DAV\Server, after |
|
| 103 | - * addPlugin is called. |
|
| 104 | - * |
|
| 105 | - * This method should set up the required event subscriptions. |
|
| 106 | - * |
|
| 107 | - * @return void |
|
| 108 | - */ |
|
| 109 | - public function initialize(Server $server) { |
|
| 110 | - $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 111 | - $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc'; |
|
| 112 | - $server->protectedProperties[] = self::FILEID_PROPERTYNAME; |
|
| 113 | - $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME; |
|
| 114 | - $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME; |
|
| 115 | - $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME; |
|
| 116 | - $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME; |
|
| 117 | - $server->protectedProperties[] = self::SHARE_ATTRIBUTES_PROPERTYNAME; |
|
| 118 | - $server->protectedProperties[] = self::SIZE_PROPERTYNAME; |
|
| 119 | - $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME; |
|
| 120 | - $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME; |
|
| 121 | - $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME; |
|
| 122 | - $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME; |
|
| 123 | - $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME; |
|
| 124 | - $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME; |
|
| 125 | - $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME; |
|
| 126 | - $server->protectedProperties[] = self::IS_FEDERATED_PROPERTYNAME; |
|
| 127 | - $server->protectedProperties[] = self::SHARE_NOTE; |
|
| 128 | - |
|
| 129 | - // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH |
|
| 130 | - $allowedProperties = ['{DAV:}getetag']; |
|
| 131 | - $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties); |
|
| 132 | - |
|
| 133 | - $this->server = $server; |
|
| 134 | - $this->server->on('propFind', [$this, 'handleGetProperties']); |
|
| 135 | - $this->server->on('propPatch', [$this, 'handleUpdateProperties']); |
|
| 136 | - $this->server->on('afterBind', [$this, 'sendFileIdHeader']); |
|
| 137 | - $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']); |
|
| 138 | - $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
| 139 | - $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']); |
|
| 140 | - $this->server->on('afterResponse', function ($request, ResponseInterface $response): void { |
|
| 141 | - $body = $response->getBody(); |
|
| 142 | - if (is_resource($body)) { |
|
| 143 | - fclose($body); |
|
| 144 | - } |
|
| 145 | - }); |
|
| 146 | - $this->server->on('beforeMove', [$this, 'checkMove']); |
|
| 147 | - $this->server->on('beforeCopy', [$this, 'checkCopy']); |
|
| 148 | - } |
|
| 149 | - |
|
| 150 | - /** |
|
| 151 | - * Plugin that checks if a copy can actually be performed. |
|
| 152 | - * |
|
| 153 | - * @param string $source source path |
|
| 154 | - * @param string $target target path |
|
| 155 | - * @throws NotFound If the source does not exist |
|
| 156 | - * @throws InvalidPath If the target is invalid |
|
| 157 | - */ |
|
| 158 | - public function checkCopy($source, $target): void { |
|
| 159 | - $sourceNode = $this->tree->getNodeForPath($source); |
|
| 160 | - if (!$sourceNode instanceof Node) { |
|
| 161 | - return; |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - // Ensure source exists |
|
| 165 | - $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
| 166 | - if ($sourceNodeFileInfo === null) { |
|
| 167 | - throw new NotFound($source . ' does not exist'); |
|
| 168 | - } |
|
| 169 | - // Ensure the target name is valid |
|
| 170 | - try { |
|
| 171 | - [$targetPath, $targetName] = \Sabre\Uri\split($target); |
|
| 172 | - $this->validator->validateFilename($targetName); |
|
| 173 | - } catch (InvalidPathException $e) { |
|
| 174 | - throw new InvalidPath($e->getMessage(), false); |
|
| 175 | - } |
|
| 176 | - // Ensure the target path is valid |
|
| 177 | - $segments = array_slice(explode('/', $targetPath), 2); |
|
| 178 | - foreach ($segments as $segment) { |
|
| 179 | - if ($this->validator->isFilenameValid($segment) === false) { |
|
| 180 | - $l = \OCP\Server::get(IFactory::class)->get('dav'); |
|
| 181 | - throw new InvalidPath($l->t('Invalid target path')); |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - |
|
| 186 | - /** |
|
| 187 | - * Plugin that checks if a move can actually be performed. |
|
| 188 | - * |
|
| 189 | - * @param string $source source path |
|
| 190 | - * @param string $target target path |
|
| 191 | - * @throws Forbidden If the source is not deletable |
|
| 192 | - * @throws NotFound If the source does not exist |
|
| 193 | - * @throws InvalidPath If the target name is invalid |
|
| 194 | - */ |
|
| 195 | - public function checkMove(string $source, string $target): void { |
|
| 196 | - $sourceNode = $this->tree->getNodeForPath($source); |
|
| 197 | - if (!$sourceNode instanceof Node) { |
|
| 198 | - return; |
|
| 199 | - } |
|
| 200 | - |
|
| 201 | - // First check copyable (move only needs additional delete permission) |
|
| 202 | - $this->checkCopy($source, $target); |
|
| 203 | - |
|
| 204 | - // The source needs to be deletable for moving |
|
| 205 | - $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
| 206 | - if (!$sourceNodeFileInfo->isDeletable()) { |
|
| 207 | - throw new Forbidden($source . ' cannot be deleted'); |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - // The source is not allowed to be the parent of the target |
|
| 211 | - if (str_starts_with($source, $target . '/')) { |
|
| 212 | - throw new Forbidden($source . ' cannot be moved to it\'s parent'); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * This sets a cookie to be able to recognize the start of the download |
|
| 218 | - * the content must not be longer than 32 characters and must only contain |
|
| 219 | - * alphanumeric characters |
|
| 220 | - * |
|
| 221 | - * @param RequestInterface $request |
|
| 222 | - * @param ResponseInterface $response |
|
| 223 | - */ |
|
| 224 | - public function handleDownloadToken(RequestInterface $request, ResponseInterface $response) { |
|
| 225 | - $queryParams = $request->getQueryParameters(); |
|
| 226 | - |
|
| 227 | - /** |
|
| 228 | - * this sets a cookie to be able to recognize the start of the download |
|
| 229 | - * the content must not be longer than 32 characters and must only contain |
|
| 230 | - * alphanumeric characters |
|
| 231 | - */ |
|
| 232 | - if (isset($queryParams['downloadStartSecret'])) { |
|
| 233 | - $token = $queryParams['downloadStartSecret']; |
|
| 234 | - if (!isset($token[32]) |
|
| 235 | - && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) { |
|
| 236 | - // FIXME: use $response->setHeader() instead |
|
| 237 | - setcookie('ocDownloadStarted', $token, time() + 20, '/'); |
|
| 238 | - } |
|
| 239 | - } |
|
| 240 | - } |
|
| 241 | - |
|
| 242 | - /** |
|
| 243 | - * Add headers to file download |
|
| 244 | - * |
|
| 245 | - * @param RequestInterface $request |
|
| 246 | - * @param ResponseInterface $response |
|
| 247 | - */ |
|
| 248 | - public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
| 249 | - // Only handle valid files |
|
| 250 | - $node = $this->tree->getNodeForPath($request->getPath()); |
|
| 251 | - if (!($node instanceof IFile)) { |
|
| 252 | - return; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - // adds a 'Content-Disposition: attachment' header in case no disposition |
|
| 256 | - // header has been set before |
|
| 257 | - if ($this->downloadAttachment && |
|
| 258 | - $response->getHeader('Content-Disposition') === null) { |
|
| 259 | - $filename = $node->getName(); |
|
| 260 | - if ($this->request->isUserAgent( |
|
| 261 | - [ |
|
| 262 | - Request::USER_AGENT_IE, |
|
| 263 | - Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
| 264 | - Request::USER_AGENT_FREEBOX, |
|
| 265 | - ])) { |
|
| 266 | - $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
| 267 | - } else { |
|
| 268 | - $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
| 269 | - . '; filename="' . rawurlencode($filename) . '"'); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - if ($node instanceof File) { |
|
| 274 | - //Add OC-Checksum header |
|
| 275 | - $checksum = $node->getChecksum(); |
|
| 276 | - if ($checksum !== null && $checksum !== '') { |
|
| 277 | - $response->addHeader('OC-Checksum', $checksum); |
|
| 278 | - } |
|
| 279 | - } |
|
| 280 | - $response->addHeader('X-Accel-Buffering', 'no'); |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - /** |
|
| 284 | - * Adds all ownCloud-specific properties |
|
| 285 | - * |
|
| 286 | - * @param PropFind $propFind |
|
| 287 | - * @param \Sabre\DAV\INode $node |
|
| 288 | - * @return void |
|
| 289 | - */ |
|
| 290 | - public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
|
| 291 | - $httpRequest = $this->server->httpRequest; |
|
| 292 | - |
|
| 293 | - if ($node instanceof Node) { |
|
| 294 | - /** |
|
| 295 | - * This was disabled, because it made dir listing throw an exception, |
|
| 296 | - * so users were unable to navigate into folders where one subitem |
|
| 297 | - * is blocked by the files_accesscontrol app, see: |
|
| 298 | - * https://github.com/nextcloud/files_accesscontrol/issues/65 |
|
| 299 | - * if (!$node->getFileInfo()->isReadable()) { |
|
| 300 | - * // avoid detecting files through this means |
|
| 301 | - * throw new NotFound(); |
|
| 302 | - * } |
|
| 303 | - */ |
|
| 304 | - |
|
| 305 | - $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) { |
|
| 306 | - return $node->getFileId(); |
|
| 307 | - }); |
|
| 308 | - |
|
| 309 | - $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { |
|
| 310 | - return $node->getInternalFileId(); |
|
| 311 | - }); |
|
| 312 | - |
|
| 313 | - $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) { |
|
| 314 | - $perms = $node->getDavPermissions(); |
|
| 315 | - if ($this->isPublic) { |
|
| 316 | - // remove mount information |
|
| 317 | - $perms = str_replace(['S', 'M'], '', $perms); |
|
| 318 | - } |
|
| 319 | - return $perms; |
|
| 320 | - }); |
|
| 321 | - |
|
| 322 | - $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 323 | - $user = $this->userSession->getUser(); |
|
| 324 | - if ($user === null) { |
|
| 325 | - return null; |
|
| 326 | - } |
|
| 327 | - return $node->getSharePermissions( |
|
| 328 | - $user->getUID() |
|
| 329 | - ); |
|
| 330 | - }); |
|
| 331 | - |
|
| 332 | - $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest): ?string { |
|
| 333 | - $user = $this->userSession->getUser(); |
|
| 334 | - if ($user === null) { |
|
| 335 | - return null; |
|
| 336 | - } |
|
| 337 | - $ncPermissions = $node->getSharePermissions( |
|
| 338 | - $user->getUID() |
|
| 339 | - ); |
|
| 340 | - $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions); |
|
| 341 | - return json_encode($ocmPermissions, JSON_THROW_ON_ERROR); |
|
| 342 | - }); |
|
| 343 | - |
|
| 344 | - $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 345 | - return json_encode($node->getShareAttributes(), JSON_THROW_ON_ERROR); |
|
| 346 | - }); |
|
| 347 | - |
|
| 348 | - $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node): string { |
|
| 349 | - return $node->getETag(); |
|
| 350 | - }); |
|
| 351 | - |
|
| 352 | - $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node): ?string { |
|
| 353 | - $owner = $node->getOwner(); |
|
| 354 | - if (!$owner) { |
|
| 355 | - return null; |
|
| 356 | - } else { |
|
| 357 | - return $owner->getUID(); |
|
| 358 | - } |
|
| 359 | - }); |
|
| 360 | - $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node): ?string { |
|
| 361 | - $owner = $node->getOwner(); |
|
| 362 | - if (!$owner) { |
|
| 363 | - return null; |
|
| 364 | - } else { |
|
| 365 | - return $owner->getDisplayName(); |
|
| 366 | - } |
|
| 367 | - }); |
|
| 368 | - |
|
| 369 | - $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
| 370 | - return json_encode($this->previewManager->isAvailable($node->getFileInfo()), JSON_THROW_ON_ERROR); |
|
| 371 | - }); |
|
| 372 | - $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node): int|float { |
|
| 373 | - return $node->getSize(); |
|
| 374 | - }); |
|
| 375 | - $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
| 376 | - return $node->getFileInfo()->getMountPoint()->getMountType(); |
|
| 377 | - }); |
|
| 378 | - |
|
| 379 | - /** |
|
| 380 | - * This is a special property which is used to determine if a node |
|
| 381 | - * is a mount root or not, e.g. a shared folder. |
|
| 382 | - * If so, then the node can only be unshared and not deleted. |
|
| 383 | - * @see https://github.com/nextcloud/server/blob/cc75294eb6b16b916a342e69998935f89222619d/lib/private/Files/View.php#L696-L698 |
|
| 384 | - */ |
|
| 385 | - $propFind->handle(self::MOUNT_ROOT_PROPERTYNAME, function () use ($node) { |
|
| 386 | - return $node->getNode()->getInternalPath() === '' ? 'true' : 'false'; |
|
| 387 | - }); |
|
| 388 | - |
|
| 389 | - $propFind->handle(self::SHARE_NOTE, function () use ($node): ?string { |
|
| 390 | - $user = $this->userSession->getUser(); |
|
| 391 | - return $node->getNoteFromShare( |
|
| 392 | - $user?->getUID() |
|
| 393 | - ); |
|
| 394 | - }); |
|
| 395 | - |
|
| 396 | - $propFind->handle(self::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, function () use ($node) { |
|
| 397 | - $storage = $node->getNode()->getStorage(); |
|
| 398 | - if ($storage->instanceOfStorage(ISharedStorage::class)) { |
|
| 399 | - /** @var ISharedStorage $storage */ |
|
| 400 | - return match($storage->getShare()->getHideDownload()) { |
|
| 401 | - true => 'true', |
|
| 402 | - false => 'false', |
|
| 403 | - }; |
|
| 404 | - } else { |
|
| 405 | - return null; |
|
| 406 | - } |
|
| 407 | - }); |
|
| 408 | - |
|
| 409 | - $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () { |
|
| 410 | - return $this->config->getSystemValue('data-fingerprint', ''); |
|
| 411 | - }); |
|
| 412 | - $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) { |
|
| 413 | - return (new \DateTimeImmutable()) |
|
| 414 | - ->setTimestamp($node->getFileInfo()->getCreationTime()) |
|
| 415 | - ->format(\DateTimeInterface::ATOM); |
|
| 416 | - }); |
|
| 417 | - $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) { |
|
| 418 | - return $node->getFileInfo()->getCreationTime(); |
|
| 419 | - }); |
|
| 420 | - |
|
| 421 | - foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) { |
|
| 422 | - $propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, $metadataValue); |
|
| 423 | - } |
|
| 424 | - |
|
| 425 | - $propFind->handle(self::HIDDEN_PROPERTYNAME, function () use ($node) { |
|
| 426 | - $isLivePhoto = isset($node->getFileInfo()->getMetadata()['files-live-photo']); |
|
| 427 | - $isMovFile = $node->getFileInfo()->getMimetype() === 'video/quicktime'; |
|
| 428 | - return ($isLivePhoto && $isMovFile) ? 'true' : 'false'; |
|
| 429 | - }); |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * Return file/folder name as displayname. The primary reason to |
|
| 433 | - * implement it this way is to avoid costly fallback to |
|
| 434 | - * CustomPropertiesBackend (esp. visible when querying all files |
|
| 435 | - * in a folder). |
|
| 436 | - */ |
|
| 437 | - $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) { |
|
| 438 | - return $node->getName(); |
|
| 439 | - }); |
|
| 440 | - |
|
| 441 | - $propFind->handle(self::IS_FEDERATED_PROPERTYNAME, function () use ($node) { |
|
| 442 | - return $node->getFileInfo()->getMountPoint() |
|
| 443 | - instanceof SharingExternalMount; |
|
| 444 | - }); |
|
| 445 | - } |
|
| 446 | - |
|
| 447 | - if ($node instanceof File) { |
|
| 448 | - $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) { |
|
| 449 | - try { |
|
| 450 | - $directDownloadUrl = $node->getDirectDownload(); |
|
| 451 | - if (isset($directDownloadUrl['url'])) { |
|
| 452 | - return $directDownloadUrl['url']; |
|
| 453 | - } |
|
| 454 | - } catch (StorageNotAvailableException $e) { |
|
| 455 | - return false; |
|
| 456 | - } catch (ForbiddenException $e) { |
|
| 457 | - return false; |
|
| 458 | - } |
|
| 459 | - return false; |
|
| 460 | - }); |
|
| 461 | - |
|
| 462 | - $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) { |
|
| 463 | - $checksum = $node->getChecksum(); |
|
| 464 | - if ($checksum === null || $checksum === '') { |
|
| 465 | - return null; |
|
| 466 | - } |
|
| 467 | - |
|
| 468 | - return new ChecksumList($checksum); |
|
| 469 | - }); |
|
| 470 | - |
|
| 471 | - $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) { |
|
| 472 | - return $node->getFileInfo()->getUploadTime(); |
|
| 473 | - }); |
|
| 474 | - } |
|
| 475 | - |
|
| 476 | - if ($node instanceof Directory) { |
|
| 477 | - $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) { |
|
| 478 | - return $node->getSize(); |
|
| 479 | - }); |
|
| 480 | - |
|
| 481 | - $requestProperties = $propFind->getRequestedProperties(); |
|
| 482 | - |
|
| 483 | - if (in_array(self::SUBFILE_COUNT_PROPERTYNAME, $requestProperties, true) |
|
| 484 | - || in_array(self::SUBFOLDER_COUNT_PROPERTYNAME, $requestProperties, true)) { |
|
| 485 | - $nbFiles = 0; |
|
| 486 | - $nbFolders = 0; |
|
| 487 | - foreach ($node->getChildren() as $child) { |
|
| 488 | - if ($child instanceof File) { |
|
| 489 | - $nbFiles++; |
|
| 490 | - } elseif ($child instanceof Directory) { |
|
| 491 | - $nbFolders++; |
|
| 492 | - } |
|
| 493 | - } |
|
| 494 | - |
|
| 495 | - $propFind->handle(self::SUBFILE_COUNT_PROPERTYNAME, $nbFiles); |
|
| 496 | - $propFind->handle(self::SUBFOLDER_COUNT_PROPERTYNAME, $nbFolders); |
|
| 497 | - } |
|
| 498 | - } |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - /** |
|
| 502 | - * translate Nextcloud permissions to OCM Permissions |
|
| 503 | - * |
|
| 504 | - * @param $ncPermissions |
|
| 505 | - * @return array |
|
| 506 | - */ |
|
| 507 | - protected function ncPermissions2ocmPermissions($ncPermissions) { |
|
| 508 | - $ocmPermissions = []; |
|
| 509 | - |
|
| 510 | - if ($ncPermissions & Constants::PERMISSION_SHARE) { |
|
| 511 | - $ocmPermissions[] = 'share'; |
|
| 512 | - } |
|
| 513 | - |
|
| 514 | - if ($ncPermissions & Constants::PERMISSION_READ) { |
|
| 515 | - $ocmPermissions[] = 'read'; |
|
| 516 | - } |
|
| 517 | - |
|
| 518 | - if (($ncPermissions & Constants::PERMISSION_CREATE) || |
|
| 519 | - ($ncPermissions & Constants::PERMISSION_UPDATE)) { |
|
| 520 | - $ocmPermissions[] = 'write'; |
|
| 521 | - } |
|
| 522 | - |
|
| 523 | - return $ocmPermissions; |
|
| 524 | - } |
|
| 525 | - |
|
| 526 | - /** |
|
| 527 | - * Update ownCloud-specific properties |
|
| 528 | - * |
|
| 529 | - * @param string $path |
|
| 530 | - * @param PropPatch $propPatch |
|
| 531 | - * |
|
| 532 | - * @return void |
|
| 533 | - */ |
|
| 534 | - public function handleUpdateProperties($path, PropPatch $propPatch) { |
|
| 535 | - $node = $this->tree->getNodeForPath($path); |
|
| 536 | - if (!($node instanceof Node)) { |
|
| 537 | - return; |
|
| 538 | - } |
|
| 539 | - |
|
| 540 | - $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) { |
|
| 541 | - if (empty($time)) { |
|
| 542 | - return false; |
|
| 543 | - } |
|
| 544 | - $node->touch($time); |
|
| 545 | - return true; |
|
| 546 | - }); |
|
| 547 | - $propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) { |
|
| 548 | - if (empty($etag)) { |
|
| 549 | - return false; |
|
| 550 | - } |
|
| 551 | - return $node->setEtag($etag) !== -1; |
|
| 552 | - }); |
|
| 553 | - $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function ($time) use ($node) { |
|
| 554 | - if (empty($time)) { |
|
| 555 | - return false; |
|
| 556 | - } |
|
| 557 | - $dateTime = new \DateTimeImmutable($time); |
|
| 558 | - $node->setCreationTime($dateTime->getTimestamp()); |
|
| 559 | - return true; |
|
| 560 | - }); |
|
| 561 | - $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function ($time) use ($node) { |
|
| 562 | - if (empty($time)) { |
|
| 563 | - return false; |
|
| 564 | - } |
|
| 565 | - $node->setCreationTime((int)$time); |
|
| 566 | - return true; |
|
| 567 | - }); |
|
| 568 | - |
|
| 569 | - $this->handleUpdatePropertiesMetadata($propPatch, $node); |
|
| 570 | - |
|
| 571 | - /** |
|
| 572 | - * Disable modification of the displayname property for files and |
|
| 573 | - * folders via PROPPATCH. See PROPFIND for more information. |
|
| 574 | - */ |
|
| 575 | - $propPatch->handle(self::DISPLAYNAME_PROPERTYNAME, function ($displayName) { |
|
| 576 | - return 403; |
|
| 577 | - }); |
|
| 578 | - } |
|
| 579 | - |
|
| 580 | - |
|
| 581 | - /** |
|
| 582 | - * handle the update of metadata from PROPPATCH requests |
|
| 583 | - * |
|
| 584 | - * @param PropPatch $propPatch |
|
| 585 | - * @param Node $node |
|
| 586 | - * |
|
| 587 | - * @throws FilesMetadataException |
|
| 588 | - */ |
|
| 589 | - private function handleUpdatePropertiesMetadata(PropPatch $propPatch, Node $node): void { |
|
| 590 | - $userId = $this->userSession->getUser()?->getUID(); |
|
| 591 | - if ($userId === null) { |
|
| 592 | - return; |
|
| 593 | - } |
|
| 594 | - |
|
| 595 | - $accessRight = $this->getMetadataFileAccessRight($node, $userId); |
|
| 596 | - $filesMetadataManager = $this->initFilesMetadataManager(); |
|
| 597 | - $knownMetadata = $filesMetadataManager->getKnownMetadata(); |
|
| 598 | - |
|
| 599 | - foreach ($propPatch->getRemainingMutations() as $mutation) { |
|
| 600 | - if (!str_starts_with($mutation, self::FILE_METADATA_PREFIX)) { |
|
| 601 | - continue; |
|
| 602 | - } |
|
| 603 | - |
|
| 604 | - $propPatch->handle( |
|
| 605 | - $mutation, |
|
| 606 | - function (mixed $value) use ($accessRight, $knownMetadata, $node, $mutation, $filesMetadataManager): bool { |
|
| 607 | - /** @var FilesMetadata $metadata */ |
|
| 608 | - $metadata = $filesMetadataManager->getMetadata((int)$node->getFileId(), true); |
|
| 609 | - $metadata->setStorageId($node->getNode()->getStorage()->getCache()->getNumericStorageId()); |
|
| 610 | - $metadataKey = substr($mutation, strlen(self::FILE_METADATA_PREFIX)); |
|
| 611 | - |
|
| 612 | - // confirm metadata key is editable via PROPPATCH |
|
| 613 | - if ($knownMetadata->getEditPermission($metadataKey) < $accessRight) { |
|
| 614 | - throw new FilesMetadataException('you do not have enough rights to update \'' . $metadataKey . '\' on this node'); |
|
| 615 | - } |
|
| 616 | - |
|
| 617 | - if ($value === null) { |
|
| 618 | - $metadata->unset($metadataKey); |
|
| 619 | - $filesMetadataManager->saveMetadata($metadata); |
|
| 620 | - return true; |
|
| 621 | - } |
|
| 622 | - |
|
| 623 | - // If the metadata is unknown, it defaults to string. |
|
| 624 | - try { |
|
| 625 | - $type = $knownMetadata->getType($metadataKey); |
|
| 626 | - } catch (FilesMetadataNotFoundException) { |
|
| 627 | - $type = IMetadataValueWrapper::TYPE_STRING; |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - switch ($type) { |
|
| 631 | - case IMetadataValueWrapper::TYPE_STRING: |
|
| 632 | - $metadata->setString($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 633 | - break; |
|
| 634 | - case IMetadataValueWrapper::TYPE_INT: |
|
| 635 | - $metadata->setInt($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 636 | - break; |
|
| 637 | - case IMetadataValueWrapper::TYPE_FLOAT: |
|
| 638 | - $metadata->setFloat($metadataKey, $value); |
|
| 639 | - break; |
|
| 640 | - case IMetadataValueWrapper::TYPE_BOOL: |
|
| 641 | - $metadata->setBool($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 642 | - break; |
|
| 643 | - case IMetadataValueWrapper::TYPE_ARRAY: |
|
| 644 | - $metadata->setArray($metadataKey, $value); |
|
| 645 | - break; |
|
| 646 | - case IMetadataValueWrapper::TYPE_STRING_LIST: |
|
| 647 | - $metadata->setStringList($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 648 | - break; |
|
| 649 | - case IMetadataValueWrapper::TYPE_INT_LIST: |
|
| 650 | - $metadata->setIntList($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 651 | - break; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - $filesMetadataManager->saveMetadata($metadata); |
|
| 655 | - |
|
| 656 | - return true; |
|
| 657 | - } |
|
| 658 | - ); |
|
| 659 | - } |
|
| 660 | - } |
|
| 661 | - |
|
| 662 | - /** |
|
| 663 | - * init default internal metadata |
|
| 664 | - * |
|
| 665 | - * @return IFilesMetadataManager |
|
| 666 | - */ |
|
| 667 | - private function initFilesMetadataManager(): IFilesMetadataManager { |
|
| 668 | - /** @var IFilesMetadataManager $manager */ |
|
| 669 | - $manager = \OCP\Server::get(IFilesMetadataManager::class); |
|
| 670 | - $manager->initMetadata('files-live-photo', IMetadataValueWrapper::TYPE_STRING, false, IMetadataValueWrapper::EDIT_REQ_OWNERSHIP); |
|
| 671 | - |
|
| 672 | - return $manager; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * based on owner and shares, returns the bottom limit to update related metadata |
|
| 677 | - * |
|
| 678 | - * @param Node $node |
|
| 679 | - * @param string $userId |
|
| 680 | - * |
|
| 681 | - * @return int |
|
| 682 | - */ |
|
| 683 | - private function getMetadataFileAccessRight(Node $node, string $userId): int { |
|
| 684 | - if ($node->getOwner()?->getUID() === $userId) { |
|
| 685 | - return IMetadataValueWrapper::EDIT_REQ_OWNERSHIP; |
|
| 686 | - } else { |
|
| 687 | - $filePermissions = $node->getSharePermissions($userId); |
|
| 688 | - if ($filePermissions & Constants::PERMISSION_UPDATE) { |
|
| 689 | - return IMetadataValueWrapper::EDIT_REQ_WRITE_PERMISSION; |
|
| 690 | - } |
|
| 691 | - } |
|
| 692 | - |
|
| 693 | - return IMetadataValueWrapper::EDIT_REQ_READ_PERMISSION; |
|
| 694 | - } |
|
| 695 | - |
|
| 696 | - /** |
|
| 697 | - * @param string $filePath |
|
| 698 | - * @param ?\Sabre\DAV\INode $node |
|
| 699 | - * @return void |
|
| 700 | - * @throws \Sabre\DAV\Exception\BadRequest |
|
| 701 | - */ |
|
| 702 | - public function sendFileIdHeader($filePath, ?\Sabre\DAV\INode $node = null) { |
|
| 703 | - // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder |
|
| 704 | - if (!$this->server->tree->nodeExists($filePath)) { |
|
| 705 | - return; |
|
| 706 | - } |
|
| 707 | - $node = $this->server->tree->getNodeForPath($filePath); |
|
| 708 | - if ($node instanceof Node) { |
|
| 709 | - $fileId = $node->getFileId(); |
|
| 710 | - if (!is_null($fileId)) { |
|
| 711 | - $this->server->httpResponse->setHeader('OC-FileId', $fileId); |
|
| 712 | - } |
|
| 713 | - } |
|
| 714 | - } |
|
| 41 | + // namespace |
|
| 42 | + public const NS_OWNCLOUD = 'http://owncloud.org/ns'; |
|
| 43 | + public const NS_NEXTCLOUD = 'http://nextcloud.org/ns'; |
|
| 44 | + public const FILEID_PROPERTYNAME = '{http://owncloud.org/ns}id'; |
|
| 45 | + public const INTERNAL_FILEID_PROPERTYNAME = '{http://owncloud.org/ns}fileid'; |
|
| 46 | + public const PERMISSIONS_PROPERTYNAME = '{http://owncloud.org/ns}permissions'; |
|
| 47 | + public const SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-collaboration-services.org/ns}share-permissions'; |
|
| 48 | + public const OCM_SHARE_PERMISSIONS_PROPERTYNAME = '{http://open-cloud-mesh.org/ns}share-permissions'; |
|
| 49 | + public const SHARE_ATTRIBUTES_PROPERTYNAME = '{http://nextcloud.org/ns}share-attributes'; |
|
| 50 | + public const DOWNLOADURL_PROPERTYNAME = '{http://owncloud.org/ns}downloadURL'; |
|
| 51 | + public const SIZE_PROPERTYNAME = '{http://owncloud.org/ns}size'; |
|
| 52 | + public const GETETAG_PROPERTYNAME = '{DAV:}getetag'; |
|
| 53 | + public const LASTMODIFIED_PROPERTYNAME = '{DAV:}lastmodified'; |
|
| 54 | + public const CREATIONDATE_PROPERTYNAME = '{DAV:}creationdate'; |
|
| 55 | + public const DISPLAYNAME_PROPERTYNAME = '{DAV:}displayname'; |
|
| 56 | + public const OWNER_ID_PROPERTYNAME = '{http://owncloud.org/ns}owner-id'; |
|
| 57 | + public const OWNER_DISPLAY_NAME_PROPERTYNAME = '{http://owncloud.org/ns}owner-display-name'; |
|
| 58 | + public const CHECKSUMS_PROPERTYNAME = '{http://owncloud.org/ns}checksums'; |
|
| 59 | + public const DATA_FINGERPRINT_PROPERTYNAME = '{http://owncloud.org/ns}data-fingerprint'; |
|
| 60 | + public const HAS_PREVIEW_PROPERTYNAME = '{http://nextcloud.org/ns}has-preview'; |
|
| 61 | + public const MOUNT_TYPE_PROPERTYNAME = '{http://nextcloud.org/ns}mount-type'; |
|
| 62 | + public const MOUNT_ROOT_PROPERTYNAME = '{http://nextcloud.org/ns}is-mount-root'; |
|
| 63 | + public const IS_FEDERATED_PROPERTYNAME = '{http://nextcloud.org/ns}is-federated'; |
|
| 64 | + public const METADATA_ETAG_PROPERTYNAME = '{http://nextcloud.org/ns}metadata_etag'; |
|
| 65 | + public const UPLOAD_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}upload_time'; |
|
| 66 | + public const CREATION_TIME_PROPERTYNAME = '{http://nextcloud.org/ns}creation_time'; |
|
| 67 | + public const SHARE_NOTE = '{http://nextcloud.org/ns}note'; |
|
| 68 | + public const SHARE_HIDE_DOWNLOAD_PROPERTYNAME = '{http://nextcloud.org/ns}hide-download'; |
|
| 69 | + public const SUBFOLDER_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-folder-count'; |
|
| 70 | + public const SUBFILE_COUNT_PROPERTYNAME = '{http://nextcloud.org/ns}contained-file-count'; |
|
| 71 | + public const FILE_METADATA_PREFIX = '{http://nextcloud.org/ns}metadata-'; |
|
| 72 | + public const HIDDEN_PROPERTYNAME = '{http://nextcloud.org/ns}hidden'; |
|
| 73 | + |
|
| 74 | + /** Reference to main server object */ |
|
| 75 | + private ?Server $server = null; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * @param Tree $tree |
|
| 79 | + * @param IConfig $config |
|
| 80 | + * @param IRequest $request |
|
| 81 | + * @param IPreview $previewManager |
|
| 82 | + * @param IUserSession $userSession |
|
| 83 | + * @param bool $isPublic Whether this is public WebDAV. If true, some returned information will be stripped off. |
|
| 84 | + * @param bool $downloadAttachment |
|
| 85 | + * @return void |
|
| 86 | + */ |
|
| 87 | + public function __construct( |
|
| 88 | + private Tree $tree, |
|
| 89 | + private IConfig $config, |
|
| 90 | + private IRequest $request, |
|
| 91 | + private IPreview $previewManager, |
|
| 92 | + private IUserSession $userSession, |
|
| 93 | + private IFilenameValidator $validator, |
|
| 94 | + private bool $isPublic = false, |
|
| 95 | + private bool $downloadAttachment = true, |
|
| 96 | + ) { |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + /** |
|
| 100 | + * This initializes the plugin. |
|
| 101 | + * |
|
| 102 | + * This function is called by \Sabre\DAV\Server, after |
|
| 103 | + * addPlugin is called. |
|
| 104 | + * |
|
| 105 | + * This method should set up the required event subscriptions. |
|
| 106 | + * |
|
| 107 | + * @return void |
|
| 108 | + */ |
|
| 109 | + public function initialize(Server $server) { |
|
| 110 | + $server->xml->namespaceMap[self::NS_OWNCLOUD] = 'oc'; |
|
| 111 | + $server->xml->namespaceMap[self::NS_NEXTCLOUD] = 'nc'; |
|
| 112 | + $server->protectedProperties[] = self::FILEID_PROPERTYNAME; |
|
| 113 | + $server->protectedProperties[] = self::INTERNAL_FILEID_PROPERTYNAME; |
|
| 114 | + $server->protectedProperties[] = self::PERMISSIONS_PROPERTYNAME; |
|
| 115 | + $server->protectedProperties[] = self::SHARE_PERMISSIONS_PROPERTYNAME; |
|
| 116 | + $server->protectedProperties[] = self::OCM_SHARE_PERMISSIONS_PROPERTYNAME; |
|
| 117 | + $server->protectedProperties[] = self::SHARE_ATTRIBUTES_PROPERTYNAME; |
|
| 118 | + $server->protectedProperties[] = self::SIZE_PROPERTYNAME; |
|
| 119 | + $server->protectedProperties[] = self::DOWNLOADURL_PROPERTYNAME; |
|
| 120 | + $server->protectedProperties[] = self::OWNER_ID_PROPERTYNAME; |
|
| 121 | + $server->protectedProperties[] = self::OWNER_DISPLAY_NAME_PROPERTYNAME; |
|
| 122 | + $server->protectedProperties[] = self::CHECKSUMS_PROPERTYNAME; |
|
| 123 | + $server->protectedProperties[] = self::DATA_FINGERPRINT_PROPERTYNAME; |
|
| 124 | + $server->protectedProperties[] = self::HAS_PREVIEW_PROPERTYNAME; |
|
| 125 | + $server->protectedProperties[] = self::MOUNT_TYPE_PROPERTYNAME; |
|
| 126 | + $server->protectedProperties[] = self::IS_FEDERATED_PROPERTYNAME; |
|
| 127 | + $server->protectedProperties[] = self::SHARE_NOTE; |
|
| 128 | + |
|
| 129 | + // normally these cannot be changed (RFC4918), but we want them modifiable through PROPPATCH |
|
| 130 | + $allowedProperties = ['{DAV:}getetag']; |
|
| 131 | + $server->protectedProperties = array_diff($server->protectedProperties, $allowedProperties); |
|
| 132 | + |
|
| 133 | + $this->server = $server; |
|
| 134 | + $this->server->on('propFind', [$this, 'handleGetProperties']); |
|
| 135 | + $this->server->on('propPatch', [$this, 'handleUpdateProperties']); |
|
| 136 | + $this->server->on('afterBind', [$this, 'sendFileIdHeader']); |
|
| 137 | + $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']); |
|
| 138 | + $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
| 139 | + $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']); |
|
| 140 | + $this->server->on('afterResponse', function ($request, ResponseInterface $response): void { |
|
| 141 | + $body = $response->getBody(); |
|
| 142 | + if (is_resource($body)) { |
|
| 143 | + fclose($body); |
|
| 144 | + } |
|
| 145 | + }); |
|
| 146 | + $this->server->on('beforeMove', [$this, 'checkMove']); |
|
| 147 | + $this->server->on('beforeCopy', [$this, 'checkCopy']); |
|
| 148 | + } |
|
| 149 | + |
|
| 150 | + /** |
|
| 151 | + * Plugin that checks if a copy can actually be performed. |
|
| 152 | + * |
|
| 153 | + * @param string $source source path |
|
| 154 | + * @param string $target target path |
|
| 155 | + * @throws NotFound If the source does not exist |
|
| 156 | + * @throws InvalidPath If the target is invalid |
|
| 157 | + */ |
|
| 158 | + public function checkCopy($source, $target): void { |
|
| 159 | + $sourceNode = $this->tree->getNodeForPath($source); |
|
| 160 | + if (!$sourceNode instanceof Node) { |
|
| 161 | + return; |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + // Ensure source exists |
|
| 165 | + $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
| 166 | + if ($sourceNodeFileInfo === null) { |
|
| 167 | + throw new NotFound($source . ' does not exist'); |
|
| 168 | + } |
|
| 169 | + // Ensure the target name is valid |
|
| 170 | + try { |
|
| 171 | + [$targetPath, $targetName] = \Sabre\Uri\split($target); |
|
| 172 | + $this->validator->validateFilename($targetName); |
|
| 173 | + } catch (InvalidPathException $e) { |
|
| 174 | + throw new InvalidPath($e->getMessage(), false); |
|
| 175 | + } |
|
| 176 | + // Ensure the target path is valid |
|
| 177 | + $segments = array_slice(explode('/', $targetPath), 2); |
|
| 178 | + foreach ($segments as $segment) { |
|
| 179 | + if ($this->validator->isFilenameValid($segment) === false) { |
|
| 180 | + $l = \OCP\Server::get(IFactory::class)->get('dav'); |
|
| 181 | + throw new InvalidPath($l->t('Invalid target path')); |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + |
|
| 186 | + /** |
|
| 187 | + * Plugin that checks if a move can actually be performed. |
|
| 188 | + * |
|
| 189 | + * @param string $source source path |
|
| 190 | + * @param string $target target path |
|
| 191 | + * @throws Forbidden If the source is not deletable |
|
| 192 | + * @throws NotFound If the source does not exist |
|
| 193 | + * @throws InvalidPath If the target name is invalid |
|
| 194 | + */ |
|
| 195 | + public function checkMove(string $source, string $target): void { |
|
| 196 | + $sourceNode = $this->tree->getNodeForPath($source); |
|
| 197 | + if (!$sourceNode instanceof Node) { |
|
| 198 | + return; |
|
| 199 | + } |
|
| 200 | + |
|
| 201 | + // First check copyable (move only needs additional delete permission) |
|
| 202 | + $this->checkCopy($source, $target); |
|
| 203 | + |
|
| 204 | + // The source needs to be deletable for moving |
|
| 205 | + $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
|
| 206 | + if (!$sourceNodeFileInfo->isDeletable()) { |
|
| 207 | + throw new Forbidden($source . ' cannot be deleted'); |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + // The source is not allowed to be the parent of the target |
|
| 211 | + if (str_starts_with($source, $target . '/')) { |
|
| 212 | + throw new Forbidden($source . ' cannot be moved to it\'s parent'); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * This sets a cookie to be able to recognize the start of the download |
|
| 218 | + * the content must not be longer than 32 characters and must only contain |
|
| 219 | + * alphanumeric characters |
|
| 220 | + * |
|
| 221 | + * @param RequestInterface $request |
|
| 222 | + * @param ResponseInterface $response |
|
| 223 | + */ |
|
| 224 | + public function handleDownloadToken(RequestInterface $request, ResponseInterface $response) { |
|
| 225 | + $queryParams = $request->getQueryParameters(); |
|
| 226 | + |
|
| 227 | + /** |
|
| 228 | + * this sets a cookie to be able to recognize the start of the download |
|
| 229 | + * the content must not be longer than 32 characters and must only contain |
|
| 230 | + * alphanumeric characters |
|
| 231 | + */ |
|
| 232 | + if (isset($queryParams['downloadStartSecret'])) { |
|
| 233 | + $token = $queryParams['downloadStartSecret']; |
|
| 234 | + if (!isset($token[32]) |
|
| 235 | + && preg_match('!^[a-zA-Z0-9]+$!', $token) === 1) { |
|
| 236 | + // FIXME: use $response->setHeader() instead |
|
| 237 | + setcookie('ocDownloadStarted', $token, time() + 20, '/'); |
|
| 238 | + } |
|
| 239 | + } |
|
| 240 | + } |
|
| 241 | + |
|
| 242 | + /** |
|
| 243 | + * Add headers to file download |
|
| 244 | + * |
|
| 245 | + * @param RequestInterface $request |
|
| 246 | + * @param ResponseInterface $response |
|
| 247 | + */ |
|
| 248 | + public function httpGet(RequestInterface $request, ResponseInterface $response) { |
|
| 249 | + // Only handle valid files |
|
| 250 | + $node = $this->tree->getNodeForPath($request->getPath()); |
|
| 251 | + if (!($node instanceof IFile)) { |
|
| 252 | + return; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + // adds a 'Content-Disposition: attachment' header in case no disposition |
|
| 256 | + // header has been set before |
|
| 257 | + if ($this->downloadAttachment && |
|
| 258 | + $response->getHeader('Content-Disposition') === null) { |
|
| 259 | + $filename = $node->getName(); |
|
| 260 | + if ($this->request->isUserAgent( |
|
| 261 | + [ |
|
| 262 | + Request::USER_AGENT_IE, |
|
| 263 | + Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
|
| 264 | + Request::USER_AGENT_FREEBOX, |
|
| 265 | + ])) { |
|
| 266 | + $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
| 267 | + } else { |
|
| 268 | + $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
| 269 | + . '; filename="' . rawurlencode($filename) . '"'); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + if ($node instanceof File) { |
|
| 274 | + //Add OC-Checksum header |
|
| 275 | + $checksum = $node->getChecksum(); |
|
| 276 | + if ($checksum !== null && $checksum !== '') { |
|
| 277 | + $response->addHeader('OC-Checksum', $checksum); |
|
| 278 | + } |
|
| 279 | + } |
|
| 280 | + $response->addHeader('X-Accel-Buffering', 'no'); |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + /** |
|
| 284 | + * Adds all ownCloud-specific properties |
|
| 285 | + * |
|
| 286 | + * @param PropFind $propFind |
|
| 287 | + * @param \Sabre\DAV\INode $node |
|
| 288 | + * @return void |
|
| 289 | + */ |
|
| 290 | + public function handleGetProperties(PropFind $propFind, \Sabre\DAV\INode $node) { |
|
| 291 | + $httpRequest = $this->server->httpRequest; |
|
| 292 | + |
|
| 293 | + if ($node instanceof Node) { |
|
| 294 | + /** |
|
| 295 | + * This was disabled, because it made dir listing throw an exception, |
|
| 296 | + * so users were unable to navigate into folders where one subitem |
|
| 297 | + * is blocked by the files_accesscontrol app, see: |
|
| 298 | + * https://github.com/nextcloud/files_accesscontrol/issues/65 |
|
| 299 | + * if (!$node->getFileInfo()->isReadable()) { |
|
| 300 | + * // avoid detecting files through this means |
|
| 301 | + * throw new NotFound(); |
|
| 302 | + * } |
|
| 303 | + */ |
|
| 304 | + |
|
| 305 | + $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) { |
|
| 306 | + return $node->getFileId(); |
|
| 307 | + }); |
|
| 308 | + |
|
| 309 | + $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { |
|
| 310 | + return $node->getInternalFileId(); |
|
| 311 | + }); |
|
| 312 | + |
|
| 313 | + $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) { |
|
| 314 | + $perms = $node->getDavPermissions(); |
|
| 315 | + if ($this->isPublic) { |
|
| 316 | + // remove mount information |
|
| 317 | + $perms = str_replace(['S', 'M'], '', $perms); |
|
| 318 | + } |
|
| 319 | + return $perms; |
|
| 320 | + }); |
|
| 321 | + |
|
| 322 | + $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 323 | + $user = $this->userSession->getUser(); |
|
| 324 | + if ($user === null) { |
|
| 325 | + return null; |
|
| 326 | + } |
|
| 327 | + return $node->getSharePermissions( |
|
| 328 | + $user->getUID() |
|
| 329 | + ); |
|
| 330 | + }); |
|
| 331 | + |
|
| 332 | + $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest): ?string { |
|
| 333 | + $user = $this->userSession->getUser(); |
|
| 334 | + if ($user === null) { |
|
| 335 | + return null; |
|
| 336 | + } |
|
| 337 | + $ncPermissions = $node->getSharePermissions( |
|
| 338 | + $user->getUID() |
|
| 339 | + ); |
|
| 340 | + $ocmPermissions = $this->ncPermissions2ocmPermissions($ncPermissions); |
|
| 341 | + return json_encode($ocmPermissions, JSON_THROW_ON_ERROR); |
|
| 342 | + }); |
|
| 343 | + |
|
| 344 | + $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 345 | + return json_encode($node->getShareAttributes(), JSON_THROW_ON_ERROR); |
|
| 346 | + }); |
|
| 347 | + |
|
| 348 | + $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node): string { |
|
| 349 | + return $node->getETag(); |
|
| 350 | + }); |
|
| 351 | + |
|
| 352 | + $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node): ?string { |
|
| 353 | + $owner = $node->getOwner(); |
|
| 354 | + if (!$owner) { |
|
| 355 | + return null; |
|
| 356 | + } else { |
|
| 357 | + return $owner->getUID(); |
|
| 358 | + } |
|
| 359 | + }); |
|
| 360 | + $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node): ?string { |
|
| 361 | + $owner = $node->getOwner(); |
|
| 362 | + if (!$owner) { |
|
| 363 | + return null; |
|
| 364 | + } else { |
|
| 365 | + return $owner->getDisplayName(); |
|
| 366 | + } |
|
| 367 | + }); |
|
| 368 | + |
|
| 369 | + $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
| 370 | + return json_encode($this->previewManager->isAvailable($node->getFileInfo()), JSON_THROW_ON_ERROR); |
|
| 371 | + }); |
|
| 372 | + $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node): int|float { |
|
| 373 | + return $node->getSize(); |
|
| 374 | + }); |
|
| 375 | + $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
| 376 | + return $node->getFileInfo()->getMountPoint()->getMountType(); |
|
| 377 | + }); |
|
| 378 | + |
|
| 379 | + /** |
|
| 380 | + * This is a special property which is used to determine if a node |
|
| 381 | + * is a mount root or not, e.g. a shared folder. |
|
| 382 | + * If so, then the node can only be unshared and not deleted. |
|
| 383 | + * @see https://github.com/nextcloud/server/blob/cc75294eb6b16b916a342e69998935f89222619d/lib/private/Files/View.php#L696-L698 |
|
| 384 | + */ |
|
| 385 | + $propFind->handle(self::MOUNT_ROOT_PROPERTYNAME, function () use ($node) { |
|
| 386 | + return $node->getNode()->getInternalPath() === '' ? 'true' : 'false'; |
|
| 387 | + }); |
|
| 388 | + |
|
| 389 | + $propFind->handle(self::SHARE_NOTE, function () use ($node): ?string { |
|
| 390 | + $user = $this->userSession->getUser(); |
|
| 391 | + return $node->getNoteFromShare( |
|
| 392 | + $user?->getUID() |
|
| 393 | + ); |
|
| 394 | + }); |
|
| 395 | + |
|
| 396 | + $propFind->handle(self::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, function () use ($node) { |
|
| 397 | + $storage = $node->getNode()->getStorage(); |
|
| 398 | + if ($storage->instanceOfStorage(ISharedStorage::class)) { |
|
| 399 | + /** @var ISharedStorage $storage */ |
|
| 400 | + return match($storage->getShare()->getHideDownload()) { |
|
| 401 | + true => 'true', |
|
| 402 | + false => 'false', |
|
| 403 | + }; |
|
| 404 | + } else { |
|
| 405 | + return null; |
|
| 406 | + } |
|
| 407 | + }); |
|
| 408 | + |
|
| 409 | + $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () { |
|
| 410 | + return $this->config->getSystemValue('data-fingerprint', ''); |
|
| 411 | + }); |
|
| 412 | + $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) { |
|
| 413 | + return (new \DateTimeImmutable()) |
|
| 414 | + ->setTimestamp($node->getFileInfo()->getCreationTime()) |
|
| 415 | + ->format(\DateTimeInterface::ATOM); |
|
| 416 | + }); |
|
| 417 | + $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) { |
|
| 418 | + return $node->getFileInfo()->getCreationTime(); |
|
| 419 | + }); |
|
| 420 | + |
|
| 421 | + foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) { |
|
| 422 | + $propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, $metadataValue); |
|
| 423 | + } |
|
| 424 | + |
|
| 425 | + $propFind->handle(self::HIDDEN_PROPERTYNAME, function () use ($node) { |
|
| 426 | + $isLivePhoto = isset($node->getFileInfo()->getMetadata()['files-live-photo']); |
|
| 427 | + $isMovFile = $node->getFileInfo()->getMimetype() === 'video/quicktime'; |
|
| 428 | + return ($isLivePhoto && $isMovFile) ? 'true' : 'false'; |
|
| 429 | + }); |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * Return file/folder name as displayname. The primary reason to |
|
| 433 | + * implement it this way is to avoid costly fallback to |
|
| 434 | + * CustomPropertiesBackend (esp. visible when querying all files |
|
| 435 | + * in a folder). |
|
| 436 | + */ |
|
| 437 | + $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) { |
|
| 438 | + return $node->getName(); |
|
| 439 | + }); |
|
| 440 | + |
|
| 441 | + $propFind->handle(self::IS_FEDERATED_PROPERTYNAME, function () use ($node) { |
|
| 442 | + return $node->getFileInfo()->getMountPoint() |
|
| 443 | + instanceof SharingExternalMount; |
|
| 444 | + }); |
|
| 445 | + } |
|
| 446 | + |
|
| 447 | + if ($node instanceof File) { |
|
| 448 | + $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) { |
|
| 449 | + try { |
|
| 450 | + $directDownloadUrl = $node->getDirectDownload(); |
|
| 451 | + if (isset($directDownloadUrl['url'])) { |
|
| 452 | + return $directDownloadUrl['url']; |
|
| 453 | + } |
|
| 454 | + } catch (StorageNotAvailableException $e) { |
|
| 455 | + return false; |
|
| 456 | + } catch (ForbiddenException $e) { |
|
| 457 | + return false; |
|
| 458 | + } |
|
| 459 | + return false; |
|
| 460 | + }); |
|
| 461 | + |
|
| 462 | + $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) { |
|
| 463 | + $checksum = $node->getChecksum(); |
|
| 464 | + if ($checksum === null || $checksum === '') { |
|
| 465 | + return null; |
|
| 466 | + } |
|
| 467 | + |
|
| 468 | + return new ChecksumList($checksum); |
|
| 469 | + }); |
|
| 470 | + |
|
| 471 | + $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) { |
|
| 472 | + return $node->getFileInfo()->getUploadTime(); |
|
| 473 | + }); |
|
| 474 | + } |
|
| 475 | + |
|
| 476 | + if ($node instanceof Directory) { |
|
| 477 | + $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) { |
|
| 478 | + return $node->getSize(); |
|
| 479 | + }); |
|
| 480 | + |
|
| 481 | + $requestProperties = $propFind->getRequestedProperties(); |
|
| 482 | + |
|
| 483 | + if (in_array(self::SUBFILE_COUNT_PROPERTYNAME, $requestProperties, true) |
|
| 484 | + || in_array(self::SUBFOLDER_COUNT_PROPERTYNAME, $requestProperties, true)) { |
|
| 485 | + $nbFiles = 0; |
|
| 486 | + $nbFolders = 0; |
|
| 487 | + foreach ($node->getChildren() as $child) { |
|
| 488 | + if ($child instanceof File) { |
|
| 489 | + $nbFiles++; |
|
| 490 | + } elseif ($child instanceof Directory) { |
|
| 491 | + $nbFolders++; |
|
| 492 | + } |
|
| 493 | + } |
|
| 494 | + |
|
| 495 | + $propFind->handle(self::SUBFILE_COUNT_PROPERTYNAME, $nbFiles); |
|
| 496 | + $propFind->handle(self::SUBFOLDER_COUNT_PROPERTYNAME, $nbFolders); |
|
| 497 | + } |
|
| 498 | + } |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + /** |
|
| 502 | + * translate Nextcloud permissions to OCM Permissions |
|
| 503 | + * |
|
| 504 | + * @param $ncPermissions |
|
| 505 | + * @return array |
|
| 506 | + */ |
|
| 507 | + protected function ncPermissions2ocmPermissions($ncPermissions) { |
|
| 508 | + $ocmPermissions = []; |
|
| 509 | + |
|
| 510 | + if ($ncPermissions & Constants::PERMISSION_SHARE) { |
|
| 511 | + $ocmPermissions[] = 'share'; |
|
| 512 | + } |
|
| 513 | + |
|
| 514 | + if ($ncPermissions & Constants::PERMISSION_READ) { |
|
| 515 | + $ocmPermissions[] = 'read'; |
|
| 516 | + } |
|
| 517 | + |
|
| 518 | + if (($ncPermissions & Constants::PERMISSION_CREATE) || |
|
| 519 | + ($ncPermissions & Constants::PERMISSION_UPDATE)) { |
|
| 520 | + $ocmPermissions[] = 'write'; |
|
| 521 | + } |
|
| 522 | + |
|
| 523 | + return $ocmPermissions; |
|
| 524 | + } |
|
| 525 | + |
|
| 526 | + /** |
|
| 527 | + * Update ownCloud-specific properties |
|
| 528 | + * |
|
| 529 | + * @param string $path |
|
| 530 | + * @param PropPatch $propPatch |
|
| 531 | + * |
|
| 532 | + * @return void |
|
| 533 | + */ |
|
| 534 | + public function handleUpdateProperties($path, PropPatch $propPatch) { |
|
| 535 | + $node = $this->tree->getNodeForPath($path); |
|
| 536 | + if (!($node instanceof Node)) { |
|
| 537 | + return; |
|
| 538 | + } |
|
| 539 | + |
|
| 540 | + $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) { |
|
| 541 | + if (empty($time)) { |
|
| 542 | + return false; |
|
| 543 | + } |
|
| 544 | + $node->touch($time); |
|
| 545 | + return true; |
|
| 546 | + }); |
|
| 547 | + $propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) { |
|
| 548 | + if (empty($etag)) { |
|
| 549 | + return false; |
|
| 550 | + } |
|
| 551 | + return $node->setEtag($etag) !== -1; |
|
| 552 | + }); |
|
| 553 | + $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function ($time) use ($node) { |
|
| 554 | + if (empty($time)) { |
|
| 555 | + return false; |
|
| 556 | + } |
|
| 557 | + $dateTime = new \DateTimeImmutable($time); |
|
| 558 | + $node->setCreationTime($dateTime->getTimestamp()); |
|
| 559 | + return true; |
|
| 560 | + }); |
|
| 561 | + $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function ($time) use ($node) { |
|
| 562 | + if (empty($time)) { |
|
| 563 | + return false; |
|
| 564 | + } |
|
| 565 | + $node->setCreationTime((int)$time); |
|
| 566 | + return true; |
|
| 567 | + }); |
|
| 568 | + |
|
| 569 | + $this->handleUpdatePropertiesMetadata($propPatch, $node); |
|
| 570 | + |
|
| 571 | + /** |
|
| 572 | + * Disable modification of the displayname property for files and |
|
| 573 | + * folders via PROPPATCH. See PROPFIND for more information. |
|
| 574 | + */ |
|
| 575 | + $propPatch->handle(self::DISPLAYNAME_PROPERTYNAME, function ($displayName) { |
|
| 576 | + return 403; |
|
| 577 | + }); |
|
| 578 | + } |
|
| 579 | + |
|
| 580 | + |
|
| 581 | + /** |
|
| 582 | + * handle the update of metadata from PROPPATCH requests |
|
| 583 | + * |
|
| 584 | + * @param PropPatch $propPatch |
|
| 585 | + * @param Node $node |
|
| 586 | + * |
|
| 587 | + * @throws FilesMetadataException |
|
| 588 | + */ |
|
| 589 | + private function handleUpdatePropertiesMetadata(PropPatch $propPatch, Node $node): void { |
|
| 590 | + $userId = $this->userSession->getUser()?->getUID(); |
|
| 591 | + if ($userId === null) { |
|
| 592 | + return; |
|
| 593 | + } |
|
| 594 | + |
|
| 595 | + $accessRight = $this->getMetadataFileAccessRight($node, $userId); |
|
| 596 | + $filesMetadataManager = $this->initFilesMetadataManager(); |
|
| 597 | + $knownMetadata = $filesMetadataManager->getKnownMetadata(); |
|
| 598 | + |
|
| 599 | + foreach ($propPatch->getRemainingMutations() as $mutation) { |
|
| 600 | + if (!str_starts_with($mutation, self::FILE_METADATA_PREFIX)) { |
|
| 601 | + continue; |
|
| 602 | + } |
|
| 603 | + |
|
| 604 | + $propPatch->handle( |
|
| 605 | + $mutation, |
|
| 606 | + function (mixed $value) use ($accessRight, $knownMetadata, $node, $mutation, $filesMetadataManager): bool { |
|
| 607 | + /** @var FilesMetadata $metadata */ |
|
| 608 | + $metadata = $filesMetadataManager->getMetadata((int)$node->getFileId(), true); |
|
| 609 | + $metadata->setStorageId($node->getNode()->getStorage()->getCache()->getNumericStorageId()); |
|
| 610 | + $metadataKey = substr($mutation, strlen(self::FILE_METADATA_PREFIX)); |
|
| 611 | + |
|
| 612 | + // confirm metadata key is editable via PROPPATCH |
|
| 613 | + if ($knownMetadata->getEditPermission($metadataKey) < $accessRight) { |
|
| 614 | + throw new FilesMetadataException('you do not have enough rights to update \'' . $metadataKey . '\' on this node'); |
|
| 615 | + } |
|
| 616 | + |
|
| 617 | + if ($value === null) { |
|
| 618 | + $metadata->unset($metadataKey); |
|
| 619 | + $filesMetadataManager->saveMetadata($metadata); |
|
| 620 | + return true; |
|
| 621 | + } |
|
| 622 | + |
|
| 623 | + // If the metadata is unknown, it defaults to string. |
|
| 624 | + try { |
|
| 625 | + $type = $knownMetadata->getType($metadataKey); |
|
| 626 | + } catch (FilesMetadataNotFoundException) { |
|
| 627 | + $type = IMetadataValueWrapper::TYPE_STRING; |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + switch ($type) { |
|
| 631 | + case IMetadataValueWrapper::TYPE_STRING: |
|
| 632 | + $metadata->setString($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 633 | + break; |
|
| 634 | + case IMetadataValueWrapper::TYPE_INT: |
|
| 635 | + $metadata->setInt($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 636 | + break; |
|
| 637 | + case IMetadataValueWrapper::TYPE_FLOAT: |
|
| 638 | + $metadata->setFloat($metadataKey, $value); |
|
| 639 | + break; |
|
| 640 | + case IMetadataValueWrapper::TYPE_BOOL: |
|
| 641 | + $metadata->setBool($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 642 | + break; |
|
| 643 | + case IMetadataValueWrapper::TYPE_ARRAY: |
|
| 644 | + $metadata->setArray($metadataKey, $value); |
|
| 645 | + break; |
|
| 646 | + case IMetadataValueWrapper::TYPE_STRING_LIST: |
|
| 647 | + $metadata->setStringList($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 648 | + break; |
|
| 649 | + case IMetadataValueWrapper::TYPE_INT_LIST: |
|
| 650 | + $metadata->setIntList($metadataKey, $value, $knownMetadata->isIndex($metadataKey)); |
|
| 651 | + break; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + $filesMetadataManager->saveMetadata($metadata); |
|
| 655 | + |
|
| 656 | + return true; |
|
| 657 | + } |
|
| 658 | + ); |
|
| 659 | + } |
|
| 660 | + } |
|
| 661 | + |
|
| 662 | + /** |
|
| 663 | + * init default internal metadata |
|
| 664 | + * |
|
| 665 | + * @return IFilesMetadataManager |
|
| 666 | + */ |
|
| 667 | + private function initFilesMetadataManager(): IFilesMetadataManager { |
|
| 668 | + /** @var IFilesMetadataManager $manager */ |
|
| 669 | + $manager = \OCP\Server::get(IFilesMetadataManager::class); |
|
| 670 | + $manager->initMetadata('files-live-photo', IMetadataValueWrapper::TYPE_STRING, false, IMetadataValueWrapper::EDIT_REQ_OWNERSHIP); |
|
| 671 | + |
|
| 672 | + return $manager; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * based on owner and shares, returns the bottom limit to update related metadata |
|
| 677 | + * |
|
| 678 | + * @param Node $node |
|
| 679 | + * @param string $userId |
|
| 680 | + * |
|
| 681 | + * @return int |
|
| 682 | + */ |
|
| 683 | + private function getMetadataFileAccessRight(Node $node, string $userId): int { |
|
| 684 | + if ($node->getOwner()?->getUID() === $userId) { |
|
| 685 | + return IMetadataValueWrapper::EDIT_REQ_OWNERSHIP; |
|
| 686 | + } else { |
|
| 687 | + $filePermissions = $node->getSharePermissions($userId); |
|
| 688 | + if ($filePermissions & Constants::PERMISSION_UPDATE) { |
|
| 689 | + return IMetadataValueWrapper::EDIT_REQ_WRITE_PERMISSION; |
|
| 690 | + } |
|
| 691 | + } |
|
| 692 | + |
|
| 693 | + return IMetadataValueWrapper::EDIT_REQ_READ_PERMISSION; |
|
| 694 | + } |
|
| 695 | + |
|
| 696 | + /** |
|
| 697 | + * @param string $filePath |
|
| 698 | + * @param ?\Sabre\DAV\INode $node |
|
| 699 | + * @return void |
|
| 700 | + * @throws \Sabre\DAV\Exception\BadRequest |
|
| 701 | + */ |
|
| 702 | + public function sendFileIdHeader($filePath, ?\Sabre\DAV\INode $node = null) { |
|
| 703 | + // we get the node for the given $filePath here because in case of afterCreateFile $node is the parent folder |
|
| 704 | + if (!$this->server->tree->nodeExists($filePath)) { |
|
| 705 | + return; |
|
| 706 | + } |
|
| 707 | + $node = $this->server->tree->getNodeForPath($filePath); |
|
| 708 | + if ($node instanceof Node) { |
|
| 709 | + $fileId = $node->getFileId(); |
|
| 710 | + if (!is_null($fileId)) { |
|
| 711 | + $this->server->httpResponse->setHeader('OC-FileId', $fileId); |
|
| 712 | + } |
|
| 713 | + } |
|
| 714 | + } |
|
| 715 | 715 | } |
@@ -135,9 +135,9 @@ discard block |
||
| 135 | 135 | $this->server->on('propPatch', [$this, 'handleUpdateProperties']); |
| 136 | 136 | $this->server->on('afterBind', [$this, 'sendFileIdHeader']); |
| 137 | 137 | $this->server->on('afterWriteContent', [$this, 'sendFileIdHeader']); |
| 138 | - $this->server->on('afterMethod:GET', [$this,'httpGet']); |
|
| 138 | + $this->server->on('afterMethod:GET', [$this, 'httpGet']); |
|
| 139 | 139 | $this->server->on('afterMethod:GET', [$this, 'handleDownloadToken']); |
| 140 | - $this->server->on('afterResponse', function ($request, ResponseInterface $response): void { |
|
| 140 | + $this->server->on('afterResponse', function($request, ResponseInterface $response): void { |
|
| 141 | 141 | $body = $response->getBody(); |
| 142 | 142 | if (is_resource($body)) { |
| 143 | 143 | fclose($body); |
@@ -164,7 +164,7 @@ discard block |
||
| 164 | 164 | // Ensure source exists |
| 165 | 165 | $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
| 166 | 166 | if ($sourceNodeFileInfo === null) { |
| 167 | - throw new NotFound($source . ' does not exist'); |
|
| 167 | + throw new NotFound($source.' does not exist'); |
|
| 168 | 168 | } |
| 169 | 169 | // Ensure the target name is valid |
| 170 | 170 | try { |
@@ -204,12 +204,12 @@ discard block |
||
| 204 | 204 | // The source needs to be deletable for moving |
| 205 | 205 | $sourceNodeFileInfo = $sourceNode->getFileInfo(); |
| 206 | 206 | if (!$sourceNodeFileInfo->isDeletable()) { |
| 207 | - throw new Forbidden($source . ' cannot be deleted'); |
|
| 207 | + throw new Forbidden($source.' cannot be deleted'); |
|
| 208 | 208 | } |
| 209 | 209 | |
| 210 | 210 | // The source is not allowed to be the parent of the target |
| 211 | - if (str_starts_with($source, $target . '/')) { |
|
| 212 | - throw new Forbidden($source . ' cannot be moved to it\'s parent'); |
|
| 211 | + if (str_starts_with($source, $target.'/')) { |
|
| 212 | + throw new Forbidden($source.' cannot be moved to it\'s parent'); |
|
| 213 | 213 | } |
| 214 | 214 | } |
| 215 | 215 | |
@@ -263,10 +263,10 @@ discard block |
||
| 263 | 263 | Request::USER_AGENT_ANDROID_MOBILE_CHROME, |
| 264 | 264 | Request::USER_AGENT_FREEBOX, |
| 265 | 265 | ])) { |
| 266 | - $response->addHeader('Content-Disposition', 'attachment; filename="' . rawurlencode($filename) . '"'); |
|
| 266 | + $response->addHeader('Content-Disposition', 'attachment; filename="'.rawurlencode($filename).'"'); |
|
| 267 | 267 | } else { |
| 268 | - $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\'' . rawurlencode($filename) |
|
| 269 | - . '; filename="' . rawurlencode($filename) . '"'); |
|
| 268 | + $response->addHeader('Content-Disposition', 'attachment; filename*=UTF-8\'\''.rawurlencode($filename) |
|
| 269 | + . '; filename="'.rawurlencode($filename).'"'); |
|
| 270 | 270 | } |
| 271 | 271 | } |
| 272 | 272 | |
@@ -302,15 +302,15 @@ discard block |
||
| 302 | 302 | * } |
| 303 | 303 | */ |
| 304 | 304 | |
| 305 | - $propFind->handle(self::FILEID_PROPERTYNAME, function () use ($node) { |
|
| 305 | + $propFind->handle(self::FILEID_PROPERTYNAME, function() use ($node) { |
|
| 306 | 306 | return $node->getFileId(); |
| 307 | 307 | }); |
| 308 | 308 | |
| 309 | - $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function () use ($node) { |
|
| 309 | + $propFind->handle(self::INTERNAL_FILEID_PROPERTYNAME, function() use ($node) { |
|
| 310 | 310 | return $node->getInternalFileId(); |
| 311 | 311 | }); |
| 312 | 312 | |
| 313 | - $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function () use ($node) { |
|
| 313 | + $propFind->handle(self::PERMISSIONS_PROPERTYNAME, function() use ($node) { |
|
| 314 | 314 | $perms = $node->getDavPermissions(); |
| 315 | 315 | if ($this->isPublic) { |
| 316 | 316 | // remove mount information |
@@ -319,7 +319,7 @@ discard block |
||
| 319 | 319 | return $perms; |
| 320 | 320 | }); |
| 321 | 321 | |
| 322 | - $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 322 | + $propFind->handle(self::SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest) { |
|
| 323 | 323 | $user = $this->userSession->getUser(); |
| 324 | 324 | if ($user === null) { |
| 325 | 325 | return null; |
@@ -329,7 +329,7 @@ discard block |
||
| 329 | 329 | ); |
| 330 | 330 | }); |
| 331 | 331 | |
| 332 | - $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function () use ($node, $httpRequest): ?string { |
|
| 332 | + $propFind->handle(self::OCM_SHARE_PERMISSIONS_PROPERTYNAME, function() use ($node, $httpRequest): ?string { |
|
| 333 | 333 | $user = $this->userSession->getUser(); |
| 334 | 334 | if ($user === null) { |
| 335 | 335 | return null; |
@@ -341,15 +341,15 @@ discard block |
||
| 341 | 341 | return json_encode($ocmPermissions, JSON_THROW_ON_ERROR); |
| 342 | 342 | }); |
| 343 | 343 | |
| 344 | - $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function () use ($node, $httpRequest) { |
|
| 344 | + $propFind->handle(self::SHARE_ATTRIBUTES_PROPERTYNAME, function() use ($node, $httpRequest) { |
|
| 345 | 345 | return json_encode($node->getShareAttributes(), JSON_THROW_ON_ERROR); |
| 346 | 346 | }); |
| 347 | 347 | |
| 348 | - $propFind->handle(self::GETETAG_PROPERTYNAME, function () use ($node): string { |
|
| 348 | + $propFind->handle(self::GETETAG_PROPERTYNAME, function() use ($node): string { |
|
| 349 | 349 | return $node->getETag(); |
| 350 | 350 | }); |
| 351 | 351 | |
| 352 | - $propFind->handle(self::OWNER_ID_PROPERTYNAME, function () use ($node): ?string { |
|
| 352 | + $propFind->handle(self::OWNER_ID_PROPERTYNAME, function() use ($node): ?string { |
|
| 353 | 353 | $owner = $node->getOwner(); |
| 354 | 354 | if (!$owner) { |
| 355 | 355 | return null; |
@@ -357,7 +357,7 @@ discard block |
||
| 357 | 357 | return $owner->getUID(); |
| 358 | 358 | } |
| 359 | 359 | }); |
| 360 | - $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function () use ($node): ?string { |
|
| 360 | + $propFind->handle(self::OWNER_DISPLAY_NAME_PROPERTYNAME, function() use ($node): ?string { |
|
| 361 | 361 | $owner = $node->getOwner(); |
| 362 | 362 | if (!$owner) { |
| 363 | 363 | return null; |
@@ -366,13 +366,13 @@ discard block |
||
| 366 | 366 | } |
| 367 | 367 | }); |
| 368 | 368 | |
| 369 | - $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function () use ($node) { |
|
| 369 | + $propFind->handle(self::HAS_PREVIEW_PROPERTYNAME, function() use ($node) { |
|
| 370 | 370 | return json_encode($this->previewManager->isAvailable($node->getFileInfo()), JSON_THROW_ON_ERROR); |
| 371 | 371 | }); |
| 372 | - $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node): int|float { |
|
| 372 | + $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node): int | float { |
|
| 373 | 373 | return $node->getSize(); |
| 374 | 374 | }); |
| 375 | - $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function () use ($node) { |
|
| 375 | + $propFind->handle(self::MOUNT_TYPE_PROPERTYNAME, function() use ($node) { |
|
| 376 | 376 | return $node->getFileInfo()->getMountPoint()->getMountType(); |
| 377 | 377 | }); |
| 378 | 378 | |
@@ -382,18 +382,18 @@ discard block |
||
| 382 | 382 | * If so, then the node can only be unshared and not deleted. |
| 383 | 383 | * @see https://github.com/nextcloud/server/blob/cc75294eb6b16b916a342e69998935f89222619d/lib/private/Files/View.php#L696-L698 |
| 384 | 384 | */ |
| 385 | - $propFind->handle(self::MOUNT_ROOT_PROPERTYNAME, function () use ($node) { |
|
| 385 | + $propFind->handle(self::MOUNT_ROOT_PROPERTYNAME, function() use ($node) { |
|
| 386 | 386 | return $node->getNode()->getInternalPath() === '' ? 'true' : 'false'; |
| 387 | 387 | }); |
| 388 | 388 | |
| 389 | - $propFind->handle(self::SHARE_NOTE, function () use ($node): ?string { |
|
| 389 | + $propFind->handle(self::SHARE_NOTE, function() use ($node): ?string { |
|
| 390 | 390 | $user = $this->userSession->getUser(); |
| 391 | 391 | return $node->getNoteFromShare( |
| 392 | 392 | $user?->getUID() |
| 393 | 393 | ); |
| 394 | 394 | }); |
| 395 | 395 | |
| 396 | - $propFind->handle(self::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, function () use ($node) { |
|
| 396 | + $propFind->handle(self::SHARE_HIDE_DOWNLOAD_PROPERTYNAME, function() use ($node) { |
|
| 397 | 397 | $storage = $node->getNode()->getStorage(); |
| 398 | 398 | if ($storage->instanceOfStorage(ISharedStorage::class)) { |
| 399 | 399 | /** @var ISharedStorage $storage */ |
@@ -406,23 +406,23 @@ discard block |
||
| 406 | 406 | } |
| 407 | 407 | }); |
| 408 | 408 | |
| 409 | - $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function () { |
|
| 409 | + $propFind->handle(self::DATA_FINGERPRINT_PROPERTYNAME, function() { |
|
| 410 | 410 | return $this->config->getSystemValue('data-fingerprint', ''); |
| 411 | 411 | }); |
| 412 | - $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function () use ($node) { |
|
| 412 | + $propFind->handle(self::CREATIONDATE_PROPERTYNAME, function() use ($node) { |
|
| 413 | 413 | return (new \DateTimeImmutable()) |
| 414 | 414 | ->setTimestamp($node->getFileInfo()->getCreationTime()) |
| 415 | 415 | ->format(\DateTimeInterface::ATOM); |
| 416 | 416 | }); |
| 417 | - $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function () use ($node) { |
|
| 417 | + $propFind->handle(self::CREATION_TIME_PROPERTYNAME, function() use ($node) { |
|
| 418 | 418 | return $node->getFileInfo()->getCreationTime(); |
| 419 | 419 | }); |
| 420 | 420 | |
| 421 | 421 | foreach ($node->getFileInfo()->getMetadata() as $metadataKey => $metadataValue) { |
| 422 | - $propFind->handle(self::FILE_METADATA_PREFIX . $metadataKey, $metadataValue); |
|
| 422 | + $propFind->handle(self::FILE_METADATA_PREFIX.$metadataKey, $metadataValue); |
|
| 423 | 423 | } |
| 424 | 424 | |
| 425 | - $propFind->handle(self::HIDDEN_PROPERTYNAME, function () use ($node) { |
|
| 425 | + $propFind->handle(self::HIDDEN_PROPERTYNAME, function() use ($node) { |
|
| 426 | 426 | $isLivePhoto = isset($node->getFileInfo()->getMetadata()['files-live-photo']); |
| 427 | 427 | $isMovFile = $node->getFileInfo()->getMimetype() === 'video/quicktime'; |
| 428 | 428 | return ($isLivePhoto && $isMovFile) ? 'true' : 'false'; |
@@ -434,18 +434,18 @@ discard block |
||
| 434 | 434 | * CustomPropertiesBackend (esp. visible when querying all files |
| 435 | 435 | * in a folder). |
| 436 | 436 | */ |
| 437 | - $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function () use ($node) { |
|
| 437 | + $propFind->handle(self::DISPLAYNAME_PROPERTYNAME, function() use ($node) { |
|
| 438 | 438 | return $node->getName(); |
| 439 | 439 | }); |
| 440 | 440 | |
| 441 | - $propFind->handle(self::IS_FEDERATED_PROPERTYNAME, function () use ($node) { |
|
| 441 | + $propFind->handle(self::IS_FEDERATED_PROPERTYNAME, function() use ($node) { |
|
| 442 | 442 | return $node->getFileInfo()->getMountPoint() |
| 443 | 443 | instanceof SharingExternalMount; |
| 444 | 444 | }); |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | if ($node instanceof File) { |
| 448 | - $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function () use ($node) { |
|
| 448 | + $propFind->handle(self::DOWNLOADURL_PROPERTYNAME, function() use ($node) { |
|
| 449 | 449 | try { |
| 450 | 450 | $directDownloadUrl = $node->getDirectDownload(); |
| 451 | 451 | if (isset($directDownloadUrl['url'])) { |
@@ -459,7 +459,7 @@ discard block |
||
| 459 | 459 | return false; |
| 460 | 460 | }); |
| 461 | 461 | |
| 462 | - $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function () use ($node) { |
|
| 462 | + $propFind->handle(self::CHECKSUMS_PROPERTYNAME, function() use ($node) { |
|
| 463 | 463 | $checksum = $node->getChecksum(); |
| 464 | 464 | if ($checksum === null || $checksum === '') { |
| 465 | 465 | return null; |
@@ -468,13 +468,13 @@ discard block |
||
| 468 | 468 | return new ChecksumList($checksum); |
| 469 | 469 | }); |
| 470 | 470 | |
| 471 | - $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function () use ($node) { |
|
| 471 | + $propFind->handle(self::UPLOAD_TIME_PROPERTYNAME, function() use ($node) { |
|
| 472 | 472 | return $node->getFileInfo()->getUploadTime(); |
| 473 | 473 | }); |
| 474 | 474 | } |
| 475 | 475 | |
| 476 | 476 | if ($node instanceof Directory) { |
| 477 | - $propFind->handle(self::SIZE_PROPERTYNAME, function () use ($node) { |
|
| 477 | + $propFind->handle(self::SIZE_PROPERTYNAME, function() use ($node) { |
|
| 478 | 478 | return $node->getSize(); |
| 479 | 479 | }); |
| 480 | 480 | |
@@ -537,20 +537,20 @@ discard block |
||
| 537 | 537 | return; |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function ($time) use ($node) { |
|
| 540 | + $propPatch->handle(self::LASTMODIFIED_PROPERTYNAME, function($time) use ($node) { |
|
| 541 | 541 | if (empty($time)) { |
| 542 | 542 | return false; |
| 543 | 543 | } |
| 544 | 544 | $node->touch($time); |
| 545 | 545 | return true; |
| 546 | 546 | }); |
| 547 | - $propPatch->handle(self::GETETAG_PROPERTYNAME, function ($etag) use ($node) { |
|
| 547 | + $propPatch->handle(self::GETETAG_PROPERTYNAME, function($etag) use ($node) { |
|
| 548 | 548 | if (empty($etag)) { |
| 549 | 549 | return false; |
| 550 | 550 | } |
| 551 | 551 | return $node->setEtag($etag) !== -1; |
| 552 | 552 | }); |
| 553 | - $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function ($time) use ($node) { |
|
| 553 | + $propPatch->handle(self::CREATIONDATE_PROPERTYNAME, function($time) use ($node) { |
|
| 554 | 554 | if (empty($time)) { |
| 555 | 555 | return false; |
| 556 | 556 | } |
@@ -558,11 +558,11 @@ discard block |
||
| 558 | 558 | $node->setCreationTime($dateTime->getTimestamp()); |
| 559 | 559 | return true; |
| 560 | 560 | }); |
| 561 | - $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function ($time) use ($node) { |
|
| 561 | + $propPatch->handle(self::CREATION_TIME_PROPERTYNAME, function($time) use ($node) { |
|
| 562 | 562 | if (empty($time)) { |
| 563 | 563 | return false; |
| 564 | 564 | } |
| 565 | - $node->setCreationTime((int)$time); |
|
| 565 | + $node->setCreationTime((int) $time); |
|
| 566 | 566 | return true; |
| 567 | 567 | }); |
| 568 | 568 | |
@@ -572,7 +572,7 @@ discard block |
||
| 572 | 572 | * Disable modification of the displayname property for files and |
| 573 | 573 | * folders via PROPPATCH. See PROPFIND for more information. |
| 574 | 574 | */ |
| 575 | - $propPatch->handle(self::DISPLAYNAME_PROPERTYNAME, function ($displayName) { |
|
| 575 | + $propPatch->handle(self::DISPLAYNAME_PROPERTYNAME, function($displayName) { |
|
| 576 | 576 | return 403; |
| 577 | 577 | }); |
| 578 | 578 | } |
@@ -603,15 +603,15 @@ discard block |
||
| 603 | 603 | |
| 604 | 604 | $propPatch->handle( |
| 605 | 605 | $mutation, |
| 606 | - function (mixed $value) use ($accessRight, $knownMetadata, $node, $mutation, $filesMetadataManager): bool { |
|
| 606 | + function(mixed $value) use ($accessRight, $knownMetadata, $node, $mutation, $filesMetadataManager): bool { |
|
| 607 | 607 | /** @var FilesMetadata $metadata */ |
| 608 | - $metadata = $filesMetadataManager->getMetadata((int)$node->getFileId(), true); |
|
| 608 | + $metadata = $filesMetadataManager->getMetadata((int) $node->getFileId(), true); |
|
| 609 | 609 | $metadata->setStorageId($node->getNode()->getStorage()->getCache()->getNumericStorageId()); |
| 610 | 610 | $metadataKey = substr($mutation, strlen(self::FILE_METADATA_PREFIX)); |
| 611 | 611 | |
| 612 | 612 | // confirm metadata key is editable via PROPPATCH |
| 613 | 613 | if ($knownMetadata->getEditPermission($metadataKey) < $accessRight) { |
| 614 | - throw new FilesMetadataException('you do not have enough rights to update \'' . $metadataKey . '\' on this node'); |
|
| 614 | + throw new FilesMetadataException('you do not have enough rights to update \''.$metadataKey.'\' on this node'); |
|
| 615 | 615 | } |
| 616 | 616 | |
| 617 | 617 | if ($value === null) { |
@@ -71,2152 +71,2152 @@ |
||
| 71 | 71 | */ |
| 72 | 72 | class ShareAPIController extends OCSController { |
| 73 | 73 | |
| 74 | - private ?Node $lockedNode = null; |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * Share20OCS constructor. |
|
| 78 | - */ |
|
| 79 | - public function __construct( |
|
| 80 | - string $appName, |
|
| 81 | - IRequest $request, |
|
| 82 | - private IManager $shareManager, |
|
| 83 | - private IGroupManager $groupManager, |
|
| 84 | - private IUserManager $userManager, |
|
| 85 | - private IRootFolder $rootFolder, |
|
| 86 | - private IURLGenerator $urlGenerator, |
|
| 87 | - private IL10N $l, |
|
| 88 | - private IConfig $config, |
|
| 89 | - private IAppManager $appManager, |
|
| 90 | - private ContainerInterface $serverContainer, |
|
| 91 | - private IUserStatusManager $userStatusManager, |
|
| 92 | - private IPreview $previewManager, |
|
| 93 | - private IDateTimeZone $dateTimeZone, |
|
| 94 | - private LoggerInterface $logger, |
|
| 95 | - private IProviderFactory $factory, |
|
| 96 | - private IMailer $mailer, |
|
| 97 | - private ?string $userId = null, |
|
| 98 | - ) { |
|
| 99 | - parent::__construct($appName, $request); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Convert an IShare to an array for OCS output |
|
| 104 | - * |
|
| 105 | - * @param IShare $share |
|
| 106 | - * @param Node|null $recipientNode |
|
| 107 | - * @return Files_SharingShare |
|
| 108 | - * @throws NotFoundException In case the node can't be resolved. |
|
| 109 | - * |
|
| 110 | - * @suppress PhanUndeclaredClassMethod |
|
| 111 | - */ |
|
| 112 | - protected function formatShare(IShare $share, ?Node $recipientNode = null): array { |
|
| 113 | - $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 114 | - $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
| 115 | - |
|
| 116 | - $isOwnShare = false; |
|
| 117 | - if ($shareOwner !== null) { |
|
| 118 | - $isOwnShare = $shareOwner->getUID() === $this->userId; |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $result = [ |
|
| 122 | - 'id' => $share->getId(), |
|
| 123 | - 'share_type' => $share->getShareType(), |
|
| 124 | - 'uid_owner' => $share->getSharedBy(), |
|
| 125 | - 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
| 126 | - // recipient permissions |
|
| 127 | - 'permissions' => $share->getPermissions(), |
|
| 128 | - // current user permissions on this share |
|
| 129 | - 'can_edit' => $this->canEditShare($share), |
|
| 130 | - 'can_delete' => $this->canDeleteShare($share), |
|
| 131 | - 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 132 | - 'parent' => null, |
|
| 133 | - 'expiration' => null, |
|
| 134 | - 'token' => null, |
|
| 135 | - 'uid_file_owner' => $share->getShareOwner(), |
|
| 136 | - 'note' => $share->getNote(), |
|
| 137 | - 'label' => $share->getLabel(), |
|
| 138 | - 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
| 139 | - ]; |
|
| 140 | - |
|
| 141 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 142 | - if ($recipientNode) { |
|
| 143 | - $node = $recipientNode; |
|
| 144 | - } else { |
|
| 145 | - $node = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 146 | - if (!$node) { |
|
| 147 | - // fallback to guessing the path |
|
| 148 | - $node = $userFolder->get($share->getTarget()); |
|
| 149 | - if ($node === null || $share->getTarget() === '') { |
|
| 150 | - throw new NotFoundException(); |
|
| 151 | - } |
|
| 152 | - } |
|
| 153 | - } |
|
| 154 | - |
|
| 155 | - $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 156 | - if ($node instanceof Folder) { |
|
| 157 | - $result['item_type'] = 'folder'; |
|
| 158 | - } else { |
|
| 159 | - $result['item_type'] = 'file'; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - // Get the original node permission if the share owner is the current user |
|
| 163 | - if ($isOwnShare) { |
|
| 164 | - $result['item_permissions'] = $node->getPermissions(); |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - // If we're on the recipient side, the node permissions |
|
| 168 | - // are bound to the share permissions. So we need to |
|
| 169 | - // adjust the permissions to the share permissions if necessary. |
|
| 170 | - if (!$isOwnShare) { |
|
| 171 | - $result['item_permissions'] = $share->getPermissions(); |
|
| 172 | - |
|
| 173 | - // For some reason, single files share are forbidden to have the delete permission |
|
| 174 | - // since we have custom methods to check those, let's adjust straight away. |
|
| 175 | - // DAV permissions does not have that issue though. |
|
| 176 | - if ($this->canDeleteShare($share) || $this->canDeleteShareFromSelf($share)) { |
|
| 177 | - $result['item_permissions'] |= Constants::PERMISSION_DELETE; |
|
| 178 | - } |
|
| 179 | - if ($this->canEditShare($share)) { |
|
| 180 | - $result['item_permissions'] |= Constants::PERMISSION_UPDATE; |
|
| 181 | - } |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - // See MOUNT_ROOT_PROPERTYNAME dav property |
|
| 185 | - $result['is-mount-root'] = $node->getInternalPath() === ''; |
|
| 186 | - $result['mount-type'] = $node->getMountPoint()->getMountType(); |
|
| 187 | - |
|
| 188 | - $result['mimetype'] = $node->getMimetype(); |
|
| 189 | - $result['has_preview'] = $this->previewManager->isAvailable($node); |
|
| 190 | - $result['storage_id'] = $node->getStorage()->getId(); |
|
| 191 | - $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 192 | - $result['item_source'] = $node->getId(); |
|
| 193 | - $result['file_source'] = $node->getId(); |
|
| 194 | - $result['file_parent'] = $node->getParent()->getId(); |
|
| 195 | - $result['file_target'] = $share->getTarget(); |
|
| 196 | - $result['item_size'] = $node->getSize(); |
|
| 197 | - $result['item_mtime'] = $node->getMTime(); |
|
| 198 | - |
|
| 199 | - $expiration = $share->getExpirationDate(); |
|
| 200 | - if ($expiration !== null) { |
|
| 201 | - $expiration->setTimezone($this->dateTimeZone->getTimeZone()); |
|
| 202 | - $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 203 | - } |
|
| 204 | - |
|
| 205 | - if ($share->getShareType() === IShare::TYPE_USER) { |
|
| 206 | - $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 207 | - $result['share_with'] = $share->getSharedWith(); |
|
| 208 | - $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
| 209 | - $result['share_with_displayname_unique'] = $sharedWith !== null ? ( |
|
| 210 | - !empty($sharedWith->getSystemEMailAddress()) ? $sharedWith->getSystemEMailAddress() : $sharedWith->getUID() |
|
| 211 | - ) : $share->getSharedWith(); |
|
| 212 | - |
|
| 213 | - $userStatuses = $this->userStatusManager->getUserStatuses([$share->getSharedWith()]); |
|
| 214 | - $userStatus = array_shift($userStatuses); |
|
| 215 | - if ($userStatus) { |
|
| 216 | - $result['status'] = [ |
|
| 217 | - 'status' => $userStatus->getStatus(), |
|
| 218 | - 'message' => $userStatus->getMessage(), |
|
| 219 | - 'icon' => $userStatus->getIcon(), |
|
| 220 | - 'clearAt' => $userStatus->getClearAt() |
|
| 221 | - ? (int)$userStatus->getClearAt()->format('U') |
|
| 222 | - : null, |
|
| 223 | - ]; |
|
| 224 | - } |
|
| 225 | - } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
| 226 | - $group = $this->groupManager->get($share->getSharedWith()); |
|
| 227 | - $result['share_with'] = $share->getSharedWith(); |
|
| 228 | - $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 229 | - } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
| 230 | - |
|
| 231 | - // "share_with" and "share_with_displayname" for passwords of link |
|
| 232 | - // shares was deprecated in Nextcloud 15, use "password" instead. |
|
| 233 | - $result['share_with'] = $share->getPassword(); |
|
| 234 | - $result['share_with_displayname'] = '(' . $this->l->t('Shared link') . ')'; |
|
| 235 | - |
|
| 236 | - $result['password'] = $share->getPassword(); |
|
| 237 | - |
|
| 238 | - $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 239 | - |
|
| 240 | - $result['token'] = $share->getToken(); |
|
| 241 | - $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
| 242 | - } elseif ($share->getShareType() === IShare::TYPE_REMOTE) { |
|
| 243 | - $result['share_with'] = $share->getSharedWith(); |
|
| 244 | - $result['share_with_displayname'] = $this->getCachedFederatedDisplayName($share->getSharedWith()); |
|
| 245 | - $result['token'] = $share->getToken(); |
|
| 246 | - } elseif ($share->getShareType() === IShare::TYPE_REMOTE_GROUP) { |
|
| 247 | - $result['share_with'] = $share->getSharedWith(); |
|
| 248 | - $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD'); |
|
| 249 | - $result['token'] = $share->getToken(); |
|
| 250 | - } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
| 251 | - $result['share_with'] = $share->getSharedWith(); |
|
| 252 | - $result['password'] = $share->getPassword(); |
|
| 253 | - $result['password_expiration_time'] = $share->getPasswordExpirationTime() !== null ? $share->getPasswordExpirationTime()->format(\DateTime::ATOM) : null; |
|
| 254 | - $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 255 | - $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL'); |
|
| 256 | - $result['token'] = $share->getToken(); |
|
| 257 | - } elseif ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
| 258 | - // getSharedWith() returns either "name (type, owner)" or |
|
| 259 | - // "name (type, owner) [id]", depending on the Teams app version. |
|
| 260 | - $hasCircleId = (substr($share->getSharedWith(), -1) === ']'); |
|
| 261 | - |
|
| 262 | - $result['share_with_displayname'] = $share->getSharedWithDisplayName(); |
|
| 263 | - if (empty($result['share_with_displayname'])) { |
|
| 264 | - $displayNameLength = ($hasCircleId ? strrpos($share->getSharedWith(), ' ') : strlen($share->getSharedWith())); |
|
| 265 | - $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $result['share_with_avatar'] = $share->getSharedWithAvatar(); |
|
| 269 | - |
|
| 270 | - $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0); |
|
| 271 | - $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' ')); |
|
| 272 | - if ($shareWithLength === false) { |
|
| 273 | - $result['share_with'] = substr($share->getSharedWith(), $shareWithStart); |
|
| 274 | - } else { |
|
| 275 | - $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 276 | - } |
|
| 277 | - } elseif ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 278 | - $result['share_with'] = $share->getSharedWith(); |
|
| 279 | - $result['share_with_displayname'] = ''; |
|
| 280 | - |
|
| 281 | - try { |
|
| 282 | - /** @var array{share_with_displayname: string, share_with_link: string, share_with?: string, token?: string} $roomShare */ |
|
| 283 | - $roomShare = $this->getRoomShareHelper()->formatShare($share); |
|
| 284 | - $result = array_merge($result, $roomShare); |
|
| 285 | - } catch (ContainerExceptionInterface $e) { |
|
| 286 | - } |
|
| 287 | - } elseif ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 288 | - $result['share_with'] = $share->getSharedWith(); |
|
| 289 | - $result['share_with_displayname'] = ''; |
|
| 290 | - |
|
| 291 | - try { |
|
| 292 | - /** @var array{share_with: string, share_with_displayname: string, share_with_link: string} $deckShare */ |
|
| 293 | - $deckShare = $this->getDeckShareHelper()->formatShare($share); |
|
| 294 | - $result = array_merge($result, $deckShare); |
|
| 295 | - } catch (ContainerExceptionInterface $e) { |
|
| 296 | - } |
|
| 297 | - } elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 298 | - $result['share_with'] = $share->getSharedWith(); |
|
| 299 | - $result['share_with_displayname'] = ''; |
|
| 300 | - |
|
| 301 | - try { |
|
| 302 | - /** @var array{share_with: string, share_with_displayname: string, token: string} $scienceMeshShare */ |
|
| 303 | - $scienceMeshShare = $this->getSciencemeshShareHelper()->formatShare($share); |
|
| 304 | - $result = array_merge($result, $scienceMeshShare); |
|
| 305 | - } catch (ContainerExceptionInterface $e) { |
|
| 306 | - } |
|
| 307 | - } |
|
| 308 | - |
|
| 309 | - |
|
| 310 | - $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
| 311 | - $result['hide_download'] = $share->getHideDownload() ? 1 : 0; |
|
| 312 | - |
|
| 313 | - $result['attributes'] = null; |
|
| 314 | - if ($attributes = $share->getAttributes()) { |
|
| 315 | - $result['attributes'] = (string)\json_encode($attributes->toArray()); |
|
| 316 | - } |
|
| 317 | - |
|
| 318 | - return $result; |
|
| 319 | - } |
|
| 320 | - |
|
| 321 | - /** |
|
| 322 | - * Check if one of the users address books knows the exact property, if |
|
| 323 | - * not we return the full name. |
|
| 324 | - * |
|
| 325 | - * @param string $query |
|
| 326 | - * @param string $property |
|
| 327 | - * @return string |
|
| 328 | - */ |
|
| 329 | - private function getDisplayNameFromAddressBook(string $query, string $property): string { |
|
| 330 | - // FIXME: If we inject the contacts manager it gets initialized before any address books are registered |
|
| 331 | - try { |
|
| 332 | - $result = Server::get(\OCP\Contacts\IManager::class)->search($query, [$property], [ |
|
| 333 | - 'limit' => 1, |
|
| 334 | - 'enumeration' => false, |
|
| 335 | - 'strict_search' => true, |
|
| 336 | - ]); |
|
| 337 | - } catch (Exception $e) { |
|
| 338 | - $this->logger->error( |
|
| 339 | - $e->getMessage(), |
|
| 340 | - ['exception' => $e] |
|
| 341 | - ); |
|
| 342 | - return $query; |
|
| 343 | - } |
|
| 344 | - |
|
| 345 | - foreach ($result as $r) { |
|
| 346 | - foreach ($r[$property] as $value) { |
|
| 347 | - if ($value === $query && $r['FN']) { |
|
| 348 | - return $r['FN']; |
|
| 349 | - } |
|
| 350 | - } |
|
| 351 | - } |
|
| 352 | - |
|
| 353 | - return $query; |
|
| 354 | - } |
|
| 355 | - |
|
| 356 | - |
|
| 357 | - /** |
|
| 358 | - * @param list<Files_SharingShare> $shares |
|
| 359 | - * @param array<string, string>|null $updatedDisplayName |
|
| 360 | - * |
|
| 361 | - * @return list<Files_SharingShare> |
|
| 362 | - */ |
|
| 363 | - private function fixMissingDisplayName(array $shares, ?array $updatedDisplayName = null): array { |
|
| 364 | - $userIds = $updated = []; |
|
| 365 | - foreach ($shares as $share) { |
|
| 366 | - // share is federated and share have no display name yet |
|
| 367 | - if ($share['share_type'] === IShare::TYPE_REMOTE |
|
| 368 | - && ($share['share_with'] ?? '') !== '' |
|
| 369 | - && ($share['share_with_displayname'] ?? '') === '') { |
|
| 370 | - $userIds[] = $userId = $share['share_with']; |
|
| 371 | - |
|
| 372 | - if ($updatedDisplayName !== null && array_key_exists($userId, $updatedDisplayName)) { |
|
| 373 | - $share['share_with_displayname'] = $updatedDisplayName[$userId]; |
|
| 374 | - } |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - // prepping userIds with displayName to be updated |
|
| 378 | - $updated[] = $share; |
|
| 379 | - } |
|
| 380 | - |
|
| 381 | - // if $updatedDisplayName is not null, it means we should have already fixed displayNames of the shares |
|
| 382 | - if ($updatedDisplayName !== null) { |
|
| 383 | - return $updated; |
|
| 384 | - } |
|
| 385 | - |
|
| 386 | - // get displayName for the generated list of userId with no displayName |
|
| 387 | - $displayNames = $this->retrieveFederatedDisplayName($userIds); |
|
| 388 | - |
|
| 389 | - // if no displayName are updated, we exit |
|
| 390 | - if (empty($displayNames)) { |
|
| 391 | - return $updated; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - // let's fix missing display name and returns all shares |
|
| 395 | - return $this->fixMissingDisplayName($shares, $displayNames); |
|
| 396 | - } |
|
| 397 | - |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * get displayName of a list of userIds from the lookup-server; through the globalsiteselector app. |
|
| 401 | - * returns an array with userIds as keys and displayName as values. |
|
| 402 | - * |
|
| 403 | - * @param array $userIds |
|
| 404 | - * @param bool $cacheOnly - do not reach LUS, get data from cache. |
|
| 405 | - * |
|
| 406 | - * @return array |
|
| 407 | - * @throws ContainerExceptionInterface |
|
| 408 | - */ |
|
| 409 | - private function retrieveFederatedDisplayName(array $userIds, bool $cacheOnly = false): array { |
|
| 410 | - // check if gss is enabled and available |
|
| 411 | - if (count($userIds) === 0 |
|
| 412 | - || !$this->appManager->isEnabledForAnyone('globalsiteselector') |
|
| 413 | - || !class_exists('\OCA\GlobalSiteSelector\Service\SlaveService')) { |
|
| 414 | - return []; |
|
| 415 | - } |
|
| 416 | - |
|
| 417 | - try { |
|
| 418 | - $slaveService = Server::get(SlaveService::class); |
|
| 419 | - } catch (\Throwable $e) { |
|
| 420 | - $this->logger->error( |
|
| 421 | - $e->getMessage(), |
|
| 422 | - ['exception' => $e] |
|
| 423 | - ); |
|
| 424 | - return []; |
|
| 425 | - } |
|
| 426 | - |
|
| 427 | - return $slaveService->getUsersDisplayName($userIds, $cacheOnly); |
|
| 428 | - } |
|
| 429 | - |
|
| 430 | - |
|
| 431 | - /** |
|
| 432 | - * retrieve displayName from cache if available (should be used on federated shares) |
|
| 433 | - * if not available in cache/lus, try for get from address-book, else returns empty string. |
|
| 434 | - * |
|
| 435 | - * @param string $userId |
|
| 436 | - * @param bool $cacheOnly if true will not reach the lus but will only get data from cache |
|
| 437 | - * |
|
| 438 | - * @return string |
|
| 439 | - */ |
|
| 440 | - private function getCachedFederatedDisplayName(string $userId, bool $cacheOnly = true): string { |
|
| 441 | - $details = $this->retrieveFederatedDisplayName([$userId], $cacheOnly); |
|
| 442 | - if (array_key_exists($userId, $details)) { |
|
| 443 | - return $details[$userId]; |
|
| 444 | - } |
|
| 445 | - |
|
| 446 | - $displayName = $this->getDisplayNameFromAddressBook($userId, 'CLOUD'); |
|
| 447 | - return ($displayName === $userId) ? '' : $displayName; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - |
|
| 451 | - |
|
| 452 | - /** |
|
| 453 | - * Get a specific share by id |
|
| 454 | - * |
|
| 455 | - * @param string $id ID of the share |
|
| 456 | - * @param bool $include_tags Include tags in the share |
|
| 457 | - * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 458 | - * @throws OCSNotFoundException Share not found |
|
| 459 | - * |
|
| 460 | - * 200: Share returned |
|
| 461 | - */ |
|
| 462 | - #[NoAdminRequired] |
|
| 463 | - public function getShare(string $id, bool $include_tags = false): DataResponse { |
|
| 464 | - try { |
|
| 465 | - $share = $this->getShareById($id); |
|
| 466 | - } catch (ShareNotFound $e) { |
|
| 467 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - try { |
|
| 471 | - if ($this->canAccessShare($share)) { |
|
| 472 | - $share = $this->formatShare($share); |
|
| 473 | - |
|
| 474 | - if ($include_tags) { |
|
| 475 | - $share = Helper::populateTags([$share], Server::get(ITagManager::class)); |
|
| 476 | - } else { |
|
| 477 | - $share = [$share]; |
|
| 478 | - } |
|
| 479 | - |
|
| 480 | - return new DataResponse($share); |
|
| 481 | - } |
|
| 482 | - } catch (NotFoundException $e) { |
|
| 483 | - // Fall through |
|
| 484 | - } |
|
| 485 | - |
|
| 486 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 487 | - } |
|
| 488 | - |
|
| 489 | - /** |
|
| 490 | - * Delete a share |
|
| 491 | - * |
|
| 492 | - * @param string $id ID of the share |
|
| 493 | - * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 494 | - * @throws OCSNotFoundException Share not found |
|
| 495 | - * @throws OCSForbiddenException Missing permissions to delete the share |
|
| 496 | - * |
|
| 497 | - * 200: Share deleted successfully |
|
| 498 | - */ |
|
| 499 | - #[NoAdminRequired] |
|
| 500 | - public function deleteShare(string $id): DataResponse { |
|
| 501 | - try { |
|
| 502 | - $share = $this->getShareById($id); |
|
| 503 | - } catch (ShareNotFound $e) { |
|
| 504 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 505 | - } |
|
| 506 | - |
|
| 507 | - try { |
|
| 508 | - $this->lock($share->getNode()); |
|
| 509 | - } catch (LockedException $e) { |
|
| 510 | - throw new OCSNotFoundException($this->l->t('Could not delete share')); |
|
| 511 | - } |
|
| 512 | - |
|
| 513 | - if (!$this->canAccessShare($share)) { |
|
| 514 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 515 | - } |
|
| 516 | - |
|
| 517 | - // if it's a group share or a room share |
|
| 518 | - // we don't delete the share, but only the |
|
| 519 | - // mount point. Allowing it to be restored |
|
| 520 | - // from the deleted shares |
|
| 521 | - if ($this->canDeleteShareFromSelf($share)) { |
|
| 522 | - $this->shareManager->deleteFromSelf($share, $this->userId); |
|
| 523 | - } else { |
|
| 524 | - if (!$this->canDeleteShare($share)) { |
|
| 525 | - throw new OCSForbiddenException($this->l->t('Could not delete share')); |
|
| 526 | - } |
|
| 527 | - |
|
| 528 | - $this->shareManager->deleteShare($share); |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - return new DataResponse(); |
|
| 532 | - } |
|
| 533 | - |
|
| 534 | - /** |
|
| 535 | - * Create a share |
|
| 536 | - * |
|
| 537 | - * @param string|null $path Path of the share |
|
| 538 | - * @param int|null $permissions Permissions for the share |
|
| 539 | - * @param int $shareType Type of the share |
|
| 540 | - * @param ?string $shareWith The entity this should be shared with |
|
| 541 | - * @param 'true'|'false'|null $publicUpload If public uploading is allowed (deprecated) |
|
| 542 | - * @param string $password Password for the share |
|
| 543 | - * @param string|null $sendPasswordByTalk Send the password for the share over Talk |
|
| 544 | - * @param ?string $expireDate The expiry date of the share in the user's timezone at 00:00. |
|
| 545 | - * If $expireDate is not supplied or set to `null`, the system default will be used. |
|
| 546 | - * @param string $note Note for the share |
|
| 547 | - * @param string $label Label for the share (only used in link and email) |
|
| 548 | - * @param string|null $attributes Additional attributes for the share |
|
| 549 | - * @param 'false'|'true'|null $sendMail Send a mail to the recipient |
|
| 550 | - * |
|
| 551 | - * @return DataResponse<Http::STATUS_OK, Files_SharingShare, array{}> |
|
| 552 | - * @throws OCSBadRequestException Unknown share type |
|
| 553 | - * @throws OCSException |
|
| 554 | - * @throws OCSForbiddenException Creating the share is not allowed |
|
| 555 | - * @throws OCSNotFoundException Creating the share failed |
|
| 556 | - * @suppress PhanUndeclaredClassMethod |
|
| 557 | - * |
|
| 558 | - * 200: Share created |
|
| 559 | - */ |
|
| 560 | - #[NoAdminRequired] |
|
| 561 | - #[UserRateLimit(limit: 20, period: 600)] |
|
| 562 | - public function createShare( |
|
| 563 | - ?string $path = null, |
|
| 564 | - ?int $permissions = null, |
|
| 565 | - int $shareType = -1, |
|
| 566 | - ?string $shareWith = null, |
|
| 567 | - ?string $publicUpload = null, |
|
| 568 | - string $password = '', |
|
| 569 | - ?string $sendPasswordByTalk = null, |
|
| 570 | - ?string $expireDate = null, |
|
| 571 | - string $note = '', |
|
| 572 | - string $label = '', |
|
| 573 | - ?string $attributes = null, |
|
| 574 | - ?string $sendMail = null, |
|
| 575 | - ): DataResponse { |
|
| 576 | - assert($this->userId !== null); |
|
| 577 | - |
|
| 578 | - $share = $this->shareManager->newShare(); |
|
| 579 | - $hasPublicUpload = $this->getLegacyPublicUpload($publicUpload); |
|
| 580 | - |
|
| 581 | - // Verify path |
|
| 582 | - if ($path === null) { |
|
| 583 | - throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); |
|
| 584 | - } |
|
| 585 | - |
|
| 586 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 587 | - try { |
|
| 588 | - /** @var \OC\Files\Node\Node $node */ |
|
| 589 | - $node = $userFolder->get($path); |
|
| 590 | - } catch (NotFoundException $e) { |
|
| 591 | - throw new OCSNotFoundException($this->l->t('Wrong path, file/folder does not exist')); |
|
| 592 | - } |
|
| 593 | - |
|
| 594 | - // a user can have access to a file through different paths, with differing permissions |
|
| 595 | - // combine all permissions to determine if the user can share this file |
|
| 596 | - $nodes = $userFolder->getById($node->getId()); |
|
| 597 | - foreach ($nodes as $nodeById) { |
|
| 598 | - /** @var \OC\Files\FileInfo $fileInfo */ |
|
| 599 | - $fileInfo = $node->getFileInfo(); |
|
| 600 | - $fileInfo['permissions'] |= $nodeById->getPermissions(); |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - $share->setNode($node); |
|
| 604 | - |
|
| 605 | - try { |
|
| 606 | - $this->lock($share->getNode()); |
|
| 607 | - } catch (LockedException $e) { |
|
| 608 | - throw new OCSNotFoundException($this->l->t('Could not create share')); |
|
| 609 | - } |
|
| 610 | - |
|
| 611 | - // Set permissions |
|
| 612 | - if ($shareType === IShare::TYPE_LINK || $shareType === IShare::TYPE_EMAIL) { |
|
| 613 | - $permissions = $this->getLinkSharePermissions($permissions, $hasPublicUpload); |
|
| 614 | - $this->validateLinkSharePermissions($node, $permissions, $hasPublicUpload); |
|
| 615 | - } else { |
|
| 616 | - // Use default permissions only for non-link shares to keep legacy behavior |
|
| 617 | - if ($permissions === null) { |
|
| 618 | - $permissions = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL); |
|
| 619 | - } |
|
| 620 | - // Non-link shares always require read permissions (link shares could be file drop) |
|
| 621 | - $permissions |= Constants::PERMISSION_READ; |
|
| 622 | - } |
|
| 623 | - |
|
| 624 | - // For legacy reasons the API allows to pass PERMISSIONS_ALL even for single file shares (I look at you Talk) |
|
| 625 | - if ($node instanceof File) { |
|
| 626 | - // if this is a single file share we remove the DELETE and CREATE permissions |
|
| 627 | - $permissions = $permissions & ~(Constants::PERMISSION_DELETE | Constants::PERMISSION_CREATE); |
|
| 628 | - } |
|
| 629 | - |
|
| 630 | - /** |
|
| 631 | - * Hack for https://github.com/owncloud/core/issues/22587 |
|
| 632 | - * We check the permissions via webdav. But the permissions of the mount point |
|
| 633 | - * do not equal the share permissions. Here we fix that for federated mounts. |
|
| 634 | - */ |
|
| 635 | - if ($node->getStorage()->instanceOfStorage(Storage::class)) { |
|
| 636 | - $permissions &= ~($permissions & ~$node->getPermissions()); |
|
| 637 | - } |
|
| 638 | - |
|
| 639 | - if ($attributes !== null) { |
|
| 640 | - $share = $this->setShareAttributes($share, $attributes); |
|
| 641 | - } |
|
| 642 | - |
|
| 643 | - // Expire date checks |
|
| 644 | - // Normally, null means no expiration date but we still set the default for backwards compatibility |
|
| 645 | - // If the client sends an empty string, we set noExpirationDate to true |
|
| 646 | - if ($expireDate !== null) { |
|
| 647 | - if ($expireDate !== '') { |
|
| 648 | - try { |
|
| 649 | - $expireDateTime = $this->parseDate($expireDate); |
|
| 650 | - $share->setExpirationDate($expireDateTime); |
|
| 651 | - } catch (\Exception $e) { |
|
| 652 | - throw new OCSNotFoundException($e->getMessage(), $e); |
|
| 653 | - } |
|
| 654 | - } else { |
|
| 655 | - // Client sent empty string for expire date. |
|
| 656 | - // Set noExpirationDate to true so overwrite is prevented. |
|
| 657 | - $share->setNoExpirationDate(true); |
|
| 658 | - } |
|
| 659 | - } |
|
| 660 | - |
|
| 661 | - $share->setSharedBy($this->userId); |
|
| 662 | - |
|
| 663 | - // Handle mail send |
|
| 664 | - if (is_null($sendMail)) { |
|
| 665 | - $allowSendMail = $this->config->getSystemValueBool('sharing.enable_share_mail', true); |
|
| 666 | - if ($allowSendMail !== true || $shareType === IShare::TYPE_EMAIL) { |
|
| 667 | - // Define a default behavior when sendMail is not provided |
|
| 668 | - // For email shares with a valid recipient, the default is to send the mail |
|
| 669 | - // For all other share types, the default is to not send the mail |
|
| 670 | - $allowSendMail = ($shareType === IShare::TYPE_EMAIL && $shareWith !== null && $shareWith !== ''); |
|
| 671 | - } |
|
| 672 | - $share->setMailSend($allowSendMail); |
|
| 673 | - } else { |
|
| 674 | - $share->setMailSend($sendMail === 'true'); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - if ($shareType === IShare::TYPE_USER) { |
|
| 678 | - // Valid user is required to share |
|
| 679 | - if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
| 680 | - throw new OCSNotFoundException($this->l->t('Please specify a valid account to share with')); |
|
| 681 | - } |
|
| 682 | - $share->setSharedWith($shareWith); |
|
| 683 | - $share->setPermissions($permissions); |
|
| 684 | - } elseif ($shareType === IShare::TYPE_GROUP) { |
|
| 685 | - if (!$this->shareManager->allowGroupSharing()) { |
|
| 686 | - throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); |
|
| 687 | - } |
|
| 688 | - |
|
| 689 | - // Valid group is required to share |
|
| 690 | - if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
| 691 | - throw new OCSNotFoundException($this->l->t('Please specify a valid group')); |
|
| 692 | - } |
|
| 693 | - $share->setSharedWith($shareWith); |
|
| 694 | - $share->setPermissions($permissions); |
|
| 695 | - } elseif ($shareType === IShare::TYPE_LINK |
|
| 696 | - || $shareType === IShare::TYPE_EMAIL) { |
|
| 697 | - |
|
| 698 | - // Can we even share links? |
|
| 699 | - if (!$this->shareManager->shareApiAllowLinks()) { |
|
| 700 | - throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); |
|
| 701 | - } |
|
| 702 | - |
|
| 703 | - $this->validateLinkSharePermissions($node, $permissions, $hasPublicUpload); |
|
| 704 | - $share->setPermissions($permissions); |
|
| 705 | - |
|
| 706 | - // Set password |
|
| 707 | - if ($password !== '') { |
|
| 708 | - $share->setPassword($password); |
|
| 709 | - } |
|
| 710 | - |
|
| 711 | - // Only share by mail have a recipient |
|
| 712 | - if (is_string($shareWith) && $shareType === IShare::TYPE_EMAIL) { |
|
| 713 | - // If sending a mail have been requested, validate the mail address |
|
| 714 | - if ($share->getMailSend() && !$this->mailer->validateMailAddress($shareWith)) { |
|
| 715 | - throw new OCSNotFoundException($this->l->t('Please specify a valid email address')); |
|
| 716 | - } |
|
| 717 | - $share->setSharedWith($shareWith); |
|
| 718 | - } |
|
| 719 | - |
|
| 720 | - // If we have a label, use it |
|
| 721 | - if ($label !== '') { |
|
| 722 | - if (strlen($label) > 255) { |
|
| 723 | - throw new OCSBadRequestException('Maximum label length is 255'); |
|
| 724 | - } |
|
| 725 | - $share->setLabel($label); |
|
| 726 | - } |
|
| 727 | - |
|
| 728 | - if ($sendPasswordByTalk === 'true') { |
|
| 729 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 730 | - throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$node->getPath()])); |
|
| 731 | - } |
|
| 732 | - |
|
| 733 | - $share->setSendPasswordByTalk(true); |
|
| 734 | - } |
|
| 735 | - } elseif ($shareType === IShare::TYPE_REMOTE) { |
|
| 736 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 737 | - throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$node->getPath(), $shareType])); |
|
| 738 | - } |
|
| 739 | - |
|
| 740 | - if ($shareWith === null) { |
|
| 741 | - throw new OCSNotFoundException($this->l->t('Please specify a valid federated account ID')); |
|
| 742 | - } |
|
| 743 | - |
|
| 744 | - $share->setSharedWith($shareWith); |
|
| 745 | - $share->setPermissions($permissions); |
|
| 746 | - $share->setSharedWithDisplayName($this->getCachedFederatedDisplayName($shareWith, false)); |
|
| 747 | - } elseif ($shareType === IShare::TYPE_REMOTE_GROUP) { |
|
| 748 | - if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 749 | - throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$node->getPath(), $shareType])); |
|
| 750 | - } |
|
| 751 | - |
|
| 752 | - if ($shareWith === null) { |
|
| 753 | - throw new OCSNotFoundException($this->l->t('Please specify a valid federated group ID')); |
|
| 754 | - } |
|
| 755 | - |
|
| 756 | - $share->setSharedWith($shareWith); |
|
| 757 | - $share->setPermissions($permissions); |
|
| 758 | - } elseif ($shareType === IShare::TYPE_CIRCLE) { |
|
| 759 | - if (!Server::get(IAppManager::class)->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 760 | - throw new OCSNotFoundException($this->l->t('You cannot share to a Team if the app is not enabled')); |
|
| 761 | - } |
|
| 762 | - |
|
| 763 | - $circle = Circles::detailsCircle($shareWith); |
|
| 764 | - |
|
| 765 | - // Valid team is required to share |
|
| 766 | - if ($circle === null) { |
|
| 767 | - throw new OCSNotFoundException($this->l->t('Please specify a valid team')); |
|
| 768 | - } |
|
| 769 | - $share->setSharedWith($shareWith); |
|
| 770 | - $share->setPermissions($permissions); |
|
| 771 | - } elseif ($shareType === IShare::TYPE_ROOM) { |
|
| 772 | - try { |
|
| 773 | - $this->getRoomShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 774 | - } catch (ContainerExceptionInterface $e) { |
|
| 775 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$node->getPath()])); |
|
| 776 | - } |
|
| 777 | - } elseif ($shareType === IShare::TYPE_DECK) { |
|
| 778 | - try { |
|
| 779 | - $this->getDeckShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 780 | - } catch (ContainerExceptionInterface $e) { |
|
| 781 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$node->getPath()])); |
|
| 782 | - } |
|
| 783 | - } elseif ($shareType === IShare::TYPE_SCIENCEMESH) { |
|
| 784 | - try { |
|
| 785 | - $this->getSciencemeshShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 786 | - } catch (ContainerExceptionInterface $e) { |
|
| 787 | - throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support ScienceMesh shares', [$node->getPath()])); |
|
| 788 | - } |
|
| 789 | - } else { |
|
| 790 | - throw new OCSBadRequestException($this->l->t('Unknown share type')); |
|
| 791 | - } |
|
| 792 | - |
|
| 793 | - $share->setShareType($shareType); |
|
| 794 | - $this->checkInheritedAttributes($share); |
|
| 795 | - |
|
| 796 | - if ($note !== '') { |
|
| 797 | - $share->setNote($note); |
|
| 798 | - } |
|
| 799 | - |
|
| 800 | - try { |
|
| 801 | - $share = $this->shareManager->createShare($share); |
|
| 802 | - } catch (HintException $e) { |
|
| 803 | - $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 804 | - throw new OCSException($e->getHint(), $code); |
|
| 805 | - } catch (GenericShareException|\InvalidArgumentException $e) { |
|
| 806 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 807 | - throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 808 | - } catch (\Exception $e) { |
|
| 809 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 810 | - throw new OCSForbiddenException('Failed to create share.', $e); |
|
| 811 | - } |
|
| 812 | - |
|
| 813 | - $output = $this->formatShare($share); |
|
| 814 | - |
|
| 815 | - return new DataResponse($output); |
|
| 816 | - } |
|
| 817 | - |
|
| 818 | - /** |
|
| 819 | - * @param null|Node $node |
|
| 820 | - * @param boolean $includeTags |
|
| 821 | - * |
|
| 822 | - * @return list<Files_SharingShare> |
|
| 823 | - */ |
|
| 824 | - private function getSharedWithMe($node, bool $includeTags): array { |
|
| 825 | - $userShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_USER, $node, -1, 0); |
|
| 826 | - $groupShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_GROUP, $node, -1, 0); |
|
| 827 | - $circleShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_CIRCLE, $node, -1, 0); |
|
| 828 | - $roomShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_ROOM, $node, -1, 0); |
|
| 829 | - $deckShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_DECK, $node, -1, 0); |
|
| 830 | - $sciencemeshShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, $node, -1, 0); |
|
| 831 | - |
|
| 832 | - $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares, $deckShares, $sciencemeshShares); |
|
| 833 | - |
|
| 834 | - $filteredShares = array_filter($shares, function (IShare $share) { |
|
| 835 | - return $share->getShareOwner() !== $this->userId; |
|
| 836 | - }); |
|
| 837 | - |
|
| 838 | - $formatted = []; |
|
| 839 | - foreach ($filteredShares as $share) { |
|
| 840 | - if ($this->canAccessShare($share)) { |
|
| 841 | - try { |
|
| 842 | - $formatted[] = $this->formatShare($share); |
|
| 843 | - } catch (NotFoundException $e) { |
|
| 844 | - // Ignore this share |
|
| 845 | - } |
|
| 846 | - } |
|
| 847 | - } |
|
| 848 | - |
|
| 849 | - if ($includeTags) { |
|
| 850 | - $formatted = Helper::populateTags($formatted, Server::get(ITagManager::class)); |
|
| 851 | - } |
|
| 852 | - |
|
| 853 | - return $formatted; |
|
| 854 | - } |
|
| 855 | - |
|
| 856 | - /** |
|
| 857 | - * @param Node $folder |
|
| 858 | - * |
|
| 859 | - * @return list<Files_SharingShare> |
|
| 860 | - * @throws OCSBadRequestException |
|
| 861 | - * @throws NotFoundException |
|
| 862 | - */ |
|
| 863 | - private function getSharesInDir(Node $folder): array { |
|
| 864 | - if (!($folder instanceof Folder)) { |
|
| 865 | - throw new OCSBadRequestException($this->l->t('Not a directory')); |
|
| 866 | - } |
|
| 867 | - |
|
| 868 | - $nodes = $folder->getDirectoryListing(); |
|
| 869 | - |
|
| 870 | - /** @var IShare[] $shares */ |
|
| 871 | - $shares = array_reduce($nodes, function ($carry, $node) { |
|
| 872 | - $carry = array_merge($carry, $this->getAllShares($node, true)); |
|
| 873 | - return $carry; |
|
| 874 | - }, []); |
|
| 875 | - |
|
| 876 | - // filter out duplicate shares |
|
| 877 | - $known = []; |
|
| 878 | - |
|
| 879 | - $formatted = $miniFormatted = []; |
|
| 880 | - $resharingRight = false; |
|
| 881 | - $known = []; |
|
| 882 | - foreach ($shares as $share) { |
|
| 883 | - if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->userId) { |
|
| 884 | - continue; |
|
| 885 | - } |
|
| 886 | - |
|
| 887 | - try { |
|
| 888 | - $format = $this->formatShare($share); |
|
| 889 | - |
|
| 890 | - $known[] = $share->getId(); |
|
| 891 | - $formatted[] = $format; |
|
| 892 | - if ($share->getSharedBy() === $this->userId) { |
|
| 893 | - $miniFormatted[] = $format; |
|
| 894 | - } |
|
| 895 | - if (!$resharingRight && $this->shareProviderResharingRights($this->userId, $share, $folder)) { |
|
| 896 | - $resharingRight = true; |
|
| 897 | - } |
|
| 898 | - } catch (\Exception $e) { |
|
| 899 | - //Ignore this share |
|
| 900 | - } |
|
| 901 | - } |
|
| 902 | - |
|
| 903 | - if (!$resharingRight) { |
|
| 904 | - $formatted = $miniFormatted; |
|
| 905 | - } |
|
| 906 | - |
|
| 907 | - return $formatted; |
|
| 908 | - } |
|
| 909 | - |
|
| 910 | - /** |
|
| 911 | - * Get shares of the current user |
|
| 912 | - * |
|
| 913 | - * @param string $shared_with_me Only get shares with the current user |
|
| 914 | - * @param string $reshares Only get shares by the current user and reshares |
|
| 915 | - * @param string $subfiles Only get all shares in a folder |
|
| 916 | - * @param string $path Get shares for a specific path |
|
| 917 | - * @param string $include_tags Include tags in the share |
|
| 918 | - * |
|
| 919 | - * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 920 | - * @throws OCSNotFoundException The folder was not found or is inaccessible |
|
| 921 | - * |
|
| 922 | - * 200: Shares returned |
|
| 923 | - */ |
|
| 924 | - #[NoAdminRequired] |
|
| 925 | - public function getShares( |
|
| 926 | - string $shared_with_me = 'false', |
|
| 927 | - string $reshares = 'false', |
|
| 928 | - string $subfiles = 'false', |
|
| 929 | - string $path = '', |
|
| 930 | - string $include_tags = 'false', |
|
| 931 | - ): DataResponse { |
|
| 932 | - $node = null; |
|
| 933 | - if ($path !== '') { |
|
| 934 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 935 | - try { |
|
| 936 | - $node = $userFolder->get($path); |
|
| 937 | - $this->lock($node); |
|
| 938 | - } catch (NotFoundException $e) { |
|
| 939 | - throw new OCSNotFoundException( |
|
| 940 | - $this->l->t('Wrong path, file/folder does not exist') |
|
| 941 | - ); |
|
| 942 | - } catch (LockedException $e) { |
|
| 943 | - throw new OCSNotFoundException($this->l->t('Could not lock node')); |
|
| 944 | - } |
|
| 945 | - } |
|
| 946 | - |
|
| 947 | - $shares = $this->getFormattedShares( |
|
| 948 | - $this->userId, |
|
| 949 | - $node, |
|
| 950 | - ($shared_with_me === 'true'), |
|
| 951 | - ($reshares === 'true'), |
|
| 952 | - ($subfiles === 'true'), |
|
| 953 | - ($include_tags === 'true') |
|
| 954 | - ); |
|
| 955 | - |
|
| 956 | - return new DataResponse($shares); |
|
| 957 | - } |
|
| 958 | - |
|
| 959 | - private function getLinkSharePermissions(?int $permissions, ?bool $legacyPublicUpload): int { |
|
| 960 | - $permissions = $permissions ?? Constants::PERMISSION_READ; |
|
| 961 | - |
|
| 962 | - // Legacy option handling |
|
| 963 | - if ($legacyPublicUpload !== null) { |
|
| 964 | - $permissions = $legacyPublicUpload |
|
| 965 | - ? (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) |
|
| 966 | - : Constants::PERMISSION_READ; |
|
| 967 | - } |
|
| 968 | - |
|
| 969 | - // TODO: It might make sense to have a dedicated setting to allow/deny converting link shares into federated ones |
|
| 970 | - if ($this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 971 | - && $this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 972 | - $permissions |= Constants::PERMISSION_SHARE; |
|
| 973 | - } |
|
| 974 | - |
|
| 975 | - return $permissions; |
|
| 976 | - } |
|
| 977 | - |
|
| 978 | - /** |
|
| 979 | - * Helper to check for legacy "publicUpload" handling. |
|
| 980 | - * If the value is set to `true` or `false` then true or false are returned. |
|
| 981 | - * Otherwise null is returned to indicate that the option was not (or wrong) set. |
|
| 982 | - * |
|
| 983 | - * @param null|string $legacyPublicUpload The value of `publicUpload` |
|
| 984 | - */ |
|
| 985 | - private function getLegacyPublicUpload(?string $legacyPublicUpload): ?bool { |
|
| 986 | - if ($legacyPublicUpload === 'true') { |
|
| 987 | - return true; |
|
| 988 | - } elseif ($legacyPublicUpload === 'false') { |
|
| 989 | - return false; |
|
| 990 | - } |
|
| 991 | - // Not set at all |
|
| 992 | - return null; |
|
| 993 | - } |
|
| 994 | - |
|
| 995 | - /** |
|
| 996 | - * For link and email shares validate that only allowed combinations are set. |
|
| 997 | - * |
|
| 998 | - * @throw OCSBadRequestException If permission combination is invalid. |
|
| 999 | - * @throw OCSForbiddenException If public upload was forbidden by the administrator. |
|
| 1000 | - */ |
|
| 1001 | - private function validateLinkSharePermissions(Node $node, int $permissions, ?bool $legacyPublicUpload): void { |
|
| 1002 | - if ($legacyPublicUpload && ($node instanceof File)) { |
|
| 1003 | - throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 1004 | - } |
|
| 1005 | - |
|
| 1006 | - // We need at least READ or CREATE (file drop) |
|
| 1007 | - if (!$this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 1008 | - && !$this->hasPermission($permissions, Constants::PERMISSION_CREATE)) { |
|
| 1009 | - throw new OCSBadRequestException($this->l->t('Share must at least have READ or CREATE permissions')); |
|
| 1010 | - } |
|
| 1011 | - |
|
| 1012 | - // UPDATE and DELETE require a READ permission |
|
| 1013 | - if (!$this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 1014 | - && ($this->hasPermission($permissions, Constants::PERMISSION_UPDATE) || $this->hasPermission($permissions, Constants::PERMISSION_DELETE))) { |
|
| 1015 | - throw new OCSBadRequestException($this->l->t('Share must have READ permission if UPDATE or DELETE permission is set')); |
|
| 1016 | - } |
|
| 1017 | - |
|
| 1018 | - // Check if public uploading was disabled |
|
| 1019 | - if ($this->hasPermission($permissions, Constants::PERMISSION_CREATE) |
|
| 1020 | - && !$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 1021 | - throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 1022 | - } |
|
| 1023 | - } |
|
| 1024 | - |
|
| 1025 | - /** |
|
| 1026 | - * @param string $viewer |
|
| 1027 | - * @param Node $node |
|
| 1028 | - * @param bool $sharedWithMe |
|
| 1029 | - * @param bool $reShares |
|
| 1030 | - * @param bool $subFiles |
|
| 1031 | - * @param bool $includeTags |
|
| 1032 | - * |
|
| 1033 | - * @return list<Files_SharingShare> |
|
| 1034 | - * @throws NotFoundException |
|
| 1035 | - * @throws OCSBadRequestException |
|
| 1036 | - */ |
|
| 1037 | - private function getFormattedShares( |
|
| 1038 | - string $viewer, |
|
| 1039 | - $node = null, |
|
| 1040 | - bool $sharedWithMe = false, |
|
| 1041 | - bool $reShares = false, |
|
| 1042 | - bool $subFiles = false, |
|
| 1043 | - bool $includeTags = false, |
|
| 1044 | - ): array { |
|
| 1045 | - if ($sharedWithMe) { |
|
| 1046 | - return $this->getSharedWithMe($node, $includeTags); |
|
| 1047 | - } |
|
| 1048 | - |
|
| 1049 | - if ($subFiles) { |
|
| 1050 | - return $this->getSharesInDir($node); |
|
| 1051 | - } |
|
| 1052 | - |
|
| 1053 | - $shares = $this->getSharesFromNode($viewer, $node, $reShares); |
|
| 1054 | - |
|
| 1055 | - $known = $formatted = $miniFormatted = []; |
|
| 1056 | - $resharingRight = false; |
|
| 1057 | - foreach ($shares as $share) { |
|
| 1058 | - try { |
|
| 1059 | - $share->getNode(); |
|
| 1060 | - } catch (NotFoundException $e) { |
|
| 1061 | - /* |
|
| 74 | + private ?Node $lockedNode = null; |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * Share20OCS constructor. |
|
| 78 | + */ |
|
| 79 | + public function __construct( |
|
| 80 | + string $appName, |
|
| 81 | + IRequest $request, |
|
| 82 | + private IManager $shareManager, |
|
| 83 | + private IGroupManager $groupManager, |
|
| 84 | + private IUserManager $userManager, |
|
| 85 | + private IRootFolder $rootFolder, |
|
| 86 | + private IURLGenerator $urlGenerator, |
|
| 87 | + private IL10N $l, |
|
| 88 | + private IConfig $config, |
|
| 89 | + private IAppManager $appManager, |
|
| 90 | + private ContainerInterface $serverContainer, |
|
| 91 | + private IUserStatusManager $userStatusManager, |
|
| 92 | + private IPreview $previewManager, |
|
| 93 | + private IDateTimeZone $dateTimeZone, |
|
| 94 | + private LoggerInterface $logger, |
|
| 95 | + private IProviderFactory $factory, |
|
| 96 | + private IMailer $mailer, |
|
| 97 | + private ?string $userId = null, |
|
| 98 | + ) { |
|
| 99 | + parent::__construct($appName, $request); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Convert an IShare to an array for OCS output |
|
| 104 | + * |
|
| 105 | + * @param IShare $share |
|
| 106 | + * @param Node|null $recipientNode |
|
| 107 | + * @return Files_SharingShare |
|
| 108 | + * @throws NotFoundException In case the node can't be resolved. |
|
| 109 | + * |
|
| 110 | + * @suppress PhanUndeclaredClassMethod |
|
| 111 | + */ |
|
| 112 | + protected function formatShare(IShare $share, ?Node $recipientNode = null): array { |
|
| 113 | + $sharedBy = $this->userManager->get($share->getSharedBy()); |
|
| 114 | + $shareOwner = $this->userManager->get($share->getShareOwner()); |
|
| 115 | + |
|
| 116 | + $isOwnShare = false; |
|
| 117 | + if ($shareOwner !== null) { |
|
| 118 | + $isOwnShare = $shareOwner->getUID() === $this->userId; |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $result = [ |
|
| 122 | + 'id' => $share->getId(), |
|
| 123 | + 'share_type' => $share->getShareType(), |
|
| 124 | + 'uid_owner' => $share->getSharedBy(), |
|
| 125 | + 'displayname_owner' => $sharedBy !== null ? $sharedBy->getDisplayName() : $share->getSharedBy(), |
|
| 126 | + // recipient permissions |
|
| 127 | + 'permissions' => $share->getPermissions(), |
|
| 128 | + // current user permissions on this share |
|
| 129 | + 'can_edit' => $this->canEditShare($share), |
|
| 130 | + 'can_delete' => $this->canDeleteShare($share), |
|
| 131 | + 'stime' => $share->getShareTime()->getTimestamp(), |
|
| 132 | + 'parent' => null, |
|
| 133 | + 'expiration' => null, |
|
| 134 | + 'token' => null, |
|
| 135 | + 'uid_file_owner' => $share->getShareOwner(), |
|
| 136 | + 'note' => $share->getNote(), |
|
| 137 | + 'label' => $share->getLabel(), |
|
| 138 | + 'displayname_file_owner' => $shareOwner !== null ? $shareOwner->getDisplayName() : $share->getShareOwner(), |
|
| 139 | + ]; |
|
| 140 | + |
|
| 141 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 142 | + if ($recipientNode) { |
|
| 143 | + $node = $recipientNode; |
|
| 144 | + } else { |
|
| 145 | + $node = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 146 | + if (!$node) { |
|
| 147 | + // fallback to guessing the path |
|
| 148 | + $node = $userFolder->get($share->getTarget()); |
|
| 149 | + if ($node === null || $share->getTarget() === '') { |
|
| 150 | + throw new NotFoundException(); |
|
| 151 | + } |
|
| 152 | + } |
|
| 153 | + } |
|
| 154 | + |
|
| 155 | + $result['path'] = $userFolder->getRelativePath($node->getPath()); |
|
| 156 | + if ($node instanceof Folder) { |
|
| 157 | + $result['item_type'] = 'folder'; |
|
| 158 | + } else { |
|
| 159 | + $result['item_type'] = 'file'; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + // Get the original node permission if the share owner is the current user |
|
| 163 | + if ($isOwnShare) { |
|
| 164 | + $result['item_permissions'] = $node->getPermissions(); |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + // If we're on the recipient side, the node permissions |
|
| 168 | + // are bound to the share permissions. So we need to |
|
| 169 | + // adjust the permissions to the share permissions if necessary. |
|
| 170 | + if (!$isOwnShare) { |
|
| 171 | + $result['item_permissions'] = $share->getPermissions(); |
|
| 172 | + |
|
| 173 | + // For some reason, single files share are forbidden to have the delete permission |
|
| 174 | + // since we have custom methods to check those, let's adjust straight away. |
|
| 175 | + // DAV permissions does not have that issue though. |
|
| 176 | + if ($this->canDeleteShare($share) || $this->canDeleteShareFromSelf($share)) { |
|
| 177 | + $result['item_permissions'] |= Constants::PERMISSION_DELETE; |
|
| 178 | + } |
|
| 179 | + if ($this->canEditShare($share)) { |
|
| 180 | + $result['item_permissions'] |= Constants::PERMISSION_UPDATE; |
|
| 181 | + } |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + // See MOUNT_ROOT_PROPERTYNAME dav property |
|
| 185 | + $result['is-mount-root'] = $node->getInternalPath() === ''; |
|
| 186 | + $result['mount-type'] = $node->getMountPoint()->getMountType(); |
|
| 187 | + |
|
| 188 | + $result['mimetype'] = $node->getMimetype(); |
|
| 189 | + $result['has_preview'] = $this->previewManager->isAvailable($node); |
|
| 190 | + $result['storage_id'] = $node->getStorage()->getId(); |
|
| 191 | + $result['storage'] = $node->getStorage()->getCache()->getNumericStorageId(); |
|
| 192 | + $result['item_source'] = $node->getId(); |
|
| 193 | + $result['file_source'] = $node->getId(); |
|
| 194 | + $result['file_parent'] = $node->getParent()->getId(); |
|
| 195 | + $result['file_target'] = $share->getTarget(); |
|
| 196 | + $result['item_size'] = $node->getSize(); |
|
| 197 | + $result['item_mtime'] = $node->getMTime(); |
|
| 198 | + |
|
| 199 | + $expiration = $share->getExpirationDate(); |
|
| 200 | + if ($expiration !== null) { |
|
| 201 | + $expiration->setTimezone($this->dateTimeZone->getTimeZone()); |
|
| 202 | + $result['expiration'] = $expiration->format('Y-m-d 00:00:00'); |
|
| 203 | + } |
|
| 204 | + |
|
| 205 | + if ($share->getShareType() === IShare::TYPE_USER) { |
|
| 206 | + $sharedWith = $this->userManager->get($share->getSharedWith()); |
|
| 207 | + $result['share_with'] = $share->getSharedWith(); |
|
| 208 | + $result['share_with_displayname'] = $sharedWith !== null ? $sharedWith->getDisplayName() : $share->getSharedWith(); |
|
| 209 | + $result['share_with_displayname_unique'] = $sharedWith !== null ? ( |
|
| 210 | + !empty($sharedWith->getSystemEMailAddress()) ? $sharedWith->getSystemEMailAddress() : $sharedWith->getUID() |
|
| 211 | + ) : $share->getSharedWith(); |
|
| 212 | + |
|
| 213 | + $userStatuses = $this->userStatusManager->getUserStatuses([$share->getSharedWith()]); |
|
| 214 | + $userStatus = array_shift($userStatuses); |
|
| 215 | + if ($userStatus) { |
|
| 216 | + $result['status'] = [ |
|
| 217 | + 'status' => $userStatus->getStatus(), |
|
| 218 | + 'message' => $userStatus->getMessage(), |
|
| 219 | + 'icon' => $userStatus->getIcon(), |
|
| 220 | + 'clearAt' => $userStatus->getClearAt() |
|
| 221 | + ? (int)$userStatus->getClearAt()->format('U') |
|
| 222 | + : null, |
|
| 223 | + ]; |
|
| 224 | + } |
|
| 225 | + } elseif ($share->getShareType() === IShare::TYPE_GROUP) { |
|
| 226 | + $group = $this->groupManager->get($share->getSharedWith()); |
|
| 227 | + $result['share_with'] = $share->getSharedWith(); |
|
| 228 | + $result['share_with_displayname'] = $group !== null ? $group->getDisplayName() : $share->getSharedWith(); |
|
| 229 | + } elseif ($share->getShareType() === IShare::TYPE_LINK) { |
|
| 230 | + |
|
| 231 | + // "share_with" and "share_with_displayname" for passwords of link |
|
| 232 | + // shares was deprecated in Nextcloud 15, use "password" instead. |
|
| 233 | + $result['share_with'] = $share->getPassword(); |
|
| 234 | + $result['share_with_displayname'] = '(' . $this->l->t('Shared link') . ')'; |
|
| 235 | + |
|
| 236 | + $result['password'] = $share->getPassword(); |
|
| 237 | + |
|
| 238 | + $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 239 | + |
|
| 240 | + $result['token'] = $share->getToken(); |
|
| 241 | + $result['url'] = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $share->getToken()]); |
|
| 242 | + } elseif ($share->getShareType() === IShare::TYPE_REMOTE) { |
|
| 243 | + $result['share_with'] = $share->getSharedWith(); |
|
| 244 | + $result['share_with_displayname'] = $this->getCachedFederatedDisplayName($share->getSharedWith()); |
|
| 245 | + $result['token'] = $share->getToken(); |
|
| 246 | + } elseif ($share->getShareType() === IShare::TYPE_REMOTE_GROUP) { |
|
| 247 | + $result['share_with'] = $share->getSharedWith(); |
|
| 248 | + $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'CLOUD'); |
|
| 249 | + $result['token'] = $share->getToken(); |
|
| 250 | + } elseif ($share->getShareType() === IShare::TYPE_EMAIL) { |
|
| 251 | + $result['share_with'] = $share->getSharedWith(); |
|
| 252 | + $result['password'] = $share->getPassword(); |
|
| 253 | + $result['password_expiration_time'] = $share->getPasswordExpirationTime() !== null ? $share->getPasswordExpirationTime()->format(\DateTime::ATOM) : null; |
|
| 254 | + $result['send_password_by_talk'] = $share->getSendPasswordByTalk(); |
|
| 255 | + $result['share_with_displayname'] = $this->getDisplayNameFromAddressBook($share->getSharedWith(), 'EMAIL'); |
|
| 256 | + $result['token'] = $share->getToken(); |
|
| 257 | + } elseif ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
| 258 | + // getSharedWith() returns either "name (type, owner)" or |
|
| 259 | + // "name (type, owner) [id]", depending on the Teams app version. |
|
| 260 | + $hasCircleId = (substr($share->getSharedWith(), -1) === ']'); |
|
| 261 | + |
|
| 262 | + $result['share_with_displayname'] = $share->getSharedWithDisplayName(); |
|
| 263 | + if (empty($result['share_with_displayname'])) { |
|
| 264 | + $displayNameLength = ($hasCircleId ? strrpos($share->getSharedWith(), ' ') : strlen($share->getSharedWith())); |
|
| 265 | + $result['share_with_displayname'] = substr($share->getSharedWith(), 0, $displayNameLength); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $result['share_with_avatar'] = $share->getSharedWithAvatar(); |
|
| 269 | + |
|
| 270 | + $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0); |
|
| 271 | + $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' ')); |
|
| 272 | + if ($shareWithLength === false) { |
|
| 273 | + $result['share_with'] = substr($share->getSharedWith(), $shareWithStart); |
|
| 274 | + } else { |
|
| 275 | + $result['share_with'] = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 276 | + } |
|
| 277 | + } elseif ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 278 | + $result['share_with'] = $share->getSharedWith(); |
|
| 279 | + $result['share_with_displayname'] = ''; |
|
| 280 | + |
|
| 281 | + try { |
|
| 282 | + /** @var array{share_with_displayname: string, share_with_link: string, share_with?: string, token?: string} $roomShare */ |
|
| 283 | + $roomShare = $this->getRoomShareHelper()->formatShare($share); |
|
| 284 | + $result = array_merge($result, $roomShare); |
|
| 285 | + } catch (ContainerExceptionInterface $e) { |
|
| 286 | + } |
|
| 287 | + } elseif ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 288 | + $result['share_with'] = $share->getSharedWith(); |
|
| 289 | + $result['share_with_displayname'] = ''; |
|
| 290 | + |
|
| 291 | + try { |
|
| 292 | + /** @var array{share_with: string, share_with_displayname: string, share_with_link: string} $deckShare */ |
|
| 293 | + $deckShare = $this->getDeckShareHelper()->formatShare($share); |
|
| 294 | + $result = array_merge($result, $deckShare); |
|
| 295 | + } catch (ContainerExceptionInterface $e) { |
|
| 296 | + } |
|
| 297 | + } elseif ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 298 | + $result['share_with'] = $share->getSharedWith(); |
|
| 299 | + $result['share_with_displayname'] = ''; |
|
| 300 | + |
|
| 301 | + try { |
|
| 302 | + /** @var array{share_with: string, share_with_displayname: string, token: string} $scienceMeshShare */ |
|
| 303 | + $scienceMeshShare = $this->getSciencemeshShareHelper()->formatShare($share); |
|
| 304 | + $result = array_merge($result, $scienceMeshShare); |
|
| 305 | + } catch (ContainerExceptionInterface $e) { |
|
| 306 | + } |
|
| 307 | + } |
|
| 308 | + |
|
| 309 | + |
|
| 310 | + $result['mail_send'] = $share->getMailSend() ? 1 : 0; |
|
| 311 | + $result['hide_download'] = $share->getHideDownload() ? 1 : 0; |
|
| 312 | + |
|
| 313 | + $result['attributes'] = null; |
|
| 314 | + if ($attributes = $share->getAttributes()) { |
|
| 315 | + $result['attributes'] = (string)\json_encode($attributes->toArray()); |
|
| 316 | + } |
|
| 317 | + |
|
| 318 | + return $result; |
|
| 319 | + } |
|
| 320 | + |
|
| 321 | + /** |
|
| 322 | + * Check if one of the users address books knows the exact property, if |
|
| 323 | + * not we return the full name. |
|
| 324 | + * |
|
| 325 | + * @param string $query |
|
| 326 | + * @param string $property |
|
| 327 | + * @return string |
|
| 328 | + */ |
|
| 329 | + private function getDisplayNameFromAddressBook(string $query, string $property): string { |
|
| 330 | + // FIXME: If we inject the contacts manager it gets initialized before any address books are registered |
|
| 331 | + try { |
|
| 332 | + $result = Server::get(\OCP\Contacts\IManager::class)->search($query, [$property], [ |
|
| 333 | + 'limit' => 1, |
|
| 334 | + 'enumeration' => false, |
|
| 335 | + 'strict_search' => true, |
|
| 336 | + ]); |
|
| 337 | + } catch (Exception $e) { |
|
| 338 | + $this->logger->error( |
|
| 339 | + $e->getMessage(), |
|
| 340 | + ['exception' => $e] |
|
| 341 | + ); |
|
| 342 | + return $query; |
|
| 343 | + } |
|
| 344 | + |
|
| 345 | + foreach ($result as $r) { |
|
| 346 | + foreach ($r[$property] as $value) { |
|
| 347 | + if ($value === $query && $r['FN']) { |
|
| 348 | + return $r['FN']; |
|
| 349 | + } |
|
| 350 | + } |
|
| 351 | + } |
|
| 352 | + |
|
| 353 | + return $query; |
|
| 354 | + } |
|
| 355 | + |
|
| 356 | + |
|
| 357 | + /** |
|
| 358 | + * @param list<Files_SharingShare> $shares |
|
| 359 | + * @param array<string, string>|null $updatedDisplayName |
|
| 360 | + * |
|
| 361 | + * @return list<Files_SharingShare> |
|
| 362 | + */ |
|
| 363 | + private function fixMissingDisplayName(array $shares, ?array $updatedDisplayName = null): array { |
|
| 364 | + $userIds = $updated = []; |
|
| 365 | + foreach ($shares as $share) { |
|
| 366 | + // share is federated and share have no display name yet |
|
| 367 | + if ($share['share_type'] === IShare::TYPE_REMOTE |
|
| 368 | + && ($share['share_with'] ?? '') !== '' |
|
| 369 | + && ($share['share_with_displayname'] ?? '') === '') { |
|
| 370 | + $userIds[] = $userId = $share['share_with']; |
|
| 371 | + |
|
| 372 | + if ($updatedDisplayName !== null && array_key_exists($userId, $updatedDisplayName)) { |
|
| 373 | + $share['share_with_displayname'] = $updatedDisplayName[$userId]; |
|
| 374 | + } |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + // prepping userIds with displayName to be updated |
|
| 378 | + $updated[] = $share; |
|
| 379 | + } |
|
| 380 | + |
|
| 381 | + // if $updatedDisplayName is not null, it means we should have already fixed displayNames of the shares |
|
| 382 | + if ($updatedDisplayName !== null) { |
|
| 383 | + return $updated; |
|
| 384 | + } |
|
| 385 | + |
|
| 386 | + // get displayName for the generated list of userId with no displayName |
|
| 387 | + $displayNames = $this->retrieveFederatedDisplayName($userIds); |
|
| 388 | + |
|
| 389 | + // if no displayName are updated, we exit |
|
| 390 | + if (empty($displayNames)) { |
|
| 391 | + return $updated; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + // let's fix missing display name and returns all shares |
|
| 395 | + return $this->fixMissingDisplayName($shares, $displayNames); |
|
| 396 | + } |
|
| 397 | + |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * get displayName of a list of userIds from the lookup-server; through the globalsiteselector app. |
|
| 401 | + * returns an array with userIds as keys and displayName as values. |
|
| 402 | + * |
|
| 403 | + * @param array $userIds |
|
| 404 | + * @param bool $cacheOnly - do not reach LUS, get data from cache. |
|
| 405 | + * |
|
| 406 | + * @return array |
|
| 407 | + * @throws ContainerExceptionInterface |
|
| 408 | + */ |
|
| 409 | + private function retrieveFederatedDisplayName(array $userIds, bool $cacheOnly = false): array { |
|
| 410 | + // check if gss is enabled and available |
|
| 411 | + if (count($userIds) === 0 |
|
| 412 | + || !$this->appManager->isEnabledForAnyone('globalsiteselector') |
|
| 413 | + || !class_exists('\OCA\GlobalSiteSelector\Service\SlaveService')) { |
|
| 414 | + return []; |
|
| 415 | + } |
|
| 416 | + |
|
| 417 | + try { |
|
| 418 | + $slaveService = Server::get(SlaveService::class); |
|
| 419 | + } catch (\Throwable $e) { |
|
| 420 | + $this->logger->error( |
|
| 421 | + $e->getMessage(), |
|
| 422 | + ['exception' => $e] |
|
| 423 | + ); |
|
| 424 | + return []; |
|
| 425 | + } |
|
| 426 | + |
|
| 427 | + return $slaveService->getUsersDisplayName($userIds, $cacheOnly); |
|
| 428 | + } |
|
| 429 | + |
|
| 430 | + |
|
| 431 | + /** |
|
| 432 | + * retrieve displayName from cache if available (should be used on federated shares) |
|
| 433 | + * if not available in cache/lus, try for get from address-book, else returns empty string. |
|
| 434 | + * |
|
| 435 | + * @param string $userId |
|
| 436 | + * @param bool $cacheOnly if true will not reach the lus but will only get data from cache |
|
| 437 | + * |
|
| 438 | + * @return string |
|
| 439 | + */ |
|
| 440 | + private function getCachedFederatedDisplayName(string $userId, bool $cacheOnly = true): string { |
|
| 441 | + $details = $this->retrieveFederatedDisplayName([$userId], $cacheOnly); |
|
| 442 | + if (array_key_exists($userId, $details)) { |
|
| 443 | + return $details[$userId]; |
|
| 444 | + } |
|
| 445 | + |
|
| 446 | + $displayName = $this->getDisplayNameFromAddressBook($userId, 'CLOUD'); |
|
| 447 | + return ($displayName === $userId) ? '' : $displayName; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + |
|
| 451 | + |
|
| 452 | + /** |
|
| 453 | + * Get a specific share by id |
|
| 454 | + * |
|
| 455 | + * @param string $id ID of the share |
|
| 456 | + * @param bool $include_tags Include tags in the share |
|
| 457 | + * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 458 | + * @throws OCSNotFoundException Share not found |
|
| 459 | + * |
|
| 460 | + * 200: Share returned |
|
| 461 | + */ |
|
| 462 | + #[NoAdminRequired] |
|
| 463 | + public function getShare(string $id, bool $include_tags = false): DataResponse { |
|
| 464 | + try { |
|
| 465 | + $share = $this->getShareById($id); |
|
| 466 | + } catch (ShareNotFound $e) { |
|
| 467 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + try { |
|
| 471 | + if ($this->canAccessShare($share)) { |
|
| 472 | + $share = $this->formatShare($share); |
|
| 473 | + |
|
| 474 | + if ($include_tags) { |
|
| 475 | + $share = Helper::populateTags([$share], Server::get(ITagManager::class)); |
|
| 476 | + } else { |
|
| 477 | + $share = [$share]; |
|
| 478 | + } |
|
| 479 | + |
|
| 480 | + return new DataResponse($share); |
|
| 481 | + } |
|
| 482 | + } catch (NotFoundException $e) { |
|
| 483 | + // Fall through |
|
| 484 | + } |
|
| 485 | + |
|
| 486 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 487 | + } |
|
| 488 | + |
|
| 489 | + /** |
|
| 490 | + * Delete a share |
|
| 491 | + * |
|
| 492 | + * @param string $id ID of the share |
|
| 493 | + * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 494 | + * @throws OCSNotFoundException Share not found |
|
| 495 | + * @throws OCSForbiddenException Missing permissions to delete the share |
|
| 496 | + * |
|
| 497 | + * 200: Share deleted successfully |
|
| 498 | + */ |
|
| 499 | + #[NoAdminRequired] |
|
| 500 | + public function deleteShare(string $id): DataResponse { |
|
| 501 | + try { |
|
| 502 | + $share = $this->getShareById($id); |
|
| 503 | + } catch (ShareNotFound $e) { |
|
| 504 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 505 | + } |
|
| 506 | + |
|
| 507 | + try { |
|
| 508 | + $this->lock($share->getNode()); |
|
| 509 | + } catch (LockedException $e) { |
|
| 510 | + throw new OCSNotFoundException($this->l->t('Could not delete share')); |
|
| 511 | + } |
|
| 512 | + |
|
| 513 | + if (!$this->canAccessShare($share)) { |
|
| 514 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 515 | + } |
|
| 516 | + |
|
| 517 | + // if it's a group share or a room share |
|
| 518 | + // we don't delete the share, but only the |
|
| 519 | + // mount point. Allowing it to be restored |
|
| 520 | + // from the deleted shares |
|
| 521 | + if ($this->canDeleteShareFromSelf($share)) { |
|
| 522 | + $this->shareManager->deleteFromSelf($share, $this->userId); |
|
| 523 | + } else { |
|
| 524 | + if (!$this->canDeleteShare($share)) { |
|
| 525 | + throw new OCSForbiddenException($this->l->t('Could not delete share')); |
|
| 526 | + } |
|
| 527 | + |
|
| 528 | + $this->shareManager->deleteShare($share); |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + return new DataResponse(); |
|
| 532 | + } |
|
| 533 | + |
|
| 534 | + /** |
|
| 535 | + * Create a share |
|
| 536 | + * |
|
| 537 | + * @param string|null $path Path of the share |
|
| 538 | + * @param int|null $permissions Permissions for the share |
|
| 539 | + * @param int $shareType Type of the share |
|
| 540 | + * @param ?string $shareWith The entity this should be shared with |
|
| 541 | + * @param 'true'|'false'|null $publicUpload If public uploading is allowed (deprecated) |
|
| 542 | + * @param string $password Password for the share |
|
| 543 | + * @param string|null $sendPasswordByTalk Send the password for the share over Talk |
|
| 544 | + * @param ?string $expireDate The expiry date of the share in the user's timezone at 00:00. |
|
| 545 | + * If $expireDate is not supplied or set to `null`, the system default will be used. |
|
| 546 | + * @param string $note Note for the share |
|
| 547 | + * @param string $label Label for the share (only used in link and email) |
|
| 548 | + * @param string|null $attributes Additional attributes for the share |
|
| 549 | + * @param 'false'|'true'|null $sendMail Send a mail to the recipient |
|
| 550 | + * |
|
| 551 | + * @return DataResponse<Http::STATUS_OK, Files_SharingShare, array{}> |
|
| 552 | + * @throws OCSBadRequestException Unknown share type |
|
| 553 | + * @throws OCSException |
|
| 554 | + * @throws OCSForbiddenException Creating the share is not allowed |
|
| 555 | + * @throws OCSNotFoundException Creating the share failed |
|
| 556 | + * @suppress PhanUndeclaredClassMethod |
|
| 557 | + * |
|
| 558 | + * 200: Share created |
|
| 559 | + */ |
|
| 560 | + #[NoAdminRequired] |
|
| 561 | + #[UserRateLimit(limit: 20, period: 600)] |
|
| 562 | + public function createShare( |
|
| 563 | + ?string $path = null, |
|
| 564 | + ?int $permissions = null, |
|
| 565 | + int $shareType = -1, |
|
| 566 | + ?string $shareWith = null, |
|
| 567 | + ?string $publicUpload = null, |
|
| 568 | + string $password = '', |
|
| 569 | + ?string $sendPasswordByTalk = null, |
|
| 570 | + ?string $expireDate = null, |
|
| 571 | + string $note = '', |
|
| 572 | + string $label = '', |
|
| 573 | + ?string $attributes = null, |
|
| 574 | + ?string $sendMail = null, |
|
| 575 | + ): DataResponse { |
|
| 576 | + assert($this->userId !== null); |
|
| 577 | + |
|
| 578 | + $share = $this->shareManager->newShare(); |
|
| 579 | + $hasPublicUpload = $this->getLegacyPublicUpload($publicUpload); |
|
| 580 | + |
|
| 581 | + // Verify path |
|
| 582 | + if ($path === null) { |
|
| 583 | + throw new OCSNotFoundException($this->l->t('Please specify a file or folder path')); |
|
| 584 | + } |
|
| 585 | + |
|
| 586 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 587 | + try { |
|
| 588 | + /** @var \OC\Files\Node\Node $node */ |
|
| 589 | + $node = $userFolder->get($path); |
|
| 590 | + } catch (NotFoundException $e) { |
|
| 591 | + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder does not exist')); |
|
| 592 | + } |
|
| 593 | + |
|
| 594 | + // a user can have access to a file through different paths, with differing permissions |
|
| 595 | + // combine all permissions to determine if the user can share this file |
|
| 596 | + $nodes = $userFolder->getById($node->getId()); |
|
| 597 | + foreach ($nodes as $nodeById) { |
|
| 598 | + /** @var \OC\Files\FileInfo $fileInfo */ |
|
| 599 | + $fileInfo = $node->getFileInfo(); |
|
| 600 | + $fileInfo['permissions'] |= $nodeById->getPermissions(); |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + $share->setNode($node); |
|
| 604 | + |
|
| 605 | + try { |
|
| 606 | + $this->lock($share->getNode()); |
|
| 607 | + } catch (LockedException $e) { |
|
| 608 | + throw new OCSNotFoundException($this->l->t('Could not create share')); |
|
| 609 | + } |
|
| 610 | + |
|
| 611 | + // Set permissions |
|
| 612 | + if ($shareType === IShare::TYPE_LINK || $shareType === IShare::TYPE_EMAIL) { |
|
| 613 | + $permissions = $this->getLinkSharePermissions($permissions, $hasPublicUpload); |
|
| 614 | + $this->validateLinkSharePermissions($node, $permissions, $hasPublicUpload); |
|
| 615 | + } else { |
|
| 616 | + // Use default permissions only for non-link shares to keep legacy behavior |
|
| 617 | + if ($permissions === null) { |
|
| 618 | + $permissions = (int)$this->config->getAppValue('core', 'shareapi_default_permissions', (string)Constants::PERMISSION_ALL); |
|
| 619 | + } |
|
| 620 | + // Non-link shares always require read permissions (link shares could be file drop) |
|
| 621 | + $permissions |= Constants::PERMISSION_READ; |
|
| 622 | + } |
|
| 623 | + |
|
| 624 | + // For legacy reasons the API allows to pass PERMISSIONS_ALL even for single file shares (I look at you Talk) |
|
| 625 | + if ($node instanceof File) { |
|
| 626 | + // if this is a single file share we remove the DELETE and CREATE permissions |
|
| 627 | + $permissions = $permissions & ~(Constants::PERMISSION_DELETE | Constants::PERMISSION_CREATE); |
|
| 628 | + } |
|
| 629 | + |
|
| 630 | + /** |
|
| 631 | + * Hack for https://github.com/owncloud/core/issues/22587 |
|
| 632 | + * We check the permissions via webdav. But the permissions of the mount point |
|
| 633 | + * do not equal the share permissions. Here we fix that for federated mounts. |
|
| 634 | + */ |
|
| 635 | + if ($node->getStorage()->instanceOfStorage(Storage::class)) { |
|
| 636 | + $permissions &= ~($permissions & ~$node->getPermissions()); |
|
| 637 | + } |
|
| 638 | + |
|
| 639 | + if ($attributes !== null) { |
|
| 640 | + $share = $this->setShareAttributes($share, $attributes); |
|
| 641 | + } |
|
| 642 | + |
|
| 643 | + // Expire date checks |
|
| 644 | + // Normally, null means no expiration date but we still set the default for backwards compatibility |
|
| 645 | + // If the client sends an empty string, we set noExpirationDate to true |
|
| 646 | + if ($expireDate !== null) { |
|
| 647 | + if ($expireDate !== '') { |
|
| 648 | + try { |
|
| 649 | + $expireDateTime = $this->parseDate($expireDate); |
|
| 650 | + $share->setExpirationDate($expireDateTime); |
|
| 651 | + } catch (\Exception $e) { |
|
| 652 | + throw new OCSNotFoundException($e->getMessage(), $e); |
|
| 653 | + } |
|
| 654 | + } else { |
|
| 655 | + // Client sent empty string for expire date. |
|
| 656 | + // Set noExpirationDate to true so overwrite is prevented. |
|
| 657 | + $share->setNoExpirationDate(true); |
|
| 658 | + } |
|
| 659 | + } |
|
| 660 | + |
|
| 661 | + $share->setSharedBy($this->userId); |
|
| 662 | + |
|
| 663 | + // Handle mail send |
|
| 664 | + if (is_null($sendMail)) { |
|
| 665 | + $allowSendMail = $this->config->getSystemValueBool('sharing.enable_share_mail', true); |
|
| 666 | + if ($allowSendMail !== true || $shareType === IShare::TYPE_EMAIL) { |
|
| 667 | + // Define a default behavior when sendMail is not provided |
|
| 668 | + // For email shares with a valid recipient, the default is to send the mail |
|
| 669 | + // For all other share types, the default is to not send the mail |
|
| 670 | + $allowSendMail = ($shareType === IShare::TYPE_EMAIL && $shareWith !== null && $shareWith !== ''); |
|
| 671 | + } |
|
| 672 | + $share->setMailSend($allowSendMail); |
|
| 673 | + } else { |
|
| 674 | + $share->setMailSend($sendMail === 'true'); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + if ($shareType === IShare::TYPE_USER) { |
|
| 678 | + // Valid user is required to share |
|
| 679 | + if ($shareWith === null || !$this->userManager->userExists($shareWith)) { |
|
| 680 | + throw new OCSNotFoundException($this->l->t('Please specify a valid account to share with')); |
|
| 681 | + } |
|
| 682 | + $share->setSharedWith($shareWith); |
|
| 683 | + $share->setPermissions($permissions); |
|
| 684 | + } elseif ($shareType === IShare::TYPE_GROUP) { |
|
| 685 | + if (!$this->shareManager->allowGroupSharing()) { |
|
| 686 | + throw new OCSNotFoundException($this->l->t('Group sharing is disabled by the administrator')); |
|
| 687 | + } |
|
| 688 | + |
|
| 689 | + // Valid group is required to share |
|
| 690 | + if ($shareWith === null || !$this->groupManager->groupExists($shareWith)) { |
|
| 691 | + throw new OCSNotFoundException($this->l->t('Please specify a valid group')); |
|
| 692 | + } |
|
| 693 | + $share->setSharedWith($shareWith); |
|
| 694 | + $share->setPermissions($permissions); |
|
| 695 | + } elseif ($shareType === IShare::TYPE_LINK |
|
| 696 | + || $shareType === IShare::TYPE_EMAIL) { |
|
| 697 | + |
|
| 698 | + // Can we even share links? |
|
| 699 | + if (!$this->shareManager->shareApiAllowLinks()) { |
|
| 700 | + throw new OCSNotFoundException($this->l->t('Public link sharing is disabled by the administrator')); |
|
| 701 | + } |
|
| 702 | + |
|
| 703 | + $this->validateLinkSharePermissions($node, $permissions, $hasPublicUpload); |
|
| 704 | + $share->setPermissions($permissions); |
|
| 705 | + |
|
| 706 | + // Set password |
|
| 707 | + if ($password !== '') { |
|
| 708 | + $share->setPassword($password); |
|
| 709 | + } |
|
| 710 | + |
|
| 711 | + // Only share by mail have a recipient |
|
| 712 | + if (is_string($shareWith) && $shareType === IShare::TYPE_EMAIL) { |
|
| 713 | + // If sending a mail have been requested, validate the mail address |
|
| 714 | + if ($share->getMailSend() && !$this->mailer->validateMailAddress($shareWith)) { |
|
| 715 | + throw new OCSNotFoundException($this->l->t('Please specify a valid email address')); |
|
| 716 | + } |
|
| 717 | + $share->setSharedWith($shareWith); |
|
| 718 | + } |
|
| 719 | + |
|
| 720 | + // If we have a label, use it |
|
| 721 | + if ($label !== '') { |
|
| 722 | + if (strlen($label) > 255) { |
|
| 723 | + throw new OCSBadRequestException('Maximum label length is 255'); |
|
| 724 | + } |
|
| 725 | + $share->setLabel($label); |
|
| 726 | + } |
|
| 727 | + |
|
| 728 | + if ($sendPasswordByTalk === 'true') { |
|
| 729 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 730 | + throw new OCSForbiddenException($this->l->t('Sharing %s sending the password by Nextcloud Talk failed because Nextcloud Talk is not enabled', [$node->getPath()])); |
|
| 731 | + } |
|
| 732 | + |
|
| 733 | + $share->setSendPasswordByTalk(true); |
|
| 734 | + } |
|
| 735 | + } elseif ($shareType === IShare::TYPE_REMOTE) { |
|
| 736 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 737 | + throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$node->getPath(), $shareType])); |
|
| 738 | + } |
|
| 739 | + |
|
| 740 | + if ($shareWith === null) { |
|
| 741 | + throw new OCSNotFoundException($this->l->t('Please specify a valid federated account ID')); |
|
| 742 | + } |
|
| 743 | + |
|
| 744 | + $share->setSharedWith($shareWith); |
|
| 745 | + $share->setPermissions($permissions); |
|
| 746 | + $share->setSharedWithDisplayName($this->getCachedFederatedDisplayName($shareWith, false)); |
|
| 747 | + } elseif ($shareType === IShare::TYPE_REMOTE_GROUP) { |
|
| 748 | + if (!$this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 749 | + throw new OCSForbiddenException($this->l->t('Sharing %1$s failed because the back end does not allow shares from type %2$s', [$node->getPath(), $shareType])); |
|
| 750 | + } |
|
| 751 | + |
|
| 752 | + if ($shareWith === null) { |
|
| 753 | + throw new OCSNotFoundException($this->l->t('Please specify a valid federated group ID')); |
|
| 754 | + } |
|
| 755 | + |
|
| 756 | + $share->setSharedWith($shareWith); |
|
| 757 | + $share->setPermissions($permissions); |
|
| 758 | + } elseif ($shareType === IShare::TYPE_CIRCLE) { |
|
| 759 | + if (!Server::get(IAppManager::class)->isEnabledForUser('circles') || !class_exists('\OCA\Circles\ShareByCircleProvider')) { |
|
| 760 | + throw new OCSNotFoundException($this->l->t('You cannot share to a Team if the app is not enabled')); |
|
| 761 | + } |
|
| 762 | + |
|
| 763 | + $circle = Circles::detailsCircle($shareWith); |
|
| 764 | + |
|
| 765 | + // Valid team is required to share |
|
| 766 | + if ($circle === null) { |
|
| 767 | + throw new OCSNotFoundException($this->l->t('Please specify a valid team')); |
|
| 768 | + } |
|
| 769 | + $share->setSharedWith($shareWith); |
|
| 770 | + $share->setPermissions($permissions); |
|
| 771 | + } elseif ($shareType === IShare::TYPE_ROOM) { |
|
| 772 | + try { |
|
| 773 | + $this->getRoomShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 774 | + } catch (ContainerExceptionInterface $e) { |
|
| 775 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$node->getPath()])); |
|
| 776 | + } |
|
| 777 | + } elseif ($shareType === IShare::TYPE_DECK) { |
|
| 778 | + try { |
|
| 779 | + $this->getDeckShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 780 | + } catch (ContainerExceptionInterface $e) { |
|
| 781 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support room shares', [$node->getPath()])); |
|
| 782 | + } |
|
| 783 | + } elseif ($shareType === IShare::TYPE_SCIENCEMESH) { |
|
| 784 | + try { |
|
| 785 | + $this->getSciencemeshShareHelper()->createShare($share, $shareWith, $permissions, $expireDate ?? ''); |
|
| 786 | + } catch (ContainerExceptionInterface $e) { |
|
| 787 | + throw new OCSForbiddenException($this->l->t('Sharing %s failed because the back end does not support ScienceMesh shares', [$node->getPath()])); |
|
| 788 | + } |
|
| 789 | + } else { |
|
| 790 | + throw new OCSBadRequestException($this->l->t('Unknown share type')); |
|
| 791 | + } |
|
| 792 | + |
|
| 793 | + $share->setShareType($shareType); |
|
| 794 | + $this->checkInheritedAttributes($share); |
|
| 795 | + |
|
| 796 | + if ($note !== '') { |
|
| 797 | + $share->setNote($note); |
|
| 798 | + } |
|
| 799 | + |
|
| 800 | + try { |
|
| 801 | + $share = $this->shareManager->createShare($share); |
|
| 802 | + } catch (HintException $e) { |
|
| 803 | + $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 804 | + throw new OCSException($e->getHint(), $code); |
|
| 805 | + } catch (GenericShareException|\InvalidArgumentException $e) { |
|
| 806 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 807 | + throw new OCSForbiddenException($e->getMessage(), $e); |
|
| 808 | + } catch (\Exception $e) { |
|
| 809 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 810 | + throw new OCSForbiddenException('Failed to create share.', $e); |
|
| 811 | + } |
|
| 812 | + |
|
| 813 | + $output = $this->formatShare($share); |
|
| 814 | + |
|
| 815 | + return new DataResponse($output); |
|
| 816 | + } |
|
| 817 | + |
|
| 818 | + /** |
|
| 819 | + * @param null|Node $node |
|
| 820 | + * @param boolean $includeTags |
|
| 821 | + * |
|
| 822 | + * @return list<Files_SharingShare> |
|
| 823 | + */ |
|
| 824 | + private function getSharedWithMe($node, bool $includeTags): array { |
|
| 825 | + $userShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_USER, $node, -1, 0); |
|
| 826 | + $groupShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_GROUP, $node, -1, 0); |
|
| 827 | + $circleShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_CIRCLE, $node, -1, 0); |
|
| 828 | + $roomShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_ROOM, $node, -1, 0); |
|
| 829 | + $deckShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_DECK, $node, -1, 0); |
|
| 830 | + $sciencemeshShares = $this->shareManager->getSharedWith($this->userId, IShare::TYPE_SCIENCEMESH, $node, -1, 0); |
|
| 831 | + |
|
| 832 | + $shares = array_merge($userShares, $groupShares, $circleShares, $roomShares, $deckShares, $sciencemeshShares); |
|
| 833 | + |
|
| 834 | + $filteredShares = array_filter($shares, function (IShare $share) { |
|
| 835 | + return $share->getShareOwner() !== $this->userId; |
|
| 836 | + }); |
|
| 837 | + |
|
| 838 | + $formatted = []; |
|
| 839 | + foreach ($filteredShares as $share) { |
|
| 840 | + if ($this->canAccessShare($share)) { |
|
| 841 | + try { |
|
| 842 | + $formatted[] = $this->formatShare($share); |
|
| 843 | + } catch (NotFoundException $e) { |
|
| 844 | + // Ignore this share |
|
| 845 | + } |
|
| 846 | + } |
|
| 847 | + } |
|
| 848 | + |
|
| 849 | + if ($includeTags) { |
|
| 850 | + $formatted = Helper::populateTags($formatted, Server::get(ITagManager::class)); |
|
| 851 | + } |
|
| 852 | + |
|
| 853 | + return $formatted; |
|
| 854 | + } |
|
| 855 | + |
|
| 856 | + /** |
|
| 857 | + * @param Node $folder |
|
| 858 | + * |
|
| 859 | + * @return list<Files_SharingShare> |
|
| 860 | + * @throws OCSBadRequestException |
|
| 861 | + * @throws NotFoundException |
|
| 862 | + */ |
|
| 863 | + private function getSharesInDir(Node $folder): array { |
|
| 864 | + if (!($folder instanceof Folder)) { |
|
| 865 | + throw new OCSBadRequestException($this->l->t('Not a directory')); |
|
| 866 | + } |
|
| 867 | + |
|
| 868 | + $nodes = $folder->getDirectoryListing(); |
|
| 869 | + |
|
| 870 | + /** @var IShare[] $shares */ |
|
| 871 | + $shares = array_reduce($nodes, function ($carry, $node) { |
|
| 872 | + $carry = array_merge($carry, $this->getAllShares($node, true)); |
|
| 873 | + return $carry; |
|
| 874 | + }, []); |
|
| 875 | + |
|
| 876 | + // filter out duplicate shares |
|
| 877 | + $known = []; |
|
| 878 | + |
|
| 879 | + $formatted = $miniFormatted = []; |
|
| 880 | + $resharingRight = false; |
|
| 881 | + $known = []; |
|
| 882 | + foreach ($shares as $share) { |
|
| 883 | + if (in_array($share->getId(), $known) || $share->getSharedWith() === $this->userId) { |
|
| 884 | + continue; |
|
| 885 | + } |
|
| 886 | + |
|
| 887 | + try { |
|
| 888 | + $format = $this->formatShare($share); |
|
| 889 | + |
|
| 890 | + $known[] = $share->getId(); |
|
| 891 | + $formatted[] = $format; |
|
| 892 | + if ($share->getSharedBy() === $this->userId) { |
|
| 893 | + $miniFormatted[] = $format; |
|
| 894 | + } |
|
| 895 | + if (!$resharingRight && $this->shareProviderResharingRights($this->userId, $share, $folder)) { |
|
| 896 | + $resharingRight = true; |
|
| 897 | + } |
|
| 898 | + } catch (\Exception $e) { |
|
| 899 | + //Ignore this share |
|
| 900 | + } |
|
| 901 | + } |
|
| 902 | + |
|
| 903 | + if (!$resharingRight) { |
|
| 904 | + $formatted = $miniFormatted; |
|
| 905 | + } |
|
| 906 | + |
|
| 907 | + return $formatted; |
|
| 908 | + } |
|
| 909 | + |
|
| 910 | + /** |
|
| 911 | + * Get shares of the current user |
|
| 912 | + * |
|
| 913 | + * @param string $shared_with_me Only get shares with the current user |
|
| 914 | + * @param string $reshares Only get shares by the current user and reshares |
|
| 915 | + * @param string $subfiles Only get all shares in a folder |
|
| 916 | + * @param string $path Get shares for a specific path |
|
| 917 | + * @param string $include_tags Include tags in the share |
|
| 918 | + * |
|
| 919 | + * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 920 | + * @throws OCSNotFoundException The folder was not found or is inaccessible |
|
| 921 | + * |
|
| 922 | + * 200: Shares returned |
|
| 923 | + */ |
|
| 924 | + #[NoAdminRequired] |
|
| 925 | + public function getShares( |
|
| 926 | + string $shared_with_me = 'false', |
|
| 927 | + string $reshares = 'false', |
|
| 928 | + string $subfiles = 'false', |
|
| 929 | + string $path = '', |
|
| 930 | + string $include_tags = 'false', |
|
| 931 | + ): DataResponse { |
|
| 932 | + $node = null; |
|
| 933 | + if ($path !== '') { |
|
| 934 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 935 | + try { |
|
| 936 | + $node = $userFolder->get($path); |
|
| 937 | + $this->lock($node); |
|
| 938 | + } catch (NotFoundException $e) { |
|
| 939 | + throw new OCSNotFoundException( |
|
| 940 | + $this->l->t('Wrong path, file/folder does not exist') |
|
| 941 | + ); |
|
| 942 | + } catch (LockedException $e) { |
|
| 943 | + throw new OCSNotFoundException($this->l->t('Could not lock node')); |
|
| 944 | + } |
|
| 945 | + } |
|
| 946 | + |
|
| 947 | + $shares = $this->getFormattedShares( |
|
| 948 | + $this->userId, |
|
| 949 | + $node, |
|
| 950 | + ($shared_with_me === 'true'), |
|
| 951 | + ($reshares === 'true'), |
|
| 952 | + ($subfiles === 'true'), |
|
| 953 | + ($include_tags === 'true') |
|
| 954 | + ); |
|
| 955 | + |
|
| 956 | + return new DataResponse($shares); |
|
| 957 | + } |
|
| 958 | + |
|
| 959 | + private function getLinkSharePermissions(?int $permissions, ?bool $legacyPublicUpload): int { |
|
| 960 | + $permissions = $permissions ?? Constants::PERMISSION_READ; |
|
| 961 | + |
|
| 962 | + // Legacy option handling |
|
| 963 | + if ($legacyPublicUpload !== null) { |
|
| 964 | + $permissions = $legacyPublicUpload |
|
| 965 | + ? (Constants::PERMISSION_READ | Constants::PERMISSION_CREATE | Constants::PERMISSION_UPDATE | Constants::PERMISSION_DELETE) |
|
| 966 | + : Constants::PERMISSION_READ; |
|
| 967 | + } |
|
| 968 | + |
|
| 969 | + // TODO: It might make sense to have a dedicated setting to allow/deny converting link shares into federated ones |
|
| 970 | + if ($this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 971 | + && $this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 972 | + $permissions |= Constants::PERMISSION_SHARE; |
|
| 973 | + } |
|
| 974 | + |
|
| 975 | + return $permissions; |
|
| 976 | + } |
|
| 977 | + |
|
| 978 | + /** |
|
| 979 | + * Helper to check for legacy "publicUpload" handling. |
|
| 980 | + * If the value is set to `true` or `false` then true or false are returned. |
|
| 981 | + * Otherwise null is returned to indicate that the option was not (or wrong) set. |
|
| 982 | + * |
|
| 983 | + * @param null|string $legacyPublicUpload The value of `publicUpload` |
|
| 984 | + */ |
|
| 985 | + private function getLegacyPublicUpload(?string $legacyPublicUpload): ?bool { |
|
| 986 | + if ($legacyPublicUpload === 'true') { |
|
| 987 | + return true; |
|
| 988 | + } elseif ($legacyPublicUpload === 'false') { |
|
| 989 | + return false; |
|
| 990 | + } |
|
| 991 | + // Not set at all |
|
| 992 | + return null; |
|
| 993 | + } |
|
| 994 | + |
|
| 995 | + /** |
|
| 996 | + * For link and email shares validate that only allowed combinations are set. |
|
| 997 | + * |
|
| 998 | + * @throw OCSBadRequestException If permission combination is invalid. |
|
| 999 | + * @throw OCSForbiddenException If public upload was forbidden by the administrator. |
|
| 1000 | + */ |
|
| 1001 | + private function validateLinkSharePermissions(Node $node, int $permissions, ?bool $legacyPublicUpload): void { |
|
| 1002 | + if ($legacyPublicUpload && ($node instanceof File)) { |
|
| 1003 | + throw new OCSBadRequestException($this->l->t('Public upload is only possible for publicly shared folders')); |
|
| 1004 | + } |
|
| 1005 | + |
|
| 1006 | + // We need at least READ or CREATE (file drop) |
|
| 1007 | + if (!$this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 1008 | + && !$this->hasPermission($permissions, Constants::PERMISSION_CREATE)) { |
|
| 1009 | + throw new OCSBadRequestException($this->l->t('Share must at least have READ or CREATE permissions')); |
|
| 1010 | + } |
|
| 1011 | + |
|
| 1012 | + // UPDATE and DELETE require a READ permission |
|
| 1013 | + if (!$this->hasPermission($permissions, Constants::PERMISSION_READ) |
|
| 1014 | + && ($this->hasPermission($permissions, Constants::PERMISSION_UPDATE) || $this->hasPermission($permissions, Constants::PERMISSION_DELETE))) { |
|
| 1015 | + throw new OCSBadRequestException($this->l->t('Share must have READ permission if UPDATE or DELETE permission is set')); |
|
| 1016 | + } |
|
| 1017 | + |
|
| 1018 | + // Check if public uploading was disabled |
|
| 1019 | + if ($this->hasPermission($permissions, Constants::PERMISSION_CREATE) |
|
| 1020 | + && !$this->shareManager->shareApiLinkAllowPublicUpload()) { |
|
| 1021 | + throw new OCSForbiddenException($this->l->t('Public upload disabled by the administrator')); |
|
| 1022 | + } |
|
| 1023 | + } |
|
| 1024 | + |
|
| 1025 | + /** |
|
| 1026 | + * @param string $viewer |
|
| 1027 | + * @param Node $node |
|
| 1028 | + * @param bool $sharedWithMe |
|
| 1029 | + * @param bool $reShares |
|
| 1030 | + * @param bool $subFiles |
|
| 1031 | + * @param bool $includeTags |
|
| 1032 | + * |
|
| 1033 | + * @return list<Files_SharingShare> |
|
| 1034 | + * @throws NotFoundException |
|
| 1035 | + * @throws OCSBadRequestException |
|
| 1036 | + */ |
|
| 1037 | + private function getFormattedShares( |
|
| 1038 | + string $viewer, |
|
| 1039 | + $node = null, |
|
| 1040 | + bool $sharedWithMe = false, |
|
| 1041 | + bool $reShares = false, |
|
| 1042 | + bool $subFiles = false, |
|
| 1043 | + bool $includeTags = false, |
|
| 1044 | + ): array { |
|
| 1045 | + if ($sharedWithMe) { |
|
| 1046 | + return $this->getSharedWithMe($node, $includeTags); |
|
| 1047 | + } |
|
| 1048 | + |
|
| 1049 | + if ($subFiles) { |
|
| 1050 | + return $this->getSharesInDir($node); |
|
| 1051 | + } |
|
| 1052 | + |
|
| 1053 | + $shares = $this->getSharesFromNode($viewer, $node, $reShares); |
|
| 1054 | + |
|
| 1055 | + $known = $formatted = $miniFormatted = []; |
|
| 1056 | + $resharingRight = false; |
|
| 1057 | + foreach ($shares as $share) { |
|
| 1058 | + try { |
|
| 1059 | + $share->getNode(); |
|
| 1060 | + } catch (NotFoundException $e) { |
|
| 1061 | + /* |
|
| 1062 | 1062 | * Ignore shares where we can't get the node |
| 1063 | 1063 | * For example deleted shares |
| 1064 | 1064 | */ |
| 1065 | - continue; |
|
| 1066 | - } |
|
| 1067 | - |
|
| 1068 | - if (in_array($share->getId(), $known) |
|
| 1069 | - || ($share->getSharedWith() === $this->userId && $share->getShareType() === IShare::TYPE_USER)) { |
|
| 1070 | - continue; |
|
| 1071 | - } |
|
| 1072 | - |
|
| 1073 | - $known[] = $share->getId(); |
|
| 1074 | - try { |
|
| 1075 | - /** @var IShare $share */ |
|
| 1076 | - $format = $this->formatShare($share, $node); |
|
| 1077 | - $formatted[] = $format; |
|
| 1078 | - |
|
| 1079 | - // let's also build a list of shares created |
|
| 1080 | - // by the current user only, in case |
|
| 1081 | - // there is no resharing rights |
|
| 1082 | - if ($share->getSharedBy() === $this->userId) { |
|
| 1083 | - $miniFormatted[] = $format; |
|
| 1084 | - } |
|
| 1085 | - |
|
| 1086 | - // check if one of those share is shared with me |
|
| 1087 | - // and if I have resharing rights on it |
|
| 1088 | - if (!$resharingRight && $this->shareProviderResharingRights($this->userId, $share, $node)) { |
|
| 1089 | - $resharingRight = true; |
|
| 1090 | - } |
|
| 1091 | - } catch (InvalidPathException|NotFoundException $e) { |
|
| 1092 | - } |
|
| 1093 | - } |
|
| 1094 | - |
|
| 1095 | - if (!$resharingRight) { |
|
| 1096 | - $formatted = $miniFormatted; |
|
| 1097 | - } |
|
| 1098 | - |
|
| 1099 | - // fix eventual missing display name from federated shares |
|
| 1100 | - $formatted = $this->fixMissingDisplayName($formatted); |
|
| 1101 | - |
|
| 1102 | - if ($includeTags) { |
|
| 1103 | - $formatted = |
|
| 1104 | - Helper::populateTags($formatted, Server::get(ITagManager::class)); |
|
| 1105 | - } |
|
| 1106 | - |
|
| 1107 | - return $formatted; |
|
| 1108 | - } |
|
| 1109 | - |
|
| 1110 | - |
|
| 1111 | - /** |
|
| 1112 | - * Get all shares relative to a file, including parent folders shares rights |
|
| 1113 | - * |
|
| 1114 | - * @param string $path Path all shares will be relative to |
|
| 1115 | - * |
|
| 1116 | - * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 1117 | - * @throws InvalidPathException |
|
| 1118 | - * @throws NotFoundException |
|
| 1119 | - * @throws OCSNotFoundException The given path is invalid |
|
| 1120 | - * @throws SharingRightsException |
|
| 1121 | - * |
|
| 1122 | - * 200: Shares returned |
|
| 1123 | - */ |
|
| 1124 | - #[NoAdminRequired] |
|
| 1125 | - public function getInheritedShares(string $path): DataResponse { |
|
| 1126 | - // get Node from (string) path. |
|
| 1127 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1128 | - try { |
|
| 1129 | - $node = $userFolder->get($path); |
|
| 1130 | - $this->lock($node); |
|
| 1131 | - } catch (NotFoundException $e) { |
|
| 1132 | - throw new OCSNotFoundException($this->l->t('Wrong path, file/folder does not exist')); |
|
| 1133 | - } catch (LockedException $e) { |
|
| 1134 | - throw new OCSNotFoundException($this->l->t('Could not lock path')); |
|
| 1135 | - } |
|
| 1136 | - |
|
| 1137 | - if (!($node->getPermissions() & Constants::PERMISSION_SHARE)) { |
|
| 1138 | - throw new SharingRightsException($this->l->t('no sharing rights on this item')); |
|
| 1139 | - } |
|
| 1140 | - |
|
| 1141 | - // The current top parent we have access to |
|
| 1142 | - $parent = $node; |
|
| 1143 | - |
|
| 1144 | - // initiate real owner. |
|
| 1145 | - $owner = $node->getOwner() |
|
| 1146 | - ->getUID(); |
|
| 1147 | - if (!$this->userManager->userExists($owner)) { |
|
| 1148 | - return new DataResponse([]); |
|
| 1149 | - } |
|
| 1150 | - |
|
| 1151 | - // get node based on the owner, fix owner in case of external storage |
|
| 1152 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1153 | - if ($node->getId() !== $userFolder->getId() && !$userFolder->isSubNode($node)) { |
|
| 1154 | - $owner = $node->getOwner() |
|
| 1155 | - ->getUID(); |
|
| 1156 | - $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1157 | - $node = $userFolder->getFirstNodeById($node->getId()); |
|
| 1158 | - } |
|
| 1159 | - $basePath = $userFolder->getPath(); |
|
| 1160 | - |
|
| 1161 | - // generate node list for each parent folders |
|
| 1162 | - /** @var Node[] $nodes */ |
|
| 1163 | - $nodes = []; |
|
| 1164 | - while (true) { |
|
| 1165 | - $node = $node->getParent(); |
|
| 1166 | - if ($node->getPath() === $basePath) { |
|
| 1167 | - break; |
|
| 1168 | - } |
|
| 1169 | - $nodes[] = $node; |
|
| 1170 | - } |
|
| 1171 | - |
|
| 1172 | - // The user that is requesting this list |
|
| 1173 | - $currentUserFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1174 | - |
|
| 1175 | - // for each nodes, retrieve shares. |
|
| 1176 | - $shares = []; |
|
| 1177 | - |
|
| 1178 | - foreach ($nodes as $node) { |
|
| 1179 | - $getShares = $this->getFormattedShares($owner, $node, false, true); |
|
| 1180 | - |
|
| 1181 | - $currentUserNode = $currentUserFolder->getFirstNodeById($node->getId()); |
|
| 1182 | - if ($currentUserNode) { |
|
| 1183 | - $parent = $currentUserNode; |
|
| 1184 | - } |
|
| 1185 | - |
|
| 1186 | - $subPath = $currentUserFolder->getRelativePath($parent->getPath()); |
|
| 1187 | - foreach ($getShares as &$share) { |
|
| 1188 | - $share['via_fileid'] = $parent->getId(); |
|
| 1189 | - $share['via_path'] = $subPath; |
|
| 1190 | - } |
|
| 1191 | - $this->mergeFormattedShares($shares, $getShares); |
|
| 1192 | - } |
|
| 1193 | - |
|
| 1194 | - return new DataResponse(array_values($shares)); |
|
| 1195 | - } |
|
| 1196 | - |
|
| 1197 | - /** |
|
| 1198 | - * Check whether a set of permissions contains the permissions to check. |
|
| 1199 | - */ |
|
| 1200 | - private function hasPermission(int $permissionsSet, int $permissionsToCheck): bool { |
|
| 1201 | - return ($permissionsSet & $permissionsToCheck) === $permissionsToCheck; |
|
| 1202 | - } |
|
| 1203 | - |
|
| 1204 | - /** |
|
| 1205 | - * Update a share |
|
| 1206 | - * |
|
| 1207 | - * @param string $id ID of the share |
|
| 1208 | - * @param int|null $permissions New permissions |
|
| 1209 | - * @param string|null $password New password |
|
| 1210 | - * @param string|null $sendPasswordByTalk New condition if the password should be send over Talk |
|
| 1211 | - * @param string|null $publicUpload New condition if public uploading is allowed |
|
| 1212 | - * @param string|null $expireDate New expiry date |
|
| 1213 | - * @param string|null $note New note |
|
| 1214 | - * @param string|null $label New label |
|
| 1215 | - * @param string|null $hideDownload New condition if the download should be hidden |
|
| 1216 | - * @param string|null $attributes New additional attributes |
|
| 1217 | - * @param string|null $sendMail if the share should be send by mail. |
|
| 1218 | - * Considering the share already exists, no mail will be send after the share is updated. |
|
| 1219 | - * You will have to use the sendMail action to send the mail. |
|
| 1220 | - * @param string|null $shareWith New recipient for email shares |
|
| 1221 | - * @param string|null $token New token |
|
| 1222 | - * @return DataResponse<Http::STATUS_OK, Files_SharingShare, array{}> |
|
| 1223 | - * @throws OCSBadRequestException Share could not be updated because the requested changes are invalid |
|
| 1224 | - * @throws OCSForbiddenException Missing permissions to update the share |
|
| 1225 | - * @throws OCSNotFoundException Share not found |
|
| 1226 | - * |
|
| 1227 | - * 200: Share updated successfully |
|
| 1228 | - */ |
|
| 1229 | - #[NoAdminRequired] |
|
| 1230 | - public function updateShare( |
|
| 1231 | - string $id, |
|
| 1232 | - ?int $permissions = null, |
|
| 1233 | - ?string $password = null, |
|
| 1234 | - ?string $sendPasswordByTalk = null, |
|
| 1235 | - ?string $publicUpload = null, |
|
| 1236 | - ?string $expireDate = null, |
|
| 1237 | - ?string $note = null, |
|
| 1238 | - ?string $label = null, |
|
| 1239 | - ?string $hideDownload = null, |
|
| 1240 | - ?string $attributes = null, |
|
| 1241 | - ?string $sendMail = null, |
|
| 1242 | - ?string $token = null, |
|
| 1243 | - ): DataResponse { |
|
| 1244 | - try { |
|
| 1245 | - $share = $this->getShareById($id); |
|
| 1246 | - } catch (ShareNotFound $e) { |
|
| 1247 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1248 | - } |
|
| 1249 | - |
|
| 1250 | - $this->lock($share->getNode()); |
|
| 1251 | - |
|
| 1252 | - if (!$this->canAccessShare($share, false)) { |
|
| 1253 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1254 | - } |
|
| 1255 | - |
|
| 1256 | - if (!$this->canEditShare($share)) { |
|
| 1257 | - throw new OCSForbiddenException($this->l->t('You are not allowed to edit incoming shares')); |
|
| 1258 | - } |
|
| 1259 | - |
|
| 1260 | - if ( |
|
| 1261 | - $permissions === null && |
|
| 1262 | - $password === null && |
|
| 1263 | - $sendPasswordByTalk === null && |
|
| 1264 | - $publicUpload === null && |
|
| 1265 | - $expireDate === null && |
|
| 1266 | - $note === null && |
|
| 1267 | - $label === null && |
|
| 1268 | - $hideDownload === null && |
|
| 1269 | - $attributes === null && |
|
| 1270 | - $sendMail === null && |
|
| 1271 | - $token === null |
|
| 1272 | - ) { |
|
| 1273 | - throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); |
|
| 1274 | - } |
|
| 1275 | - |
|
| 1276 | - if ($note !== null) { |
|
| 1277 | - $share->setNote($note); |
|
| 1278 | - } |
|
| 1279 | - |
|
| 1280 | - if ($attributes !== null) { |
|
| 1281 | - $share = $this->setShareAttributes($share, $attributes); |
|
| 1282 | - } |
|
| 1283 | - |
|
| 1284 | - // Handle mail send |
|
| 1285 | - if ($sendMail === 'true' || $sendMail === 'false') { |
|
| 1286 | - $share->setMailSend($sendMail === 'true'); |
|
| 1287 | - } |
|
| 1288 | - |
|
| 1289 | - /** |
|
| 1290 | - * expiration date, password and publicUpload only make sense for link shares |
|
| 1291 | - */ |
|
| 1292 | - if ($share->getShareType() === IShare::TYPE_LINK |
|
| 1293 | - || $share->getShareType() === IShare::TYPE_EMAIL) { |
|
| 1294 | - |
|
| 1295 | - // Update hide download state |
|
| 1296 | - if ($hideDownload === 'true') { |
|
| 1297 | - $share->setHideDownload(true); |
|
| 1298 | - } elseif ($hideDownload === 'false') { |
|
| 1299 | - $share->setHideDownload(false); |
|
| 1300 | - } |
|
| 1301 | - |
|
| 1302 | - // If either manual permissions are specified or publicUpload |
|
| 1303 | - // then we need to also update the permissions of the share |
|
| 1304 | - if ($permissions !== null || $publicUpload !== null) { |
|
| 1305 | - $hasPublicUpload = $this->getLegacyPublicUpload($publicUpload); |
|
| 1306 | - $permissions = $this->getLinkSharePermissions($permissions ?? Constants::PERMISSION_READ, $hasPublicUpload); |
|
| 1307 | - $this->validateLinkSharePermissions($share->getNode(), $permissions, $hasPublicUpload); |
|
| 1308 | - $share->setPermissions($permissions); |
|
| 1309 | - } |
|
| 1310 | - |
|
| 1311 | - if ($password === '') { |
|
| 1312 | - $share->setPassword(null); |
|
| 1313 | - } elseif ($password !== null) { |
|
| 1314 | - $share->setPassword($password); |
|
| 1315 | - } |
|
| 1316 | - |
|
| 1317 | - if ($label !== null) { |
|
| 1318 | - if (strlen($label) > 255) { |
|
| 1319 | - throw new OCSBadRequestException('Maximum label length is 255'); |
|
| 1320 | - } |
|
| 1321 | - $share->setLabel($label); |
|
| 1322 | - } |
|
| 1323 | - |
|
| 1324 | - if ($sendPasswordByTalk === 'true') { |
|
| 1325 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1326 | - throw new OCSForbiddenException($this->l->t('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.')); |
|
| 1327 | - } |
|
| 1328 | - |
|
| 1329 | - $share->setSendPasswordByTalk(true); |
|
| 1330 | - } elseif ($sendPasswordByTalk !== null) { |
|
| 1331 | - $share->setSendPasswordByTalk(false); |
|
| 1332 | - } |
|
| 1333 | - |
|
| 1334 | - if ($token !== null) { |
|
| 1335 | - if (!$this->shareManager->allowCustomTokens()) { |
|
| 1336 | - throw new OCSForbiddenException($this->l->t('Custom share link tokens have been disabled by the administrator')); |
|
| 1337 | - } |
|
| 1338 | - if (!$this->validateToken($token)) { |
|
| 1339 | - throw new OCSBadRequestException($this->l->t('Tokens must contain at least 1 character and may only contain letters, numbers, or a hyphen')); |
|
| 1340 | - } |
|
| 1341 | - $share->setToken($token); |
|
| 1342 | - } |
|
| 1343 | - } |
|
| 1344 | - |
|
| 1345 | - // NOT A LINK SHARE |
|
| 1346 | - else { |
|
| 1347 | - if ($permissions !== null) { |
|
| 1348 | - $share->setPermissions($permissions); |
|
| 1349 | - } |
|
| 1350 | - } |
|
| 1351 | - |
|
| 1352 | - if ($expireDate === '') { |
|
| 1353 | - $share->setExpirationDate(null); |
|
| 1354 | - } elseif ($expireDate !== null) { |
|
| 1355 | - try { |
|
| 1356 | - $expireDateTime = $this->parseDate($expireDate); |
|
| 1357 | - $share->setExpirationDate($expireDateTime); |
|
| 1358 | - } catch (\Exception $e) { |
|
| 1359 | - throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 1360 | - } |
|
| 1361 | - } |
|
| 1362 | - |
|
| 1363 | - try { |
|
| 1364 | - $this->checkInheritedAttributes($share); |
|
| 1365 | - $share = $this->shareManager->updateShare($share); |
|
| 1366 | - } catch (HintException $e) { |
|
| 1367 | - $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 1368 | - throw new OCSException($e->getHint(), (int)$code); |
|
| 1369 | - } catch (\Exception $e) { |
|
| 1370 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 1371 | - throw new OCSBadRequestException('Failed to update share.', $e); |
|
| 1372 | - } |
|
| 1373 | - |
|
| 1374 | - return new DataResponse($this->formatShare($share)); |
|
| 1375 | - } |
|
| 1376 | - |
|
| 1377 | - private function validateToken(string $token): bool { |
|
| 1378 | - if (mb_strlen($token) === 0) { |
|
| 1379 | - return false; |
|
| 1380 | - } |
|
| 1381 | - if (!preg_match('/^[a-z0-9-]+$/i', $token)) { |
|
| 1382 | - return false; |
|
| 1383 | - } |
|
| 1384 | - return true; |
|
| 1385 | - } |
|
| 1386 | - |
|
| 1387 | - /** |
|
| 1388 | - * Get all shares that are still pending |
|
| 1389 | - * |
|
| 1390 | - * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 1391 | - * |
|
| 1392 | - * 200: Pending shares returned |
|
| 1393 | - */ |
|
| 1394 | - #[NoAdminRequired] |
|
| 1395 | - public function pendingShares(): DataResponse { |
|
| 1396 | - $pendingShares = []; |
|
| 1397 | - |
|
| 1398 | - $shareTypes = [ |
|
| 1399 | - IShare::TYPE_USER, |
|
| 1400 | - IShare::TYPE_GROUP |
|
| 1401 | - ]; |
|
| 1402 | - |
|
| 1403 | - foreach ($shareTypes as $shareType) { |
|
| 1404 | - $shares = $this->shareManager->getSharedWith($this->userId, $shareType, null, -1, 0); |
|
| 1405 | - |
|
| 1406 | - foreach ($shares as $share) { |
|
| 1407 | - if ($share->getStatus() === IShare::STATUS_PENDING || $share->getStatus() === IShare::STATUS_REJECTED) { |
|
| 1408 | - $pendingShares[] = $share; |
|
| 1409 | - } |
|
| 1410 | - } |
|
| 1411 | - } |
|
| 1412 | - |
|
| 1413 | - $result = array_values(array_filter(array_map(function (IShare $share) { |
|
| 1414 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 1415 | - $node = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1416 | - if (!$node) { |
|
| 1417 | - // fallback to guessing the path |
|
| 1418 | - $node = $userFolder->get($share->getTarget()); |
|
| 1419 | - if ($node === null || $share->getTarget() === '') { |
|
| 1420 | - return null; |
|
| 1421 | - } |
|
| 1422 | - } |
|
| 1423 | - |
|
| 1424 | - try { |
|
| 1425 | - $formattedShare = $this->formatShare($share, $node); |
|
| 1426 | - $formattedShare['path'] = '/' . $share->getNode()->getName(); |
|
| 1427 | - $formattedShare['permissions'] = 0; |
|
| 1428 | - return $formattedShare; |
|
| 1429 | - } catch (NotFoundException $e) { |
|
| 1430 | - return null; |
|
| 1431 | - } |
|
| 1432 | - }, $pendingShares), function ($entry) { |
|
| 1433 | - return $entry !== null; |
|
| 1434 | - })); |
|
| 1435 | - |
|
| 1436 | - return new DataResponse($result); |
|
| 1437 | - } |
|
| 1438 | - |
|
| 1439 | - /** |
|
| 1440 | - * Accept a share |
|
| 1441 | - * |
|
| 1442 | - * @param string $id ID of the share |
|
| 1443 | - * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 1444 | - * @throws OCSNotFoundException Share not found |
|
| 1445 | - * @throws OCSException |
|
| 1446 | - * @throws OCSBadRequestException Share could not be accepted |
|
| 1447 | - * |
|
| 1448 | - * 200: Share accepted successfully |
|
| 1449 | - */ |
|
| 1450 | - #[NoAdminRequired] |
|
| 1451 | - public function acceptShare(string $id): DataResponse { |
|
| 1452 | - try { |
|
| 1453 | - $share = $this->getShareById($id); |
|
| 1454 | - } catch (ShareNotFound $e) { |
|
| 1455 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1456 | - } |
|
| 1457 | - |
|
| 1458 | - if (!$this->canAccessShare($share)) { |
|
| 1459 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1460 | - } |
|
| 1461 | - |
|
| 1462 | - try { |
|
| 1463 | - $this->shareManager->acceptShare($share, $this->userId); |
|
| 1464 | - } catch (HintException $e) { |
|
| 1465 | - $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 1466 | - throw new OCSException($e->getHint(), (int)$code); |
|
| 1467 | - } catch (\Exception $e) { |
|
| 1468 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 1469 | - throw new OCSBadRequestException('Failed to accept share.', $e); |
|
| 1470 | - } |
|
| 1471 | - |
|
| 1472 | - return new DataResponse(); |
|
| 1473 | - } |
|
| 1474 | - |
|
| 1475 | - /** |
|
| 1476 | - * Does the user have read permission on the share |
|
| 1477 | - * |
|
| 1478 | - * @param IShare $share the share to check |
|
| 1479 | - * @param boolean $checkGroups check groups as well? |
|
| 1480 | - * @return boolean |
|
| 1481 | - * @throws NotFoundException |
|
| 1482 | - * |
|
| 1483 | - * @suppress PhanUndeclaredClassMethod |
|
| 1484 | - */ |
|
| 1485 | - protected function canAccessShare(IShare $share, bool $checkGroups = true): bool { |
|
| 1486 | - // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1487 | - if ($share->getPermissions() === 0) { |
|
| 1488 | - return false; |
|
| 1489 | - } |
|
| 1490 | - |
|
| 1491 | - // Owner of the file and the sharer of the file can always get share |
|
| 1492 | - if ($share->getShareOwner() === $this->userId |
|
| 1493 | - || $share->getSharedBy() === $this->userId) { |
|
| 1494 | - return true; |
|
| 1495 | - } |
|
| 1496 | - |
|
| 1497 | - // If the share is shared with you, you can access it! |
|
| 1498 | - if ($share->getShareType() === IShare::TYPE_USER |
|
| 1499 | - && $share->getSharedWith() === $this->userId) { |
|
| 1500 | - return true; |
|
| 1501 | - } |
|
| 1502 | - |
|
| 1503 | - // Have reshare rights on the shared file/folder ? |
|
| 1504 | - // Does the currentUser have access to the shared file? |
|
| 1505 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1506 | - $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1507 | - if ($file && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1508 | - return true; |
|
| 1509 | - } |
|
| 1510 | - |
|
| 1511 | - // If in the recipient group, you can see the share |
|
| 1512 | - if ($checkGroups && $share->getShareType() === IShare::TYPE_GROUP) { |
|
| 1513 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 1514 | - $user = $this->userManager->get($this->userId); |
|
| 1515 | - if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 1516 | - return true; |
|
| 1517 | - } |
|
| 1518 | - } |
|
| 1519 | - |
|
| 1520 | - if ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
| 1521 | - // TODO: have a sanity check like above? |
|
| 1522 | - return true; |
|
| 1523 | - } |
|
| 1524 | - |
|
| 1525 | - if ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 1526 | - try { |
|
| 1527 | - return $this->getRoomShareHelper()->canAccessShare($share, $this->userId); |
|
| 1528 | - } catch (ContainerExceptionInterface $e) { |
|
| 1529 | - return false; |
|
| 1530 | - } |
|
| 1531 | - } |
|
| 1532 | - |
|
| 1533 | - if ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 1534 | - try { |
|
| 1535 | - return $this->getDeckShareHelper()->canAccessShare($share, $this->userId); |
|
| 1536 | - } catch (ContainerExceptionInterface $e) { |
|
| 1537 | - return false; |
|
| 1538 | - } |
|
| 1539 | - } |
|
| 1540 | - |
|
| 1541 | - if ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 1542 | - try { |
|
| 1543 | - return $this->getSciencemeshShareHelper()->canAccessShare($share, $this->userId); |
|
| 1544 | - } catch (ContainerExceptionInterface $e) { |
|
| 1545 | - return false; |
|
| 1546 | - } |
|
| 1547 | - } |
|
| 1548 | - |
|
| 1549 | - return false; |
|
| 1550 | - } |
|
| 1551 | - |
|
| 1552 | - /** |
|
| 1553 | - * Does the user have edit permission on the share |
|
| 1554 | - * |
|
| 1555 | - * @param IShare $share the share to check |
|
| 1556 | - * @return boolean |
|
| 1557 | - */ |
|
| 1558 | - protected function canEditShare(IShare $share): bool { |
|
| 1559 | - // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1560 | - if ($share->getPermissions() === 0) { |
|
| 1561 | - return false; |
|
| 1562 | - } |
|
| 1563 | - |
|
| 1564 | - // The owner of the file and the creator of the share |
|
| 1565 | - // can always edit the share |
|
| 1566 | - if ($share->getShareOwner() === $this->userId || |
|
| 1567 | - $share->getSharedBy() === $this->userId |
|
| 1568 | - ) { |
|
| 1569 | - return true; |
|
| 1570 | - } |
|
| 1571 | - |
|
| 1572 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1573 | - $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1574 | - if ($file?->getMountPoint() instanceof IShareOwnerlessMount && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1575 | - return true; |
|
| 1576 | - } |
|
| 1577 | - |
|
| 1578 | - //! we do NOT support some kind of `admin` in groups. |
|
| 1579 | - //! You cannot edit shares shared to a group you're |
|
| 1580 | - //! a member of if you're not the share owner or the file owner! |
|
| 1581 | - |
|
| 1582 | - return false; |
|
| 1583 | - } |
|
| 1584 | - |
|
| 1585 | - /** |
|
| 1586 | - * Does the user have delete permission on the share |
|
| 1587 | - * |
|
| 1588 | - * @param IShare $share the share to check |
|
| 1589 | - * @return boolean |
|
| 1590 | - */ |
|
| 1591 | - protected function canDeleteShare(IShare $share): bool { |
|
| 1592 | - // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1593 | - if ($share->getPermissions() === 0) { |
|
| 1594 | - return false; |
|
| 1595 | - } |
|
| 1596 | - |
|
| 1597 | - // if the user is the recipient, i can unshare |
|
| 1598 | - // the share with self |
|
| 1599 | - if ($share->getShareType() === IShare::TYPE_USER && |
|
| 1600 | - $share->getSharedWith() === $this->userId |
|
| 1601 | - ) { |
|
| 1602 | - return true; |
|
| 1603 | - } |
|
| 1604 | - |
|
| 1605 | - // The owner of the file and the creator of the share |
|
| 1606 | - // can always delete the share |
|
| 1607 | - if ($share->getShareOwner() === $this->userId || |
|
| 1608 | - $share->getSharedBy() === $this->userId |
|
| 1609 | - ) { |
|
| 1610 | - return true; |
|
| 1611 | - } |
|
| 1612 | - |
|
| 1613 | - $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1614 | - $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1615 | - if ($file?->getMountPoint() instanceof IShareOwnerlessMount && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1616 | - return true; |
|
| 1617 | - } |
|
| 1618 | - |
|
| 1619 | - return false; |
|
| 1620 | - } |
|
| 1621 | - |
|
| 1622 | - /** |
|
| 1623 | - * Does the user have delete permission on the share |
|
| 1624 | - * This differs from the canDeleteShare function as it only |
|
| 1625 | - * remove the share for the current user. It does NOT |
|
| 1626 | - * completely delete the share but only the mount point. |
|
| 1627 | - * It can then be restored from the deleted shares section. |
|
| 1628 | - * |
|
| 1629 | - * @param IShare $share the share to check |
|
| 1630 | - * @return boolean |
|
| 1631 | - * |
|
| 1632 | - * @suppress PhanUndeclaredClassMethod |
|
| 1633 | - */ |
|
| 1634 | - protected function canDeleteShareFromSelf(IShare $share): bool { |
|
| 1635 | - if ($share->getShareType() !== IShare::TYPE_GROUP && |
|
| 1636 | - $share->getShareType() !== IShare::TYPE_ROOM && |
|
| 1637 | - $share->getShareType() !== IShare::TYPE_DECK && |
|
| 1638 | - $share->getShareType() !== IShare::TYPE_SCIENCEMESH |
|
| 1639 | - ) { |
|
| 1640 | - return false; |
|
| 1641 | - } |
|
| 1642 | - |
|
| 1643 | - if ($share->getShareOwner() === $this->userId || |
|
| 1644 | - $share->getSharedBy() === $this->userId |
|
| 1645 | - ) { |
|
| 1646 | - // Delete the whole share, not just for self |
|
| 1647 | - return false; |
|
| 1648 | - } |
|
| 1649 | - |
|
| 1650 | - // If in the recipient group, you can delete the share from self |
|
| 1651 | - if ($share->getShareType() === IShare::TYPE_GROUP) { |
|
| 1652 | - $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 1653 | - $user = $this->userManager->get($this->userId); |
|
| 1654 | - if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 1655 | - return true; |
|
| 1656 | - } |
|
| 1657 | - } |
|
| 1658 | - |
|
| 1659 | - if ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 1660 | - try { |
|
| 1661 | - return $this->getRoomShareHelper()->canAccessShare($share, $this->userId); |
|
| 1662 | - } catch (ContainerExceptionInterface $e) { |
|
| 1663 | - return false; |
|
| 1664 | - } |
|
| 1665 | - } |
|
| 1666 | - |
|
| 1667 | - if ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 1668 | - try { |
|
| 1669 | - return $this->getDeckShareHelper()->canAccessShare($share, $this->userId); |
|
| 1670 | - } catch (ContainerExceptionInterface $e) { |
|
| 1671 | - return false; |
|
| 1672 | - } |
|
| 1673 | - } |
|
| 1674 | - |
|
| 1675 | - if ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 1676 | - try { |
|
| 1677 | - return $this->getSciencemeshShareHelper()->canAccessShare($share, $this->userId); |
|
| 1678 | - } catch (ContainerExceptionInterface $e) { |
|
| 1679 | - return false; |
|
| 1680 | - } |
|
| 1681 | - } |
|
| 1682 | - |
|
| 1683 | - return false; |
|
| 1684 | - } |
|
| 1685 | - |
|
| 1686 | - /** |
|
| 1687 | - * Make sure that the passed date is valid ISO 8601 |
|
| 1688 | - * So YYYY-MM-DD |
|
| 1689 | - * If not throw an exception |
|
| 1690 | - * |
|
| 1691 | - * @param string $expireDate |
|
| 1692 | - * |
|
| 1693 | - * @throws \Exception |
|
| 1694 | - * @return \DateTime |
|
| 1695 | - */ |
|
| 1696 | - private function parseDate(string $expireDate): \DateTime { |
|
| 1697 | - try { |
|
| 1698 | - $date = new \DateTime(trim($expireDate, '"'), $this->dateTimeZone->getTimeZone()); |
|
| 1699 | - // Make sure it expires at midnight in owner timezone |
|
| 1700 | - $date->setTime(0, 0, 0); |
|
| 1701 | - } catch (\Exception $e) { |
|
| 1702 | - throw new \Exception($this->l->t('Invalid date. Format must be YYYY-MM-DD')); |
|
| 1703 | - } |
|
| 1704 | - |
|
| 1705 | - return $date; |
|
| 1706 | - } |
|
| 1707 | - |
|
| 1708 | - /** |
|
| 1709 | - * Since we have multiple providers but the OCS Share API v1 does |
|
| 1710 | - * not support this we need to check all backends. |
|
| 1711 | - * |
|
| 1712 | - * @param string $id |
|
| 1713 | - * @return IShare |
|
| 1714 | - * @throws ShareNotFound |
|
| 1715 | - */ |
|
| 1716 | - private function getShareById(string $id): IShare { |
|
| 1717 | - $share = null; |
|
| 1718 | - |
|
| 1719 | - // First check if it is an internal share. |
|
| 1720 | - try { |
|
| 1721 | - $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->userId); |
|
| 1722 | - return $share; |
|
| 1723 | - } catch (ShareNotFound $e) { |
|
| 1724 | - // Do nothing, just try the other share type |
|
| 1725 | - } |
|
| 1726 | - |
|
| 1727 | - |
|
| 1728 | - try { |
|
| 1729 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_CIRCLE)) { |
|
| 1730 | - $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->userId); |
|
| 1731 | - return $share; |
|
| 1732 | - } |
|
| 1733 | - } catch (ShareNotFound $e) { |
|
| 1734 | - // Do nothing, just try the other share type |
|
| 1735 | - } |
|
| 1736 | - |
|
| 1737 | - try { |
|
| 1738 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 1739 | - $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->userId); |
|
| 1740 | - return $share; |
|
| 1741 | - } |
|
| 1742 | - } catch (ShareNotFound $e) { |
|
| 1743 | - // Do nothing, just try the other share type |
|
| 1744 | - } |
|
| 1745 | - |
|
| 1746 | - try { |
|
| 1747 | - $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->userId); |
|
| 1748 | - return $share; |
|
| 1749 | - } catch (ShareNotFound $e) { |
|
| 1750 | - // Do nothing, just try the other share type |
|
| 1751 | - } |
|
| 1752 | - |
|
| 1753 | - try { |
|
| 1754 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 1755 | - $share = $this->shareManager->getShareById('deck:' . $id, $this->userId); |
|
| 1756 | - return $share; |
|
| 1757 | - } |
|
| 1758 | - } catch (ShareNotFound $e) { |
|
| 1759 | - // Do nothing, just try the other share type |
|
| 1760 | - } |
|
| 1761 | - |
|
| 1762 | - try { |
|
| 1763 | - if ($this->shareManager->shareProviderExists(IShare::TYPE_SCIENCEMESH)) { |
|
| 1764 | - $share = $this->shareManager->getShareById('sciencemesh:' . $id, $this->userId); |
|
| 1765 | - return $share; |
|
| 1766 | - } |
|
| 1767 | - } catch (ShareNotFound $e) { |
|
| 1768 | - // Do nothing, just try the other share type |
|
| 1769 | - } |
|
| 1770 | - |
|
| 1771 | - if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1772 | - throw new ShareNotFound(); |
|
| 1773 | - } |
|
| 1774 | - $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->userId); |
|
| 1775 | - |
|
| 1776 | - return $share; |
|
| 1777 | - } |
|
| 1778 | - |
|
| 1779 | - /** |
|
| 1780 | - * Lock a Node |
|
| 1781 | - * |
|
| 1782 | - * @param Node $node |
|
| 1783 | - * @throws LockedException |
|
| 1784 | - */ |
|
| 1785 | - private function lock(Node $node) { |
|
| 1786 | - $node->lock(ILockingProvider::LOCK_SHARED); |
|
| 1787 | - $this->lockedNode = $node; |
|
| 1788 | - } |
|
| 1789 | - |
|
| 1790 | - /** |
|
| 1791 | - * Cleanup the remaining locks |
|
| 1792 | - * @throws LockedException |
|
| 1793 | - */ |
|
| 1794 | - public function cleanup() { |
|
| 1795 | - if ($this->lockedNode !== null) { |
|
| 1796 | - $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED); |
|
| 1797 | - } |
|
| 1798 | - } |
|
| 1799 | - |
|
| 1800 | - /** |
|
| 1801 | - * Returns the helper of ShareAPIController for room shares. |
|
| 1802 | - * |
|
| 1803 | - * If the Talk application is not enabled or the helper is not available |
|
| 1804 | - * a ContainerExceptionInterface is thrown instead. |
|
| 1805 | - * |
|
| 1806 | - * @return \OCA\Talk\Share\Helper\ShareAPIController |
|
| 1807 | - * @throws ContainerExceptionInterface |
|
| 1808 | - */ |
|
| 1809 | - private function getRoomShareHelper() { |
|
| 1810 | - if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1811 | - throw new QueryException(); |
|
| 1812 | - } |
|
| 1813 | - |
|
| 1814 | - return $this->serverContainer->get('\OCA\Talk\Share\Helper\ShareAPIController'); |
|
| 1815 | - } |
|
| 1816 | - |
|
| 1817 | - /** |
|
| 1818 | - * Returns the helper of ShareAPIHelper for deck shares. |
|
| 1819 | - * |
|
| 1820 | - * If the Deck application is not enabled or the helper is not available |
|
| 1821 | - * a ContainerExceptionInterface is thrown instead. |
|
| 1822 | - * |
|
| 1823 | - * @return \OCA\Deck\Sharing\ShareAPIHelper |
|
| 1824 | - * @throws ContainerExceptionInterface |
|
| 1825 | - */ |
|
| 1826 | - private function getDeckShareHelper() { |
|
| 1827 | - if (!$this->appManager->isEnabledForUser('deck')) { |
|
| 1828 | - throw new QueryException(); |
|
| 1829 | - } |
|
| 1830 | - |
|
| 1831 | - return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper'); |
|
| 1832 | - } |
|
| 1833 | - |
|
| 1834 | - /** |
|
| 1835 | - * Returns the helper of ShareAPIHelper for sciencemesh shares. |
|
| 1836 | - * |
|
| 1837 | - * If the sciencemesh application is not enabled or the helper is not available |
|
| 1838 | - * a ContainerExceptionInterface is thrown instead. |
|
| 1839 | - * |
|
| 1840 | - * @return \OCA\Deck\Sharing\ShareAPIHelper |
|
| 1841 | - * @throws ContainerExceptionInterface |
|
| 1842 | - */ |
|
| 1843 | - private function getSciencemeshShareHelper() { |
|
| 1844 | - if (!$this->appManager->isEnabledForUser('sciencemesh')) { |
|
| 1845 | - throw new QueryException(); |
|
| 1846 | - } |
|
| 1847 | - |
|
| 1848 | - return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper'); |
|
| 1849 | - } |
|
| 1850 | - |
|
| 1851 | - /** |
|
| 1852 | - * @param string $viewer |
|
| 1853 | - * @param Node $node |
|
| 1854 | - * @param bool $reShares |
|
| 1855 | - * |
|
| 1856 | - * @return IShare[] |
|
| 1857 | - */ |
|
| 1858 | - private function getSharesFromNode(string $viewer, $node, bool $reShares): array { |
|
| 1859 | - $providers = [ |
|
| 1860 | - IShare::TYPE_USER, |
|
| 1861 | - IShare::TYPE_GROUP, |
|
| 1862 | - IShare::TYPE_LINK, |
|
| 1863 | - IShare::TYPE_EMAIL, |
|
| 1864 | - IShare::TYPE_CIRCLE, |
|
| 1865 | - IShare::TYPE_ROOM, |
|
| 1866 | - IShare::TYPE_DECK, |
|
| 1867 | - IShare::TYPE_SCIENCEMESH |
|
| 1868 | - ]; |
|
| 1869 | - |
|
| 1870 | - // Should we assume that the (currentUser) viewer is the owner of the node !? |
|
| 1871 | - $shares = []; |
|
| 1872 | - foreach ($providers as $provider) { |
|
| 1873 | - if (!$this->shareManager->shareProviderExists($provider)) { |
|
| 1874 | - continue; |
|
| 1875 | - } |
|
| 1876 | - |
|
| 1877 | - $providerShares = |
|
| 1878 | - $this->shareManager->getSharesBy($viewer, $provider, $node, $reShares, -1, 0); |
|
| 1879 | - $shares = array_merge($shares, $providerShares); |
|
| 1880 | - } |
|
| 1881 | - |
|
| 1882 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1883 | - $federatedShares = $this->shareManager->getSharesBy( |
|
| 1884 | - $this->userId, IShare::TYPE_REMOTE, $node, $reShares, -1, 0 |
|
| 1885 | - ); |
|
| 1886 | - $shares = array_merge($shares, $federatedShares); |
|
| 1887 | - } |
|
| 1888 | - |
|
| 1889 | - if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 1890 | - $federatedShares = $this->shareManager->getSharesBy( |
|
| 1891 | - $this->userId, IShare::TYPE_REMOTE_GROUP, $node, $reShares, -1, 0 |
|
| 1892 | - ); |
|
| 1893 | - $shares = array_merge($shares, $federatedShares); |
|
| 1894 | - } |
|
| 1895 | - |
|
| 1896 | - return $shares; |
|
| 1897 | - } |
|
| 1898 | - |
|
| 1899 | - |
|
| 1900 | - /** |
|
| 1901 | - * @param Node $node |
|
| 1902 | - * |
|
| 1903 | - * @throws SharingRightsException |
|
| 1904 | - */ |
|
| 1905 | - private function confirmSharingRights(Node $node): void { |
|
| 1906 | - if (!$this->hasResharingRights($this->userId, $node)) { |
|
| 1907 | - throw new SharingRightsException($this->l->t('No sharing rights on this item')); |
|
| 1908 | - } |
|
| 1909 | - } |
|
| 1910 | - |
|
| 1911 | - |
|
| 1912 | - /** |
|
| 1913 | - * @param string $viewer |
|
| 1914 | - * @param Node $node |
|
| 1915 | - * |
|
| 1916 | - * @return bool |
|
| 1917 | - */ |
|
| 1918 | - private function hasResharingRights($viewer, $node): bool { |
|
| 1919 | - if ($viewer === $node->getOwner()->getUID()) { |
|
| 1920 | - return true; |
|
| 1921 | - } |
|
| 1922 | - |
|
| 1923 | - foreach ([$node, $node->getParent()] as $node) { |
|
| 1924 | - $shares = $this->getSharesFromNode($viewer, $node, true); |
|
| 1925 | - foreach ($shares as $share) { |
|
| 1926 | - try { |
|
| 1927 | - if ($this->shareProviderResharingRights($viewer, $share, $node)) { |
|
| 1928 | - return true; |
|
| 1929 | - } |
|
| 1930 | - } catch (InvalidPathException|NotFoundException $e) { |
|
| 1931 | - } |
|
| 1932 | - } |
|
| 1933 | - } |
|
| 1934 | - |
|
| 1935 | - return false; |
|
| 1936 | - } |
|
| 1937 | - |
|
| 1938 | - |
|
| 1939 | - /** |
|
| 1940 | - * Returns if we can find resharing rights in an IShare object for a specific user. |
|
| 1941 | - * |
|
| 1942 | - * @suppress PhanUndeclaredClassMethod |
|
| 1943 | - * |
|
| 1944 | - * @param string $userId |
|
| 1945 | - * @param IShare $share |
|
| 1946 | - * @param Node $node |
|
| 1947 | - * |
|
| 1948 | - * @return bool |
|
| 1949 | - * @throws NotFoundException |
|
| 1950 | - * @throws InvalidPathException |
|
| 1951 | - */ |
|
| 1952 | - private function shareProviderResharingRights(string $userId, IShare $share, $node): bool { |
|
| 1953 | - if ($share->getShareOwner() === $userId) { |
|
| 1954 | - return true; |
|
| 1955 | - } |
|
| 1956 | - |
|
| 1957 | - // we check that current user have parent resharing rights on the current file |
|
| 1958 | - if ($node !== null && ($node->getPermissions() & Constants::PERMISSION_SHARE) !== 0) { |
|
| 1959 | - return true; |
|
| 1960 | - } |
|
| 1961 | - |
|
| 1962 | - if ((Constants::PERMISSION_SHARE & $share->getPermissions()) === 0) { |
|
| 1963 | - return false; |
|
| 1964 | - } |
|
| 1965 | - |
|
| 1966 | - if ($share->getShareType() === IShare::TYPE_USER && $share->getSharedWith() === $userId) { |
|
| 1967 | - return true; |
|
| 1968 | - } |
|
| 1969 | - |
|
| 1970 | - if ($share->getShareType() === IShare::TYPE_GROUP && $this->groupManager->isInGroup($userId, $share->getSharedWith())) { |
|
| 1971 | - return true; |
|
| 1972 | - } |
|
| 1973 | - |
|
| 1974 | - if ($share->getShareType() === IShare::TYPE_CIRCLE && Server::get(IAppManager::class)->isEnabledForUser('circles') |
|
| 1975 | - && class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 1976 | - $hasCircleId = (str_ends_with($share->getSharedWith(), ']')); |
|
| 1977 | - $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0); |
|
| 1978 | - $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' ')); |
|
| 1979 | - if ($shareWithLength === false) { |
|
| 1980 | - $sharedWith = substr($share->getSharedWith(), $shareWithStart); |
|
| 1981 | - } else { |
|
| 1982 | - $sharedWith = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 1983 | - } |
|
| 1984 | - try { |
|
| 1985 | - $member = Circles::getMember($sharedWith, $userId, 1); |
|
| 1986 | - if ($member->getLevel() >= 4) { |
|
| 1987 | - return true; |
|
| 1988 | - } |
|
| 1989 | - return false; |
|
| 1990 | - } catch (ContainerExceptionInterface $e) { |
|
| 1991 | - return false; |
|
| 1992 | - } |
|
| 1993 | - } |
|
| 1994 | - |
|
| 1995 | - return false; |
|
| 1996 | - } |
|
| 1997 | - |
|
| 1998 | - /** |
|
| 1999 | - * Get all the shares for the current user |
|
| 2000 | - * |
|
| 2001 | - * @param Node|null $path |
|
| 2002 | - * @param boolean $reshares |
|
| 2003 | - * @return IShare[] |
|
| 2004 | - */ |
|
| 2005 | - private function getAllShares(?Node $path = null, bool $reshares = false) { |
|
| 2006 | - // Get all shares |
|
| 2007 | - $userShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_USER, $path, $reshares, -1, 0); |
|
| 2008 | - $groupShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_GROUP, $path, $reshares, -1, 0); |
|
| 2009 | - $linkShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_LINK, $path, $reshares, -1, 0); |
|
| 2010 | - |
|
| 2011 | - // EMAIL SHARES |
|
| 2012 | - $mailShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_EMAIL, $path, $reshares, -1, 0); |
|
| 2013 | - |
|
| 2014 | - // TEAM SHARES |
|
| 2015 | - $circleShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_CIRCLE, $path, $reshares, -1, 0); |
|
| 2016 | - |
|
| 2017 | - // TALK SHARES |
|
| 2018 | - $roomShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_ROOM, $path, $reshares, -1, 0); |
|
| 2019 | - |
|
| 2020 | - // DECK SHARES |
|
| 2021 | - $deckShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_DECK, $path, $reshares, -1, 0); |
|
| 2022 | - |
|
| 2023 | - // SCIENCEMESH SHARES |
|
| 2024 | - $sciencemeshShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_SCIENCEMESH, $path, $reshares, -1, 0); |
|
| 2025 | - |
|
| 2026 | - // FEDERATION |
|
| 2027 | - if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 2028 | - $federatedShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE, $path, $reshares, -1, 0); |
|
| 2029 | - } else { |
|
| 2030 | - $federatedShares = []; |
|
| 2031 | - } |
|
| 2032 | - if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 2033 | - $federatedGroupShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); |
|
| 2034 | - } else { |
|
| 2035 | - $federatedGroupShares = []; |
|
| 2036 | - } |
|
| 2037 | - |
|
| 2038 | - return array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares, $deckShares, $sciencemeshShares, $federatedShares, $federatedGroupShares); |
|
| 2039 | - } |
|
| 2040 | - |
|
| 2041 | - |
|
| 2042 | - /** |
|
| 2043 | - * merging already formatted shares. |
|
| 2044 | - * We'll make an associative array to easily detect duplicate Ids. |
|
| 2045 | - * Keys _needs_ to be removed after all shares are retrieved and merged. |
|
| 2046 | - * |
|
| 2047 | - * @param array $shares |
|
| 2048 | - * @param array $newShares |
|
| 2049 | - */ |
|
| 2050 | - private function mergeFormattedShares(array &$shares, array $newShares) { |
|
| 2051 | - foreach ($newShares as $newShare) { |
|
| 2052 | - if (!array_key_exists($newShare['id'], $shares)) { |
|
| 2053 | - $shares[$newShare['id']] = $newShare; |
|
| 2054 | - } |
|
| 2055 | - } |
|
| 2056 | - } |
|
| 2057 | - |
|
| 2058 | - /** |
|
| 2059 | - * @param IShare $share |
|
| 2060 | - * @param string|null $attributesString |
|
| 2061 | - * @return IShare modified share |
|
| 2062 | - */ |
|
| 2063 | - private function setShareAttributes(IShare $share, ?string $attributesString) { |
|
| 2064 | - $newShareAttributes = null; |
|
| 2065 | - if ($attributesString !== null) { |
|
| 2066 | - $newShareAttributes = $this->shareManager->newShare()->newAttributes(); |
|
| 2067 | - $formattedShareAttributes = \json_decode($attributesString, true); |
|
| 2068 | - if (is_array($formattedShareAttributes)) { |
|
| 2069 | - foreach ($formattedShareAttributes as $formattedAttr) { |
|
| 2070 | - $newShareAttributes->setAttribute( |
|
| 2071 | - $formattedAttr['scope'], |
|
| 2072 | - $formattedAttr['key'], |
|
| 2073 | - $formattedAttr['value'], |
|
| 2074 | - ); |
|
| 2075 | - } |
|
| 2076 | - } else { |
|
| 2077 | - throw new OCSBadRequestException($this->l->t('Invalid share attributes provided: "%s"', [$attributesString])); |
|
| 2078 | - } |
|
| 2079 | - } |
|
| 2080 | - $share->setAttributes($newShareAttributes); |
|
| 2081 | - |
|
| 2082 | - return $share; |
|
| 2083 | - } |
|
| 2084 | - |
|
| 2085 | - private function checkInheritedAttributes(IShare $share): void { |
|
| 2086 | - if (!$share->getSharedBy()) { |
|
| 2087 | - return; // Probably in a test |
|
| 2088 | - } |
|
| 2089 | - |
|
| 2090 | - $canDownload = false; |
|
| 2091 | - $hideDownload = true; |
|
| 2092 | - |
|
| 2093 | - $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 2094 | - $nodes = $userFolder->getById($share->getNodeId()); |
|
| 2095 | - foreach ($nodes as $node) { |
|
| 2096 | - // Owner always can download it - so allow it and break |
|
| 2097 | - if ($node->getOwner()?->getUID() === $share->getSharedBy()) { |
|
| 2098 | - $canDownload = true; |
|
| 2099 | - $hideDownload = false; |
|
| 2100 | - break; |
|
| 2101 | - } |
|
| 2102 | - |
|
| 2103 | - if ($node->getStorage()->instanceOfStorage(SharedStorage::class)) { |
|
| 2104 | - $storage = $node->getStorage(); |
|
| 2105 | - if ($storage instanceof Wrapper) { |
|
| 2106 | - $storage = $storage->getInstanceOfStorage(SharedStorage::class); |
|
| 2107 | - if ($storage === null) { |
|
| 2108 | - throw new \RuntimeException('Should not happen, instanceOfStorage but getInstanceOfStorage return null'); |
|
| 2109 | - } |
|
| 2110 | - } else { |
|
| 2111 | - throw new \RuntimeException('Should not happen, instanceOfStorage but not a wrapper'); |
|
| 2112 | - } |
|
| 2113 | - |
|
| 2114 | - /** @var SharedStorage $storage */ |
|
| 2115 | - $originalShare = $storage->getShare(); |
|
| 2116 | - $inheritedAttributes = $originalShare->getAttributes(); |
|
| 2117 | - // hide if hidden and also the current share enforces hide (can only be false if one share is false or user is owner) |
|
| 2118 | - $hideDownload = $hideDownload && $originalShare->getHideDownload(); |
|
| 2119 | - // allow download if already allowed by previous share or when the current share allows downloading |
|
| 2120 | - $canDownload = $canDownload || $inheritedAttributes === null || $inheritedAttributes->getAttribute('permissions', 'download') !== false; |
|
| 2121 | - } |
|
| 2122 | - } |
|
| 2123 | - |
|
| 2124 | - if ($hideDownload || !$canDownload) { |
|
| 2125 | - $share->setHideDownload(true); |
|
| 2126 | - |
|
| 2127 | - if (!$canDownload) { |
|
| 2128 | - $attributes = $share->getAttributes() ?? $share->newAttributes(); |
|
| 2129 | - $attributes->setAttribute('permissions', 'download', false); |
|
| 2130 | - $share->setAttributes($attributes); |
|
| 2131 | - } |
|
| 2132 | - } |
|
| 2133 | - } |
|
| 2134 | - |
|
| 2135 | - /** |
|
| 2136 | - * Send a mail notification again for a share. |
|
| 2137 | - * The mail_send option must be enabled for the given share. |
|
| 2138 | - * @param string $id the share ID |
|
| 2139 | - * @param string $password the password to check against. Necessary for password protected shares. |
|
| 2140 | - * @throws OCSNotFoundException Share not found |
|
| 2141 | - * @throws OCSForbiddenException You are not allowed to send mail notifications |
|
| 2142 | - * @throws OCSBadRequestException Invalid request or wrong password |
|
| 2143 | - * @throws OCSException Error while sending mail notification |
|
| 2144 | - * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 2145 | - * |
|
| 2146 | - * 200: The email notification was sent successfully |
|
| 2147 | - */ |
|
| 2148 | - #[NoAdminRequired] |
|
| 2149 | - #[UserRateLimit(limit: 10, period: 600)] |
|
| 2150 | - public function sendShareEmail(string $id, $password = ''): DataResponse { |
|
| 2151 | - try { |
|
| 2152 | - $share = $this->getShareById($id); |
|
| 2153 | - |
|
| 2154 | - if (!$this->canAccessShare($share, false)) { |
|
| 2155 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 2156 | - } |
|
| 2157 | - |
|
| 2158 | - if (!$this->canEditShare($share)) { |
|
| 2159 | - throw new OCSForbiddenException($this->l->t('You are not allowed to send mail notifications')); |
|
| 2160 | - } |
|
| 2161 | - |
|
| 2162 | - // For mail and link shares, the user must be |
|
| 2163 | - // the owner of the share, not only the file owner. |
|
| 2164 | - if ($share->getShareType() === IShare::TYPE_EMAIL |
|
| 2165 | - || $share->getShareType() === IShare::TYPE_LINK) { |
|
| 2166 | - if ($share->getSharedBy() !== $this->userId) { |
|
| 2167 | - throw new OCSForbiddenException($this->l->t('You are not allowed to send mail notifications')); |
|
| 2168 | - } |
|
| 2169 | - } |
|
| 2170 | - |
|
| 2171 | - try { |
|
| 2172 | - $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 2173 | - if (!($provider instanceof IShareProviderWithNotification)) { |
|
| 2174 | - throw new OCSBadRequestException($this->l->t('No mail notification configured for this share type')); |
|
| 2175 | - } |
|
| 2176 | - |
|
| 2177 | - // Circumvent the password encrypted data by |
|
| 2178 | - // setting the password clear. We're not storing |
|
| 2179 | - // the password clear, it is just a temporary |
|
| 2180 | - // object manipulation. The password will stay |
|
| 2181 | - // encrypted in the database. |
|
| 2182 | - if ($share->getPassword() !== null && $share->getPassword() !== $password) { |
|
| 2183 | - if (!$this->shareManager->checkPassword($share, $password)) { |
|
| 2184 | - throw new OCSBadRequestException($this->l->t('Wrong password')); |
|
| 2185 | - } |
|
| 2186 | - $share = $share->setPassword($password); |
|
| 2187 | - } |
|
| 2188 | - |
|
| 2189 | - $provider->sendMailNotification($share); |
|
| 2190 | - return new DataResponse(); |
|
| 2191 | - } catch (Exception $e) { |
|
| 2192 | - $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 2193 | - throw new OCSException($this->l->t('Error while sending mail notification')); |
|
| 2194 | - } |
|
| 2195 | - |
|
| 2196 | - } catch (ShareNotFound $e) { |
|
| 2197 | - throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 2198 | - } |
|
| 2199 | - } |
|
| 2200 | - |
|
| 2201 | - /** |
|
| 2202 | - * Get a unique share token |
|
| 2203 | - * |
|
| 2204 | - * @throws OCSException Failed to generate a unique token |
|
| 2205 | - * |
|
| 2206 | - * @return DataResponse<Http::STATUS_OK, array{token: string}, array{}> |
|
| 2207 | - * |
|
| 2208 | - * 200: Token generated successfully |
|
| 2209 | - */ |
|
| 2210 | - #[ApiRoute(verb: 'GET', url: '/api/v1/token')] |
|
| 2211 | - #[NoAdminRequired] |
|
| 2212 | - public function generateToken(): DataResponse { |
|
| 2213 | - try { |
|
| 2214 | - $token = $this->shareManager->generateToken(); |
|
| 2215 | - return new DataResponse([ |
|
| 2216 | - 'token' => $token, |
|
| 2217 | - ]); |
|
| 2218 | - } catch (ShareTokenException $e) { |
|
| 2219 | - throw new OCSException($this->l->t('Failed to generate a unique token')); |
|
| 2220 | - } |
|
| 2221 | - } |
|
| 1065 | + continue; |
|
| 1066 | + } |
|
| 1067 | + |
|
| 1068 | + if (in_array($share->getId(), $known) |
|
| 1069 | + || ($share->getSharedWith() === $this->userId && $share->getShareType() === IShare::TYPE_USER)) { |
|
| 1070 | + continue; |
|
| 1071 | + } |
|
| 1072 | + |
|
| 1073 | + $known[] = $share->getId(); |
|
| 1074 | + try { |
|
| 1075 | + /** @var IShare $share */ |
|
| 1076 | + $format = $this->formatShare($share, $node); |
|
| 1077 | + $formatted[] = $format; |
|
| 1078 | + |
|
| 1079 | + // let's also build a list of shares created |
|
| 1080 | + // by the current user only, in case |
|
| 1081 | + // there is no resharing rights |
|
| 1082 | + if ($share->getSharedBy() === $this->userId) { |
|
| 1083 | + $miniFormatted[] = $format; |
|
| 1084 | + } |
|
| 1085 | + |
|
| 1086 | + // check if one of those share is shared with me |
|
| 1087 | + // and if I have resharing rights on it |
|
| 1088 | + if (!$resharingRight && $this->shareProviderResharingRights($this->userId, $share, $node)) { |
|
| 1089 | + $resharingRight = true; |
|
| 1090 | + } |
|
| 1091 | + } catch (InvalidPathException|NotFoundException $e) { |
|
| 1092 | + } |
|
| 1093 | + } |
|
| 1094 | + |
|
| 1095 | + if (!$resharingRight) { |
|
| 1096 | + $formatted = $miniFormatted; |
|
| 1097 | + } |
|
| 1098 | + |
|
| 1099 | + // fix eventual missing display name from federated shares |
|
| 1100 | + $formatted = $this->fixMissingDisplayName($formatted); |
|
| 1101 | + |
|
| 1102 | + if ($includeTags) { |
|
| 1103 | + $formatted = |
|
| 1104 | + Helper::populateTags($formatted, Server::get(ITagManager::class)); |
|
| 1105 | + } |
|
| 1106 | + |
|
| 1107 | + return $formatted; |
|
| 1108 | + } |
|
| 1109 | + |
|
| 1110 | + |
|
| 1111 | + /** |
|
| 1112 | + * Get all shares relative to a file, including parent folders shares rights |
|
| 1113 | + * |
|
| 1114 | + * @param string $path Path all shares will be relative to |
|
| 1115 | + * |
|
| 1116 | + * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 1117 | + * @throws InvalidPathException |
|
| 1118 | + * @throws NotFoundException |
|
| 1119 | + * @throws OCSNotFoundException The given path is invalid |
|
| 1120 | + * @throws SharingRightsException |
|
| 1121 | + * |
|
| 1122 | + * 200: Shares returned |
|
| 1123 | + */ |
|
| 1124 | + #[NoAdminRequired] |
|
| 1125 | + public function getInheritedShares(string $path): DataResponse { |
|
| 1126 | + // get Node from (string) path. |
|
| 1127 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1128 | + try { |
|
| 1129 | + $node = $userFolder->get($path); |
|
| 1130 | + $this->lock($node); |
|
| 1131 | + } catch (NotFoundException $e) { |
|
| 1132 | + throw new OCSNotFoundException($this->l->t('Wrong path, file/folder does not exist')); |
|
| 1133 | + } catch (LockedException $e) { |
|
| 1134 | + throw new OCSNotFoundException($this->l->t('Could not lock path')); |
|
| 1135 | + } |
|
| 1136 | + |
|
| 1137 | + if (!($node->getPermissions() & Constants::PERMISSION_SHARE)) { |
|
| 1138 | + throw new SharingRightsException($this->l->t('no sharing rights on this item')); |
|
| 1139 | + } |
|
| 1140 | + |
|
| 1141 | + // The current top parent we have access to |
|
| 1142 | + $parent = $node; |
|
| 1143 | + |
|
| 1144 | + // initiate real owner. |
|
| 1145 | + $owner = $node->getOwner() |
|
| 1146 | + ->getUID(); |
|
| 1147 | + if (!$this->userManager->userExists($owner)) { |
|
| 1148 | + return new DataResponse([]); |
|
| 1149 | + } |
|
| 1150 | + |
|
| 1151 | + // get node based on the owner, fix owner in case of external storage |
|
| 1152 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1153 | + if ($node->getId() !== $userFolder->getId() && !$userFolder->isSubNode($node)) { |
|
| 1154 | + $owner = $node->getOwner() |
|
| 1155 | + ->getUID(); |
|
| 1156 | + $userFolder = $this->rootFolder->getUserFolder($owner); |
|
| 1157 | + $node = $userFolder->getFirstNodeById($node->getId()); |
|
| 1158 | + } |
|
| 1159 | + $basePath = $userFolder->getPath(); |
|
| 1160 | + |
|
| 1161 | + // generate node list for each parent folders |
|
| 1162 | + /** @var Node[] $nodes */ |
|
| 1163 | + $nodes = []; |
|
| 1164 | + while (true) { |
|
| 1165 | + $node = $node->getParent(); |
|
| 1166 | + if ($node->getPath() === $basePath) { |
|
| 1167 | + break; |
|
| 1168 | + } |
|
| 1169 | + $nodes[] = $node; |
|
| 1170 | + } |
|
| 1171 | + |
|
| 1172 | + // The user that is requesting this list |
|
| 1173 | + $currentUserFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1174 | + |
|
| 1175 | + // for each nodes, retrieve shares. |
|
| 1176 | + $shares = []; |
|
| 1177 | + |
|
| 1178 | + foreach ($nodes as $node) { |
|
| 1179 | + $getShares = $this->getFormattedShares($owner, $node, false, true); |
|
| 1180 | + |
|
| 1181 | + $currentUserNode = $currentUserFolder->getFirstNodeById($node->getId()); |
|
| 1182 | + if ($currentUserNode) { |
|
| 1183 | + $parent = $currentUserNode; |
|
| 1184 | + } |
|
| 1185 | + |
|
| 1186 | + $subPath = $currentUserFolder->getRelativePath($parent->getPath()); |
|
| 1187 | + foreach ($getShares as &$share) { |
|
| 1188 | + $share['via_fileid'] = $parent->getId(); |
|
| 1189 | + $share['via_path'] = $subPath; |
|
| 1190 | + } |
|
| 1191 | + $this->mergeFormattedShares($shares, $getShares); |
|
| 1192 | + } |
|
| 1193 | + |
|
| 1194 | + return new DataResponse(array_values($shares)); |
|
| 1195 | + } |
|
| 1196 | + |
|
| 1197 | + /** |
|
| 1198 | + * Check whether a set of permissions contains the permissions to check. |
|
| 1199 | + */ |
|
| 1200 | + private function hasPermission(int $permissionsSet, int $permissionsToCheck): bool { |
|
| 1201 | + return ($permissionsSet & $permissionsToCheck) === $permissionsToCheck; |
|
| 1202 | + } |
|
| 1203 | + |
|
| 1204 | + /** |
|
| 1205 | + * Update a share |
|
| 1206 | + * |
|
| 1207 | + * @param string $id ID of the share |
|
| 1208 | + * @param int|null $permissions New permissions |
|
| 1209 | + * @param string|null $password New password |
|
| 1210 | + * @param string|null $sendPasswordByTalk New condition if the password should be send over Talk |
|
| 1211 | + * @param string|null $publicUpload New condition if public uploading is allowed |
|
| 1212 | + * @param string|null $expireDate New expiry date |
|
| 1213 | + * @param string|null $note New note |
|
| 1214 | + * @param string|null $label New label |
|
| 1215 | + * @param string|null $hideDownload New condition if the download should be hidden |
|
| 1216 | + * @param string|null $attributes New additional attributes |
|
| 1217 | + * @param string|null $sendMail if the share should be send by mail. |
|
| 1218 | + * Considering the share already exists, no mail will be send after the share is updated. |
|
| 1219 | + * You will have to use the sendMail action to send the mail. |
|
| 1220 | + * @param string|null $shareWith New recipient for email shares |
|
| 1221 | + * @param string|null $token New token |
|
| 1222 | + * @return DataResponse<Http::STATUS_OK, Files_SharingShare, array{}> |
|
| 1223 | + * @throws OCSBadRequestException Share could not be updated because the requested changes are invalid |
|
| 1224 | + * @throws OCSForbiddenException Missing permissions to update the share |
|
| 1225 | + * @throws OCSNotFoundException Share not found |
|
| 1226 | + * |
|
| 1227 | + * 200: Share updated successfully |
|
| 1228 | + */ |
|
| 1229 | + #[NoAdminRequired] |
|
| 1230 | + public function updateShare( |
|
| 1231 | + string $id, |
|
| 1232 | + ?int $permissions = null, |
|
| 1233 | + ?string $password = null, |
|
| 1234 | + ?string $sendPasswordByTalk = null, |
|
| 1235 | + ?string $publicUpload = null, |
|
| 1236 | + ?string $expireDate = null, |
|
| 1237 | + ?string $note = null, |
|
| 1238 | + ?string $label = null, |
|
| 1239 | + ?string $hideDownload = null, |
|
| 1240 | + ?string $attributes = null, |
|
| 1241 | + ?string $sendMail = null, |
|
| 1242 | + ?string $token = null, |
|
| 1243 | + ): DataResponse { |
|
| 1244 | + try { |
|
| 1245 | + $share = $this->getShareById($id); |
|
| 1246 | + } catch (ShareNotFound $e) { |
|
| 1247 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1248 | + } |
|
| 1249 | + |
|
| 1250 | + $this->lock($share->getNode()); |
|
| 1251 | + |
|
| 1252 | + if (!$this->canAccessShare($share, false)) { |
|
| 1253 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1254 | + } |
|
| 1255 | + |
|
| 1256 | + if (!$this->canEditShare($share)) { |
|
| 1257 | + throw new OCSForbiddenException($this->l->t('You are not allowed to edit incoming shares')); |
|
| 1258 | + } |
|
| 1259 | + |
|
| 1260 | + if ( |
|
| 1261 | + $permissions === null && |
|
| 1262 | + $password === null && |
|
| 1263 | + $sendPasswordByTalk === null && |
|
| 1264 | + $publicUpload === null && |
|
| 1265 | + $expireDate === null && |
|
| 1266 | + $note === null && |
|
| 1267 | + $label === null && |
|
| 1268 | + $hideDownload === null && |
|
| 1269 | + $attributes === null && |
|
| 1270 | + $sendMail === null && |
|
| 1271 | + $token === null |
|
| 1272 | + ) { |
|
| 1273 | + throw new OCSBadRequestException($this->l->t('Wrong or no update parameter given')); |
|
| 1274 | + } |
|
| 1275 | + |
|
| 1276 | + if ($note !== null) { |
|
| 1277 | + $share->setNote($note); |
|
| 1278 | + } |
|
| 1279 | + |
|
| 1280 | + if ($attributes !== null) { |
|
| 1281 | + $share = $this->setShareAttributes($share, $attributes); |
|
| 1282 | + } |
|
| 1283 | + |
|
| 1284 | + // Handle mail send |
|
| 1285 | + if ($sendMail === 'true' || $sendMail === 'false') { |
|
| 1286 | + $share->setMailSend($sendMail === 'true'); |
|
| 1287 | + } |
|
| 1288 | + |
|
| 1289 | + /** |
|
| 1290 | + * expiration date, password and publicUpload only make sense for link shares |
|
| 1291 | + */ |
|
| 1292 | + if ($share->getShareType() === IShare::TYPE_LINK |
|
| 1293 | + || $share->getShareType() === IShare::TYPE_EMAIL) { |
|
| 1294 | + |
|
| 1295 | + // Update hide download state |
|
| 1296 | + if ($hideDownload === 'true') { |
|
| 1297 | + $share->setHideDownload(true); |
|
| 1298 | + } elseif ($hideDownload === 'false') { |
|
| 1299 | + $share->setHideDownload(false); |
|
| 1300 | + } |
|
| 1301 | + |
|
| 1302 | + // If either manual permissions are specified or publicUpload |
|
| 1303 | + // then we need to also update the permissions of the share |
|
| 1304 | + if ($permissions !== null || $publicUpload !== null) { |
|
| 1305 | + $hasPublicUpload = $this->getLegacyPublicUpload($publicUpload); |
|
| 1306 | + $permissions = $this->getLinkSharePermissions($permissions ?? Constants::PERMISSION_READ, $hasPublicUpload); |
|
| 1307 | + $this->validateLinkSharePermissions($share->getNode(), $permissions, $hasPublicUpload); |
|
| 1308 | + $share->setPermissions($permissions); |
|
| 1309 | + } |
|
| 1310 | + |
|
| 1311 | + if ($password === '') { |
|
| 1312 | + $share->setPassword(null); |
|
| 1313 | + } elseif ($password !== null) { |
|
| 1314 | + $share->setPassword($password); |
|
| 1315 | + } |
|
| 1316 | + |
|
| 1317 | + if ($label !== null) { |
|
| 1318 | + if (strlen($label) > 255) { |
|
| 1319 | + throw new OCSBadRequestException('Maximum label length is 255'); |
|
| 1320 | + } |
|
| 1321 | + $share->setLabel($label); |
|
| 1322 | + } |
|
| 1323 | + |
|
| 1324 | + if ($sendPasswordByTalk === 'true') { |
|
| 1325 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1326 | + throw new OCSForbiddenException($this->l->t('"Sending the password by Nextcloud Talk" for sharing a file or folder failed because Nextcloud Talk is not enabled.')); |
|
| 1327 | + } |
|
| 1328 | + |
|
| 1329 | + $share->setSendPasswordByTalk(true); |
|
| 1330 | + } elseif ($sendPasswordByTalk !== null) { |
|
| 1331 | + $share->setSendPasswordByTalk(false); |
|
| 1332 | + } |
|
| 1333 | + |
|
| 1334 | + if ($token !== null) { |
|
| 1335 | + if (!$this->shareManager->allowCustomTokens()) { |
|
| 1336 | + throw new OCSForbiddenException($this->l->t('Custom share link tokens have been disabled by the administrator')); |
|
| 1337 | + } |
|
| 1338 | + if (!$this->validateToken($token)) { |
|
| 1339 | + throw new OCSBadRequestException($this->l->t('Tokens must contain at least 1 character and may only contain letters, numbers, or a hyphen')); |
|
| 1340 | + } |
|
| 1341 | + $share->setToken($token); |
|
| 1342 | + } |
|
| 1343 | + } |
|
| 1344 | + |
|
| 1345 | + // NOT A LINK SHARE |
|
| 1346 | + else { |
|
| 1347 | + if ($permissions !== null) { |
|
| 1348 | + $share->setPermissions($permissions); |
|
| 1349 | + } |
|
| 1350 | + } |
|
| 1351 | + |
|
| 1352 | + if ($expireDate === '') { |
|
| 1353 | + $share->setExpirationDate(null); |
|
| 1354 | + } elseif ($expireDate !== null) { |
|
| 1355 | + try { |
|
| 1356 | + $expireDateTime = $this->parseDate($expireDate); |
|
| 1357 | + $share->setExpirationDate($expireDateTime); |
|
| 1358 | + } catch (\Exception $e) { |
|
| 1359 | + throw new OCSBadRequestException($e->getMessage(), $e); |
|
| 1360 | + } |
|
| 1361 | + } |
|
| 1362 | + |
|
| 1363 | + try { |
|
| 1364 | + $this->checkInheritedAttributes($share); |
|
| 1365 | + $share = $this->shareManager->updateShare($share); |
|
| 1366 | + } catch (HintException $e) { |
|
| 1367 | + $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 1368 | + throw new OCSException($e->getHint(), (int)$code); |
|
| 1369 | + } catch (\Exception $e) { |
|
| 1370 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 1371 | + throw new OCSBadRequestException('Failed to update share.', $e); |
|
| 1372 | + } |
|
| 1373 | + |
|
| 1374 | + return new DataResponse($this->formatShare($share)); |
|
| 1375 | + } |
|
| 1376 | + |
|
| 1377 | + private function validateToken(string $token): bool { |
|
| 1378 | + if (mb_strlen($token) === 0) { |
|
| 1379 | + return false; |
|
| 1380 | + } |
|
| 1381 | + if (!preg_match('/^[a-z0-9-]+$/i', $token)) { |
|
| 1382 | + return false; |
|
| 1383 | + } |
|
| 1384 | + return true; |
|
| 1385 | + } |
|
| 1386 | + |
|
| 1387 | + /** |
|
| 1388 | + * Get all shares that are still pending |
|
| 1389 | + * |
|
| 1390 | + * @return DataResponse<Http::STATUS_OK, list<Files_SharingShare>, array{}> |
|
| 1391 | + * |
|
| 1392 | + * 200: Pending shares returned |
|
| 1393 | + */ |
|
| 1394 | + #[NoAdminRequired] |
|
| 1395 | + public function pendingShares(): DataResponse { |
|
| 1396 | + $pendingShares = []; |
|
| 1397 | + |
|
| 1398 | + $shareTypes = [ |
|
| 1399 | + IShare::TYPE_USER, |
|
| 1400 | + IShare::TYPE_GROUP |
|
| 1401 | + ]; |
|
| 1402 | + |
|
| 1403 | + foreach ($shareTypes as $shareType) { |
|
| 1404 | + $shares = $this->shareManager->getSharedWith($this->userId, $shareType, null, -1, 0); |
|
| 1405 | + |
|
| 1406 | + foreach ($shares as $share) { |
|
| 1407 | + if ($share->getStatus() === IShare::STATUS_PENDING || $share->getStatus() === IShare::STATUS_REJECTED) { |
|
| 1408 | + $pendingShares[] = $share; |
|
| 1409 | + } |
|
| 1410 | + } |
|
| 1411 | + } |
|
| 1412 | + |
|
| 1413 | + $result = array_values(array_filter(array_map(function (IShare $share) { |
|
| 1414 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 1415 | + $node = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1416 | + if (!$node) { |
|
| 1417 | + // fallback to guessing the path |
|
| 1418 | + $node = $userFolder->get($share->getTarget()); |
|
| 1419 | + if ($node === null || $share->getTarget() === '') { |
|
| 1420 | + return null; |
|
| 1421 | + } |
|
| 1422 | + } |
|
| 1423 | + |
|
| 1424 | + try { |
|
| 1425 | + $formattedShare = $this->formatShare($share, $node); |
|
| 1426 | + $formattedShare['path'] = '/' . $share->getNode()->getName(); |
|
| 1427 | + $formattedShare['permissions'] = 0; |
|
| 1428 | + return $formattedShare; |
|
| 1429 | + } catch (NotFoundException $e) { |
|
| 1430 | + return null; |
|
| 1431 | + } |
|
| 1432 | + }, $pendingShares), function ($entry) { |
|
| 1433 | + return $entry !== null; |
|
| 1434 | + })); |
|
| 1435 | + |
|
| 1436 | + return new DataResponse($result); |
|
| 1437 | + } |
|
| 1438 | + |
|
| 1439 | + /** |
|
| 1440 | + * Accept a share |
|
| 1441 | + * |
|
| 1442 | + * @param string $id ID of the share |
|
| 1443 | + * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 1444 | + * @throws OCSNotFoundException Share not found |
|
| 1445 | + * @throws OCSException |
|
| 1446 | + * @throws OCSBadRequestException Share could not be accepted |
|
| 1447 | + * |
|
| 1448 | + * 200: Share accepted successfully |
|
| 1449 | + */ |
|
| 1450 | + #[NoAdminRequired] |
|
| 1451 | + public function acceptShare(string $id): DataResponse { |
|
| 1452 | + try { |
|
| 1453 | + $share = $this->getShareById($id); |
|
| 1454 | + } catch (ShareNotFound $e) { |
|
| 1455 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1456 | + } |
|
| 1457 | + |
|
| 1458 | + if (!$this->canAccessShare($share)) { |
|
| 1459 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 1460 | + } |
|
| 1461 | + |
|
| 1462 | + try { |
|
| 1463 | + $this->shareManager->acceptShare($share, $this->userId); |
|
| 1464 | + } catch (HintException $e) { |
|
| 1465 | + $code = $e->getCode() === 0 ? 403 : $e->getCode(); |
|
| 1466 | + throw new OCSException($e->getHint(), (int)$code); |
|
| 1467 | + } catch (\Exception $e) { |
|
| 1468 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 1469 | + throw new OCSBadRequestException('Failed to accept share.', $e); |
|
| 1470 | + } |
|
| 1471 | + |
|
| 1472 | + return new DataResponse(); |
|
| 1473 | + } |
|
| 1474 | + |
|
| 1475 | + /** |
|
| 1476 | + * Does the user have read permission on the share |
|
| 1477 | + * |
|
| 1478 | + * @param IShare $share the share to check |
|
| 1479 | + * @param boolean $checkGroups check groups as well? |
|
| 1480 | + * @return boolean |
|
| 1481 | + * @throws NotFoundException |
|
| 1482 | + * |
|
| 1483 | + * @suppress PhanUndeclaredClassMethod |
|
| 1484 | + */ |
|
| 1485 | + protected function canAccessShare(IShare $share, bool $checkGroups = true): bool { |
|
| 1486 | + // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1487 | + if ($share->getPermissions() === 0) { |
|
| 1488 | + return false; |
|
| 1489 | + } |
|
| 1490 | + |
|
| 1491 | + // Owner of the file and the sharer of the file can always get share |
|
| 1492 | + if ($share->getShareOwner() === $this->userId |
|
| 1493 | + || $share->getSharedBy() === $this->userId) { |
|
| 1494 | + return true; |
|
| 1495 | + } |
|
| 1496 | + |
|
| 1497 | + // If the share is shared with you, you can access it! |
|
| 1498 | + if ($share->getShareType() === IShare::TYPE_USER |
|
| 1499 | + && $share->getSharedWith() === $this->userId) { |
|
| 1500 | + return true; |
|
| 1501 | + } |
|
| 1502 | + |
|
| 1503 | + // Have reshare rights on the shared file/folder ? |
|
| 1504 | + // Does the currentUser have access to the shared file? |
|
| 1505 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1506 | + $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1507 | + if ($file && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1508 | + return true; |
|
| 1509 | + } |
|
| 1510 | + |
|
| 1511 | + // If in the recipient group, you can see the share |
|
| 1512 | + if ($checkGroups && $share->getShareType() === IShare::TYPE_GROUP) { |
|
| 1513 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 1514 | + $user = $this->userManager->get($this->userId); |
|
| 1515 | + if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 1516 | + return true; |
|
| 1517 | + } |
|
| 1518 | + } |
|
| 1519 | + |
|
| 1520 | + if ($share->getShareType() === IShare::TYPE_CIRCLE) { |
|
| 1521 | + // TODO: have a sanity check like above? |
|
| 1522 | + return true; |
|
| 1523 | + } |
|
| 1524 | + |
|
| 1525 | + if ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 1526 | + try { |
|
| 1527 | + return $this->getRoomShareHelper()->canAccessShare($share, $this->userId); |
|
| 1528 | + } catch (ContainerExceptionInterface $e) { |
|
| 1529 | + return false; |
|
| 1530 | + } |
|
| 1531 | + } |
|
| 1532 | + |
|
| 1533 | + if ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 1534 | + try { |
|
| 1535 | + return $this->getDeckShareHelper()->canAccessShare($share, $this->userId); |
|
| 1536 | + } catch (ContainerExceptionInterface $e) { |
|
| 1537 | + return false; |
|
| 1538 | + } |
|
| 1539 | + } |
|
| 1540 | + |
|
| 1541 | + if ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 1542 | + try { |
|
| 1543 | + return $this->getSciencemeshShareHelper()->canAccessShare($share, $this->userId); |
|
| 1544 | + } catch (ContainerExceptionInterface $e) { |
|
| 1545 | + return false; |
|
| 1546 | + } |
|
| 1547 | + } |
|
| 1548 | + |
|
| 1549 | + return false; |
|
| 1550 | + } |
|
| 1551 | + |
|
| 1552 | + /** |
|
| 1553 | + * Does the user have edit permission on the share |
|
| 1554 | + * |
|
| 1555 | + * @param IShare $share the share to check |
|
| 1556 | + * @return boolean |
|
| 1557 | + */ |
|
| 1558 | + protected function canEditShare(IShare $share): bool { |
|
| 1559 | + // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1560 | + if ($share->getPermissions() === 0) { |
|
| 1561 | + return false; |
|
| 1562 | + } |
|
| 1563 | + |
|
| 1564 | + // The owner of the file and the creator of the share |
|
| 1565 | + // can always edit the share |
|
| 1566 | + if ($share->getShareOwner() === $this->userId || |
|
| 1567 | + $share->getSharedBy() === $this->userId |
|
| 1568 | + ) { |
|
| 1569 | + return true; |
|
| 1570 | + } |
|
| 1571 | + |
|
| 1572 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1573 | + $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1574 | + if ($file?->getMountPoint() instanceof IShareOwnerlessMount && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1575 | + return true; |
|
| 1576 | + } |
|
| 1577 | + |
|
| 1578 | + //! we do NOT support some kind of `admin` in groups. |
|
| 1579 | + //! You cannot edit shares shared to a group you're |
|
| 1580 | + //! a member of if you're not the share owner or the file owner! |
|
| 1581 | + |
|
| 1582 | + return false; |
|
| 1583 | + } |
|
| 1584 | + |
|
| 1585 | + /** |
|
| 1586 | + * Does the user have delete permission on the share |
|
| 1587 | + * |
|
| 1588 | + * @param IShare $share the share to check |
|
| 1589 | + * @return boolean |
|
| 1590 | + */ |
|
| 1591 | + protected function canDeleteShare(IShare $share): bool { |
|
| 1592 | + // A file with permissions 0 can't be accessed by us. So Don't show it |
|
| 1593 | + if ($share->getPermissions() === 0) { |
|
| 1594 | + return false; |
|
| 1595 | + } |
|
| 1596 | + |
|
| 1597 | + // if the user is the recipient, i can unshare |
|
| 1598 | + // the share with self |
|
| 1599 | + if ($share->getShareType() === IShare::TYPE_USER && |
|
| 1600 | + $share->getSharedWith() === $this->userId |
|
| 1601 | + ) { |
|
| 1602 | + return true; |
|
| 1603 | + } |
|
| 1604 | + |
|
| 1605 | + // The owner of the file and the creator of the share |
|
| 1606 | + // can always delete the share |
|
| 1607 | + if ($share->getShareOwner() === $this->userId || |
|
| 1608 | + $share->getSharedBy() === $this->userId |
|
| 1609 | + ) { |
|
| 1610 | + return true; |
|
| 1611 | + } |
|
| 1612 | + |
|
| 1613 | + $userFolder = $this->rootFolder->getUserFolder($this->userId); |
|
| 1614 | + $file = $userFolder->getFirstNodeById($share->getNodeId()); |
|
| 1615 | + if ($file?->getMountPoint() instanceof IShareOwnerlessMount && $this->shareProviderResharingRights($this->userId, $share, $file)) { |
|
| 1616 | + return true; |
|
| 1617 | + } |
|
| 1618 | + |
|
| 1619 | + return false; |
|
| 1620 | + } |
|
| 1621 | + |
|
| 1622 | + /** |
|
| 1623 | + * Does the user have delete permission on the share |
|
| 1624 | + * This differs from the canDeleteShare function as it only |
|
| 1625 | + * remove the share for the current user. It does NOT |
|
| 1626 | + * completely delete the share but only the mount point. |
|
| 1627 | + * It can then be restored from the deleted shares section. |
|
| 1628 | + * |
|
| 1629 | + * @param IShare $share the share to check |
|
| 1630 | + * @return boolean |
|
| 1631 | + * |
|
| 1632 | + * @suppress PhanUndeclaredClassMethod |
|
| 1633 | + */ |
|
| 1634 | + protected function canDeleteShareFromSelf(IShare $share): bool { |
|
| 1635 | + if ($share->getShareType() !== IShare::TYPE_GROUP && |
|
| 1636 | + $share->getShareType() !== IShare::TYPE_ROOM && |
|
| 1637 | + $share->getShareType() !== IShare::TYPE_DECK && |
|
| 1638 | + $share->getShareType() !== IShare::TYPE_SCIENCEMESH |
|
| 1639 | + ) { |
|
| 1640 | + return false; |
|
| 1641 | + } |
|
| 1642 | + |
|
| 1643 | + if ($share->getShareOwner() === $this->userId || |
|
| 1644 | + $share->getSharedBy() === $this->userId |
|
| 1645 | + ) { |
|
| 1646 | + // Delete the whole share, not just for self |
|
| 1647 | + return false; |
|
| 1648 | + } |
|
| 1649 | + |
|
| 1650 | + // If in the recipient group, you can delete the share from self |
|
| 1651 | + if ($share->getShareType() === IShare::TYPE_GROUP) { |
|
| 1652 | + $sharedWith = $this->groupManager->get($share->getSharedWith()); |
|
| 1653 | + $user = $this->userManager->get($this->userId); |
|
| 1654 | + if ($user !== null && $sharedWith !== null && $sharedWith->inGroup($user)) { |
|
| 1655 | + return true; |
|
| 1656 | + } |
|
| 1657 | + } |
|
| 1658 | + |
|
| 1659 | + if ($share->getShareType() === IShare::TYPE_ROOM) { |
|
| 1660 | + try { |
|
| 1661 | + return $this->getRoomShareHelper()->canAccessShare($share, $this->userId); |
|
| 1662 | + } catch (ContainerExceptionInterface $e) { |
|
| 1663 | + return false; |
|
| 1664 | + } |
|
| 1665 | + } |
|
| 1666 | + |
|
| 1667 | + if ($share->getShareType() === IShare::TYPE_DECK) { |
|
| 1668 | + try { |
|
| 1669 | + return $this->getDeckShareHelper()->canAccessShare($share, $this->userId); |
|
| 1670 | + } catch (ContainerExceptionInterface $e) { |
|
| 1671 | + return false; |
|
| 1672 | + } |
|
| 1673 | + } |
|
| 1674 | + |
|
| 1675 | + if ($share->getShareType() === IShare::TYPE_SCIENCEMESH) { |
|
| 1676 | + try { |
|
| 1677 | + return $this->getSciencemeshShareHelper()->canAccessShare($share, $this->userId); |
|
| 1678 | + } catch (ContainerExceptionInterface $e) { |
|
| 1679 | + return false; |
|
| 1680 | + } |
|
| 1681 | + } |
|
| 1682 | + |
|
| 1683 | + return false; |
|
| 1684 | + } |
|
| 1685 | + |
|
| 1686 | + /** |
|
| 1687 | + * Make sure that the passed date is valid ISO 8601 |
|
| 1688 | + * So YYYY-MM-DD |
|
| 1689 | + * If not throw an exception |
|
| 1690 | + * |
|
| 1691 | + * @param string $expireDate |
|
| 1692 | + * |
|
| 1693 | + * @throws \Exception |
|
| 1694 | + * @return \DateTime |
|
| 1695 | + */ |
|
| 1696 | + private function parseDate(string $expireDate): \DateTime { |
|
| 1697 | + try { |
|
| 1698 | + $date = new \DateTime(trim($expireDate, '"'), $this->dateTimeZone->getTimeZone()); |
|
| 1699 | + // Make sure it expires at midnight in owner timezone |
|
| 1700 | + $date->setTime(0, 0, 0); |
|
| 1701 | + } catch (\Exception $e) { |
|
| 1702 | + throw new \Exception($this->l->t('Invalid date. Format must be YYYY-MM-DD')); |
|
| 1703 | + } |
|
| 1704 | + |
|
| 1705 | + return $date; |
|
| 1706 | + } |
|
| 1707 | + |
|
| 1708 | + /** |
|
| 1709 | + * Since we have multiple providers but the OCS Share API v1 does |
|
| 1710 | + * not support this we need to check all backends. |
|
| 1711 | + * |
|
| 1712 | + * @param string $id |
|
| 1713 | + * @return IShare |
|
| 1714 | + * @throws ShareNotFound |
|
| 1715 | + */ |
|
| 1716 | + private function getShareById(string $id): IShare { |
|
| 1717 | + $share = null; |
|
| 1718 | + |
|
| 1719 | + // First check if it is an internal share. |
|
| 1720 | + try { |
|
| 1721 | + $share = $this->shareManager->getShareById('ocinternal:' . $id, $this->userId); |
|
| 1722 | + return $share; |
|
| 1723 | + } catch (ShareNotFound $e) { |
|
| 1724 | + // Do nothing, just try the other share type |
|
| 1725 | + } |
|
| 1726 | + |
|
| 1727 | + |
|
| 1728 | + try { |
|
| 1729 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_CIRCLE)) { |
|
| 1730 | + $share = $this->shareManager->getShareById('ocCircleShare:' . $id, $this->userId); |
|
| 1731 | + return $share; |
|
| 1732 | + } |
|
| 1733 | + } catch (ShareNotFound $e) { |
|
| 1734 | + // Do nothing, just try the other share type |
|
| 1735 | + } |
|
| 1736 | + |
|
| 1737 | + try { |
|
| 1738 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_EMAIL)) { |
|
| 1739 | + $share = $this->shareManager->getShareById('ocMailShare:' . $id, $this->userId); |
|
| 1740 | + return $share; |
|
| 1741 | + } |
|
| 1742 | + } catch (ShareNotFound $e) { |
|
| 1743 | + // Do nothing, just try the other share type |
|
| 1744 | + } |
|
| 1745 | + |
|
| 1746 | + try { |
|
| 1747 | + $share = $this->shareManager->getShareById('ocRoomShare:' . $id, $this->userId); |
|
| 1748 | + return $share; |
|
| 1749 | + } catch (ShareNotFound $e) { |
|
| 1750 | + // Do nothing, just try the other share type |
|
| 1751 | + } |
|
| 1752 | + |
|
| 1753 | + try { |
|
| 1754 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_DECK)) { |
|
| 1755 | + $share = $this->shareManager->getShareById('deck:' . $id, $this->userId); |
|
| 1756 | + return $share; |
|
| 1757 | + } |
|
| 1758 | + } catch (ShareNotFound $e) { |
|
| 1759 | + // Do nothing, just try the other share type |
|
| 1760 | + } |
|
| 1761 | + |
|
| 1762 | + try { |
|
| 1763 | + if ($this->shareManager->shareProviderExists(IShare::TYPE_SCIENCEMESH)) { |
|
| 1764 | + $share = $this->shareManager->getShareById('sciencemesh:' . $id, $this->userId); |
|
| 1765 | + return $share; |
|
| 1766 | + } |
|
| 1767 | + } catch (ShareNotFound $e) { |
|
| 1768 | + // Do nothing, just try the other share type |
|
| 1769 | + } |
|
| 1770 | + |
|
| 1771 | + if (!$this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1772 | + throw new ShareNotFound(); |
|
| 1773 | + } |
|
| 1774 | + $share = $this->shareManager->getShareById('ocFederatedSharing:' . $id, $this->userId); |
|
| 1775 | + |
|
| 1776 | + return $share; |
|
| 1777 | + } |
|
| 1778 | + |
|
| 1779 | + /** |
|
| 1780 | + * Lock a Node |
|
| 1781 | + * |
|
| 1782 | + * @param Node $node |
|
| 1783 | + * @throws LockedException |
|
| 1784 | + */ |
|
| 1785 | + private function lock(Node $node) { |
|
| 1786 | + $node->lock(ILockingProvider::LOCK_SHARED); |
|
| 1787 | + $this->lockedNode = $node; |
|
| 1788 | + } |
|
| 1789 | + |
|
| 1790 | + /** |
|
| 1791 | + * Cleanup the remaining locks |
|
| 1792 | + * @throws LockedException |
|
| 1793 | + */ |
|
| 1794 | + public function cleanup() { |
|
| 1795 | + if ($this->lockedNode !== null) { |
|
| 1796 | + $this->lockedNode->unlock(ILockingProvider::LOCK_SHARED); |
|
| 1797 | + } |
|
| 1798 | + } |
|
| 1799 | + |
|
| 1800 | + /** |
|
| 1801 | + * Returns the helper of ShareAPIController for room shares. |
|
| 1802 | + * |
|
| 1803 | + * If the Talk application is not enabled or the helper is not available |
|
| 1804 | + * a ContainerExceptionInterface is thrown instead. |
|
| 1805 | + * |
|
| 1806 | + * @return \OCA\Talk\Share\Helper\ShareAPIController |
|
| 1807 | + * @throws ContainerExceptionInterface |
|
| 1808 | + */ |
|
| 1809 | + private function getRoomShareHelper() { |
|
| 1810 | + if (!$this->appManager->isEnabledForUser('spreed')) { |
|
| 1811 | + throw new QueryException(); |
|
| 1812 | + } |
|
| 1813 | + |
|
| 1814 | + return $this->serverContainer->get('\OCA\Talk\Share\Helper\ShareAPIController'); |
|
| 1815 | + } |
|
| 1816 | + |
|
| 1817 | + /** |
|
| 1818 | + * Returns the helper of ShareAPIHelper for deck shares. |
|
| 1819 | + * |
|
| 1820 | + * If the Deck application is not enabled or the helper is not available |
|
| 1821 | + * a ContainerExceptionInterface is thrown instead. |
|
| 1822 | + * |
|
| 1823 | + * @return \OCA\Deck\Sharing\ShareAPIHelper |
|
| 1824 | + * @throws ContainerExceptionInterface |
|
| 1825 | + */ |
|
| 1826 | + private function getDeckShareHelper() { |
|
| 1827 | + if (!$this->appManager->isEnabledForUser('deck')) { |
|
| 1828 | + throw new QueryException(); |
|
| 1829 | + } |
|
| 1830 | + |
|
| 1831 | + return $this->serverContainer->get('\OCA\Deck\Sharing\ShareAPIHelper'); |
|
| 1832 | + } |
|
| 1833 | + |
|
| 1834 | + /** |
|
| 1835 | + * Returns the helper of ShareAPIHelper for sciencemesh shares. |
|
| 1836 | + * |
|
| 1837 | + * If the sciencemesh application is not enabled or the helper is not available |
|
| 1838 | + * a ContainerExceptionInterface is thrown instead. |
|
| 1839 | + * |
|
| 1840 | + * @return \OCA\Deck\Sharing\ShareAPIHelper |
|
| 1841 | + * @throws ContainerExceptionInterface |
|
| 1842 | + */ |
|
| 1843 | + private function getSciencemeshShareHelper() { |
|
| 1844 | + if (!$this->appManager->isEnabledForUser('sciencemesh')) { |
|
| 1845 | + throw new QueryException(); |
|
| 1846 | + } |
|
| 1847 | + |
|
| 1848 | + return $this->serverContainer->get('\OCA\ScienceMesh\Sharing\ShareAPIHelper'); |
|
| 1849 | + } |
|
| 1850 | + |
|
| 1851 | + /** |
|
| 1852 | + * @param string $viewer |
|
| 1853 | + * @param Node $node |
|
| 1854 | + * @param bool $reShares |
|
| 1855 | + * |
|
| 1856 | + * @return IShare[] |
|
| 1857 | + */ |
|
| 1858 | + private function getSharesFromNode(string $viewer, $node, bool $reShares): array { |
|
| 1859 | + $providers = [ |
|
| 1860 | + IShare::TYPE_USER, |
|
| 1861 | + IShare::TYPE_GROUP, |
|
| 1862 | + IShare::TYPE_LINK, |
|
| 1863 | + IShare::TYPE_EMAIL, |
|
| 1864 | + IShare::TYPE_CIRCLE, |
|
| 1865 | + IShare::TYPE_ROOM, |
|
| 1866 | + IShare::TYPE_DECK, |
|
| 1867 | + IShare::TYPE_SCIENCEMESH |
|
| 1868 | + ]; |
|
| 1869 | + |
|
| 1870 | + // Should we assume that the (currentUser) viewer is the owner of the node !? |
|
| 1871 | + $shares = []; |
|
| 1872 | + foreach ($providers as $provider) { |
|
| 1873 | + if (!$this->shareManager->shareProviderExists($provider)) { |
|
| 1874 | + continue; |
|
| 1875 | + } |
|
| 1876 | + |
|
| 1877 | + $providerShares = |
|
| 1878 | + $this->shareManager->getSharesBy($viewer, $provider, $node, $reShares, -1, 0); |
|
| 1879 | + $shares = array_merge($shares, $providerShares); |
|
| 1880 | + } |
|
| 1881 | + |
|
| 1882 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 1883 | + $federatedShares = $this->shareManager->getSharesBy( |
|
| 1884 | + $this->userId, IShare::TYPE_REMOTE, $node, $reShares, -1, 0 |
|
| 1885 | + ); |
|
| 1886 | + $shares = array_merge($shares, $federatedShares); |
|
| 1887 | + } |
|
| 1888 | + |
|
| 1889 | + if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 1890 | + $federatedShares = $this->shareManager->getSharesBy( |
|
| 1891 | + $this->userId, IShare::TYPE_REMOTE_GROUP, $node, $reShares, -1, 0 |
|
| 1892 | + ); |
|
| 1893 | + $shares = array_merge($shares, $federatedShares); |
|
| 1894 | + } |
|
| 1895 | + |
|
| 1896 | + return $shares; |
|
| 1897 | + } |
|
| 1898 | + |
|
| 1899 | + |
|
| 1900 | + /** |
|
| 1901 | + * @param Node $node |
|
| 1902 | + * |
|
| 1903 | + * @throws SharingRightsException |
|
| 1904 | + */ |
|
| 1905 | + private function confirmSharingRights(Node $node): void { |
|
| 1906 | + if (!$this->hasResharingRights($this->userId, $node)) { |
|
| 1907 | + throw new SharingRightsException($this->l->t('No sharing rights on this item')); |
|
| 1908 | + } |
|
| 1909 | + } |
|
| 1910 | + |
|
| 1911 | + |
|
| 1912 | + /** |
|
| 1913 | + * @param string $viewer |
|
| 1914 | + * @param Node $node |
|
| 1915 | + * |
|
| 1916 | + * @return bool |
|
| 1917 | + */ |
|
| 1918 | + private function hasResharingRights($viewer, $node): bool { |
|
| 1919 | + if ($viewer === $node->getOwner()->getUID()) { |
|
| 1920 | + return true; |
|
| 1921 | + } |
|
| 1922 | + |
|
| 1923 | + foreach ([$node, $node->getParent()] as $node) { |
|
| 1924 | + $shares = $this->getSharesFromNode($viewer, $node, true); |
|
| 1925 | + foreach ($shares as $share) { |
|
| 1926 | + try { |
|
| 1927 | + if ($this->shareProviderResharingRights($viewer, $share, $node)) { |
|
| 1928 | + return true; |
|
| 1929 | + } |
|
| 1930 | + } catch (InvalidPathException|NotFoundException $e) { |
|
| 1931 | + } |
|
| 1932 | + } |
|
| 1933 | + } |
|
| 1934 | + |
|
| 1935 | + return false; |
|
| 1936 | + } |
|
| 1937 | + |
|
| 1938 | + |
|
| 1939 | + /** |
|
| 1940 | + * Returns if we can find resharing rights in an IShare object for a specific user. |
|
| 1941 | + * |
|
| 1942 | + * @suppress PhanUndeclaredClassMethod |
|
| 1943 | + * |
|
| 1944 | + * @param string $userId |
|
| 1945 | + * @param IShare $share |
|
| 1946 | + * @param Node $node |
|
| 1947 | + * |
|
| 1948 | + * @return bool |
|
| 1949 | + * @throws NotFoundException |
|
| 1950 | + * @throws InvalidPathException |
|
| 1951 | + */ |
|
| 1952 | + private function shareProviderResharingRights(string $userId, IShare $share, $node): bool { |
|
| 1953 | + if ($share->getShareOwner() === $userId) { |
|
| 1954 | + return true; |
|
| 1955 | + } |
|
| 1956 | + |
|
| 1957 | + // we check that current user have parent resharing rights on the current file |
|
| 1958 | + if ($node !== null && ($node->getPermissions() & Constants::PERMISSION_SHARE) !== 0) { |
|
| 1959 | + return true; |
|
| 1960 | + } |
|
| 1961 | + |
|
| 1962 | + if ((Constants::PERMISSION_SHARE & $share->getPermissions()) === 0) { |
|
| 1963 | + return false; |
|
| 1964 | + } |
|
| 1965 | + |
|
| 1966 | + if ($share->getShareType() === IShare::TYPE_USER && $share->getSharedWith() === $userId) { |
|
| 1967 | + return true; |
|
| 1968 | + } |
|
| 1969 | + |
|
| 1970 | + if ($share->getShareType() === IShare::TYPE_GROUP && $this->groupManager->isInGroup($userId, $share->getSharedWith())) { |
|
| 1971 | + return true; |
|
| 1972 | + } |
|
| 1973 | + |
|
| 1974 | + if ($share->getShareType() === IShare::TYPE_CIRCLE && Server::get(IAppManager::class)->isEnabledForUser('circles') |
|
| 1975 | + && class_exists('\OCA\Circles\Api\v1\Circles')) { |
|
| 1976 | + $hasCircleId = (str_ends_with($share->getSharedWith(), ']')); |
|
| 1977 | + $shareWithStart = ($hasCircleId ? strrpos($share->getSharedWith(), '[') + 1 : 0); |
|
| 1978 | + $shareWithLength = ($hasCircleId ? -1 : strpos($share->getSharedWith(), ' ')); |
|
| 1979 | + if ($shareWithLength === false) { |
|
| 1980 | + $sharedWith = substr($share->getSharedWith(), $shareWithStart); |
|
| 1981 | + } else { |
|
| 1982 | + $sharedWith = substr($share->getSharedWith(), $shareWithStart, $shareWithLength); |
|
| 1983 | + } |
|
| 1984 | + try { |
|
| 1985 | + $member = Circles::getMember($sharedWith, $userId, 1); |
|
| 1986 | + if ($member->getLevel() >= 4) { |
|
| 1987 | + return true; |
|
| 1988 | + } |
|
| 1989 | + return false; |
|
| 1990 | + } catch (ContainerExceptionInterface $e) { |
|
| 1991 | + return false; |
|
| 1992 | + } |
|
| 1993 | + } |
|
| 1994 | + |
|
| 1995 | + return false; |
|
| 1996 | + } |
|
| 1997 | + |
|
| 1998 | + /** |
|
| 1999 | + * Get all the shares for the current user |
|
| 2000 | + * |
|
| 2001 | + * @param Node|null $path |
|
| 2002 | + * @param boolean $reshares |
|
| 2003 | + * @return IShare[] |
|
| 2004 | + */ |
|
| 2005 | + private function getAllShares(?Node $path = null, bool $reshares = false) { |
|
| 2006 | + // Get all shares |
|
| 2007 | + $userShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_USER, $path, $reshares, -1, 0); |
|
| 2008 | + $groupShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_GROUP, $path, $reshares, -1, 0); |
|
| 2009 | + $linkShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_LINK, $path, $reshares, -1, 0); |
|
| 2010 | + |
|
| 2011 | + // EMAIL SHARES |
|
| 2012 | + $mailShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_EMAIL, $path, $reshares, -1, 0); |
|
| 2013 | + |
|
| 2014 | + // TEAM SHARES |
|
| 2015 | + $circleShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_CIRCLE, $path, $reshares, -1, 0); |
|
| 2016 | + |
|
| 2017 | + // TALK SHARES |
|
| 2018 | + $roomShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_ROOM, $path, $reshares, -1, 0); |
|
| 2019 | + |
|
| 2020 | + // DECK SHARES |
|
| 2021 | + $deckShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_DECK, $path, $reshares, -1, 0); |
|
| 2022 | + |
|
| 2023 | + // SCIENCEMESH SHARES |
|
| 2024 | + $sciencemeshShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_SCIENCEMESH, $path, $reshares, -1, 0); |
|
| 2025 | + |
|
| 2026 | + // FEDERATION |
|
| 2027 | + if ($this->shareManager->outgoingServer2ServerSharesAllowed()) { |
|
| 2028 | + $federatedShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE, $path, $reshares, -1, 0); |
|
| 2029 | + } else { |
|
| 2030 | + $federatedShares = []; |
|
| 2031 | + } |
|
| 2032 | + if ($this->shareManager->outgoingServer2ServerGroupSharesAllowed()) { |
|
| 2033 | + $federatedGroupShares = $this->shareManager->getSharesBy($this->userId, IShare::TYPE_REMOTE_GROUP, $path, $reshares, -1, 0); |
|
| 2034 | + } else { |
|
| 2035 | + $federatedGroupShares = []; |
|
| 2036 | + } |
|
| 2037 | + |
|
| 2038 | + return array_merge($userShares, $groupShares, $linkShares, $mailShares, $circleShares, $roomShares, $deckShares, $sciencemeshShares, $federatedShares, $federatedGroupShares); |
|
| 2039 | + } |
|
| 2040 | + |
|
| 2041 | + |
|
| 2042 | + /** |
|
| 2043 | + * merging already formatted shares. |
|
| 2044 | + * We'll make an associative array to easily detect duplicate Ids. |
|
| 2045 | + * Keys _needs_ to be removed after all shares are retrieved and merged. |
|
| 2046 | + * |
|
| 2047 | + * @param array $shares |
|
| 2048 | + * @param array $newShares |
|
| 2049 | + */ |
|
| 2050 | + private function mergeFormattedShares(array &$shares, array $newShares) { |
|
| 2051 | + foreach ($newShares as $newShare) { |
|
| 2052 | + if (!array_key_exists($newShare['id'], $shares)) { |
|
| 2053 | + $shares[$newShare['id']] = $newShare; |
|
| 2054 | + } |
|
| 2055 | + } |
|
| 2056 | + } |
|
| 2057 | + |
|
| 2058 | + /** |
|
| 2059 | + * @param IShare $share |
|
| 2060 | + * @param string|null $attributesString |
|
| 2061 | + * @return IShare modified share |
|
| 2062 | + */ |
|
| 2063 | + private function setShareAttributes(IShare $share, ?string $attributesString) { |
|
| 2064 | + $newShareAttributes = null; |
|
| 2065 | + if ($attributesString !== null) { |
|
| 2066 | + $newShareAttributes = $this->shareManager->newShare()->newAttributes(); |
|
| 2067 | + $formattedShareAttributes = \json_decode($attributesString, true); |
|
| 2068 | + if (is_array($formattedShareAttributes)) { |
|
| 2069 | + foreach ($formattedShareAttributes as $formattedAttr) { |
|
| 2070 | + $newShareAttributes->setAttribute( |
|
| 2071 | + $formattedAttr['scope'], |
|
| 2072 | + $formattedAttr['key'], |
|
| 2073 | + $formattedAttr['value'], |
|
| 2074 | + ); |
|
| 2075 | + } |
|
| 2076 | + } else { |
|
| 2077 | + throw new OCSBadRequestException($this->l->t('Invalid share attributes provided: "%s"', [$attributesString])); |
|
| 2078 | + } |
|
| 2079 | + } |
|
| 2080 | + $share->setAttributes($newShareAttributes); |
|
| 2081 | + |
|
| 2082 | + return $share; |
|
| 2083 | + } |
|
| 2084 | + |
|
| 2085 | + private function checkInheritedAttributes(IShare $share): void { |
|
| 2086 | + if (!$share->getSharedBy()) { |
|
| 2087 | + return; // Probably in a test |
|
| 2088 | + } |
|
| 2089 | + |
|
| 2090 | + $canDownload = false; |
|
| 2091 | + $hideDownload = true; |
|
| 2092 | + |
|
| 2093 | + $userFolder = $this->rootFolder->getUserFolder($share->getSharedBy()); |
|
| 2094 | + $nodes = $userFolder->getById($share->getNodeId()); |
|
| 2095 | + foreach ($nodes as $node) { |
|
| 2096 | + // Owner always can download it - so allow it and break |
|
| 2097 | + if ($node->getOwner()?->getUID() === $share->getSharedBy()) { |
|
| 2098 | + $canDownload = true; |
|
| 2099 | + $hideDownload = false; |
|
| 2100 | + break; |
|
| 2101 | + } |
|
| 2102 | + |
|
| 2103 | + if ($node->getStorage()->instanceOfStorage(SharedStorage::class)) { |
|
| 2104 | + $storage = $node->getStorage(); |
|
| 2105 | + if ($storage instanceof Wrapper) { |
|
| 2106 | + $storage = $storage->getInstanceOfStorage(SharedStorage::class); |
|
| 2107 | + if ($storage === null) { |
|
| 2108 | + throw new \RuntimeException('Should not happen, instanceOfStorage but getInstanceOfStorage return null'); |
|
| 2109 | + } |
|
| 2110 | + } else { |
|
| 2111 | + throw new \RuntimeException('Should not happen, instanceOfStorage but not a wrapper'); |
|
| 2112 | + } |
|
| 2113 | + |
|
| 2114 | + /** @var SharedStorage $storage */ |
|
| 2115 | + $originalShare = $storage->getShare(); |
|
| 2116 | + $inheritedAttributes = $originalShare->getAttributes(); |
|
| 2117 | + // hide if hidden and also the current share enforces hide (can only be false if one share is false or user is owner) |
|
| 2118 | + $hideDownload = $hideDownload && $originalShare->getHideDownload(); |
|
| 2119 | + // allow download if already allowed by previous share or when the current share allows downloading |
|
| 2120 | + $canDownload = $canDownload || $inheritedAttributes === null || $inheritedAttributes->getAttribute('permissions', 'download') !== false; |
|
| 2121 | + } |
|
| 2122 | + } |
|
| 2123 | + |
|
| 2124 | + if ($hideDownload || !$canDownload) { |
|
| 2125 | + $share->setHideDownload(true); |
|
| 2126 | + |
|
| 2127 | + if (!$canDownload) { |
|
| 2128 | + $attributes = $share->getAttributes() ?? $share->newAttributes(); |
|
| 2129 | + $attributes->setAttribute('permissions', 'download', false); |
|
| 2130 | + $share->setAttributes($attributes); |
|
| 2131 | + } |
|
| 2132 | + } |
|
| 2133 | + } |
|
| 2134 | + |
|
| 2135 | + /** |
|
| 2136 | + * Send a mail notification again for a share. |
|
| 2137 | + * The mail_send option must be enabled for the given share. |
|
| 2138 | + * @param string $id the share ID |
|
| 2139 | + * @param string $password the password to check against. Necessary for password protected shares. |
|
| 2140 | + * @throws OCSNotFoundException Share not found |
|
| 2141 | + * @throws OCSForbiddenException You are not allowed to send mail notifications |
|
| 2142 | + * @throws OCSBadRequestException Invalid request or wrong password |
|
| 2143 | + * @throws OCSException Error while sending mail notification |
|
| 2144 | + * @return DataResponse<Http::STATUS_OK, list<empty>, array{}> |
|
| 2145 | + * |
|
| 2146 | + * 200: The email notification was sent successfully |
|
| 2147 | + */ |
|
| 2148 | + #[NoAdminRequired] |
|
| 2149 | + #[UserRateLimit(limit: 10, period: 600)] |
|
| 2150 | + public function sendShareEmail(string $id, $password = ''): DataResponse { |
|
| 2151 | + try { |
|
| 2152 | + $share = $this->getShareById($id); |
|
| 2153 | + |
|
| 2154 | + if (!$this->canAccessShare($share, false)) { |
|
| 2155 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 2156 | + } |
|
| 2157 | + |
|
| 2158 | + if (!$this->canEditShare($share)) { |
|
| 2159 | + throw new OCSForbiddenException($this->l->t('You are not allowed to send mail notifications')); |
|
| 2160 | + } |
|
| 2161 | + |
|
| 2162 | + // For mail and link shares, the user must be |
|
| 2163 | + // the owner of the share, not only the file owner. |
|
| 2164 | + if ($share->getShareType() === IShare::TYPE_EMAIL |
|
| 2165 | + || $share->getShareType() === IShare::TYPE_LINK) { |
|
| 2166 | + if ($share->getSharedBy() !== $this->userId) { |
|
| 2167 | + throw new OCSForbiddenException($this->l->t('You are not allowed to send mail notifications')); |
|
| 2168 | + } |
|
| 2169 | + } |
|
| 2170 | + |
|
| 2171 | + try { |
|
| 2172 | + $provider = $this->factory->getProviderForType($share->getShareType()); |
|
| 2173 | + if (!($provider instanceof IShareProviderWithNotification)) { |
|
| 2174 | + throw new OCSBadRequestException($this->l->t('No mail notification configured for this share type')); |
|
| 2175 | + } |
|
| 2176 | + |
|
| 2177 | + // Circumvent the password encrypted data by |
|
| 2178 | + // setting the password clear. We're not storing |
|
| 2179 | + // the password clear, it is just a temporary |
|
| 2180 | + // object manipulation. The password will stay |
|
| 2181 | + // encrypted in the database. |
|
| 2182 | + if ($share->getPassword() !== null && $share->getPassword() !== $password) { |
|
| 2183 | + if (!$this->shareManager->checkPassword($share, $password)) { |
|
| 2184 | + throw new OCSBadRequestException($this->l->t('Wrong password')); |
|
| 2185 | + } |
|
| 2186 | + $share = $share->setPassword($password); |
|
| 2187 | + } |
|
| 2188 | + |
|
| 2189 | + $provider->sendMailNotification($share); |
|
| 2190 | + return new DataResponse(); |
|
| 2191 | + } catch (Exception $e) { |
|
| 2192 | + $this->logger->error($e->getMessage(), ['exception' => $e]); |
|
| 2193 | + throw new OCSException($this->l->t('Error while sending mail notification')); |
|
| 2194 | + } |
|
| 2195 | + |
|
| 2196 | + } catch (ShareNotFound $e) { |
|
| 2197 | + throw new OCSNotFoundException($this->l->t('Wrong share ID, share does not exist')); |
|
| 2198 | + } |
|
| 2199 | + } |
|
| 2200 | + |
|
| 2201 | + /** |
|
| 2202 | + * Get a unique share token |
|
| 2203 | + * |
|
| 2204 | + * @throws OCSException Failed to generate a unique token |
|
| 2205 | + * |
|
| 2206 | + * @return DataResponse<Http::STATUS_OK, array{token: string}, array{}> |
|
| 2207 | + * |
|
| 2208 | + * 200: Token generated successfully |
|
| 2209 | + */ |
|
| 2210 | + #[ApiRoute(verb: 'GET', url: '/api/v1/token')] |
|
| 2211 | + #[NoAdminRequired] |
|
| 2212 | + public function generateToken(): DataResponse { |
|
| 2213 | + try { |
|
| 2214 | + $token = $this->shareManager->generateToken(); |
|
| 2215 | + return new DataResponse([ |
|
| 2216 | + 'token' => $token, |
|
| 2217 | + ]); |
|
| 2218 | + } catch (ShareTokenException $e) { |
|
| 2219 | + throw new OCSException($this->l->t('Failed to generate a unique token')); |
|
| 2220 | + } |
|
| 2221 | + } |
|
| 2222 | 2222 | } |
@@ -38,223 +38,223 @@ |
||
| 38 | 38 | |
| 39 | 39 | class DefaultPublicShareTemplateProvider implements IPublicShareTemplateProvider { |
| 40 | 40 | |
| 41 | - public function __construct( |
|
| 42 | - private IUserManager $userManager, |
|
| 43 | - private IAccountManager $accountManager, |
|
| 44 | - private IPreview $previewManager, |
|
| 45 | - protected FederatedShareProvider $federatedShareProvider, |
|
| 46 | - private IUrlGenerator $urlGenerator, |
|
| 47 | - private IEventDispatcher $eventDispatcher, |
|
| 48 | - private IL10N $l10n, |
|
| 49 | - private Defaults $defaults, |
|
| 50 | - private IConfig $config, |
|
| 51 | - private IRequest $request, |
|
| 52 | - private IInitialState $initialState, |
|
| 53 | - private IAppConfig $appConfig, |
|
| 54 | - ) { |
|
| 55 | - } |
|
| 41 | + public function __construct( |
|
| 42 | + private IUserManager $userManager, |
|
| 43 | + private IAccountManager $accountManager, |
|
| 44 | + private IPreview $previewManager, |
|
| 45 | + protected FederatedShareProvider $federatedShareProvider, |
|
| 46 | + private IUrlGenerator $urlGenerator, |
|
| 47 | + private IEventDispatcher $eventDispatcher, |
|
| 48 | + private IL10N $l10n, |
|
| 49 | + private Defaults $defaults, |
|
| 50 | + private IConfig $config, |
|
| 51 | + private IRequest $request, |
|
| 52 | + private IInitialState $initialState, |
|
| 53 | + private IAppConfig $appConfig, |
|
| 54 | + ) { |
|
| 55 | + } |
|
| 56 | 56 | |
| 57 | - public function shouldRespond(IShare $share): bool { |
|
| 58 | - return true; |
|
| 59 | - } |
|
| 57 | + public function shouldRespond(IShare $share): bool { |
|
| 58 | + return true; |
|
| 59 | + } |
|
| 60 | 60 | |
| 61 | - public function renderPage(IShare $share, string $token, string $path): TemplateResponse { |
|
| 62 | - $shareNode = $share->getNode(); |
|
| 63 | - $ownerName = ''; |
|
| 64 | - $ownerId = ''; |
|
| 61 | + public function renderPage(IShare $share, string $token, string $path): TemplateResponse { |
|
| 62 | + $shareNode = $share->getNode(); |
|
| 63 | + $ownerName = ''; |
|
| 64 | + $ownerId = ''; |
|
| 65 | 65 | |
| 66 | - // Only make the share owner public if they allowed to show their name |
|
| 67 | - $owner = $this->userManager->get($share->getShareOwner()); |
|
| 68 | - if ($owner instanceof IUser) { |
|
| 69 | - $ownerAccount = $this->accountManager->getAccount($owner); |
|
| 66 | + // Only make the share owner public if they allowed to show their name |
|
| 67 | + $owner = $this->userManager->get($share->getShareOwner()); |
|
| 68 | + if ($owner instanceof IUser) { |
|
| 69 | + $ownerAccount = $this->accountManager->getAccount($owner); |
|
| 70 | 70 | |
| 71 | - $ownerNameProperty = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME); |
|
| 72 | - if ($ownerNameProperty->getScope() === IAccountManager::SCOPE_PUBLISHED) { |
|
| 73 | - $ownerName = $owner->getDisplayName(); |
|
| 74 | - $ownerId = $owner->getUID(); |
|
| 75 | - } |
|
| 76 | - } |
|
| 71 | + $ownerNameProperty = $ownerAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME); |
|
| 72 | + if ($ownerNameProperty->getScope() === IAccountManager::SCOPE_PUBLISHED) { |
|
| 73 | + $ownerName = $owner->getDisplayName(); |
|
| 74 | + $ownerId = $owner->getUID(); |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | - $view = 'public-share'; |
|
| 79 | - if ($shareNode instanceof File) { |
|
| 80 | - $view = 'public-file-share'; |
|
| 81 | - $this->initialState->provideInitialState('fileId', $shareNode->getId()); |
|
| 82 | - } elseif (($share->getPermissions() & Constants::PERMISSION_CREATE) |
|
| 83 | - && !($share->getPermissions() & Constants::PERMISSION_READ) |
|
| 84 | - ) { |
|
| 85 | - // share is a folder with create but no read permissions -> file drop only |
|
| 86 | - $view = 'public-file-drop'; |
|
| 87 | - // Only needed for file drops |
|
| 88 | - $this->initialState->provideInitialState( |
|
| 89 | - 'disclaimer', |
|
| 90 | - $this->appConfig->getValueString('core', 'shareapi_public_link_disclaimertext'), |
|
| 91 | - ); |
|
| 92 | - } |
|
| 93 | - // Set up initial state |
|
| 94 | - $this->initialState->provideInitialState('isPublic', true); |
|
| 95 | - $this->initialState->provideInitialState('sharingToken', $token); |
|
| 96 | - $this->initialState->provideInitialState('sharePermissions', $share->getPermissions()); |
|
| 97 | - $this->initialState->provideInitialState('filename', $shareNode->getName()); |
|
| 98 | - $this->initialState->provideInitialState('view', $view); |
|
| 78 | + $view = 'public-share'; |
|
| 79 | + if ($shareNode instanceof File) { |
|
| 80 | + $view = 'public-file-share'; |
|
| 81 | + $this->initialState->provideInitialState('fileId', $shareNode->getId()); |
|
| 82 | + } elseif (($share->getPermissions() & Constants::PERMISSION_CREATE) |
|
| 83 | + && !($share->getPermissions() & Constants::PERMISSION_READ) |
|
| 84 | + ) { |
|
| 85 | + // share is a folder with create but no read permissions -> file drop only |
|
| 86 | + $view = 'public-file-drop'; |
|
| 87 | + // Only needed for file drops |
|
| 88 | + $this->initialState->provideInitialState( |
|
| 89 | + 'disclaimer', |
|
| 90 | + $this->appConfig->getValueString('core', 'shareapi_public_link_disclaimertext'), |
|
| 91 | + ); |
|
| 92 | + } |
|
| 93 | + // Set up initial state |
|
| 94 | + $this->initialState->provideInitialState('isPublic', true); |
|
| 95 | + $this->initialState->provideInitialState('sharingToken', $token); |
|
| 96 | + $this->initialState->provideInitialState('sharePermissions', $share->getPermissions()); |
|
| 97 | + $this->initialState->provideInitialState('filename', $shareNode->getName()); |
|
| 98 | + $this->initialState->provideInitialState('view', $view); |
|
| 99 | 99 | |
| 100 | - // Load scripts and styles for UI |
|
| 101 | - Util::addInitScript('files', 'init'); |
|
| 102 | - Util::addInitScript(Application::APP_ID, 'init'); |
|
| 103 | - Util::addInitScript(Application::APP_ID, 'init-public'); |
|
| 104 | - Util::addScript('files', 'main'); |
|
| 100 | + // Load scripts and styles for UI |
|
| 101 | + Util::addInitScript('files', 'init'); |
|
| 102 | + Util::addInitScript(Application::APP_ID, 'init'); |
|
| 103 | + Util::addInitScript(Application::APP_ID, 'init-public'); |
|
| 104 | + Util::addScript('files', 'main'); |
|
| 105 | 105 | |
| 106 | - // Add file-request script if needed |
|
| 107 | - $attributes = $share->getAttributes(); |
|
| 108 | - $isFileRequest = $attributes?->getAttribute('fileRequest', 'enabled') === true; |
|
| 109 | - if ($isFileRequest) { |
|
| 110 | - Util::addScript(Application::APP_ID, 'public-file-request'); |
|
| 111 | - } |
|
| 106 | + // Add file-request script if needed |
|
| 107 | + $attributes = $share->getAttributes(); |
|
| 108 | + $isFileRequest = $attributes?->getAttribute('fileRequest', 'enabled') === true; |
|
| 109 | + if ($isFileRequest) { |
|
| 110 | + Util::addScript(Application::APP_ID, 'public-file-request'); |
|
| 111 | + } |
|
| 112 | 112 | |
| 113 | - // Load Viewer scripts |
|
| 114 | - if (class_exists(LoadViewer::class)) { |
|
| 115 | - $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 116 | - } |
|
| 113 | + // Load Viewer scripts |
|
| 114 | + if (class_exists(LoadViewer::class)) { |
|
| 115 | + $this->eventDispatcher->dispatchTyped(new LoadViewer()); |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - // Allow external apps to register their scripts |
|
| 119 | - $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share)); |
|
| 118 | + // Allow external apps to register their scripts |
|
| 119 | + $this->eventDispatcher->dispatchTyped(new BeforeTemplateRenderedEvent($share)); |
|
| 120 | 120 | |
| 121 | - $this->addMetaHeaders($share); |
|
| 121 | + $this->addMetaHeaders($share); |
|
| 122 | 122 | |
| 123 | - // CSP to allow office |
|
| 124 | - $csp = new ContentSecurityPolicy(); |
|
| 125 | - $csp->addAllowedFrameDomain('\'self\''); |
|
| 123 | + // CSP to allow office |
|
| 124 | + $csp = new ContentSecurityPolicy(); |
|
| 125 | + $csp->addAllowedFrameDomain('\'self\''); |
|
| 126 | 126 | |
| 127 | - $response = new PublicTemplateResponse( |
|
| 128 | - 'files', |
|
| 129 | - 'index', |
|
| 130 | - ); |
|
| 131 | - $response->setContentSecurityPolicy($csp); |
|
| 127 | + $response = new PublicTemplateResponse( |
|
| 128 | + 'files', |
|
| 129 | + 'index', |
|
| 130 | + ); |
|
| 131 | + $response->setContentSecurityPolicy($csp); |
|
| 132 | 132 | |
| 133 | - // If the share has a label, use it as the title |
|
| 134 | - if ($share->getLabel() !== '') { |
|
| 135 | - $response->setHeaderTitle($share->getLabel()); |
|
| 136 | - $response->setParams(['pageTitle' => $share->getLabel()]); |
|
| 137 | - } else { |
|
| 138 | - $response->setHeaderTitle($shareNode->getName()); |
|
| 139 | - $response->setParams(['pageTitle' => $shareNode->getName()]); |
|
| 140 | - } |
|
| 133 | + // If the share has a label, use it as the title |
|
| 134 | + if ($share->getLabel() !== '') { |
|
| 135 | + $response->setHeaderTitle($share->getLabel()); |
|
| 136 | + $response->setParams(['pageTitle' => $share->getLabel()]); |
|
| 137 | + } else { |
|
| 138 | + $response->setHeaderTitle($shareNode->getName()); |
|
| 139 | + $response->setParams(['pageTitle' => $shareNode->getName()]); |
|
| 140 | + } |
|
| 141 | 141 | |
| 142 | - if ($ownerName !== '') { |
|
| 143 | - $response->setHeaderDetails($this->l10n->t('shared by %s', [$ownerName])); |
|
| 144 | - } |
|
| 142 | + if ($ownerName !== '') { |
|
| 143 | + $response->setHeaderDetails($this->l10n->t('shared by %s', [$ownerName])); |
|
| 144 | + } |
|
| 145 | 145 | |
| 146 | - // Create the header action menu |
|
| 147 | - $headerActions = []; |
|
| 148 | - if ($view !== 'public-file-drop' && !$share->getHideDownload()) { |
|
| 149 | - // The download URL is used for the "download" header action as well as in some cases for the direct link |
|
| 150 | - $downloadUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', [ |
|
| 151 | - 'token' => $token, |
|
| 152 | - 'filename' => ($shareNode instanceof File) ? $shareNode->getName() : null, |
|
| 153 | - ]); |
|
| 146 | + // Create the header action menu |
|
| 147 | + $headerActions = []; |
|
| 148 | + if ($view !== 'public-file-drop' && !$share->getHideDownload()) { |
|
| 149 | + // The download URL is used for the "download" header action as well as in some cases for the direct link |
|
| 150 | + $downloadUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadShare', [ |
|
| 151 | + 'token' => $token, |
|
| 152 | + 'filename' => ($shareNode instanceof File) ? $shareNode->getName() : null, |
|
| 153 | + ]); |
|
| 154 | 154 | |
| 155 | - // If not a file drop, then add the download header action |
|
| 156 | - $headerActions[] = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 0, (string)$shareNode->getSize()); |
|
| 155 | + // If not a file drop, then add the download header action |
|
| 156 | + $headerActions[] = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 0, (string)$shareNode->getSize()); |
|
| 157 | 157 | |
| 158 | - // If remote sharing is enabled also add the remote share action to the menu |
|
| 159 | - if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
| 160 | - $headerActions[] = new ExternalShareMenuAction( |
|
| 161 | - // TRANSLATORS The placeholder refers to the software product name as in 'Add to your Nextcloud' |
|
| 162 | - $this->l10n->t('Add to your %s', [$this->defaults->getProductName()]), |
|
| 163 | - 'icon-external', |
|
| 164 | - $ownerId, |
|
| 165 | - $ownerName, |
|
| 166 | - $shareNode->getName(), |
|
| 167 | - ); |
|
| 168 | - } |
|
| 169 | - } |
|
| 158 | + // If remote sharing is enabled also add the remote share action to the menu |
|
| 159 | + if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
|
| 160 | + $headerActions[] = new ExternalShareMenuAction( |
|
| 161 | + // TRANSLATORS The placeholder refers to the software product name as in 'Add to your Nextcloud' |
|
| 162 | + $this->l10n->t('Add to your %s', [$this->defaults->getProductName()]), |
|
| 163 | + 'icon-external', |
|
| 164 | + $ownerId, |
|
| 165 | + $ownerName, |
|
| 166 | + $shareNode->getName(), |
|
| 167 | + ); |
|
| 168 | + } |
|
| 169 | + } |
|
| 170 | 170 | |
| 171 | - $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 172 | - // By default use the share link as the direct link |
|
| 173 | - $directLink = $shareUrl; |
|
| 174 | - // Add the direct link header actions |
|
| 175 | - if ($shareNode->getMimePart() === 'image') { |
|
| 176 | - // If this is a file and especially an image directly point to the image preview |
|
| 177 | - $directLink = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 178 | - } elseif (($share->getPermissions() & Constants::PERMISSION_READ) && !$share->getHideDownload()) { |
|
| 179 | - // Can read and no download restriction, so just download it |
|
| 180 | - $directLink = $downloadUrl ?? $shareUrl; |
|
| 181 | - } |
|
| 182 | - $headerActions[] = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $directLink); |
|
| 183 | - $response->setHeaderActions($headerActions); |
|
| 171 | + $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 172 | + // By default use the share link as the direct link |
|
| 173 | + $directLink = $shareUrl; |
|
| 174 | + // Add the direct link header actions |
|
| 175 | + if ($shareNode->getMimePart() === 'image') { |
|
| 176 | + // If this is a file and especially an image directly point to the image preview |
|
| 177 | + $directLink = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 178 | + } elseif (($share->getPermissions() & Constants::PERMISSION_READ) && !$share->getHideDownload()) { |
|
| 179 | + // Can read and no download restriction, so just download it |
|
| 180 | + $directLink = $downloadUrl ?? $shareUrl; |
|
| 181 | + } |
|
| 182 | + $headerActions[] = new LinkMenuAction($this->l10n->t('Direct link'), 'icon-public', $directLink); |
|
| 183 | + $response->setHeaderActions($headerActions); |
|
| 184 | 184 | |
| 185 | - return $response; |
|
| 186 | - } |
|
| 185 | + return $response; |
|
| 186 | + } |
|
| 187 | 187 | |
| 188 | - /** |
|
| 189 | - * Add OpenGraph headers to response for preview |
|
| 190 | - * @param IShare $share The share for which to add the headers |
|
| 191 | - */ |
|
| 192 | - protected function addMetaHeaders(IShare $share): void { |
|
| 193 | - $shareNode = $share->getNode(); |
|
| 194 | - $token = $share->getToken(); |
|
| 195 | - $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 188 | + /** |
|
| 189 | + * Add OpenGraph headers to response for preview |
|
| 190 | + * @param IShare $share The share for which to add the headers |
|
| 191 | + */ |
|
| 192 | + protected function addMetaHeaders(IShare $share): void { |
|
| 193 | + $shareNode = $share->getNode(); |
|
| 194 | + $token = $share->getToken(); |
|
| 195 | + $shareUrl = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.showShare', ['token' => $token]); |
|
| 196 | 196 | |
| 197 | - // Handle preview generation for OpenGraph |
|
| 198 | - $hasImagePreview = false; |
|
| 199 | - if ($this->previewManager->isMimeSupported($shareNode->getMimetype())) { |
|
| 200 | - // For images we can use direct links |
|
| 201 | - if ($shareNode->getMimePart() === 'image') { |
|
| 202 | - $hasImagePreview = true; |
|
| 203 | - $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 204 | - // Whatsapp is kind of picky about their size requirements |
|
| 205 | - if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 206 | - $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 207 | - 'token' => $token, |
|
| 208 | - 'x' => 256, |
|
| 209 | - 'y' => 256, |
|
| 210 | - 'a' => true, |
|
| 211 | - ]); |
|
| 212 | - } |
|
| 213 | - } else { |
|
| 214 | - // For normal files use preview API |
|
| 215 | - $ogPreview = $this->urlGenerator->linkToRouteAbsolute( |
|
| 216 | - 'files_sharing.PublicPreview.getPreview', |
|
| 217 | - [ |
|
| 218 | - 'x' => 256, |
|
| 219 | - 'y' => 256, |
|
| 220 | - 'file' => $share->getTarget(), |
|
| 221 | - 'token' => $token, |
|
| 222 | - ], |
|
| 223 | - ); |
|
| 224 | - } |
|
| 225 | - } else { |
|
| 226 | - // No preview supported, so we just add the favicon |
|
| 227 | - $ogPreview = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 228 | - } |
|
| 197 | + // Handle preview generation for OpenGraph |
|
| 198 | + $hasImagePreview = false; |
|
| 199 | + if ($this->previewManager->isMimeSupported($shareNode->getMimetype())) { |
|
| 200 | + // For images we can use direct links |
|
| 201 | + if ($shareNode->getMimePart() === 'image') { |
|
| 202 | + $hasImagePreview = true; |
|
| 203 | + $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.publicpreview.directLink', ['token' => $token]); |
|
| 204 | + // Whatsapp is kind of picky about their size requirements |
|
| 205 | + if ($this->request->isUserAgent(['/^WhatsApp/'])) { |
|
| 206 | + $ogPreview = $this->urlGenerator->linkToRouteAbsolute('files_sharing.PublicPreview.getPreview', [ |
|
| 207 | + 'token' => $token, |
|
| 208 | + 'x' => 256, |
|
| 209 | + 'y' => 256, |
|
| 210 | + 'a' => true, |
|
| 211 | + ]); |
|
| 212 | + } |
|
| 213 | + } else { |
|
| 214 | + // For normal files use preview API |
|
| 215 | + $ogPreview = $this->urlGenerator->linkToRouteAbsolute( |
|
| 216 | + 'files_sharing.PublicPreview.getPreview', |
|
| 217 | + [ |
|
| 218 | + 'x' => 256, |
|
| 219 | + 'y' => 256, |
|
| 220 | + 'file' => $share->getTarget(), |
|
| 221 | + 'token' => $token, |
|
| 222 | + ], |
|
| 223 | + ); |
|
| 224 | + } |
|
| 225 | + } else { |
|
| 226 | + // No preview supported, so we just add the favicon |
|
| 227 | + $ogPreview = $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('core', 'favicon-fb.png')); |
|
| 228 | + } |
|
| 229 | 229 | |
| 230 | - $title = $shareNode->getName(); |
|
| 231 | - $siteName = $this->defaults->getName(); |
|
| 232 | - $description = $siteName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''); |
|
| 230 | + $title = $shareNode->getName(); |
|
| 231 | + $siteName = $this->defaults->getName(); |
|
| 232 | + $description = $siteName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''); |
|
| 233 | 233 | |
| 234 | - // OpenGraph Support: http://ogp.me/ |
|
| 235 | - Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]); |
|
| 236 | - Util::addHeader('meta', ['property' => 'og:description', 'content' => $description]); |
|
| 237 | - Util::addHeader('meta', ['property' => 'og:site_name', 'content' => $siteName]); |
|
| 238 | - Util::addHeader('meta', ['property' => 'og:url', 'content' => $shareUrl]); |
|
| 239 | - Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
| 240 | - Util::addHeader('meta', ['property' => 'og:image', 'content' => $ogPreview]); // recommended to always have the image |
|
| 241 | - if ($shareNode->getMimePart() === 'image') { |
|
| 242 | - Util::addHeader('meta', ['property' => 'og:image:type', 'content' => $shareNode->getMimeType()]); |
|
| 243 | - } elseif ($shareNode->getMimePart() === 'audio') { |
|
| 244 | - $audio = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadshare', ['token' => $token]); |
|
| 245 | - Util::addHeader('meta', ['property' => 'og:audio', 'content' => $audio]); |
|
| 246 | - Util::addHeader('meta', ['property' => 'og:audio:type', 'content' => $shareNode->getMimeType()]); |
|
| 247 | - } elseif ($shareNode->getMimePart() === 'video') { |
|
| 248 | - $video = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadshare', ['token' => $token]); |
|
| 249 | - Util::addHeader('meta', ['property' => 'og:video', 'content' => $video]); |
|
| 250 | - Util::addHeader('meta', ['property' => 'og:video:type', 'content' => $shareNode->getMimeType()]); |
|
| 251 | - } |
|
| 234 | + // OpenGraph Support: http://ogp.me/ |
|
| 235 | + Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]); |
|
| 236 | + Util::addHeader('meta', ['property' => 'og:description', 'content' => $description]); |
|
| 237 | + Util::addHeader('meta', ['property' => 'og:site_name', 'content' => $siteName]); |
|
| 238 | + Util::addHeader('meta', ['property' => 'og:url', 'content' => $shareUrl]); |
|
| 239 | + Util::addHeader('meta', ['property' => 'og:type', 'content' => 'website']); |
|
| 240 | + Util::addHeader('meta', ['property' => 'og:image', 'content' => $ogPreview]); // recommended to always have the image |
|
| 241 | + if ($shareNode->getMimePart() === 'image') { |
|
| 242 | + Util::addHeader('meta', ['property' => 'og:image:type', 'content' => $shareNode->getMimeType()]); |
|
| 243 | + } elseif ($shareNode->getMimePart() === 'audio') { |
|
| 244 | + $audio = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadshare', ['token' => $token]); |
|
| 245 | + Util::addHeader('meta', ['property' => 'og:audio', 'content' => $audio]); |
|
| 246 | + Util::addHeader('meta', ['property' => 'og:audio:type', 'content' => $shareNode->getMimeType()]); |
|
| 247 | + } elseif ($shareNode->getMimePart() === 'video') { |
|
| 248 | + $video = $this->urlGenerator->linkToRouteAbsolute('files_sharing.sharecontroller.downloadshare', ['token' => $token]); |
|
| 249 | + Util::addHeader('meta', ['property' => 'og:video', 'content' => $video]); |
|
| 250 | + Util::addHeader('meta', ['property' => 'og:video:type', 'content' => $shareNode->getMimeType()]); |
|
| 251 | + } |
|
| 252 | 252 | |
| 253 | 253 | |
| 254 | - // Twitter Support: https://developer.x.com/en/docs/x-for-websites/cards/overview/markup |
|
| 255 | - Util::addHeader('meta', ['property' => 'twitter:title', 'content' => $title]); |
|
| 256 | - Util::addHeader('meta', ['property' => 'twitter:description', 'content' => $description]); |
|
| 257 | - Util::addHeader('meta', ['property' => 'twitter:card', 'content' => $hasImagePreview ? 'summary_large_image' : 'summary']); |
|
| 258 | - Util::addHeader('meta', ['property' => 'twitter:image', 'content' => $ogPreview]); |
|
| 259 | - } |
|
| 254 | + // Twitter Support: https://developer.x.com/en/docs/x-for-websites/cards/overview/markup |
|
| 255 | + Util::addHeader('meta', ['property' => 'twitter:title', 'content' => $title]); |
|
| 256 | + Util::addHeader('meta', ['property' => 'twitter:description', 'content' => $description]); |
|
| 257 | + Util::addHeader('meta', ['property' => 'twitter:card', 'content' => $hasImagePreview ? 'summary_large_image' : 'summary']); |
|
| 258 | + Util::addHeader('meta', ['property' => 'twitter:image', 'content' => $ogPreview]); |
|
| 259 | + } |
|
| 260 | 260 | } |
@@ -153,7 +153,7 @@ discard block |
||
| 153 | 153 | ]); |
| 154 | 154 | |
| 155 | 155 | // If not a file drop, then add the download header action |
| 156 | - $headerActions[] = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 0, (string)$shareNode->getSize()); |
|
| 156 | + $headerActions[] = new SimpleMenuAction('download', $this->l10n->t('Download'), 'icon-download', $downloadUrl, 0, (string) $shareNode->getSize()); |
|
| 157 | 157 | |
| 158 | 158 | // If remote sharing is enabled also add the remote share action to the menu |
| 159 | 159 | if ($this->federatedShareProvider->isOutgoingServer2serverShareEnabled()) { |
@@ -229,7 +229,7 @@ discard block |
||
| 229 | 229 | |
| 230 | 230 | $title = $shareNode->getName(); |
| 231 | 231 | $siteName = $this->defaults->getName(); |
| 232 | - $description = $siteName . ($this->defaults->getSlogan() !== '' ? ' - ' . $this->defaults->getSlogan() : ''); |
|
| 232 | + $description = $siteName.($this->defaults->getSlogan() !== '' ? ' - '.$this->defaults->getSlogan() : ''); |
|
| 233 | 233 | |
| 234 | 234 | // OpenGraph Support: http://ogp.me/ |
| 235 | 235 | Util::addHeader('meta', ['property' => 'og:title', 'content' => $title]); |