DavUtil.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 OC\Files\Mount\MoveableMount;
  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. $p = '';
  59. if ($info->isShared()) {
  60. $p .= 'S';
  61. }
  62. if ($info->isShareable()) {
  63. $p .= 'R';
  64. }
  65. if ($info->isMounted()) {
  66. $p .= 'M';
  67. }
  68. if ($info->isReadable()) {
  69. $p .= 'G';
  70. }
  71. if ($info->isDeletable()) {
  72. $p .= 'D';
  73. }
  74. if ($info->isUpdateable()) {
  75. $p .= 'NV'; // Renameable, Movable
  76. }
  77. // since we always add update permissions for the root of movable mounts
  78. // we need to check the shared cache item directly to determine if it's writable
  79. $storage = $info->getStorage();
  80. if ($info->getInternalPath() === '' && $info->getMountPoint() instanceof MoveableMount) {
  81. $rootEntry = $storage->getCache()->get('');
  82. $isWritable = $rootEntry->getPermissions() & Constants::PERMISSION_UPDATE;
  83. } else {
  84. $isWritable = $info->isUpdateable();
  85. }
  86. if ($info->getType() === FileInfo::TYPE_FILE) {
  87. if ($isWritable) {
  88. $p .= 'W';
  89. }
  90. } else {
  91. if ($info->isCreatable()) {
  92. $p .= 'CK';
  93. }
  94. }
  95. return $p;
  96. }
  97. }