DavUtil.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. namespace OCP\Files;
  7. use OCP\Constants;
  8. use OCP\Files\Mount\IMovableMount;
  9. /**
  10. * This class provides different helper functions related to WebDAV protocol
  11. *
  12. * @since 25.0.0
  13. */
  14. class DavUtil {
  15. /**
  16. * Compute the fileId to use for dav responses
  17. *
  18. * @param int $id Id of the file returned by FileInfo::getId
  19. * @since 25.0.0
  20. */
  21. public static function getDavFileId(int $id): string {
  22. $instanceId = \OC_Util::getInstanceId();
  23. $id = sprintf('%08d', $id);
  24. return $id . $instanceId;
  25. }
  26. /**
  27. * Compute the format needed for returning permissions for dav
  28. *
  29. * @since 25.0.0
  30. */
  31. public static function getDavPermissions(FileInfo $info): string {
  32. $permissions = $info->getPermissions();
  33. $p = '';
  34. if ($info->isShared()) {
  35. $p .= 'S';
  36. }
  37. if ($permissions & Constants::PERMISSION_SHARE) {
  38. $p .= 'R';
  39. }
  40. if ($info->isMounted()) {
  41. $p .= 'M';
  42. }
  43. if ($permissions & Constants::PERMISSION_READ) {
  44. $p .= 'G';
  45. }
  46. if ($permissions & Constants::PERMISSION_DELETE) {
  47. $p .= 'D';
  48. }
  49. if ($permissions & Constants::PERMISSION_UPDATE) {
  50. $p .= 'NV'; // Renameable, Movable
  51. }
  52. // since we always add update permissions for the root of movable mounts
  53. // we need to check the shared cache item directly to determine if it's writable
  54. $storage = $info->getStorage();
  55. if ($info->getInternalPath() === '' && $info->getMountPoint() instanceof IMovableMount) {
  56. $rootEntry = $storage->getCache()->get('');
  57. $isWritable = $rootEntry->getPermissions() & Constants::PERMISSION_UPDATE;
  58. } else {
  59. $isWritable = $permissions & Constants::PERMISSION_UPDATE;
  60. }
  61. if ($info->getType() === FileInfo::TYPE_FILE) {
  62. if ($isWritable) {
  63. $p .= 'W';
  64. }
  65. } else {
  66. if ($permissions & Constants::PERMISSION_CREATE) {
  67. $p .= 'CK';
  68. }
  69. }
  70. return $p;
  71. }
  72. }