You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Plugin.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2021 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\DAV\CalDAV\Trashbin;
  25. use Closure;
  26. use DateTimeImmutable;
  27. use DateTimeInterface;
  28. use OCA\DAV\CalDAV\Calendar;
  29. use OCA\DAV\CalDAV\RetentionService;
  30. use OCP\IRequest;
  31. use Sabre\DAV\Exception\NotFound;
  32. use Sabre\DAV\INode;
  33. use Sabre\DAV\PropFind;
  34. use Sabre\DAV\Server;
  35. use Sabre\DAV\ServerPlugin;
  36. use Sabre\HTTP\RequestInterface;
  37. use Sabre\HTTP\ResponseInterface;
  38. use function array_slice;
  39. use function implode;
  40. class Plugin extends ServerPlugin {
  41. public const PROPERTY_DELETED_AT = '{http://nextcloud.com/ns}deleted-at';
  42. public const PROPERTY_CALENDAR_URI = '{http://nextcloud.com/ns}calendar-uri';
  43. public const PROPERTY_RETENTION_DURATION = '{http://nextcloud.com/ns}trash-bin-retention-duration';
  44. /** @var bool */
  45. private $disableTrashbin;
  46. /** @var RetentionService */
  47. private $retentionService;
  48. /** @var Server */
  49. private $server;
  50. public function __construct(IRequest $request,
  51. RetentionService $retentionService) {
  52. $this->disableTrashbin = $request->getHeader('X-NC-CalDAV-No-Trashbin') === '1';
  53. $this->retentionService = $retentionService;
  54. }
  55. public function initialize(Server $server): void {
  56. $this->server = $server;
  57. $server->on('beforeMethod:*', [$this, 'beforeMethod']);
  58. $server->on('propFind', Closure::fromCallable([$this, 'propFind']));
  59. }
  60. public function beforeMethod(RequestInterface $request, ResponseInterface $response): void {
  61. if (!$this->disableTrashbin) {
  62. return;
  63. }
  64. $path = $request->getPath();
  65. $pathParts = explode('/', ltrim($path, '/'));
  66. if (\count($pathParts) < 3) {
  67. // We are looking for a path like calendars/username/calendarname
  68. return;
  69. }
  70. // $calendarPath will look like calendars/username/calendarname
  71. $calendarPath = implode(
  72. '/',
  73. array_slice($pathParts, 0, 3)
  74. );
  75. try {
  76. $calendar = $this->server->tree->getNodeForPath($calendarPath);
  77. if (!($calendar instanceof Calendar)) {
  78. // This is odd
  79. return;
  80. }
  81. /** @var Calendar $calendar */
  82. $calendar->disableTrashbin();
  83. } catch (NotFound $ex) {
  84. return;
  85. }
  86. }
  87. private function propFind(
  88. PropFind $propFind,
  89. INode $node): void {
  90. if ($node instanceof DeletedCalendarObject) {
  91. $propFind->handle(self::PROPERTY_DELETED_AT, function () use ($node) {
  92. $ts = $node->getDeletedAt();
  93. if ($ts === null) {
  94. return null;
  95. }
  96. return (new DateTimeImmutable())
  97. ->setTimestamp($ts)
  98. ->format(DateTimeInterface::ATOM);
  99. });
  100. $propFind->handle(self::PROPERTY_CALENDAR_URI, function () use ($node) {
  101. return $node->getCalendarUri();
  102. });
  103. }
  104. if ($node instanceof TrashbinHome) {
  105. $propFind->handle(self::PROPERTY_RETENTION_DURATION, function () use ($node) {
  106. return $this->retentionService->getDuration();
  107. });
  108. }
  109. }
  110. public function getFeatures(): array {
  111. return ['nc-calendar-trashbin'];
  112. }
  113. public function getPluginName(): string {
  114. return 'nc-calendar-trashbin';
  115. }
  116. }