CalendarObject.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\DAV\CalDAV;
  8. use OCP\IL10N;
  9. use Sabre\VObject\Component;
  10. use Sabre\VObject\Property;
  11. use Sabre\VObject\Reader;
  12. class CalendarObject extends \Sabre\CalDAV\CalendarObject {
  13. /** @var IL10N */
  14. protected $l10n;
  15. /**
  16. * CalendarObject constructor.
  17. *
  18. * @param CalDavBackend $caldavBackend
  19. * @param IL10N $l10n
  20. * @param array $calendarInfo
  21. * @param array $objectData
  22. */
  23. public function __construct(CalDavBackend $caldavBackend, IL10N $l10n,
  24. array $calendarInfo,
  25. array $objectData) {
  26. parent::__construct($caldavBackend, $calendarInfo, $objectData);
  27. if ($this->isShared()) {
  28. unset($this->objectData['size']);
  29. }
  30. $this->l10n = $l10n;
  31. }
  32. /**
  33. * @inheritdoc
  34. */
  35. public function get() {
  36. $data = parent::get();
  37. if (!$this->isShared()) {
  38. return $data;
  39. }
  40. $vObject = Reader::read($data);
  41. // remove VAlarms if calendar is shared read-only
  42. if (!$this->canWrite()) {
  43. $this->removeVAlarms($vObject);
  44. }
  45. // shows as busy if event is declared confidential
  46. if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
  47. $this->createConfidentialObject($vObject);
  48. }
  49. return $vObject->serialize();
  50. }
  51. public function getId(): int {
  52. return (int) $this->objectData['id'];
  53. }
  54. protected function isShared() {
  55. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  56. return false;
  57. }
  58. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  59. }
  60. /**
  61. * @param Component\VCalendar $vObject
  62. * @return void
  63. */
  64. private function createConfidentialObject(Component\VCalendar $vObject): void {
  65. /** @var Component $vElement */
  66. $vElements = array_filter($vObject->getComponents(), static function ($vElement) {
  67. return $vElement instanceof Component\VEvent || $vElement instanceof Component\VJournal || $vElement instanceof Component\VTodo;
  68. });
  69. foreach ($vElements as $vElement) {
  70. if (empty($vElement->select('SUMMARY'))) {
  71. $vElement->add('SUMMARY', $this->l10n->t('Busy')); // This is needed to mask "Untitled Event" events
  72. }
  73. foreach ($vElement->children() as &$property) {
  74. /** @var Property $property */
  75. switch ($property->name) {
  76. case 'CREATED':
  77. case 'DTSTART':
  78. case 'RRULE':
  79. case 'RECURRENCE-ID':
  80. case 'RDATE':
  81. case 'DURATION':
  82. case 'DTEND':
  83. case 'CLASS':
  84. case 'EXRULE':
  85. case 'EXDATE':
  86. case 'UID':
  87. break;
  88. case 'SUMMARY':
  89. $property->setValue($this->l10n->t('Busy'));
  90. break;
  91. default:
  92. $vElement->__unset($property->name);
  93. unset($property);
  94. break;
  95. }
  96. }
  97. }
  98. }
  99. /**
  100. * @param Component\VCalendar $vObject
  101. * @return void
  102. */
  103. private function removeVAlarms(Component\VCalendar $vObject) {
  104. $subcomponents = $vObject->getComponents();
  105. foreach ($subcomponents as $subcomponent) {
  106. unset($subcomponent->VALARM);
  107. }
  108. }
  109. /**
  110. * @return bool
  111. */
  112. private function canWrite() {
  113. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  114. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  115. }
  116. return true;
  117. }
  118. public function getCalendarId(): int {
  119. return (int)$this->objectData['calendarid'];
  120. }
  121. public function getPrincipalUri(): string {
  122. return $this->calendarInfo['principaluri'];
  123. }
  124. public function getOwner(): ?string {
  125. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  126. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  127. }
  128. return parent::getOwner();
  129. }
  130. }