CalendarObject.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2017, Georg Ehrke
  5. * @copyright Copyright (c) 2020, Gary Kim <gary@garykim.dev>
  6. *
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Gary Kim <gary@garykim.dev>
  9. * @author Georg Ehrke <oc.list@georgehrke.com>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\CalDAV;
  28. use OCP\IL10N;
  29. use Sabre\VObject\Component;
  30. use Sabre\VObject\Property;
  31. use Sabre\VObject\Reader;
  32. class CalendarObject extends \Sabre\CalDAV\CalendarObject {
  33. /** @var IL10N */
  34. protected $l10n;
  35. /**
  36. * CalendarObject constructor.
  37. *
  38. * @param CalDavBackend $caldavBackend
  39. * @param IL10N $l10n
  40. * @param array $calendarInfo
  41. * @param array $objectData
  42. */
  43. public function __construct(CalDavBackend $caldavBackend, IL10N $l10n,
  44. array $calendarInfo,
  45. array $objectData) {
  46. parent::__construct($caldavBackend, $calendarInfo, $objectData);
  47. if ($this->isShared()) {
  48. unset($this->objectData['size']);
  49. }
  50. $this->l10n = $l10n;
  51. }
  52. /**
  53. * @inheritdoc
  54. */
  55. public function get() {
  56. $data = parent::get();
  57. if (!$this->isShared()) {
  58. return $data;
  59. }
  60. $vObject = Reader::read($data);
  61. // remove VAlarms if calendar is shared read-only
  62. if (!$this->canWrite()) {
  63. $this->removeVAlarms($vObject);
  64. }
  65. // shows as busy if event is declared confidential
  66. if ($this->objectData['classification'] === CalDavBackend::CLASSIFICATION_CONFIDENTIAL) {
  67. $this->createConfidentialObject($vObject);
  68. }
  69. return $vObject->serialize();
  70. }
  71. public function getId(): int {
  72. return (int) $this->objectData['id'];
  73. }
  74. protected function isShared() {
  75. if (!isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  76. return false;
  77. }
  78. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'] !== $this->calendarInfo['principaluri'];
  79. }
  80. /**
  81. * @param Component\VCalendar $vObject
  82. * @return void
  83. */
  84. private function createConfidentialObject(Component\VCalendar $vObject) {
  85. /** @var Component $vElement */
  86. $vElement = null;
  87. if (isset($vObject->VEVENT)) {
  88. $vElement = $vObject->VEVENT;
  89. }
  90. if (isset($vObject->VJOURNAL)) {
  91. $vElement = $vObject->VJOURNAL;
  92. }
  93. if (isset($vObject->VTODO)) {
  94. $vElement = $vObject->VTODO;
  95. }
  96. if (!is_null($vElement)) {
  97. foreach ($vElement->children() as &$property) {
  98. /** @var Property $property */
  99. switch ($property->name) {
  100. case 'CREATED':
  101. case 'DTSTART':
  102. case 'RRULE':
  103. case 'DURATION':
  104. case 'DTEND':
  105. case 'CLASS':
  106. case 'UID':
  107. break;
  108. case 'SUMMARY':
  109. $property->setValue($this->l10n->t('Busy'));
  110. break;
  111. default:
  112. $vElement->__unset($property->name);
  113. unset($property);
  114. break;
  115. }
  116. }
  117. }
  118. }
  119. /**
  120. * @param Component\VCalendar $vObject
  121. * @return void
  122. */
  123. private function removeVAlarms(Component\VCalendar $vObject) {
  124. $subcomponents = $vObject->getComponents();
  125. foreach ($subcomponents as $subcomponent) {
  126. unset($subcomponent->VALARM);
  127. }
  128. }
  129. /**
  130. * @return bool
  131. */
  132. private function canWrite() {
  133. if (isset($this->calendarInfo['{http://owncloud.org/ns}read-only'])) {
  134. return !$this->calendarInfo['{http://owncloud.org/ns}read-only'];
  135. }
  136. return true;
  137. }
  138. public function getCalendarId(): int {
  139. return (int)$this->objectData['calendarid'];
  140. }
  141. public function getPrincipalUri(): string {
  142. return $this->calendarInfo['principaluri'];
  143. }
  144. public function getOwner(): ?string {
  145. if (isset($this->calendarInfo['{http://owncloud.org/ns}owner-principal'])) {
  146. return $this->calendarInfo['{http://owncloud.org/ns}owner-principal'];
  147. }
  148. return parent::getOwner();
  149. }
  150. }