DavUtil.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2022 Côme Chilliet <come.chilliet@nextcloud.com>
  4. *
  5. * @author Arthur Schiwon <blizzz@owncloud.com>
  6. * @author Bart Visscher <bartv@thisnet.nl>
  7. * @author Jakob Sack <mail@jakobsack.de>
  8. * @author Jörn Friedrich Dreyer <jfd@butonic.de>
  9. * @author Klaas Freitag <freitag@owncloud.com>
  10. * @author Markus Goetz <markus@woboq.com>
  11. * @author Morris Jobke <hey@morrisjobke.de>
  12. * @author Robin Appelman <icewind@owncloud.com>
  13. * @author Thomas Müller <thomas.mueller@tmit.eu>
  14. * @author Vincent Petry <pvince81@owncloud.com>
  15. * @author Côme Chilliet <come.chilliet@nextcloud.com>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OCP\Files;
  33. use OCP\Constants;
  34. use OCP\Files\Mount\IMovableMount;
  35. /**
  36. * This class provides different helper functions related to WebDAV protocol
  37. *
  38. * @since 25.0.0
  39. */
  40. class DavUtil {
  41. /**
  42. * Compute the fileId to use for dav responses
  43. *
  44. * @param int $id Id of the file returned by FileInfo::getId
  45. * @since 25.0.0
  46. */
  47. public static function getDavFileId(int $id): string {
  48. $instanceId = \OC_Util::getInstanceId();
  49. $id = sprintf('%08d', $id);
  50. return $id . $instanceId;
  51. }
  52. /**
  53. * Compute the format needed for returning permissions for dav
  54. *
  55. * @since 25.0.0
  56. */
  57. public static function getDavPermissions(FileInfo $info): string {
  58. $permissions = $info->getPermissions();
  59. $p = '';
  60. if ($info->isShared()) {
  61. $p .= 'S';
  62. }
  63. if ($permissions & Constants::PERMISSION_SHARE) {
  64. $p .= 'R';
  65. }
  66. if ($info->isMounted()) {
  67. $p .= 'M';
  68. }
  69. if ($permissions & Constants::PERMISSION_READ) {
  70. $p .= 'G';
  71. }
  72. if ($permissions & Constants::PERMISSION_DELETE) {
  73. $p .= 'D';
  74. }
  75. if ($permissions & Constants::PERMISSION_UPDATE) {
  76. $p .= 'NV'; // Renameable, Movable
  77. }
  78. // since we always add update permissions for the root of movable mounts
  79. // we need to check the shared cache item directly to determine if it's writable
  80. $storage = $info->getStorage();
  81. if ($info->getInternalPath() === '' && $info->getMountPoint() instanceof IMovableMount) {
  82. $rootEntry = $storage->getCache()->get('');
  83. $isWritable = $rootEntry->getPermissions() & Constants::PERMISSION_UPDATE;
  84. } else {
  85. $isWritable = $permissions & Constants::PERMISSION_UPDATE;
  86. }
  87. if ($info->getType() === FileInfo::TYPE_FILE) {
  88. if ($isWritable) {
  89. $p .= 'W';
  90. }
  91. } else {
  92. if ($permissions & Constants::PERMISSION_CREATE) {
  93. $p .= 'CK';
  94. }
  95. }
  96. return $p;
  97. }
  98. }