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.

CalendarObject.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2023 Ferdinand Thiessen <opensource@fthiessen.de>
  5. *
  6. * @author Ferdinand Thiessen <opensource@fthiessen.de>
  7. *
  8. * @license AGPL-3.0-or-later
  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\AppCalendar;
  25. use OCP\Calendar\ICalendar;
  26. use OCP\Calendar\ICreateFromString;
  27. use OCP\Constants;
  28. use Sabre\CalDAV\ICalendarObject;
  29. use Sabre\DAV\Exception\Forbidden;
  30. use Sabre\DAV\Exception\NotFound;
  31. use Sabre\DAVACL\IACL;
  32. use Sabre\VObject\Component\VCalendar;
  33. use Sabre\VObject\Property\ICalendar\DateTime;
  34. class CalendarObject implements ICalendarObject, IACL {
  35. private VCalendar $vobject;
  36. private AppCalendar $calendar;
  37. private ICalendar|ICreateFromString $backend;
  38. public function __construct(AppCalendar $calendar, ICalendar $backend, VCalendar $vobject) {
  39. $this->backend = $backend;
  40. $this->calendar = $calendar;
  41. $this->vobject = $vobject;
  42. }
  43. public function getOwner() {
  44. return $this->calendar->getOwner();
  45. }
  46. public function getGroup() {
  47. return $this->calendar->getGroup();
  48. }
  49. public function getACL(): array {
  50. $acl = [
  51. [
  52. 'privilege' => '{DAV:}read',
  53. 'principal' => $this->getOwner(),
  54. 'protected' => true,
  55. ]
  56. ];
  57. if ($this->calendar->getPermissions() & Constants::PERMISSION_UPDATE) {
  58. $acl[] = [
  59. 'privilege' => '{DAV:}write-content',
  60. 'principal' => $this->getOwner(),
  61. 'protected' => true,
  62. ];
  63. }
  64. return $acl;
  65. }
  66. public function setACL(array $acl): void {
  67. throw new Forbidden('Setting ACL is not supported on this node');
  68. }
  69. public function getSupportedPrivilegeSet(): ?array {
  70. return null;
  71. }
  72. public function put($data): void {
  73. if ($this->backend instanceof ICreateFromString && $this->calendar->getPermissions() & Constants::PERMISSION_UPDATE) {
  74. if (is_resource($data)) {
  75. $data = stream_get_contents($data) ?: '';
  76. }
  77. $this->backend->createFromString($this->getName(), $data);
  78. } else {
  79. throw new Forbidden('This calendar-object is read-only');
  80. }
  81. }
  82. public function get(): string {
  83. return $this->vobject->serialize();
  84. }
  85. public function getContentType(): string {
  86. return 'text/calendar; charset=utf-8';
  87. }
  88. public function getETag(): ?string {
  89. return null;
  90. }
  91. public function getSize() {
  92. return mb_strlen($this->vobject->serialize());
  93. }
  94. public function delete(): void {
  95. if ($this->backend instanceof ICreateFromString && $this->calendar->getPermissions() & Constants::PERMISSION_DELETE) {
  96. /** @var \Sabre\VObject\Component[] */
  97. $components = $this->vobject->getBaseComponents();
  98. foreach ($components as $key => $component) {
  99. $components[$key]->STATUS = 'CANCELLED';
  100. $components[$key]->SEQUENCE = isset($component->SEQUENCE) ? ((int)$component->SEQUENCE->getValue()) + 1 : 1;
  101. if ($component->name === 'VEVENT') {
  102. $components[$key]->METHOD = 'CANCEL';
  103. }
  104. }
  105. $this->backend->createFromString($this->getName(), (new VCalendar($components))->serialize());
  106. } else {
  107. throw new Forbidden('This calendar-object is read-only');
  108. }
  109. }
  110. public function getName(): string {
  111. // Every object is required to have an UID
  112. $base = $this->vobject->getBaseComponent();
  113. // This should never happen except the app provides invalid calendars (VEvent, VTodo... all require UID to be present)
  114. if ($base === null) {
  115. throw new NotFound('Invalid node');
  116. }
  117. if (isset($base->{'X-FILENAME'})) {
  118. return (string)$base->{'X-FILENAME'};
  119. }
  120. return (string)$base->UID . '.ics';
  121. }
  122. public function setName($name): void {
  123. throw new Forbidden('This calendar-object is read-only');
  124. }
  125. public function getLastModified(): ?int {
  126. $base = $this->vobject->getBaseComponent();
  127. if ($base !== null && $this->vobject->getBaseComponent()->{'LAST-MODIFIED'}) {
  128. /** @var DateTime */
  129. $lastModified = $this->vobject->getBaseComponent()->{'LAST-MODIFIED'};
  130. return $lastModified->getDateTime()->getTimestamp();
  131. }
  132. return null;
  133. }
  134. }