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 3.7KB

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